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 Guenther Deschner (gd@samba.org)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "utils/net.h"
28 * @brief RPC based subcommands for the 'net' utility.
30 * This file should contain much of the functionality that used to
31 * be found in rpcclient, execpt that the commands should change
32 * less often, and the fucntionality should be sane (the user is not
33 * expected to know a rid/sid before they conduct an operation etc.)
35 * @todo Perhaps eventually these should be split out into a number
36 * of files, as this could get quite big.
41 * Many of the RPC functions need the domain sid. This function gets
42 * it at the start of every run
44 * @param cli A cli_state already connected to the remote machine
46 * @return The Domain SID of the remote machine.
49 static DOM_SID
*net_get_remote_domain_sid(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
, char **domain_name
)
53 NTSTATUS result
= NT_STATUS_OK
;
54 uint32 info_class
= 5;
56 if (!cli_nt_session_open (cli
, PI_LSARPC
)) {
57 fprintf(stderr
, "could not initialise lsa pipe\n");
61 result
= cli_lsa_open_policy(cli
, mem_ctx
, False
,
62 SEC_RIGHTS_MAXIMUM_ALLOWED
,
64 if (!NT_STATUS_IS_OK(result
)) {
68 result
= cli_lsa_query_info_policy(cli
, mem_ctx
, &pol
, info_class
,
69 domain_name
, &domain_sid
);
70 if (!NT_STATUS_IS_OK(result
)) {
72 fprintf(stderr
, "could not obtain sid for domain %s\n", cli
->domain
);
74 if (!NT_STATUS_IS_OK(result
)) {
75 fprintf(stderr
, "error: %s\n", nt_errstr(result
));
81 cli_lsa_close(cli
, mem_ctx
, &pol
);
82 cli_nt_session_close(cli
);
88 * Run a single RPC command, from start to finish.
90 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
91 * @param conn_flag a NET_FLAG_ combination. Passed to
92 * net_make_ipc_connection.
93 * @param argc Standard main() style argc
94 * @param argc Standard main() style argv. Initial components are already
96 * @return A shell status integer (0 for success)
99 int run_rpc_command(struct cli_state
*cli_arg
, const int pipe_idx
, int conn_flags
,
101 int argc
, const char **argv
)
103 struct cli_state
*cli
= NULL
;
109 /* make use of cli_state handed over as an argument, if possible */
111 cli
= net_make_ipc_connection(conn_flags
);
121 if (!(mem_ctx
= talloc_init("run_rpc_command"))) {
122 DEBUG(0, ("talloc_init() failed\n"));
127 domain_sid
= net_get_remote_domain_sid(cli
, mem_ctx
, &domain_name
);
129 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
130 if (!cli_nt_session_open(cli
, pipe_idx
)) {
131 DEBUG(0, ("Could not initialise pipe\n"));
135 nt_status
= fn(domain_sid
, domain_name
, cli
, mem_ctx
, argc
, argv
);
137 if (!NT_STATUS_IS_OK(nt_status
)) {
138 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status
)));
140 DEBUG(5, ("rpc command function succedded\n"));
143 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
144 if (cli
->pipes
[cli
->pipe_idx
].fnum
)
145 cli_nt_session_close(cli
);
148 /* close the connection only if it was opened here */
152 talloc_destroy(mem_ctx
);
154 return (!NT_STATUS_IS_OK(nt_status
));
158 /****************************************************************************/
162 * Force a change of the trust acccount password.
164 * All parameters are provided by the run_rpc_command function, except for
165 * argc, argv which are passes through.
167 * @param domain_sid The domain sid aquired from the remote server
168 * @param cli A cli_state connected to the server.
169 * @param mem_ctx Talloc context, destoyed on compleation of the function.
170 * @param argc Standard main() style argc
171 * @param argc Standard main() style argv. Initial components are already
174 * @return Normal NTSTATUS return.
177 static NTSTATUS
rpc_changetrustpw_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
178 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
179 int argc
, const char **argv
) {
181 return trust_pw_find_change_and_store_it(cli
, mem_ctx
, opt_target_workgroup
);
185 * Force a change of the trust acccount password.
187 * @param argc Standard main() style argc
188 * @param argc Standard main() style argv. Initial components are already
191 * @return A shell status integer (0 for success)
194 int net_rpc_changetrustpw(int argc
, const char **argv
)
196 return run_rpc_command(NULL
, PI_NETLOGON
, NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
197 rpc_changetrustpw_internals
,
202 /****************************************************************************/
206 * Join a domain, the old way.
208 * This uses 'machinename' as the inital password, and changes it.
210 * The password should be created with 'server manager' or equiv first.
212 * All parameters are provided by the run_rpc_command function, except for
213 * argc, argv which are passes through.
215 * @param domain_sid The domain sid aquired from the remote server
216 * @param cli A cli_state connected to the server.
217 * @param mem_ctx Talloc context, destoyed on compleation of the function.
218 * @param argc Standard main() style argc
219 * @param argc Standard main() style argv. Initial components are already
222 * @return Normal NTSTATUS return.
225 static NTSTATUS
rpc_oldjoin_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
226 struct cli_state
*cli
,
228 int argc
, const char **argv
) {
230 fstring trust_passwd
;
231 unsigned char orig_trust_passwd_hash
[16];
233 uint32 sec_channel_type
;
236 check what type of join - if the user want's to join as
237 a BDC, the server must agree that we are a BDC.
240 sec_channel_type
= get_sec_channel_type(argv
[0]);
242 sec_channel_type
= get_sec_channel_type(NULL
);
245 fstrcpy(trust_passwd
, global_myname());
246 strlower_m(trust_passwd
);
249 * Machine names can be 15 characters, but the max length on
250 * a password is 14. --jerry
253 trust_passwd
[14] = '\0';
255 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
257 result
= trust_pw_change_and_store_it(cli
, mem_ctx
, opt_target_workgroup
,
258 orig_trust_passwd_hash
,
261 if (NT_STATUS_IS_OK(result
))
262 printf("Joined domain %s.\n",opt_target_workgroup
);
265 if (!secrets_store_domain_sid(opt_target_workgroup
, domain_sid
)) {
266 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup
));
267 result
= NT_STATUS_UNSUCCESSFUL
;
274 * Join a domain, the old way.
276 * @param argc Standard main() style argc
277 * @param argc Standard main() style argv. Initial components are already
280 * @return A shell status integer (0 for success)
283 static int net_rpc_perform_oldjoin(int argc
, const char **argv
)
285 return run_rpc_command(NULL
, PI_NETLOGON
,
286 NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
287 rpc_oldjoin_internals
,
292 * Join a domain, the old way. This function exists to allow
293 * the message to be displayed when oldjoin was explicitly
294 * requested, but not when it was implied by "net rpc join"
296 * @param argc Standard main() style argc
297 * @param argc Standard main() style argv. Initial components are already
300 * @return A shell status integer (0 for success)
303 static int net_rpc_oldjoin(int argc
, const char **argv
)
305 int rc
= net_rpc_perform_oldjoin(argc
, argv
);
308 d_printf("Failed to join domain\n");
315 * Basic usage function for 'net rpc join'
316 * @param argc Standard main() style argc
317 * @param argc Standard main() style argv. Initial components are already
321 static int rpc_join_usage(int argc
, const char **argv
)
323 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
324 "\t to join a domain with admin username & password\n"\
325 "\t\t password will be prompted if needed and none is specified\n"\
326 "\t <type> can be (default MEMBER)\n"\
327 "\t\t BDC - Join as a BDC\n"\
328 "\t\t PDC - Join as a PDC\n"\
329 "\t\t MEMBER - Join as a MEMBER server\n");
331 net_common_flags_usage(argc
, argv
);
336 * 'net rpc join' entrypoint.
337 * @param argc Standard main() style argc
338 * @param argc Standard main() style argv. Initial components are already
341 * Main 'net_rpc_join()' (where the admain username/password is used) is
343 * Try to just change the password, but if that doesn't work, use/prompt
344 * for a username/password.
347 int net_rpc_join(int argc
, const char **argv
)
349 if ((net_rpc_perform_oldjoin(argc
, argv
) == 0))
352 return net_rpc_join_newstyle(argc
, argv
);
358 * display info about a rpc domain
360 * All parameters are provided by the run_rpc_command function, except for
361 * argc, argv which are passed through.
363 * @param domain_sid The domain sid acquired from the remote server
364 * @param cli A cli_state connected to the server.
365 * @param mem_ctx Talloc context, destoyed on completion of the function.
366 * @param argc Standard main() style argc
367 * @param argv Standard main() style argv. Initial components are already
370 * @return Normal NTSTATUS return.
374 rpc_info_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
375 struct cli_state
*cli
,
376 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
378 POLICY_HND connect_pol
, domain_pol
;
379 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
383 sid_to_string(sid_str
, domain_sid
);
385 /* Get sam policy handle */
386 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
388 if (!NT_STATUS_IS_OK(result
)) {
392 /* Get domain policy handle */
393 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
394 MAXIMUM_ALLOWED_ACCESS
,
395 domain_sid
, &domain_pol
);
396 if (!NT_STATUS_IS_OK(result
)) {
401 result
= cli_samr_query_dom_info(cli
, mem_ctx
, &domain_pol
,
403 if (NT_STATUS_IS_OK(result
)) {
404 TALLOC_CTX
*ctx
= talloc_init("rpc_info_internals");
405 d_printf("Domain Name: %s\n", unistr2_tdup(ctx
, &ctr
.info
.inf2
.uni_domain
));
406 d_printf("Domain SID: %s\n", sid_str
);
407 d_printf("Sequence number: %u\n", ctr
.info
.inf2
.seq_num
.low
);
408 d_printf("Num users: %u\n", ctr
.info
.inf2
.num_domain_usrs
);
409 d_printf("Num domain groups: %u\n", ctr
.info
.inf2
.num_domain_grps
);
410 d_printf("Num local groups: %u\n", ctr
.info
.inf2
.num_local_grps
);
420 * 'net rpc info' entrypoint.
421 * @param argc Standard main() style argc
422 * @param argc Standard main() style argv. Initial components are already
425 int net_rpc_info(int argc
, const char **argv
)
427 return run_rpc_command(NULL
, PI_SAMR
, NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
434 * Fetch domain SID into the local secrets.tdb
436 * All parameters are provided by the run_rpc_command function, except for
437 * argc, argv which are passes through.
439 * @param domain_sid The domain sid acquired from the remote server
440 * @param cli A cli_state connected to the server.
441 * @param mem_ctx Talloc context, destoyed on completion of the function.
442 * @param argc Standard main() style argc
443 * @param argv Standard main() style argv. Initial components are already
446 * @return Normal NTSTATUS return.
450 rpc_getsid_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
451 struct cli_state
*cli
,
452 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
456 sid_to_string(sid_str
, domain_sid
);
457 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
458 sid_str
, domain_name
);
460 if (!secrets_store_domain_sid(domain_name
, domain_sid
)) {
461 DEBUG(0,("Can't store domain SID\n"));
462 return NT_STATUS_UNSUCCESSFUL
;
470 * 'net rpc getsid' entrypoint.
471 * @param argc Standard main() style argc
472 * @param argc Standard main() style argv. Initial components are already
475 int net_rpc_getsid(int argc
, const char **argv
)
477 return run_rpc_command(NULL
, PI_SAMR
, NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
478 rpc_getsid_internals
,
483 /****************************************************************************/
486 * Basic usage function for 'net rpc user'
487 * @param argc Standard main() style argc.
488 * @param argv Standard main() style argv. Initial components are already
492 static int rpc_user_usage(int argc
, const char **argv
)
494 return net_help_user(argc
, argv
);
498 * Add a new user to a remote RPC server
500 * All parameters are provided by the run_rpc_command function, except for
501 * argc, argv which are passes through.
503 * @param domain_sid The domain sid acquired from the remote server
504 * @param cli A cli_state connected to the server.
505 * @param mem_ctx Talloc context, destoyed on completion of the function.
506 * @param argc Standard main() style argc
507 * @param argv Standard main() style argv. Initial components are already
510 * @return Normal NTSTATUS return.
513 static NTSTATUS
rpc_user_add_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
514 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
515 int argc
, const char **argv
) {
517 POLICY_HND connect_pol
, domain_pol
, user_pol
;
518 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
519 const char *acct_name
;
521 uint32 unknown
, user_rid
;
524 d_printf("User must be specified\n");
525 rpc_user_usage(argc
, argv
);
531 /* Get sam policy handle */
533 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
535 if (!NT_STATUS_IS_OK(result
)) {
539 /* Get domain policy handle */
541 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
542 MAXIMUM_ALLOWED_ACCESS
,
543 domain_sid
, &domain_pol
);
544 if (!NT_STATUS_IS_OK(result
)) {
548 /* Create domain user */
550 acb_info
= ACB_NORMAL
;
551 unknown
= 0xe005000b; /* No idea what this is - a permission mask? */
553 result
= cli_samr_create_dom_user(cli
, mem_ctx
, &domain_pol
,
554 acct_name
, acb_info
, unknown
,
555 &user_pol
, &user_rid
);
556 if (!NT_STATUS_IS_OK(result
)) {
561 if (!NT_STATUS_IS_OK(result
)) {
562 d_printf("Failed to add user %s - %s\n", acct_name
,
565 d_printf("Added user %s\n", acct_name
);
571 * Add a new user to a remote RPC server
573 * @param argc Standard main() style argc
574 * @param argv Standard main() style argv. Initial components are already
577 * @return A shell status integer (0 for success)
580 static int rpc_user_add(int argc
, const char **argv
)
582 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_add_internals
,
587 * Delete a user from a remote RPC server
589 * All parameters are provided by the run_rpc_command function, except for
590 * argc, argv which are passes through.
592 * @param domain_sid The domain sid acquired from the remote server
593 * @param cli A cli_state connected to the server.
594 * @param mem_ctx Talloc context, destoyed on completion of the function.
595 * @param argc Standard main() style argc
596 * @param argv Standard main() style argv. Initial components are already
599 * @return Normal NTSTATUS return.
602 static NTSTATUS
rpc_user_del_internals(const DOM_SID
*domain_sid
,
603 const char *domain_name
,
604 struct cli_state
*cli
,
606 int argc
, const char **argv
)
608 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
609 POLICY_HND connect_pol
, domain_pol
, user_pol
;
612 d_printf("User must be specified\n");
613 rpc_user_usage(argc
, argv
);
616 /* Get sam policy and domain handles */
618 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
621 if (!NT_STATUS_IS_OK(result
)) {
625 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
626 MAXIMUM_ALLOWED_ACCESS
,
627 domain_sid
, &domain_pol
);
629 if (!NT_STATUS_IS_OK(result
)) {
633 /* Get handle on user */
636 uint32
*user_rids
, num_rids
, *name_types
;
637 uint32 flags
= 0x000003e8; /* Unknown */
639 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
,
641 &num_rids
, &user_rids
,
644 if (!NT_STATUS_IS_OK(result
)) {
648 result
= cli_samr_open_user(cli
, mem_ctx
, &domain_pol
,
649 MAXIMUM_ALLOWED_ACCESS
,
650 user_rids
[0], &user_pol
);
652 if (!NT_STATUS_IS_OK(result
)) {
659 result
= cli_samr_delete_dom_user(cli
, mem_ctx
, &user_pol
);
661 if (!NT_STATUS_IS_OK(result
)) {
665 /* Display results */
666 if (!NT_STATUS_IS_OK(result
)) {
667 d_printf("Failed to delete user account - %s\n", nt_errstr(result
));
669 d_printf("Deleted user account\n");
678 * Rename a user on a remote RPC server
680 * All parameters are provided by the run_rpc_command function, except for
681 * argc, argv which are passes through.
683 * @param domain_sid The domain sid acquired from the remote server
684 * @param cli A cli_state connected to the server.
685 * @param mem_ctx Talloc context, destoyed on completion of the function.
686 * @param argc Standard main() style argc
687 * @param argv Standard main() style argv. Initial components are already
690 * @return Normal NTSTATUS return.
693 static NTSTATUS
rpc_user_rename_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
694 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
695 int argc
, const char **argv
) {
697 POLICY_HND connect_pol
, domain_pol
, user_pol
;
698 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
699 uint32 info_level
= 7;
700 const char *old_name
, *new_name
;
702 uint32 flags
= 0x000003e8; /* Unknown */
703 uint32 num_rids
, *name_types
;
704 uint32 num_names
= 1;
706 SAM_USERINFO_CTR
*user_ctr
;
707 SAM_USERINFO_CTR ctr
;
708 SAM_USER_INFO_7 info7
;
711 d_printf("New and old username must be specified\n");
712 rpc_user_usage(argc
, argv
);
720 ZERO_STRUCT(user_ctr
);
722 /* Get sam policy handle */
724 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
726 if (!NT_STATUS_IS_OK(result
)) {
730 /* Get domain policy handle */
732 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
733 MAXIMUM_ALLOWED_ACCESS
,
734 domain_sid
, &domain_pol
);
735 if (!NT_STATUS_IS_OK(result
)) {
739 names
= TALLOC_ARRAY(mem_ctx
, const char *, num_names
);
741 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
,
742 flags
, num_names
, names
,
743 &num_rids
, &user_rid
, &name_types
);
744 if (!NT_STATUS_IS_OK(result
)) {
748 /* Open domain user */
749 result
= cli_samr_open_user(cli
, mem_ctx
, &domain_pol
,
750 MAXIMUM_ALLOWED_ACCESS
, user_rid
[0], &user_pol
);
752 if (!NT_STATUS_IS_OK(result
)) {
756 /* Query user info */
757 result
= cli_samr_query_userinfo(cli
, mem_ctx
, &user_pol
,
758 info_level
, &user_ctr
);
760 if (!NT_STATUS_IS_OK(result
)) {
764 ctr
.switch_value
= info_level
;
765 ctr
.info
.id7
= &info7
;
767 init_sam_user_info7(&info7
, new_name
);
770 result
= cli_samr_set_userinfo(cli
, mem_ctx
, &user_pol
,
771 info_level
, &cli
->user_session_key
, &ctr
);
773 if (!NT_STATUS_IS_OK(result
)) {
778 if (!NT_STATUS_IS_OK(result
)) {
779 d_printf("Failed to rename user from %s to %s - %s\n", old_name
, new_name
,
782 d_printf("Renamed user from %s to %s\n", old_name
, new_name
);
789 * Rename a user on a remote RPC server
791 * @param argc Standard main() style argc
792 * @param argv Standard main() style argv. Initial components are already
795 * @return A shell status integer (0 for success)
798 static int rpc_user_rename(int argc
, const char **argv
)
800 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_rename_internals
,
805 * Delete a user from a remote RPC server
807 * @param argc Standard main() style argc
808 * @param argv Standard main() style argv. Initial components are already
811 * @return A shell status integer (0 for success)
814 static int rpc_user_delete(int argc
, const char **argv
)
816 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_del_internals
,
821 * Set a password for a user on a remote RPC server
823 * All parameters are provided by the run_rpc_command function, except for
824 * argc, argv which are passes through.
826 * @param domain_sid The domain sid acquired from the remote server
827 * @param cli A cli_state connected to the server.
828 * @param mem_ctx Talloc context, destoyed on completion of the function.
829 * @param argc Standard main() style argc
830 * @param argv Standard main() style argv. Initial components are already
833 * @return Normal NTSTATUS return.
836 static NTSTATUS
rpc_user_password_internals(const DOM_SID
*domain_sid
,
837 const char *domain_name
,
838 struct cli_state
*cli
,
840 int argc
, const char **argv
)
842 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
843 POLICY_HND connect_pol
, domain_pol
, user_pol
;
844 SAM_USERINFO_CTR ctr
;
845 SAM_USER_INFO_24 p24
;
848 const char *new_password
;
852 d_printf("User must be specified\n");
853 rpc_user_usage(argc
, argv
);
860 new_password
= argv
[1];
862 asprintf(&prompt
, "Enter new password for %s:", user
);
863 new_password
= getpass(prompt
);
867 /* Get sam policy and domain handles */
869 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
872 if (!NT_STATUS_IS_OK(result
)) {
876 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
877 MAXIMUM_ALLOWED_ACCESS
,
878 domain_sid
, &domain_pol
);
880 if (!NT_STATUS_IS_OK(result
)) {
884 /* Get handle on user */
887 uint32
*user_rids
, num_rids
, *name_types
;
888 uint32 flags
= 0x000003e8; /* Unknown */
890 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
,
892 &num_rids
, &user_rids
,
895 if (!NT_STATUS_IS_OK(result
)) {
899 result
= cli_samr_open_user(cli
, mem_ctx
, &domain_pol
,
900 MAXIMUM_ALLOWED_ACCESS
,
901 user_rids
[0], &user_pol
);
903 if (!NT_STATUS_IS_OK(result
)) {
908 /* Set password on account */
913 encode_pw_buffer(pwbuf
, new_password
, STR_UNICODE
);
915 init_sam_user_info24(&p24
, (char *)pwbuf
,24);
917 ctr
.switch_value
= 24;
918 ctr
.info
.id24
= &p24
;
920 result
= cli_samr_set_userinfo(cli
, mem_ctx
, &user_pol
, 24,
921 &cli
->user_session_key
, &ctr
);
923 if (!NT_STATUS_IS_OK(result
)) {
927 /* Display results */
935 * Set a user's password on a remote RPC server
937 * @param argc Standard main() style argc
938 * @param argv Standard main() style argv. Initial components are already
941 * @return A shell status integer (0 for success)
944 static int rpc_user_password(int argc
, const char **argv
)
946 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_password_internals
,
951 * List user's groups on a remote RPC server
953 * All parameters are provided by the run_rpc_command function, except for
954 * argc, argv which are passes through.
956 * @param domain_sid The domain sid acquired from the remote server
957 * @param cli A cli_state connected to the server.
958 * @param mem_ctx Talloc context, destoyed on completion of the function.
959 * @param argc Standard main() style argc
960 * @param argv Standard main() style argv. Initial components are already
963 * @return Normal NTSTATUS return.
967 rpc_user_info_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
968 struct cli_state
*cli
,
969 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
971 POLICY_HND connect_pol
, domain_pol
, user_pol
;
972 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
973 uint32
*rids
, num_rids
, *name_types
, num_names
;
974 uint32 flags
= 0x000003e8; /* Unknown */
980 d_printf("User must be specified\n");
981 rpc_user_usage(argc
, argv
);
984 /* Get sam policy handle */
986 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
988 if (!NT_STATUS_IS_OK(result
)) goto done
;
990 /* Get domain policy handle */
992 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
993 MAXIMUM_ALLOWED_ACCESS
,
994 domain_sid
, &domain_pol
);
995 if (!NT_STATUS_IS_OK(result
)) goto done
;
997 /* Get handle on user */
999 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
,
1001 &num_rids
, &rids
, &name_types
);
1003 if (!NT_STATUS_IS_OK(result
)) goto done
;
1005 result
= cli_samr_open_user(cli
, mem_ctx
, &domain_pol
,
1006 MAXIMUM_ALLOWED_ACCESS
,
1007 rids
[0], &user_pol
);
1008 if (!NT_STATUS_IS_OK(result
)) goto done
;
1010 result
= cli_samr_query_usergroups(cli
, mem_ctx
, &user_pol
,
1011 &num_rids
, &user_gids
);
1013 if (!NT_STATUS_IS_OK(result
)) goto done
;
1018 rids
= TALLOC_ARRAY(mem_ctx
, uint32
, num_rids
);
1020 for (i
= 0; i
< num_rids
; i
++)
1021 rids
[i
] = user_gids
[i
].g_rid
;
1023 result
= cli_samr_lookup_rids(cli
, mem_ctx
, &domain_pol
,
1025 &num_names
, &names
, &name_types
);
1027 if (!NT_STATUS_IS_OK(result
)) {
1031 /* Display results */
1033 for (i
= 0; i
< num_names
; i
++)
1034 printf("%s\n", names
[i
]);
1041 * List a user's groups from a remote RPC server
1043 * @param argc Standard main() style argc
1044 * @param argv Standard main() style argv. Initial components are already
1047 * @return A shell status integer (0 for success)
1050 static int rpc_user_info(int argc
, const char **argv
)
1052 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_info_internals
,
1057 * List users on a remote RPC server
1059 * All parameters are provided by the run_rpc_command function, except for
1060 * argc, argv which are passes through.
1062 * @param domain_sid The domain sid acquired from the remote server
1063 * @param cli A cli_state connected to the server.
1064 * @param mem_ctx Talloc context, destoyed on completion of the function.
1065 * @param argc Standard main() style argc
1066 * @param argv Standard main() style argv. Initial components are already
1069 * @return Normal NTSTATUS return.
1073 rpc_user_list_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
1074 struct cli_state
*cli
,
1075 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
1077 POLICY_HND connect_pol
, domain_pol
;
1078 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1079 uint32 start_idx
=0, num_entries
, i
, loop_count
= 0;
1080 SAM_DISPINFO_CTR ctr
;
1081 SAM_DISPINFO_1 info1
;
1083 /* Get sam policy handle */
1085 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1087 if (!NT_STATUS_IS_OK(result
)) {
1091 /* Get domain policy handle */
1093 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
1094 MAXIMUM_ALLOWED_ACCESS
,
1095 domain_sid
, &domain_pol
);
1096 if (!NT_STATUS_IS_OK(result
)) {
1100 /* Query domain users */
1103 ctr
.sam
.info1
= &info1
;
1104 if (opt_long_list_entries
)
1105 d_printf("\nUser name Comment"\
1106 "\n-----------------------------\n");
1109 uint32 max_entries
, max_size
;
1111 get_query_dispinfo_params(
1112 loop_count
, &max_entries
, &max_size
);
1114 result
= cli_samr_query_dispinfo(cli
, mem_ctx
, &domain_pol
,
1115 &start_idx
, 1, &num_entries
,
1116 max_entries
, max_size
, &ctr
);
1119 for (i
= 0; i
< num_entries
; i
++) {
1120 unistr2_to_ascii(user
, &(&ctr
.sam
.info1
->str
[i
])->uni_acct_name
, sizeof(user
)-1);
1121 if (opt_long_list_entries
)
1122 unistr2_to_ascii(desc
, &(&ctr
.sam
.info1
->str
[i
])->uni_acct_desc
, sizeof(desc
)-1);
1124 if (opt_long_list_entries
)
1125 printf("%-21.21s %s\n", user
, desc
);
1127 printf("%s\n", user
);
1129 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
1136 * 'net rpc user' entrypoint.
1137 * @param argc Standard main() style argc
1138 * @param argc Standard main() style argv. Initial components are already
1142 int net_rpc_user(int argc
, const char **argv
)
1144 struct functable func
[] = {
1145 {"add", rpc_user_add
},
1146 {"info", rpc_user_info
},
1147 {"delete", rpc_user_delete
},
1148 {"password", rpc_user_password
},
1149 {"rename", rpc_user_rename
},
1154 if (opt_long_list_entries
) {
1157 return run_rpc_command(NULL
,PI_SAMR
, 0,
1158 rpc_user_list_internals
,
1162 return net_run_function(argc
, argv
, func
, rpc_user_usage
);
1166 /****************************************************************************/
1169 * Basic usage function for 'net rpc group'
1170 * @param argc Standard main() style argc.
1171 * @param argv Standard main() style argv. Initial components are already
1175 static int rpc_group_usage(int argc
, const char **argv
)
1177 return net_help_group(argc
, argv
);
1181 * Delete group on a remote RPC server
1183 * All parameters are provided by the run_rpc_command function, except for
1184 * argc, argv which are passes through.
1186 * @param domain_sid The domain sid acquired from the remote server
1187 * @param cli A cli_state connected to the server.
1188 * @param mem_ctx Talloc context, destoyed on completion of the function.
1189 * @param argc Standard main() style argc
1190 * @param argv Standard main() style argv. Initial components are already
1193 * @return Normal NTSTATUS return.
1196 static NTSTATUS
rpc_group_delete_internals(const DOM_SID
*domain_sid
,
1197 const char *domain_name
,
1198 struct cli_state
*cli
,
1199 TALLOC_CTX
*mem_ctx
,
1200 int argc
, const char **argv
)
1202 POLICY_HND connect_pol
, domain_pol
, group_pol
, user_pol
;
1203 BOOL group_is_primary
= False
;
1204 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1206 uint32
*group_rids
, num_rids
, *name_types
, num_members
,
1207 *group_attrs
, group_rid
;
1208 uint32 flags
= 0x000003e8; /* Unknown */
1211 /* DOM_GID *user_gids; */
1212 SAM_USERINFO_CTR
*user_ctr
;
1216 d_printf("specify group\n");
1217 rpc_group_usage(argc
,argv
);
1218 return NT_STATUS_OK
; /* ok? */
1221 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1224 if (!NT_STATUS_IS_OK(result
)) {
1225 d_printf("Request samr_connect failed\n");
1229 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
1230 MAXIMUM_ALLOWED_ACCESS
,
1231 domain_sid
, &domain_pol
);
1233 if (!NT_STATUS_IS_OK(result
)) {
1234 d_printf("Request open_domain failed\n");
1238 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
,
1240 &num_rids
, &group_rids
,
1243 if (!NT_STATUS_IS_OK(result
)) {
1244 d_printf("Lookup of '%s' failed\n",argv
[0]);
1248 switch (name_types
[0])
1250 case SID_NAME_DOM_GRP
:
1251 result
= cli_samr_open_group(cli
, mem_ctx
, &domain_pol
,
1252 MAXIMUM_ALLOWED_ACCESS
,
1253 group_rids
[0], &group_pol
);
1254 if (!NT_STATUS_IS_OK(result
)) {
1255 d_printf("Request open_group failed");
1259 group_rid
= group_rids
[0];
1261 result
= cli_samr_query_groupmem(cli
, mem_ctx
, &group_pol
,
1262 &num_members
, &group_rids
,
1265 if (!NT_STATUS_IS_OK(result
)) {
1266 d_printf("Unable to query group members of %s",argv
[0]);
1271 d_printf("Domain Group %s (rid: %d) has %d members\n",
1272 argv
[0],group_rid
,num_members
);
1275 /* Check if group is anyone's primary group */
1276 for (i
= 0; i
< num_members
; i
++)
1278 result
= cli_samr_open_user(cli
, mem_ctx
, &domain_pol
,
1279 MAXIMUM_ALLOWED_ACCESS
,
1280 group_rids
[i
], &user_pol
);
1282 if (!NT_STATUS_IS_OK(result
)) {
1283 d_printf("Unable to open group member %d\n",group_rids
[i
]);
1287 ZERO_STRUCT(user_ctr
);
1289 result
= cli_samr_query_userinfo(cli
, mem_ctx
, &user_pol
,
1292 if (!NT_STATUS_IS_OK(result
)) {
1293 d_printf("Unable to lookup userinfo for group member %d\n",group_rids
[i
]);
1297 if (user_ctr
->info
.id21
->group_rid
== group_rid
) {
1298 unistr2_to_ascii(temp
, &(user_ctr
->info
.id21
)->uni_user_name
,
1301 d_printf("Group is primary group of %s\n",temp
);
1302 group_is_primary
= True
;
1305 cli_samr_close(cli
, mem_ctx
, &user_pol
);
1308 if (group_is_primary
) {
1309 d_printf("Unable to delete group because some of it's "
1310 "members have it as primary group\n");
1311 result
= NT_STATUS_MEMBERS_PRIMARY_GROUP
;
1315 /* remove all group members */
1316 for (i
= 0; i
< num_members
; i
++)
1319 d_printf("Remove group member %d...",group_rids
[i
]);
1320 result
= cli_samr_del_groupmem(cli
, mem_ctx
, &group_pol
, group_rids
[i
]);
1322 if (NT_STATUS_IS_OK(result
)) {
1327 d_printf("failed\n");
1332 result
= cli_samr_delete_dom_group(cli
, mem_ctx
, &group_pol
);
1335 /* removing a local group is easier... */
1336 case SID_NAME_ALIAS
:
1337 result
= cli_samr_open_alias(cli
, mem_ctx
, &domain_pol
,
1338 MAXIMUM_ALLOWED_ACCESS
,
1339 group_rids
[0], &group_pol
);
1341 if (!NT_STATUS_IS_OK(result
)) {
1342 d_printf("Request open_alias failed\n");
1346 result
= cli_samr_delete_dom_alias(cli
, mem_ctx
, &group_pol
);
1349 d_printf("%s is of type %s. This command is only for deleting local or global groups\n",
1350 argv
[0],sid_type_lookup(name_types
[0]));
1351 result
= NT_STATUS_UNSUCCESSFUL
;
1356 if (NT_STATUS_IS_OK(result
)) {
1358 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types
[0]),argv
[0]);
1360 d_printf("Deleting of %s failed: %s\n",argv
[0],
1361 get_friendly_nt_error_msg(result
));
1369 static int rpc_group_delete(int argc
, const char **argv
)
1371 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_group_delete_internals
,
1376 rpc_group_add_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
1377 struct cli_state
*cli
,
1378 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
1380 POLICY_HND connect_pol
, domain_pol
, group_pol
;
1381 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1382 GROUP_INFO_CTR group_info
;
1385 d_printf("Group name must be specified\n");
1386 rpc_group_usage(argc
, argv
);
1387 return NT_STATUS_OK
;
1390 /* Get sam policy handle */
1392 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1394 if (!NT_STATUS_IS_OK(result
)) goto done
;
1396 /* Get domain policy handle */
1398 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
1399 MAXIMUM_ALLOWED_ACCESS
,
1400 domain_sid
, &domain_pol
);
1401 if (!NT_STATUS_IS_OK(result
)) goto done
;
1403 /* Create the group */
1405 result
= cli_samr_create_dom_group(cli
, mem_ctx
, &domain_pol
,
1406 argv
[0], MAXIMUM_ALLOWED_ACCESS
,
1408 if (!NT_STATUS_IS_OK(result
)) goto done
;
1410 if (strlen(opt_comment
) == 0) goto done
;
1412 /* We've got a comment to set */
1414 group_info
.switch_value1
= 4;
1415 init_samr_group_info4(&group_info
.group
.info4
, opt_comment
);
1417 result
= cli_samr_set_groupinfo(cli
, mem_ctx
, &group_pol
, &group_info
);
1418 if (!NT_STATUS_IS_OK(result
)) goto done
;
1421 if (NT_STATUS_IS_OK(result
))
1422 DEBUG(5, ("add group succeeded\n"));
1424 d_printf("add group failed: %s\n", nt_errstr(result
));
1430 rpc_alias_add_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
1431 struct cli_state
*cli
,
1432 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
1434 POLICY_HND connect_pol
, domain_pol
, alias_pol
;
1435 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1436 ALIAS_INFO_CTR alias_info
;
1439 d_printf("Alias name must be specified\n");
1440 rpc_group_usage(argc
, argv
);
1441 return NT_STATUS_OK
;
1444 /* Get sam policy handle */
1446 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1448 if (!NT_STATUS_IS_OK(result
)) goto done
;
1450 /* Get domain policy handle */
1452 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
1453 MAXIMUM_ALLOWED_ACCESS
,
1454 domain_sid
, &domain_pol
);
1455 if (!NT_STATUS_IS_OK(result
)) goto done
;
1457 /* Create the group */
1459 result
= cli_samr_create_dom_alias(cli
, mem_ctx
, &domain_pol
,
1460 argv
[0], &alias_pol
);
1461 if (!NT_STATUS_IS_OK(result
)) goto done
;
1463 if (strlen(opt_comment
) == 0) goto done
;
1465 /* We've got a comment to set */
1467 alias_info
.level
= 3;
1468 init_samr_alias_info3(&alias_info
.alias
.info3
, opt_comment
);
1470 result
= cli_samr_set_aliasinfo(cli
, mem_ctx
, &alias_pol
, &alias_info
);
1471 if (!NT_STATUS_IS_OK(result
)) goto done
;
1474 if (NT_STATUS_IS_OK(result
))
1475 DEBUG(5, ("add alias succeeded\n"));
1477 d_printf("add alias failed: %s\n", nt_errstr(result
));
1482 static int rpc_group_add(int argc
, const char **argv
)
1485 return run_rpc_command(NULL
, PI_SAMR
, 0,
1486 rpc_alias_add_internals
,
1489 return run_rpc_command(NULL
, PI_SAMR
, 0,
1490 rpc_group_add_internals
,
1495 get_sid_from_name(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
, const char *name
,
1496 DOM_SID
*sid
, enum SID_NAME_USE
*type
)
1498 int current_pipe
= cli
->pipe_idx
;
1500 DOM_SID
*sids
= NULL
;
1501 uint32
*types
= NULL
;
1503 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1505 if (current_pipe
!= PI_LSARPC
) {
1507 if (current_pipe
!= -1)
1508 cli_nt_session_close(cli
);
1510 if (!cli_nt_session_open(cli
, PI_LSARPC
))
1514 result
= cli_lsa_open_policy(cli
, mem_ctx
, False
,
1515 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
1517 if (!NT_STATUS_IS_OK(result
))
1520 result
= cli_lsa_lookup_names(cli
, mem_ctx
, &lsa_pol
, 1,
1521 &name
, &sids
, &types
);
1523 if (NT_STATUS_IS_OK(result
)) {
1524 sid_copy(sid
, &sids
[0]);
1528 cli_lsa_close(cli
, mem_ctx
, &lsa_pol
);
1531 if (current_pipe
!= PI_LSARPC
) {
1532 cli_nt_session_close(cli
);
1533 if (current_pipe
!= -1)
1534 cli_nt_session_open(cli
, current_pipe
);
1537 if (!NT_STATUS_IS_OK(result
) && (StrnCaseCmp(name
, "S-", 2) == 0)) {
1539 /* Try as S-1-5-whatever */
1543 if (string_to_sid(&tmp_sid
, name
)) {
1544 sid_copy(sid
, &tmp_sid
);
1545 *type
= SID_NAME_UNKNOWN
;
1546 result
= NT_STATUS_OK
;
1554 rpc_add_groupmem(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1555 const DOM_SID
*group_sid
, const char *member
)
1557 POLICY_HND connect_pol
, domain_pol
;
1560 POLICY_HND group_pol
;
1563 uint32
*rids
= NULL
;
1564 uint32
*rid_types
= NULL
;
1568 sid_copy(&sid
, group_sid
);
1570 if (!sid_split_rid(&sid
, &group_rid
))
1571 return NT_STATUS_UNSUCCESSFUL
;
1573 /* Get sam policy handle */
1574 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1576 if (!NT_STATUS_IS_OK(result
))
1579 /* Get domain policy handle */
1580 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
1581 MAXIMUM_ALLOWED_ACCESS
,
1583 if (!NT_STATUS_IS_OK(result
))
1586 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
, 1000,
1588 &num_rids
, &rids
, &rid_types
);
1590 if (!NT_STATUS_IS_OK(result
)) {
1591 d_printf("Could not lookup up group member %s\n", member
);
1595 result
= cli_samr_open_group(cli
, mem_ctx
, &domain_pol
,
1596 MAXIMUM_ALLOWED_ACCESS
,
1597 group_rid
, &group_pol
);
1599 if (!NT_STATUS_IS_OK(result
))
1602 result
= cli_samr_add_groupmem(cli
, mem_ctx
, &group_pol
, rids
[0]);
1605 cli_samr_close(cli
, mem_ctx
, &connect_pol
);
1610 rpc_add_aliasmem(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1611 const DOM_SID
*alias_sid
, const char *member
)
1613 POLICY_HND connect_pol
, domain_pol
;
1616 POLICY_HND alias_pol
;
1619 enum SID_NAME_USE member_type
;
1623 sid_copy(&sid
, alias_sid
);
1625 if (!sid_split_rid(&sid
, &alias_rid
))
1626 return NT_STATUS_UNSUCCESSFUL
;
1628 result
= get_sid_from_name(cli
, mem_ctx
, member
,
1629 &member_sid
, &member_type
);
1631 if (!NT_STATUS_IS_OK(result
)) {
1632 d_printf("Could not lookup up group member %s\n", member
);
1636 /* Get sam policy handle */
1637 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1639 if (!NT_STATUS_IS_OK(result
)) {
1643 /* Get domain policy handle */
1644 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
1645 MAXIMUM_ALLOWED_ACCESS
,
1647 if (!NT_STATUS_IS_OK(result
)) {
1651 result
= cli_samr_open_alias(cli
, mem_ctx
, &domain_pol
,
1652 MAXIMUM_ALLOWED_ACCESS
,
1653 alias_rid
, &alias_pol
);
1655 if (!NT_STATUS_IS_OK(result
))
1658 result
= cli_samr_add_aliasmem(cli
, mem_ctx
, &alias_pol
, &member_sid
);
1660 if (!NT_STATUS_IS_OK(result
))
1664 cli_samr_close(cli
, mem_ctx
, &connect_pol
);
1669 rpc_group_addmem_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
1670 struct cli_state
*cli
,
1671 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
1674 enum SID_NAME_USE group_type
;
1677 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
1678 return NT_STATUS_UNSUCCESSFUL
;
1681 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
1682 &group_sid
, &group_type
))) {
1683 d_printf("Could not lookup group name %s\n", argv
[0]);
1684 return NT_STATUS_UNSUCCESSFUL
;
1687 if (group_type
== SID_NAME_DOM_GRP
) {
1688 NTSTATUS result
= rpc_add_groupmem(cli
, mem_ctx
,
1689 &group_sid
, argv
[1]);
1691 if (!NT_STATUS_IS_OK(result
)) {
1692 d_printf("Could not add %s to %s: %s\n",
1693 argv
[1], argv
[0], nt_errstr(result
));
1698 if (group_type
== SID_NAME_ALIAS
) {
1699 NTSTATUS result
= rpc_add_aliasmem(cli
, mem_ctx
,
1700 &group_sid
, argv
[1]);
1702 if (!NT_STATUS_IS_OK(result
)) {
1703 d_printf("Could not add %s to %s: %s\n",
1704 argv
[1], argv
[0], nt_errstr(result
));
1709 d_printf("Can only add members to global or local groups which "
1710 "%s is not\n", argv
[0]);
1712 return NT_STATUS_UNSUCCESSFUL
;
1715 static int rpc_group_addmem(int argc
, const char **argv
)
1717 return run_rpc_command(NULL
, PI_SAMR
, 0,
1718 rpc_group_addmem_internals
,
1723 rpc_del_groupmem(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1724 const DOM_SID
*group_sid
, const char *member
)
1726 POLICY_HND connect_pol
, domain_pol
;
1729 POLICY_HND group_pol
;
1732 uint32
*rids
= NULL
;
1733 uint32
*rid_types
= NULL
;
1737 sid_copy(&sid
, group_sid
);
1739 if (!sid_split_rid(&sid
, &group_rid
))
1740 return NT_STATUS_UNSUCCESSFUL
;
1742 /* Get sam policy handle */
1743 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1745 if (!NT_STATUS_IS_OK(result
))
1748 /* Get domain policy handle */
1749 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
1750 MAXIMUM_ALLOWED_ACCESS
,
1752 if (!NT_STATUS_IS_OK(result
))
1755 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
, 1000,
1757 &num_rids
, &rids
, &rid_types
);
1759 if (!NT_STATUS_IS_OK(result
)) {
1760 d_printf("Could not lookup up group member %s\n", member
);
1764 result
= cli_samr_open_group(cli
, mem_ctx
, &domain_pol
,
1765 MAXIMUM_ALLOWED_ACCESS
,
1766 group_rid
, &group_pol
);
1768 if (!NT_STATUS_IS_OK(result
))
1771 result
= cli_samr_del_groupmem(cli
, mem_ctx
, &group_pol
, rids
[0]);
1774 cli_samr_close(cli
, mem_ctx
, &connect_pol
);
1779 rpc_del_aliasmem(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
1780 const DOM_SID
*alias_sid
, const char *member
)
1782 POLICY_HND connect_pol
, domain_pol
;
1785 POLICY_HND alias_pol
;
1788 enum SID_NAME_USE member_type
;
1792 sid_copy(&sid
, alias_sid
);
1794 if (!sid_split_rid(&sid
, &alias_rid
))
1795 return NT_STATUS_UNSUCCESSFUL
;
1797 result
= get_sid_from_name(cli
, mem_ctx
, member
,
1798 &member_sid
, &member_type
);
1800 if (!NT_STATUS_IS_OK(result
)) {
1801 d_printf("Could not lookup up group member %s\n", member
);
1805 /* Get sam policy handle */
1806 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1808 if (!NT_STATUS_IS_OK(result
)) {
1812 /* Get domain policy handle */
1813 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
1814 MAXIMUM_ALLOWED_ACCESS
,
1816 if (!NT_STATUS_IS_OK(result
)) {
1820 result
= cli_samr_open_alias(cli
, mem_ctx
, &domain_pol
,
1821 MAXIMUM_ALLOWED_ACCESS
,
1822 alias_rid
, &alias_pol
);
1824 if (!NT_STATUS_IS_OK(result
))
1827 result
= cli_samr_del_aliasmem(cli
, mem_ctx
, &alias_pol
, &member_sid
);
1829 if (!NT_STATUS_IS_OK(result
))
1833 cli_samr_close(cli
, mem_ctx
, &connect_pol
);
1838 rpc_group_delmem_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
1839 struct cli_state
*cli
,
1840 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
1843 enum SID_NAME_USE group_type
;
1846 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
1847 return NT_STATUS_UNSUCCESSFUL
;
1850 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
1851 &group_sid
, &group_type
))) {
1852 d_printf("Could not lookup group name %s\n", argv
[0]);
1853 return NT_STATUS_UNSUCCESSFUL
;
1856 if (group_type
== SID_NAME_DOM_GRP
) {
1857 NTSTATUS result
= rpc_del_groupmem(cli
, mem_ctx
,
1858 &group_sid
, argv
[1]);
1860 if (!NT_STATUS_IS_OK(result
)) {
1861 d_printf("Could not del %s from %s: %s\n",
1862 argv
[1], argv
[0], nt_errstr(result
));
1867 if (group_type
== SID_NAME_ALIAS
) {
1868 NTSTATUS result
= rpc_del_aliasmem(cli
, mem_ctx
,
1869 &group_sid
, argv
[1]);
1871 if (!NT_STATUS_IS_OK(result
)) {
1872 d_printf("Could not del %s from %s: %s\n",
1873 argv
[1], argv
[0], nt_errstr(result
));
1878 d_printf("Can only delete members from global or local groups which "
1879 "%s is not\n", argv
[0]);
1881 return NT_STATUS_UNSUCCESSFUL
;
1884 static int rpc_group_delmem(int argc
, const char **argv
)
1886 return run_rpc_command(NULL
, PI_SAMR
, 0,
1887 rpc_group_delmem_internals
,
1892 * List groups on a remote RPC server
1894 * All parameters are provided by the run_rpc_command function, except for
1895 * argc, argv which are passes through.
1897 * @param domain_sid The domain sid acquired from the remote server
1898 * @param cli A cli_state connected to the server.
1899 * @param mem_ctx Talloc context, destoyed on completion of the function.
1900 * @param argc Standard main() style argc
1901 * @param argv Standard main() style argv. Initial components are already
1904 * @return Normal NTSTATUS return.
1908 rpc_group_list_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
1909 struct cli_state
*cli
,
1910 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
1912 POLICY_HND connect_pol
, domain_pol
;
1913 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1914 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
1915 struct acct_info
*groups
;
1916 BOOL global
= False
;
1918 BOOL builtin
= False
;
1926 for (i
=0; i
<argc
; i
++) {
1927 if (strequal(argv
[i
], "global"))
1930 if (strequal(argv
[i
], "local"))
1933 if (strequal(argv
[i
], "builtin"))
1937 /* Get sam policy handle */
1939 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1941 if (!NT_STATUS_IS_OK(result
)) {
1945 /* Get domain policy handle */
1947 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
1948 MAXIMUM_ALLOWED_ACCESS
,
1949 domain_sid
, &domain_pol
);
1950 if (!NT_STATUS_IS_OK(result
)) {
1954 /* Query domain groups */
1955 if (opt_long_list_entries
)
1956 d_printf("\nGroup name Comment"\
1957 "\n-----------------------------\n");
1959 SAM_DISPINFO_CTR ctr
;
1960 SAM_DISPINFO_3 info3
;
1965 ctr
.sam
.info3
= &info3
;
1969 get_query_dispinfo_params(
1970 loop_count
, &max_entries
, &max_size
);
1972 result
= cli_samr_query_dispinfo(cli
, mem_ctx
, &domain_pol
,
1973 &start_idx
, 3, &num_entries
,
1974 max_entries
, max_size
, &ctr
);
1976 if (!NT_STATUS_IS_OK(result
) &&
1977 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
1980 for (i
= 0; i
< num_entries
; i
++) {
1982 fstring group
, desc
;
1984 unistr2_to_ascii(group
, &(&ctr
.sam
.info3
->str
[i
])->uni_grp_name
, sizeof(group
)-1);
1985 unistr2_to_ascii(desc
, &(&ctr
.sam
.info3
->str
[i
])->uni_grp_desc
, sizeof(desc
)-1);
1987 if (opt_long_list_entries
)
1988 printf("%-21.21s %-50.50s\n",
1991 printf("%s\n", group
);
1993 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
1994 /* query domain aliases */
1999 /* The max_size field in cli_samr_enum_als_groups is more like
2000 * an account_control field with indiviual bits what to
2001 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2002 * everything. I'm too lazy (sorry) to get this through to
2003 * rpc_parse/ etc. Volker */
2005 result
= cli_samr_enum_als_groups(cli
, mem_ctx
, &domain_pol
,
2007 &groups
, &num_entries
);
2009 if (!NT_STATUS_IS_OK(result
) &&
2010 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2013 for (i
= 0; i
< num_entries
; i
++) {
2015 char *description
= NULL
;
2017 if (opt_long_list_entries
) {
2019 POLICY_HND alias_pol
;
2022 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli
, mem_ctx
,
2027 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli
, mem_ctx
,
2030 (NT_STATUS_IS_OK(cli_samr_close(cli
, mem_ctx
,
2032 description
= unistr2_tdup(mem_ctx
,
2033 ctr
.alias
.info3
.description
.string
);
2037 if (description
!= NULL
) {
2038 printf("%-21.21s %-50.50s\n",
2039 groups
[i
].acct_name
,
2042 printf("%s\n", groups
[i
].acct_name
);
2045 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2046 cli_samr_close(cli
, mem_ctx
, &domain_pol
);
2047 /* Get builtin policy handle */
2049 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
2050 MAXIMUM_ALLOWED_ACCESS
,
2051 &global_sid_Builtin
, &domain_pol
);
2052 if (!NT_STATUS_IS_OK(result
)) {
2055 /* query builtin aliases */
2058 if (!builtin
) break;
2060 result
= cli_samr_enum_als_groups(cli
, mem_ctx
, &domain_pol
,
2061 &start_idx
, max_entries
,
2062 &groups
, &num_entries
);
2064 if (!NT_STATUS_IS_OK(result
) &&
2065 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2068 for (i
= 0; i
< num_entries
; i
++) {
2070 char *description
= NULL
;
2072 if (opt_long_list_entries
) {
2074 POLICY_HND alias_pol
;
2077 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli
, mem_ctx
,
2082 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli
, mem_ctx
,
2085 (NT_STATUS_IS_OK(cli_samr_close(cli
, mem_ctx
,
2087 description
= unistr2_tdup(mem_ctx
,
2088 ctr
.alias
.info3
.description
.string
);
2092 if (description
!= NULL
) {
2093 printf("%-21.21s %-50.50s\n",
2094 groups
[i
].acct_name
,
2097 printf("%s\n", groups
[i
].acct_name
);
2100 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2106 static int rpc_group_list(int argc
, const char **argv
)
2108 return run_rpc_command(NULL
, PI_SAMR
, 0,
2109 rpc_group_list_internals
,
2114 rpc_list_group_members(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
2115 const char *domain_name
, const DOM_SID
*domain_sid
,
2116 POLICY_HND
*domain_pol
, uint32 rid
)
2119 POLICY_HND group_pol
;
2120 uint32 num_members
, *group_rids
, *group_attrs
;
2127 sid_to_string(sid_str
, domain_sid
);
2129 result
= cli_samr_open_group(cli
, mem_ctx
, domain_pol
,
2130 MAXIMUM_ALLOWED_ACCESS
,
2133 if (!NT_STATUS_IS_OK(result
))
2136 result
= cli_samr_query_groupmem(cli
, mem_ctx
, &group_pol
,
2137 &num_members
, &group_rids
,
2140 if (!NT_STATUS_IS_OK(result
))
2143 while (num_members
> 0) {
2144 int this_time
= 512;
2146 if (num_members
< this_time
)
2147 this_time
= num_members
;
2149 result
= cli_samr_lookup_rids(cli
, mem_ctx
, domain_pol
,
2150 this_time
, group_rids
,
2151 &num_names
, &names
, &name_types
);
2153 if (!NT_STATUS_IS_OK(result
))
2156 /* We only have users as members, but make the output
2157 the same as the output of alias members */
2159 for (i
= 0; i
< this_time
; i
++) {
2161 if (opt_long_list_entries
) {
2162 printf("%s-%d %s\\%s %d\n", sid_str
,
2163 group_rids
[i
], domain_name
, names
[i
],
2166 printf("%s\\%s\n", domain_name
, names
[i
]);
2170 num_members
-= this_time
;
2174 return NT_STATUS_OK
;
2178 rpc_list_alias_members(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
2179 POLICY_HND
*domain_pol
, uint32 rid
)
2182 POLICY_HND alias_pol
, lsa_pol
;
2184 DOM_SID
*alias_sids
;
2190 result
= cli_samr_open_alias(cli
, mem_ctx
, domain_pol
,
2191 MAXIMUM_ALLOWED_ACCESS
, rid
, &alias_pol
);
2193 if (!NT_STATUS_IS_OK(result
))
2196 result
= cli_samr_query_aliasmem(cli
, mem_ctx
, &alias_pol
,
2197 &num_members
, &alias_sids
);
2199 if (!NT_STATUS_IS_OK(result
)) {
2200 d_printf("Couldn't list alias members\n");
2204 if (num_members
== 0) {
2205 return NT_STATUS_OK
;
2208 cli_nt_session_close(cli
);
2210 if (!cli_nt_session_open(cli
, PI_LSARPC
)) {
2211 d_printf("Couldn't open LSA pipe\n");
2215 result
= cli_lsa_open_policy(cli
, mem_ctx
, True
,
2216 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
2218 if (!NT_STATUS_IS_OK(result
)) {
2219 d_printf("Couldn't open LSA policy handle\n");
2223 result
= cli_lsa_lookup_sids(cli
, mem_ctx
, &lsa_pol
, num_members
,
2225 &domains
, &names
, &types
);
2227 if (!NT_STATUS_IS_OK(result
) &&
2228 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
2229 d_printf("Couldn't lookup SIDs\n");
2233 for (i
= 0; i
< num_members
; i
++) {
2235 sid_to_string(sid_str
, &alias_sids
[i
]);
2237 if (opt_long_list_entries
) {
2238 printf("%s %s\\%s %d\n", sid_str
,
2239 domains
[i
] ? domains
[i
] : "*unknown*",
2240 names
[i
] ? names
[i
] : "*unknown*", types
[i
]);
2243 printf("%s\\%s\n", domains
[i
], names
[i
]);
2245 printf("%s\n", sid_str
);
2249 return NT_STATUS_OK
;
2253 rpc_group_members_internals(const DOM_SID
*domain_sid
,
2254 const char *domain_name
,
2255 struct cli_state
*cli
,
2256 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
2259 POLICY_HND connect_pol
, domain_pol
;
2260 uint32 num_rids
, *rids
, *rid_types
;
2262 /* Get sam policy handle */
2264 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
2267 if (!NT_STATUS_IS_OK(result
))
2270 /* Get domain policy handle */
2272 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
2273 MAXIMUM_ALLOWED_ACCESS
,
2274 domain_sid
, &domain_pol
);
2276 if (!NT_STATUS_IS_OK(result
))
2279 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
, 1000,
2280 1, argv
, &num_rids
, &rids
, &rid_types
);
2282 if (!NT_STATUS_IS_OK(result
)) {
2284 /* Ok, did not find it in the global sam, try with builtin */
2286 DOM_SID sid_Builtin
;
2288 cli_samr_close(cli
, mem_ctx
, &domain_pol
);
2290 string_to_sid(&sid_Builtin
, "S-1-5-32");
2292 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
2293 MAXIMUM_ALLOWED_ACCESS
,
2294 &sid_Builtin
, &domain_pol
);
2296 if (!NT_STATUS_IS_OK(result
)) {
2297 d_printf("Couldn't find group %s\n", argv
[0]);
2301 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
, 1000,
2305 if (!NT_STATUS_IS_OK(result
)) {
2306 d_printf("Couldn't find group %s\n", argv
[0]);
2311 if (num_rids
!= 1) {
2312 d_printf("Couldn't find group %s\n", argv
[0]);
2316 if (rid_types
[0] == SID_NAME_DOM_GRP
) {
2317 return rpc_list_group_members(cli
, mem_ctx
, domain_name
,
2318 domain_sid
, &domain_pol
,
2322 if (rid_types
[0] == SID_NAME_ALIAS
) {
2323 return rpc_list_alias_members(cli
, mem_ctx
, &domain_pol
,
2327 return NT_STATUS_NO_SUCH_GROUP
;
2330 static int rpc_group_members(int argc
, const char **argv
)
2333 return rpc_group_usage(argc
, argv
);
2336 return run_rpc_command(NULL
, PI_SAMR
, 0,
2337 rpc_group_members_internals
,
2342 rpc_group_rename_internals(const DOM_SID
*domain_sid
,
2343 const char *domain_name
,
2344 struct cli_state
*cli
,
2345 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
2348 POLICY_HND connect_pol
, domain_pol
, group_pol
;
2349 uint32 num_rids
, *rids
, *rid_types
;
2353 d_printf("Usage: 'net rpc group rename group newname'\n");
2354 return NT_STATUS_UNSUCCESSFUL
;
2357 /* Get sam policy handle */
2359 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
2362 if (!NT_STATUS_IS_OK(result
))
2365 /* Get domain policy handle */
2367 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
2368 MAXIMUM_ALLOWED_ACCESS
,
2369 domain_sid
, &domain_pol
);
2371 if (!NT_STATUS_IS_OK(result
))
2374 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
, 1000,
2375 1, argv
, &num_rids
, &rids
, &rid_types
);
2377 if (num_rids
!= 1) {
2378 d_printf("Couldn't find group %s\n", argv
[0]);
2382 if (rid_types
[0] != SID_NAME_DOM_GRP
) {
2383 d_printf("Can only rename domain groups\n");
2384 return NT_STATUS_UNSUCCESSFUL
;
2387 result
= cli_samr_open_group(cli
, mem_ctx
, &domain_pol
,
2388 MAXIMUM_ALLOWED_ACCESS
,
2389 rids
[0], &group_pol
);
2391 if (!NT_STATUS_IS_OK(result
))
2396 ctr
.switch_value1
= 2;
2397 init_samr_group_info2(&ctr
.group
.info2
, argv
[1]);
2399 result
= cli_samr_set_groupinfo(cli
, mem_ctx
, &group_pol
, &ctr
);
2401 if (!NT_STATUS_IS_OK(result
))
2404 return NT_STATUS_NO_SUCH_GROUP
;
2407 static int rpc_group_rename(int argc
, const char **argv
)
2410 return rpc_group_usage(argc
, argv
);
2413 return run_rpc_command(NULL
, PI_SAMR
, 0,
2414 rpc_group_rename_internals
,
2419 * 'net rpc group' entrypoint.
2420 * @param argc Standard main() style argc
2421 * @param argc Standard main() style argv. Initial components are already
2425 int net_rpc_group(int argc
, const char **argv
)
2427 struct functable func
[] = {
2428 {"add", rpc_group_add
},
2429 {"delete", rpc_group_delete
},
2430 {"addmem", rpc_group_addmem
},
2431 {"delmem", rpc_group_delmem
},
2432 {"list", rpc_group_list
},
2433 {"members", rpc_group_members
},
2434 {"rename", rpc_group_rename
},
2439 if (opt_long_list_entries
) {
2442 return run_rpc_command(NULL
, PI_SAMR
, 0,
2443 rpc_group_list_internals
,
2447 return net_run_function(argc
, argv
, func
, rpc_group_usage
);
2450 /****************************************************************************/
2452 static int rpc_share_usage(int argc
, const char **argv
)
2454 return net_help_share(argc
, argv
);
2458 * Add a share on a remote RPC server
2460 * All parameters are provided by the run_rpc_command function, except for
2461 * argc, argv which are passes through.
2463 * @param domain_sid The domain sid acquired from the remote server
2464 * @param cli A cli_state connected to the server.
2465 * @param mem_ctx Talloc context, destoyed on completion of the function.
2466 * @param argc Standard main() style argc
2467 * @param argv Standard main() style argv. Initial components are already
2470 * @return Normal NTSTATUS return.
2473 rpc_share_add_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
2474 struct cli_state
*cli
,
2475 TALLOC_CTX
*mem_ctx
,int argc
, const char **argv
)
2478 char *sharename
=talloc_strdup(mem_ctx
, argv
[0]);
2480 uint32 type
=0; /* only allow disk shares to be added */
2481 uint32 num_users
=0, perms
=0;
2482 char *password
=NULL
; /* don't allow a share password */
2485 path
= strchr(sharename
, '=');
2487 return NT_STATUS_UNSUCCESSFUL
;
2490 result
= cli_srvsvc_net_share_add(cli
, mem_ctx
, sharename
, type
,
2491 opt_comment
, perms
, opt_maxusers
,
2492 num_users
, path
, password
,
2494 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
2497 static int rpc_share_add(int argc
, const char **argv
)
2499 if ((argc
< 1) || !strchr(argv
[0], '=')) {
2500 DEBUG(1,("Sharename or path not specified on add\n"));
2501 return rpc_share_usage(argc
, argv
);
2503 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
2504 rpc_share_add_internals
,
2509 * Delete a share on a remote RPC server
2511 * All parameters are provided by the run_rpc_command function, except for
2512 * argc, argv which are passes through.
2514 * @param domain_sid The domain sid acquired from the remote server
2515 * @param cli A cli_state connected to the server.
2516 * @param mem_ctx Talloc context, destoyed on completion of the function.
2517 * @param argc Standard main() style argc
2518 * @param argv Standard main() style argv. Initial components are already
2521 * @return Normal NTSTATUS return.
2524 rpc_share_del_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
2525 struct cli_state
*cli
,
2526 TALLOC_CTX
*mem_ctx
,int argc
, const char **argv
)
2530 result
= cli_srvsvc_net_share_del(cli
, mem_ctx
, argv
[0]);
2531 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
2535 * Delete a share on a remote RPC server
2537 * @param domain_sid The domain sid acquired from the remote server
2538 * @param argc Standard main() style argc
2539 * @param argv Standard main() style argv. Initial components are already
2542 * @return A shell status integer (0 for success)
2544 static int rpc_share_delete(int argc
, const char **argv
)
2547 DEBUG(1,("Sharename not specified on delete\n"));
2548 return rpc_share_usage(argc
, argv
);
2550 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
2551 rpc_share_del_internals
,
2556 * Formatted print of share info
2558 * @param info1 pointer to SRV_SHARE_INFO_1 to format
2561 static void display_share_info_1(SRV_SHARE_INFO_1
*info1
)
2563 fstring netname
= "", remark
= "";
2565 rpcstr_pull_unistr2_fstring(netname
, &info1
->info_1_str
.uni_netname
);
2566 rpcstr_pull_unistr2_fstring(remark
, &info1
->info_1_str
.uni_remark
);
2568 if (opt_long_list_entries
) {
2569 d_printf("%-12s %-8.8s %-50s\n",
2570 netname
, share_type
[info1
->info_1
.type
], remark
);
2572 d_printf("%s\n", netname
);
2578 * List shares on a remote RPC server
2580 * All parameters are provided by the run_rpc_command function, except for
2581 * argc, argv which are passes through.
2583 * @param domain_sid The domain sid acquired from the remote server
2584 * @param cli A cli_state connected to the server.
2585 * @param mem_ctx Talloc context, destoyed on completion of the function.
2586 * @param argc Standard main() style argc
2587 * @param argv Standard main() style argv. Initial components are already
2590 * @return Normal NTSTATUS return.
2594 rpc_share_list_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
2595 struct cli_state
*cli
,
2596 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
2598 SRV_SHARE_INFO_CTR ctr
;
2601 uint32 preferred_len
= 0xffffffff, i
;
2603 init_enum_hnd(&hnd
, 0);
2605 result
= cli_srvsvc_net_share_enum(
2606 cli
, mem_ctx
, 1, &ctr
, preferred_len
, &hnd
);
2608 if (!W_ERROR_IS_OK(result
))
2611 /* Display results */
2613 if (opt_long_list_entries
) {
2615 "\nEnumerating shared resources (exports) on remote server:\n\n"\
2616 "\nShare name Type Description\n"\
2617 "---------- ---- -----------\n");
2619 for (i
= 0; i
< ctr
.num_entries
; i
++)
2620 display_share_info_1(&ctr
.share
.info1
[i
]);
2622 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
2626 * Migrate shares from a remote RPC server to the local RPC srever
2628 * All parameters are provided by the run_rpc_command function, except for
2629 * argc, argv which are passes through.
2631 * @param domain_sid The domain sid acquired from the remote server
2632 * @param cli A cli_state connected to the server.
2633 * @param mem_ctx Talloc context, destoyed on completion of the function.
2634 * @param argc Standard main() style argc
2635 * @param argv Standard main() style argv. Initial components are already
2638 * @return Normal NTSTATUS return.
2641 rpc_share_migrate_shares_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
2642 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
2643 int argc
, const char **argv
)
2646 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
2647 SRV_SHARE_INFO_CTR ctr_src
;
2649 uint32 type
= 0; /* only allow disk shares to be added */
2650 uint32 num_uses
= 0, perms
= 0, max_uses
= 0;
2651 char *password
= NULL
; /* don't allow a share password */
2652 uint32 preferred_len
= 0xffffffff, i
;
2653 BOOL got_dst_srvsvc_pipe
= False
;
2654 struct cli_state
*cli_dst
= NULL
;
2655 uint32 level
= 502; /* includes secdesc */
2656 SEC_DESC
*share_sd
= NULL
;
2658 init_enum_hnd(&hnd
, 0);
2660 result
= cli_srvsvc_net_share_enum(
2661 cli
, mem_ctx
, level
, &ctr_src
, preferred_len
, &hnd
);
2662 if (!W_ERROR_IS_OK(result
))
2665 /* connect local PI_SRVSVC */
2666 nt_status
= connect_pipe(&cli_dst
, PI_SRVSVC
, &got_dst_srvsvc_pipe
);
2667 if (!NT_STATUS_IS_OK(nt_status
))
2671 for (i
= 0; i
< ctr_src
.num_entries
; i
++) {
2673 fstring netname
= "", remark
= "", path
= "";
2674 /* reset error-code */
2675 nt_status
= NT_STATUS_UNSUCCESSFUL
;
2677 rpcstr_pull_unistr2_fstring(
2678 netname
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_netname
);
2679 rpcstr_pull_unistr2_fstring(
2680 remark
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_remark
);
2681 rpcstr_pull_unistr2_fstring(
2682 path
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_path
);
2683 num_uses
= ctr_src
.share
.info502
[i
].info_502
.num_uses
;
2684 max_uses
= ctr_src
.share
.info502
[i
].info_502
.max_uses
;
2685 perms
= ctr_src
.share
.info502
[i
].info_502
.perms
;
2689 share_sd
= dup_sec_desc(
2690 mem_ctx
, ctr_src
.share
.info502
[i
].info_502_str
.sd
);
2692 /* since we do not have NetShareGetInfo implemented in samba3 we
2693 only can skip inside the enum-ctr_src */
2695 char *one_share
= talloc_strdup(mem_ctx
, argv
[0]);
2696 if (!strequal(netname
, one_share
))
2700 /* skip builtin shares */
2701 /* FIXME: should print$ be added too ? */
2702 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$") ||
2703 strequal(netname
,"global"))
2706 if (opt_exclude
&& in_list(netname
, opt_exclude
, False
)) {
2707 printf("excluding [%s]\n", netname
);
2711 /* only work with file-shares */
2712 if (!cli_send_tconX(cli
, netname
, "A:", "", 0)) {
2713 d_printf("skipping [%s]: not a file share.\n", netname
);
2721 /* finallly add the share on the dst server
2722 please note that samba currently does not allow to
2723 add a share without existing directory */
2725 printf("migrating: [%s], path: %s, comment: %s, %s share-ACLs\n",
2726 netname
, path
, remark
, opt_acls
? "including" : "without" );
2728 if (opt_verbose
&& opt_acls
)
2729 display_sec_desc(share_sd
);
2731 result
= cli_srvsvc_net_share_add(cli_dst
, mem_ctx
, netname
, type
,
2732 remark
, perms
, max_uses
,
2733 num_uses
, path
, password
,
2736 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ALREADY_EXISTS
)) {
2737 printf(" [%s] does already exist\n", netname
);
2741 if (!W_ERROR_IS_OK(result
)) {
2742 printf("cannot add share: %s\n", dos_errstr(result
));
2748 nt_status
= NT_STATUS_OK
;
2751 if (got_dst_srvsvc_pipe
) {
2752 cli_nt_session_close(cli_dst
);
2753 cli_shutdown(cli_dst
);
2761 * Migrate shares from a rpc-server to another
2763 * @param argc Standard main() style argc
2764 * @param argv Standard main() style argv. Initial components are already
2767 * @return A shell status integer (0 for success)
2769 static int rpc_share_migrate_shares(int argc
, const char **argv
)
2773 printf("no server to migrate\n");
2777 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
2778 rpc_share_migrate_shares_internals
,
2782 typedef struct copy_clistate
{
2783 TALLOC_CTX
*mem_ctx
;
2784 struct cli_state
*cli_share_src
;
2785 struct cli_state
*cli_share_dst
;
2793 * @param f file_info
2794 * @param mask current search mask
2795 * @param state arg-pointer
2798 static void copy_fn(const char *mnt
, file_info
*f
, const char *mask
, void *state
)
2800 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
2801 struct copy_clistate
*local_state
= (struct copy_clistate
*)state
;
2802 fstring filename
, new_mask
, dir
;
2804 if (strequal(f
->name
, ".") || strequal(f
->name
, ".."))
2807 DEBUG(3,("got mask: %s, name: %s\n", mask
, f
->name
));
2810 if (f
->mode
& aDIR
) {
2812 DEBUG(3,("got dir: %s\n", f
->name
));
2814 fstrcpy(dir
, local_state
->cwd
);
2816 fstrcat(dir
, f
->name
);
2818 /* create that directory */
2819 nt_status
= net_copy_file(local_state
->mem_ctx
,
2820 local_state
->cli_share_src
,
2821 local_state
->cli_share_dst
,
2823 opt_acls
? True
: False
,
2824 opt_attrs
? True
: False
,
2825 opt_timestamps
? True
: False
,
2828 if (!NT_STATUS_IS_OK(nt_status
))
2829 printf("could not copy dir %s: %s\n",
2830 dir
, nt_errstr(nt_status
));
2832 /* search below that directory */
2833 fstrcpy(new_mask
, dir
);
2834 fstrcat(new_mask
, "\\*");
2836 if (!sync_files(local_state
->mem_ctx
,
2837 local_state
->cli_share_src
,
2838 local_state
->cli_share_dst
,
2841 printf("could not sync files\n");
2848 fstrcpy(filename
, local_state
->cwd
);
2849 fstrcat(filename
, "\\");
2850 fstrcat(filename
, f
->name
);
2852 DEBUG(3,("got file: %s\n", filename
));
2854 nt_status
= net_copy_file(local_state
->mem_ctx
,
2855 local_state
->cli_share_src
,
2856 local_state
->cli_share_dst
,
2858 opt_acls
? True
: False
,
2859 opt_attrs
? True
: False
,
2860 opt_timestamps
? True
: False
,
2863 if (!NT_STATUS_IS_OK(nt_status
))
2864 printf("could not copy file %s: %s\n",
2865 filename
, nt_errstr(nt_status
));
2870 * sync files, can be called recursivly to list files
2871 * and then call copy_fn for each file
2873 * @param mem_ctx TALLOC_CTX
2874 * @param cli_share_src a connected share on the originating server
2875 * @param cli_share_dst a connected share on the destination server
2876 * @param mask the current search mask
2877 * @param cwd the current path
2879 * @return Boolean result
2881 BOOL
sync_files(TALLOC_CTX
*mem_ctx
,
2882 struct cli_state
*cli_share_src
,
2883 struct cli_state
*cli_share_dst
,
2884 pstring mask
, fstring cwd
)
2888 uint16 attribute
= aSYSTEM
| aHIDDEN
| aDIR
;
2889 struct copy_clistate clistate
;
2891 clistate
.mem_ctx
= mem_ctx
;
2892 clistate
.cli_share_src
= cli_share_src
;
2893 clistate
.cli_share_dst
= cli_share_dst
;
2896 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
2898 if (cli_list(cli_share_src
, mask
, attribute
, copy_fn
, &clistate
) == -1) {
2899 d_printf("listing %s failed with error: %s\n",
2900 mask
, cli_errstr(cli_share_src
));
2909 * Sync all files inside a remote share to another share (over smb)
2911 * All parameters are provided by the run_rpc_command function, except for
2912 * argc, argv which are passes through.
2914 * @param domain_sid The domain sid acquired from the remote server
2915 * @param cli A cli_state connected to the server.
2916 * @param mem_ctx Talloc context, destoyed on completion of the function.
2917 * @param argc Standard main() style argc
2918 * @param argv Standard main() style argv. Initial components are already
2921 * @return Normal NTSTATUS return.
2924 rpc_share_migrate_files_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
2925 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
2926 int argc
, const char **argv
)
2929 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
2930 SRV_SHARE_INFO_CTR ctr_src
;
2932 uint32 preferred_len
= 0xffffffff, i
;
2934 struct cli_state
*cli_share_src
= NULL
;
2935 struct cli_state
*cli_share_dst
= NULL
;
2936 BOOL got_src_share
= False
;
2937 BOOL got_dst_share
= False
;
2941 dst
= SMB_STRDUP(opt_destination
?opt_destination
:"127.0.0.1");
2943 init_enum_hnd(&hnd
, 0);
2945 result
= cli_srvsvc_net_share_enum(
2946 cli
, mem_ctx
, level
, &ctr_src
, preferred_len
, &hnd
);
2948 if (!W_ERROR_IS_OK(result
))
2951 for (i
= 0; i
< ctr_src
.num_entries
; i
++) {
2953 fstring netname
= "", remark
= "", path
= "";
2955 rpcstr_pull_unistr2_fstring(
2956 netname
, &ctr_src
.share
.info2
[i
].info_2_str
.uni_netname
);
2957 rpcstr_pull_unistr2_fstring(
2958 remark
, &ctr_src
.share
.info2
[i
].info_2_str
.uni_remark
);
2959 rpcstr_pull_unistr2_fstring(
2960 path
, &ctr_src
.share
.info2
[i
].info_2_str
.uni_path
);
2962 /* since we do not have NetShareGetInfo implemented in samba3 we
2963 only can skip inside the enum-ctr_src */
2965 char *one_share
= talloc_strdup(mem_ctx
, argv
[0]);
2966 if (!strequal(netname
, one_share
))
2970 /* skip builtin and hidden shares
2971 In particular, one might not want to mirror whole discs :) */
2972 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$"))
2975 if (strequal(netname
, "print$") || netname
[1] == '$') {
2976 d_printf("skipping [%s]: builtin/hidden share\n", netname
);
2980 if (opt_exclude
&& in_list(netname
, opt_exclude
, False
)) {
2981 printf("excluding [%s]\n", netname
);
2985 /* only work with file-shares */
2986 if (!cli_send_tconX(cli
, netname
, "A:", "", 0)) {
2987 d_printf("skipping [%s]: not a file share.\n", netname
);
2992 return NT_STATUS_UNSUCCESSFUL
;
2994 printf("syncing [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
2996 opt_acls
? "including" : "without",
2997 opt_attrs
? "including" : "without",
2998 opt_timestamps
? "(preserving timestamps)" : "");
3001 /* open share source */
3002 nt_status
= connect_to_service(&cli_share_src
, &cli
->dest_ip
,
3003 cli
->desthost
, netname
, "A:");
3004 if (!NT_STATUS_IS_OK(nt_status
))
3007 got_src_share
= True
;
3010 /* open share destination */
3011 nt_status
= connect_to_service(&cli_share_dst
, NULL
,
3012 dst
, netname
, "A:");
3013 if (!NT_STATUS_IS_OK(nt_status
))
3016 got_dst_share
= True
;
3019 /* now call the filesync */
3020 pstrcpy(mask
, "\\*");
3022 if (!sync_files(mem_ctx
, cli_share_src
, cli_share_dst
, mask
, NULL
)) {
3023 d_printf("could not sync files for share: %s\n", netname
);
3024 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3030 nt_status
= NT_STATUS_OK
;
3035 cli_shutdown(cli_share_src
);
3038 cli_shutdown(cli_share_dst
);
3044 static int rpc_share_migrate_files(int argc
, const char **argv
)
3048 printf("no server to migrate\n");
3052 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
3053 rpc_share_migrate_files_internals
,
3058 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3059 * from one server to another
3061 * @param argc Standard main() style argc
3062 * @param argv Standard main() style argv. Initial components are already
3065 * @return A shell status integer (0 for success)
3068 static int rpc_share_migrate_all(int argc
, const char **argv
)
3073 printf("no server to migrate\n");
3077 ret
= run_rpc_command(NULL
, PI_SRVSVC
, 0, rpc_share_migrate_shares_internals
, argc
, argv
);
3081 ret
= run_rpc_command(NULL
, PI_SRVSVC
, 0, rpc_share_migrate_shares_security_internals
, argc
, argv
);
3085 return run_rpc_command(NULL
, PI_SRVSVC
, 0, rpc_share_migrate_files_internals
, argc
, argv
);
3090 * 'net rpc share migrate' entrypoint.
3091 * @param argc Standard main() style argc
3092 * @param argv Standard main() style argv. Initial components are already
3095 static int rpc_share_migrate(int argc
, const char **argv
)
3098 struct functable func
[] = {
3099 {"all", rpc_share_migrate_all
},
3100 {"files", rpc_share_migrate_files
},
3101 {"help", rpc_share_usage
},
3102 /* {"security", rpc_share_migrate_security},*/
3103 {"shares", rpc_share_migrate_shares
},
3107 return net_run_function(argc
, argv
, func
, rpc_share_usage
);
3116 static int num_server_aliases
;
3117 static struct full_alias
*server_aliases
;
3120 * Add an alias to the static list.
3122 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
3124 if (server_aliases
== NULL
)
3125 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
3127 server_aliases
[num_server_aliases
] = *alias
;
3128 num_server_aliases
+= 1;
3132 * For a specific domain on the server, fetch all the aliases
3133 * and their members. Add all of them to the server_aliases.
3136 rpc_fetch_domain_aliases(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
3137 POLICY_HND
*connect_pol
,
3138 const DOM_SID
*domain_sid
)
3140 uint32 start_idx
, max_entries
, num_entries
, i
;
3141 struct acct_info
*groups
;
3143 POLICY_HND domain_pol
;
3145 /* Get domain policy handle */
3147 result
= cli_samr_open_domain(cli
, mem_ctx
, connect_pol
,
3148 MAXIMUM_ALLOWED_ACCESS
,
3149 domain_sid
, &domain_pol
);
3150 if (!NT_STATUS_IS_OK(result
))
3157 result
= cli_samr_enum_als_groups(cli
, mem_ctx
, &domain_pol
,
3158 &start_idx
, max_entries
,
3159 &groups
, &num_entries
);
3161 for (i
= 0; i
< num_entries
; i
++) {
3163 POLICY_HND alias_pol
;
3164 struct full_alias alias
;
3168 result
= cli_samr_open_alias(cli
, mem_ctx
, &domain_pol
,
3169 MAXIMUM_ALLOWED_ACCESS
,
3172 if (!NT_STATUS_IS_OK(result
))
3175 result
= cli_samr_query_aliasmem(cli
, mem_ctx
,
3179 if (!NT_STATUS_IS_OK(result
))
3182 result
= cli_samr_close(cli
, mem_ctx
, &alias_pol
);
3183 if (!NT_STATUS_IS_OK(result
))
3186 alias
.members
= NULL
;
3188 if (alias
.num_members
> 0) {
3189 alias
.members
= SMB_MALLOC_ARRAY(DOM_SID
, alias
.num_members
);
3191 for (j
= 0; j
< alias
.num_members
; j
++)
3192 sid_copy(&alias
.members
[j
],
3196 sid_copy(&alias
.sid
, domain_sid
);
3197 sid_append_rid(&alias
.sid
, groups
[i
].rid
);
3199 push_alias(mem_ctx
, &alias
);
3201 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
3203 result
= NT_STATUS_OK
;
3206 cli_samr_close(cli
, mem_ctx
, &domain_pol
);
3212 * Dump server_aliases as names for debugging purposes.
3215 rpc_aliaslist_dump(const DOM_SID
*domain_sid
, const char *domain_name
,
3216 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
3217 int argc
, const char **argv
)
3223 result
= cli_lsa_open_policy(cli
, mem_ctx
, True
,
3224 SEC_RIGHTS_MAXIMUM_ALLOWED
,
3226 if (!NT_STATUS_IS_OK(result
))
3229 for (i
=0; i
<num_server_aliases
; i
++) {
3235 struct full_alias
*alias
= &server_aliases
[i
];
3237 result
= cli_lsa_lookup_sids(cli
, mem_ctx
, &lsa_pol
, 1,
3239 &domains
, &names
, &types
);
3240 if (!NT_STATUS_IS_OK(result
))
3243 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
3245 if (alias
->num_members
== 0) {
3250 result
= cli_lsa_lookup_sids(cli
, mem_ctx
, &lsa_pol
,
3253 &domains
, &names
, &types
);
3255 if (!NT_STATUS_IS_OK(result
) &&
3256 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
3259 for (j
=0; j
<alias
->num_members
; j
++)
3260 DEBUG(1, ("%s\\%s (%d); ",
3261 domains
[j
] ? domains
[j
] : "*unknown*",
3262 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
3266 cli_lsa_close(cli
, mem_ctx
, &lsa_pol
);
3268 return NT_STATUS_OK
;
3272 * Fetch a list of all server aliases and their members into
3276 rpc_aliaslist_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
3277 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
3278 int argc
, const char **argv
)
3281 POLICY_HND connect_pol
;
3283 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
3286 if (!NT_STATUS_IS_OK(result
))
3289 result
= rpc_fetch_domain_aliases(cli
, mem_ctx
, &connect_pol
,
3290 &global_sid_Builtin
);
3292 if (!NT_STATUS_IS_OK(result
))
3295 result
= rpc_fetch_domain_aliases(cli
, mem_ctx
, &connect_pol
,
3298 cli_samr_close(cli
, mem_ctx
, &connect_pol
);
3303 static void init_user_token(NT_USER_TOKEN
*token
, DOM_SID
*user_sid
)
3305 token
->num_sids
= 4;
3307 token
->user_sids
= SMB_MALLOC_ARRAY(DOM_SID
, 4);
3309 token
->user_sids
[0] = *user_sid
;
3310 sid_copy(&token
->user_sids
[1], &global_sid_World
);
3311 sid_copy(&token
->user_sids
[2], &global_sid_Network
);
3312 sid_copy(&token
->user_sids
[3], &global_sid_Authenticated_Users
);
3315 static void free_user_token(NT_USER_TOKEN
*token
)
3317 SAFE_FREE(token
->user_sids
);
3320 static BOOL
is_sid_in_token(NT_USER_TOKEN
*token
, DOM_SID
*sid
)
3324 for (i
=0; i
<token
->num_sids
; i
++) {
3325 if (sid_compare(sid
, &token
->user_sids
[i
]) == 0)
3331 static void add_sid_to_token(NT_USER_TOKEN
*token
, DOM_SID
*sid
)
3333 if (is_sid_in_token(token
, sid
))
3336 token
->user_sids
= SMB_REALLOC_ARRAY(token
->user_sids
, DOM_SID
, token
->num_sids
+1);
3338 sid_copy(&token
->user_sids
[token
->num_sids
], sid
);
3340 token
->num_sids
+= 1;
3345 NT_USER_TOKEN token
;
3348 static void dump_user_token(struct user_token
*token
)
3352 d_printf("%s\n", token
->name
);
3354 for (i
=0; i
<token
->token
.num_sids
; i
++) {
3355 d_printf(" %s\n", sid_string_static(&token
->token
.user_sids
[i
]));
3359 static BOOL
is_alias_member(DOM_SID
*sid
, struct full_alias
*alias
)
3363 for (i
=0; i
<alias
->num_members
; i
++) {
3364 if (sid_compare(sid
, &alias
->members
[i
]) == 0)
3371 static void collect_sid_memberships(NT_USER_TOKEN
*token
, DOM_SID sid
)
3375 for (i
=0; i
<num_server_aliases
; i
++) {
3376 if (is_alias_member(&sid
, &server_aliases
[i
]))
3377 add_sid_to_token(token
, &server_aliases
[i
].sid
);
3382 * We got a user token with all the SIDs we can know about without asking the
3383 * server directly. These are the user and domain group sids. All of these can
3384 * be members of aliases. So scan the list of aliases for each of the SIDs and
3385 * add them to the token.
3388 static void collect_alias_memberships(NT_USER_TOKEN
*token
)
3390 int num_global_sids
= token
->num_sids
;
3393 for (i
=0; i
<num_global_sids
; i
++) {
3394 collect_sid_memberships(token
, token
->user_sids
[i
]);
3398 static BOOL
get_user_sids(const char *domain
, const char *user
,
3399 NT_USER_TOKEN
*token
)
3401 struct winbindd_request request
;
3402 struct winbindd_response response
;
3410 fstr_sprintf(full_name
, "%s%c%s",
3411 domain
, *lp_winbind_separator(), user
);
3413 /* First let's find out the user sid */
3415 ZERO_STRUCT(request
);
3416 ZERO_STRUCT(response
);
3418 fstrcpy(request
.data
.name
.dom_name
, domain
);
3419 fstrcpy(request
.data
.name
.name
, user
);
3421 result
= winbindd_request(WINBINDD_LOOKUPNAME
, &request
, &response
);
3423 if (result
!= NSS_STATUS_SUCCESS
) {
3424 DEBUG(1, ("winbind could not find %s\n", full_name
));
3428 if (response
.data
.sid
.type
!= SID_NAME_USER
) {
3429 DEBUG(1, ("%s is not a user\n", full_name
));
3433 string_to_sid(&user_sid
, response
.data
.sid
.sid
);
3435 init_user_token(token
, &user_sid
);
3437 /* And now the groups winbind knows about */
3439 ZERO_STRUCT(response
);
3441 fstrcpy(request
.data
.username
, full_name
);
3443 result
= winbindd_request(WINBINDD_GETGROUPS
, &request
, &response
);
3445 if (result
!= NSS_STATUS_SUCCESS
) {
3446 DEBUG(1, ("winbind could not get groups of %s\n", full_name
));
3450 for (i
= 0; i
< response
.data
.num_entries
; i
++) {
3451 gid_t gid
= ((gid_t
*)response
.extra_data
)[i
];
3454 struct winbindd_request sidrequest
;
3455 struct winbindd_response sidresponse
;
3457 ZERO_STRUCT(sidrequest
);
3458 ZERO_STRUCT(sidresponse
);
3460 sidrequest
.data
.gid
= gid
;
3462 result
= winbindd_request(WINBINDD_GID_TO_SID
,
3463 &sidrequest
, &sidresponse
);
3465 if (result
!= NSS_STATUS_SUCCESS
) {
3466 DEBUG(1, ("winbind could not find SID of gid %d\n",
3471 DEBUG(3, (" %s\n", sidresponse
.data
.sid
.sid
));
3473 string_to_sid(&sid
, sidresponse
.data
.sid
.sid
);
3474 add_sid_to_token(token
, &sid
);
3477 SAFE_FREE(response
.extra_data
);
3483 * Get a list of all user tokens we want to look at
3485 static BOOL
get_user_tokens(int *num_tokens
, struct user_token
**user_tokens
)
3487 struct winbindd_request request
;
3488 struct winbindd_response response
;
3489 const char *extra_data
;
3492 struct user_token
*result
;
3494 if (lp_winbind_use_default_domain() &&
3495 (opt_target_workgroup
== NULL
)) {
3496 d_printf("winbind use default domain = yes set, please "
3497 "specify a workgroup\n");
3501 /* Send request to winbind daemon */
3503 ZERO_STRUCT(request
);
3504 ZERO_STRUCT(response
);
3506 if (winbindd_request(WINBINDD_LIST_USERS
, &request
, &response
) !=
3510 /* Look through extra data */
3512 if (!response
.extra_data
)
3515 extra_data
= (const char *)response
.extra_data
;
3518 while(next_token(&extra_data
, name
, ",", sizeof(fstring
))) {
3522 result
= SMB_MALLOC_ARRAY(struct user_token
, *num_tokens
);
3524 if (result
== NULL
) {
3525 DEBUG(1, ("Could not malloc sid array\n"));
3529 extra_data
= (const char *)response
.extra_data
;
3532 while(next_token(&extra_data
, name
, ",", sizeof(fstring
))) {
3534 fstring domain
, user
;
3537 fstrcpy(result
[i
].name
, name
);
3539 p
= strchr(name
, *lp_winbind_separator());
3541 DEBUG(3, ("%s\n", name
));
3544 fstrcpy(domain
, opt_target_workgroup
);
3545 fstrcpy(user
, name
);
3548 fstrcpy(domain
, name
);
3553 get_user_sids(domain
, user
, &(result
[i
].token
));
3557 SAFE_FREE(response
.extra_data
);
3559 *user_tokens
= result
;
3564 static BOOL
get_user_tokens_from_file(FILE *f
,
3566 struct user_token
**tokens
)
3568 struct user_token
*token
= NULL
;
3573 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
3577 if (line
[strlen(line
)-1] == '\n')
3578 line
[strlen(line
)-1] = '\0';
3580 if (line
[0] == ' ') {
3584 string_to_sid(&sid
, &line
[1]);
3586 if (token
== NULL
) {
3587 DEBUG(0, ("File does not begin with username"));
3591 add_sid_to_token(&token
->token
, &sid
);
3595 /* And a new user... */
3598 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
3599 if (*tokens
== NULL
) {
3600 DEBUG(0, ("Could not realloc tokens\n"));
3604 token
= &((*tokens
)[*num_tokens
-1]);
3606 fstrcpy(token
->name
, line
);
3607 token
->token
.num_sids
= 0;
3608 token
->token
.user_sids
= NULL
;
3617 * Show the list of all users that have access to a share
3620 static void show_userlist(struct cli_state
*cli
,
3621 TALLOC_CTX
*mem_ctx
, const char *netname
,
3622 int num_tokens
, struct user_token
*tokens
)
3625 SEC_DESC
*share_sd
= NULL
;
3626 SEC_DESC
*root_sd
= NULL
;
3628 SRV_SHARE_INFO info
;
3632 result
= cli_srvsvc_net_share_get_info(cli
, mem_ctx
, netname
,
3635 if (!W_ERROR_IS_OK(result
)) {
3636 DEBUG(1, ("Coult not query secdesc for share %s\n",
3641 share_sd
= info
.share
.info502
.info_502_str
.sd
;
3642 if (share_sd
== NULL
) {
3643 DEBUG(1, ("Got no secdesc for share %s\n",
3649 if (!cli_send_tconX(cli
, netname
, "A:", "", 0)) {
3653 fnum
= cli_nt_create(cli
, "\\", READ_CONTROL_ACCESS
);
3656 root_sd
= cli_query_secdesc(cli
, fnum
, mem_ctx
);
3659 for (i
=0; i
<num_tokens
; i
++) {
3663 if (share_sd
!= NULL
) {
3664 if (!se_access_check(share_sd
, &tokens
[i
].token
,
3665 1, &acc_granted
, &status
)) {
3666 DEBUG(1, ("Could not check share_sd for "
3672 if (!NT_STATUS_IS_OK(status
))
3676 if (root_sd
== NULL
) {
3677 d_printf(" %s\n", tokens
[i
].name
);
3681 if (!se_access_check(root_sd
, &tokens
[i
].token
,
3682 1, &acc_granted
, &status
)) {
3683 DEBUG(1, ("Could not check root_sd for user %s\n",
3688 if (!NT_STATUS_IS_OK(status
))
3691 d_printf(" %s\n", tokens
[i
].name
);
3695 cli_close(cli
, fnum
);
3707 static void collect_share(const char *name
, uint32 m
,
3708 const char *comment
, void *state
)
3710 struct share_list
*share_list
= (struct share_list
*)state
;
3712 if (m
!= STYPE_DISKTREE
)
3715 share_list
->num_shares
+= 1;
3716 share_list
->shares
= SMB_REALLOC_ARRAY(share_list
->shares
, char *, share_list
->num_shares
);
3717 share_list
->shares
[share_list
->num_shares
-1] = SMB_STRDUP(name
);
3720 static void rpc_share_userlist_usage(void)
3726 * List shares on a remote RPC server, including the security descriptors
3728 * All parameters are provided by the run_rpc_command function, except for
3729 * argc, argv which are passes through.
3731 * @param domain_sid The domain sid acquired from the remote server
3732 * @param cli A cli_state connected to the server.
3733 * @param mem_ctx Talloc context, destoyed on completion of the function.
3734 * @param argc Standard main() style argc
3735 * @param argv Standard main() style argv. Initial components are already
3738 * @return Normal NTSTATUS return.
3742 rpc_share_allowedusers_internals(const DOM_SID
*domain_sid
,
3743 const char *domain_name
,
3744 struct cli_state
*cli
,
3745 TALLOC_CTX
*mem_ctx
,
3746 int argc
, const char **argv
)
3754 struct user_token
*tokens
= NULL
;
3757 struct share_list share_list
;
3760 rpc_share_userlist_usage();
3761 return NT_STATUS_UNSUCCESSFUL
;
3767 f
= fopen(argv
[0], "r");
3771 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
3772 return NT_STATUS_UNSUCCESSFUL
;
3775 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
3781 DEBUG(0, ("Could not read users from file\n"));
3782 return NT_STATUS_UNSUCCESSFUL
;
3785 for (i
=0; i
<num_tokens
; i
++)
3786 collect_alias_memberships(&tokens
[i
].token
);
3788 init_enum_hnd(&hnd
, 0);
3790 share_list
.num_shares
= 0;
3791 share_list
.shares
= NULL
;
3793 ret
= cli_RNetShareEnum(cli
, collect_share
, &share_list
);
3796 DEBUG(0, ("Error returning browse list: %s\n",
3801 for (i
= 0; i
< share_list
.num_shares
; i
++) {
3802 char *netname
= share_list
.shares
[i
];
3804 if (netname
[strlen(netname
)-1] == '$')
3807 d_printf("%s\n", netname
);
3809 show_userlist(cli
, mem_ctx
, netname
,
3810 num_tokens
, tokens
);
3813 for (i
=0; i
<num_tokens
; i
++) {
3814 free_user_token(&tokens
[i
].token
);
3817 SAFE_FREE(share_list
.shares
);
3819 return NT_STATUS_OK
;
3823 rpc_share_allowedusers(int argc
, const char **argv
)
3827 result
= run_rpc_command(NULL
, PI_SAMR
, 0,
3828 rpc_aliaslist_internals
,
3833 result
= run_rpc_command(NULL
, PI_LSARPC
, 0,
3839 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
3840 rpc_share_allowedusers_internals
,
3844 int net_usersidlist(int argc
, const char **argv
)
3847 struct user_token
*tokens
= NULL
;
3851 net_usersidlist_usage(argc
, argv
);
3855 if (!get_user_tokens(&num_tokens
, &tokens
)) {
3856 DEBUG(0, ("Could not get the user/sid list\n"));
3860 for (i
=0; i
<num_tokens
; i
++) {
3861 dump_user_token(&tokens
[i
]);
3862 free_user_token(&tokens
[i
].token
);
3869 int net_usersidlist_usage(int argc
, const char **argv
)
3871 d_printf("net usersidlist\n"
3872 "\tprints out a list of all users the running winbind knows\n"
3873 "\tabout, together with all their SIDs. This is used as\n"
3874 "\tinput to the 'net rpc share allowedusers' command.\n\n");
3876 net_common_flags_usage(argc
, argv
);
3881 * 'net rpc share' entrypoint.
3882 * @param argc Standard main() style argc
3883 * @param argv Standard main() style argv. Initial components are already
3887 int net_rpc_share(int argc
, const char **argv
)
3889 struct functable func
[] = {
3890 {"add", rpc_share_add
},
3891 {"delete", rpc_share_delete
},
3892 {"allowedusers", rpc_share_allowedusers
},
3893 {"migrate", rpc_share_migrate
},
3898 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
3899 rpc_share_list_internals
,
3902 return net_run_function(argc
, argv
, func
, rpc_share_usage
);
3905 /****************************************************************************/
3907 static int rpc_file_usage(int argc
, const char **argv
)
3909 return net_help_file(argc
, argv
);
3913 * Close a file on a remote RPC server
3915 * All parameters are provided by the run_rpc_command function, except for
3916 * argc, argv which are passes through.
3918 * @param domain_sid The domain sid acquired from the remote server
3919 * @param cli A cli_state connected to the server.
3920 * @param mem_ctx Talloc context, destoyed on completion of the function.
3921 * @param argc Standard main() style argc
3922 * @param argv Standard main() style argv. Initial components are already
3925 * @return Normal NTSTATUS return.
3928 rpc_file_close_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
3929 struct cli_state
*cli
,
3930 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
3933 result
= cli_srvsvc_net_file_close(cli
, mem_ctx
, atoi(argv
[0]));
3934 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3938 * Close a file on a remote RPC server
3940 * @param argc Standard main() style argc
3941 * @param argv Standard main() style argv. Initial components are already
3944 * @return A shell status integer (0 for success)
3946 static int rpc_file_close(int argc
, const char **argv
)
3949 DEBUG(1, ("No fileid given on close\n"));
3950 return(rpc_file_usage(argc
, argv
));
3953 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
3954 rpc_file_close_internals
,
3959 * Formatted print of open file info
3961 * @param info3 FILE_INFO_3 contents
3962 * @param str3 strings for FILE_INFO_3
3965 static void display_file_info_3(FILE_INFO_3
*info3
, FILE_INFO_3_STR
*str3
)
3967 fstring user
= "", path
= "";
3969 rpcstr_pull_unistr2_fstring(user
, &str3
->uni_user_name
);
3970 rpcstr_pull_unistr2_fstring(path
, &str3
->uni_path_name
);
3972 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
3973 info3
->id
, user
, info3
->perms
, info3
->num_locks
, path
);
3977 * List open files on a remote RPC server
3979 * All parameters are provided by the run_rpc_command function, except for
3980 * argc, argv which are passes through.
3982 * @param domain_sid The domain sid acquired from the remote server
3983 * @param cli A cli_state connected to the server.
3984 * @param mem_ctx Talloc context, destoyed on completion of the function.
3985 * @param argc Standard main() style argc
3986 * @param argv Standard main() style argv. Initial components are already
3989 * @return Normal NTSTATUS return.
3993 rpc_file_list_internals(const DOM_SID
*domain_sid
, const char *domain_name
,
3994 struct cli_state
*cli
,
3995 TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
3997 SRV_FILE_INFO_CTR ctr
;
4000 uint32 preferred_len
= 0xffffffff, i
;
4001 const char *username
=NULL
;
4003 init_enum_hnd(&hnd
, 0);
4005 /* if argc > 0, must be user command */
4007 username
= smb_xstrdup(argv
[0]);
4009 result
= cli_srvsvc_net_file_enum(
4010 cli
, mem_ctx
, 3, username
, &ctr
, preferred_len
, &hnd
);
4012 if (!W_ERROR_IS_OK(result
))
4015 /* Display results */
4018 "\nEnumerating open files on remote server:\n\n"\
4019 "\nFileId Opened by Perms Locks Path"\
4020 "\n------ --------- ----- ----- ---- \n");
4021 for (i
= 0; i
< ctr
.num_entries
; i
++)
4022 display_file_info_3(&ctr
.file
.info3
[i
].info_3
,
4023 &ctr
.file
.info3
[i
].info_3_str
);
4025 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
4030 * List files for a user on a remote RPC server
4032 * @param argc Standard main() style argc
4033 * @param argv Standard main() style argv. Initial components are already
4036 * @return A shell status integer (0 for success)
4038 static int rpc_file_user(int argc
, const char **argv
)
4041 DEBUG(1, ("No username given\n"));
4042 return(rpc_file_usage(argc
, argv
));
4045 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
4046 rpc_file_list_internals
,
4052 * 'net rpc file' entrypoint.
4053 * @param argc Standard main() style argc
4054 * @param argv Standard main() style argv. Initial components are already
4058 int net_rpc_file(int argc
, const char **argv
)
4060 struct functable func
[] = {
4061 {"close", rpc_file_close
},
4062 {"user", rpc_file_user
},
4064 {"info", rpc_file_info
},
4070 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
4071 rpc_file_list_internals
,
4074 return net_run_function(argc
, argv
, func
, rpc_file_usage
);
4077 /****************************************************************************/
4082 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
4084 * All parameters are provided by the run_rpc_command function, except for
4085 * argc, argv which are passed through.
4087 * @param domain_sid The domain sid aquired from the remote server
4088 * @param cli A cli_state connected to the server.
4089 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4090 * @param argc Standard main() style argc
4091 * @param argv Standard main() style argv. Initial components are already
4094 * @return Normal NTSTATUS return.
4097 static NTSTATUS
rpc_shutdown_abort_internals(const DOM_SID
*domain_sid
,
4098 const char *domain_name
,
4099 struct cli_state
*cli
,
4100 TALLOC_CTX
*mem_ctx
,
4101 int argc
, const char **argv
)
4103 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
4105 result
= cli_shutdown_abort(cli
, mem_ctx
);
4107 if (NT_STATUS_IS_OK(result
)) {
4108 d_printf("\nShutdown successfully aborted\n");
4109 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4111 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4118 * ABORT the shutdown of a remote RPC Server, over winreg pipe
4120 * All parameters are provided by the run_rpc_command function, except for
4121 * argc, argv which are passed through.
4123 * @param domain_sid The domain sid aquired from the remote server
4124 * @param cli A cli_state connected to the server.
4125 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4126 * @param argc Standard main() style argc
4127 * @param argv Standard main() style argv. Initial components are already
4130 * @return Normal NTSTATUS return.
4133 static NTSTATUS
rpc_reg_shutdown_abort_internals(const DOM_SID
*domain_sid
,
4134 const char *domain_name
,
4135 struct cli_state
*cli
,
4136 TALLOC_CTX
*mem_ctx
,
4137 int argc
, const char **argv
)
4139 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
4141 result
= werror_to_ntstatus(cli_reg_abort_shutdown(cli
, mem_ctx
));
4143 if (NT_STATUS_IS_OK(result
)) {
4144 d_printf("\nShutdown successfully aborted\n");
4145 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
4147 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
4153 * ABORT the Shut down of a remote RPC server
4155 * @param argc Standard main() style argc
4156 * @param argv Standard main() style argv. Initial components are already
4159 * @return A shell status integer (0 for success)
4162 static int rpc_shutdown_abort(int argc
, const char **argv
)
4164 int rc
= run_rpc_command(NULL
, PI_SHUTDOWN
, 0,
4165 rpc_shutdown_abort_internals
,
4171 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4173 return run_rpc_command(NULL
, PI_WINREG
, 0,
4174 rpc_reg_shutdown_abort_internals
,
4179 * Shut down a remote RPC Server via initshutdown pipe
4181 * All parameters are provided by the run_rpc_command function, except for
4182 * argc, argv which are passes through.
4184 * @param domain_sid The domain sid aquired from the remote server
4185 * @param cli A cli_state connected to the server.
4186 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4187 * @param argc Standard main() style argc
4188 * @param argc Standard main() style argv. Initial components are already
4191 * @return Normal NTSTATUS return.
4194 static NTSTATUS
rpc_init_shutdown_internals(const DOM_SID
*domain_sid
,
4195 const char *domain_name
,
4196 struct cli_state
*cli
,
4197 TALLOC_CTX
*mem_ctx
,
4198 int argc
, const char **argv
)
4200 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
4201 const char *msg
= "This machine will be shutdown shortly";
4202 uint32 timeout
= 20;
4208 timeout
= opt_timeout
;
4211 /* create an entry */
4212 result
= cli_shutdown_init(cli
, mem_ctx
, msg
, timeout
, opt_reboot
,
4215 if (NT_STATUS_IS_OK(result
)) {
4216 d_printf("\nShutdown of remote machine succeeded\n");
4217 DEBUG(5,("Shutdown of remote machine succeeded\n"));
4219 DEBUG(0,("Shutdown of remote machine failed!\n"));
4225 * Shut down a remote RPC Server via winreg pipe
4227 * All parameters are provided by the run_rpc_command function, except for
4228 * argc, argv which are passes through.
4230 * @param domain_sid The domain sid aquired from the remote server
4231 * @param cli A cli_state connected to the server.
4232 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4233 * @param argc Standard main() style argc
4234 * @param argc Standard main() style argv. Initial components are already
4237 * @return Normal NTSTATUS return.
4240 static NTSTATUS
rpc_reg_shutdown_internals(const DOM_SID
*domain_sid
,
4241 const char *domain_name
,
4242 struct cli_state
*cli
,
4243 TALLOC_CTX
*mem_ctx
,
4244 int argc
, const char **argv
)
4246 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
4247 const char *msg
= "This machine will be shutdown shortly";
4248 uint32 timeout
= 20;
4253 struct poptOption long_options
[] = {
4254 {"message", 'm', POPT_ARG_STRING
, &msg
},
4255 {"timeout", 't', POPT_ARG_INT
, &timeout
},
4256 {"reboot", 'r', POPT_ARG_NONE
, &reboot
},
4257 {"force", 'f', POPT_ARG_NONE
, &force
},
4261 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
,
4262 POPT_CONTEXT_KEEP_FIRST
);
4264 rc
= poptGetNextOpt(pc
);
4267 /* an error occurred during option processing */
4268 DEBUG(0, ("%s: %s\n",
4269 poptBadOption(pc
, POPT_BADOPTION_NOALIAS
),
4271 return NT_STATUS_INVALID_PARAMETER
;
4278 timeout
= opt_timeout
;
4281 /* create an entry */
4282 result
= werror_to_ntstatus(cli_reg_shutdown(cli
, mem_ctx
, msg
, timeout
, opt_reboot
, opt_force
));
4284 if (NT_STATUS_IS_OK(result
)) {
4285 d_printf("\nShutdown of remote machine succeeded\n");
4286 DEBUG(5,("Shutdown of remote machine succeeded\n"));
4289 DEBUG(0,("Shutdown of remote machine failed!\n"));
4295 * Shut down a remote RPC server
4297 * @param argc Standard main() style argc
4298 * @param argc Standard main() style argv. Initial components are already
4301 * @return A shell status integer (0 for success)
4304 static int rpc_shutdown(int argc
, const char **argv
)
4306 int rc
= run_rpc_command(NULL
, PI_SHUTDOWN
, 0,
4307 rpc_init_shutdown_internals
,
4312 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4314 return run_rpc_command(NULL
, PI_WINREG
, 0, rpc_reg_shutdown_internals
,
4318 /***************************************************************************
4319 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
4321 ***************************************************************************/
4324 * Add interdomain trust account to the RPC server.
4325 * All parameters (except for argc and argv) are passed by run_rpc_command
4328 * @param domain_sid The domain sid acquired from the server
4329 * @param cli A cli_state connected to the server.
4330 * @param mem_ctx Talloc context, destoyed on completion of the function.
4331 * @param argc Standard main() style argc
4332 * @param argc Standard main() style argv. Initial components are already
4335 * @return normal NTSTATUS return code
4338 static NTSTATUS
rpc_trustdom_add_internals(const DOM_SID
*domain_sid
,
4339 const char *domain_name
,
4340 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
4341 int argc
, const char **argv
) {
4343 POLICY_HND connect_pol
, domain_pol
, user_pol
;
4344 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
4347 uint32 unknown
, user_rid
;
4350 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
4351 return NT_STATUS_INVALID_PARAMETER
;
4355 * Make valid trusting domain account (ie. uppercased and with '$' appended)
4358 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
4359 return NT_STATUS_NO_MEMORY
;
4362 strupper_m(acct_name
);
4364 /* Get samr policy handle */
4365 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
4367 if (!NT_STATUS_IS_OK(result
)) {
4371 /* Get domain policy handle */
4372 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
4373 MAXIMUM_ALLOWED_ACCESS
,
4374 domain_sid
, &domain_pol
);
4375 if (!NT_STATUS_IS_OK(result
)) {
4379 /* Create trusting domain's account */
4380 acb_info
= ACB_NORMAL
;
4381 unknown
= 0xe00500b0; /* No idea what this is - a permission mask?
4382 mimir: yes, most probably it is */
4384 result
= cli_samr_create_dom_user(cli
, mem_ctx
, &domain_pol
,
4385 acct_name
, acb_info
, unknown
,
4386 &user_pol
, &user_rid
);
4387 if (!NT_STATUS_IS_OK(result
)) {
4392 SAM_USERINFO_CTR ctr
;
4393 SAM_USER_INFO_23 p23
;
4399 encode_pw_buffer((char *)pwbuf
, argv
[1], STR_UNICODE
);
4403 ZERO_STRUCT(notime
);
4407 memset(hrs
.hours
, 0xFF, sizeof(hrs
.hours
));
4408 acb_info
= ACB_DOMTRUST
;
4410 init_sam_user_info23A(&p23
, ¬ime
, ¬ime
, ¬ime
,
4411 ¬ime
, ¬ime
, ¬ime
,
4412 nostr
, nostr
, nostr
, nostr
, nostr
,
4413 nostr
, nostr
, nostr
, nostr
, nostr
,
4414 0, 0, acb_info
, ACCT_FLAGS
, 168, &hrs
,
4415 0, 0, (char *)pwbuf
);
4416 ctr
.switch_value
= 23;
4417 ctr
.info
.id23
= &p23
;
4418 p23
.passmustchange
= 0;
4420 result
= cli_samr_set_userinfo(cli
, mem_ctx
, &user_pol
, 23,
4421 &cli
->user_session_key
, &ctr
);
4423 if (!NT_STATUS_IS_OK(result
)) {
4424 DEBUG(0,("Could not set trust account password: %s\n",
4425 nt_errstr(result
)));
4431 SAFE_FREE(acct_name
);
4436 * Create interdomain trust account for a remote domain.
4438 * @param argc standard argc
4439 * @param argv standard argv without initial components
4441 * @return Integer status (0 means success)
4444 static int rpc_trustdom_add(int argc
, const char **argv
)
4447 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_trustdom_add_internals
,
4450 d_printf("Usage: net rpc trustdom add <domain>\n");
4457 * Remove interdomain trust account from the RPC server.
4458 * All parameters (except for argc and argv) are passed by run_rpc_command
4461 * @param domain_sid The domain sid acquired from the server
4462 * @param cli A cli_state connected to the server.
4463 * @param mem_ctx Talloc context, destoyed on completion of the function.
4464 * @param argc Standard main() style argc
4465 * @param argc Standard main() style argv. Initial components are already
4468 * @return normal NTSTATUS return code
4471 static NTSTATUS
rpc_trustdom_del_internals(const DOM_SID
*domain_sid
,
4472 const char *domain_name
,
4473 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
4474 int argc
, const char **argv
) {
4476 POLICY_HND connect_pol
, domain_pol
, user_pol
;
4477 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
4480 DOM_SID trust_acct_sid
;
4481 uint32
*user_rids
, num_rids
, *name_types
;
4482 uint32 flags
= 0x000003e8; /* Unknown */
4485 d_printf("Usage: net rpc trustdom del <domain_name>\n");
4486 return NT_STATUS_INVALID_PARAMETER
;
4490 * Make valid trusting domain account (ie. uppercased and with '$' appended)
4492 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
4494 if (acct_name
== NULL
)
4495 return NT_STATUS_NO_MEMORY
;
4497 strupper_m(acct_name
);
4499 names
= TALLOC_ARRAY(mem_ctx
, const char *, 1);
4500 names
[0] = acct_name
;
4503 /* Get samr policy handle */
4504 result
= cli_samr_connect(cli
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
4506 if (!NT_STATUS_IS_OK(result
)) {
4510 /* Get domain policy handle */
4511 result
= cli_samr_open_domain(cli
, mem_ctx
, &connect_pol
,
4512 MAXIMUM_ALLOWED_ACCESS
,
4513 domain_sid
, &domain_pol
);
4514 if (!NT_STATUS_IS_OK(result
)) {
4518 result
= cli_samr_lookup_names(cli
, mem_ctx
, &domain_pol
, flags
, 1,
4520 &user_rids
, &name_types
);
4522 if (!NT_STATUS_IS_OK(result
)) {
4526 result
= cli_samr_open_user(cli
, mem_ctx
, &domain_pol
,
4527 MAXIMUM_ALLOWED_ACCESS
,
4528 user_rids
[0], &user_pol
);
4530 if (!NT_STATUS_IS_OK(result
)) {
4534 /* append the rid to the domain sid */
4535 sid_copy(&trust_acct_sid
, domain_sid
);
4536 if (!sid_append_rid(&trust_acct_sid
, user_rids
[0])) {
4540 /* remove the sid */
4542 result
= cli_samr_remove_sid_foreign_domain(cli
, mem_ctx
, &user_pol
,
4545 if (!NT_STATUS_IS_OK(result
)) {
4551 result
= cli_samr_delete_dom_user(cli
, mem_ctx
, &user_pol
);
4553 if (!NT_STATUS_IS_OK(result
)) {
4557 if (!NT_STATUS_IS_OK(result
)) {
4558 DEBUG(0,("Could not set trust account password: %s\n",
4559 nt_errstr(result
)));
4568 * Delete interdomain trust account for a remote domain.
4570 * @param argc standard argc
4571 * @param argv standard argv without initial components
4573 * @return Integer status (0 means success)
4576 static int rpc_trustdom_del(int argc
, const char **argv
)
4579 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_trustdom_del_internals
,
4582 d_printf("Usage: net rpc trustdom del <domain>\n");
4589 * Establish trust relationship to a trusting domain.
4590 * Interdomain account must already be created on remote PDC.
4592 * @param argc standard argc
4593 * @param argv standard argv without initial components
4595 * @return Integer status (0 means success)
4598 static int rpc_trustdom_establish(int argc
, const char **argv
)
4600 struct cli_state
*cli
;
4601 struct in_addr server_ip
;
4602 POLICY_HND connect_hnd
;
4603 TALLOC_CTX
*mem_ctx
;
4605 DOM_SID
*domain_sid
;
4606 smb_ucs2_t
*uni_domain_name
;
4609 char* domain_name_pol
;
4614 * Connect to \\server\ipc$ as 'our domain' account with password
4618 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
4622 domain_name
= smb_xstrdup(argv
[0]);
4623 strupper_m(domain_name
);
4625 /* account name used at first is our domain's name with '$' */
4626 asprintf(&acct_name
, "%s$", lp_workgroup());
4627 strupper_m(acct_name
);
4630 * opt_workgroup will be used by connection functions further,
4631 * hence it should be set to remote domain name instead of ours
4633 if (opt_workgroup
) {
4634 opt_workgroup
= smb_xstrdup(domain_name
);
4637 opt_user_name
= acct_name
;
4639 /* find the domain controller */
4640 if (!net_find_pdc(&server_ip
, pdc_name
, domain_name
)) {
4641 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
4645 /* connect to ipc$ as username/password */
4646 nt_status
= connect_to_ipc(&cli
, &server_ip
, pdc_name
);
4647 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
4649 /* Is it trusting domain account for sure ? */
4650 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
4651 nt_errstr(nt_status
)));
4656 * Connect to \\server\ipc$ again (this time anonymously)
4659 nt_status
= connect_to_ipc_anonymous(&cli
, &server_ip
, (char*)pdc_name
);
4661 if (NT_STATUS_IS_ERR(nt_status
)) {
4662 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
4663 domain_name
, nt_errstr(nt_status
)));
4667 * Use NetServerEnum2 to make sure we're talking to a proper server
4670 if (!cli_get_pdc_name(cli
, domain_name
, (char*)pdc_name
)) {
4671 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
4672 for domain %s\n", domain_name
));
4675 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
4676 "domain %s", domain_name
))) {
4677 DEBUG(0, ("talloc_init() failed\n"));
4683 * Call LsaOpenPolicy and LsaQueryInfo
4686 if (!cli_nt_session_open(cli
, PI_LSARPC
)) {
4687 DEBUG(0, ("Could not initialise lsa pipe\n"));
4692 nt_status
= cli_lsa_open_policy2(cli
, mem_ctx
, True
, SEC_RIGHTS_QUERY_VALUE
,
4694 if (NT_STATUS_IS_ERR(nt_status
)) {
4695 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
4696 nt_errstr(nt_status
)));
4700 /* Querying info level 5 */
4702 nt_status
= cli_lsa_query_info_policy(cli
, mem_ctx
, &connect_hnd
,
4704 &domain_name_pol
, &domain_sid
);
4705 if (NT_STATUS_IS_ERR(nt_status
)) {
4706 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
4707 nt_errstr(nt_status
)));
4711 if (push_ucs2_talloc(mem_ctx
, &uni_domain_name
, domain_name_pol
) < 0) {
4712 DEBUG(0, ("Could not convert domain name %s to unicode\n",
4717 /* There should be actually query info level 3 (following nt serv behaviour),
4718 but I still don't know if it's _really_ necessary */
4721 * Store the password in secrets db
4724 if (!secrets_store_trusted_domain_password(domain_name
,
4726 strlen_w(uni_domain_name
)+1,
4729 DEBUG(0, ("Storing password for trusted domain failed.\n"));
4734 * Close the pipes and clean up
4737 nt_status
= cli_lsa_close(cli
, mem_ctx
, &connect_hnd
);
4738 if (NT_STATUS_IS_ERR(nt_status
)) {
4739 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
4740 nt_errstr(nt_status
)));
4744 if (cli
->pipes
[cli
->pipe_idx
].fnum
)
4745 cli_nt_session_close(cli
);
4749 talloc_destroy(mem_ctx
);
4751 d_printf("Trust to domain %s established\n", domain_name
);
4756 * Revoke trust relationship to the remote domain
4758 * @param argc standard argc
4759 * @param argv standard argv without initial components
4761 * @return Integer status (0 means success)
4764 static int rpc_trustdom_revoke(int argc
, const char **argv
)
4768 if (argc
< 1) return -1;
4770 /* generate upper cased domain name */
4771 domain_name
= smb_xstrdup(argv
[0]);
4772 strupper_m(domain_name
);
4774 /* delete password of the trust */
4775 if (!trusted_domain_password_delete(domain_name
)) {
4776 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
4785 * Usage for 'net rpc trustdom' command
4787 * @param argc standard argc
4788 * @param argv standard argv without inital components
4790 * @return Integer status returned to shell
4793 static int rpc_trustdom_usage(int argc
, const char **argv
)
4795 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
4796 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
4797 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
4798 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
4799 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
4800 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
4805 static NTSTATUS
rpc_query_domain_sid(const DOM_SID
*domain_sid
,
4806 const char *domain_name
,
4807 struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
4808 int argc
, const char **argv
)
4811 sid_to_string(str_sid
, domain_sid
);
4812 d_printf("%s\n", str_sid
);
4813 return NT_STATUS_OK
;
4816 static void print_trusted_domain(DOM_SID
*dom_sid
, const char *trusted_dom_name
)
4818 fstring ascii_sid
, padding
;
4819 int pad_len
, col_len
= 20;
4821 /* convert sid into ascii string */
4822 sid_to_string(ascii_sid
, dom_sid
);
4824 /* calculate padding space for d_printf to look nicer */
4825 pad_len
= col_len
- strlen(trusted_dom_name
);
4826 padding
[pad_len
] = 0;
4827 do padding
[--pad_len
] = ' '; while (pad_len
);
4829 d_printf("%s%s%s\n", trusted_dom_name
, padding
, ascii_sid
);
4832 static NTSTATUS
vampire_trusted_domain(struct cli_state
*cli
,
4833 TALLOC_CTX
*mem_ctx
,
4836 const char *trusted_dom_name
)
4839 LSA_TRUSTED_DOMAIN_INFO
*info
;
4842 smb_ucs2_t
*uni_dom_name
;
4844 nt_status
= cli_lsa_query_trusted_domain_info_by_sid(cli
, mem_ctx
, pol
, 4, &dom_sid
, &info
);
4846 if (NT_STATUS_IS_ERR(nt_status
)) {
4847 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
4848 nt_errstr(nt_status
)));
4852 data
= data_blob(NULL
, info
->password
.password
.length
);
4854 memcpy(data
.data
, info
->password
.password
.data
, info
->password
.password
.length
);
4855 data
.length
= info
->password
.password
.length
;
4857 cleartextpwd
= decrypt_trustdom_secret(cli
->pwd
.password
, &data
);
4859 if (cleartextpwd
== NULL
) {
4860 DEBUG(0,("retrieved NULL password\n"));
4861 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4865 if (push_ucs2_talloc(mem_ctx
, &uni_dom_name
, trusted_dom_name
) < 0) {
4866 DEBUG(0, ("Could not convert domain name %s to unicode\n",
4868 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4872 if (!secrets_store_trusted_domain_password(trusted_dom_name
,
4874 strlen_w(uni_dom_name
)+1,
4877 DEBUG(0, ("Storing password for trusted domain failed.\n"));
4878 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4882 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], password: [%s]\n",
4883 trusted_dom_name
, sid_string_static(&dom_sid
), cleartextpwd
));
4886 SAFE_FREE(cleartextpwd
);
4887 data_blob_free(&data
);
4892 static int rpc_trustdom_vampire(int argc
, const char **argv
)
4894 /* common variables */
4895 TALLOC_CTX
* mem_ctx
;
4896 struct cli_state
*cli
;
4898 const char *domain_name
= NULL
;
4899 DOM_SID
*queried_dom_sid
;
4900 POLICY_HND connect_hnd
;
4902 /* trusted domains listing variables */
4903 unsigned int num_domains
, enum_ctx
= 0;
4905 DOM_SID
*domain_sids
;
4906 char **trusted_dom_names
;
4911 * Listing trusted domains (stored in secrets.tdb, if local)
4914 mem_ctx
= talloc_init("trust relationships vampire");
4917 * set domain and pdc name to local samba server (default)
4918 * or to remote one given in command line
4921 if (StrCaseCmp(opt_workgroup
, lp_workgroup())) {
4922 domain_name
= opt_workgroup
;
4923 opt_target_workgroup
= opt_workgroup
;
4925 fstrcpy(pdc_name
, global_myname());
4926 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
4927 opt_target_workgroup
= domain_name
;
4930 /* open \PIPE\lsarpc and open policy handle */
4931 if (!(cli
= net_make_ipc_connection(NET_FLAGS_PDC
))) {
4932 DEBUG(0, ("Couldn't connect to domain controller\n"));
4936 if (!cli_nt_session_open(cli
, PI_LSARPC
)) {
4937 DEBUG(0, ("Could not initialise lsa pipe\n"));
4941 nt_status
= cli_lsa_open_policy2(cli
, mem_ctx
, False
, SEC_RIGHTS_QUERY_VALUE
,
4943 if (NT_STATUS_IS_ERR(nt_status
)) {
4944 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
4945 nt_errstr(nt_status
)));
4949 /* query info level 5 to obtain sid of a domain being queried */
4950 nt_status
= cli_lsa_query_info_policy(
4951 cli
, mem_ctx
, &connect_hnd
, 5 /* info level */,
4952 &dummy
, &queried_dom_sid
);
4954 if (NT_STATUS_IS_ERR(nt_status
)) {
4955 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
4956 nt_errstr(nt_status
)));
4961 * Keep calling LsaEnumTrustdom over opened pipe until
4962 * the end of enumeration is reached
4965 d_printf("Vampire trusted domains:\n\n");
4968 nt_status
= cli_lsa_enum_trust_dom(cli
, mem_ctx
, &connect_hnd
, &enum_ctx
,
4970 &trusted_dom_names
, &domain_sids
);
4972 if (NT_STATUS_IS_ERR(nt_status
)) {
4973 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
4974 nt_errstr(nt_status
)));
4978 for (i
= 0; i
< num_domains
; i
++) {
4980 print_trusted_domain(&(domain_sids
[i
]), trusted_dom_names
[i
]);
4982 nt_status
= vampire_trusted_domain(cli
, mem_ctx
, &connect_hnd
,
4983 domain_sids
[i
], trusted_dom_names
[i
]);
4984 if (!NT_STATUS_IS_OK(nt_status
))
4989 * in case of no trusted domains say something rather
4990 * than just display blank line
4992 if (!num_domains
) d_printf("none\n");
4994 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
4996 /* close this connection before doing next one */
4997 nt_status
= cli_lsa_close(cli
, mem_ctx
, &connect_hnd
);
4998 if (NT_STATUS_IS_ERR(nt_status
)) {
4999 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5000 nt_errstr(nt_status
)));
5004 /* close lsarpc pipe and connection to IPC$ */
5005 cli_nt_session_close(cli
);
5008 talloc_destroy(mem_ctx
);
5012 static int rpc_trustdom_list(int argc
, const char **argv
)
5014 /* common variables */
5015 TALLOC_CTX
* mem_ctx
;
5016 struct cli_state
*cli
, *remote_cli
;
5018 const char *domain_name
= NULL
;
5019 DOM_SID
*queried_dom_sid
;
5021 int ascii_dom_name_len
;
5022 POLICY_HND connect_hnd
;
5024 /* trusted domains listing variables */
5025 unsigned int num_domains
, enum_ctx
= 0;
5026 int i
, pad_len
, col_len
= 20;
5027 DOM_SID
*domain_sids
;
5028 char **trusted_dom_names
;
5032 /* trusting domains listing variables */
5033 POLICY_HND domain_hnd
;
5034 char **trusting_dom_names
;
5035 uint32
*trusting_dom_rids
;
5038 * Listing trusted domains (stored in secrets.tdb, if local)
5041 mem_ctx
= talloc_init("trust relationships listing");
5044 * set domain and pdc name to local samba server (default)
5045 * or to remote one given in command line
5048 if (StrCaseCmp(opt_workgroup
, lp_workgroup())) {
5049 domain_name
= opt_workgroup
;
5050 opt_target_workgroup
= opt_workgroup
;
5052 fstrcpy(pdc_name
, global_myname());
5053 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
5054 opt_target_workgroup
= domain_name
;
5057 /* open \PIPE\lsarpc and open policy handle */
5058 if (!(cli
= net_make_ipc_connection(NET_FLAGS_PDC
))) {
5059 DEBUG(0, ("Couldn't connect to domain controller\n"));
5063 if (!cli_nt_session_open(cli
, PI_LSARPC
)) {
5064 DEBUG(0, ("Could not initialise lsa pipe\n"));
5068 nt_status
= cli_lsa_open_policy2(cli
, mem_ctx
, False
, SEC_RIGHTS_QUERY_VALUE
,
5070 if (NT_STATUS_IS_ERR(nt_status
)) {
5071 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5072 nt_errstr(nt_status
)));
5076 /* query info level 5 to obtain sid of a domain being queried */
5077 nt_status
= cli_lsa_query_info_policy(
5078 cli
, mem_ctx
, &connect_hnd
, 5 /* info level */,
5079 &dummy
, &queried_dom_sid
);
5081 if (NT_STATUS_IS_ERR(nt_status
)) {
5082 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5083 nt_errstr(nt_status
)));
5088 * Keep calling LsaEnumTrustdom over opened pipe until
5089 * the end of enumeration is reached
5092 d_printf("Trusted domains list:\n\n");
5095 nt_status
= cli_lsa_enum_trust_dom(cli
, mem_ctx
, &connect_hnd
, &enum_ctx
,
5097 &trusted_dom_names
, &domain_sids
);
5099 if (NT_STATUS_IS_ERR(nt_status
)) {
5100 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5101 nt_errstr(nt_status
)));
5105 for (i
= 0; i
< num_domains
; i
++) {
5106 print_trusted_domain(&(domain_sids
[i
]), trusted_dom_names
[i
]);
5110 * in case of no trusted domains say something rather
5111 * than just display blank line
5113 if (!num_domains
) d_printf("none\n");
5115 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
5117 /* close this connection before doing next one */
5118 nt_status
= cli_lsa_close(cli
, mem_ctx
, &connect_hnd
);
5119 if (NT_STATUS_IS_ERR(nt_status
)) {
5120 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5121 nt_errstr(nt_status
)));
5125 cli_nt_session_close(cli
);
5128 * Listing trusting domains (stored in passdb backend, if local)
5131 d_printf("\nTrusting domains list:\n\n");
5134 * Open \PIPE\samr and get needed policy handles
5136 if (!cli_nt_session_open(cli
, PI_SAMR
)) {
5137 DEBUG(0, ("Could not initialise samr pipe\n"));
5142 nt_status
= cli_samr_connect(cli
, mem_ctx
, SA_RIGHT_SAM_OPEN_DOMAIN
,
5144 if (!NT_STATUS_IS_OK(nt_status
)) {
5145 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
5146 nt_errstr(nt_status
)));
5150 /* SamrOpenDomain - we have to open domain policy handle in order to be
5151 able to enumerate accounts*/
5152 nt_status
= cli_samr_open_domain(cli
, mem_ctx
, &connect_hnd
,
5153 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS
,
5154 queried_dom_sid
, &domain_hnd
);
5155 if (!NT_STATUS_IS_OK(nt_status
)) {
5156 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
5157 nt_errstr(nt_status
)));
5162 * perform actual enumeration
5165 enum_ctx
= 0; /* reset enumeration context from last enumeration */
5168 nt_status
= cli_samr_enum_dom_users(cli
, mem_ctx
, &domain_hnd
,
5169 &enum_ctx
, ACB_DOMTRUST
, 0xffff,
5170 &trusting_dom_names
, &trusting_dom_rids
,
5172 if (NT_STATUS_IS_ERR(nt_status
)) {
5173 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
5174 nt_errstr(nt_status
)));
5178 for (i
= 0; i
< num_domains
; i
++) {
5181 * get each single domain's sid (do we _really_ need this ?):
5182 * 1) connect to domain's pdc
5183 * 2) query the pdc for domain's sid
5186 /* get rid of '$' tail */
5187 ascii_dom_name_len
= strlen(trusting_dom_names
[i
]);
5188 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
5189 trusting_dom_names
[i
][ascii_dom_name_len
- 1] = '\0';
5191 /* calculate padding space for d_printf to look nicer */
5192 pad_len
= col_len
- strlen(trusting_dom_names
[i
]);
5193 padding
[pad_len
] = 0;
5194 do padding
[--pad_len
] = ' '; while (pad_len
);
5196 /* set opt_* variables to remote domain */
5197 strupper_m(trusting_dom_names
[i
]);
5198 opt_workgroup
= talloc_strdup(mem_ctx
, trusting_dom_names
[i
]);
5199 opt_target_workgroup
= opt_workgroup
;
5201 d_printf("%s%s", trusting_dom_names
[i
], padding
);
5203 /* connect to remote domain controller */
5204 remote_cli
= net_make_ipc_connection(NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
);
5206 /* query for domain's sid */
5207 if (run_rpc_command(remote_cli
, PI_LSARPC
, 0, rpc_query_domain_sid
, argc
, argv
))
5208 d_printf("couldn't get domain's sid\n");
5210 cli_shutdown(remote_cli
);
5213 d_printf("domain controller is not responding\n");
5217 if (!num_domains
) d_printf("none\n");
5219 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
5221 /* close opened samr and domain policy handles */
5222 nt_status
= cli_samr_close(cli
, mem_ctx
, &domain_hnd
);
5223 if (!NT_STATUS_IS_OK(nt_status
)) {
5224 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
5227 nt_status
= cli_samr_close(cli
, mem_ctx
, &connect_hnd
);
5228 if (!NT_STATUS_IS_OK(nt_status
)) {
5229 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
5232 /* close samr pipe and connection to IPC$ */
5233 cli_nt_session_close(cli
);
5236 talloc_destroy(mem_ctx
);
5241 * Entrypoint for 'net rpc trustdom' code
5243 * @param argc standard argc
5244 * @param argv standard argv without initial components
5246 * @return Integer status (0 means success)
5249 static int rpc_trustdom(int argc
, const char **argv
)
5251 struct functable func
[] = {
5252 {"add", rpc_trustdom_add
},
5253 {"del", rpc_trustdom_del
},
5254 {"establish", rpc_trustdom_establish
},
5255 {"revoke", rpc_trustdom_revoke
},
5256 {"help", rpc_trustdom_usage
},
5257 {"list", rpc_trustdom_list
},
5258 {"vampire", rpc_trustdom_vampire
},
5263 rpc_trustdom_usage(argc
, argv
);
5267 return (net_run_function(argc
, argv
, func
, rpc_user_usage
));
5271 * Check if a server will take rpc commands
5272 * @param flags Type of server to connect to (PDC, DMB, localhost)
5273 * if the host is not explicitly specified
5274 * @return BOOL (true means rpc supported)
5276 BOOL
net_rpc_check(unsigned flags
)
5278 struct cli_state cli
;
5280 struct in_addr server_ip
;
5281 char *server_name
= NULL
;
5283 /* flags (i.e. server type) may depend on command */
5284 if (!net_find_server(flags
, &server_ip
, &server_name
))
5288 if (cli_initialise(&cli
) == False
)
5291 if (!cli_connect(&cli
, server_name
, &server_ip
))
5293 if (!attempt_netbios_session_request(&cli
, global_myname(),
5294 server_name
, &server_ip
))
5296 if (!cli_negprot(&cli
))
5298 if (cli
.protocol
< PROTOCOL_NT1
)
5307 /* dump sam database via samsync rpc calls */
5308 static int rpc_samdump(int argc
, const char **argv
) {
5309 return run_rpc_command(NULL
, PI_NETLOGON
, NET_FLAGS_ANONYMOUS
, rpc_samdump_internals
,
5313 /* syncronise sam database via samsync rpc calls */
5314 static int rpc_vampire(int argc
, const char **argv
) {
5315 return run_rpc_command(NULL
, PI_NETLOGON
, NET_FLAGS_ANONYMOUS
, rpc_vampire_internals
,
5320 * Migrate everything from a print-server
5322 * @param argc Standard main() style argc
5323 * @param argv Standard main() style argv. Initial components are already
5326 * @return A shell status integer (0 for success)
5328 * The order is important !
5329 * To successfully add drivers the print-queues have to exist !
5330 * Applying ACLs should be the last step, because you're easily locked out
5333 static int rpc_printer_migrate_all(int argc
, const char **argv
)
5338 printf("no server to migrate\n");
5342 ret
= run_rpc_command(NULL
, PI_SPOOLSS
, 0, rpc_printer_migrate_printers_internals
, argc
, argv
);
5346 ret
= run_rpc_command(NULL
, PI_SPOOLSS
, 0, rpc_printer_migrate_drivers_internals
, argc
, argv
);
5350 ret
= run_rpc_command(NULL
, PI_SPOOLSS
, 0, rpc_printer_migrate_forms_internals
, argc
, argv
);
5354 ret
= run_rpc_command(NULL
, PI_SPOOLSS
, 0, rpc_printer_migrate_settings_internals
, argc
, argv
);
5358 return run_rpc_command(NULL
, PI_SPOOLSS
, 0, rpc_printer_migrate_security_internals
, argc
, argv
);
5363 * Migrate print-drivers from a print-server
5365 * @param argc Standard main() style argc
5366 * @param argv Standard main() style argv. Initial components are already
5369 * @return A shell status integer (0 for success)
5371 static int rpc_printer_migrate_drivers(int argc
, const char **argv
)
5374 printf("no server to migrate\n");
5378 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
5379 rpc_printer_migrate_drivers_internals
,
5384 * Migrate print-forms from a print-server
5386 * @param argc Standard main() style argc
5387 * @param argv Standard main() style argv. Initial components are already
5390 * @return A shell status integer (0 for success)
5392 static int rpc_printer_migrate_forms(int argc
, const char **argv
)
5395 printf("no server to migrate\n");
5399 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
5400 rpc_printer_migrate_forms_internals
,
5405 * Migrate printers from a print-server
5407 * @param argc Standard main() style argc
5408 * @param argv Standard main() style argv. Initial components are already
5411 * @return A shell status integer (0 for success)
5413 static int rpc_printer_migrate_printers(int argc
, const char **argv
)
5416 printf("no server to migrate\n");
5420 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
5421 rpc_printer_migrate_printers_internals
,
5426 * Migrate printer-ACLs from a print-server
5428 * @param argc Standard main() style argc
5429 * @param argv Standard main() style argv. Initial components are already
5432 * @return A shell status integer (0 for success)
5434 static int rpc_printer_migrate_security(int argc
, const char **argv
)
5437 printf("no server to migrate\n");
5441 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
5442 rpc_printer_migrate_security_internals
,
5447 * Migrate printer-settings from a print-server
5449 * @param argc Standard main() style argc
5450 * @param argv Standard main() style argv. Initial components are already
5453 * @return A shell status integer (0 for success)
5455 static int rpc_printer_migrate_settings(int argc
, const char **argv
)
5458 printf("no server to migrate\n");
5462 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
5463 rpc_printer_migrate_settings_internals
,
5468 * 'net rpc printer' entrypoint.
5469 * @param argc Standard main() style argc
5470 * @param argv Standard main() style argv. Initial components are already
5474 int rpc_printer_migrate(int argc
, const char **argv
)
5477 /* ouch: when addriver and setdriver are called from within
5478 rpc_printer_migrate_drivers_internals, the printer-queue already
5481 struct functable func
[] = {
5482 {"all", rpc_printer_migrate_all
},
5483 {"drivers", rpc_printer_migrate_drivers
},
5484 {"forms", rpc_printer_migrate_forms
},
5485 {"help", rpc_printer_usage
},
5486 {"printers", rpc_printer_migrate_printers
},
5487 {"security", rpc_printer_migrate_security
},
5488 {"settings", rpc_printer_migrate_settings
},
5492 return net_run_function(argc
, argv
, func
, rpc_printer_usage
);
5497 * List printers on a remote RPC server
5499 * @param argc Standard main() style argc
5500 * @param argv Standard main() style argv. Initial components are already
5503 * @return A shell status integer (0 for success)
5505 static int rpc_printer_list(int argc
, const char **argv
)
5508 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
5509 rpc_printer_list_internals
,
5514 * List printer-drivers on a remote RPC server
5516 * @param argc Standard main() style argc
5517 * @param argv Standard main() style argv. Initial components are already
5520 * @return A shell status integer (0 for success)
5522 static int rpc_printer_driver_list(int argc
, const char **argv
)
5525 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
5526 rpc_printer_driver_list_internals
,
5531 * Publish printer in ADS via MSRPC
5533 * @param argc Standard main() style argc
5534 * @param argv Standard main() style argv. Initial components are already
5537 * @return A shell status integer (0 for success)
5539 static int rpc_printer_publish_publish(int argc
, const char **argv
)
5542 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
5543 rpc_printer_publish_publish_internals
,
5548 * Update printer in ADS via MSRPC
5550 * @param argc Standard main() style argc
5551 * @param argv Standard main() style argv. Initial components are already
5554 * @return A shell status integer (0 for success)
5556 static int rpc_printer_publish_update(int argc
, const char **argv
)
5559 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
5560 rpc_printer_publish_update_internals
,
5565 * UnPublish printer in ADS via MSRPC
5567 * @param argc Standard main() style argc
5568 * @param argv Standard main() style argv. Initial components are already
5571 * @return A shell status integer (0 for success)
5573 static int rpc_printer_publish_unpublish(int argc
, const char **argv
)
5576 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
5577 rpc_printer_publish_unpublish_internals
,
5582 * List published printers via MSRPC
5584 * @param argc Standard main() style argc
5585 * @param argv Standard main() style argv. Initial components are already
5588 * @return A shell status integer (0 for success)
5590 static int rpc_printer_publish_list(int argc
, const char **argv
)
5593 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
5594 rpc_printer_publish_list_internals
,
5600 * Publish printer in ADS
5602 * @param argc Standard main() style argc
5603 * @param argv Standard main() style argv. Initial components are already
5606 * @return A shell status integer (0 for success)
5608 static int rpc_printer_publish(int argc
, const char **argv
)
5611 struct functable func
[] = {
5612 {"publish", rpc_printer_publish_publish
},
5613 {"update", rpc_printer_publish_update
},
5614 {"unpublish", rpc_printer_publish_unpublish
},
5615 {"list", rpc_printer_publish_list
},
5616 {"help", rpc_printer_usage
},
5621 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
5622 rpc_printer_publish_list_internals
,
5625 return net_run_function(argc
, argv
, func
, rpc_printer_usage
);
5631 * Display rpc printer help page.
5632 * @param argc Standard main() style argc
5633 * @param argv Standard main() style argv. Initial components are already
5636 int rpc_printer_usage(int argc
, const char **argv
)
5638 return net_help_printer(argc
, argv
);
5642 * 'net rpc printer' entrypoint.
5643 * @param argc Standard main() style argc
5644 * @param argv Standard main() style argv. Initial components are already
5647 int net_rpc_printer(int argc
, const char **argv
)
5649 struct functable func
[] = {
5650 {"list", rpc_printer_list
},
5651 {"migrate", rpc_printer_migrate
},
5652 {"driver", rpc_printer_driver_list
},
5653 {"publish", rpc_printer_publish
},
5658 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
5659 rpc_printer_list_internals
,
5662 return net_run_function(argc
, argv
, func
, rpc_printer_usage
);
5665 /****************************************************************************/
5669 * Basic usage function for 'net rpc'
5670 * @param argc Standard main() style argc
5671 * @param argv Standard main() style argv. Initial components are already
5675 int net_rpc_usage(int argc
, const char **argv
)
5677 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
5678 d_printf(" net rpc join \t\t\tto join a domain \n");
5679 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n");
5680 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
5681 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
5682 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
5683 d_printf(" net rpc group \t\tto list groups\n");
5684 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
5685 d_printf(" net rpc printer \t\tto list and migrate printers\n");
5686 d_printf(" net rpc file \t\t\tto list open files\n");
5687 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
5688 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
5689 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
5690 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
5691 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
5692 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
5693 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
5694 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
5695 d_printf(" net rpc registry\t\tto manage registry hives\n");
5697 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
5698 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
5699 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
5700 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
5701 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
5707 * Help function for 'net rpc'. Calls command specific help if requested
5708 * or displays usage of net rpc
5709 * @param argc Standard main() style argc
5710 * @param argv Standard main() style argv. Initial components are already
5714 int net_rpc_help(int argc
, const char **argv
)
5716 struct functable func
[] = {
5717 {"join", rpc_join_usage
},
5718 {"user", rpc_user_usage
},
5719 {"group", rpc_group_usage
},
5720 {"share", rpc_share_usage
},
5721 /*{"changetrustpw", rpc_changetrustpw_usage}, */
5722 {"trustdom", rpc_trustdom_usage
},
5723 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
5724 /*{"shutdown", rpc_shutdown_usage}, */
5729 net_rpc_usage(argc
, argv
);
5733 return (net_run_function(argc
, argv
, func
, rpc_user_usage
));
5738 * 'net rpc' entrypoint.
5739 * @param argc Standard main() style argc
5740 * @param argv Standard main() style argv. Initial components are already
5744 int net_rpc(int argc
, const char **argv
)
5746 struct functable func
[] = {
5747 {"info", net_rpc_info
},
5748 {"join", net_rpc_join
},
5749 {"oldjoin", net_rpc_oldjoin
},
5750 {"testjoin", net_rpc_testjoin
},
5751 {"user", net_rpc_user
},
5752 {"password", rpc_user_password
},
5753 {"group", net_rpc_group
},
5754 {"share", net_rpc_share
},
5755 {"file", net_rpc_file
},
5756 {"printer", net_rpc_printer
},
5757 {"changetrustpw", net_rpc_changetrustpw
},
5758 {"trustdom", rpc_trustdom
},
5759 {"abortshutdown", rpc_shutdown_abort
},
5760 {"shutdown", rpc_shutdown
},
5761 {"samdump", rpc_samdump
},
5762 {"vampire", rpc_vampire
},
5763 {"getsid", net_rpc_getsid
},
5764 {"rights", net_rpc_rights
},
5765 {"service", net_rpc_service
},
5766 {"registry", net_rpc_registry
},
5767 {"help", net_rpc_help
},
5770 return net_run_function(argc
, argv
, func
, net_rpc_usage
);