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 "rpc_client/cli_samr.h"
28 #include "rpc_client/init_samr.h"
29 #include "../librpc/gen_ndr/cli_lsa.h"
30 #include "rpc_client/cli_lsarpc.h"
31 #include "../librpc/gen_ndr/cli_netlogon.h"
32 #include "../librpc/gen_ndr/cli_srvsvc.h"
33 #include "../librpc/gen_ndr/cli_spoolss.h"
34 #include "../librpc/gen_ndr/cli_initshutdown.h"
35 #include "../librpc/gen_ndr/cli_winreg.h"
37 #include "lib/netapi/netapi.h"
39 static int net_mode_share
;
40 static bool sync_files(struct copy_clistate
*cp_clistate
, const char *mask
);
45 * @brief RPC based subcommands for the 'net' utility.
47 * This file should contain much of the functionality that used to
48 * be found in rpcclient, execpt that the commands should change
49 * less often, and the fucntionality should be sane (the user is not
50 * expected to know a rid/sid before they conduct an operation etc.)
52 * @todo Perhaps eventually these should be split out into a number
53 * of files, as this could get quite big.
58 * Many of the RPC functions need the domain sid. This function gets
59 * it at the start of every run
61 * @param cli A cli_state already connected to the remote machine
63 * @return The Domain SID of the remote machine.
66 NTSTATUS
net_get_remote_domain_sid(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
67 struct dom_sid
**domain_sid
,
68 const char **domain_name
)
70 struct rpc_pipe_client
*lsa_pipe
= NULL
;
71 struct policy_handle pol
;
72 NTSTATUS result
= NT_STATUS_OK
;
73 union lsa_PolicyInformation
*info
= NULL
;
75 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
77 if (!NT_STATUS_IS_OK(result
)) {
78 d_fprintf(stderr
, _("Could not initialise lsa pipe\n"));
82 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, false,
83 SEC_FLAG_MAXIMUM_ALLOWED
,
85 if (!NT_STATUS_IS_OK(result
)) {
86 d_fprintf(stderr
, "open_policy %s: %s\n",
92 result
= rpccli_lsa_QueryInfoPolicy(lsa_pipe
, mem_ctx
,
94 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
96 if (!NT_STATUS_IS_OK(result
)) {
97 d_fprintf(stderr
, "lsaquery %s: %s\n",
103 *domain_name
= info
->account_domain
.name
.string
;
104 *domain_sid
= info
->account_domain
.sid
;
106 rpccli_lsa_Close(lsa_pipe
, mem_ctx
, &pol
);
107 TALLOC_FREE(lsa_pipe
);
113 * Run a single RPC command, from start to finish.
115 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
116 * @param conn_flag a NET_FLAG_ combination. Passed to
117 * net_make_ipc_connection.
118 * @param argc Standard main() style argc.
119 * @param argv Standard main() style argv. Initial components are already
121 * @return A shell status integer (0 for success).
124 int run_rpc_command(struct net_context
*c
,
125 struct cli_state
*cli_arg
,
126 const struct ndr_syntax_id
*interface
,
132 struct cli_state
*cli
= NULL
;
133 struct rpc_pipe_client
*pipe_hnd
= NULL
;
136 struct dom_sid
*domain_sid
;
137 const char *domain_name
;
140 /* make use of cli_state handed over as an argument, if possible */
142 nt_status
= net_make_ipc_connection(c
, conn_flags
, &cli
);
143 if (!NT_STATUS_IS_OK(nt_status
)) {
144 DEBUG(1, ("failed to make ipc connection: %s\n",
145 nt_errstr(nt_status
)));
158 if (!(mem_ctx
= talloc_init("run_rpc_command"))) {
159 DEBUG(0, ("talloc_init() failed\n"));
163 nt_status
= net_get_remote_domain_sid(cli
, mem_ctx
, &domain_sid
,
165 if (!NT_STATUS_IS_OK(nt_status
)) {
169 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
170 if (lp_client_schannel()
171 && (ndr_syntax_id_equal(interface
,
172 &ndr_table_netlogon
.syntax_id
))) {
173 /* Always try and create an schannel netlogon pipe. */
174 nt_status
= cli_rpc_pipe_open_schannel(
175 cli
, interface
, NCACN_NP
,
176 DCERPC_AUTH_LEVEL_PRIVACY
, domain_name
,
178 if (!NT_STATUS_IS_OK(nt_status
)) {
179 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
180 nt_errstr(nt_status
) ));
184 if (conn_flags
& NET_FLAGS_SEAL
) {
185 nt_status
= cli_rpc_pipe_open_ntlmssp(
187 (conn_flags
& NET_FLAGS_TCP
) ?
188 NCACN_IP_TCP
: NCACN_NP
,
189 DCERPC_AUTH_LEVEL_PRIVACY
,
190 lp_workgroup(), c
->opt_user_name
,
191 c
->opt_password
, &pipe_hnd
);
193 nt_status
= cli_rpc_pipe_open_noauth(
197 if (!NT_STATUS_IS_OK(nt_status
)) {
198 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
199 get_pipe_name_from_syntax(
200 talloc_tos(), interface
),
201 nt_errstr(nt_status
) ));
207 nt_status
= fn(c
, domain_sid
, domain_name
, cli
, pipe_hnd
, mem_ctx
, argc
, argv
);
209 if (!NT_STATUS_IS_OK(nt_status
)) {
210 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status
)));
213 DEBUG(5, ("rpc command function succedded\n"));
216 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
218 TALLOC_FREE(pipe_hnd
);
223 /* close the connection only if it was opened here */
228 talloc_destroy(mem_ctx
);
233 * Force a change of the trust acccount password.
235 * All parameters are provided by the run_rpc_command function, except for
236 * argc, argv which are passed through.
238 * @param domain_sid The domain sid acquired from the remote server.
239 * @param cli A cli_state connected to the server.
240 * @param mem_ctx Talloc context, destroyed on completion of the function.
241 * @param argc Standard main() style argc.
242 * @param argv Standard main() style argv. Initial components are already
245 * @return Normal NTSTATUS return.
248 static NTSTATUS
rpc_changetrustpw_internals(struct net_context
*c
,
249 const struct dom_sid
*domain_sid
,
250 const char *domain_name
,
251 struct cli_state
*cli
,
252 struct rpc_pipe_client
*pipe_hnd
,
259 status
= trust_pw_find_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
);
260 if (!NT_STATUS_IS_OK(status
)) {
261 d_fprintf(stderr
, _("Failed to change machine account password: %s\n"),
270 * Force a change of the trust acccount password.
272 * @param argc Standard main() style argc.
273 * @param argv Standard main() style argv. Initial components are already
276 * @return A shell status integer (0 for success).
279 int net_rpc_changetrustpw(struct net_context
*c
, int argc
, const char **argv
)
281 if (c
->display_usage
) {
283 "net rpc changetrustpw\n"
286 _("Change the machine trust password"));
290 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
291 NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
292 rpc_changetrustpw_internals
,
297 * Join a domain, the old way.
299 * This uses 'machinename' as the inital password, and changes it.
301 * The password should be created with 'server manager' or equiv first.
303 * All parameters are provided by the run_rpc_command function, except for
304 * argc, argv which are passed through.
306 * @param domain_sid The domain sid acquired from the remote server.
307 * @param cli A cli_state connected to the server.
308 * @param mem_ctx Talloc context, destroyed on completion of the function.
309 * @param argc Standard main() style argc.
310 * @param argv Standard main() style argv. Initial components are already
313 * @return Normal NTSTATUS return.
316 static NTSTATUS
rpc_oldjoin_internals(struct net_context
*c
,
317 const struct dom_sid
*domain_sid
,
318 const char *domain_name
,
319 struct cli_state
*cli
,
320 struct rpc_pipe_client
*pipe_hnd
,
326 fstring trust_passwd
;
327 unsigned char orig_trust_passwd_hash
[16];
329 enum netr_SchannelType sec_channel_type
;
331 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
333 if (!NT_STATUS_IS_OK(result
)) {
334 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
337 nt_errstr(result
) ));
342 check what type of join - if the user want's to join as
343 a BDC, the server must agree that we are a BDC.
346 sec_channel_type
= get_sec_channel_type(argv
[0]);
348 sec_channel_type
= get_sec_channel_type(NULL
);
351 fstrcpy(trust_passwd
, global_myname());
352 strlower_m(trust_passwd
);
355 * Machine names can be 15 characters, but the max length on
356 * a password is 14. --jerry
359 trust_passwd
[14] = '\0';
361 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
363 result
= trust_pw_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
,
365 orig_trust_passwd_hash
,
368 if (NT_STATUS_IS_OK(result
))
369 printf(_("Joined domain %s.\n"), c
->opt_target_workgroup
);
372 if (!secrets_store_domain_sid(c
->opt_target_workgroup
, domain_sid
)) {
373 DEBUG(0, ("error storing domain sid for %s\n", c
->opt_target_workgroup
));
374 result
= NT_STATUS_UNSUCCESSFUL
;
381 * Join a domain, the old way.
383 * @param argc Standard main() style argc.
384 * @param argv Standard main() style argv. Initial components are already
387 * @return A shell status integer (0 for success).
390 static int net_rpc_perform_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
392 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
393 NET_FLAGS_NO_PIPE
| NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
394 rpc_oldjoin_internals
,
399 * Join a domain, the old way. This function exists to allow
400 * the message to be displayed when oldjoin was explicitly
401 * requested, but not when it was implied by "net rpc join".
403 * @param argc Standard main() style argc.
404 * @param argv Standard main() style argv. Initial components are already
407 * @return A shell status integer (0 for success).
410 static int net_rpc_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
414 if (c
->display_usage
) {
419 _("Join a domain the old way"));
423 rc
= net_rpc_perform_oldjoin(c
, argc
, argv
);
426 d_fprintf(stderr
, _("Failed to join domain\n"));
433 * 'net rpc join' entrypoint.
434 * @param argc Standard main() style argc.
435 * @param argv Standard main() style argv. Initial components are already
438 * Main 'net_rpc_join()' (where the admin username/password is used) is
440 * Try to just change the password, but if that doesn't work, use/prompt
441 * for a username/password.
444 int net_rpc_join(struct net_context
*c
, int argc
, const char **argv
)
446 if (c
->display_usage
) {
449 _("net rpc join -U <username>[%%password] <type>\n"
451 " username\tName of the admin user"
452 " password\tPassword of the admin user, will "
453 "prompt if not specified\n"
454 " type\tCan be one of the following:\n"
455 "\t\tMEMBER\tJoin as member server (default)\n"
456 "\t\tBDC\tJoin as BDC\n"
457 "\t\tPDC\tJoin as PDC\n"));
461 if (lp_server_role() == ROLE_STANDALONE
) {
462 d_printf(_("cannot join as standalone machine\n"));
466 if (strlen(global_myname()) > 15) {
467 d_printf(_("Our netbios name can be at most 15 chars long, "
468 "\"%s\" is %u chars long\n"),
469 global_myname(), (unsigned int)strlen(global_myname()));
473 if ((net_rpc_perform_oldjoin(c
, argc
, argv
) == 0))
476 return net_rpc_join_newstyle(c
, argc
, argv
);
480 * display info about a rpc domain
482 * All parameters are provided by the run_rpc_command function, except for
483 * argc, argv which are passed through.
485 * @param domain_sid The domain sid acquired from the remote server
486 * @param cli A cli_state connected to the server.
487 * @param mem_ctx Talloc context, destroyed on completion of the function.
488 * @param argc Standard main() style argc.
489 * @param argv Standard main() style argv. Initial components are already
492 * @return Normal NTSTATUS return.
495 NTSTATUS
rpc_info_internals(struct net_context
*c
,
496 const struct dom_sid
*domain_sid
,
497 const char *domain_name
,
498 struct cli_state
*cli
,
499 struct rpc_pipe_client
*pipe_hnd
,
504 struct policy_handle connect_pol
, domain_pol
;
505 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
506 union samr_DomainInfo
*info
= NULL
;
509 sid_to_fstring(sid_str
, domain_sid
);
511 /* Get sam policy handle */
512 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
514 MAXIMUM_ALLOWED_ACCESS
,
516 if (!NT_STATUS_IS_OK(result
)) {
517 d_fprintf(stderr
, _("Could not connect to SAM: %s\n"),
522 /* Get domain policy handle */
523 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
525 MAXIMUM_ALLOWED_ACCESS
,
526 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
528 if (!NT_STATUS_IS_OK(result
)) {
529 d_fprintf(stderr
, _("Could not open domain: %s\n"),
534 result
= rpccli_samr_QueryDomainInfo(pipe_hnd
, mem_ctx
,
538 if (NT_STATUS_IS_OK(result
)) {
539 d_printf(_("Domain Name: %s\n"),
540 info
->general
.domain_name
.string
);
541 d_printf(_("Domain SID: %s\n"), sid_str
);
542 d_printf(_("Sequence number: %llu\n"),
543 (unsigned long long)info
->general
.sequence_num
);
544 d_printf(_("Num users: %u\n"), info
->general
.num_users
);
545 d_printf(_("Num domain groups: %u\n"),info
->general
.num_groups
);
546 d_printf(_("Num local groups: %u\n"),info
->general
.num_aliases
);
554 * 'net rpc info' entrypoint.
555 * @param argc Standard main() style argc.
556 * @param argv Standard main() style argv. Initial components are already
560 int net_rpc_info(struct net_context
*c
, int argc
, const char **argv
)
562 if (c
->display_usage
) {
567 _("Display information about the domain"));
571 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
,
572 NET_FLAGS_PDC
, rpc_info_internals
,
577 * Fetch domain SID into the local secrets.tdb.
579 * All parameters are provided by the run_rpc_command function, except for
580 * argc, argv which are passed through.
582 * @param domain_sid The domain sid acquired from the remote server.
583 * @param cli A cli_state connected to the server.
584 * @param mem_ctx Talloc context, destroyed on completion of the function.
585 * @param argc Standard main() style argc.
586 * @param argv Standard main() style argv. Initial components are already
589 * @return Normal NTSTATUS return.
592 static NTSTATUS
rpc_getsid_internals(struct net_context
*c
,
593 const struct dom_sid
*domain_sid
,
594 const char *domain_name
,
595 struct cli_state
*cli
,
596 struct rpc_pipe_client
*pipe_hnd
,
603 sid_to_fstring(sid_str
, domain_sid
);
604 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
605 sid_str
, domain_name
);
607 if (!secrets_store_domain_sid(domain_name
, domain_sid
)) {
608 DEBUG(0,("Can't store domain SID\n"));
609 return NT_STATUS_UNSUCCESSFUL
;
616 * 'net rpc getsid' entrypoint.
617 * @param argc Standard main() style argc.
618 * @param argv Standard main() style argv. Initial components are already
622 int net_rpc_getsid(struct net_context
*c
, int argc
, const char **argv
)
624 int conn_flags
= NET_FLAGS_PDC
;
626 if (!c
->opt_user_specified
) {
627 conn_flags
|= NET_FLAGS_ANONYMOUS
;
630 if (c
->display_usage
) {
635 _("Fetch domain SID into local secrets.tdb"));
639 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
,
641 rpc_getsid_internals
,
645 /****************************************************************************/
648 * Basic usage function for 'net rpc user'.
649 * @param argc Standard main() style argc.
650 * @param argv Standard main() style argv. Initial components are already
654 static int rpc_user_usage(struct net_context
*c
, int argc
, const char **argv
)
656 return net_user_usage(c
, argc
, argv
);
660 * Add a new user to a remote RPC server.
662 * @param argc Standard main() style argc.
663 * @param argv Standard main() style argv. Initial components are already
666 * @return A shell status integer (0 for success).
669 static int rpc_user_add(struct net_context
*c
, int argc
, const char **argv
)
671 NET_API_STATUS status
;
672 struct USER_INFO_1 info1
;
673 uint32_t parm_error
= 0;
675 if (argc
< 1 || c
->display_usage
) {
676 rpc_user_usage(c
, argc
, argv
);
682 info1
.usri1_name
= argv
[0];
684 info1
.usri1_password
= argv
[1];
687 status
= NetUserAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
690 d_fprintf(stderr
,_("Failed to add user '%s' with error: %s.\n"),
691 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
695 d_printf(_("Added user '%s'.\n"), argv
[0]);
702 * Rename a user on a remote RPC server.
704 * @param argc Standard main() style argc.
705 * @param argv Standard main() style argv. Initial components are already
708 * @return A shell status integer (0 for success).
711 static int rpc_user_rename(struct net_context
*c
, int argc
, const char **argv
)
713 NET_API_STATUS status
;
714 struct USER_INFO_0 u0
;
715 uint32_t parm_err
= 0;
717 if (argc
!= 2 || c
->display_usage
) {
718 rpc_user_usage(c
, argc
, argv
);
722 u0
.usri0_name
= argv
[1];
724 status
= NetUserSetInfo(c
->opt_host
, argv
[0],
725 0, (uint8_t *)&u0
, &parm_err
);
728 _("Failed to rename user from %s to %s - %s\n"),
730 libnetapi_get_error_string(c
->netapi_ctx
, status
));
732 d_printf(_("Renamed user from %s to %s\n"), argv
[0], argv
[1]);
739 * Set a user's primary group
741 * @param argc Standard main() style argc.
742 * @param argv Standard main() style argv. Initial components are already
745 * @return A shell status integer (0 for success).
748 static int rpc_user_setprimarygroup(struct net_context
*c
, int argc
,
751 NET_API_STATUS status
;
753 struct GROUP_INFO_2
*g2
;
754 struct USER_INFO_1051 u1051
;
755 uint32_t parm_err
= 0;
757 if (argc
!= 2 || c
->display_usage
) {
758 rpc_user_usage(c
, argc
, argv
);
762 status
= NetGroupGetInfo(c
->opt_host
, argv
[1], 2, &buffer
);
764 d_fprintf(stderr
, _("Failed to find group name %s -- %s\n"),
766 libnetapi_get_error_string(c
->netapi_ctx
, status
));
769 g2
= (struct GROUP_INFO_2
*)buffer
;
771 u1051
.usri1051_primary_group_id
= g2
->grpi2_group_id
;
773 NetApiBufferFree(buffer
);
775 status
= NetUserSetInfo(c
->opt_host
, argv
[0], 1051,
776 (uint8_t *)&u1051
, &parm_err
);
779 _("Failed to set user's primary group %s to %s - "
780 "%s\n"), argv
[0], argv
[1],
781 libnetapi_get_error_string(c
->netapi_ctx
, status
));
783 d_printf(_("Set primary group of user %s to %s\n"), argv
[0],
790 * Delete a user from a remote RPC server.
792 * @param argc Standard main() style argc.
793 * @param argv Standard main() style argv. Initial components are already
796 * @return A shell status integer (0 for success).
799 static int rpc_user_delete(struct net_context
*c
, int argc
, const char **argv
)
801 NET_API_STATUS status
;
803 if (argc
< 1 || c
->display_usage
) {
804 rpc_user_usage(c
, argc
, argv
);
808 status
= NetUserDel(c
->opt_host
, argv
[0]);
811 d_fprintf(stderr
, _("Failed to delete user '%s' with: %s.\n"),
813 libnetapi_get_error_string(c
->netapi_ctx
, status
));
816 d_printf(_("Deleted user '%s'.\n"), argv
[0]);
823 * Set a user's password on a remote RPC server.
825 * @param argc Standard main() style argc.
826 * @param argv Standard main() style argv. Initial components are already
829 * @return A shell status integer (0 for success).
832 static int rpc_user_password(struct net_context
*c
, int argc
, const char **argv
)
834 NET_API_STATUS status
;
836 struct USER_INFO_1003 u1003
;
837 uint32_t parm_err
= 0;
840 if (argc
< 1 || c
->display_usage
) {
841 rpc_user_usage(c
, argc
, argv
);
846 u1003
.usri1003_password
= argv
[1];
848 ret
= asprintf(&prompt
, _("Enter new password for %s:"),
853 u1003
.usri1003_password
= talloc_strdup(c
, getpass(prompt
));
855 if (u1003
.usri1003_password
== NULL
) {
860 status
= NetUserSetInfo(c
->opt_host
, argv
[0], 1003, (uint8_t *)&u1003
, &parm_err
);
862 /* Display results */
865 _("Failed to set password for '%s' with error: %s.\n"),
866 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
875 * List a user's groups from a remote RPC server.
877 * @param argc Standard main() style argc.
878 * @param argv Standard main() style argv. Initial components are already
881 * @return A shell status integer (0 for success)
884 static int rpc_user_info(struct net_context
*c
, int argc
, const char **argv
)
887 NET_API_STATUS status
;
888 struct GROUP_USERS_INFO_0
*u0
= NULL
;
889 uint32_t entries_read
= 0;
890 uint32_t total_entries
= 0;
894 if (argc
< 1 || c
->display_usage
) {
895 rpc_user_usage(c
, argc
, argv
);
899 status
= NetUserGetGroups(c
->opt_host
,
902 (uint8_t **)(void *)&u0
,
908 _("Failed to get groups for '%s' with error: %s.\n"),
909 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
914 for (i
=0; i
< entries_read
; i
++) {
915 printf("%s\n", u0
->grui0_name
);
923 * List users on a remote RPC server.
925 * All parameters are provided by the run_rpc_command function, except for
926 * argc, argv which are passed through.
928 * @param domain_sid The domain sid acquired from the remote server.
929 * @param cli A cli_state connected to the server.
930 * @param mem_ctx Talloc context, destroyed on completion of the function.
931 * @param argc Standard main() style argc.
932 * @param argv Standard main() style argv. Initial components are already
935 * @return Normal NTSTATUS return.
938 static int rpc_user_list(struct net_context
*c
, int argc
, const char **argv
)
940 NET_API_STATUS status
;
941 uint32_t start_idx
=0, num_entries
, i
, loop_count
= 0;
942 struct NET_DISPLAY_USER
*info
= NULL
;
945 /* Query domain users */
946 if (c
->opt_long_list_entries
)
947 d_printf(_("\nUser name Comment"
948 "\n-----------------------------\n"));
950 uint32_t max_entries
, max_size
;
952 get_query_dispinfo_params(
953 loop_count
, &max_entries
, &max_size
);
955 status
= NetQueryDisplayInformation(c
->opt_host
,
962 if (status
!= 0 && status
!= ERROR_MORE_DATA
) {
966 info
= (struct NET_DISPLAY_USER
*)buffer
;
968 for (i
= 0; i
< num_entries
; i
++) {
970 if (c
->opt_long_list_entries
)
971 printf("%-21.21s %s\n", info
->usri1_name
,
972 info
->usri1_comment
);
974 printf("%s\n", info
->usri1_name
);
978 NetApiBufferFree(buffer
);
981 start_idx
+= num_entries
;
983 } while (status
== ERROR_MORE_DATA
);
989 * 'net rpc user' entrypoint.
990 * @param argc Standard main() style argc.
991 * @param argv Standard main() style argv. Initial components are already
995 int net_rpc_user(struct net_context
*c
, int argc
, const char **argv
)
997 NET_API_STATUS status
;
999 struct functable func
[] = {
1004 N_("Add specified user"),
1005 N_("net rpc user add\n"
1006 " Add specified user")
1012 N_("List domain groups of user"),
1013 N_("net rpc user info\n"
1014 " List domain groups of user")
1020 N_("Remove specified user"),
1021 N_("net rpc user delete\n"
1022 " Remove specified user")
1028 N_("Change user password"),
1029 N_("net rpc user password\n"
1030 " Change user password")
1036 N_("Rename specified user"),
1037 N_("net rpc user rename\n"
1038 " Rename specified user")
1042 rpc_user_setprimarygroup
,
1044 "Set a user's primary group",
1045 "net rpc user setprimarygroup\n"
1046 " Set a user's primary group"
1048 {NULL
, NULL
, 0, NULL
, NULL
}
1051 status
= libnetapi_init(&c
->netapi_ctx
);
1055 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
1056 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
1057 if (c
->opt_kerberos
) {
1058 libnetapi_set_use_kerberos(c
->netapi_ctx
);
1062 if (c
->display_usage
) {
1067 _("List all users"));
1068 net_display_usage_from_functable(func
);
1072 return rpc_user_list(c
, argc
, argv
);
1075 return net_run_function(c
, argc
, argv
, "net rpc user", func
);
1078 static NTSTATUS
rpc_sh_user_list(struct net_context
*c
,
1079 TALLOC_CTX
*mem_ctx
,
1080 struct rpc_sh_ctx
*ctx
,
1081 struct rpc_pipe_client
*pipe_hnd
,
1082 int argc
, const char **argv
)
1084 return werror_to_ntstatus(W_ERROR(rpc_user_list(c
, argc
, argv
)));
1087 static NTSTATUS
rpc_sh_user_info(struct net_context
*c
,
1088 TALLOC_CTX
*mem_ctx
,
1089 struct rpc_sh_ctx
*ctx
,
1090 struct rpc_pipe_client
*pipe_hnd
,
1091 int argc
, const char **argv
)
1093 return werror_to_ntstatus(W_ERROR(rpc_user_info(c
, argc
, argv
)));
1096 static NTSTATUS
rpc_sh_handle_user(struct net_context
*c
,
1097 TALLOC_CTX
*mem_ctx
,
1098 struct rpc_sh_ctx
*ctx
,
1099 struct rpc_pipe_client
*pipe_hnd
,
1100 int argc
, const char **argv
,
1102 struct net_context
*c
,
1103 TALLOC_CTX
*mem_ctx
,
1104 struct rpc_sh_ctx
*ctx
,
1105 struct rpc_pipe_client
*pipe_hnd
,
1106 struct policy_handle
*user_hnd
,
1107 int argc
, const char **argv
))
1109 struct policy_handle connect_pol
, domain_pol
, user_pol
;
1110 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1113 enum lsa_SidType type
;
1116 d_fprintf(stderr
, "%s %s <username>\n", _("Usage:"),
1118 return NT_STATUS_INVALID_PARAMETER
;
1121 ZERO_STRUCT(connect_pol
);
1122 ZERO_STRUCT(domain_pol
);
1123 ZERO_STRUCT(user_pol
);
1125 result
= net_rpc_lookup_name(c
, mem_ctx
, rpc_pipe_np_smb_conn(pipe_hnd
),
1126 argv
[0], NULL
, NULL
, &sid
, &type
);
1127 if (!NT_STATUS_IS_OK(result
)) {
1128 d_fprintf(stderr
, _("Could not lookup %s: %s\n"), argv
[0],
1133 if (type
!= SID_NAME_USER
) {
1134 d_fprintf(stderr
, _("%s is a %s, not a user\n"), argv
[0],
1135 sid_type_lookup(type
));
1136 result
= NT_STATUS_NO_SUCH_USER
;
1140 if (!sid_peek_check_rid(ctx
->domain_sid
, &sid
, &rid
)) {
1141 d_fprintf(stderr
, _("%s is not in our domain\n"), argv
[0]);
1142 result
= NT_STATUS_NO_SUCH_USER
;
1146 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1148 MAXIMUM_ALLOWED_ACCESS
,
1150 if (!NT_STATUS_IS_OK(result
)) {
1154 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1156 MAXIMUM_ALLOWED_ACCESS
,
1159 if (!NT_STATUS_IS_OK(result
)) {
1163 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1165 MAXIMUM_ALLOWED_ACCESS
,
1168 if (!NT_STATUS_IS_OK(result
)) {
1172 result
= fn(c
, mem_ctx
, ctx
, pipe_hnd
, &user_pol
, argc
-1, argv
+1);
1175 if (is_valid_policy_hnd(&user_pol
)) {
1176 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1178 if (is_valid_policy_hnd(&domain_pol
)) {
1179 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1181 if (is_valid_policy_hnd(&connect_pol
)) {
1182 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1187 static NTSTATUS
rpc_sh_user_show_internals(struct net_context
*c
,
1188 TALLOC_CTX
*mem_ctx
,
1189 struct rpc_sh_ctx
*ctx
,
1190 struct rpc_pipe_client
*pipe_hnd
,
1191 struct policy_handle
*user_hnd
,
1192 int argc
, const char **argv
)
1195 union samr_UserInfo
*info
= NULL
;
1198 d_fprintf(stderr
, "%s %s show <username>\n", _("Usage:"),
1200 return NT_STATUS_INVALID_PARAMETER
;
1203 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1207 if (!NT_STATUS_IS_OK(result
)) {
1211 d_printf(_("user rid: %d, group rid: %d\n"),
1213 info
->info21
.primary_gid
);
1218 static NTSTATUS
rpc_sh_user_show(struct net_context
*c
,
1219 TALLOC_CTX
*mem_ctx
,
1220 struct rpc_sh_ctx
*ctx
,
1221 struct rpc_pipe_client
*pipe_hnd
,
1222 int argc
, const char **argv
)
1224 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1225 rpc_sh_user_show_internals
);
1228 #define FETCHSTR(name, rec) \
1229 do { if (strequal(ctx->thiscmd, name)) { \
1230 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1233 #define SETSTR(name, rec, flag) \
1234 do { if (strequal(ctx->thiscmd, name)) { \
1235 init_lsa_String(&(info->info21.rec), argv[0]); \
1236 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1239 static NTSTATUS
rpc_sh_user_str_edit_internals(struct net_context
*c
,
1240 TALLOC_CTX
*mem_ctx
,
1241 struct rpc_sh_ctx
*ctx
,
1242 struct rpc_pipe_client
*pipe_hnd
,
1243 struct policy_handle
*user_hnd
,
1244 int argc
, const char **argv
)
1247 const char *username
;
1248 const char *oldval
= "";
1249 union samr_UserInfo
*info
= NULL
;
1252 d_fprintf(stderr
, "%s %s <username> [new value|NULL]\n",
1253 _("Usage:"), ctx
->whoami
);
1254 return NT_STATUS_INVALID_PARAMETER
;
1257 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1261 if (!NT_STATUS_IS_OK(result
)) {
1265 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1267 FETCHSTR("fullname", full_name
);
1268 FETCHSTR("homedir", home_directory
);
1269 FETCHSTR("homedrive", home_drive
);
1270 FETCHSTR("logonscript", logon_script
);
1271 FETCHSTR("profilepath", profile_path
);
1272 FETCHSTR("description", description
);
1275 d_printf(_("%s's %s: [%s]\n"), username
, ctx
->thiscmd
, oldval
);
1279 if (strcmp(argv
[0], "NULL") == 0) {
1283 ZERO_STRUCT(info
->info21
);
1285 SETSTR("fullname", full_name
, FULL_NAME
);
1286 SETSTR("homedir", home_directory
, HOME_DIRECTORY
);
1287 SETSTR("homedrive", home_drive
, HOME_DRIVE
);
1288 SETSTR("logonscript", logon_script
, LOGON_SCRIPT
);
1289 SETSTR("profilepath", profile_path
, PROFILE_PATH
);
1290 SETSTR("description", description
, DESCRIPTION
);
1292 result
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1297 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username
,
1298 ctx
->thiscmd
, oldval
, argv
[0]);
1305 #define HANDLEFLG(name, rec) \
1306 do { if (strequal(ctx->thiscmd, name)) { \
1307 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1309 newflags = oldflags | ACB_##rec; \
1311 newflags = oldflags & ~ACB_##rec; \
1314 static NTSTATUS
rpc_sh_user_str_edit(struct net_context
*c
,
1315 TALLOC_CTX
*mem_ctx
,
1316 struct rpc_sh_ctx
*ctx
,
1317 struct rpc_pipe_client
*pipe_hnd
,
1318 int argc
, const char **argv
)
1320 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1321 rpc_sh_user_str_edit_internals
);
1324 static NTSTATUS
rpc_sh_user_flag_edit_internals(struct net_context
*c
,
1325 TALLOC_CTX
*mem_ctx
,
1326 struct rpc_sh_ctx
*ctx
,
1327 struct rpc_pipe_client
*pipe_hnd
,
1328 struct policy_handle
*user_hnd
,
1329 int argc
, const char **argv
)
1332 const char *username
;
1333 const char *oldval
= "unknown";
1334 uint32 oldflags
, newflags
;
1336 union samr_UserInfo
*info
= NULL
;
1339 ((argc
== 1) && !strequal(argv
[0], "yes") &&
1340 !strequal(argv
[0], "no"))) {
1341 /* TRANSATORS: The yes|no here are program keywords. Please do
1343 d_fprintf(stderr
, _("Usage: %s <username> [yes|no]\n"),
1345 return NT_STATUS_INVALID_PARAMETER
;
1348 newval
= strequal(argv
[0], "yes");
1350 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1354 if (!NT_STATUS_IS_OK(result
)) {
1358 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1359 oldflags
= info
->info21
.acct_flags
;
1360 newflags
= info
->info21
.acct_flags
;
1362 HANDLEFLG("disabled", DISABLED
);
1363 HANDLEFLG("pwnotreq", PWNOTREQ
);
1364 HANDLEFLG("autolock", AUTOLOCK
);
1365 HANDLEFLG("pwnoexp", PWNOEXP
);
1368 d_printf(_("%s's %s flag: %s\n"), username
, ctx
->thiscmd
,
1373 ZERO_STRUCT(info
->info21
);
1375 info
->info21
.acct_flags
= newflags
;
1376 info
->info21
.fields_present
= SAMR_FIELD_ACCT_FLAGS
;
1378 result
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1383 if (NT_STATUS_IS_OK(result
)) {
1384 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username
,
1385 ctx
->thiscmd
, oldval
, argv
[0]);
1393 static NTSTATUS
rpc_sh_user_flag_edit(struct net_context
*c
,
1394 TALLOC_CTX
*mem_ctx
,
1395 struct rpc_sh_ctx
*ctx
,
1396 struct rpc_pipe_client
*pipe_hnd
,
1397 int argc
, const char **argv
)
1399 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1400 rpc_sh_user_flag_edit_internals
);
1403 struct rpc_sh_cmd
*net_rpc_user_edit_cmds(struct net_context
*c
,
1404 TALLOC_CTX
*mem_ctx
,
1405 struct rpc_sh_ctx
*ctx
)
1407 static struct rpc_sh_cmd cmds
[] = {
1409 { "fullname", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1410 N_("Show/Set a user's full name") },
1412 { "homedir", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1413 N_("Show/Set a user's home directory") },
1415 { "homedrive", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1416 N_("Show/Set a user's home drive") },
1418 { "logonscript", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1419 N_("Show/Set a user's logon script") },
1421 { "profilepath", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1422 N_("Show/Set a user's profile path") },
1424 { "description", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1425 N_("Show/Set a user's description") },
1427 { "disabled", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1428 N_("Show/Set whether a user is disabled") },
1430 { "autolock", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1431 N_("Show/Set whether a user locked out") },
1433 { "pwnotreq", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1434 N_("Show/Set whether a user does not need a password") },
1436 { "pwnoexp", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1437 N_("Show/Set whether a user's password does not expire") },
1439 { NULL
, NULL
, 0, NULL
, NULL
}
1445 struct rpc_sh_cmd
*net_rpc_user_cmds(struct net_context
*c
,
1446 TALLOC_CTX
*mem_ctx
,
1447 struct rpc_sh_ctx
*ctx
)
1449 static struct rpc_sh_cmd cmds
[] = {
1451 { "list", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_list
,
1452 N_("List available users") },
1454 { "info", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_info
,
1455 N_("List the domain groups a user is member of") },
1457 { "show", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_show
,
1458 N_("Show info about a user") },
1460 { "edit", net_rpc_user_edit_cmds
, 0, NULL
,
1461 N_("Show/Modify a user's fields") },
1463 { NULL
, NULL
, 0, NULL
, NULL
}
1469 /****************************************************************************/
1472 * Basic usage function for 'net rpc group'.
1473 * @param argc Standard main() style argc.
1474 * @param argv Standard main() style argv. Initial components are already
1478 static int rpc_group_usage(struct net_context
*c
, int argc
, const char **argv
)
1480 return net_group_usage(c
, argc
, argv
);
1484 * Delete group on a remote RPC server.
1486 * All parameters are provided by the run_rpc_command function, except for
1487 * argc, argv which are passed through.
1489 * @param domain_sid The domain sid acquired from the remote server.
1490 * @param cli A cli_state connected to the server.
1491 * @param mem_ctx Talloc context, destroyed on completion of the function.
1492 * @param argc Standard main() style argc.
1493 * @param argv Standard main() style argv. Initial components are already
1496 * @return Normal NTSTATUS return.
1499 static NTSTATUS
rpc_group_delete_internals(struct net_context
*c
,
1500 const struct dom_sid
*domain_sid
,
1501 const char *domain_name
,
1502 struct cli_state
*cli
,
1503 struct rpc_pipe_client
*pipe_hnd
,
1504 TALLOC_CTX
*mem_ctx
,
1508 struct policy_handle connect_pol
, domain_pol
, group_pol
, user_pol
;
1509 bool group_is_primary
= false;
1510 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1512 struct samr_RidTypeArray
*rids
= NULL
;
1515 /* struct samr_RidWithAttribute *user_gids; */
1517 struct samr_Ids group_rids
, name_types
;
1518 struct lsa_String lsa_acct_name
;
1519 union samr_UserInfo
*info
= NULL
;
1521 if (argc
< 1 || c
->display_usage
) {
1522 rpc_group_usage(c
, argc
,argv
);
1523 return NT_STATUS_OK
; /* ok? */
1526 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1528 MAXIMUM_ALLOWED_ACCESS
,
1531 if (!NT_STATUS_IS_OK(result
)) {
1532 d_fprintf(stderr
, _("Request samr_Connect2 failed\n"));
1536 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1538 MAXIMUM_ALLOWED_ACCESS
,
1539 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1542 if (!NT_STATUS_IS_OK(result
)) {
1543 d_fprintf(stderr
, _("Request open_domain failed\n"));
1547 init_lsa_String(&lsa_acct_name
, argv
[0]);
1549 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1555 if (!NT_STATUS_IS_OK(result
)) {
1556 d_fprintf(stderr
, _("Lookup of '%s' failed\n"),argv
[0]);
1560 switch (name_types
.ids
[0])
1562 case SID_NAME_DOM_GRP
:
1563 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
1565 MAXIMUM_ALLOWED_ACCESS
,
1568 if (!NT_STATUS_IS_OK(result
)) {
1569 d_fprintf(stderr
, _("Request open_group failed"));
1573 group_rid
= group_rids
.ids
[0];
1575 result
= rpccli_samr_QueryGroupMember(pipe_hnd
, mem_ctx
,
1579 if (!NT_STATUS_IS_OK(result
)) {
1581 _("Unable to query group members of %s"),
1586 if (c
->opt_verbose
) {
1588 _("Domain Group %s (rid: %d) has %d members\n"),
1589 argv
[0],group_rid
, rids
->count
);
1592 /* Check if group is anyone's primary group */
1593 for (i
= 0; i
< rids
->count
; i
++)
1595 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1597 MAXIMUM_ALLOWED_ACCESS
,
1601 if (!NT_STATUS_IS_OK(result
)) {
1603 _("Unable to open group member %d\n"),
1608 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1613 if (!NT_STATUS_IS_OK(result
)) {
1615 _("Unable to lookup userinfo for group "
1621 if (info
->info21
.primary_gid
== group_rid
) {
1622 if (c
->opt_verbose
) {
1623 d_printf(_("Group is primary group "
1625 info
->info21
.account_name
.string
);
1627 group_is_primary
= true;
1630 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1633 if (group_is_primary
) {
1634 d_fprintf(stderr
, _("Unable to delete group because "
1635 "some of it's members have it as primary "
1637 result
= NT_STATUS_MEMBERS_PRIMARY_GROUP
;
1641 /* remove all group members */
1642 for (i
= 0; i
< rids
->count
; i
++)
1645 d_printf(_("Remove group member %d..."),
1647 result
= rpccli_samr_DeleteGroupMember(pipe_hnd
, mem_ctx
,
1651 if (NT_STATUS_IS_OK(result
)) {
1653 d_printf(_("ok\n"));
1656 d_printf("%s\n", _("failed"));
1661 result
= rpccli_samr_DeleteDomainGroup(pipe_hnd
, mem_ctx
,
1665 /* removing a local group is easier... */
1666 case SID_NAME_ALIAS
:
1667 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
1669 MAXIMUM_ALLOWED_ACCESS
,
1673 if (!NT_STATUS_IS_OK(result
)) {
1674 d_fprintf(stderr
, _("Request open_alias failed\n"));
1678 result
= rpccli_samr_DeleteDomAlias(pipe_hnd
, mem_ctx
,
1682 d_fprintf(stderr
, _("%s is of type %s. This command is only "
1683 "for deleting local or global groups\n"),
1684 argv
[0],sid_type_lookup(name_types
.ids
[0]));
1685 result
= NT_STATUS_UNSUCCESSFUL
;
1689 if (NT_STATUS_IS_OK(result
)) {
1691 d_printf(_("Deleted %s '%s'\n"),
1692 sid_type_lookup(name_types
.ids
[0]), argv
[0]);
1694 d_fprintf(stderr
, _("Deleting of %s failed: %s\n"), argv
[0],
1695 get_friendly_nt_error_msg(result
));
1703 static int rpc_group_delete(struct net_context
*c
, int argc
, const char **argv
)
1705 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
1706 rpc_group_delete_internals
, argc
,argv
);
1709 static int rpc_group_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1711 NET_API_STATUS status
;
1712 struct GROUP_INFO_1 info1
;
1713 uint32_t parm_error
= 0;
1715 if (argc
!= 1 || c
->display_usage
) {
1716 rpc_group_usage(c
, argc
, argv
);
1722 info1
.grpi1_name
= argv
[0];
1723 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1724 info1
.grpi1_comment
= c
->opt_comment
;
1727 status
= NetGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1731 _("Failed to add group '%s' with error: %s.\n"),
1732 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1736 d_printf(_("Added group '%s'.\n"), argv
[0]);
1742 static int rpc_alias_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1744 NET_API_STATUS status
;
1745 struct LOCALGROUP_INFO_1 info1
;
1746 uint32_t parm_error
= 0;
1748 if (argc
!= 1 || c
->display_usage
) {
1749 rpc_group_usage(c
, argc
, argv
);
1755 info1
.lgrpi1_name
= argv
[0];
1756 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1757 info1
.lgrpi1_comment
= c
->opt_comment
;
1760 status
= NetLocalGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1764 _("Failed to add alias '%s' with error: %s.\n"),
1765 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1769 d_printf(_("Added alias '%s'.\n"), argv
[0]);
1775 static int rpc_group_add(struct net_context
*c
, int argc
, const char **argv
)
1777 if (c
->opt_localgroup
)
1778 return rpc_alias_add_internals(c
, argc
, argv
);
1780 return rpc_group_add_internals(c
, argc
, argv
);
1783 static NTSTATUS
get_sid_from_name(struct cli_state
*cli
,
1784 TALLOC_CTX
*mem_ctx
,
1786 struct dom_sid
*sid
,
1787 enum lsa_SidType
*type
)
1789 struct dom_sid
*sids
= NULL
;
1790 enum lsa_SidType
*types
= NULL
;
1791 struct rpc_pipe_client
*pipe_hnd
= NULL
;
1792 struct policy_handle lsa_pol
;
1793 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1795 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
1797 if (!NT_STATUS_IS_OK(result
)) {
1801 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, false,
1802 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
1804 if (!NT_STATUS_IS_OK(result
)) {
1808 result
= rpccli_lsa_lookup_names(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
1809 &name
, NULL
, 1, &sids
, &types
);
1811 if (NT_STATUS_IS_OK(result
)) {
1812 sid_copy(sid
, &sids
[0]);
1816 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
1820 TALLOC_FREE(pipe_hnd
);
1823 if (!NT_STATUS_IS_OK(result
) && (StrnCaseCmp(name
, "S-", 2) == 0)) {
1825 /* Try as S-1-5-whatever */
1827 struct dom_sid tmp_sid
;
1829 if (string_to_sid(&tmp_sid
, name
)) {
1830 sid_copy(sid
, &tmp_sid
);
1831 *type
= SID_NAME_UNKNOWN
;
1832 result
= NT_STATUS_OK
;
1839 static NTSTATUS
rpc_add_groupmem(struct rpc_pipe_client
*pipe_hnd
,
1840 TALLOC_CTX
*mem_ctx
,
1841 const struct dom_sid
*group_sid
,
1844 struct policy_handle connect_pol
, domain_pol
;
1847 struct policy_handle group_pol
;
1849 struct samr_Ids rids
, rid_types
;
1850 struct lsa_String lsa_acct_name
;
1854 sid_copy(&sid
, group_sid
);
1856 if (!sid_split_rid(&sid
, &group_rid
)) {
1857 return NT_STATUS_UNSUCCESSFUL
;
1860 /* Get sam policy handle */
1861 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1863 MAXIMUM_ALLOWED_ACCESS
,
1865 if (!NT_STATUS_IS_OK(result
)) {
1869 /* Get domain policy handle */
1870 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1872 MAXIMUM_ALLOWED_ACCESS
,
1875 if (!NT_STATUS_IS_OK(result
)) {
1879 init_lsa_String(&lsa_acct_name
, member
);
1881 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1888 if (!NT_STATUS_IS_OK(result
)) {
1889 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
1894 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
1896 MAXIMUM_ALLOWED_ACCESS
,
1900 if (!NT_STATUS_IS_OK(result
)) {
1904 result
= rpccli_samr_AddGroupMember(pipe_hnd
, mem_ctx
,
1907 0x0005); /* unknown flags */
1910 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1914 static NTSTATUS
rpc_add_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
1915 TALLOC_CTX
*mem_ctx
,
1916 const struct dom_sid
*alias_sid
,
1919 struct policy_handle connect_pol
, domain_pol
;
1922 struct policy_handle alias_pol
;
1924 struct dom_sid member_sid
;
1925 enum lsa_SidType member_type
;
1929 sid_copy(&sid
, alias_sid
);
1931 if (!sid_split_rid(&sid
, &alias_rid
)) {
1932 return NT_STATUS_UNSUCCESSFUL
;
1935 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
1936 member
, &member_sid
, &member_type
);
1938 if (!NT_STATUS_IS_OK(result
)) {
1939 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
1944 /* Get sam policy handle */
1945 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1947 MAXIMUM_ALLOWED_ACCESS
,
1949 if (!NT_STATUS_IS_OK(result
)) {
1953 /* Get domain policy handle */
1954 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1956 MAXIMUM_ALLOWED_ACCESS
,
1959 if (!NT_STATUS_IS_OK(result
)) {
1963 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
1965 MAXIMUM_ALLOWED_ACCESS
,
1969 if (!NT_STATUS_IS_OK(result
)) {
1973 result
= rpccli_samr_AddAliasMember(pipe_hnd
, mem_ctx
,
1977 if (!NT_STATUS_IS_OK(result
)) {
1982 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1986 static NTSTATUS
rpc_group_addmem_internals(struct net_context
*c
,
1987 const struct dom_sid
*domain_sid
,
1988 const char *domain_name
,
1989 struct cli_state
*cli
,
1990 struct rpc_pipe_client
*pipe_hnd
,
1991 TALLOC_CTX
*mem_ctx
,
1995 struct dom_sid group_sid
;
1996 enum lsa_SidType group_type
;
1998 if (argc
!= 2 || c
->display_usage
) {
2001 _("net rpc group addmem <group> <member>\n"
2002 " Add a member to a group\n"
2003 " group\tGroup to add member to\n"
2004 " member\tMember to add to group\n"));
2005 return NT_STATUS_UNSUCCESSFUL
;
2008 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2009 &group_sid
, &group_type
))) {
2010 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2012 return NT_STATUS_UNSUCCESSFUL
;
2015 if (group_type
== SID_NAME_DOM_GRP
) {
2016 NTSTATUS result
= rpc_add_groupmem(pipe_hnd
, mem_ctx
,
2017 &group_sid
, argv
[1]);
2019 if (!NT_STATUS_IS_OK(result
)) {
2020 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2021 argv
[1], argv
[0], nt_errstr(result
));
2026 if (group_type
== SID_NAME_ALIAS
) {
2027 NTSTATUS result
= rpc_add_aliasmem(pipe_hnd
, mem_ctx
,
2028 &group_sid
, argv
[1]);
2030 if (!NT_STATUS_IS_OK(result
)) {
2031 d_fprintf(stderr
, _("Could not add %s to %s: %s\n"),
2032 argv
[1], argv
[0], nt_errstr(result
));
2037 d_fprintf(stderr
, _("Can only add members to global or local groups "
2038 "which %s is not\n"), argv
[0]);
2040 return NT_STATUS_UNSUCCESSFUL
;
2043 static int rpc_group_addmem(struct net_context
*c
, int argc
, const char **argv
)
2045 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2046 rpc_group_addmem_internals
,
2050 static NTSTATUS
rpc_del_groupmem(struct net_context
*c
,
2051 struct rpc_pipe_client
*pipe_hnd
,
2052 TALLOC_CTX
*mem_ctx
,
2053 const struct dom_sid
*group_sid
,
2056 struct policy_handle connect_pol
, domain_pol
;
2059 struct policy_handle group_pol
;
2061 struct samr_Ids rids
, rid_types
;
2062 struct lsa_String lsa_acct_name
;
2066 sid_copy(&sid
, group_sid
);
2068 if (!sid_split_rid(&sid
, &group_rid
))
2069 return NT_STATUS_UNSUCCESSFUL
;
2071 /* Get sam policy handle */
2072 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2074 MAXIMUM_ALLOWED_ACCESS
,
2076 if (!NT_STATUS_IS_OK(result
))
2079 /* Get domain policy handle */
2080 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2082 MAXIMUM_ALLOWED_ACCESS
,
2085 if (!NT_STATUS_IS_OK(result
))
2088 init_lsa_String(&lsa_acct_name
, member
);
2090 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2096 if (!NT_STATUS_IS_OK(result
)) {
2097 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2102 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2104 MAXIMUM_ALLOWED_ACCESS
,
2108 if (!NT_STATUS_IS_OK(result
))
2111 result
= rpccli_samr_DeleteGroupMember(pipe_hnd
, mem_ctx
,
2116 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2120 static NTSTATUS
rpc_del_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2121 TALLOC_CTX
*mem_ctx
,
2122 const struct dom_sid
*alias_sid
,
2125 struct policy_handle connect_pol
, domain_pol
;
2128 struct policy_handle alias_pol
;
2130 struct dom_sid member_sid
;
2131 enum lsa_SidType member_type
;
2135 sid_copy(&sid
, alias_sid
);
2137 if (!sid_split_rid(&sid
, &alias_rid
))
2138 return NT_STATUS_UNSUCCESSFUL
;
2140 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2141 member
, &member_sid
, &member_type
);
2143 if (!NT_STATUS_IS_OK(result
)) {
2144 d_fprintf(stderr
, _("Could not lookup up group member %s\n"),
2149 /* Get sam policy handle */
2150 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2152 MAXIMUM_ALLOWED_ACCESS
,
2154 if (!NT_STATUS_IS_OK(result
)) {
2158 /* Get domain policy handle */
2159 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2161 MAXIMUM_ALLOWED_ACCESS
,
2164 if (!NT_STATUS_IS_OK(result
)) {
2168 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2170 MAXIMUM_ALLOWED_ACCESS
,
2174 if (!NT_STATUS_IS_OK(result
))
2177 result
= rpccli_samr_DeleteAliasMember(pipe_hnd
, mem_ctx
,
2181 if (!NT_STATUS_IS_OK(result
))
2185 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2189 static NTSTATUS
rpc_group_delmem_internals(struct net_context
*c
,
2190 const struct dom_sid
*domain_sid
,
2191 const char *domain_name
,
2192 struct cli_state
*cli
,
2193 struct rpc_pipe_client
*pipe_hnd
,
2194 TALLOC_CTX
*mem_ctx
,
2198 struct dom_sid group_sid
;
2199 enum lsa_SidType group_type
;
2201 if (argc
!= 2 || c
->display_usage
) {
2204 _("net rpc group delmem <group> <member>\n"
2205 " Delete a member from a group\n"
2206 " group\tGroup to delete member from\n"
2207 " member\tMember to delete from group\n"));
2208 return NT_STATUS_UNSUCCESSFUL
;
2211 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2212 &group_sid
, &group_type
))) {
2213 d_fprintf(stderr
, _("Could not lookup group name %s\n"),
2215 return NT_STATUS_UNSUCCESSFUL
;
2218 if (group_type
== SID_NAME_DOM_GRP
) {
2219 NTSTATUS result
= rpc_del_groupmem(c
, pipe_hnd
, mem_ctx
,
2220 &group_sid
, argv
[1]);
2222 if (!NT_STATUS_IS_OK(result
)) {
2223 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2224 argv
[1], argv
[0], nt_errstr(result
));
2229 if (group_type
== SID_NAME_ALIAS
) {
2230 NTSTATUS result
= rpc_del_aliasmem(pipe_hnd
, mem_ctx
,
2231 &group_sid
, argv
[1]);
2233 if (!NT_STATUS_IS_OK(result
)) {
2234 d_fprintf(stderr
, _("Could not del %s from %s: %s\n"),
2235 argv
[1], argv
[0], nt_errstr(result
));
2240 d_fprintf(stderr
, _("Can only delete members from global or local "
2241 "groups which %s is not\n"), argv
[0]);
2243 return NT_STATUS_UNSUCCESSFUL
;
2246 static int rpc_group_delmem(struct net_context
*c
, int argc
, const char **argv
)
2248 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2249 rpc_group_delmem_internals
,
2254 * List groups on a remote RPC server.
2256 * All parameters are provided by the run_rpc_command function, except for
2257 * argc, argv which are passes through.
2259 * @param domain_sid The domain sid acquired from the remote server.
2260 * @param cli A cli_state connected to the server.
2261 * @param mem_ctx Talloc context, destroyed on completion of the function.
2262 * @param argc Standard main() style argc.
2263 * @param argv Standard main() style argv. Initial components are already
2266 * @return Normal NTSTATUS return.
2269 static NTSTATUS
rpc_group_list_internals(struct net_context
*c
,
2270 const struct dom_sid
*domain_sid
,
2271 const char *domain_name
,
2272 struct cli_state
*cli
,
2273 struct rpc_pipe_client
*pipe_hnd
,
2274 TALLOC_CTX
*mem_ctx
,
2278 struct policy_handle connect_pol
, domain_pol
;
2279 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2280 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
2281 struct samr_SamArray
*groups
= NULL
;
2282 bool global
= false;
2284 bool builtin
= false;
2286 if (c
->display_usage
) {
2289 _("net rpc group list [global] [local] [builtin]\n"
2290 " List groups on RPC server\n"
2291 " global\tList global groups\n"
2292 " local\tList local groups\n"
2293 " builtin\tList builtin groups\n"
2294 " If none of global, local or builtin is "
2295 "specified, all three options are considered "
2297 return NT_STATUS_OK
;
2306 for (i
=0; i
<argc
; i
++) {
2307 if (strequal(argv
[i
], "global"))
2310 if (strequal(argv
[i
], "local"))
2313 if (strequal(argv
[i
], "builtin"))
2317 /* Get sam policy handle */
2319 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2321 MAXIMUM_ALLOWED_ACCESS
,
2323 if (!NT_STATUS_IS_OK(result
)) {
2327 /* Get domain policy handle */
2329 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2331 MAXIMUM_ALLOWED_ACCESS
,
2332 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2334 if (!NT_STATUS_IS_OK(result
)) {
2338 /* Query domain groups */
2339 if (c
->opt_long_list_entries
)
2340 d_printf(_("\nGroup name Comment"
2341 "\n-----------------------------\n"));
2343 uint32_t max_size
, total_size
, returned_size
;
2344 union samr_DispInfo info
;
2348 get_query_dispinfo_params(
2349 loop_count
, &max_entries
, &max_size
);
2351 result
= rpccli_samr_QueryDisplayInfo(pipe_hnd
, mem_ctx
,
2360 num_entries
= info
.info3
.count
;
2361 start_idx
+= info
.info3
.count
;
2363 if (!NT_STATUS_IS_OK(result
) &&
2364 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2367 for (i
= 0; i
< num_entries
; i
++) {
2369 const char *group
= NULL
;
2370 const char *desc
= NULL
;
2372 group
= info
.info3
.entries
[i
].account_name
.string
;
2373 desc
= info
.info3
.entries
[i
].description
.string
;
2375 if (c
->opt_long_list_entries
)
2376 printf("%-21.21s %-50.50s\n",
2379 printf("%s\n", group
);
2381 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2382 /* query domain aliases */
2387 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
2393 if (!NT_STATUS_IS_OK(result
) &&
2394 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2397 for (i
= 0; i
< num_entries
; i
++) {
2399 const char *description
= NULL
;
2401 if (c
->opt_long_list_entries
) {
2403 struct policy_handle alias_pol
;
2404 union samr_AliasInfo
*info
= NULL
;
2406 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2409 groups
->entries
[i
].idx
,
2411 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd
, mem_ctx
,
2415 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd
, mem_ctx
,
2417 description
= info
->description
.string
;
2421 if (description
!= NULL
) {
2422 printf("%-21.21s %-50.50s\n",
2423 groups
->entries
[i
].name
.string
,
2426 printf("%s\n", groups
->entries
[i
].name
.string
);
2429 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2430 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
2431 /* Get builtin policy handle */
2433 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2435 MAXIMUM_ALLOWED_ACCESS
,
2436 CONST_DISCARD(struct dom_sid2
*, &global_sid_Builtin
),
2438 if (!NT_STATUS_IS_OK(result
)) {
2441 /* query builtin aliases */
2444 if (!builtin
) break;
2446 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
2452 if (!NT_STATUS_IS_OK(result
) &&
2453 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2456 for (i
= 0; i
< num_entries
; i
++) {
2458 const char *description
= NULL
;
2460 if (c
->opt_long_list_entries
) {
2462 struct policy_handle alias_pol
;
2463 union samr_AliasInfo
*info
= NULL
;
2465 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2468 groups
->entries
[i
].idx
,
2470 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd
, mem_ctx
,
2474 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd
, mem_ctx
,
2476 description
= info
->description
.string
;
2480 if (description
!= NULL
) {
2481 printf("%-21.21s %-50.50s\n",
2482 groups
->entries
[i
].name
.string
,
2485 printf("%s\n", groups
->entries
[i
].name
.string
);
2488 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2494 static int rpc_group_list(struct net_context
*c
, int argc
, const char **argv
)
2496 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2497 rpc_group_list_internals
,
2501 static NTSTATUS
rpc_list_group_members(struct net_context
*c
,
2502 struct rpc_pipe_client
*pipe_hnd
,
2503 TALLOC_CTX
*mem_ctx
,
2504 const char *domain_name
,
2505 const struct dom_sid
*domain_sid
,
2506 struct policy_handle
*domain_pol
,
2510 struct policy_handle group_pol
;
2511 uint32 num_members
, *group_rids
;
2513 struct samr_RidTypeArray
*rids
= NULL
;
2514 struct lsa_Strings names
;
2515 struct samr_Ids types
;
2518 sid_to_fstring(sid_str
, domain_sid
);
2520 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2522 MAXIMUM_ALLOWED_ACCESS
,
2526 if (!NT_STATUS_IS_OK(result
))
2529 result
= rpccli_samr_QueryGroupMember(pipe_hnd
, mem_ctx
,
2533 if (!NT_STATUS_IS_OK(result
))
2536 num_members
= rids
->count
;
2537 group_rids
= rids
->rids
;
2539 while (num_members
> 0) {
2540 int this_time
= 512;
2542 if (num_members
< this_time
)
2543 this_time
= num_members
;
2545 result
= rpccli_samr_LookupRids(pipe_hnd
, mem_ctx
,
2552 if (!NT_STATUS_IS_OK(result
))
2555 /* We only have users as members, but make the output
2556 the same as the output of alias members */
2558 for (i
= 0; i
< this_time
; i
++) {
2560 if (c
->opt_long_list_entries
) {
2561 printf("%s-%d %s\\%s %d\n", sid_str
,
2562 group_rids
[i
], domain_name
,
2563 names
.names
[i
].string
,
2566 printf("%s\\%s\n", domain_name
,
2567 names
.names
[i
].string
);
2571 num_members
-= this_time
;
2575 return NT_STATUS_OK
;
2578 static NTSTATUS
rpc_list_alias_members(struct net_context
*c
,
2579 struct rpc_pipe_client
*pipe_hnd
,
2580 TALLOC_CTX
*mem_ctx
,
2581 struct policy_handle
*domain_pol
,
2585 struct rpc_pipe_client
*lsa_pipe
;
2586 struct policy_handle alias_pol
, lsa_pol
;
2588 struct dom_sid
*alias_sids
;
2591 enum lsa_SidType
*types
;
2593 struct lsa_SidArray sid_array
;
2595 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2597 MAXIMUM_ALLOWED_ACCESS
,
2601 if (!NT_STATUS_IS_OK(result
))
2604 result
= rpccli_samr_GetMembersInAlias(pipe_hnd
, mem_ctx
,
2608 if (!NT_STATUS_IS_OK(result
)) {
2609 d_fprintf(stderr
, _("Couldn't list alias members\n"));
2613 num_members
= sid_array
.num_sids
;
2615 if (num_members
== 0) {
2616 return NT_STATUS_OK
;
2619 result
= cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd
),
2620 &ndr_table_lsarpc
.syntax_id
,
2622 if (!NT_STATUS_IS_OK(result
)) {
2623 d_fprintf(stderr
, _("Couldn't open LSA pipe. Error was %s\n"),
2624 nt_errstr(result
) );
2628 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, true,
2629 SEC_FLAG_MAXIMUM_ALLOWED
, &lsa_pol
);
2631 if (!NT_STATUS_IS_OK(result
)) {
2632 d_fprintf(stderr
, _("Couldn't open LSA policy handle\n"));
2633 TALLOC_FREE(lsa_pipe
);
2637 alias_sids
= TALLOC_ZERO_ARRAY(mem_ctx
, struct dom_sid
, num_members
);
2639 d_fprintf(stderr
, _("Out of memory\n"));
2640 TALLOC_FREE(lsa_pipe
);
2641 return NT_STATUS_NO_MEMORY
;
2644 for (i
=0; i
<num_members
; i
++) {
2645 sid_copy(&alias_sids
[i
], sid_array
.sids
[i
].sid
);
2648 result
= rpccli_lsa_lookup_sids(lsa_pipe
, mem_ctx
, &lsa_pol
,
2649 num_members
, alias_sids
,
2650 &domains
, &names
, &types
);
2652 if (!NT_STATUS_IS_OK(result
) &&
2653 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
2654 d_fprintf(stderr
, _("Couldn't lookup SIDs\n"));
2655 TALLOC_FREE(lsa_pipe
);
2659 for (i
= 0; i
< num_members
; i
++) {
2661 sid_to_fstring(sid_str
, &alias_sids
[i
]);
2663 if (c
->opt_long_list_entries
) {
2664 printf("%s %s\\%s %d\n", sid_str
,
2665 domains
[i
] ? domains
[i
] : _("*unknown*"),
2666 names
[i
] ? names
[i
] : _("*unknown*"), types
[i
]);
2669 printf("%s\\%s\n", domains
[i
], names
[i
]);
2671 printf("%s\n", sid_str
);
2675 TALLOC_FREE(lsa_pipe
);
2676 return NT_STATUS_OK
;
2679 static NTSTATUS
rpc_group_members_internals(struct net_context
*c
,
2680 const struct dom_sid
*domain_sid
,
2681 const char *domain_name
,
2682 struct cli_state
*cli
,
2683 struct rpc_pipe_client
*pipe_hnd
,
2684 TALLOC_CTX
*mem_ctx
,
2689 struct policy_handle connect_pol
, domain_pol
;
2690 struct samr_Ids rids
, rid_types
;
2691 struct lsa_String lsa_acct_name
;
2693 /* Get sam policy handle */
2695 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2697 MAXIMUM_ALLOWED_ACCESS
,
2700 if (!NT_STATUS_IS_OK(result
))
2703 /* Get domain policy handle */
2705 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2707 MAXIMUM_ALLOWED_ACCESS
,
2708 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2711 if (!NT_STATUS_IS_OK(result
))
2714 init_lsa_String(&lsa_acct_name
, argv
[0]); /* sure? */
2716 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2723 if (!NT_STATUS_IS_OK(result
)) {
2725 /* Ok, did not find it in the global sam, try with builtin */
2727 struct dom_sid sid_Builtin
;
2729 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
2731 sid_copy(&sid_Builtin
, &global_sid_Builtin
);
2733 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2735 MAXIMUM_ALLOWED_ACCESS
,
2739 if (!NT_STATUS_IS_OK(result
)) {
2740 d_fprintf(stderr
, _("Couldn't find group %s\n"),
2745 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2752 if (!NT_STATUS_IS_OK(result
)) {
2753 d_fprintf(stderr
, _("Couldn't find group %s\n"),
2759 if (rids
.count
!= 1) {
2760 d_fprintf(stderr
, _("Couldn't find group %s\n"),
2765 if (rid_types
.ids
[0] == SID_NAME_DOM_GRP
) {
2766 return rpc_list_group_members(c
, pipe_hnd
, mem_ctx
, domain_name
,
2767 domain_sid
, &domain_pol
,
2771 if (rid_types
.ids
[0] == SID_NAME_ALIAS
) {
2772 return rpc_list_alias_members(c
, pipe_hnd
, mem_ctx
, &domain_pol
,
2776 return NT_STATUS_NO_SUCH_GROUP
;
2779 static int rpc_group_members(struct net_context
*c
, int argc
, const char **argv
)
2781 if (argc
!= 1 || c
->display_usage
) {
2782 return rpc_group_usage(c
, argc
, argv
);
2785 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2786 rpc_group_members_internals
,
2790 static int rpc_group_rename_internals(struct net_context
*c
, int argc
, const char **argv
)
2792 NET_API_STATUS status
;
2793 struct GROUP_INFO_0 g0
;
2797 d_printf(_("Usage:\n"));
2798 d_printf("net rpc group rename group newname\n");
2802 g0
.grpi0_name
= argv
[1];
2804 status
= NetGroupSetInfo(c
->opt_host
,
2811 d_fprintf(stderr
, _("Renaming group %s failed with: %s\n"),
2812 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
2820 static int rpc_group_rename(struct net_context
*c
, int argc
, const char **argv
)
2822 if (argc
!= 2 || c
->display_usage
) {
2823 return rpc_group_usage(c
, argc
, argv
);
2826 return rpc_group_rename_internals(c
, argc
, argv
);
2830 * 'net rpc group' entrypoint.
2831 * @param argc Standard main() style argc.
2832 * @param argv Standard main() style argv. Initial components are already
2836 int net_rpc_group(struct net_context
*c
, int argc
, const char **argv
)
2838 NET_API_STATUS status
;
2840 struct functable func
[] = {
2845 N_("Create specified group"),
2846 N_("net rpc group add\n"
2847 " Create specified group")
2853 N_("Delete specified group"),
2854 N_("net rpc group delete\n"
2855 " Delete specified group")
2861 N_("Add member to group"),
2862 N_("net rpc group addmem\n"
2863 " Add member to group")
2869 N_("Remove member from group"),
2870 N_("net rpc group delmem\n"
2871 " Remove member from group")
2878 N_("net rpc group list\n"
2885 N_("List group members"),
2886 N_("net rpc group members\n"
2887 " List group members")
2894 N_("net rpc group rename\n"
2897 {NULL
, NULL
, 0, NULL
, NULL
}
2900 status
= libnetapi_init(&c
->netapi_ctx
);
2904 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
2905 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
2906 if (c
->opt_kerberos
) {
2907 libnetapi_set_use_kerberos(c
->netapi_ctx
);
2911 if (c
->display_usage
) {
2912 d_printf(_("Usage:\n"));
2913 d_printf(_("net rpc group\n"
2914 " Alias for net rpc group list global "
2915 "local builtin\n"));
2916 net_display_usage_from_functable(func
);
2920 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2921 rpc_group_list_internals
,
2925 return net_run_function(c
, argc
, argv
, "net rpc group", func
);
2928 /****************************************************************************/
2930 static int rpc_share_usage(struct net_context
*c
, int argc
, const char **argv
)
2932 return net_share_usage(c
, argc
, argv
);
2936 * Add a share on a remote RPC server.
2938 * @param argc Standard main() style argc.
2939 * @param argv Standard main() style argv. Initial components are already
2942 * @return A shell status integer (0 for success).
2945 static int rpc_share_add(struct net_context
*c
, int argc
, const char **argv
)
2947 NET_API_STATUS status
;
2950 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
2951 uint32 num_users
=0, perms
=0;
2952 char *password
=NULL
; /* don't allow a share password */
2953 struct SHARE_INFO_2 i2
;
2954 uint32_t parm_error
= 0;
2956 if ((argc
< 1) || !strchr(argv
[0], '=') || c
->display_usage
) {
2957 return rpc_share_usage(c
, argc
, argv
);
2960 if ((sharename
= talloc_strdup(c
, argv
[0])) == NULL
) {
2964 path
= strchr(sharename
, '=');
2971 i2
.shi2_netname
= sharename
;
2972 i2
.shi2_type
= type
;
2973 i2
.shi2_remark
= c
->opt_comment
;
2974 i2
.shi2_permissions
= perms
;
2975 i2
.shi2_max_uses
= c
->opt_maxusers
;
2976 i2
.shi2_current_uses
= num_users
;
2977 i2
.shi2_path
= path
;
2978 i2
.shi2_passwd
= password
;
2980 status
= NetShareAdd(c
->opt_host
,
2985 printf(_("NetShareAdd failed with: %s\n"),
2986 libnetapi_get_error_string(c
->netapi_ctx
, status
));
2993 * Delete a share on a remote RPC server.
2995 * @param domain_sid The domain sid acquired from the remote server.
2996 * @param argc Standard main() style argc.
2997 * @param argv Standard main() style argv. Initial components are already
3000 * @return A shell status integer (0 for success).
3002 static int rpc_share_delete(struct net_context
*c
, int argc
, const char **argv
)
3004 if (argc
< 1 || c
->display_usage
) {
3005 return rpc_share_usage(c
, argc
, argv
);
3008 return NetShareDel(c
->opt_host
, argv
[0], 0);
3012 * Formatted print of share info
3014 * @param r pointer to SHARE_INFO_1 to format
3017 static void display_share_info_1(struct net_context
*c
,
3018 struct SHARE_INFO_1
*r
)
3020 if (c
->opt_long_list_entries
) {
3021 d_printf("%-12s %-8.8s %-50s\n",
3023 net_share_type_str(r
->shi1_type
& ~(STYPE_TEMPORARY
|STYPE_HIDDEN
)),
3026 d_printf("%s\n", r
->shi1_netname
);
3030 static WERROR
get_share_info(struct net_context
*c
,
3031 struct rpc_pipe_client
*pipe_hnd
,
3032 TALLOC_CTX
*mem_ctx
,
3036 struct srvsvc_NetShareInfoCtr
*info_ctr
)
3040 union srvsvc_NetShareInfo info
;
3042 /* no specific share requested, enumerate all */
3045 uint32_t preferred_len
= 0xffffffff;
3046 uint32_t total_entries
= 0;
3047 uint32_t resume_handle
= 0;
3049 info_ctr
->level
= level
;
3051 status
= rpccli_srvsvc_NetShareEnumAll(pipe_hnd
, mem_ctx
,
3061 /* request just one share */
3062 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
3069 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
3074 ZERO_STRUCTP(info_ctr
);
3076 info_ctr
->level
= level
;
3081 struct srvsvc_NetShareCtr1
*ctr1
;
3083 ctr1
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr1
);
3084 W_ERROR_HAVE_NO_MEMORY(ctr1
);
3087 ctr1
->array
= info
.info1
;
3089 info_ctr
->ctr
.ctr1
= ctr1
;
3093 struct srvsvc_NetShareCtr2
*ctr2
;
3095 ctr2
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr2
);
3096 W_ERROR_HAVE_NO_MEMORY(ctr2
);
3099 ctr2
->array
= info
.info2
;
3101 info_ctr
->ctr
.ctr2
= ctr2
;
3105 struct srvsvc_NetShareCtr502
*ctr502
;
3107 ctr502
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr502
);
3108 W_ERROR_HAVE_NO_MEMORY(ctr502
);
3111 ctr502
->array
= info
.info502
;
3113 info_ctr
->ctr
.ctr502
= ctr502
;
3121 * 'net rpc share list' entrypoint.
3122 * @param argc Standard main() style argc.
3123 * @param argv Standard main() style argv. Initial components are already
3126 static int rpc_share_list(struct net_context
*c
, int argc
, const char **argv
)
3128 NET_API_STATUS status
;
3129 struct SHARE_INFO_1
*i1
= NULL
;
3130 uint32_t entries_read
= 0;
3131 uint32_t total_entries
= 0;
3132 uint32_t resume_handle
= 0;
3133 uint32_t i
, level
= 1;
3135 if (c
->display_usage
) {
3137 "net rpc share list\n"
3140 _("List shares on remote server"));
3144 status
= NetShareEnum(c
->opt_host
,
3146 (uint8_t **)(void *)&i1
,
3155 /* Display results */
3157 if (c
->opt_long_list_entries
) {
3159 "\nEnumerating shared resources (exports) on remote server:\n\n"
3160 "\nShare name Type Description\n"
3161 "---------- ---- -----------\n"));
3163 for (i
= 0; i
< entries_read
; i
++)
3164 display_share_info_1(c
, &i1
[i
]);
3169 static bool check_share_availability(struct cli_state
*cli
, const char *netname
)
3173 status
= cli_tcon_andx(cli
, netname
, "A:", "", 0);
3174 if (!NT_STATUS_IS_OK(status
)) {
3175 d_printf(_("skipping [%s]: not a file share.\n"), netname
);
3179 status
= cli_tdis(cli
);
3180 if (!NT_STATUS_IS_OK(status
)) {
3181 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status
));
3188 static bool check_share_sanity(struct net_context
*c
, struct cli_state
*cli
,
3189 const char *netname
, uint32 type
)
3191 /* only support disk shares */
3192 if (! ( type
== STYPE_DISKTREE
|| type
== (STYPE_DISKTREE
| STYPE_HIDDEN
)) ) {
3193 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname
,
3198 /* skip builtin shares */
3199 /* FIXME: should print$ be added too ? */
3200 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$") ||
3201 strequal(netname
,"global"))
3204 if (c
->opt_exclude
&& in_list(netname
, c
->opt_exclude
, false)) {
3205 printf(_("excluding [%s]\n"), netname
);
3209 return check_share_availability(cli
, netname
);
3213 * Migrate shares from a remote RPC server to the local RPC server.
3215 * All parameters are provided by the run_rpc_command function, except for
3216 * argc, argv which are passed through.
3218 * @param domain_sid The domain sid acquired from the remote server.
3219 * @param cli A cli_state connected to the server.
3220 * @param mem_ctx Talloc context, destroyed on completion of the function.
3221 * @param argc Standard main() style argc.
3222 * @param argv Standard main() style argv. Initial components are already
3225 * @return Normal NTSTATUS return.
3228 static NTSTATUS
rpc_share_migrate_shares_internals(struct net_context
*c
,
3229 const struct dom_sid
*domain_sid
,
3230 const char *domain_name
,
3231 struct cli_state
*cli
,
3232 struct rpc_pipe_client
*pipe_hnd
,
3233 TALLOC_CTX
*mem_ctx
,
3238 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3239 struct srvsvc_NetShareInfoCtr ctr_src
;
3241 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3242 struct cli_state
*cli_dst
= NULL
;
3243 uint32 level
= 502; /* includes secdesc */
3244 uint32_t parm_error
= 0;
3246 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3248 if (!W_ERROR_IS_OK(result
))
3251 /* connect destination PI_SRVSVC */
3252 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3253 &ndr_table_srvsvc
.syntax_id
);
3254 if (!NT_STATUS_IS_OK(nt_status
))
3258 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3260 union srvsvc_NetShareInfo info
;
3261 struct srvsvc_NetShareInfo502 info502
=
3262 ctr_src
.ctr
.ctr502
->array
[i
];
3264 /* reset error-code */
3265 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3267 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3270 /* finally add the share on the dst server */
3272 printf(_("migrating: [%s], path: %s, comment: %s, without "
3274 info502
.name
, info502
.path
, info502
.comment
);
3276 info
.info502
= &info502
;
3278 nt_status
= rpccli_srvsvc_NetShareAdd(srvsvc_pipe
, mem_ctx
,
3279 srvsvc_pipe
->desthost
,
3285 if (W_ERROR_V(result
) == W_ERROR_V(WERR_FILE_EXISTS
)) {
3286 printf(_(" [%s] does already exist\n"),
3291 if (!NT_STATUS_IS_OK(nt_status
) || !W_ERROR_IS_OK(result
)) {
3292 printf(_("cannot add share: %s\n"), win_errstr(result
));
3298 nt_status
= NT_STATUS_OK
;
3302 cli_shutdown(cli_dst
);
3310 * Migrate shares from a RPC server to another.
3312 * @param argc Standard main() style argc.
3313 * @param argv Standard main() style argv. Initial components are already
3316 * @return A shell status integer (0 for success).
3318 static int rpc_share_migrate_shares(struct net_context
*c
, int argc
,
3321 if (c
->display_usage
) {
3323 "net rpc share migrate shares\n"
3326 _("Migrate shares to local server"));
3331 printf(_("no server to migrate\n"));
3335 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3336 rpc_share_migrate_shares_internals
,
3343 * @param f file_info
3344 * @param mask current search mask
3345 * @param state arg-pointer
3348 static void copy_fn(const char *mnt
, struct file_info
*f
,
3349 const char *mask
, void *state
)
3351 static NTSTATUS nt_status
;
3352 static struct copy_clistate
*local_state
;
3353 static fstring filename
, new_mask
;
3356 struct net_context
*c
;
3358 local_state
= (struct copy_clistate
*)state
;
3359 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3363 if (strequal(f
->name
, ".") || strequal(f
->name
, ".."))
3366 DEBUG(3,("got mask: %s, name: %s\n", mask
, f
->name
));
3369 if (f
->mode
& aDIR
) {
3371 DEBUG(3,("got dir: %s\n", f
->name
));
3373 fstrcpy(dir
, local_state
->cwd
);
3375 fstrcat(dir
, f
->name
);
3377 switch (net_mode_share
)
3379 case NET_MODE_SHARE_MIGRATE
:
3380 /* create that directory */
3381 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3382 local_state
->cli_share_src
,
3383 local_state
->cli_share_dst
,
3385 c
->opt_acls
? true : false,
3386 c
->opt_attrs
? true : false,
3387 c
->opt_timestamps
? true:false,
3391 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3395 if (!NT_STATUS_IS_OK(nt_status
))
3396 printf(_("could not handle dir %s: %s\n"),
3397 dir
, nt_errstr(nt_status
));
3399 /* search below that directory */
3400 fstrcpy(new_mask
, dir
);
3401 fstrcat(new_mask
, "\\*");
3403 old_dir
= local_state
->cwd
;
3404 local_state
->cwd
= dir
;
3405 if (!sync_files(local_state
, new_mask
))
3406 printf(_("could not handle files\n"));
3407 local_state
->cwd
= old_dir
;
3414 fstrcpy(filename
, local_state
->cwd
);
3415 fstrcat(filename
, "\\");
3416 fstrcat(filename
, f
->name
);
3418 DEBUG(3,("got file: %s\n", filename
));
3420 switch (net_mode_share
)
3422 case NET_MODE_SHARE_MIGRATE
:
3423 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3424 local_state
->cli_share_src
,
3425 local_state
->cli_share_dst
,
3427 c
->opt_acls
? true : false,
3428 c
->opt_attrs
? true : false,
3429 c
->opt_timestamps
? true: false,
3433 d_fprintf(stderr
, _("Unsupported file mode %d\n"),
3438 if (!NT_STATUS_IS_OK(nt_status
))
3439 printf(_("could not handle file %s: %s\n"),
3440 filename
, nt_errstr(nt_status
));
3445 * sync files, can be called recursivly to list files
3446 * and then call copy_fn for each file
3448 * @param cp_clistate pointer to the copy_clistate we work with
3449 * @param mask the current search mask
3451 * @return Boolean result
3453 static bool sync_files(struct copy_clistate
*cp_clistate
, const char *mask
)
3455 struct cli_state
*targetcli
;
3456 char *targetpath
= NULL
;
3459 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
3461 if ( !cli_resolve_path(talloc_tos(), "", NULL
, cp_clistate
->cli_share_src
,
3462 mask
, &targetcli
, &targetpath
) ) {
3463 d_fprintf(stderr
, _("cli_resolve_path %s failed with error: "
3465 mask
, cli_errstr(cp_clistate
->cli_share_src
));
3469 status
= cli_list(targetcli
, targetpath
, cp_clistate
->attribute
,
3470 copy_fn
, cp_clistate
);
3471 if (!NT_STATUS_IS_OK(status
)) {
3472 d_fprintf(stderr
, _("listing %s failed with error: %s\n"),
3473 mask
, nt_errstr(status
));
3482 * Set the top level directory permissions before we do any further copies.
3483 * Should set up ACL inheritance.
3486 bool copy_top_level_perms(struct net_context
*c
,
3487 struct copy_clistate
*cp_clistate
,
3488 const char *sharename
)
3490 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3492 switch (net_mode_share
) {
3493 case NET_MODE_SHARE_MIGRATE
:
3494 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename
));
3495 nt_status
= net_copy_fileattr(c
,
3496 cp_clistate
->mem_ctx
,
3497 cp_clistate
->cli_share_src
,
3498 cp_clistate
->cli_share_dst
,
3500 c
->opt_acls
? true : false,
3501 c
->opt_attrs
? true : false,
3502 c
->opt_timestamps
? true: false,
3506 d_fprintf(stderr
, _("Unsupported mode %d\n"), net_mode_share
);
3510 if (!NT_STATUS_IS_OK(nt_status
)) {
3511 printf(_("Could handle directory attributes for top level "
3512 "directory of share %s. Error %s\n"),
3513 sharename
, nt_errstr(nt_status
));
3521 * Sync all files inside a remote share to another share (over smb).
3523 * All parameters are provided by the run_rpc_command function, except for
3524 * argc, argv which are passed through.
3526 * @param domain_sid The domain sid acquired from the remote server.
3527 * @param cli A cli_state connected to the server.
3528 * @param mem_ctx Talloc context, destroyed on completion of the function.
3529 * @param argc Standard main() style argc.
3530 * @param argv Standard main() style argv. Initial components are already
3533 * @return Normal NTSTATUS return.
3536 static NTSTATUS
rpc_share_migrate_files_internals(struct net_context
*c
,
3537 const struct dom_sid
*domain_sid
,
3538 const char *domain_name
,
3539 struct cli_state
*cli
,
3540 struct rpc_pipe_client
*pipe_hnd
,
3541 TALLOC_CTX
*mem_ctx
,
3546 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3547 struct srvsvc_NetShareInfoCtr ctr_src
;
3550 struct copy_clistate cp_clistate
;
3551 bool got_src_share
= false;
3552 bool got_dst_share
= false;
3553 const char *mask
= "\\*";
3556 dst
= SMB_STRDUP(c
->opt_destination
?c
->opt_destination
:"127.0.0.1");
3558 nt_status
= NT_STATUS_NO_MEMORY
;
3562 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3565 if (!W_ERROR_IS_OK(result
))
3568 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3570 struct srvsvc_NetShareInfo502 info502
=
3571 ctr_src
.ctr
.ctr502
->array
[i
];
3573 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3576 /* one might not want to mirror whole discs :) */
3577 if (strequal(info502
.name
, "print$") || info502
.name
[1] == '$') {
3578 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3583 switch (net_mode_share
)
3585 case NET_MODE_SHARE_MIGRATE
:
3589 d_fprintf(stderr
, _("Unsupported mode %d\n"),
3593 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3596 c
->opt_acls
? _("including") : _("without"),
3597 c
->opt_attrs
? _("including") : _("without"),
3598 c
->opt_timestamps
? _("(preserving timestamps)") : "");
3600 cp_clistate
.mem_ctx
= mem_ctx
;
3601 cp_clistate
.cli_share_src
= NULL
;
3602 cp_clistate
.cli_share_dst
= NULL
;
3603 cp_clistate
.cwd
= NULL
;
3604 cp_clistate
.attribute
= aSYSTEM
| aHIDDEN
| aDIR
;
3607 /* open share source */
3608 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_src
,
3609 &cli
->dest_ss
, cli
->desthost
,
3610 info502
.name
, "A:");
3611 if (!NT_STATUS_IS_OK(nt_status
))
3614 got_src_share
= true;
3616 if (net_mode_share
== NET_MODE_SHARE_MIGRATE
) {
3617 /* open share destination */
3618 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_dst
,
3619 NULL
, dst
, info502
.name
, "A:");
3620 if (!NT_STATUS_IS_OK(nt_status
))
3623 got_dst_share
= true;
3626 if (!copy_top_level_perms(c
, &cp_clistate
, info502
.name
)) {
3627 d_fprintf(stderr
, _("Could not handle the top level "
3628 "directory permissions for the "
3629 "share: %s\n"), info502
.name
);
3630 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3634 if (!sync_files(&cp_clistate
, mask
)) {
3635 d_fprintf(stderr
, _("could not handle files for share: "
3636 "%s\n"), info502
.name
);
3637 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3642 nt_status
= NT_STATUS_OK
;
3647 cli_shutdown(cp_clistate
.cli_share_src
);
3650 cli_shutdown(cp_clistate
.cli_share_dst
);
3657 static int rpc_share_migrate_files(struct net_context
*c
, int argc
, const char **argv
)
3659 if (c
->display_usage
) {
3661 "net share migrate files\n"
3664 _("Migrate files to local server"));
3669 d_printf(_("no server to migrate\n"));
3673 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3674 rpc_share_migrate_files_internals
,
3679 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3681 * All parameters are provided by the run_rpc_command function, except for
3682 * argc, argv which are passed through.
3684 * @param domain_sid The domain sid acquired from the remote server.
3685 * @param cli A cli_state connected to the server.
3686 * @param mem_ctx Talloc context, destroyed on completion of the function.
3687 * @param argc Standard main() style argc.
3688 * @param argv Standard main() style argv. Initial components are already
3691 * @return Normal NTSTATUS return.
3694 static NTSTATUS
rpc_share_migrate_security_internals(struct net_context
*c
,
3695 const struct dom_sid
*domain_sid
,
3696 const char *domain_name
,
3697 struct cli_state
*cli
,
3698 struct rpc_pipe_client
*pipe_hnd
,
3699 TALLOC_CTX
*mem_ctx
,
3704 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3705 struct srvsvc_NetShareInfoCtr ctr_src
;
3706 union srvsvc_NetShareInfo info
;
3708 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3709 struct cli_state
*cli_dst
= NULL
;
3710 uint32 level
= 502; /* includes secdesc */
3711 uint32_t parm_error
= 0;
3713 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3716 if (!W_ERROR_IS_OK(result
))
3719 /* connect destination PI_SRVSVC */
3720 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3721 &ndr_table_srvsvc
.syntax_id
);
3722 if (!NT_STATUS_IS_OK(nt_status
))
3726 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3728 struct srvsvc_NetShareInfo502 info502
=
3729 ctr_src
.ctr
.ctr502
->array
[i
];
3731 /* reset error-code */
3732 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3734 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3737 printf(_("migrating: [%s], path: %s, comment: %s, including "
3739 info502
.name
, info502
.path
, info502
.comment
);
3742 display_sec_desc(info502
.sd_buf
.sd
);
3744 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3745 info
.info502
= &info502
;
3747 /* finally modify the share on the dst server */
3748 nt_status
= rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe
, mem_ctx
,
3749 srvsvc_pipe
->desthost
,
3755 if (!NT_STATUS_IS_OK(nt_status
) || !W_ERROR_IS_OK(result
)) {
3756 printf(_("cannot set share-acl: %s\n"),
3757 win_errstr(result
));
3763 nt_status
= NT_STATUS_OK
;
3767 cli_shutdown(cli_dst
);
3775 * Migrate share-acls from a RPC server to another.
3777 * @param argc Standard main() style argc.
3778 * @param argv Standard main() style argv. Initial components are already
3781 * @return A shell status integer (0 for success).
3783 static int rpc_share_migrate_security(struct net_context
*c
, int argc
,
3786 if (c
->display_usage
) {
3788 "net rpc share migrate security\n"
3791 _("Migrate share-acls to local server"));
3796 d_printf(_("no server to migrate\n"));
3800 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3801 rpc_share_migrate_security_internals
,
3806 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3807 * from one server to another.
3809 * @param argc Standard main() style argc.
3810 * @param argv Standard main() style argv. Initial components are already
3813 * @return A shell status integer (0 for success).
3816 static int rpc_share_migrate_all(struct net_context
*c
, int argc
,
3821 if (c
->display_usage
) {
3823 "net rpc share migrate all\n"
3826 _("Migrates shares including all share settings"));
3831 d_printf(_("no server to migrate\n"));
3835 /* order is important. we don't want to be locked out by the share-acl
3836 * before copying files - gd */
3838 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3839 rpc_share_migrate_shares_internals
, argc
, argv
);
3843 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3844 rpc_share_migrate_files_internals
, argc
, argv
);
3848 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3849 rpc_share_migrate_security_internals
, argc
,
3855 * 'net rpc share migrate' entrypoint.
3856 * @param argc Standard main() style argc.
3857 * @param argv Standard main() style argv. Initial components are already
3860 static int rpc_share_migrate(struct net_context
*c
, int argc
, const char **argv
)
3863 struct functable func
[] = {
3866 rpc_share_migrate_all
,
3868 N_("Migrate shares from remote to local server"),
3869 N_("net rpc share migrate all\n"
3870 " Migrate shares from remote to local server")
3874 rpc_share_migrate_files
,
3876 N_("Migrate files from remote to local server"),
3877 N_("net rpc share migrate files\n"
3878 " Migrate files from remote to local server")
3882 rpc_share_migrate_security
,
3884 N_("Migrate share-ACLs from remote to local server"),
3885 N_("net rpc share migrate security\n"
3886 " Migrate share-ACLs from remote to local server")
3890 rpc_share_migrate_shares
,
3892 N_("Migrate shares from remote to local server"),
3893 N_("net rpc share migrate shares\n"
3894 " Migrate shares from remote to local server")
3896 {NULL
, NULL
, 0, NULL
, NULL
}
3899 net_mode_share
= NET_MODE_SHARE_MIGRATE
;
3901 return net_run_function(c
, argc
, argv
, "net rpc share migrate", func
);
3907 struct dom_sid
*members
;
3910 static int num_server_aliases
;
3911 static struct full_alias
*server_aliases
;
3914 * Add an alias to the static list.
3916 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
3918 if (server_aliases
== NULL
)
3919 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
3921 server_aliases
[num_server_aliases
] = *alias
;
3922 num_server_aliases
+= 1;
3926 * For a specific domain on the server, fetch all the aliases
3927 * and their members. Add all of them to the server_aliases.
3930 static NTSTATUS
rpc_fetch_domain_aliases(struct rpc_pipe_client
*pipe_hnd
,
3931 TALLOC_CTX
*mem_ctx
,
3932 struct policy_handle
*connect_pol
,
3933 const struct dom_sid
*domain_sid
)
3935 uint32 start_idx
, max_entries
, num_entries
, i
;
3936 struct samr_SamArray
*groups
= NULL
;
3938 struct policy_handle domain_pol
;
3940 /* Get domain policy handle */
3942 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
3944 MAXIMUM_ALLOWED_ACCESS
,
3945 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
3947 if (!NT_STATUS_IS_OK(result
))
3954 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
3960 for (i
= 0; i
< num_entries
; i
++) {
3962 struct policy_handle alias_pol
;
3963 struct full_alias alias
;
3964 struct lsa_SidArray sid_array
;
3967 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
3969 MAXIMUM_ALLOWED_ACCESS
,
3970 groups
->entries
[i
].idx
,
3972 if (!NT_STATUS_IS_OK(result
))
3975 result
= rpccli_samr_GetMembersInAlias(pipe_hnd
, mem_ctx
,
3978 if (!NT_STATUS_IS_OK(result
))
3981 alias
.num_members
= sid_array
.num_sids
;
3983 result
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &alias_pol
);
3984 if (!NT_STATUS_IS_OK(result
))
3987 alias
.members
= NULL
;
3989 if (alias
.num_members
> 0) {
3990 alias
.members
= SMB_MALLOC_ARRAY(struct dom_sid
, alias
.num_members
);
3992 for (j
= 0; j
< alias
.num_members
; j
++)
3993 sid_copy(&alias
.members
[j
],
3994 sid_array
.sids
[j
].sid
);
3997 sid_compose(&alias
.sid
, domain_sid
,
3998 groups
->entries
[i
].idx
);
4000 push_alias(mem_ctx
, &alias
);
4002 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
4004 result
= NT_STATUS_OK
;
4007 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
4013 * Dump server_aliases as names for debugging purposes.
4016 static NTSTATUS
rpc_aliaslist_dump(struct net_context
*c
,
4017 const struct dom_sid
*domain_sid
,
4018 const char *domain_name
,
4019 struct cli_state
*cli
,
4020 struct rpc_pipe_client
*pipe_hnd
,
4021 TALLOC_CTX
*mem_ctx
,
4027 struct policy_handle lsa_pol
;
4029 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
4030 SEC_FLAG_MAXIMUM_ALLOWED
,
4032 if (!NT_STATUS_IS_OK(result
))
4035 for (i
=0; i
<num_server_aliases
; i
++) {
4038 enum lsa_SidType
*types
;
4041 struct full_alias
*alias
= &server_aliases
[i
];
4043 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
4045 &domains
, &names
, &types
);
4046 if (!NT_STATUS_IS_OK(result
))
4049 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
4051 if (alias
->num_members
== 0) {
4056 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
,
4059 &domains
, &names
, &types
);
4061 if (!NT_STATUS_IS_OK(result
) &&
4062 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
4065 for (j
=0; j
<alias
->num_members
; j
++)
4066 DEBUG(1, ("%s\\%s (%d); ",
4067 domains
[j
] ? domains
[j
] : "*unknown*",
4068 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
4072 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
4074 return NT_STATUS_OK
;
4078 * Fetch a list of all server aliases and their members into
4082 static NTSTATUS
rpc_aliaslist_internals(struct net_context
*c
,
4083 const struct dom_sid
*domain_sid
,
4084 const char *domain_name
,
4085 struct cli_state
*cli
,
4086 struct rpc_pipe_client
*pipe_hnd
,
4087 TALLOC_CTX
*mem_ctx
,
4092 struct policy_handle connect_pol
;
4094 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
4096 MAXIMUM_ALLOWED_ACCESS
,
4099 if (!NT_STATUS_IS_OK(result
))
4102 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4103 &global_sid_Builtin
);
4105 if (!NT_STATUS_IS_OK(result
))
4108 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4111 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
4116 static void init_user_token(NT_USER_TOKEN
*token
, struct dom_sid
*user_sid
)
4118 token
->num_sids
= 4;
4120 if (!(token
->user_sids
= SMB_MALLOC_ARRAY(struct dom_sid
, 4))) {
4121 d_fprintf(stderr
, "malloc %s\n",_("failed"));
4122 token
->num_sids
= 0;
4126 token
->user_sids
[0] = *user_sid
;
4127 sid_copy(&token
->user_sids
[1], &global_sid_World
);
4128 sid_copy(&token
->user_sids
[2], &global_sid_Network
);
4129 sid_copy(&token
->user_sids
[3], &global_sid_Authenticated_Users
);
4132 static void free_user_token(NT_USER_TOKEN
*token
)
4134 SAFE_FREE(token
->user_sids
);
4137 static void add_sid_to_token(NT_USER_TOKEN
*token
, struct dom_sid
*sid
)
4139 if (is_sid_in_token(token
, sid
))
4142 token
->user_sids
= SMB_REALLOC_ARRAY(token
->user_sids
, struct dom_sid
, token
->num_sids
+1);
4143 if (!token
->user_sids
) {
4147 sid_copy(&token
->user_sids
[token
->num_sids
], sid
);
4149 token
->num_sids
+= 1;
4154 NT_USER_TOKEN token
;
4157 static void dump_user_token(struct user_token
*token
)
4161 d_printf("%s\n", token
->name
);
4163 for (i
=0; i
<token
->token
.num_sids
; i
++) {
4164 d_printf(" %s\n", sid_string_tos(&token
->token
.user_sids
[i
]));
4168 static bool is_alias_member(struct dom_sid
*sid
, struct full_alias
*alias
)
4172 for (i
=0; i
<alias
->num_members
; i
++) {
4173 if (sid_compare(sid
, &alias
->members
[i
]) == 0)
4180 static void collect_sid_memberships(NT_USER_TOKEN
*token
, struct dom_sid sid
)
4184 for (i
=0; i
<num_server_aliases
; i
++) {
4185 if (is_alias_member(&sid
, &server_aliases
[i
]))
4186 add_sid_to_token(token
, &server_aliases
[i
].sid
);
4191 * We got a user token with all the SIDs we can know about without asking the
4192 * server directly. These are the user and domain group sids. All of these can
4193 * be members of aliases. So scan the list of aliases for each of the SIDs and
4194 * add them to the token.
4197 static void collect_alias_memberships(NT_USER_TOKEN
*token
)
4199 int num_global_sids
= token
->num_sids
;
4202 for (i
=0; i
<num_global_sids
; i
++) {
4203 collect_sid_memberships(token
, token
->user_sids
[i
]);
4207 static bool get_user_sids(const char *domain
, const char *user
, NT_USER_TOKEN
*token
)
4209 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4210 enum wbcSidType type
;
4212 struct wbcDomainSid wsid
;
4213 char *sid_str
= NULL
;
4214 struct dom_sid user_sid
;
4215 uint32_t num_groups
;
4216 gid_t
*groups
= NULL
;
4219 fstr_sprintf(full_name
, "%s%c%s",
4220 domain
, *lp_winbind_separator(), user
);
4222 /* First let's find out the user sid */
4224 wbc_status
= wbcLookupName(domain
, user
, &wsid
, &type
);
4226 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4227 DEBUG(1, ("winbind could not find %s: %s\n",
4228 full_name
, wbcErrorString(wbc_status
)));
4232 wbc_status
= wbcSidToString(&wsid
, &sid_str
);
4233 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4237 if (type
!= SID_NAME_USER
) {
4238 wbcFreeMemory(sid_str
);
4239 DEBUG(1, ("%s is not a user\n", full_name
));
4243 if (!string_to_sid(&user_sid
, sid_str
)) {
4244 DEBUG(1,("Could not convert sid %s from string\n", sid_str
));
4248 wbcFreeMemory(sid_str
);
4251 init_user_token(token
, &user_sid
);
4253 /* And now the groups winbind knows about */
4255 wbc_status
= wbcGetGroups(full_name
, &num_groups
, &groups
);
4256 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4257 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4258 full_name
, wbcErrorString(wbc_status
)));
4262 for (i
= 0; i
< num_groups
; i
++) {
4263 gid_t gid
= groups
[i
];
4266 wbc_status
= wbcGidToSid(gid
, &wsid
);
4267 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4268 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4269 (unsigned int)gid
, wbcErrorString(wbc_status
)));
4270 wbcFreeMemory(groups
);
4274 wbc_status
= wbcSidToString(&wsid
, &sid_str
);
4275 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4276 wbcFreeMemory(groups
);
4280 DEBUG(3, (" %s\n", sid_str
));
4282 string_to_sid(&sid
, sid_str
);
4283 wbcFreeMemory(sid_str
);
4286 add_sid_to_token(token
, &sid
);
4288 wbcFreeMemory(groups
);
4294 * Get a list of all user tokens we want to look at
4297 static bool get_user_tokens(struct net_context
*c
, int *num_tokens
,
4298 struct user_token
**user_tokens
)
4300 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4301 uint32_t i
, num_users
;
4303 struct user_token
*result
;
4304 TALLOC_CTX
*frame
= NULL
;
4306 if (lp_winbind_use_default_domain() &&
4307 (c
->opt_target_workgroup
== NULL
)) {
4308 d_fprintf(stderr
, _("winbind use default domain = yes set, "
4309 "please specify a workgroup\n"));
4313 /* Send request to winbind daemon */
4315 wbc_status
= wbcListUsers(NULL
, &num_users
, &users
);
4316 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4317 DEBUG(1, (_("winbind could not list users: %s\n"),
4318 wbcErrorString(wbc_status
)));
4322 result
= SMB_MALLOC_ARRAY(struct user_token
, num_users
);
4324 if (result
== NULL
) {
4325 DEBUG(1, ("Could not malloc sid array\n"));
4326 wbcFreeMemory(users
);
4330 frame
= talloc_stackframe();
4331 for (i
=0; i
< num_users
; i
++) {
4332 fstring domain
, user
;
4335 fstrcpy(result
[i
].name
, users
[i
]);
4337 p
= strchr(users
[i
], *lp_winbind_separator());
4339 DEBUG(3, ("%s\n", users
[i
]));
4342 fstrcpy(domain
, c
->opt_target_workgroup
);
4343 fstrcpy(user
, users
[i
]);
4346 fstrcpy(domain
, users
[i
]);
4351 get_user_sids(domain
, user
, &(result
[i
].token
));
4355 wbcFreeMemory(users
);
4357 *num_tokens
= num_users
;
4358 *user_tokens
= result
;
4363 static bool get_user_tokens_from_file(FILE *f
,
4365 struct user_token
**tokens
)
4367 struct user_token
*token
= NULL
;
4372 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4376 if ((strlen(line
) > 0) && (line
[strlen(line
)-1] == '\n')) {
4377 line
[strlen(line
)-1] = '\0';
4380 if (line
[0] == ' ') {
4384 if(!string_to_sid(&sid
, &line
[1])) {
4385 DEBUG(1,("get_user_tokens_from_file: Could "
4386 "not convert sid %s \n",&line
[1]));
4390 if (token
== NULL
) {
4391 DEBUG(0, ("File does not begin with username"));
4395 add_sid_to_token(&token
->token
, &sid
);
4399 /* And a new user... */
4402 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4403 if (*tokens
== NULL
) {
4404 DEBUG(0, ("Could not realloc tokens\n"));
4408 token
= &((*tokens
)[*num_tokens
-1]);
4410 fstrcpy(token
->name
, line
);
4411 token
->token
.num_sids
= 0;
4412 token
->token
.user_sids
= NULL
;
4421 * Show the list of all users that have access to a share
4424 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4425 TALLOC_CTX
*mem_ctx
,
4426 const char *netname
,
4428 struct user_token
*tokens
)
4431 struct security_descriptor
*share_sd
= NULL
;
4432 struct security_descriptor
*root_sd
= NULL
;
4433 struct cli_state
*cli
= rpc_pipe_np_smb_conn(pipe_hnd
);
4435 union srvsvc_NetShareInfo info
;
4440 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
4447 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4448 DEBUG(1, ("Coult not query secdesc for share %s\n",
4453 share_sd
= info
.info502
->sd_buf
.sd
;
4454 if (share_sd
== NULL
) {
4455 DEBUG(1, ("Got no secdesc for share %s\n",
4461 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli
, netname
, "A:", "", 0))) {
4465 if (!NT_STATUS_IS_OK(cli_ntcreate(cli
, "\\", 0, READ_CONTROL_ACCESS
, 0,
4466 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0, &fnum
))) {
4467 root_sd
= cli_query_secdesc(cli
, fnum
, mem_ctx
);
4470 for (i
=0; i
<num_tokens
; i
++) {
4473 if (share_sd
!= NULL
) {
4474 status
= se_access_check(share_sd
, &tokens
[i
].token
,
4477 if (!NT_STATUS_IS_OK(status
)) {
4478 DEBUG(1, ("Could not check share_sd for "
4485 if (root_sd
== NULL
) {
4486 d_printf(" %s\n", tokens
[i
].name
);
4490 status
= se_access_check(root_sd
, &tokens
[i
].token
,
4492 if (!NT_STATUS_IS_OK(status
)) {
4493 DEBUG(1, ("Could not check root_sd for user %s\n",
4497 d_printf(" %s\n", tokens
[i
].name
);
4500 if (fnum
!= (uint16_t)-1)
4501 cli_close(cli
, fnum
);
4513 static void collect_share(const char *name
, uint32 m
,
4514 const char *comment
, void *state
)
4516 struct share_list
*share_list
= (struct share_list
*)state
;
4518 if (m
!= STYPE_DISKTREE
)
4521 share_list
->num_shares
+= 1;
4522 share_list
->shares
= SMB_REALLOC_ARRAY(share_list
->shares
, char *, share_list
->num_shares
);
4523 if (!share_list
->shares
) {
4524 share_list
->num_shares
= 0;
4527 share_list
->shares
[share_list
->num_shares
-1] = SMB_STRDUP(name
);
4531 * List shares on a remote RPC server, including the security descriptors.
4533 * All parameters are provided by the run_rpc_command function, except for
4534 * argc, argv which are passed through.
4536 * @param domain_sid The domain sid acquired from the remote server.
4537 * @param cli A cli_state connected to the server.
4538 * @param mem_ctx Talloc context, destroyed on completion of the function.
4539 * @param argc Standard main() style argc.
4540 * @param argv Standard main() style argv. Initial components are already
4543 * @return Normal NTSTATUS return.
4546 static NTSTATUS
rpc_share_allowedusers_internals(struct net_context
*c
,
4547 const struct dom_sid
*domain_sid
,
4548 const char *domain_name
,
4549 struct cli_state
*cli
,
4550 struct rpc_pipe_client
*pipe_hnd
,
4551 TALLOC_CTX
*mem_ctx
,
4560 struct user_token
*tokens
= NULL
;
4563 struct share_list share_list
;
4568 f
= fopen(argv
[0], "r");
4572 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4573 return NT_STATUS_UNSUCCESSFUL
;
4576 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
4582 DEBUG(0, ("Could not read users from file\n"));
4583 return NT_STATUS_UNSUCCESSFUL
;
4586 for (i
=0; i
<num_tokens
; i
++)
4587 collect_alias_memberships(&tokens
[i
].token
);
4589 share_list
.num_shares
= 0;
4590 share_list
.shares
= NULL
;
4592 ret
= cli_RNetShareEnum(cli
, collect_share
, &share_list
);
4595 DEBUG(0, ("Error returning browse list: %s\n",
4600 for (i
= 0; i
< share_list
.num_shares
; i
++) {
4601 char *netname
= share_list
.shares
[i
];
4603 if (netname
[strlen(netname
)-1] == '$')
4606 d_printf("%s\n", netname
);
4608 show_userlist(pipe_hnd
, mem_ctx
, netname
,
4609 num_tokens
, tokens
);
4612 for (i
=0; i
<num_tokens
; i
++) {
4613 free_user_token(&tokens
[i
].token
);
4616 SAFE_FREE(share_list
.shares
);
4618 return NT_STATUS_OK
;
4621 static int rpc_share_allowedusers(struct net_context
*c
, int argc
,
4626 if (c
->display_usage
) {
4628 "net rpc share allowedusers\n"
4631 _("List allowed users"));
4635 result
= run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
4636 rpc_aliaslist_internals
,
4641 result
= run_rpc_command(c
, NULL
, &ndr_table_lsarpc
.syntax_id
, 0,
4647 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4648 rpc_share_allowedusers_internals
,
4652 int net_usersidlist(struct net_context
*c
, int argc
, const char **argv
)
4655 struct user_token
*tokens
= NULL
;
4659 net_usersidlist_usage(c
, argc
, argv
);
4663 if (!get_user_tokens(c
, &num_tokens
, &tokens
)) {
4664 DEBUG(0, ("Could not get the user/sid list\n"));
4668 for (i
=0; i
<num_tokens
; i
++) {
4669 dump_user_token(&tokens
[i
]);
4670 free_user_token(&tokens
[i
].token
);
4677 int net_usersidlist_usage(struct net_context
*c
, int argc
, const char **argv
)
4679 d_printf(_("net usersidlist\n"
4680 "\tprints out a list of all users the running winbind knows\n"
4681 "\tabout, together with all their SIDs. This is used as\n"
4682 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
4684 net_common_flags_usage(c
, argc
, argv
);
4689 * 'net rpc share' entrypoint.
4690 * @param argc Standard main() style argc.
4691 * @param argv Standard main() style argv. Initial components are already
4695 int net_rpc_share(struct net_context
*c
, int argc
, const char **argv
)
4697 NET_API_STATUS status
;
4699 struct functable func
[] = {
4705 N_("net rpc share add\n"
4713 N_("net rpc share delete\n"
4718 rpc_share_allowedusers
,
4720 N_("Modify allowed users"),
4721 N_("net rpc share allowedusers\n"
4722 " Modify allowed users")
4728 N_("Migrate share to local server"),
4729 N_("net rpc share migrate\n"
4730 " Migrate share to local server")
4737 N_("net rpc share list\n"
4740 {NULL
, NULL
, 0, NULL
, NULL
}
4743 status
= libnetapi_init(&c
->netapi_ctx
);
4747 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
4748 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
4749 if (c
->opt_kerberos
) {
4750 libnetapi_set_use_kerberos(c
->netapi_ctx
);
4754 if (c
->display_usage
) {
4759 " Alias for net rpc share list\n"));
4760 net_display_usage_from_functable(func
);
4764 return rpc_share_list(c
, argc
, argv
);
4767 return net_run_function(c
, argc
, argv
, "net rpc share", func
);
4770 static NTSTATUS
rpc_sh_share_list(struct net_context
*c
,
4771 TALLOC_CTX
*mem_ctx
,
4772 struct rpc_sh_ctx
*ctx
,
4773 struct rpc_pipe_client
*pipe_hnd
,
4774 int argc
, const char **argv
)
4777 return werror_to_ntstatus(W_ERROR(rpc_share_list(c
, argc
, argv
)));
4780 static NTSTATUS
rpc_sh_share_add(struct net_context
*c
,
4781 TALLOC_CTX
*mem_ctx
,
4782 struct rpc_sh_ctx
*ctx
,
4783 struct rpc_pipe_client
*pipe_hnd
,
4784 int argc
, const char **argv
)
4786 NET_API_STATUS status
;
4787 uint32_t parm_err
= 0;
4788 struct SHARE_INFO_2 i2
;
4790 if ((argc
< 2) || (argc
> 3)) {
4791 d_fprintf(stderr
, _("Usage: %s <share> <path> [comment]\n"),
4793 return NT_STATUS_INVALID_PARAMETER
;
4796 i2
.shi2_netname
= argv
[0];
4797 i2
.shi2_type
= STYPE_DISKTREE
;
4798 i2
.shi2_remark
= (argc
== 3) ? argv
[2] : "";
4799 i2
.shi2_permissions
= 0;
4800 i2
.shi2_max_uses
= 0;
4801 i2
.shi2_current_uses
= 0;
4802 i2
.shi2_path
= argv
[1];
4803 i2
.shi2_passwd
= NULL
;
4805 status
= NetShareAdd(pipe_hnd
->desthost
,
4810 return werror_to_ntstatus(W_ERROR(status
));
4813 static NTSTATUS
rpc_sh_share_delete(struct net_context
*c
,
4814 TALLOC_CTX
*mem_ctx
,
4815 struct rpc_sh_ctx
*ctx
,
4816 struct rpc_pipe_client
*pipe_hnd
,
4817 int argc
, const char **argv
)
4820 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
4821 return NT_STATUS_INVALID_PARAMETER
;
4824 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd
->desthost
, argv
[0], 0)));
4827 static NTSTATUS
rpc_sh_share_info(struct net_context
*c
,
4828 TALLOC_CTX
*mem_ctx
,
4829 struct rpc_sh_ctx
*ctx
,
4830 struct rpc_pipe_client
*pipe_hnd
,
4831 int argc
, const char **argv
)
4833 union srvsvc_NetShareInfo info
;
4838 d_fprintf(stderr
, "%s %s <share>\n", _("Usage:"), ctx
->whoami
);
4839 return NT_STATUS_INVALID_PARAMETER
;
4842 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
4848 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4852 d_printf(_("Name: %s\n"), info
.info2
->name
);
4853 d_printf(_("Comment: %s\n"), info
.info2
->comment
);
4854 d_printf(_("Path: %s\n"), info
.info2
->path
);
4855 d_printf(_("Password: %s\n"), info
.info2
->password
);
4858 return werror_to_ntstatus(result
);
4861 struct rpc_sh_cmd
*net_rpc_share_cmds(struct net_context
*c
, TALLOC_CTX
*mem_ctx
,
4862 struct rpc_sh_ctx
*ctx
)
4864 static struct rpc_sh_cmd cmds
[] = {
4866 { "list", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_list
,
4867 N_("List available shares") },
4869 { "add", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_add
,
4870 N_("Add a share") },
4872 { "delete", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_delete
,
4873 N_("Delete a share") },
4875 { "info", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_info
,
4876 N_("Get information about a share") },
4878 { NULL
, NULL
, 0, NULL
, NULL
}
4884 /****************************************************************************/
4886 static int rpc_file_usage(struct net_context
*c
, int argc
, const char **argv
)
4888 return net_file_usage(c
, argc
, argv
);
4892 * Close a file on a remote RPC server.
4894 * @param argc Standard main() style argc.
4895 * @param argv Standard main() style argv. Initial components are already
4898 * @return A shell status integer (0 for success).
4900 static int rpc_file_close(struct net_context
*c
, int argc
, const char **argv
)
4902 if (argc
< 1 || c
->display_usage
) {
4903 return rpc_file_usage(c
, argc
, argv
);
4906 return NetFileClose(c
->opt_host
, atoi(argv
[0]));
4910 * Formatted print of open file info
4912 * @param r struct FILE_INFO_3 contents
4915 static void display_file_info_3(struct FILE_INFO_3
*r
)
4917 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4918 r
->fi3_id
, r
->fi3_username
, r
->fi3_permissions
,
4919 r
->fi3_num_locks
, r
->fi3_pathname
);
4923 * List files for a user on a remote RPC server.
4925 * @param argc Standard main() style argc.
4926 * @param argv Standard main() style argv. Initial components are already
4929 * @return A shell status integer (0 for success)..
4932 static int rpc_file_user(struct net_context
*c
, int argc
, const char **argv
)
4934 NET_API_STATUS status
;
4935 uint32 preferred_len
= 0xffffffff, i
;
4936 const char *username
=NULL
;
4937 uint32_t total_entries
= 0;
4938 uint32_t entries_read
= 0;
4939 uint32_t resume_handle
= 0;
4940 struct FILE_INFO_3
*i3
= NULL
;
4942 if (c
->display_usage
) {
4943 return rpc_file_usage(c
, argc
, argv
);
4946 /* if argc > 0, must be user command */
4948 username
= smb_xstrdup(argv
[0]);
4951 status
= NetFileEnum(c
->opt_host
,
4955 (uint8_t **)(void *)&i3
,
4965 /* Display results */
4968 "\nEnumerating open files on remote server:\n\n"
4969 "\nFileId Opened by Perms Locks Path"
4970 "\n------ --------- ----- ----- ---- \n"));
4971 for (i
= 0; i
< entries_read
; i
++) {
4972 display_file_info_3(&i3
[i
]);
4979 * 'net rpc file' entrypoint.
4980 * @param argc Standard main() style argc.
4981 * @param argv Standard main() style argv. Initial components are already
4985 int net_rpc_file(struct net_context
*c
, int argc
, const char **argv
)
4987 NET_API_STATUS status
;
4989 struct functable func
[] = {
4994 N_("Close opened file"),
4995 N_("net rpc file close\n"
4996 " Close opened file")
5002 N_("List files opened by user"),
5003 N_("net rpc file user\n"
5004 " List files opened by user")
5011 N_("Display information about opened file"),
5012 N_("net rpc file info\n"
5013 " Display information about opened file")
5016 {NULL
, NULL
, 0, NULL
, NULL
}
5019 status
= libnetapi_init(&c
->netapi_ctx
);
5023 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
5024 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
5025 if (c
->opt_kerberos
) {
5026 libnetapi_set_use_kerberos(c
->netapi_ctx
);
5030 if (c
->display_usage
) {
5031 d_printf(_("Usage:\n"));
5032 d_printf(_("net rpc file\n"
5033 " List opened files\n"));
5034 net_display_usage_from_functable(func
);
5038 return rpc_file_user(c
, argc
, argv
);
5041 return net_run_function(c
, argc
, argv
, "net rpc file", func
);
5045 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5047 * All parameters are provided by the run_rpc_command function, except for
5048 * argc, argv which are passed through.
5050 * @param c A net_context structure.
5051 * @param domain_sid The domain sid acquired from the remote server.
5052 * @param cli A cli_state connected to the server.
5053 * @param mem_ctx Talloc context, destroyed on completion of the function.
5054 * @param argc Standard main() style argc.
5055 * @param argv Standard main() style argv. Initial components are already
5058 * @return Normal NTSTATUS return.
5061 static NTSTATUS
rpc_shutdown_abort_internals(struct net_context
*c
,
5062 const struct dom_sid
*domain_sid
,
5063 const char *domain_name
,
5064 struct cli_state
*cli
,
5065 struct rpc_pipe_client
*pipe_hnd
,
5066 TALLOC_CTX
*mem_ctx
,
5070 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5072 result
= rpccli_initshutdown_Abort(pipe_hnd
, mem_ctx
, NULL
, NULL
);
5074 if (NT_STATUS_IS_OK(result
)) {
5075 d_printf(_("\nShutdown successfully aborted\n"));
5076 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5078 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5084 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5086 * All parameters are provided by the run_rpc_command function, except for
5087 * argc, argv which are passed through.
5089 * @param c A net_context structure.
5090 * @param domain_sid The domain sid acquired from the remote server.
5091 * @param cli A cli_state connected to the server.
5092 * @param mem_ctx Talloc context, destroyed on completion of the function.
5093 * @param argc Standard main() style argc.
5094 * @param argv Standard main() style argv. Initial components are already
5097 * @return Normal NTSTATUS return.
5100 static NTSTATUS
rpc_reg_shutdown_abort_internals(struct net_context
*c
,
5101 const struct dom_sid
*domain_sid
,
5102 const char *domain_name
,
5103 struct cli_state
*cli
,
5104 struct rpc_pipe_client
*pipe_hnd
,
5105 TALLOC_CTX
*mem_ctx
,
5109 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5111 result
= rpccli_winreg_AbortSystemShutdown(pipe_hnd
, mem_ctx
, NULL
, NULL
);
5113 if (NT_STATUS_IS_OK(result
)) {
5114 d_printf(_("\nShutdown successfully aborted\n"));
5115 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5117 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5123 * ABORT the shutdown of a remote RPC server.
5125 * @param argc Standard main() style argc.
5126 * @param argv Standard main() style argv. Initial components are already
5129 * @return A shell status integer (0 for success).
5132 static int rpc_shutdown_abort(struct net_context
*c
, int argc
,
5137 if (c
->display_usage
) {
5139 "net rpc abortshutdown\n"
5142 _("Abort a scheduled shutdown"));
5146 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
5147 rpc_shutdown_abort_internals
, argc
, argv
);
5152 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5154 return run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
5155 rpc_reg_shutdown_abort_internals
,
5160 * Shut down a remote RPC Server via initshutdown pipe.
5162 * All parameters are provided by the run_rpc_command function, except for
5163 * argc, argv which are passed through.
5165 * @param c A net_context structure.
5166 * @param domain_sid The domain sid acquired from the remote server.
5167 * @param cli A cli_state connected to the server.
5168 * @param mem_ctx Talloc context, destroyed on completion of the function.
5169 * @param argc Standard main() style argc.
5170 * @param argv Standard main() style argv. Initial components are already
5173 * @return Normal NTSTATUS return.
5176 NTSTATUS
rpc_init_shutdown_internals(struct net_context
*c
,
5177 const struct dom_sid
*domain_sid
,
5178 const char *domain_name
,
5179 struct cli_state
*cli
,
5180 struct rpc_pipe_client
*pipe_hnd
,
5181 TALLOC_CTX
*mem_ctx
,
5185 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5186 const char *msg
= N_("This machine will be shutdown shortly");
5187 uint32 timeout
= 20;
5188 struct lsa_StringLarge msg_string
;
5190 if (c
->opt_comment
) {
5191 msg
= c
->opt_comment
;
5193 if (c
->opt_timeout
) {
5194 timeout
= c
->opt_timeout
;
5197 msg_string
.string
= msg
;
5199 /* create an entry */
5200 result
= rpccli_initshutdown_Init(pipe_hnd
, mem_ctx
, NULL
,
5201 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5204 if (NT_STATUS_IS_OK(result
)) {
5205 d_printf(_("\nShutdown of remote machine succeeded\n"));
5206 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5208 DEBUG(1,("Shutdown of remote machine failed!\n"));
5214 * Shut down a remote RPC Server via winreg pipe.
5216 * All parameters are provided by the run_rpc_command function, except for
5217 * argc, argv which are passed through.
5219 * @param c A net_context structure.
5220 * @param domain_sid The domain sid acquired from the remote server.
5221 * @param cli A cli_state connected to the server.
5222 * @param mem_ctx Talloc context, destroyed on completion of the function.
5223 * @param argc Standard main() style argc.
5224 * @param argv Standard main() style argv. Initial components are already
5227 * @return Normal NTSTATUS return.
5230 NTSTATUS
rpc_reg_shutdown_internals(struct net_context
*c
,
5231 const struct dom_sid
*domain_sid
,
5232 const char *domain_name
,
5233 struct cli_state
*cli
,
5234 struct rpc_pipe_client
*pipe_hnd
,
5235 TALLOC_CTX
*mem_ctx
,
5239 const char *msg
= N_("This machine will be shutdown shortly");
5240 uint32 timeout
= 20;
5241 struct lsa_StringLarge msg_string
;
5245 if (c
->opt_comment
) {
5246 msg
= c
->opt_comment
;
5248 msg_string
.string
= msg
;
5250 if (c
->opt_timeout
) {
5251 timeout
= c
->opt_timeout
;
5254 /* create an entry */
5255 result
= rpccli_winreg_InitiateSystemShutdown(pipe_hnd
, mem_ctx
, NULL
,
5256 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5259 if (NT_STATUS_IS_OK(result
)) {
5260 d_printf(_("\nShutdown of remote machine succeeded\n"));
5262 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5263 if ( W_ERROR_EQUAL(werr
, WERR_MACHINE_LOCKED
) )
5264 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5266 d_fprintf(stderr
, "\nresult was: %s\n", win_errstr(werr
));
5273 * Shut down a remote RPC server.
5275 * @param argc Standard main() style argc.
5276 * @param argv Standard main() style argv. Initial components are already
5279 * @return A shell status integer (0 for success).
5282 static int rpc_shutdown(struct net_context
*c
, int argc
, const char **argv
)
5286 if (c
->display_usage
) {
5288 "net rpc shutdown\n"
5291 _("Shut down a remote RPC server"));
5295 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
5296 rpc_init_shutdown_internals
, argc
, argv
);
5299 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5300 rc
= run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
5301 rpc_reg_shutdown_internals
, argc
, argv
);
5307 /***************************************************************************
5308 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5309 ***************************************************************************/
5312 * Add interdomain trust account to the RPC server.
5313 * All parameters (except for argc and argv) are passed by run_rpc_command
5316 * @param c A net_context structure.
5317 * @param domain_sid The domain sid acquired from the server.
5318 * @param cli A cli_state connected to the server.
5319 * @param mem_ctx Talloc context, destroyed on completion of the function.
5320 * @param argc Standard main() style argc.
5321 * @param argv Standard main() style argv. Initial components are already
5324 * @return normal NTSTATUS return code.
5327 static NTSTATUS
rpc_trustdom_add_internals(struct net_context
*c
,
5328 const struct dom_sid
*domain_sid
,
5329 const char *domain_name
,
5330 struct cli_state
*cli
,
5331 struct rpc_pipe_client
*pipe_hnd
,
5332 TALLOC_CTX
*mem_ctx
,
5336 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5337 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5339 struct lsa_String lsa_acct_name
;
5341 uint32 acct_flags
=0;
5343 uint32_t access_granted
= 0;
5344 union samr_UserInfo info
;
5345 unsigned int orig_timeout
;
5350 _(" net rpc trustdom add <domain_name> "
5351 "<trust password>\n"));
5352 return NT_STATUS_INVALID_PARAMETER
;
5356 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5359 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5360 return NT_STATUS_NO_MEMORY
;
5363 strupper_m(acct_name
);
5365 init_lsa_String(&lsa_acct_name
, acct_name
);
5367 /* Get samr policy handle */
5368 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
5370 MAXIMUM_ALLOWED_ACCESS
,
5372 if (!NT_STATUS_IS_OK(result
)) {
5376 /* Get domain policy handle */
5377 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
5379 MAXIMUM_ALLOWED_ACCESS
,
5380 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5382 if (!NT_STATUS_IS_OK(result
)) {
5386 /* This call can take a long time - allow the server to time out.
5387 * 35 seconds should do it. */
5389 orig_timeout
= rpccli_set_timeout(pipe_hnd
, 35000);
5391 /* Create trusting domain's account */
5392 acb_info
= ACB_NORMAL
;
5393 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
5394 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
5395 SAMR_USER_ACCESS_SET_PASSWORD
|
5396 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
5397 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
5399 result
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
5408 /* And restore our original timeout. */
5409 rpccli_set_timeout(pipe_hnd
, orig_timeout
);
5411 if (!NT_STATUS_IS_OK(result
)) {
5412 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5413 acct_name
, nt_errstr(result
));
5418 struct samr_CryptPassword crypt_pwd
;
5420 ZERO_STRUCT(info
.info23
);
5422 init_samr_CryptPassword(argv
[1],
5423 &cli
->user_session_key
,
5426 info
.info23
.info
.fields_present
= SAMR_FIELD_ACCT_FLAGS
|
5427 SAMR_FIELD_NT_PASSWORD_PRESENT
;
5428 info
.info23
.info
.acct_flags
= ACB_DOMTRUST
;
5429 info
.info23
.password
= crypt_pwd
;
5431 result
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
5436 if (!NT_STATUS_IS_OK(result
)) {
5437 DEBUG(0,("Could not set trust account password: %s\n",
5438 nt_errstr(result
)));
5444 SAFE_FREE(acct_name
);
5449 * Create interdomain trust account for a remote domain.
5451 * @param argc Standard argc.
5452 * @param argv Standard argv without initial components.
5454 * @return Integer status (0 means success).
5457 static int rpc_trustdom_add(struct net_context
*c
, int argc
, const char **argv
)
5459 if (argc
> 0 && !c
->display_usage
) {
5460 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5461 rpc_trustdom_add_internals
, argc
, argv
);
5465 _("net rpc trustdom add <domain_name> <trust "
5473 * Remove interdomain trust account from the RPC server.
5474 * All parameters (except for argc and argv) are passed by run_rpc_command
5477 * @param c A net_context structure.
5478 * @param domain_sid The domain sid acquired from the server.
5479 * @param cli A cli_state connected to the server.
5480 * @param mem_ctx Talloc context, destroyed on completion of the function.
5481 * @param argc Standard main() style argc.
5482 * @param argv Standard main() style argv. Initial components are already
5485 * @return normal NTSTATUS return code.
5488 static NTSTATUS
rpc_trustdom_del_internals(struct net_context
*c
,
5489 const struct dom_sid
*domain_sid
,
5490 const char *domain_name
,
5491 struct cli_state
*cli
,
5492 struct rpc_pipe_client
*pipe_hnd
,
5493 TALLOC_CTX
*mem_ctx
,
5497 struct policy_handle connect_pol
, domain_pol
, user_pol
;
5498 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5500 struct dom_sid trust_acct_sid
;
5501 struct samr_Ids user_rids
, name_types
;
5502 struct lsa_String lsa_acct_name
;
5507 _(" net rpc trustdom del <domain_name>\n"));
5508 return NT_STATUS_INVALID_PARAMETER
;
5512 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5514 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
5516 if (acct_name
== NULL
)
5517 return NT_STATUS_NO_MEMORY
;
5519 strupper_m(acct_name
);
5521 /* Get samr policy handle */
5522 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
5524 MAXIMUM_ALLOWED_ACCESS
,
5526 if (!NT_STATUS_IS_OK(result
)) {
5530 /* Get domain policy handle */
5531 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
5533 MAXIMUM_ALLOWED_ACCESS
,
5534 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5536 if (!NT_STATUS_IS_OK(result
)) {
5540 init_lsa_String(&lsa_acct_name
, acct_name
);
5542 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
5549 if (!NT_STATUS_IS_OK(result
)) {
5550 d_printf(_("net rpc trustdom del: LookupNames on user %s "
5552 acct_name
, nt_errstr(result
) );
5556 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
5558 MAXIMUM_ALLOWED_ACCESS
,
5562 if (!NT_STATUS_IS_OK(result
)) {
5563 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
5565 acct_name
, nt_errstr(result
) );
5569 /* append the rid to the domain sid */
5570 if (!sid_compose(&trust_acct_sid
, domain_sid
, user_rids
.ids
[0])) {
5574 /* remove the sid */
5576 result
= rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd
, mem_ctx
,
5579 if (!NT_STATUS_IS_OK(result
)) {
5580 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
5581 " on user %s failed %s\n"),
5582 acct_name
, nt_errstr(result
) );
5588 result
= rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
5591 if (!NT_STATUS_IS_OK(result
)) {
5592 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
5594 acct_name
, nt_errstr(result
) );
5598 if (!NT_STATUS_IS_OK(result
)) {
5599 d_printf(_("Could not set trust account password: %s\n"),
5609 * Delete interdomain trust account for a remote domain.
5611 * @param argc Standard argc.
5612 * @param argv Standard argv without initial components.
5614 * @return Integer status (0 means success).
5617 static int rpc_trustdom_del(struct net_context
*c
, int argc
, const char **argv
)
5619 if (argc
> 0 && !c
->display_usage
) {
5620 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5621 rpc_trustdom_del_internals
, argc
, argv
);
5625 _("net rpc trustdom del <domain>\n"));
5630 static NTSTATUS
rpc_trustdom_get_pdc(struct net_context
*c
,
5631 struct cli_state
*cli
,
5632 TALLOC_CTX
*mem_ctx
,
5633 const char *domain_name
)
5635 char *dc_name
= NULL
;
5636 const char *buffer
= NULL
;
5637 struct rpc_pipe_client
*netr
;
5640 /* Use NetServerEnum2 */
5642 if (cli_get_pdc_name(cli
, domain_name
, &dc_name
)) {
5644 return NT_STATUS_OK
;
5647 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5648 for domain %s\n", domain_name
));
5650 /* Try netr_GetDcName */
5652 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
5654 if (!NT_STATUS_IS_OK(status
)) {
5658 status
= rpccli_netr_GetDcName(netr
, mem_ctx
,
5665 if (NT_STATUS_IS_OK(status
)) {
5669 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5670 for domain %s\n", domain_name
));
5676 * Establish trust relationship to a trusting domain.
5677 * Interdomain account must already be created on remote PDC.
5679 * @param c A net_context structure.
5680 * @param argc Standard argc.
5681 * @param argv Standard argv without initial components.
5683 * @return Integer status (0 means success).
5686 static int rpc_trustdom_establish(struct net_context
*c
, int argc
,
5689 struct cli_state
*cli
= NULL
;
5690 struct sockaddr_storage server_ss
;
5691 struct rpc_pipe_client
*pipe_hnd
= NULL
;
5692 struct policy_handle connect_hnd
;
5693 TALLOC_CTX
*mem_ctx
;
5695 struct dom_sid
*domain_sid
;
5700 union lsa_PolicyInformation
*info
= NULL
;
5703 * Connect to \\server\ipc$ as 'our domain' account with password
5706 if (argc
!= 1 || c
->display_usage
) {
5709 _("net rpc trustdom establish <domain_name>\n"));
5713 domain_name
= smb_xstrdup(argv
[0]);
5714 strupper_m(domain_name
);
5716 /* account name used at first is our domain's name with '$' */
5717 if (asprintf(&acct_name
, "%s$", lp_workgroup()) == -1) {
5720 strupper_m(acct_name
);
5723 * opt_workgroup will be used by connection functions further,
5724 * hence it should be set to remote domain name instead of ours
5726 if (c
->opt_workgroup
) {
5727 c
->opt_workgroup
= smb_xstrdup(domain_name
);
5730 c
->opt_user_name
= acct_name
;
5732 /* find the domain controller */
5733 if (!net_find_pdc(&server_ss
, pdc_name
, domain_name
)) {
5734 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
5738 /* connect to ipc$ as username/password */
5739 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
, pdc_name
);
5740 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
5742 /* Is it trusting domain account for sure ? */
5743 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5744 nt_errstr(nt_status
)));
5748 /* store who we connected to */
5750 saf_store( domain_name
, pdc_name
);
5753 * Connect to \\server\ipc$ again (this time anonymously)
5756 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
5759 if (NT_STATUS_IS_ERR(nt_status
)) {
5760 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5761 domain_name
, nt_errstr(nt_status
)));
5765 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
5766 "domain %s", domain_name
))) {
5767 DEBUG(0, ("talloc_init() failed\n"));
5772 /* Make sure we're talking to a proper server */
5774 nt_status
= rpc_trustdom_get_pdc(c
, cli
, mem_ctx
, domain_name
);
5775 if (!NT_STATUS_IS_OK(nt_status
)) {
5777 talloc_destroy(mem_ctx
);
5782 * Call LsaOpenPolicy and LsaQueryInfo
5785 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
5787 if (!NT_STATUS_IS_OK(nt_status
)) {
5788 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
5790 talloc_destroy(mem_ctx
);
5794 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, true, KEY_QUERY_VALUE
,
5796 if (NT_STATUS_IS_ERR(nt_status
)) {
5797 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5798 nt_errstr(nt_status
)));
5800 talloc_destroy(mem_ctx
);
5804 /* Querying info level 5 */
5806 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
5808 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
5810 if (NT_STATUS_IS_ERR(nt_status
)) {
5811 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5812 nt_errstr(nt_status
)));
5814 talloc_destroy(mem_ctx
);
5818 domain_sid
= info
->account_domain
.sid
;
5820 /* There should be actually query info level 3 (following nt serv behaviour),
5821 but I still don't know if it's _really_ necessary */
5824 * Store the password in secrets db
5827 if (!pdb_set_trusteddom_pw(domain_name
, c
->opt_password
, domain_sid
)) {
5828 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5830 talloc_destroy(mem_ctx
);
5835 * Close the pipes and clean up
5838 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
5839 if (NT_STATUS_IS_ERR(nt_status
)) {
5840 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5841 nt_errstr(nt_status
)));
5843 talloc_destroy(mem_ctx
);
5849 talloc_destroy(mem_ctx
);
5851 d_printf(_("Trust to domain %s established\n"), domain_name
);
5856 * Revoke trust relationship to the remote domain.
5858 * @param c A net_context structure.
5859 * @param argc Standard argc.
5860 * @param argv Standard argv without initial components.
5862 * @return Integer status (0 means success).
5865 static int rpc_trustdom_revoke(struct net_context
*c
, int argc
,
5871 if (argc
< 1 || c
->display_usage
) {
5874 _("net rpc trustdom revoke <domain_name>\n"
5875 " Revoke trust relationship\n"
5876 " domain_name\tName of domain to revoke trust\n"));
5880 /* generate upper cased domain name */
5881 domain_name
= smb_xstrdup(argv
[0]);
5882 strupper_m(domain_name
);
5884 /* delete password of the trust */
5885 if (!pdb_del_trusteddom_pw(domain_name
)) {
5886 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5893 SAFE_FREE(domain_name
);
5897 static NTSTATUS
rpc_query_domain_sid(struct net_context
*c
,
5898 const struct dom_sid
*domain_sid
,
5899 const char *domain_name
,
5900 struct cli_state
*cli
,
5901 struct rpc_pipe_client
*pipe_hnd
,
5902 TALLOC_CTX
*mem_ctx
,
5907 if (!sid_to_fstring(str_sid
, domain_sid
)) {
5908 return NT_STATUS_UNSUCCESSFUL
;
5910 d_printf("%s\n", str_sid
);
5911 return NT_STATUS_OK
;
5914 static void print_trusted_domain(struct dom_sid
*dom_sid
, const char *trusted_dom_name
)
5918 /* convert sid into ascii string */
5919 sid_to_fstring(ascii_sid
, dom_sid
);
5921 d_printf("%-20s%s\n", trusted_dom_name
, ascii_sid
);
5924 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
5925 TALLOC_CTX
*mem_ctx
,
5926 struct policy_handle
*pol
,
5927 struct dom_sid dom_sid
,
5928 const char *trusted_dom_name
)
5931 union lsa_TrustedDomainInfo
*info
= NULL
;
5932 char *cleartextpwd
= NULL
;
5933 uint8_t session_key
[16];
5934 DATA_BLOB session_key_blob
;
5935 DATA_BLOB data
= data_blob_null
;
5937 nt_status
= rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd
, mem_ctx
,
5940 LSA_TRUSTED_DOMAIN_INFO_PASSWORD
,
5942 if (NT_STATUS_IS_ERR(nt_status
)) {
5943 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5944 nt_errstr(nt_status
)));
5948 data
= data_blob(info
->password
.password
->data
,
5949 info
->password
.password
->length
);
5951 if (!rpccli_get_pwd_hash(pipe_hnd
, session_key
)) {
5952 DEBUG(0, ("Could not retrieve password hash\n"));
5956 session_key_blob
= data_blob_const(session_key
, sizeof(session_key
));
5957 cleartextpwd
= sess_decrypt_string(mem_ctx
, &data
, &session_key_blob
);
5959 if (cleartextpwd
== NULL
) {
5960 DEBUG(0,("retrieved NULL password\n"));
5961 nt_status
= NT_STATUS_UNSUCCESSFUL
;
5965 if (!pdb_set_trusteddom_pw(trusted_dom_name
, cleartextpwd
, &dom_sid
)) {
5966 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5967 nt_status
= NT_STATUS_UNSUCCESSFUL
;
5971 #ifdef DEBUG_PASSWORD
5972 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5973 "password: [%s]\n", trusted_dom_name
,
5974 sid_string_dbg(&dom_sid
), cleartextpwd
));
5978 SAFE_FREE(cleartextpwd
);
5979 data_blob_free(&data
);
5984 static int rpc_trustdom_vampire(struct net_context
*c
, int argc
,
5987 /* common variables */
5988 TALLOC_CTX
* mem_ctx
;
5989 struct cli_state
*cli
= NULL
;
5990 struct rpc_pipe_client
*pipe_hnd
= NULL
;
5992 const char *domain_name
= NULL
;
5993 struct dom_sid
*queried_dom_sid
;
5994 struct policy_handle connect_hnd
;
5995 union lsa_PolicyInformation
*info
= NULL
;
5997 /* trusted domains listing variables */
5998 unsigned int enum_ctx
= 0;
6000 struct lsa_DomainList dom_list
;
6003 if (c
->display_usage
) {
6005 "net rpc trustdom vampire\n"
6008 _("Vampire trust relationship from remote server"));
6013 * Listing trusted domains (stored in secrets.tdb, if local)
6016 mem_ctx
= talloc_init("trust relationships vampire");
6019 * set domain and pdc name to local samba server (default)
6020 * or to remote one given in command line
6023 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
6024 domain_name
= c
->opt_workgroup
;
6025 c
->opt_target_workgroup
= c
->opt_workgroup
;
6027 fstrcpy(pdc_name
, global_myname());
6028 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6029 c
->opt_target_workgroup
= domain_name
;
6032 /* open \PIPE\lsarpc and open policy handle */
6033 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6034 if (!NT_STATUS_IS_OK(nt_status
)) {
6035 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6036 nt_errstr(nt_status
)));
6037 talloc_destroy(mem_ctx
);
6041 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6043 if (!NT_STATUS_IS_OK(nt_status
)) {
6044 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6045 nt_errstr(nt_status
) ));
6047 talloc_destroy(mem_ctx
);
6051 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6053 if (NT_STATUS_IS_ERR(nt_status
)) {
6054 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6055 nt_errstr(nt_status
)));
6057 talloc_destroy(mem_ctx
);
6061 /* query info level 5 to obtain sid of a domain being queried */
6062 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
6064 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6067 if (NT_STATUS_IS_ERR(nt_status
)) {
6068 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6069 nt_errstr(nt_status
)));
6071 talloc_destroy(mem_ctx
);
6075 queried_dom_sid
= info
->account_domain
.sid
;
6078 * Keep calling LsaEnumTrustdom over opened pipe until
6079 * the end of enumeration is reached
6082 d_printf(_("Vampire trusted domains:\n\n"));
6085 nt_status
= rpccli_lsa_EnumTrustDom(pipe_hnd
, mem_ctx
,
6090 if (NT_STATUS_IS_ERR(nt_status
)) {
6091 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6092 nt_errstr(nt_status
)));
6094 talloc_destroy(mem_ctx
);
6098 for (i
= 0; i
< dom_list
.count
; i
++) {
6100 print_trusted_domain(dom_list
.domains
[i
].sid
,
6101 dom_list
.domains
[i
].name
.string
);
6103 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
6104 *dom_list
.domains
[i
].sid
,
6105 dom_list
.domains
[i
].name
.string
);
6106 if (!NT_STATUS_IS_OK(nt_status
)) {
6108 talloc_destroy(mem_ctx
);
6114 * in case of no trusted domains say something rather
6115 * than just display blank line
6117 if (!dom_list
.count
) d_printf(_("none\n"));
6119 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6121 /* close this connection before doing next one */
6122 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6123 if (NT_STATUS_IS_ERR(nt_status
)) {
6124 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6125 nt_errstr(nt_status
)));
6127 talloc_destroy(mem_ctx
);
6131 /* close lsarpc pipe and connection to IPC$ */
6134 talloc_destroy(mem_ctx
);
6138 static int rpc_trustdom_list(struct net_context
*c
, int argc
, const char **argv
)
6140 /* common variables */
6141 TALLOC_CTX
* mem_ctx
;
6142 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
6143 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6145 const char *domain_name
= NULL
;
6146 struct dom_sid
*queried_dom_sid
;
6147 int ascii_dom_name_len
;
6148 struct policy_handle connect_hnd
;
6149 union lsa_PolicyInformation
*info
= NULL
;
6151 /* trusted domains listing variables */
6152 unsigned int num_domains
, enum_ctx
= 0;
6154 struct lsa_DomainList dom_list
;
6158 /* trusting domains listing variables */
6159 struct policy_handle domain_hnd
;
6160 struct samr_SamArray
*trusts
= NULL
;
6162 if (c
->display_usage
) {
6164 "net rpc trustdom list\n"
6167 _("List incoming and outgoing trust relationships"));
6172 * Listing trusted domains (stored in secrets.tdb, if local)
6175 mem_ctx
= talloc_init("trust relationships listing");
6178 * set domain and pdc name to local samba server (default)
6179 * or to remote one given in command line
6182 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
6183 domain_name
= c
->opt_workgroup
;
6184 c
->opt_target_workgroup
= c
->opt_workgroup
;
6186 fstrcpy(pdc_name
, global_myname());
6187 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6188 c
->opt_target_workgroup
= domain_name
;
6191 /* open \PIPE\lsarpc and open policy handle */
6192 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6193 if (!NT_STATUS_IS_OK(nt_status
)) {
6194 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6195 nt_errstr(nt_status
)));
6196 talloc_destroy(mem_ctx
);
6200 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6202 if (!NT_STATUS_IS_OK(nt_status
)) {
6203 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6204 nt_errstr(nt_status
) ));
6206 talloc_destroy(mem_ctx
);
6210 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, KEY_QUERY_VALUE
,
6212 if (NT_STATUS_IS_ERR(nt_status
)) {
6213 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6214 nt_errstr(nt_status
)));
6216 talloc_destroy(mem_ctx
);
6220 /* query info level 5 to obtain sid of a domain being queried */
6221 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
6223 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6226 if (NT_STATUS_IS_ERR(nt_status
)) {
6227 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6228 nt_errstr(nt_status
)));
6230 talloc_destroy(mem_ctx
);
6234 queried_dom_sid
= info
->account_domain
.sid
;
6237 * Keep calling LsaEnumTrustdom over opened pipe until
6238 * the end of enumeration is reached
6241 d_printf(_("Trusted domains list:\n\n"));
6243 found_domain
= false;
6246 nt_status
= rpccli_lsa_EnumTrustDom(pipe_hnd
, mem_ctx
,
6251 if (NT_STATUS_IS_ERR(nt_status
)) {
6252 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6253 nt_errstr(nt_status
)));
6255 talloc_destroy(mem_ctx
);
6259 for (i
= 0; i
< dom_list
.count
; i
++) {
6260 print_trusted_domain(dom_list
.domains
[i
].sid
,
6261 dom_list
.domains
[i
].name
.string
);
6262 found_domain
= true;
6266 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6269 * in case of no trusted domains say something rather
6270 * than just display blank line
6272 if (!found_domain
) {
6273 d_printf(_("none\n"));
6276 /* close this connection before doing next one */
6277 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6278 if (NT_STATUS_IS_ERR(nt_status
)) {
6279 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6280 nt_errstr(nt_status
)));
6282 talloc_destroy(mem_ctx
);
6286 TALLOC_FREE(pipe_hnd
);
6289 * Listing trusting domains (stored in passdb backend, if local)
6292 d_printf(_("\nTrusting domains list:\n\n"));
6295 * Open \PIPE\samr and get needed policy handles
6297 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
6299 if (!NT_STATUS_IS_OK(nt_status
)) {
6300 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6302 talloc_destroy(mem_ctx
);
6307 nt_status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
6309 SAMR_ACCESS_LOOKUP_DOMAIN
,
6311 if (!NT_STATUS_IS_OK(nt_status
)) {
6312 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6313 nt_errstr(nt_status
)));
6315 talloc_destroy(mem_ctx
);
6319 /* SamrOpenDomain - we have to open domain policy handle in order to be
6320 able to enumerate accounts*/
6321 nt_status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
6323 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS
,
6326 if (!NT_STATUS_IS_OK(nt_status
)) {
6327 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6328 nt_errstr(nt_status
)));
6330 talloc_destroy(mem_ctx
);
6335 * perform actual enumeration
6338 found_domain
= false;
6340 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6343 nt_status
= rpccli_samr_EnumDomainUsers(pipe_hnd
, mem_ctx
,
6350 if (NT_STATUS_IS_ERR(nt_status
)) {
6351 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6352 nt_errstr(nt_status
)));
6354 talloc_destroy(mem_ctx
);
6358 for (i
= 0; i
< num_domains
; i
++) {
6360 char *str
= CONST_DISCARD(char *, trusts
->entries
[i
].name
.string
);
6362 found_domain
= true;
6365 * get each single domain's sid (do we _really_ need this ?):
6366 * 1) connect to domain's pdc
6367 * 2) query the pdc for domain's sid
6370 /* get rid of '$' tail */
6371 ascii_dom_name_len
= strlen(str
);
6372 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
6373 str
[ascii_dom_name_len
- 1] = '\0';
6375 /* set opt_* variables to remote domain */
6377 c
->opt_workgroup
= talloc_strdup(mem_ctx
, str
);
6378 c
->opt_target_workgroup
= c
->opt_workgroup
;
6380 d_printf("%-20s", str
);
6382 /* connect to remote domain controller */
6383 nt_status
= net_make_ipc_connection(c
,
6384 NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
,
6386 if (NT_STATUS_IS_OK(nt_status
)) {
6387 /* query for domain's sid */
6388 if (run_rpc_command(
6390 &ndr_table_lsarpc
.syntax_id
, 0,
6391 rpc_query_domain_sid
, argc
,
6393 d_printf(_("strange - couldn't get domain's sid\n"));
6395 cli_shutdown(remote_cli
);
6398 d_fprintf(stderr
, _("domain controller is not "
6399 "responding: %s\n"),
6400 nt_errstr(nt_status
));
6401 d_printf(_("couldn't get domain's sid\n"));
6405 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6407 if (!found_domain
) {
6411 /* close opened samr and domain policy handles */
6412 nt_status
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_hnd
);
6413 if (!NT_STATUS_IS_OK(nt_status
)) {
6414 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
6417 nt_status
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6418 if (!NT_STATUS_IS_OK(nt_status
)) {
6419 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
6422 /* close samr pipe and connection to IPC$ */
6425 talloc_destroy(mem_ctx
);
6430 * Entrypoint for 'net rpc trustdom' code.
6432 * @param argc Standard argc.
6433 * @param argv Standard argv without initial components.
6435 * @return Integer status (0 means success).
6438 static int rpc_trustdom(struct net_context
*c
, int argc
, const char **argv
)
6440 struct functable func
[] = {
6445 N_("Add trusting domain's account"),
6446 N_("net rpc trustdom add\n"
6447 " Add trusting domain's account")
6453 N_("Remove trusting domain's account"),
6454 N_("net rpc trustdom del\n"
6455 " Remove trusting domain's account")
6459 rpc_trustdom_establish
,
6461 N_("Establish outgoing trust relationship"),
6462 N_("net rpc trustdom establish\n"
6463 " Establish outgoing trust relationship")
6467 rpc_trustdom_revoke
,
6469 N_("Revoke outgoing trust relationship"),
6470 N_("net rpc trustdom revoke\n"
6471 " Revoke outgoing trust relationship")
6477 N_("List in- and outgoing domain trusts"),
6478 N_("net rpc trustdom list\n"
6479 " List in- and outgoing domain trusts")
6483 rpc_trustdom_vampire
,
6485 N_("Vampire trusts from remote server"),
6486 N_("net rpc trustdom vampire\n"
6487 " Vampire trusts from remote server")
6489 {NULL
, NULL
, 0, NULL
, NULL
}
6492 return net_run_function(c
, argc
, argv
, "net rpc trustdom", func
);
6496 * Check if a server will take rpc commands
6497 * @param flags Type of server to connect to (PDC, DMB, localhost)
6498 * if the host is not explicitly specified
6499 * @return bool (true means rpc supported)
6501 bool net_rpc_check(struct net_context
*c
, unsigned flags
)
6503 struct cli_state
*cli
;
6505 struct sockaddr_storage server_ss
;
6506 char *server_name
= NULL
;
6509 /* flags (i.e. server type) may depend on command */
6510 if (!net_find_server(c
, NULL
, flags
, &server_ss
, &server_name
))
6513 if ((cli
= cli_initialise()) == NULL
) {
6517 status
= cli_connect(cli
, server_name
, &server_ss
);
6518 if (!NT_STATUS_IS_OK(status
))
6520 if (!attempt_netbios_session_request(&cli
, global_myname(),
6521 server_name
, &server_ss
))
6523 status
= cli_negprot(cli
);
6524 if (!NT_STATUS_IS_OK(status
))
6526 if (cli
->protocol
< PROTOCOL_NT1
)
6535 /* dump sam database via samsync rpc calls */
6536 static int rpc_samdump(struct net_context
*c
, int argc
, const char **argv
) {
6537 if (c
->display_usage
) {
6542 _("Dump remote SAM database"));
6546 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
6547 NET_FLAGS_ANONYMOUS
,
6548 rpc_samdump_internals
, argc
, argv
);
6551 /* syncronise sam database via samsync rpc calls */
6552 static int rpc_vampire(struct net_context
*c
, int argc
, const char **argv
)
6554 struct functable func
[] = {
6559 N_("Dump remote SAM database to ldif"),
6560 N_("net rpc vampire ldif\n"
6561 " Dump remote SAM database to LDIF file or "
6568 N_("Dump remote SAM database to Kerberos Keytab"),
6569 N_("net rpc vampire keytab\n"
6570 " Dump remote SAM database to Kerberos keytab "
6577 N_("Dump remote SAM database to passdb"),
6578 N_("net rpc vampire passdb\n"
6579 " Dump remote SAM database to passdb")
6582 {NULL
, NULL
, 0, NULL
, NULL
}
6586 if (c
->display_usage
) {
6591 _("Vampire remote SAM database"));
6595 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
6596 NET_FLAGS_ANONYMOUS
,
6597 rpc_vampire_internals
,
6601 return net_run_function(c
, argc
, argv
, "net rpc vampire", func
);
6605 * Migrate everything from a print server.
6607 * @param c A net_context structure.
6608 * @param argc Standard main() style argc.
6609 * @param argv Standard main() style argv. Initial components are already
6612 * @return A shell status integer (0 for success).
6614 * The order is important !
6615 * To successfully add drivers the print queues have to exist !
6616 * Applying ACLs should be the last step, because you're easily locked out.
6619 static int rpc_printer_migrate_all(struct net_context
*c
, int argc
,
6624 if (c
->display_usage
) {
6626 "net rpc printer migrate all\n"
6629 _("Migrate everything from a print server"));
6634 d_printf(_("no server to migrate\n"));
6638 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6639 rpc_printer_migrate_printers_internals
, argc
,
6644 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6645 rpc_printer_migrate_drivers_internals
, argc
,
6650 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6651 rpc_printer_migrate_forms_internals
, argc
, argv
);
6655 ret
= run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6656 rpc_printer_migrate_settings_internals
, argc
,
6661 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6662 rpc_printer_migrate_security_internals
, argc
,
6668 * Migrate print drivers from a print server.
6670 * @param c A net_context structure.
6671 * @param argc Standard main() style argc.
6672 * @param argv Standard main() style argv. Initial components are already
6675 * @return A shell status integer (0 for success).
6677 static int rpc_printer_migrate_drivers(struct net_context
*c
, int argc
,
6680 if (c
->display_usage
) {
6682 "net rpc printer migrate drivers\n"
6685 _("Migrate print-drivers from a print-server"));
6690 d_printf(_("no server to migrate\n"));
6694 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6695 rpc_printer_migrate_drivers_internals
,
6700 * Migrate print-forms from a print-server.
6702 * @param c A net_context structure.
6703 * @param argc Standard main() style argc.
6704 * @param argv Standard main() style argv. Initial components are already
6707 * @return A shell status integer (0 for success).
6709 static int rpc_printer_migrate_forms(struct net_context
*c
, int argc
,
6712 if (c
->display_usage
) {
6714 "net rpc printer migrate forms\n"
6717 _("Migrate print-forms from a print-server"));
6722 d_printf(_("no server to migrate\n"));
6726 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6727 rpc_printer_migrate_forms_internals
,
6732 * Migrate printers from a print-server.
6734 * @param c A net_context structure.
6735 * @param argc Standard main() style argc.
6736 * @param argv Standard main() style argv. Initial components are already
6739 * @return A shell status integer (0 for success).
6741 static int rpc_printer_migrate_printers(struct net_context
*c
, int argc
,
6744 if (c
->display_usage
) {
6746 "net rpc printer migrate printers\n"
6749 _("Migrate printers from a print-server"));
6754 d_printf(_("no server to migrate\n"));
6758 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6759 rpc_printer_migrate_printers_internals
,
6764 * Migrate printer-ACLs from a print-server
6766 * @param c A net_context structure.
6767 * @param argc Standard main() style argc.
6768 * @param argv Standard main() style argv. Initial components are already
6771 * @return A shell status integer (0 for success).
6773 static int rpc_printer_migrate_security(struct net_context
*c
, int argc
,
6776 if (c
->display_usage
) {
6778 "net rpc printer migrate security\n"
6781 _("Migrate printer-ACLs from a print-server"));
6786 d_printf(_("no server to migrate\n"));
6790 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6791 rpc_printer_migrate_security_internals
,
6796 * Migrate printer-settings from a print-server.
6798 * @param c A net_context structure.
6799 * @param argc Standard main() style argc.
6800 * @param argv Standard main() style argv. Initial components are already
6803 * @return A shell status integer (0 for success).
6805 static int rpc_printer_migrate_settings(struct net_context
*c
, int argc
,
6808 if (c
->display_usage
) {
6810 "net rpc printer migrate settings\n"
6813 _("Migrate printer-settings from a "
6819 d_printf(_("no server to migrate\n"));
6823 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6824 rpc_printer_migrate_settings_internals
,
6829 * 'net rpc printer' entrypoint.
6831 * @param c A net_context structure.
6832 * @param argc Standard main() style argc.
6833 * @param argv Standard main() style argv. Initial components are already
6837 int rpc_printer_migrate(struct net_context
*c
, int argc
, const char **argv
)
6840 /* ouch: when addriver and setdriver are called from within
6841 rpc_printer_migrate_drivers_internals, the printer-queue already
6844 struct functable func
[] = {
6847 rpc_printer_migrate_all
,
6849 N_("Migrate all from remote to local print server"),
6850 N_("net rpc printer migrate all\n"
6851 " Migrate all from remote to local print server")
6855 rpc_printer_migrate_drivers
,
6857 N_("Migrate drivers to local server"),
6858 N_("net rpc printer migrate drivers\n"
6859 " Migrate drivers to local server")
6863 rpc_printer_migrate_forms
,
6865 N_("Migrate froms to local server"),
6866 N_("net rpc printer migrate forms\n"
6867 " Migrate froms to local server")
6871 rpc_printer_migrate_printers
,
6873 N_("Migrate printers to local server"),
6874 N_("net rpc printer migrate printers\n"
6875 " Migrate printers to local server")
6879 rpc_printer_migrate_security
,
6881 N_("Mirgate printer ACLs to local server"),
6882 N_("net rpc printer migrate security\n"
6883 " Mirgate printer ACLs to local server")
6887 rpc_printer_migrate_settings
,
6889 N_("Migrate printer settings to local server"),
6890 N_("net rpc printer migrate settings\n"
6891 " Migrate printer settings to local server")
6893 {NULL
, NULL
, 0, NULL
, NULL
}
6896 return net_run_function(c
, argc
, argv
, "net rpc printer migrate",func
);
6901 * List printers on a remote RPC server.
6903 * @param c A net_context structure.
6904 * @param argc Standard main() style argc.
6905 * @param argv Standard main() style argv. Initial components are already
6908 * @return A shell status integer (0 for success).
6910 static int rpc_printer_list(struct net_context
*c
, int argc
, const char **argv
)
6912 if (c
->display_usage
) {
6914 "net rpc printer list\n"
6917 _("List printers on a remote RPC server"));
6921 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6922 rpc_printer_list_internals
,
6927 * List printer-drivers on a remote RPC server.
6929 * @param c A net_context structure.
6930 * @param argc Standard main() style argc.
6931 * @param argv Standard main() style argv. Initial components are already
6934 * @return A shell status integer (0 for success).
6936 static int rpc_printer_driver_list(struct net_context
*c
, int argc
,
6939 if (c
->display_usage
) {
6941 "net rpc printer driver\n"
6944 _("List printer-drivers on a remote RPC server"));
6948 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6949 rpc_printer_driver_list_internals
,
6954 * Publish printer in ADS via MSRPC.
6956 * @param c A net_context structure.
6957 * @param argc Standard main() style argc.
6958 * @param argv Standard main() style argv. Initial components are already
6961 * @return A shell status integer (0 for success).
6963 static int rpc_printer_publish_publish(struct net_context
*c
, int argc
,
6966 if (c
->display_usage
) {
6968 "net rpc printer publish publish\n"
6971 _("Publish printer in ADS via MSRPC"));
6975 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
6976 rpc_printer_publish_publish_internals
,
6981 * Update printer in ADS via MSRPC.
6983 * @param c A net_context structure.
6984 * @param argc Standard main() style argc.
6985 * @param argv Standard main() style argv. Initial components are already
6988 * @return A shell status integer (0 for success).
6990 static int rpc_printer_publish_update(struct net_context
*c
, int argc
, const char **argv
)
6992 if (c
->display_usage
) {
6994 "net rpc printer publish update\n"
6997 _("Update printer in ADS via MSRPC"));
7001 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7002 rpc_printer_publish_update_internals
,
7007 * UnPublish printer in ADS via MSRPC.
7009 * @param c A net_context structure.
7010 * @param argc Standard main() style argc.
7011 * @param argv Standard main() style argv. Initial components are already
7014 * @return A shell status integer (0 for success).
7016 static int rpc_printer_publish_unpublish(struct net_context
*c
, int argc
,
7019 if (c
->display_usage
) {
7021 "net rpc printer publish unpublish\n"
7024 _("UnPublish printer in ADS via MSRPC"));
7028 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7029 rpc_printer_publish_unpublish_internals
,
7034 * List published printers via MSRPC.
7036 * @param c A net_context structure.
7037 * @param argc Standard main() style argc.
7038 * @param argv Standard main() style argv. Initial components are already
7041 * @return A shell status integer (0 for success).
7043 static int rpc_printer_publish_list(struct net_context
*c
, int argc
,
7046 if (c
->display_usage
) {
7048 "net rpc printer publish list\n"
7051 _("List published printers via MSRPC"));
7055 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7056 rpc_printer_publish_list_internals
,
7062 * Publish printer in ADS.
7064 * @param c A net_context structure.
7065 * @param argc Standard main() style argc.
7066 * @param argv Standard main() style argv. Initial components are already
7069 * @return A shell status integer (0 for success).
7071 static int rpc_printer_publish(struct net_context
*c
, int argc
,
7075 struct functable func
[] = {
7078 rpc_printer_publish_publish
,
7080 N_("Publish printer in AD"),
7081 N_("net rpc printer publish publish\n"
7082 " Publish printer in AD")
7086 rpc_printer_publish_update
,
7088 N_("Update printer in AD"),
7089 N_("net rpc printer publish update\n"
7090 " Update printer in AD")
7094 rpc_printer_publish_unpublish
,
7096 N_("Unpublish printer"),
7097 N_("net rpc printer publish unpublish\n"
7098 " Unpublish printer")
7102 rpc_printer_publish_list
,
7104 N_("List published printers"),
7105 N_("net rpc printer publish list\n"
7106 " List published printers")
7108 {NULL
, NULL
, 0, NULL
, NULL
}
7112 if (c
->display_usage
) {
7113 d_printf(_("Usage:\n"));
7114 d_printf(_("net rpc printer publish\n"
7115 " List published printers\n"
7116 " Alias of net rpc printer publish "
7118 net_display_usage_from_functable(func
);
7121 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7122 rpc_printer_publish_list_internals
,
7126 return net_run_function(c
, argc
, argv
, "net rpc printer publish",func
);
7132 * Display rpc printer help page.
7134 * @param c A net_context structure.
7135 * @param argc Standard main() style argc.
7136 * @param argv Standard main() style argv. Initial components are already
7139 int rpc_printer_usage(struct net_context
*c
, int argc
, const char **argv
)
7141 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7142 "\tlists all printers on print-server\n\n"));
7143 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7144 "\tlists all printer-drivers on print-server\n\n"));
7145 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7146 "\tpublishes printer settings in Active Directory\n"
7147 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7148 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7149 "\n\tmigrates printers from remote to local server\n\n"));
7150 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7151 "\n\tmigrates printer-settings from remote to local server\n\n"));
7152 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7153 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7154 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7155 "\n\tmigrates printer-forms from remote to local server\n\n"));
7156 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7157 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7158 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7159 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7160 "\tremote to local print-server\n\n"));
7161 net_common_methods_usage(c
, argc
, argv
);
7162 net_common_flags_usage(c
, argc
, argv
);
7164 "\t-v or --verbose\t\t\tgive verbose output\n"
7165 "\t --destination\t\tmigration target server (default: localhost)\n"));
7171 * 'net rpc printer' entrypoint.
7173 * @param c A net_context structure.
7174 * @param argc Standard main() style argc.
7175 * @param argv Standard main() style argv. Initial components are already
7178 int net_rpc_printer(struct net_context
*c
, int argc
, const char **argv
)
7180 struct functable func
[] = {
7185 N_("List all printers on print server"),
7186 N_("net rpc printer list\n"
7187 " List all printers on print server")
7191 rpc_printer_migrate
,
7193 N_("Migrate printer to local server"),
7194 N_("net rpc printer migrate\n"
7195 " Migrate printer to local server")
7199 rpc_printer_driver_list
,
7201 N_("List printer drivers"),
7202 N_("net rpc printer driver\n"
7203 " List printer drivers")
7207 rpc_printer_publish
,
7209 N_("Publish printer in AD"),
7210 N_("net rpc printer publish\n"
7211 " Publish printer in AD")
7213 {NULL
, NULL
, 0, NULL
, NULL
}
7217 if (c
->display_usage
) {
7218 d_printf(_("Usage:\n"));
7219 d_printf(_("net rpc printer\n"
7220 " List printers\n"));
7221 net_display_usage_from_functable(func
);
7224 return run_rpc_command(c
, NULL
, &ndr_table_spoolss
.syntax_id
, 0,
7225 rpc_printer_list_internals
,
7229 return net_run_function(c
, argc
, argv
, "net rpc printer", func
);
7233 * 'net rpc' entrypoint.
7235 * @param c A net_context structure.
7236 * @param argc Standard main() style argc.
7237 * @param argv Standard main() style argv. Initial components are already
7241 int net_rpc(struct net_context
*c
, int argc
, const char **argv
)
7243 NET_API_STATUS status
;
7245 struct functable func
[] = {
7250 N_("Modify global audit settings"),
7251 N_("net rpc audit\n"
7252 " Modify global audit settings")
7258 N_("Show basic info about a domain"),
7260 " Show basic info about a domain")
7266 N_("Join a domain"),
7274 N_("Join a domain created in server manager"),
7275 N_("net rpc oldjoin\n"
7276 " Join a domain created in server manager")
7282 N_("Test that a join is valid"),
7283 N_("net rpc testjoin\n"
7284 " Test that a join is valid")
7290 N_("List/modify users"),
7292 " List/modify users")
7298 N_("Change a user password"),
7299 N_("net rpc password\n"
7300 " Change a user password\n"
7301 " Alias for net rpc user password")
7307 N_("List/modify groups"),
7308 N_("net rpc group\n"
7309 " List/modify groups")
7315 N_("List/modify shares"),
7316 N_("net rpc share\n"
7317 " List/modify shares")
7323 N_("List open files"),
7331 N_("List/modify printers"),
7332 N_("net rpc printer\n"
7333 " List/modify printers")
7337 net_rpc_changetrustpw
,
7339 N_("Change trust account password"),
7340 N_("net rpc changetrustpw\n"
7341 " Change trust account password")
7347 N_("Modify domain trusts"),
7348 N_("net rpc trustdom\n"
7349 " Modify domain trusts")
7355 N_("Abort a remote shutdown"),
7356 N_("net rpc abortshutdown\n"
7357 " Abort a remote shutdown")
7363 N_("Shutdown a remote server"),
7364 N_("net rpc shutdown\n"
7365 " Shutdown a remote server")
7371 N_("Dump SAM data of remote NT PDC"),
7372 N_("net rpc samdump\n"
7373 " Dump SAM data of remote NT PDC")
7379 N_("Sync a remote NT PDC's data into local passdb"),
7380 N_("net rpc vampire\n"
7381 " Sync a remote NT PDC's data into local passdb")
7387 N_("Fetch the domain sid into local secrets.tdb"),
7388 N_("net rpc getsid\n"
7389 " Fetch the domain sid into local secrets.tdb")
7395 N_("Manage privileges assigned to SID"),
7396 N_("net rpc rights\n"
7397 " Manage privileges assigned to SID")
7403 N_("Start/stop/query remote services"),
7404 N_("net rpc service\n"
7405 " Start/stop/query remote services")
7411 N_("Manage registry hives"),
7412 N_("net rpc registry\n"
7413 " Manage registry hives")
7419 N_("Open interactive shell on remote server"),
7420 N_("net rpc shell\n"
7421 " Open interactive shell on remote server")
7423 {NULL
, NULL
, 0, NULL
, NULL
}
7426 status
= libnetapi_init(&c
->netapi_ctx
);
7430 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
7431 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
7432 if (c
->opt_kerberos
) {
7433 libnetapi_set_use_kerberos(c
->netapi_ctx
);
7435 if (c
->opt_ccache
) {
7436 libnetapi_set_use_ccache(c
->netapi_ctx
);
7439 return net_run_function(c
, argc
, argv
, "net rpc", func
);