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)
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "../utils/net.h"
27 * @brief RPC based subcommands for the 'net' utility.
29 * This file should contain much of the functionality that used to
30 * be found in rpcclient, execpt that the commands should change
31 * less often, and the fucntionality should be sane (the user is not
32 * expected to know a rid/sid before they conduct an operation etc.)
34 * @todo Perhaps eventually these should be split out into a number
35 * of files, as this could get quite big.
39 /* A function of this type is passed to the 'run_rpc_command' wrapper */
40 typedef NTSTATUS (*rpc_command_fn
)(const DOM_SID
*, const char *,
41 struct cli_state
*, TALLOC_CTX
*, int, const char **);
44 * Many of the RPC functions need the domain sid. This function gets
45 * it at the start of every run
47 * @param cli A cli_state already connected to the remote machine
49 * @return The Domain SID of the remote machine.
52 static DOM_SID
*net_get_remote_domain_sid(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
, char **domain_name
)
56 NTSTATUS result
= NT_STATUS_OK
;
57 uint32 info_class
= 5;
59 if (!cli_nt_session_open (cli
, PI_LSARPC
)) {
60 fprintf(stderr
, "could not initialise lsa pipe\n");
64 result
= cli_lsa_open_policy(cli
, mem_ctx
, False
,
65 SEC_RIGHTS_MAXIMUM_ALLOWED
,
67 if (!NT_STATUS_IS_OK(result
)) {
71 result
= cli_lsa_query_info_policy(cli
, mem_ctx
, &pol
, info_class
,
72 domain_name
, &domain_sid
);
73 if (!NT_STATUS_IS_OK(result
)) {
75 fprintf(stderr
, "could not obtain sid for domain %s\n", cli
->domain
);
77 if (!NT_STATUS_IS_OK(result
)) {
78 fprintf(stderr
, "error: %s\n", nt_errstr(result
));
84 cli_lsa_close(cli
, mem_ctx
, &pol
);
85 cli_nt_session_close(cli
);
91 * Run a single RPC command, from start to finish.
93 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
94 * @param conn_flag a NET_FLAG_ combination. Passed to
95 * net_make_ipc_connection.
96 * @param argc Standard main() style argc
97 * @param argc Standard main() style argv. Initial components are already
99 * @return A shell status integer (0 for success)
102 static int run_rpc_command(struct cli_state
*cli_arg
, const int pipe_idx
, int conn_flags
,
104 int argc
, const char **argv
)
106 struct cli_state
*cli
= NULL
;
112 /* make use of cli_state handed over as an argument, if possible */
114 cli
= net_make_ipc_connection(conn_flags
);
124 if (!(mem_ctx
= talloc_init("run_rpc_command"))) {
125 DEBUG(0, ("talloc_init() failed\n"));
130 domain_sid
= net_get_remote_domain_sid(cli
, mem_ctx
, &domain_name
);
132 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
133 if (!cli_nt_session_open(cli
, pipe_idx
)) {
134 DEBUG(0, ("Could not initialise pipe\n"));
138 nt_status
= fn(domain_sid
, domain_name
, cli
, mem_ctx
, argc
, argv
);
140 if (!NT_STATUS_IS_OK(nt_status
)) {
141 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status
)));
143 DEBUG(5, ("rpc command function succedded\n"));
146 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
147 if (cli
->nt_pipe_fnum
)
148 cli_nt_session_close(cli
);
151 /* close the connection only if it was opened here */
155 talloc_destroy(mem_ctx
);
157 return (!NT_STATUS_IS_OK(nt_status
));
161 /****************************************************************************/
165 * Force a change of the trust acccount password.
167 * All parameters are provided by the run_rpc_command function, except for
168 * argc, argv which are passes through.
170 * @param domain_sid The domain sid aquired from the remote server
171 * @param cli A cli_state connected to the server.
172 * @param mem_ctx Talloc context, destoyed on compleation of the function.
173 * @param argc Standard main() style argc
174 * @param argc Standard main() style argv. Initial components are already
177 * @return Normal NTSTATUS return.
180 static NTSTATUS
rpc_changetrustpw_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
181 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
182 int argc
, const char **argv
) {
184 return trust_pw_find_change_and_store_it(cli
, mem_ctx
, opt_target_workgroup
);
188 * Force a change of the trust acccount password.
190 * @param argc Standard main() style argc
191 * @param argc Standard main() style argv. Initial components are already
194 * @return A shell status integer (0 for success)
197 int net_rpc_changetrustpw(int argc
, const char **argv
)
199 return run_rpc_command(NULL
, PI_NETLOGON
, NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
200 rpc_changetrustpw_internals
,
205 /****************************************************************************/
209 * Join a domain, the old way.
211 * This uses 'machinename' as the inital password, and changes it.
213 * The password should be created with 'server manager' or equiv first.
215 * All parameters are provided by the run_rpc_command function, except for
216 * argc, argv which are passes through.
218 * @param domain_sid The domain sid aquired from the remote server
219 * @param cli A cli_state connected to the server.
220 * @param mem_ctx Talloc context, destoyed on compleation of the function.
221 * @param argc Standard main() style argc
222 * @param argc Standard main() style argv. Initial components are already
225 * @return Normal NTSTATUS return.
228 static NTSTATUS
rpc_oldjoin_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
229 struct cli_state
*cli
,
231 int argc
, const char **argv
) {
233 fstring trust_passwd
;
234 unsigned char orig_trust_passwd_hash
[16];
236 uint32 sec_channel_type
;
239 check what type of join - if the user want's to join as
240 a BDC, the server must agree that we are a BDC.
243 sec_channel_type
= get_sec_channel_type(argv
[0]);
245 sec_channel_type
= get_sec_channel_type(NULL
);
248 fstrcpy(trust_passwd
, global_myname());
249 strlower_m(trust_passwd
);
252 * Machine names can be 15 characters, but the max length on
253 * a password is 14. --jerry
256 trust_passwd
[14] = '\0';
258 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
260 result
= trust_pw_change_and_store_it(cli
, mem_ctx
, opt_target_workgroup
,
261 orig_trust_passwd_hash
,
264 if (NT_STATUS_IS_OK(result
))
265 printf("Joined domain %s.\n",opt_target_workgroup
);
268 if (!secrets_store_domain_sid(opt_target_workgroup
, domain_sid
)) {
269 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup
));
270 result
= NT_STATUS_UNSUCCESSFUL
;
277 * Join a domain, the old way.
279 * @param argc Standard main() style argc
280 * @param argc Standard main() style argv. Initial components are already
283 * @return A shell status integer (0 for success)
286 static int net_rpc_perform_oldjoin(int argc
, const char **argv
)
288 return run_rpc_command(NULL
, PI_NETLOGON
,
289 NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
290 rpc_oldjoin_internals
,
295 * Join a domain, the old way. This function exists to allow
296 * the message to be displayed when oldjoin was explicitly
297 * requested, but not when it was implied by "net rpc join"
299 * @param argc Standard main() style argc
300 * @param argc Standard main() style argv. Initial components are already
303 * @return A shell status integer (0 for success)
306 static int net_rpc_oldjoin(int argc
, const char **argv
)
308 int rc
= net_rpc_perform_oldjoin(argc
, argv
);
311 d_printf("Failed to join domain\n");
318 * Basic usage function for 'net rpc join'
319 * @param argc Standard main() style argc
320 * @param argc Standard main() style argv. Initial components are already
324 static int rpc_join_usage(int argc
, const char **argv
)
326 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
327 "\t to join a domain with admin username & password\n"\
328 "\t\t password will be prompted if needed and none is specified\n"\
329 "\t <type> can be (default MEMBER)\n"\
330 "\t\t BDC - Join as a BDC\n"\
331 "\t\t PDC - Join as a PDC\n"\
332 "\t\t MEMBER - Join as a MEMBER server\n");
334 net_common_flags_usage(argc
, argv
);
339 * 'net rpc join' entrypoint.
340 * @param argc Standard main() style argc
341 * @param argc Standard main() style argv. Initial components are already
344 * Main 'net_rpc_join()' (where the admain username/password is used) is
346 * Try to just change the password, but if that doesn't work, use/prompt
347 * for a username/password.
350 int net_rpc_join(int argc
, const char **argv
)
352 if ((net_rpc_perform_oldjoin(argc
, argv
) == 0))
355 return net_rpc_join_newstyle(argc
, argv
);
361 * display info about a rpc domain
363 * All parameters are provided by the run_rpc_command function, except for
364 * argc, argv which are passed through.
366 * @param domain_sid The domain sid acquired from the remote server
367 * @param cli A cli_state connected to the server.
368 * @param mem_ctx Talloc context, destoyed on completion of the function.
369 * @param argc Standard main() style argc
370 * @param argv Standard main() style argv. Initial components are already
373 * @return Normal NTSTATUS return.
377 rpc_info_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
378 struct cli_state
*cli
,
379 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
381 POLICY_HND connect_pol
, domain_pol
;
382 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
386 sid_to_string(sid_str
, domain_sid
);
388 /* Get sam policy handle */
389 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
391 if (!NT_STATUS_IS_OK(result
)) {
395 /* Get domain policy handle */
396 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
397 MAXIMUM_ALLOWED_ACCESS
,
398 domain_sid
, &domain_pol
);
399 if (!NT_STATUS_IS_OK(result
)) {
404 result
= cli_samr_query_dom_info(cli
, mem_ctx
, &domain_pol
,
406 if (NT_STATUS_IS_OK(result
)) {
407 TALLOC_CTX
*ctx
= talloc_init("rpc_info_internals");
408 d_printf("Domain Name: %s\n", unistr2_tdup(ctx
, &ctr
.info
.inf2
.uni_domain
));
409 d_printf("Domain SID: %s\n", sid_str
);
410 d_printf("Sequence number: %u\n", ctr
.info
.inf2
.seq_num
);
411 d_printf("Num users: %u\n", ctr
.info
.inf2
.num_domain_usrs
);
412 d_printf("Num domain groups: %u\n", ctr
.info
.inf2
.num_domain_grps
);
413 d_printf("Num local groups: %u\n", ctr
.info
.inf2
.num_local_grps
);
423 * 'net rpc info' entrypoint.
424 * @param argc Standard main() style argc
425 * @param argc Standard main() style argv. Initial components are already
428 int net_rpc_info(int argc
, const char **argv
)
430 return run_rpc_command(NULL
, PI_SAMR
, NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
437 * Fetch domain SID into the local secrets.tdb
439 * All parameters are provided by the run_rpc_command function, except for
440 * argc, argv which are passes through.
442 * @param domain_sid The domain sid acquired from the remote server
443 * @param cli A cli_state connected to the server.
444 * @param mem_ctx Talloc context, destoyed on completion of the function.
445 * @param argc Standard main() style argc
446 * @param argv Standard main() style argv. Initial components are already
449 * @return Normal NTSTATUS return.
453 rpc_getsid_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
454 struct cli_state
*cli
,
455 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
459 sid_to_string(sid_str
, domain_sid
);
460 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
461 sid_str
, domain_name
);
463 if (!secrets_store_domain_sid(domain_name
, domain_sid
)) {
464 DEBUG(0,("Can't store domain SID\n"));
465 return NT_STATUS_UNSUCCESSFUL
;
473 * 'net rpc getsid' entrypoint.
474 * @param argc Standard main() style argc
475 * @param argc Standard main() style argv. Initial components are already
478 int net_rpc_getsid(int argc
, const char **argv
)
480 return run_rpc_command(NULL
, PI_SAMR
, NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
481 rpc_getsid_internals
,
486 /****************************************************************************/
489 * Basic usage function for 'net rpc user'
490 * @param argc Standard main() style argc.
491 * @param argv Standard main() style argv. Initial components are already
495 static int rpc_user_usage(int argc
, const char **argv
)
497 return net_help_user(argc
, argv
);
501 * Add a new user to a remote RPC server
503 * All parameters are provided by the run_rpc_command function, except for
504 * argc, argv which are passes through.
506 * @param domain_sid The domain sid acquired from the remote server
507 * @param cli A cli_state connected to the server.
508 * @param mem_ctx Talloc context, destoyed on completion of the function.
509 * @param argc Standard main() style argc
510 * @param argv Standard main() style argv. Initial components are already
513 * @return Normal NTSTATUS return.
516 static NTSTATUS
rpc_user_add_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
517 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
518 int argc
, const char **argv
) {
520 POLICY_HND connect_pol
, domain_pol
, user_pol
;
521 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
522 const char *acct_name
;
524 uint32 unknown
, user_rid
;
527 d_printf("User must be specified\n");
528 rpc_user_usage(argc
, argv
);
534 /* Get sam policy handle */
536 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
538 if (!NT_STATUS_IS_OK(result
)) {
542 /* Get domain policy handle */
544 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
545 MAXIMUM_ALLOWED_ACCESS
,
546 domain_sid
, &domain_pol
);
547 if (!NT_STATUS_IS_OK(result
)) {
551 /* Create domain user */
553 acb_info
= ACB_NORMAL
;
554 unknown
= 0xe005000b; /* No idea what this is - a permission mask? */
556 result
= cli_samr_create_dom_user(cli
, mem_ctx
, &domain_pol
,
557 acct_name
, acb_info
, unknown
,
558 &user_pol
, &user_rid
);
559 if (!NT_STATUS_IS_OK(result
)) {
564 if (!NT_STATUS_IS_OK(result
)) {
565 d_printf("Failed to add user %s - %s\n", acct_name
,
568 d_printf("Added user %s\n", acct_name
);
574 * Add a new user to a remote RPC server
576 * @param argc Standard main() style argc
577 * @param argv Standard main() style argv. Initial components are already
580 * @return A shell status integer (0 for success)
583 static int rpc_user_add(int argc
, const char **argv
)
585 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_add_internals
,
590 * Delete a user from a remote RPC server
592 * All parameters are provided by the run_rpc_command function, except for
593 * argc, argv which are passes through.
595 * @param domain_sid The domain sid acquired from the remote server
596 * @param cli A cli_state connected to the server.
597 * @param mem_ctx Talloc context, destoyed on completion of the function.
598 * @param argc Standard main() style argc
599 * @param argv Standard main() style argv. Initial components are already
602 * @return Normal NTSTATUS return.
605 static NTSTATUS
rpc_user_del_internals(const DOM_SID
*domain_sid
,
606 const char *domain_name
,
607 struct cli_state
*cli
,
609 int argc
, const char **argv
)
611 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
612 POLICY_HND connect_pol
, domain_pol
, user_pol
;
615 d_printf("User must be specified\n");
616 rpc_user_usage(argc
, argv
);
619 /* Get sam policy and domain handles */
621 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
624 if (!NT_STATUS_IS_OK(result
)) {
628 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
629 MAXIMUM_ALLOWED_ACCESS
,
630 domain_sid
, &domain_pol
);
632 if (!NT_STATUS_IS_OK(result
)) {
636 /* Get handle on user */
639 uint32
*user_rids
, num_rids
, *name_types
;
640 uint32 flags
= 0x000003e8; /* Unknown */
642 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
,
644 &num_rids
, &user_rids
,
647 if (!NT_STATUS_IS_OK(result
)) {
651 result
= cli_samr_open_user(cli
, mem_ctx
, &domain_pol
,
652 MAXIMUM_ALLOWED_ACCESS
,
653 user_rids
[0], &user_pol
);
655 if (!NT_STATUS_IS_OK(result
)) {
662 result
= cli_samr_delete_dom_user(cli
, mem_ctx
, &user_pol
);
664 if (!NT_STATUS_IS_OK(result
)) {
668 /* Display results */
676 * Delete a user from a remote RPC server
678 * @param argc Standard main() style argc
679 * @param argv Standard main() style argv. Initial components are already
682 * @return A shell status integer (0 for success)
685 static int rpc_user_delete(int argc
, const char **argv
)
687 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_del_internals
,
692 * Set a password for a user on a remote RPC server
694 * All parameters are provided by the run_rpc_command function, except for
695 * argc, argv which are passes through.
697 * @param domain_sid The domain sid acquired from the remote server
698 * @param cli A cli_state connected to the server.
699 * @param mem_ctx Talloc context, destoyed on completion of the function.
700 * @param argc Standard main() style argc
701 * @param argv Standard main() style argv. Initial components are already
704 * @return Normal NTSTATUS return.
707 static NTSTATUS
rpc_user_password_internals(const DOM_SID
*domain_sid
,
708 const char *domain_name
,
709 struct cli_state
*cli
,
711 int argc
, const char **argv
)
713 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
714 POLICY_HND connect_pol
, domain_pol
, user_pol
;
715 SAM_USERINFO_CTR ctr
;
716 SAM_USER_INFO_24 p24
;
719 const char *new_password
;
723 d_printf("User must be specified\n");
724 rpc_user_usage(argc
, argv
);
731 new_password
= argv
[1];
733 asprintf(&prompt
, "Enter new password for %s:", user
);
734 new_password
= getpass(prompt
);
738 /* Get sam policy and domain handles */
740 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
743 if (!NT_STATUS_IS_OK(result
)) {
747 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
748 MAXIMUM_ALLOWED_ACCESS
,
749 domain_sid
, &domain_pol
);
751 if (!NT_STATUS_IS_OK(result
)) {
755 /* Get handle on user */
758 uint32
*user_rids
, num_rids
, *name_types
;
759 uint32 flags
= 0x000003e8; /* Unknown */
761 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
,
763 &num_rids
, &user_rids
,
766 if (!NT_STATUS_IS_OK(result
)) {
770 result
= cli_samr_open_user(cli
, mem_ctx
, &domain_pol
,
771 MAXIMUM_ALLOWED_ACCESS
,
772 user_rids
[0], &user_pol
);
774 if (!NT_STATUS_IS_OK(result
)) {
779 /* Set password on account */
784 encode_pw_buffer(pwbuf
, new_password
, STR_UNICODE
);
786 init_sam_user_info24(&p24
, (char *)pwbuf
,24);
788 ctr
.switch_value
= 24;
789 ctr
.info
.id24
= &p24
;
791 result
= cli_samr_set_userinfo(cli
, mem_ctx
, &user_pol
, 24,
792 &cli
->user_session_key
, &ctr
);
794 if (!NT_STATUS_IS_OK(result
)) {
798 /* Display results */
806 * Set a user's password on a remote RPC server
808 * @param argc Standard main() style argc
809 * @param argv Standard main() style argv. Initial components are already
812 * @return A shell status integer (0 for success)
815 static int rpc_user_password(int argc
, const char **argv
)
817 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_password_internals
,
822 * List user's groups on a remote RPC server
824 * All parameters are provided by the run_rpc_command function, except for
825 * argc, argv which are passes through.
827 * @param domain_sid The domain sid acquired from the remote server
828 * @param cli A cli_state connected to the server.
829 * @param mem_ctx Talloc context, destoyed on completion of the function.
830 * @param argc Standard main() style argc
831 * @param argv Standard main() style argv. Initial components are already
834 * @return Normal NTSTATUS return.
838 rpc_user_info_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
839 struct cli_state
*cli
,
840 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
842 POLICY_HND connect_pol
, domain_pol
, user_pol
;
843 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
844 uint32
*rids
, num_rids
, *name_types
, num_names
;
845 uint32 flags
= 0x000003e8; /* Unknown */
851 d_printf("User must be specified\n");
852 rpc_user_usage(argc
, argv
);
855 /* Get sam policy handle */
857 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
859 if (!NT_STATUS_IS_OK(result
)) goto done
;
861 /* Get domain policy handle */
863 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
864 MAXIMUM_ALLOWED_ACCESS
,
865 domain_sid
, &domain_pol
);
866 if (!NT_STATUS_IS_OK(result
)) goto done
;
868 /* Get handle on user */
870 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
,
872 &num_rids
, &rids
, &name_types
);
874 if (!NT_STATUS_IS_OK(result
)) goto done
;
876 result
= cli_samr_open_user(cli
, mem_ctx
, &domain_pol
,
877 MAXIMUM_ALLOWED_ACCESS
,
879 if (!NT_STATUS_IS_OK(result
)) goto done
;
881 result
= cli_samr_query_usergroups(cli
, mem_ctx
, &user_pol
,
882 &num_rids
, &user_gids
);
886 rids
= (uint32
*)talloc(mem_ctx
, sizeof(uint32
) * num_rids
);
888 for (i
= 0; i
< num_rids
; i
++)
889 rids
[i
] = user_gids
[i
].g_rid
;
891 result
= cli_samr_lookup_rids(cli
, mem_ctx
, &domain_pol
,
892 flags
, num_rids
, rids
,
893 &num_names
, &names
, &name_types
);
895 if (!NT_STATUS_IS_OK(result
)) {
899 /* Display results */
901 for (i
= 0; i
< num_names
; i
++)
902 printf("%s\n", names
[i
]);
909 * List a user's groups from a remote RPC server
911 * @param argc Standard main() style argc
912 * @param argv Standard main() style argv. Initial components are already
915 * @return A shell status integer (0 for success)
918 static int rpc_user_info(int argc
, const char **argv
)
920 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_info_internals
,
925 * List users on a remote RPC server
927 * All parameters are provided by the run_rpc_command function, except for
928 * argc, argv which are passes through.
930 * @param domain_sid The domain sid acquired from the remote server
931 * @param cli A cli_state connected to the server.
932 * @param mem_ctx Talloc context, destoyed on completion of the function.
933 * @param argc Standard main() style argc
934 * @param argv Standard main() style argv. Initial components are already
937 * @return Normal NTSTATUS return.
941 rpc_user_list_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
942 struct cli_state
*cli
,
943 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
945 POLICY_HND connect_pol
, domain_pol
;
946 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
947 uint32 start_idx
=0, num_entries
, i
, loop_count
= 0;
948 SAM_DISPINFO_CTR ctr
;
949 SAM_DISPINFO_1 info1
;
951 /* Get sam policy handle */
953 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
955 if (!NT_STATUS_IS_OK(result
)) {
959 /* Get domain policy handle */
961 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
962 MAXIMUM_ALLOWED_ACCESS
,
963 domain_sid
, &domain_pol
);
964 if (!NT_STATUS_IS_OK(result
)) {
968 /* Query domain users */
971 ctr
.sam
.info1
= &info1
;
972 if (opt_long_list_entries
)
973 d_printf("\nUser name Comment"\
974 "\n-----------------------------\n");
977 uint32 max_entries
, max_size
;
979 get_query_dispinfo_params(
980 loop_count
, &max_entries
, &max_size
);
982 result
= cli_samr_query_dispinfo(cli
, mem_ctx
, &domain_pol
,
983 &start_idx
, 1, &num_entries
,
984 max_entries
, max_size
, &ctr
);
987 for (i
= 0; i
< num_entries
; i
++) {
988 unistr2_to_ascii(user
, &(&ctr
.sam
.info1
->str
[i
])->uni_acct_name
, sizeof(user
)-1);
989 if (opt_long_list_entries
)
990 unistr2_to_ascii(desc
, &(&ctr
.sam
.info1
->str
[i
])->uni_acct_desc
, sizeof(desc
)-1);
992 if (opt_long_list_entries
)
993 printf("%-21.21s %s\n", user
, desc
);
995 printf("%s\n", user
);
997 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
1004 * 'net rpc user' entrypoint.
1005 * @param argc Standard main() style argc
1006 * @param argc Standard main() style argv. Initial components are already
1010 int net_rpc_user(int argc
, const char **argv
)
1012 struct functable func
[] = {
1013 {"add", rpc_user_add
},
1014 {"info", rpc_user_info
},
1015 {"delete", rpc_user_delete
},
1016 {"password", rpc_user_password
},
1021 if (opt_long_list_entries
) {
1024 return run_rpc_command(NULL
,PI_SAMR
, 0,
1025 rpc_user_list_internals
,
1029 return net_run_function(argc
, argv
, func
, rpc_user_usage
);
1033 /****************************************************************************/
1036 * Basic usage function for 'net rpc group'
1037 * @param argc Standard main() style argc.
1038 * @param argv Standard main() style argv. Initial components are already
1042 static int rpc_group_usage(int argc
, const char **argv
)
1044 return net_help_group(argc
, argv
);
1048 rpc_group_add_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
1049 struct cli_state
*cli
,
1050 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
1052 POLICY_HND connect_pol
, domain_pol
, group_pol
;
1053 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1054 GROUP_INFO_CTR group_info
;
1057 d_printf("Group name must be specified\n");
1058 rpc_group_usage(argc
, argv
);
1059 return NT_STATUS_OK
;
1062 /* Get sam policy handle */
1064 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1066 if (!NT_STATUS_IS_OK(result
)) goto done
;
1068 /* Get domain policy handle */
1070 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
1071 MAXIMUM_ALLOWED_ACCESS
,
1072 domain_sid
, &domain_pol
);
1073 if (!NT_STATUS_IS_OK(result
)) goto done
;
1075 /* Create the group */
1077 result
= cli_samr_create_dom_group(cli
, mem_ctx
, &domain_pol
,
1078 argv
[0], MAXIMUM_ALLOWED_ACCESS
,
1080 if (!NT_STATUS_IS_OK(result
)) goto done
;
1082 if (strlen(opt_comment
) == 0) goto done
;
1084 /* We've got a comment to set */
1086 group_info
.switch_value1
= 4;
1087 init_samr_group_info4(&group_info
.group
.info4
, opt_comment
);
1089 result
= cli_samr_set_groupinfo(cli
, mem_ctx
, &group_pol
, &group_info
);
1090 if (!NT_STATUS_IS_OK(result
)) goto done
;
1093 if (NT_STATUS_IS_OK(result
))
1094 DEBUG(5, ("add group succeeded\n"));
1096 d_printf("add group failed: %s\n", nt_errstr(result
));
1101 static int rpc_group_add(int argc
, const char **argv
)
1103 return run_rpc_command(NULL
, PI_SAMR
, 0,
1104 rpc_group_add_internals
,
1109 * List groups on a remote RPC server
1111 * All parameters are provided by the run_rpc_command function, except for
1112 * argc, argv which are passes through.
1114 * @param domain_sid The domain sid acquired from the remote server
1115 * @param cli A cli_state connected to the server.
1116 * @param mem_ctx Talloc context, destoyed on completion of the function.
1117 * @param argc Standard main() style argc
1118 * @param argv Standard main() style argv. Initial components are already
1121 * @return Normal NTSTATUS return.
1125 rpc_group_list_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
1126 struct cli_state
*cli
,
1127 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
1129 POLICY_HND connect_pol
, domain_pol
;
1130 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1131 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
1132 struct acct_info
*groups
;
1133 DOM_SID global_sid_Builtin
;
1134 BOOL global
= False
;
1136 BOOL builtin
= False
;
1144 for (i
=0; i
<argc
; i
++) {
1145 if (strequal(argv
[i
], "global"))
1148 if (strequal(argv
[i
], "local"))
1151 if (strequal(argv
[i
], "builtin"))
1155 string_to_sid(&global_sid_Builtin
, "S-1-5-32");
1157 /* Get sam policy handle */
1159 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1161 if (!NT_STATUS_IS_OK(result
)) {
1165 /* Get domain policy handle */
1167 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
1168 MAXIMUM_ALLOWED_ACCESS
,
1169 domain_sid
, &domain_pol
);
1170 if (!NT_STATUS_IS_OK(result
)) {
1174 /* Query domain groups */
1175 if (opt_long_list_entries
)
1176 d_printf("\nGroup name Comment"\
1177 "\n-----------------------------\n");
1179 SAM_DISPINFO_CTR ctr
;
1180 SAM_DISPINFO_3 info3
;
1185 ctr
.sam
.info3
= &info3
;
1189 get_query_dispinfo_params(
1190 loop_count
, &max_entries
, &max_size
);
1192 result
= cli_samr_query_dispinfo(cli
, mem_ctx
, &domain_pol
,
1193 &start_idx
, 3, &num_entries
,
1194 max_entries
, max_size
, &ctr
);
1196 if (!NT_STATUS_IS_OK(result
))
1199 for (i
= 0; i
< num_entries
; i
++) {
1201 fstring group
, desc
;
1203 unistr2_to_ascii(group
, &(&ctr
.sam
.info3
->str
[i
])->uni_grp_name
, sizeof(group
)-1);
1204 unistr2_to_ascii(desc
, &(&ctr
.sam
.info3
->str
[i
])->uni_grp_desc
, sizeof(desc
)-1);
1206 if (opt_long_list_entries
)
1207 printf("%-21.21s %-50.50s\n",
1210 printf("%s\n", group
);
1212 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
1213 /* query domain aliases */
1218 result
= cli_samr_enum_als_groups(cli
, mem_ctx
, &domain_pol
,
1219 &start_idx
, max_entries
,
1220 &groups
, &num_entries
);
1222 if (!NT_STATUS_IS_OK(result
))
1225 for (i
= 0; i
< num_entries
; i
++) {
1227 char *description
= NULL
;
1229 if (opt_long_list_entries
) {
1231 POLICY_HND alias_pol
;
1234 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli
, mem_ctx
,
1239 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli
, mem_ctx
,
1242 (NT_STATUS_IS_OK(cli_samr_close(cli
, mem_ctx
,
1244 description
= unistr2_tdup(mem_ctx
,
1245 &ctr
.alias
.info3
.uni_acct_desc
);
1249 if (description
!= NULL
) {
1250 printf("%-21.21s %-50.50s\n",
1251 groups
[i
].acct_name
,
1254 printf("%s\n", groups
[i
].acct_name
);
1257 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
1258 cli_samr_close(cli
, mem_ctx
, &domain_pol
);
1259 /* Get builtin policy handle */
1261 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
1262 MAXIMUM_ALLOWED_ACCESS
,
1263 &global_sid_Builtin
, &domain_pol
);
1264 if (!NT_STATUS_IS_OK(result
)) {
1267 /* query builtin aliases */
1270 if (!builtin
) break;
1272 result
= cli_samr_enum_als_groups(cli
, mem_ctx
, &domain_pol
,
1273 &start_idx
, max_entries
,
1274 &groups
, &num_entries
);
1276 if (!NT_STATUS_IS_OK(result
))
1279 for (i
= 0; i
< num_entries
; i
++) {
1281 char *description
= NULL
;
1283 if (opt_long_list_entries
) {
1285 POLICY_HND alias_pol
;
1288 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli
, mem_ctx
,
1293 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli
, mem_ctx
,
1296 (NT_STATUS_IS_OK(cli_samr_close(cli
, mem_ctx
,
1298 description
= unistr2_tdup(mem_ctx
,
1299 &ctr
.alias
.info3
.uni_acct_desc
);
1303 if (description
!= NULL
) {
1304 printf("%-21.21s %-50.50s\n",
1305 groups
[i
].acct_name
,
1308 printf("%s\n", groups
[i
].acct_name
);
1311 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
1317 static int rpc_group_list(int argc
, const char **argv
)
1319 return run_rpc_command(NULL
, PI_SAMR
, 0,
1320 rpc_group_list_internals
,
1325 rpc_list_group_members(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1326 const char *domain_name
, const DOM_SID
*domain_sid
,
1327 POLICY_HND
*domain_pol
, uint32 rid
)
1330 POLICY_HND group_pol
;
1331 uint32 num_members
, *group_rids
, *group_attrs
;
1338 sid_to_string(sid_str
, domain_sid
);
1340 result
= cli_samr_open_group(cli
, mem_ctx
, domain_pol
,
1341 MAXIMUM_ALLOWED_ACCESS
,
1344 if (!NT_STATUS_IS_OK(result
))
1347 result
= cli_samr_query_groupmem(cli
, mem_ctx
, &group_pol
,
1348 &num_members
, &group_rids
,
1351 if (!NT_STATUS_IS_OK(result
))
1354 while (num_members
> 0) {
1355 int this_time
= 512;
1357 if (num_members
< this_time
)
1358 this_time
= num_members
;
1360 result
= cli_samr_lookup_rids(cli
, mem_ctx
, domain_pol
, 1000,
1361 this_time
, group_rids
,
1362 &num_names
, &names
, &name_types
);
1364 if (!NT_STATUS_IS_OK(result
))
1367 /* We only have users as members, but make the output
1368 the same as the output of alias members */
1370 for (i
= 0; i
< this_time
; i
++) {
1372 if (opt_long_list_entries
) {
1373 printf("%s-%d %s\\%s %d\n", sid_str
,
1374 group_rids
[i
], domain_name
, names
[i
],
1377 printf("%s\\%s\n", domain_name
, names
[i
]);
1381 num_members
-= this_time
;
1385 return NT_STATUS_OK
;
1389 rpc_list_alias_members(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1390 POLICY_HND
*domain_pol
, uint32 rid
)
1393 POLICY_HND alias_pol
, lsa_pol
;
1395 DOM_SID
*alias_sids
;
1401 result
= cli_samr_open_alias(cli
, mem_ctx
, domain_pol
,
1402 MAXIMUM_ALLOWED_ACCESS
, rid
, &alias_pol
);
1404 if (!NT_STATUS_IS_OK(result
))
1407 result
= cli_samr_query_aliasmem(cli
, mem_ctx
, &alias_pol
,
1408 &num_members
, &alias_sids
);
1410 if (!NT_STATUS_IS_OK(result
)) {
1411 d_printf("Couldn't list alias members\n");
1415 if (num_members
== 0) {
1416 return NT_STATUS_OK
;
1419 cli_nt_session_close(cli
);
1421 if (!cli_nt_session_open(cli
, PI_LSARPC
)) {
1422 d_printf("Couldn't open LSA pipe\n");
1426 result
= cli_lsa_open_policy(cli
, mem_ctx
, True
,
1427 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
1429 if (!NT_STATUS_IS_OK(result
)) {
1430 d_printf("Couldn't open LSA policy handle\n");
1434 result
= cli_lsa_lookup_sids(cli
, mem_ctx
, &lsa_pol
, num_members
,
1436 &domains
, &names
, &types
);
1438 if (!NT_STATUS_IS_OK(result
) &&
1439 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
1440 d_printf("Couldn't lookup SIDs\n");
1444 for (i
= 0; i
< num_members
; i
++) {
1446 sid_to_string(sid_str
, &alias_sids
[i
]);
1448 if (opt_long_list_entries
) {
1449 printf("%s %s\\%s %d\n", sid_str
,
1450 domains
[i
] ? domains
[i
] : "*unknown*",
1451 names
[i
] ? names
[i
] : "*unknown*", types
[i
]);
1454 printf("%s\\%s\n", domains
[i
], names
[i
]);
1456 printf("%s\n", sid_str
);
1460 return NT_STATUS_OK
;
1464 rpc_group_members_internals(const DOM_SID
*domain_sid
,
1465 const char *domain_name
,
1466 struct cli_state
*cli
,
1467 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
1470 POLICY_HND connect_pol
, domain_pol
;
1471 uint32 num_rids
, *rids
, *rid_types
;
1473 /* Get sam policy handle */
1475 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1478 if (!NT_STATUS_IS_OK(result
))
1481 /* Get domain policy handle */
1483 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
1484 MAXIMUM_ALLOWED_ACCESS
,
1485 domain_sid
, &domain_pol
);
1487 if (!NT_STATUS_IS_OK(result
))
1490 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
, 1000,
1491 1, argv
, &num_rids
, &rids
, &rid_types
);
1493 if (!NT_STATUS_IS_OK(result
)) {
1495 /* Ok, did not find it in the global sam, try with builtin */
1497 DOM_SID sid_Builtin
;
1499 cli_samr_close(cli
, mem_ctx
, &domain_pol
);
1501 string_to_sid(&sid_Builtin
, "S-1-5-32");
1503 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
1504 MAXIMUM_ALLOWED_ACCESS
,
1505 &sid_Builtin
, &domain_pol
);
1507 if (!NT_STATUS_IS_OK(result
)) {
1508 d_printf("Couldn't find group %s\n", argv
[0]);
1512 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
, 1000,
1516 if (!NT_STATUS_IS_OK(result
)) {
1517 d_printf("Couldn't find group %s\n", argv
[0]);
1522 if (num_rids
!= 1) {
1523 d_printf("Couldn't find group %s\n", argv
[0]);
1527 if (rid_types
[0] == SID_NAME_DOM_GRP
) {
1528 return rpc_list_group_members(cli
, mem_ctx
, domain_name
,
1529 domain_sid
, &domain_pol
,
1533 if (rid_types
[0] == SID_NAME_ALIAS
) {
1534 return rpc_list_alias_members(cli
, mem_ctx
, &domain_pol
,
1538 return NT_STATUS_NO_SUCH_GROUP
;
1541 static int rpc_group_members(int argc
, const char **argv
)
1544 return rpc_group_usage(argc
, argv
);
1547 return run_rpc_command(NULL
, PI_SAMR
, 0,
1548 rpc_group_members_internals
,
1553 * 'net rpc group' entrypoint.
1554 * @param argc Standard main() style argc
1555 * @param argc Standard main() style argv. Initial components are already
1559 int net_rpc_group(int argc
, const char **argv
)
1561 struct functable func
[] = {
1562 {"add", rpc_group_add
},
1564 {"delete", rpc_group_delete
},
1566 {"list", rpc_group_list
},
1567 {"members", rpc_group_members
},
1572 if (opt_long_list_entries
) {
1575 return run_rpc_command(NULL
, PI_SAMR
, 0,
1576 rpc_group_list_internals
,
1580 return net_run_function(argc
, argv
, func
, rpc_group_usage
);
1583 /****************************************************************************/
1585 static int rpc_share_usage(int argc
, const char **argv
)
1587 return net_help_share(argc
, argv
);
1591 * Add a share on a remote RPC server
1593 * All parameters are provided by the run_rpc_command function, except for
1594 * argc, argv which are passes through.
1596 * @param domain_sid The domain sid acquired from the remote server
1597 * @param cli A cli_state connected to the server.
1598 * @param mem_ctx Talloc context, destoyed on completion of the function.
1599 * @param argc Standard main() style argc
1600 * @param argv Standard main() style argv. Initial components are already
1603 * @return Normal NTSTATUS return.
1606 rpc_share_add_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
1607 struct cli_state
*cli
,
1608 TALLOC_CTX
*mem_ctx
,int argc
, const char **argv
)
1611 char *sharename
=talloc_strdup(mem_ctx
, argv
[0]);
1613 uint32 type
=0; /* only allow disk shares to be added */
1614 uint32 num_users
=0, perms
=0;
1615 char *password
=NULL
; /* don't allow a share password */
1617 path
= strchr(sharename
, '=');
1619 return NT_STATUS_UNSUCCESSFUL
;
1622 result
= cli_srvsvc_net_share_add(cli
, mem_ctx
, sharename
, type
,
1623 opt_comment
, perms
, opt_maxusers
,
1624 num_users
, path
, password
);
1625 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1628 static int rpc_share_add(int argc
, const char **argv
)
1630 if ((argc
< 1) || !strchr(argv
[0], '=')) {
1631 DEBUG(1,("Sharename or path not specified on add\n"));
1632 return rpc_share_usage(argc
, argv
);
1634 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
1635 rpc_share_add_internals
,
1640 * Delete a share on a remote RPC server
1642 * All parameters are provided by the run_rpc_command function, except for
1643 * argc, argv which are passes through.
1645 * @param domain_sid The domain sid acquired from the remote server
1646 * @param cli A cli_state connected to the server.
1647 * @param mem_ctx Talloc context, destoyed on completion of the function.
1648 * @param argc Standard main() style argc
1649 * @param argv Standard main() style argv. Initial components are already
1652 * @return Normal NTSTATUS return.
1655 rpc_share_del_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
1656 struct cli_state
*cli
,
1657 TALLOC_CTX
*mem_ctx
,int argc
, const char **argv
)
1661 result
= cli_srvsvc_net_share_del(cli
, mem_ctx
, argv
[0]);
1662 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1666 * Delete a share on a remote RPC server
1668 * @param domain_sid The domain sid acquired from the remote server
1669 * @param argc Standard main() style argc
1670 * @param argv Standard main() style argv. Initial components are already
1673 * @return A shell status integer (0 for success)
1675 static int rpc_share_delete(int argc
, const char **argv
)
1678 DEBUG(1,("Sharename not specified on delete\n"));
1679 return rpc_share_usage(argc
, argv
);
1681 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
1682 rpc_share_del_internals
,
1687 * Formatted print of share info
1689 * @param info1 pointer to SRV_SHARE_INFO_1 to format
1692 static void display_share_info_1(SRV_SHARE_INFO_1
*info1
)
1694 fstring netname
= "", remark
= "";
1696 rpcstr_pull_unistr2_fstring(netname
, &info1
->info_1_str
.uni_netname
);
1697 rpcstr_pull_unistr2_fstring(remark
, &info1
->info_1_str
.uni_remark
);
1699 if (opt_long_list_entries
) {
1700 d_printf("%-12s %-8.8s %-50s\n",
1701 netname
, share_type
[info1
->info_1
.type
], remark
);
1703 d_printf("%s\n", netname
);
1709 * List shares on a remote RPC server
1711 * All parameters are provided by the run_rpc_command function, except for
1712 * argc, argv which are passes through.
1714 * @param domain_sid The domain sid acquired from the remote server
1715 * @param cli A cli_state connected to the server.
1716 * @param mem_ctx Talloc context, destoyed on completion of the function.
1717 * @param argc Standard main() style argc
1718 * @param argv Standard main() style argv. Initial components are already
1721 * @return Normal NTSTATUS return.
1725 rpc_share_list_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
1726 struct cli_state
*cli
,
1727 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
1729 SRV_SHARE_INFO_CTR ctr
;
1732 uint32 preferred_len
= 0xffffffff, i
;
1734 init_enum_hnd(&hnd
, 0);
1736 result
= cli_srvsvc_net_share_enum(
1737 cli
, mem_ctx
, 1, &ctr
, preferred_len
, &hnd
);
1739 if (!W_ERROR_IS_OK(result
))
1742 /* Display results */
1744 if (opt_long_list_entries
) {
1746 "\nEnumerating shared resources (exports) on remote server:\n\n"\
1747 "\nShare name Type Description\n"\
1748 "---------- ---- -----------\n");
1750 for (i
= 0; i
< ctr
.num_entries
; i
++)
1751 display_share_info_1(&ctr
.share
.info1
[i
]);
1753 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1757 * 'net rpc share' entrypoint.
1758 * @param argc Standard main() style argc
1759 * @param argv Standard main() style argv. Initial components are already
1763 int net_rpc_share(int argc
, const char **argv
)
1765 struct functable func
[] = {
1766 {"add", rpc_share_add
},
1767 {"delete", rpc_share_delete
},
1772 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
1773 rpc_share_list_internals
,
1776 return net_run_function(argc
, argv
, func
, rpc_share_usage
);
1779 /****************************************************************************/
1781 static int rpc_file_usage(int argc
, const char **argv
)
1783 return net_help_file(argc
, argv
);
1787 * Close a file on a remote RPC server
1789 * All parameters are provided by the run_rpc_command function, except for
1790 * argc, argv which are passes through.
1792 * @param domain_sid The domain sid acquired from the remote server
1793 * @param cli A cli_state connected to the server.
1794 * @param mem_ctx Talloc context, destoyed on completion of the function.
1795 * @param argc Standard main() style argc
1796 * @param argv Standard main() style argv. Initial components are already
1799 * @return Normal NTSTATUS return.
1802 rpc_file_close_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
1803 struct cli_state
*cli
,
1804 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
1807 result
= cli_srvsvc_net_file_close(cli
, mem_ctx
, atoi(argv
[0]));
1808 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1812 * Close a file on a remote RPC server
1814 * @param argc Standard main() style argc
1815 * @param argv Standard main() style argv. Initial components are already
1818 * @return A shell status integer (0 for success)
1820 static int rpc_file_close(int argc
, const char **argv
)
1823 DEBUG(1, ("No fileid given on close\n"));
1824 return(rpc_file_usage(argc
, argv
));
1827 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
1828 rpc_file_close_internals
,
1833 * Formatted print of open file info
1835 * @param info3 FILE_INFO_3 contents
1836 * @param str3 strings for FILE_INFO_3
1839 static void display_file_info_3(FILE_INFO_3
*info3
, FILE_INFO_3_STR
*str3
)
1841 fstring user
= "", path
= "";
1843 rpcstr_pull_unistr2_fstring(user
, &str3
->uni_user_name
);
1844 rpcstr_pull_unistr2_fstring(path
, &str3
->uni_path_name
);
1846 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
1847 info3
->id
, user
, info3
->perms
, info3
->num_locks
, path
);
1851 * List open files on a remote RPC server
1853 * All parameters are provided by the run_rpc_command function, except for
1854 * argc, argv which are passes through.
1856 * @param domain_sid The domain sid acquired from the remote server
1857 * @param cli A cli_state connected to the server.
1858 * @param mem_ctx Talloc context, destoyed on completion of the function.
1859 * @param argc Standard main() style argc
1860 * @param argv Standard main() style argv. Initial components are already
1863 * @return Normal NTSTATUS return.
1867 rpc_file_list_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
1868 struct cli_state
*cli
,
1869 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
1871 SRV_FILE_INFO_CTR ctr
;
1874 uint32 preferred_len
= 0xffffffff, i
;
1875 const char *username
=NULL
;
1877 init_enum_hnd(&hnd
, 0);
1879 /* if argc > 0, must be user command */
1881 username
= smb_xstrdup(argv
[0]);
1883 result
= cli_srvsvc_net_file_enum(
1884 cli
, mem_ctx
, 3, username
, &ctr
, preferred_len
, &hnd
);
1886 if (!W_ERROR_IS_OK(result
))
1889 /* Display results */
1892 "\nEnumerating open files on remote server:\n\n"\
1893 "\nFileId Opened by Perms Locks Path"\
1894 "\n------ --------- ----- ----- ---- \n");
1895 for (i
= 0; i
< ctr
.num_entries
; i
++)
1896 display_file_info_3(&ctr
.file
.info3
[i
].info_3
,
1897 &ctr
.file
.info3
[i
].info_3_str
);
1899 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
1904 * List files for a user on a remote RPC server
1906 * @param argc Standard main() style argc
1907 * @param argv Standard main() style argv. Initial components are already
1910 * @return A shell status integer (0 for success)
1912 static int rpc_file_user(int argc
, const char **argv
)
1915 DEBUG(1, ("No username given\n"));
1916 return(rpc_file_usage(argc
, argv
));
1919 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
1920 rpc_file_list_internals
,
1926 * 'net rpc file' entrypoint.
1927 * @param argc Standard main() style argc
1928 * @param argv Standard main() style argv. Initial components are already
1932 int net_rpc_file(int argc
, const char **argv
)
1934 struct functable func
[] = {
1935 {"close", rpc_file_close
},
1936 {"user", rpc_file_user
},
1938 {"info", rpc_file_info
},
1944 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
1945 rpc_file_list_internals
,
1948 return net_run_function(argc
, argv
, func
, rpc_file_usage
);
1951 /****************************************************************************/
1956 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
1958 * All parameters are provided by the run_rpc_command function, except for
1959 * argc, argv which are passed through.
1961 * @param domain_sid The domain sid aquired from the remote server
1962 * @param cli A cli_state connected to the server.
1963 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1964 * @param argc Standard main() style argc
1965 * @param argv Standard main() style argv. Initial components are already
1968 * @return Normal NTSTATUS return.
1971 static NTSTATUS
rpc_shutdown_abort_internals(const DOM_SID
*domain_sid
,
1972 const char *domain_name
,
1973 struct cli_state
*cli
,
1974 TALLOC_CTX
*mem_ctx
,
1975 int argc
, const char **argv
)
1977 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1979 result
= cli_shutdown_abort(cli
, mem_ctx
);
1981 if (NT_STATUS_IS_OK(result
))
1982 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
1984 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
1991 * ABORT the shutdown of a remote RPC Server, over winreg pipe
1993 * All parameters are provided by the run_rpc_command function, except for
1994 * argc, argv which are passed through.
1996 * @param domain_sid The domain sid aquired from the remote server
1997 * @param cli A cli_state connected to the server.
1998 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1999 * @param argc Standard main() style argc
2000 * @param argv Standard main() style argv. Initial components are already
2003 * @return Normal NTSTATUS return.
2006 static NTSTATUS
rpc_reg_shutdown_abort_internals(const DOM_SID
*domain_sid
,
2007 const char *domain_name
,
2008 struct cli_state
*cli
,
2009 TALLOC_CTX
*mem_ctx
,
2010 int argc
, const char **argv
)
2012 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2014 result
= cli_reg_abort_shutdown(cli
, mem_ctx
);
2016 if (NT_STATUS_IS_OK(result
))
2017 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
2019 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
2025 * ABORT the Shut down of a remote RPC server
2027 * @param argc Standard main() style argc
2028 * @param argv Standard main() style argv. Initial components are already
2031 * @return A shell status integer (0 for success)
2034 static int rpc_shutdown_abort(int argc
, const char **argv
)
2036 int rc
= run_rpc_command(NULL
, PI_SHUTDOWN
, 0,
2037 rpc_shutdown_abort_internals
,
2043 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
2045 return run_rpc_command(NULL
, PI_WINREG
, 0,
2046 rpc_reg_shutdown_abort_internals
,
2051 * Shut down a remote RPC Server
2053 * All parameters are provided by the run_rpc_command function, except for
2054 * argc, argv which are passes through.
2056 * @param domain_sid The domain sid aquired from the remote server
2057 * @param cli A cli_state connected to the server.
2058 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2059 * @param argc Standard main() style argc
2060 * @param argc Standard main() style argv. Initial components are already
2063 * @return Normal NTSTATUS return.
2066 static NTSTATUS
rpc_shutdown_internals(const DOM_SID
*domain_sid
,
2067 const char *domain_name
,
2068 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
2069 int argc
, const char **argv
)
2071 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2072 const char *msg
= "This machine will be shutdown shortly";
2073 uint32 timeout
= 20;
2078 struct poptOption long_options
[] = {
2079 {"message", 'm', POPT_ARG_STRING
, &msg
},
2080 {"timeout", 't', POPT_ARG_INT
, &timeout
},
2081 {"reboot", 'r', POPT_ARG_NONE
, &reboot
},
2082 {"force", 'f', POPT_ARG_NONE
, &force
},
2086 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
,
2087 POPT_CONTEXT_KEEP_FIRST
);
2089 rc
= poptGetNextOpt(pc
);
2092 /* an error occurred during option processing */
2093 DEBUG(0, ("%s: %s\n",
2094 poptBadOption(pc
, POPT_BADOPTION_NOALIAS
),
2096 return NT_STATUS_INVALID_PARAMETER
;
2103 timeout
= opt_timeout
;
2106 /* create an entry */
2107 result
= cli_reg_shutdown(cli
, mem_ctx
, msg
, timeout
, opt_reboot
, opt_force
);
2109 if (NT_STATUS_IS_OK(result
))
2110 DEBUG(5,("Shutdown of remote machine succeeded\n"));
2112 DEBUG(0,("Shutdown of remote machine failed!\n"));
2118 * Shut down a remote RPC server
2120 * @param argc Standard main() style argc
2121 * @param argc Standard main() style argv. Initial components are already
2124 * @return A shell status integer (0 for success)
2127 static int rpc_shutdown(int argc
, const char **argv
)
2129 return run_rpc_command(NULL
, PI_WINREG
, 0, rpc_shutdown_internals
,
2133 /***************************************************************************
2134 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
2136 ***************************************************************************/
2139 * Add interdomain trust account to the RPC server.
2140 * All parameters (except for argc and argv) are passed by run_rpc_command
2143 * @param domain_sid The domain sid acquired from the server
2144 * @param cli A cli_state connected to the server.
2145 * @param mem_ctx Talloc context, destoyed on completion of the function.
2146 * @param argc Standard main() style argc
2147 * @param argc Standard main() style argv. Initial components are already
2150 * @return normal NTSTATUS return code
2153 static NTSTATUS
rpc_trustdom_add_internals(const DOM_SID
*domain_sid
,
2154 const char *domain_name
,
2155 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
2156 int argc
, const char **argv
) {
2158 POLICY_HND connect_pol
, domain_pol
, user_pol
;
2159 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2162 uint32 unknown
, user_rid
;
2165 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
2166 return NT_STATUS_INVALID_PARAMETER
;
2170 * Make valid trusting domain account (ie. uppercased and with '$' appended)
2173 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
2174 return NT_STATUS_NO_MEMORY
;
2177 strupper_m(acct_name
);
2179 /* Get samr policy handle */
2180 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
2182 if (!NT_STATUS_IS_OK(result
)) {
2186 /* Get domain policy handle */
2187 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
2188 MAXIMUM_ALLOWED_ACCESS
,
2189 domain_sid
, &domain_pol
);
2190 if (!NT_STATUS_IS_OK(result
)) {
2194 /* Create trusting domain's account */
2195 acb_info
= ACB_DOMTRUST
;
2196 unknown
= 0xe00500b0; /* No idea what this is - a permission mask?
2197 mimir: yes, most probably it is */
2199 result
= cli_samr_create_dom_user(cli
, mem_ctx
, &domain_pol
,
2200 acct_name
, acb_info
, unknown
,
2201 &user_pol
, &user_rid
);
2202 if (!NT_STATUS_IS_OK(result
)) {
2207 SAM_USERINFO_CTR ctr
;
2208 SAM_USER_INFO_24 p24
;
2211 encode_pw_buffer((char *)pwbuf
, argv
[1], STR_UNICODE
);
2216 init_sam_user_info24(&p24
, (char *)pwbuf
, 24);
2218 ctr
.switch_value
= 24;
2219 ctr
.info
.id24
= &p24
;
2221 result
= cli_samr_set_userinfo(cli
, mem_ctx
, &user_pol
, 24,
2222 &cli
->user_session_key
, &ctr
);
2224 if (!NT_STATUS_IS_OK(result
)) {
2225 DEBUG(0,("Could not set trust account password: %s\n",
2226 nt_errstr(result
)));
2232 SAFE_FREE(acct_name
);
2237 * Create interdomain trust account for a remote domain.
2239 * @param argc standard argc
2240 * @param argv standard argv without initial components
2242 * @return Integer status (0 means success)
2245 static int rpc_trustdom_add(int argc
, const char **argv
)
2248 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_trustdom_add_internals
,
2251 d_printf("Usage: net rpc trustdom add <domain>\n");
2258 * Delete interdomain trust account for a remote domain.
2260 * @param argc standard argc
2261 * @param argv standard argv without initial components
2263 * @return Integer status (0 means success)
2266 static int rpc_trustdom_del(int argc
, const char **argv
)
2268 d_printf("Sorry, not yet implemented.\n");
2269 d_printf("Use 'smbpasswd -x -i' instead.\n");
2275 * Establish trust relationship to a trusting domain.
2276 * Interdomain account must already be created on remote PDC.
2278 * @param argc standard argc
2279 * @param argv standard argv without initial components
2281 * @return Integer status (0 means success)
2284 static int rpc_trustdom_establish(int argc
, const char **argv
)
2286 struct cli_state
*cli
;
2287 struct in_addr server_ip
;
2288 POLICY_HND connect_hnd
;
2289 TALLOC_CTX
*mem_ctx
;
2291 DOM_SID
*domain_sid
;
2292 WKS_INFO_100 wks_info
;
2295 char* domain_name_pol
;
2300 * Connect to \\server\ipc$ as 'our domain' account with password
2304 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
2308 domain_name
= smb_xstrdup(argv
[0]);
2309 strupper_m(domain_name
);
2311 /* account name used at first is our domain's name with '$' */
2312 asprintf(&acct_name
, "%s$", lp_workgroup());
2313 strupper_m(acct_name
);
2316 * opt_workgroup will be used by connection functions further,
2317 * hence it should be set to remote domain name instead of ours
2319 if (opt_workgroup
) {
2320 opt_workgroup
= smb_xstrdup(domain_name
);
2323 opt_user_name
= acct_name
;
2325 /* find the domain controller */
2326 if (!net_find_pdc(&server_ip
, pdc_name
, domain_name
)) {
2327 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
2331 /* connect to ipc$ as username/password */
2332 nt_status
= connect_to_ipc(&cli
, &server_ip
, pdc_name
);
2333 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
2335 /* Is it trusting domain account for sure ? */
2336 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
2337 nt_errstr(nt_status
)));
2342 * Connect to \\server\ipc$ again (this time anonymously)
2345 nt_status
= connect_to_ipc_anonymous(&cli
, &server_ip
, (char*)pdc_name
);
2347 if (NT_STATUS_IS_ERR(nt_status
)) {
2348 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
2349 domain_name
, nt_errstr(nt_status
)));
2353 * Use NetServerEnum2 to make sure we're talking to a proper server
2356 if (!cli_get_pdc_name(cli
, domain_name
, (char*)pdc_name
)) {
2357 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
2358 for domain %s\n", domain_name
));
2362 * Call WksQueryInfo to check remote server's capabilities
2363 * note: It is now used only to get unicode domain name
2366 if (!cli_nt_session_open(cli
, PI_WKSSVC
)) {
2367 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
2371 if (!(mem_ctx
= talloc_init("establishing trust relationship to domain %s",
2373 DEBUG(0, ("talloc_init() failed\n"));
2378 nt_status
= cli_wks_query_info(cli
, mem_ctx
, &wks_info
);
2380 if (NT_STATUS_IS_ERR(nt_status
)) {
2381 DEBUG(0, ("WksQueryInfo call failed.\n"));
2385 if (cli
->nt_pipe_fnum
)
2386 cli_nt_session_close(cli
);
2390 * Call LsaOpenPolicy and LsaQueryInfo
2393 if (!(mem_ctx
= talloc_init("rpc_trustdom_establish"))) {
2394 DEBUG(0, ("talloc_init() failed\n"));
2399 if (!cli_nt_session_open(cli
, PI_LSARPC
)) {
2400 DEBUG(0, ("Could not initialise lsa pipe\n"));
2405 nt_status
= cli_lsa_open_policy2(cli
, mem_ctx
, True
, SEC_RIGHTS_QUERY_VALUE
,
2407 if (NT_STATUS_IS_ERR(nt_status
)) {
2408 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2409 nt_errstr(nt_status
)));
2413 /* Querying info level 5 */
2415 nt_status
= cli_lsa_query_info_policy(cli
, mem_ctx
, &connect_hnd
,
2416 5 /* info level */, &domain_name_pol
,
2418 if (NT_STATUS_IS_ERR(nt_status
)) {
2419 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2420 nt_errstr(nt_status
)));
2427 /* There should be actually query info level 3 (following nt serv behaviour),
2428 but I still don't know if it's _really_ necessary */
2431 * Store the password in secrets db
2434 if (!secrets_store_trusted_domain_password(domain_name
, wks_info
.uni_lan_grp
.buffer
,
2435 wks_info
.uni_lan_grp
.uni_str_len
, opt_password
,
2437 DEBUG(0, ("Storing password for trusted domain failed.\n"));
2442 * Close the pipes and clean up
2445 nt_status
= cli_lsa_close(cli
, mem_ctx
, &connect_hnd
);
2446 if (NT_STATUS_IS_ERR(nt_status
)) {
2447 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
2448 nt_errstr(nt_status
)));
2452 if (cli
->nt_pipe_fnum
)
2453 cli_nt_session_close(cli
);
2455 talloc_destroy(mem_ctx
);
2457 DEBUG(0, ("Success!\n"));
2462 * Revoke trust relationship to the remote domain
2464 * @param argc standard argc
2465 * @param argv standard argv without initial components
2467 * @return Integer status (0 means success)
2470 static int rpc_trustdom_revoke(int argc
, const char **argv
)
2474 if (argc
< 1) return -1;
2476 /* generate upper cased domain name */
2477 domain_name
= smb_xstrdup(argv
[0]);
2478 strupper_m(domain_name
);
2480 /* delete password of the trust */
2481 if (!trusted_domain_password_delete(domain_name
)) {
2482 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
2491 * Usage for 'net rpc trustdom' command
2493 * @param argc standard argc
2494 * @param argv standard argv without inital components
2496 * @return Integer status returned to shell
2499 static int rpc_trustdom_usage(int argc
, const char **argv
)
2501 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
2502 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
2503 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
2504 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
2505 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
2510 static NTSTATUS
rpc_query_domain_sid(const DOM_SID
*domain_sid
,
2511 const char *domain_name
,
2512 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
2513 int argc
, const char **argv
)
2516 sid_to_string(str_sid
, domain_sid
);
2517 d_printf("%s\n", str_sid
);
2518 return NT_STATUS_OK
;
2522 static int rpc_trustdom_list(int argc
, const char **argv
)
2524 /* common variables */
2525 TALLOC_CTX
* mem_ctx
;
2526 struct cli_state
*cli
, *remote_cli
;
2528 const char *domain_name
= NULL
;
2529 DOM_SID
*queried_dom_sid
;
2530 fstring ascii_sid
, padding
;
2531 int ascii_dom_name_len
;
2532 POLICY_HND connect_hnd
;
2534 /* trusted domains listing variables */
2535 unsigned int num_domains
, enum_ctx
= 0;
2536 int i
, pad_len
, col_len
= 20;
2537 DOM_SID
*domain_sids
;
2538 char **trusted_dom_names
;
2542 /* trusting domains listing variables */
2543 POLICY_HND domain_hnd
;
2544 char **trusting_dom_names
;
2545 uint32
*trusting_dom_rids
;
2548 * Listing trusted domains (stored in secrets.tdb, if local)
2551 mem_ctx
= talloc_init("trust relationships listing");
2554 * set domain and pdc name to local samba server (default)
2555 * or to remote one given in command line
2558 if (StrCaseCmp(opt_workgroup
, lp_workgroup())) {
2559 domain_name
= opt_workgroup
;
2560 opt_target_workgroup
= opt_workgroup
;
2562 fstrcpy(pdc_name
, global_myname());
2563 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
2564 opt_target_workgroup
= domain_name
;
2567 /* open \PIPE\lsarpc and open policy handle */
2568 if (!(cli
= net_make_ipc_connection(NET_FLAGS_PDC
))) {
2569 DEBUG(0, ("Couldn't connect to domain controller\n"));
2573 if (!cli_nt_session_open(cli
, PI_LSARPC
)) {
2574 DEBUG(0, ("Could not initialise lsa pipe\n"));
2578 nt_status
= cli_lsa_open_policy2(cli
, mem_ctx
, False
, SEC_RIGHTS_QUERY_VALUE
,
2580 if (NT_STATUS_IS_ERR(nt_status
)) {
2581 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2582 nt_errstr(nt_status
)));
2586 /* query info level 5 to obtain sid of a domain being queried */
2587 nt_status
= cli_lsa_query_info_policy(
2588 cli
, mem_ctx
, &connect_hnd
, 5 /* info level */,
2589 &dummy
, &queried_dom_sid
);
2591 if (NT_STATUS_IS_ERR(nt_status
)) {
2592 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2593 nt_errstr(nt_status
)));
2598 * Keep calling LsaEnumTrustdom over opened pipe until
2599 * the end of enumeration is reached
2602 d_printf("Trusted domains list:\n\n");
2605 nt_status
= cli_lsa_enum_trust_dom(cli
, mem_ctx
, &connect_hnd
, &enum_ctx
,
2607 &trusted_dom_names
, &domain_sids
);
2609 if (NT_STATUS_IS_ERR(nt_status
)) {
2610 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
2611 nt_errstr(nt_status
)));
2615 for (i
= 0; i
< num_domains
; i
++) {
2616 /* convert sid into ascii string */
2617 sid_to_string(ascii_sid
, &(domain_sids
[i
]));
2619 /* calculate padding space for d_printf to look nicer */
2620 pad_len
= col_len
- strlen(trusted_dom_names
[i
]);
2621 padding
[pad_len
] = 0;
2622 do padding
[--pad_len
] = ' '; while (pad_len
);
2624 d_printf("%s%s%s\n", trusted_dom_names
[i
], padding
, ascii_sid
);
2628 * in case of no trusted domains say something rather
2629 * than just display blank line
2631 if (!num_domains
) d_printf("none\n");
2633 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
2635 /* close this connection before doing next one */
2636 nt_status
= cli_lsa_close(cli
, mem_ctx
, &connect_hnd
);
2637 if (NT_STATUS_IS_ERR(nt_status
)) {
2638 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
2639 nt_errstr(nt_status
)));
2643 cli_nt_session_close(cli
);
2646 * Listing trusting domains (stored in passdb backend, if local)
2649 d_printf("\nTrusting domains list:\n\n");
2652 * Open \PIPE\samr and get needed policy handles
2654 if (!cli_nt_session_open(cli
, PI_SAMR
)) {
2655 DEBUG(0, ("Could not initialise samr pipe\n"));
2660 nt_status
= cli_samr_connect(cli
, mem_ctx
, SA_RIGHT_SAM_OPEN_DOMAIN
,
2662 if (!NT_STATUS_IS_OK(nt_status
)) {
2663 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
2664 nt_errstr(nt_status
)));
2668 /* SamrOpenDomain - we have to open domain policy handle in order to be
2669 able to enumerate accounts*/
2670 nt_status
= cli_samr_open_domain(cli
, mem_ctx
, &connect_hnd
,
2671 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS
,
2672 queried_dom_sid
, &domain_hnd
);
2673 if (!NT_STATUS_IS_OK(nt_status
)) {
2674 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
2675 nt_errstr(nt_status
)));
2680 * perform actual enumeration
2683 enum_ctx
= 0; /* reset enumeration context from last enumeration */
2686 nt_status
= cli_samr_enum_dom_users(cli
, mem_ctx
, &domain_hnd
,
2687 &enum_ctx
, ACB_DOMTRUST
, 0xffff,
2688 &trusting_dom_names
, &trusting_dom_rids
,
2690 if (NT_STATUS_IS_ERR(nt_status
)) {
2691 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
2692 nt_errstr(nt_status
)));
2696 for (i
= 0; i
< num_domains
; i
++) {
2699 * get each single domain's sid (do we _really_ need this ?):
2700 * 1) connect to domain's pdc
2701 * 2) query the pdc for domain's sid
2704 /* get rid of '$' tail */
2705 ascii_dom_name_len
= strlen(trusting_dom_names
[i
]);
2706 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
2707 trusting_dom_names
[i
][ascii_dom_name_len
- 1] = '\0';
2709 /* calculate padding space for d_printf to look nicer */
2710 pad_len
= col_len
- strlen(trusting_dom_names
[i
]);
2711 padding
[pad_len
] = 0;
2712 do padding
[--pad_len
] = ' '; while (pad_len
);
2714 /* set opt_* variables to remote domain */
2715 strupper_m(trusting_dom_names
[i
]);
2716 opt_workgroup
= talloc_strdup(mem_ctx
, trusting_dom_names
[i
]);
2717 opt_target_workgroup
= opt_workgroup
;
2719 d_printf("%s%s", trusting_dom_names
[i
], padding
);
2721 /* connect to remote domain controller */
2722 remote_cli
= net_make_ipc_connection(NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
);
2724 /* query for domain's sid */
2725 if (run_rpc_command(remote_cli
, PI_LSARPC
, 0, rpc_query_domain_sid
, argc
, argv
))
2726 d_printf("couldn't get domain's sid\n");
2728 cli_shutdown(remote_cli
);
2731 d_printf("domain controller is not responding\n");
2735 if (!num_domains
) d_printf("none\n");
2737 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
2739 /* close opened samr and domain policy handles */
2740 nt_status
= cli_samr_close(cli
, mem_ctx
, &domain_hnd
);
2741 if (!NT_STATUS_IS_OK(nt_status
)) {
2742 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
2745 nt_status
= cli_samr_close(cli
, mem_ctx
, &connect_hnd
);
2746 if (!NT_STATUS_IS_OK(nt_status
)) {
2747 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
2750 /* close samr pipe and connection to IPC$ */
2751 cli_nt_session_close(cli
);
2754 talloc_destroy(mem_ctx
);
2759 * Entrypoint for 'net rpc trustdom' code
2761 * @param argc standard argc
2762 * @param argv standard argv without initial components
2764 * @return Integer status (0 means success)
2767 static int rpc_trustdom(int argc
, const char **argv
)
2769 struct functable func
[] = {
2770 {"add", rpc_trustdom_add
},
2771 {"del", rpc_trustdom_del
},
2772 {"establish", rpc_trustdom_establish
},
2773 {"revoke", rpc_trustdom_revoke
},
2774 {"help", rpc_trustdom_usage
},
2775 {"list", rpc_trustdom_list
},
2780 rpc_trustdom_usage(argc
, argv
);
2784 return (net_run_function(argc
, argv
, func
, rpc_user_usage
));
2788 * Check if a server will take rpc commands
2789 * @param flags Type of server to connect to (PDC, DMB, localhost)
2790 * if the host is not explicitly specified
2791 * @return BOOL (true means rpc supported)
2793 BOOL
net_rpc_check(unsigned flags
)
2795 struct cli_state cli
;
2797 struct in_addr server_ip
;
2798 char *server_name
= NULL
;
2800 /* flags (i.e. server type) may depend on command */
2801 if (!net_find_server(flags
, &server_ip
, &server_name
))
2805 if (cli_initialise(&cli
) == False
)
2808 if (!cli_connect(&cli
, server_name
, &server_ip
))
2810 if (!attempt_netbios_session_request(&cli
, global_myname(),
2811 server_name
, &server_ip
))
2813 if (!cli_negprot(&cli
))
2815 if (cli
.protocol
< PROTOCOL_NT1
)
2824 /* dump sam database via samsync rpc calls */
2825 static int rpc_samdump(int argc
, const char **argv
) {
2826 return run_rpc_command(NULL
, PI_NETLOGON
, NET_FLAGS_ANONYMOUS
, rpc_samdump_internals
,
2830 /* syncronise sam database via samsync rpc calls */
2831 static int rpc_vampire(int argc
, const char **argv
) {
2832 return run_rpc_command(NULL
, PI_NETLOGON
, NET_FLAGS_ANONYMOUS
, rpc_vampire_internals
,
2835 /****************************************************************************/
2839 * Basic usage function for 'net rpc'
2840 * @param argc Standard main() style argc
2841 * @param argv Standard main() style argv. Initial components are already
2845 int net_rpc_usage(int argc
, const char **argv
)
2847 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
2848 d_printf(" net rpc join \t\t\tto join a domain \n");
2849 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n\n\n");
2850 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
2851 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
2852 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass");
2853 d_printf(" net rpc group \t\tto list groups\n");
2854 d_printf(" net rpc share \t\tto add, delete, and list shares\n");
2855 d_printf(" net rpc file \t\t\tto list open files\n");
2856 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
2857 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
2858 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
2859 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
2860 d_printf(" net rpc trustdom \t\tto create trusting domain's account\n"
2861 "\t\t\t\t\tor establish trust\n");
2862 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
2863 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
2865 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
2866 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
2867 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
2868 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
2869 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
2875 * Help function for 'net rpc'. Calls command specific help if requested
2876 * or displays usage of net rpc
2877 * @param argc Standard main() style argc
2878 * @param argv Standard main() style argv. Initial components are already
2882 int net_rpc_help(int argc
, const char **argv
)
2884 struct functable func
[] = {
2885 {"join", rpc_join_usage
},
2886 {"user", rpc_user_usage
},
2887 {"group", rpc_group_usage
},
2888 {"share", rpc_share_usage
},
2889 /*{"changetrustpw", rpc_changetrustpw_usage}, */
2890 {"trustdom", rpc_trustdom_usage
},
2891 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
2892 /*{"shutdown", rpc_shutdown_usage}, */
2897 net_rpc_usage(argc
, argv
);
2901 return (net_run_function(argc
, argv
, func
, rpc_user_usage
));
2906 * 'net rpc' entrypoint.
2907 * @param argc Standard main() style argc
2908 * @param argv Standard main() style argv. Initial components are already
2912 int net_rpc(int argc
, const char **argv
)
2914 struct functable func
[] = {
2915 {"info", net_rpc_info
},
2916 {"join", net_rpc_join
},
2917 {"oldjoin", net_rpc_oldjoin
},
2918 {"testjoin", net_rpc_testjoin
},
2919 {"user", net_rpc_user
},
2920 {"password", rpc_user_password
},
2921 {"group", net_rpc_group
},
2922 {"share", net_rpc_share
},
2923 {"file", net_rpc_file
},
2924 {"changetrustpw", net_rpc_changetrustpw
},
2925 {"trustdom", rpc_trustdom
},
2926 {"abortshutdown", rpc_shutdown_abort
},
2927 {"shutdown", rpc_shutdown
},
2928 {"samdump", rpc_samdump
},
2929 {"vampire", rpc_vampire
},
2930 {"getsid", net_rpc_getsid
},
2931 {"help", net_rpc_help
},
2934 return net_run_function(argc
, argv
, func
, net_rpc_usage
);