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)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "utils/net.h"
26 static int net_mode_share
;
31 * @brief RPC based subcommands for the 'net' utility.
33 * This file should contain much of the functionality that used to
34 * be found in rpcclient, execpt that the commands should change
35 * less often, and the fucntionality should be sane (the user is not
36 * expected to know a rid/sid before they conduct an operation etc.)
38 * @todo Perhaps eventually these should be split out into a number
39 * of files, as this could get quite big.
44 * Many of the RPC functions need the domain sid. This function gets
45 * it at the start of every run
47 * @param cli A cli_state already connected to the remote machine
49 * @return The Domain SID of the remote machine.
52 NTSTATUS
net_get_remote_domain_sid(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
53 DOM_SID
**domain_sid
, char **domain_name
)
55 struct rpc_pipe_client
*lsa_pipe
;
57 NTSTATUS result
= NT_STATUS_OK
;
58 uint32 info_class
= 5;
60 lsa_pipe
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &result
);
62 d_fprintf(stderr
, "Could not initialise lsa pipe\n");
66 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, False
,
67 SEC_RIGHTS_MAXIMUM_ALLOWED
,
69 if (!NT_STATUS_IS_OK(result
)) {
70 d_fprintf(stderr
, "open_policy failed: %s\n",
75 result
= rpccli_lsa_query_info_policy(lsa_pipe
, mem_ctx
, &pol
,
76 info_class
, domain_name
,
78 if (!NT_STATUS_IS_OK(result
)) {
79 d_fprintf(stderr
, "lsaquery failed: %s\n",
84 rpccli_lsa_close(lsa_pipe
, mem_ctx
, &pol
);
85 cli_rpc_pipe_close(lsa_pipe
);
91 * Run a single RPC command, from start to finish.
93 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
94 * @param conn_flag a NET_FLAG_ combination. Passed to
95 * net_make_ipc_connection.
96 * @param argc Standard main() style argc
97 * @param argc Standard main() style argv. Initial components are already
99 * @return A shell status integer (0 for success)
102 int run_rpc_command(struct cli_state
*cli_arg
,
109 struct cli_state
*cli
= NULL
;
110 struct rpc_pipe_client
*pipe_hnd
= NULL
;
116 /* make use of cli_state handed over as an argument, if possible */
118 cli
= net_make_ipc_connection(conn_flags
);
129 if (!(mem_ctx
= talloc_init("run_rpc_command"))) {
130 DEBUG(0, ("talloc_init() failed\n"));
135 nt_status
= net_get_remote_domain_sid(cli
, mem_ctx
, &domain_sid
,
137 if (!NT_STATUS_IS_OK(nt_status
)) {
142 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
143 if (lp_client_schannel() && (pipe_idx
== PI_NETLOGON
)) {
144 /* Always try and create an schannel netlogon pipe. */
145 pipe_hnd
= cli_rpc_pipe_open_schannel(cli
, pipe_idx
,
146 PIPE_AUTH_LEVEL_PRIVACY
,
150 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
151 nt_errstr(nt_status
) ));
156 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, pipe_idx
, &nt_status
);
158 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
159 cli_get_pipe_name(pipe_idx
),
160 nt_errstr(nt_status
) ));
167 nt_status
= fn(domain_sid
, domain_name
, cli
, pipe_hnd
, mem_ctx
, argc
, argv
);
169 if (!NT_STATUS_IS_OK(nt_status
)) {
170 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status
)));
172 DEBUG(5, ("rpc command function succedded\n"));
175 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
177 cli_rpc_pipe_close(pipe_hnd
);
181 /* close the connection only if it was opened here */
186 talloc_destroy(mem_ctx
);
187 return (!NT_STATUS_IS_OK(nt_status
));
191 * Force a change of the trust acccount password.
193 * All parameters are provided by the run_rpc_command function, except for
194 * argc, argv which are passes through.
196 * @param domain_sid The domain sid aquired from the remote server
197 * @param cli A cli_state connected to the server.
198 * @param mem_ctx Talloc context, destoyed on compleation of the function.
199 * @param argc Standard main() style argc
200 * @param argc Standard main() style argv. Initial components are already
203 * @return Normal NTSTATUS return.
206 static NTSTATUS
rpc_changetrustpw_internals(const DOM_SID
*domain_sid
,
207 const char *domain_name
,
208 struct cli_state
*cli
,
209 struct rpc_pipe_client
*pipe_hnd
,
215 return trust_pw_find_change_and_store_it(pipe_hnd
, mem_ctx
, opt_target_workgroup
);
219 * Force a change of the trust acccount password.
221 * @param argc Standard main() style argc
222 * @param argc Standard main() style argv. Initial components are already
225 * @return A shell status integer (0 for success)
228 int net_rpc_changetrustpw(int argc
, const char **argv
)
230 return run_rpc_command(NULL
, PI_NETLOGON
, NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
231 rpc_changetrustpw_internals
,
236 * Join a domain, the old way.
238 * This uses 'machinename' as the inital password, and changes it.
240 * The password should be created with 'server manager' or equiv first.
242 * All parameters are provided by the run_rpc_command function, except for
243 * argc, argv which are passes through.
245 * @param domain_sid The domain sid aquired from the remote server
246 * @param cli A cli_state connected to the server.
247 * @param mem_ctx Talloc context, destoyed on compleation of the function.
248 * @param argc Standard main() style argc
249 * @param argc Standard main() style argv. Initial components are already
252 * @return Normal NTSTATUS return.
255 static NTSTATUS
rpc_oldjoin_internals(const DOM_SID
*domain_sid
,
256 const char *domain_name
,
257 struct cli_state
*cli
,
258 struct rpc_pipe_client
*pipe_hnd
,
264 fstring trust_passwd
;
265 unsigned char orig_trust_passwd_hash
[16];
267 uint32 sec_channel_type
;
269 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_NETLOGON
, &result
);
271 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
274 nt_errstr(result
) ));
279 check what type of join - if the user want's to join as
280 a BDC, the server must agree that we are a BDC.
283 sec_channel_type
= get_sec_channel_type(argv
[0]);
285 sec_channel_type
= get_sec_channel_type(NULL
);
288 fstrcpy(trust_passwd
, global_myname());
289 strlower_m(trust_passwd
);
292 * Machine names can be 15 characters, but the max length on
293 * a password is 14. --jerry
296 trust_passwd
[14] = '\0';
298 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
300 result
= trust_pw_change_and_store_it(pipe_hnd
, mem_ctx
, opt_target_workgroup
,
301 orig_trust_passwd_hash
,
304 if (NT_STATUS_IS_OK(result
))
305 printf("Joined domain %s.\n",opt_target_workgroup
);
308 if (!secrets_store_domain_sid(opt_target_workgroup
, domain_sid
)) {
309 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup
));
310 result
= NT_STATUS_UNSUCCESSFUL
;
317 * Join a domain, the old way.
319 * @param argc Standard main() style argc
320 * @param argc Standard main() style argv. Initial components are already
323 * @return A shell status integer (0 for success)
326 static int net_rpc_perform_oldjoin(int argc
, const char **argv
)
328 return run_rpc_command(NULL
, PI_NETLOGON
,
329 NET_FLAGS_NO_PIPE
| NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
330 rpc_oldjoin_internals
,
335 * Join a domain, the old way. This function exists to allow
336 * the message to be displayed when oldjoin was explicitly
337 * requested, but not when it was implied by "net rpc join"
339 * @param argc Standard main() style argc
340 * @param argc Standard main() style argv. Initial components are already
343 * @return A shell status integer (0 for success)
346 static int net_rpc_oldjoin(int argc
, const char **argv
)
348 int rc
= net_rpc_perform_oldjoin(argc
, argv
);
351 d_fprintf(stderr
, "Failed to join domain\n");
358 * Basic usage function for 'net rpc join'
359 * @param argc Standard main() style argc
360 * @param argc Standard main() style argv. Initial components are already
364 static int rpc_join_usage(int argc
, const char **argv
)
366 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
367 "\t to join a domain with admin username & password\n"\
368 "\t\t password will be prompted if needed and none is specified\n"\
369 "\t <type> can be (default MEMBER)\n"\
370 "\t\t BDC - Join as a BDC\n"\
371 "\t\t PDC - Join as a PDC\n"\
372 "\t\t MEMBER - Join as a MEMBER server\n");
374 net_common_flags_usage(argc
, argv
);
379 * 'net rpc join' entrypoint.
380 * @param argc Standard main() style argc
381 * @param argc Standard main() style argv. Initial components are already
384 * Main 'net_rpc_join()' (where the admin username/password is used) is
386 * Try to just change the password, but if that doesn't work, use/prompt
387 * for a username/password.
390 int net_rpc_join(int argc
, const char **argv
)
392 if (lp_server_role() == ROLE_STANDALONE
) {
393 d_printf("cannot join as standalone machine\n");
397 if (strlen(global_myname()) > 15) {
398 d_printf("Our netbios name can be at most 15 chars long, "
399 "\"%s\" is %u chars long\n",
400 global_myname(), (unsigned int)strlen(global_myname()));
404 if ((net_rpc_perform_oldjoin(argc
, argv
) == 0))
407 return net_rpc_join_newstyle(argc
, argv
);
411 * display info about a rpc domain
413 * All parameters are provided by the run_rpc_command function, except for
414 * argc, argv which are passed through.
416 * @param domain_sid The domain sid acquired from the remote server
417 * @param cli A cli_state connected to the server.
418 * @param mem_ctx Talloc context, destoyed on completion of the function.
419 * @param argc Standard main() style argc
420 * @param argv Standard main() style argv. Initial components are already
423 * @return Normal NTSTATUS return.
426 NTSTATUS
rpc_info_internals(const DOM_SID
*domain_sid
,
427 const char *domain_name
,
428 struct cli_state
*cli
,
429 struct rpc_pipe_client
*pipe_hnd
,
434 POLICY_HND connect_pol
, domain_pol
;
435 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
439 sid_to_string(sid_str
, domain_sid
);
441 /* Get sam policy handle */
442 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
444 if (!NT_STATUS_IS_OK(result
)) {
445 d_fprintf(stderr
, "Could not connect to SAM: %s\n", nt_errstr(result
));
449 /* Get domain policy handle */
450 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
451 MAXIMUM_ALLOWED_ACCESS
,
452 domain_sid
, &domain_pol
);
453 if (!NT_STATUS_IS_OK(result
)) {
454 d_fprintf(stderr
, "Could not open domain: %s\n", nt_errstr(result
));
459 result
= rpccli_samr_query_dom_info(pipe_hnd
, mem_ctx
, &domain_pol
,
461 if (NT_STATUS_IS_OK(result
)) {
462 TALLOC_CTX
*ctx
= talloc_init("rpc_info_internals");
463 d_printf("Domain Name: %s\n", unistr2_tdup(ctx
, &ctr
.info
.inf2
.uni_domain
));
464 d_printf("Domain SID: %s\n", sid_str
);
465 d_printf("Sequence number: %llu\n", (unsigned long long)ctr
.info
.inf2
.seq_num
);
466 d_printf("Num users: %u\n", ctr
.info
.inf2
.num_domain_usrs
);
467 d_printf("Num domain groups: %u\n", ctr
.info
.inf2
.num_domain_grps
);
468 d_printf("Num local groups: %u\n", ctr
.info
.inf2
.num_local_grps
);
477 * 'net rpc info' entrypoint.
478 * @param argc Standard main() style argc
479 * @param argc Standard main() style argv. Initial components are already
483 int net_rpc_info(int argc
, const char **argv
)
485 return run_rpc_command(NULL
, PI_SAMR
, NET_FLAGS_PDC
,
491 * Fetch domain SID into the local secrets.tdb
493 * All parameters are provided by the run_rpc_command function, except for
494 * argc, argv which are passes through.
496 * @param domain_sid The domain sid acquired from the remote server
497 * @param cli A cli_state connected to the server.
498 * @param mem_ctx Talloc context, destoyed on completion of the function.
499 * @param argc Standard main() style argc
500 * @param argv Standard main() style argv. Initial components are already
503 * @return Normal NTSTATUS return.
506 static NTSTATUS
rpc_getsid_internals(const DOM_SID
*domain_sid
,
507 const char *domain_name
,
508 struct cli_state
*cli
,
509 struct rpc_pipe_client
*pipe_hnd
,
516 sid_to_string(sid_str
, domain_sid
);
517 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
518 sid_str
, domain_name
);
520 if (!secrets_store_domain_sid(domain_name
, domain_sid
)) {
521 DEBUG(0,("Can't store domain SID\n"));
522 return NT_STATUS_UNSUCCESSFUL
;
529 * 'net rpc getsid' entrypoint.
530 * @param argc Standard main() style argc
531 * @param argc Standard main() style argv. Initial components are already
535 int net_rpc_getsid(int argc
, const char **argv
)
537 return run_rpc_command(NULL
, PI_SAMR
, NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
538 rpc_getsid_internals
,
542 /****************************************************************************/
545 * Basic usage function for 'net rpc user'
546 * @param argc Standard main() style argc.
547 * @param argv Standard main() style argv. Initial components are already
551 static int rpc_user_usage(int argc
, const char **argv
)
553 return net_help_user(argc
, argv
);
557 * Add a new user to a remote RPC server
559 * All parameters are provided by the run_rpc_command function, except for
560 * argc, argv which are passes through.
562 * @param domain_sid The domain sid acquired from the remote server
563 * @param cli A cli_state connected to the server.
564 * @param mem_ctx Talloc context, destoyed on completion of the function.
565 * @param argc Standard main() style argc
566 * @param argv Standard main() style argv. Initial components are already
569 * @return Normal NTSTATUS return.
572 static NTSTATUS
rpc_user_add_internals(const DOM_SID
*domain_sid
,
573 const char *domain_name
,
574 struct cli_state
*cli
,
575 struct rpc_pipe_client
*pipe_hnd
,
577 int argc
, const char **argv
)
580 POLICY_HND connect_pol
, domain_pol
, user_pol
;
581 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
582 const char *acct_name
;
588 d_printf("User must be specified\n");
589 rpc_user_usage(argc
, argv
);
595 /* Get sam policy handle */
597 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
599 if (!NT_STATUS_IS_OK(result
)) {
603 /* Get domain policy handle */
605 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
606 MAXIMUM_ALLOWED_ACCESS
,
607 domain_sid
, &domain_pol
);
608 if (!NT_STATUS_IS_OK(result
)) {
612 /* Create domain user */
614 acb_info
= ACB_NORMAL
;
615 acct_flags
= SAMR_GENERIC_READ
| SAMR_GENERIC_WRITE
|
616 SAMR_GENERIC_EXECUTE
| SAMR_STANDARD_WRITEDAC
|
617 SAMR_STANDARD_DELETE
| SAMR_USER_SETPASS
| SAMR_USER_GETATTR
|
619 DEBUG(10, ("Creating account with flags: %d\n",acct_flags
));
621 result
= rpccli_samr_create_dom_user(pipe_hnd
, mem_ctx
, &domain_pol
,
622 acct_name
, acb_info
, acct_flags
,
623 &user_pol
, &user_rid
);
624 if (!NT_STATUS_IS_OK(result
)) {
630 uint32
*user_rids
, num_rids
, *name_types
;
631 uint32 flags
= 0x000003e8; /* Unknown */
632 SAM_USERINFO_CTR ctr
;
633 SAM_USER_INFO_24 p24
;
636 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
,
637 flags
, 1, &acct_name
,
638 &num_rids
, &user_rids
,
641 if (!NT_STATUS_IS_OK(result
)) {
645 result
= rpccli_samr_open_user(pipe_hnd
, mem_ctx
, &domain_pol
,
646 MAXIMUM_ALLOWED_ACCESS
,
647 user_rids
[0], &user_pol
);
649 if (!NT_STATUS_IS_OK(result
)) {
653 /* Set password on account */
658 encode_pw_buffer(pwbuf
, argv
[1], STR_UNICODE
);
660 init_sam_user_info24(&p24
, (char *)pwbuf
,24);
662 ctr
.switch_value
= 24;
663 ctr
.info
.id24
= &p24
;
665 result
= rpccli_samr_set_userinfo(pipe_hnd
, mem_ctx
, &user_pol
, 24,
666 &cli
->user_session_key
, &ctr
);
668 if (!NT_STATUS_IS_OK(result
)) {
669 d_fprintf(stderr
, "Failed to set password for user %s - %s\n",
670 acct_name
, nt_errstr(result
));
672 result
= rpccli_samr_delete_dom_user(pipe_hnd
, mem_ctx
, &user_pol
);
674 if (!NT_STATUS_IS_OK(result
)) {
675 d_fprintf(stderr
, "Failed to delete user %s - %s\n",
676 acct_name
, nt_errstr(result
));
683 if (!NT_STATUS_IS_OK(result
)) {
684 d_fprintf(stderr
, "Failed to add user %s - %s\n", acct_name
,
687 d_printf("Added user %s\n", acct_name
);
693 * Add a new user to a remote RPC server
695 * @param argc Standard main() style argc
696 * @param argv Standard main() style argv. Initial components are already
699 * @return A shell status integer (0 for success)
702 static int rpc_user_add(int argc
, const char **argv
)
704 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_add_internals
,
709 * Delete a user from a remote RPC server
711 * All parameters are provided by the run_rpc_command function, except for
712 * argc, argv which are passes through.
714 * @param domain_sid The domain sid acquired from the remote server
715 * @param cli A cli_state connected to the server.
716 * @param mem_ctx Talloc context, destoyed on completion of the function.
717 * @param argc Standard main() style argc
718 * @param argv Standard main() style argv. Initial components are already
721 * @return Normal NTSTATUS return.
724 static NTSTATUS
rpc_user_del_internals(const DOM_SID
*domain_sid
,
725 const char *domain_name
,
726 struct cli_state
*cli
,
727 struct rpc_pipe_client
*pipe_hnd
,
732 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
733 POLICY_HND connect_pol
, domain_pol
, user_pol
;
736 d_printf("User must be specified\n");
737 rpc_user_usage(argc
, argv
);
740 /* Get sam policy and domain handles */
742 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
745 if (!NT_STATUS_IS_OK(result
)) {
749 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
750 MAXIMUM_ALLOWED_ACCESS
,
751 domain_sid
, &domain_pol
);
753 if (!NT_STATUS_IS_OK(result
)) {
757 /* Get handle on user */
760 uint32
*user_rids
, num_rids
, *name_types
;
761 uint32 flags
= 0x000003e8; /* Unknown */
763 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
,
765 &num_rids
, &user_rids
,
768 if (!NT_STATUS_IS_OK(result
)) {
772 result
= rpccli_samr_open_user(pipe_hnd
, mem_ctx
, &domain_pol
,
773 MAXIMUM_ALLOWED_ACCESS
,
774 user_rids
[0], &user_pol
);
776 if (!NT_STATUS_IS_OK(result
)) {
783 result
= rpccli_samr_delete_dom_user(pipe_hnd
, mem_ctx
, &user_pol
);
785 if (!NT_STATUS_IS_OK(result
)) {
789 /* Display results */
790 if (!NT_STATUS_IS_OK(result
)) {
791 d_fprintf(stderr
, "Failed to delete user account - %s\n", nt_errstr(result
));
793 d_printf("Deleted user account\n");
801 * Rename a user on a remote RPC server
803 * All parameters are provided by the run_rpc_command function, except for
804 * argc, argv which are passes through.
806 * @param domain_sid The domain sid acquired from the remote server
807 * @param cli A cli_state connected to the server.
808 * @param mem_ctx Talloc context, destoyed on completion of the function.
809 * @param argc Standard main() style argc
810 * @param argv Standard main() style argv. Initial components are already
813 * @return Normal NTSTATUS return.
816 static NTSTATUS
rpc_user_rename_internals(const DOM_SID
*domain_sid
,
817 const char *domain_name
,
818 struct cli_state
*cli
,
819 struct rpc_pipe_client
*pipe_hnd
,
824 POLICY_HND connect_pol
, domain_pol
, user_pol
;
825 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
826 uint32 info_level
= 7;
827 const char *old_name
, *new_name
;
829 uint32 flags
= 0x000003e8; /* Unknown */
830 uint32 num_rids
, *name_types
;
831 uint32 num_names
= 1;
833 SAM_USERINFO_CTR
*user_ctr
;
834 SAM_USERINFO_CTR ctr
;
835 SAM_USER_INFO_7 info7
;
838 d_printf("Old and new username must be specified\n");
839 rpc_user_usage(argc
, argv
);
847 ZERO_STRUCT(user_ctr
);
849 /* Get sam policy handle */
851 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
853 if (!NT_STATUS_IS_OK(result
)) {
857 /* Get domain policy handle */
859 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
860 MAXIMUM_ALLOWED_ACCESS
,
861 domain_sid
, &domain_pol
);
862 if (!NT_STATUS_IS_OK(result
)) {
866 if ((names
= TALLOC_ARRAY(mem_ctx
, const char *, num_names
)) == NULL
) {
867 result
= NT_STATUS_NO_MEMORY
;
871 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
,
872 flags
, num_names
, names
,
873 &num_rids
, &user_rid
, &name_types
);
874 if (!NT_STATUS_IS_OK(result
)) {
878 /* Open domain user */
879 result
= rpccli_samr_open_user(pipe_hnd
, mem_ctx
, &domain_pol
,
880 MAXIMUM_ALLOWED_ACCESS
, user_rid
[0], &user_pol
);
882 if (!NT_STATUS_IS_OK(result
)) {
886 /* Query user info */
887 result
= rpccli_samr_query_userinfo(pipe_hnd
, mem_ctx
, &user_pol
,
888 info_level
, &user_ctr
);
890 if (!NT_STATUS_IS_OK(result
)) {
894 ctr
.switch_value
= info_level
;
895 ctr
.info
.id7
= &info7
;
897 init_sam_user_info7(&info7
, new_name
);
900 result
= rpccli_samr_set_userinfo(pipe_hnd
, mem_ctx
, &user_pol
,
901 info_level
, &cli
->user_session_key
, &ctr
);
903 if (!NT_STATUS_IS_OK(result
)) {
908 if (!NT_STATUS_IS_OK(result
)) {
909 d_fprintf(stderr
, "Failed to rename user from %s to %s - %s\n", old_name
, new_name
,
912 d_printf("Renamed user from %s to %s\n", old_name
, new_name
);
918 * Rename a user on a remote RPC server
920 * @param argc Standard main() style argc
921 * @param argv Standard main() style argv. Initial components are already
924 * @return A shell status integer (0 for success)
927 static int rpc_user_rename(int argc
, const char **argv
)
929 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_rename_internals
,
934 * Delete a user from a remote RPC server
936 * @param argc Standard main() style argc
937 * @param argv Standard main() style argv. Initial components are already
940 * @return A shell status integer (0 for success)
943 static int rpc_user_delete(int argc
, const char **argv
)
945 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_del_internals
,
950 * Set a password for a user on a remote RPC server
952 * All parameters are provided by the run_rpc_command function, except for
953 * argc, argv which are passes through.
955 * @param domain_sid The domain sid acquired from the remote server
956 * @param cli A cli_state connected to the server.
957 * @param mem_ctx Talloc context, destoyed on completion of the function.
958 * @param argc Standard main() style argc
959 * @param argv Standard main() style argv. Initial components are already
962 * @return Normal NTSTATUS return.
965 static NTSTATUS
rpc_user_password_internals(const DOM_SID
*domain_sid
,
966 const char *domain_name
,
967 struct cli_state
*cli
,
968 struct rpc_pipe_client
*pipe_hnd
,
973 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
974 POLICY_HND connect_pol
, domain_pol
, user_pol
;
975 SAM_USERINFO_CTR ctr
;
976 SAM_USER_INFO_24 p24
;
979 const char *new_password
;
983 d_printf("User must be specified\n");
984 rpc_user_usage(argc
, argv
);
991 new_password
= argv
[1];
993 asprintf(&prompt
, "Enter new password for %s:", user
);
994 new_password
= getpass(prompt
);
998 /* Get sam policy and domain handles */
1000 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1003 if (!NT_STATUS_IS_OK(result
)) {
1007 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
1008 MAXIMUM_ALLOWED_ACCESS
,
1009 domain_sid
, &domain_pol
);
1011 if (!NT_STATUS_IS_OK(result
)) {
1015 /* Get handle on user */
1018 uint32
*user_rids
, num_rids
, *name_types
;
1019 uint32 flags
= 0x000003e8; /* Unknown */
1021 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
,
1023 &num_rids
, &user_rids
,
1026 if (!NT_STATUS_IS_OK(result
)) {
1030 result
= rpccli_samr_open_user(pipe_hnd
, mem_ctx
, &domain_pol
,
1031 MAXIMUM_ALLOWED_ACCESS
,
1032 user_rids
[0], &user_pol
);
1034 if (!NT_STATUS_IS_OK(result
)) {
1039 /* Set password on account */
1044 encode_pw_buffer(pwbuf
, new_password
, STR_UNICODE
);
1046 init_sam_user_info24(&p24
, (char *)pwbuf
,24);
1048 ctr
.switch_value
= 24;
1049 ctr
.info
.id24
= &p24
;
1051 result
= rpccli_samr_set_userinfo(pipe_hnd
, mem_ctx
, &user_pol
, 24,
1052 &cli
->user_session_key
, &ctr
);
1054 if (!NT_STATUS_IS_OK(result
)) {
1058 /* Display results */
1066 * Set a user's password on a remote RPC server
1068 * @param argc Standard main() style argc
1069 * @param argv Standard main() style argv. Initial components are already
1072 * @return A shell status integer (0 for success)
1075 static int rpc_user_password(int argc
, const char **argv
)
1077 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_password_internals
,
1082 * List user's groups on a remote RPC server
1084 * All parameters are provided by the run_rpc_command function, except for
1085 * argc, argv which are passes through.
1087 * @param domain_sid The domain sid acquired from the remote server
1088 * @param cli A cli_state connected to the server.
1089 * @param mem_ctx Talloc context, destoyed on completion of the function.
1090 * @param argc Standard main() style argc
1091 * @param argv Standard main() style argv. Initial components are already
1094 * @return Normal NTSTATUS return.
1097 static NTSTATUS
rpc_user_info_internals(const DOM_SID
*domain_sid
,
1098 const char *domain_name
,
1099 struct cli_state
*cli
,
1100 struct rpc_pipe_client
*pipe_hnd
,
1101 TALLOC_CTX
*mem_ctx
,
1105 POLICY_HND connect_pol
, domain_pol
, user_pol
;
1106 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1107 uint32
*rids
, num_rids
, *name_types
, num_names
;
1108 uint32 flags
= 0x000003e8; /* Unknown */
1114 d_printf("User must be specified\n");
1115 rpc_user_usage(argc
, argv
);
1116 return NT_STATUS_OK
;
1118 /* Get sam policy handle */
1120 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1122 if (!NT_STATUS_IS_OK(result
)) goto done
;
1124 /* Get domain policy handle */
1126 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
1127 MAXIMUM_ALLOWED_ACCESS
,
1128 domain_sid
, &domain_pol
);
1129 if (!NT_STATUS_IS_OK(result
)) goto done
;
1131 /* Get handle on user */
1133 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
,
1135 &num_rids
, &rids
, &name_types
);
1137 if (!NT_STATUS_IS_OK(result
)) goto done
;
1139 result
= rpccli_samr_open_user(pipe_hnd
, mem_ctx
, &domain_pol
,
1140 MAXIMUM_ALLOWED_ACCESS
,
1141 rids
[0], &user_pol
);
1142 if (!NT_STATUS_IS_OK(result
)) goto done
;
1144 result
= rpccli_samr_query_usergroups(pipe_hnd
, mem_ctx
, &user_pol
,
1145 &num_rids
, &user_gids
);
1147 if (!NT_STATUS_IS_OK(result
)) goto done
;
1152 if ((rids
= TALLOC_ARRAY(mem_ctx
, uint32
, num_rids
)) == NULL
) {
1153 result
= NT_STATUS_NO_MEMORY
;
1157 for (i
= 0; i
< num_rids
; i
++)
1158 rids
[i
] = user_gids
[i
].g_rid
;
1160 result
= rpccli_samr_lookup_rids(pipe_hnd
, mem_ctx
, &domain_pol
,
1162 &num_names
, &names
, &name_types
);
1164 if (!NT_STATUS_IS_OK(result
)) {
1168 /* Display results */
1170 for (i
= 0; i
< num_names
; i
++)
1171 printf("%s\n", names
[i
]);
1178 * List a user's groups from a remote RPC server
1180 * @param argc Standard main() style argc
1181 * @param argv Standard main() style argv. Initial components are already
1184 * @return A shell status integer (0 for success)
1187 static int rpc_user_info(int argc
, const char **argv
)
1189 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_info_internals
,
1194 * List users on a remote RPC server
1196 * All parameters are provided by the run_rpc_command function, except for
1197 * argc, argv which are passes through.
1199 * @param domain_sid The domain sid acquired from the remote server
1200 * @param cli A cli_state connected to the server.
1201 * @param mem_ctx Talloc context, destoyed on completion of the function.
1202 * @param argc Standard main() style argc
1203 * @param argv Standard main() style argv. Initial components are already
1206 * @return Normal NTSTATUS return.
1209 static NTSTATUS
rpc_user_list_internals(const DOM_SID
*domain_sid
,
1210 const char *domain_name
,
1211 struct cli_state
*cli
,
1212 struct rpc_pipe_client
*pipe_hnd
,
1213 TALLOC_CTX
*mem_ctx
,
1217 POLICY_HND connect_pol
, domain_pol
;
1218 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1219 uint32 start_idx
=0, num_entries
, i
, loop_count
= 0;
1220 SAM_DISPINFO_CTR ctr
;
1221 SAM_DISPINFO_1 info1
;
1223 /* Get sam policy handle */
1225 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1227 if (!NT_STATUS_IS_OK(result
)) {
1231 /* Get domain policy handle */
1233 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
1234 MAXIMUM_ALLOWED_ACCESS
,
1235 domain_sid
, &domain_pol
);
1236 if (!NT_STATUS_IS_OK(result
)) {
1240 /* Query domain users */
1243 ctr
.sam
.info1
= &info1
;
1244 if (opt_long_list_entries
)
1245 d_printf("\nUser name Comment"\
1246 "\n-----------------------------\n");
1249 uint32 max_entries
, max_size
;
1251 get_query_dispinfo_params(
1252 loop_count
, &max_entries
, &max_size
);
1254 result
= rpccli_samr_query_dispinfo(pipe_hnd
, mem_ctx
, &domain_pol
,
1255 &start_idx
, 1, &num_entries
,
1256 max_entries
, max_size
, &ctr
);
1259 for (i
= 0; i
< num_entries
; i
++) {
1260 unistr2_to_ascii(user
, &(&ctr
.sam
.info1
->str
[i
])->uni_acct_name
, sizeof(user
)-1);
1261 if (opt_long_list_entries
)
1262 unistr2_to_ascii(desc
, &(&ctr
.sam
.info1
->str
[i
])->uni_acct_desc
, sizeof(desc
)-1);
1264 if (opt_long_list_entries
)
1265 printf("%-21.21s %s\n", user
, desc
);
1267 printf("%s\n", user
);
1269 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
1276 * 'net rpc user' entrypoint.
1277 * @param argc Standard main() style argc
1278 * @param argc Standard main() style argv. Initial components are already
1282 int net_rpc_user(int argc
, const char **argv
)
1284 struct functable func
[] = {
1285 {"add", rpc_user_add
},
1286 {"info", rpc_user_info
},
1287 {"delete", rpc_user_delete
},
1288 {"password", rpc_user_password
},
1289 {"rename", rpc_user_rename
},
1294 return run_rpc_command(NULL
,PI_SAMR
, 0,
1295 rpc_user_list_internals
,
1299 return net_run_function(argc
, argv
, func
, rpc_user_usage
);
1302 static NTSTATUS
rpc_sh_user_list(TALLOC_CTX
*mem_ctx
,
1303 struct rpc_sh_ctx
*ctx
,
1304 struct rpc_pipe_client
*pipe_hnd
,
1305 int argc
, const char **argv
)
1307 return rpc_user_list_internals(ctx
->domain_sid
, ctx
->domain_name
,
1308 ctx
->cli
, pipe_hnd
, mem_ctx
,
1312 static NTSTATUS
rpc_sh_user_info(TALLOC_CTX
*mem_ctx
,
1313 struct rpc_sh_ctx
*ctx
,
1314 struct rpc_pipe_client
*pipe_hnd
,
1315 int argc
, const char **argv
)
1317 return rpc_user_info_internals(ctx
->domain_sid
, ctx
->domain_name
,
1318 ctx
->cli
, pipe_hnd
, mem_ctx
,
1322 static NTSTATUS
rpc_sh_handle_user(TALLOC_CTX
*mem_ctx
,
1323 struct rpc_sh_ctx
*ctx
,
1324 struct rpc_pipe_client
*pipe_hnd
,
1325 int argc
, const char **argv
,
1327 TALLOC_CTX
*mem_ctx
,
1328 struct rpc_sh_ctx
*ctx
,
1329 struct rpc_pipe_client
*pipe_hnd
,
1330 const POLICY_HND
*user_hnd
,
1331 int argc
, const char **argv
))
1334 POLICY_HND connect_pol
, domain_pol
, user_pol
;
1335 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1338 enum lsa_SidType type
;
1341 d_fprintf(stderr
, "usage: %s <username>\n", ctx
->whoami
);
1342 return NT_STATUS_INVALID_PARAMETER
;
1345 ZERO_STRUCT(connect_pol
);
1346 ZERO_STRUCT(domain_pol
);
1347 ZERO_STRUCT(user_pol
);
1349 result
= net_rpc_lookup_name(mem_ctx
, pipe_hnd
->cli
, argv
[0],
1350 NULL
, NULL
, &sid
, &type
);
1351 if (!NT_STATUS_IS_OK(result
)) {
1352 d_fprintf(stderr
, "Could not lookup %s: %s\n", argv
[0],
1357 if (type
!= SID_NAME_USER
) {
1358 d_fprintf(stderr
, "%s is a %s, not a user\n", argv
[0],
1359 sid_type_lookup(type
));
1360 result
= NT_STATUS_NO_SUCH_USER
;
1364 if (!sid_peek_check_rid(ctx
->domain_sid
, &sid
, &rid
)) {
1365 d_fprintf(stderr
, "%s is not in our domain\n", argv
[0]);
1366 result
= NT_STATUS_NO_SUCH_USER
;
1370 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
,
1371 MAXIMUM_ALLOWED_ACCESS
, &connect_pol
);
1372 if (!NT_STATUS_IS_OK(result
)) {
1376 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
1377 MAXIMUM_ALLOWED_ACCESS
,
1378 ctx
->domain_sid
, &domain_pol
);
1379 if (!NT_STATUS_IS_OK(result
)) {
1383 result
= rpccli_samr_open_user(pipe_hnd
, mem_ctx
, &domain_pol
,
1384 MAXIMUM_ALLOWED_ACCESS
,
1386 if (!NT_STATUS_IS_OK(result
)) {
1390 result
= fn(mem_ctx
, ctx
, pipe_hnd
, &user_pol
, argc
-1, argv
+1);
1393 if (is_valid_policy_hnd(&user_pol
)) {
1394 rpccli_samr_close(pipe_hnd
, mem_ctx
, &user_pol
);
1396 if (is_valid_policy_hnd(&domain_pol
)) {
1397 rpccli_samr_close(pipe_hnd
, mem_ctx
, &domain_pol
);
1399 if (is_valid_policy_hnd(&connect_pol
)) {
1400 rpccli_samr_close(pipe_hnd
, mem_ctx
, &connect_pol
);
1405 static NTSTATUS
rpc_sh_user_show_internals(TALLOC_CTX
*mem_ctx
,
1406 struct rpc_sh_ctx
*ctx
,
1407 struct rpc_pipe_client
*pipe_hnd
,
1408 const POLICY_HND
*user_hnd
,
1409 int argc
, const char **argv
)
1412 SAM_USERINFO_CTR
*ctr
;
1413 SAM_USER_INFO_21
*info
;
1416 d_fprintf(stderr
, "usage: %s show <username>\n", ctx
->whoami
);
1417 return NT_STATUS_INVALID_PARAMETER
;
1420 result
= rpccli_samr_query_userinfo(pipe_hnd
, mem_ctx
, user_hnd
,
1422 if (!NT_STATUS_IS_OK(result
)) {
1426 info
= ctr
->info
.id21
;
1428 d_printf("user rid: %d, group rid: %d\n", info
->user_rid
,
1434 static NTSTATUS
rpc_sh_user_show(TALLOC_CTX
*mem_ctx
,
1435 struct rpc_sh_ctx
*ctx
,
1436 struct rpc_pipe_client
*pipe_hnd
,
1437 int argc
, const char **argv
)
1439 return rpc_sh_handle_user(mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1440 rpc_sh_user_show_internals
);
1443 #define FETCHSTR(name, rec) \
1444 do { if (strequal(ctx->thiscmd, name)) { \
1445 oldval = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_##rec); } \
1448 #define SETSTR(name, rec, flag) \
1449 do { if (strequal(ctx->thiscmd, name)) { \
1450 init_unistr2(&usr->uni_##rec, argv[0], UNI_STR_TERMINATE); \
1451 init_uni_hdr(&usr->hdr_##rec, &usr->uni_##rec); \
1452 usr->fields_present |= ACCT_##flag; } \
1455 static NTSTATUS
rpc_sh_user_str_edit_internals(TALLOC_CTX
*mem_ctx
,
1456 struct rpc_sh_ctx
*ctx
,
1457 struct rpc_pipe_client
*pipe_hnd
,
1458 const POLICY_HND
*user_hnd
,
1459 int argc
, const char **argv
)
1462 SAM_USERINFO_CTR
*ctr
;
1463 SAM_USER_INFO_21
*usr
;
1464 const char *username
;
1465 const char *oldval
= "";
1468 d_fprintf(stderr
, "usage: %s <username> [new value|NULL]\n",
1470 return NT_STATUS_INVALID_PARAMETER
;
1473 result
= rpccli_samr_query_userinfo(pipe_hnd
, mem_ctx
, user_hnd
,
1475 if (!NT_STATUS_IS_OK(result
)) {
1479 usr
= ctr
->info
.id21
;
1481 username
= rpcstr_pull_unistr2_talloc(mem_ctx
, &usr
->uni_user_name
);
1483 FETCHSTR("fullname", full_name
);
1484 FETCHSTR("homedir", home_dir
);
1485 FETCHSTR("homedrive", dir_drive
);
1486 FETCHSTR("logonscript", logon_script
);
1487 FETCHSTR("profilepath", profile_path
);
1488 FETCHSTR("description", acct_desc
);
1491 d_printf("%s's %s: [%s]\n", username
, ctx
->thiscmd
, oldval
);
1497 if (strcmp(argv
[0], "NULL") == 0) {
1501 SETSTR("fullname", full_name
, FULL_NAME
);
1502 SETSTR("homedir", home_dir
, HOME_DIR
);
1503 SETSTR("homedrive", dir_drive
, HOME_DRIVE
);
1504 SETSTR("logonscript", logon_script
, LOGON_SCRIPT
);
1505 SETSTR("profilepath", profile_path
, PROFILE
);
1506 SETSTR("description", acct_desc
, DESCRIPTION
);
1508 result
= rpccli_samr_set_userinfo2(
1509 pipe_hnd
, mem_ctx
, user_hnd
, 21,
1510 &pipe_hnd
->cli
->user_session_key
, ctr
);
1512 d_printf("Set %s's %s from [%s] to [%s]\n", username
,
1513 ctx
->thiscmd
, oldval
, argv
[0]);
1520 #define HANDLEFLG(name, rec) \
1521 do { if (strequal(ctx->thiscmd, name)) { \
1522 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1524 newflags = oldflags | ACB_##rec; \
1526 newflags = oldflags & ~ACB_##rec; \
1529 static NTSTATUS
rpc_sh_user_str_edit(TALLOC_CTX
*mem_ctx
,
1530 struct rpc_sh_ctx
*ctx
,
1531 struct rpc_pipe_client
*pipe_hnd
,
1532 int argc
, const char **argv
)
1534 return rpc_sh_handle_user(mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1535 rpc_sh_user_str_edit_internals
);
1538 static NTSTATUS
rpc_sh_user_flag_edit_internals(TALLOC_CTX
*mem_ctx
,
1539 struct rpc_sh_ctx
*ctx
,
1540 struct rpc_pipe_client
*pipe_hnd
,
1541 const POLICY_HND
*user_hnd
,
1542 int argc
, const char **argv
)
1545 SAM_USERINFO_CTR
*ctr
;
1546 SAM_USER_INFO_21
*usr
;
1547 const char *username
;
1548 const char *oldval
= "unknown";
1549 uint32 oldflags
, newflags
;
1553 ((argc
== 1) && !strequal(argv
[0], "yes") &&
1554 !strequal(argv
[0], "no"))) {
1555 d_fprintf(stderr
, "usage: %s <username> [yes|no]\n",
1557 return NT_STATUS_INVALID_PARAMETER
;
1560 newval
= strequal(argv
[0], "yes");
1562 result
= rpccli_samr_query_userinfo(pipe_hnd
, mem_ctx
, user_hnd
,
1564 if (!NT_STATUS_IS_OK(result
)) {
1568 usr
= ctr
->info
.id21
;
1570 username
= rpcstr_pull_unistr2_talloc(mem_ctx
, &usr
->uni_user_name
);
1571 oldflags
= usr
->acb_info
;
1572 newflags
= usr
->acb_info
;
1574 HANDLEFLG("disabled", DISABLED
);
1575 HANDLEFLG("pwnotreq", PWNOTREQ
);
1576 HANDLEFLG("autolock", AUTOLOCK
);
1577 HANDLEFLG("pwnoexp", PWNOEXP
);
1580 d_printf("%s's %s flag: %s\n", username
, ctx
->thiscmd
, oldval
);
1586 usr
->acb_info
= newflags
;
1587 usr
->fields_present
= ACCT_FLAGS
;
1589 result
= rpccli_samr_set_userinfo2(
1590 pipe_hnd
, mem_ctx
, user_hnd
, 21,
1591 &pipe_hnd
->cli
->user_session_key
, ctr
);
1593 if (NT_STATUS_IS_OK(result
)) {
1594 d_printf("Set %s's %s flag from [%s] to [%s]\n", username
,
1595 ctx
->thiscmd
, oldval
, argv
[0]);
1603 static NTSTATUS
rpc_sh_user_flag_edit(TALLOC_CTX
*mem_ctx
,
1604 struct rpc_sh_ctx
*ctx
,
1605 struct rpc_pipe_client
*pipe_hnd
,
1606 int argc
, const char **argv
)
1608 return rpc_sh_handle_user(mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1609 rpc_sh_user_flag_edit_internals
);
1612 struct rpc_sh_cmd
*net_rpc_user_edit_cmds(TALLOC_CTX
*mem_ctx
,
1613 struct rpc_sh_ctx
*ctx
)
1615 static struct rpc_sh_cmd cmds
[] = {
1617 { "fullname", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1618 "Show/Set a user's full name" },
1620 { "homedir", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1621 "Show/Set a user's home directory" },
1623 { "homedrive", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1624 "Show/Set a user's home drive" },
1626 { "logonscript", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1627 "Show/Set a user's logon script" },
1629 { "profilepath", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1630 "Show/Set a user's profile path" },
1632 { "description", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1633 "Show/Set a user's description" },
1635 { "disabled", NULL
, PI_SAMR
, rpc_sh_user_flag_edit
,
1636 "Show/Set whether a user is disabled" },
1638 { "autolock", NULL
, PI_SAMR
, rpc_sh_user_flag_edit
,
1639 "Show/Set whether a user locked out" },
1641 { "pwnotreq", NULL
, PI_SAMR
, rpc_sh_user_flag_edit
,
1642 "Show/Set whether a user does not need a password" },
1644 { "pwnoexp", NULL
, PI_SAMR
, rpc_sh_user_flag_edit
,
1645 "Show/Set whether a user's password does not expire" },
1647 { NULL
, NULL
, 0, NULL
, NULL
}
1653 struct rpc_sh_cmd
*net_rpc_user_cmds(TALLOC_CTX
*mem_ctx
,
1654 struct rpc_sh_ctx
*ctx
)
1656 static struct rpc_sh_cmd cmds
[] = {
1658 { "list", NULL
, PI_SAMR
, rpc_sh_user_list
,
1659 "List available users" },
1661 { "info", NULL
, PI_SAMR
, rpc_sh_user_info
,
1662 "List the domain groups a user is member of" },
1664 { "show", NULL
, PI_SAMR
, rpc_sh_user_show
,
1665 "Show info about a user" },
1667 { "edit", net_rpc_user_edit_cmds
, 0, NULL
,
1668 "Show/Modify a user's fields" },
1670 { NULL
, NULL
, 0, NULL
, NULL
}
1676 /****************************************************************************/
1679 * Basic usage function for 'net rpc group'
1680 * @param argc Standard main() style argc.
1681 * @param argv Standard main() style argv. Initial components are already
1685 static int rpc_group_usage(int argc
, const char **argv
)
1687 return net_help_group(argc
, argv
);
1691 * Delete group on a remote RPC server
1693 * All parameters are provided by the run_rpc_command function, except for
1694 * argc, argv which are passes through.
1696 * @param domain_sid The domain sid acquired from the remote server
1697 * @param cli A cli_state connected to the server.
1698 * @param mem_ctx Talloc context, destoyed on completion of the function.
1699 * @param argc Standard main() style argc
1700 * @param argv Standard main() style argv. Initial components are already
1703 * @return Normal NTSTATUS return.
1706 static NTSTATUS
rpc_group_delete_internals(const DOM_SID
*domain_sid
,
1707 const char *domain_name
,
1708 struct cli_state
*cli
,
1709 struct rpc_pipe_client
*pipe_hnd
,
1710 TALLOC_CTX
*mem_ctx
,
1714 POLICY_HND connect_pol
, domain_pol
, group_pol
, user_pol
;
1715 BOOL group_is_primary
= False
;
1716 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1718 uint32
*group_rids
, num_rids
, *name_types
, num_members
,
1719 *group_attrs
, group_rid
;
1720 uint32 flags
= 0x000003e8; /* Unknown */
1723 /* DOM_GID *user_gids; */
1724 SAM_USERINFO_CTR
*user_ctr
;
1728 d_printf("specify group\n");
1729 rpc_group_usage(argc
,argv
);
1730 return NT_STATUS_OK
; /* ok? */
1733 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1736 if (!NT_STATUS_IS_OK(result
)) {
1737 d_fprintf(stderr
, "Request samr_connect failed\n");
1741 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
1742 MAXIMUM_ALLOWED_ACCESS
,
1743 domain_sid
, &domain_pol
);
1745 if (!NT_STATUS_IS_OK(result
)) {
1746 d_fprintf(stderr
, "Request open_domain failed\n");
1750 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
,
1752 &num_rids
, &group_rids
,
1755 if (!NT_STATUS_IS_OK(result
)) {
1756 d_fprintf(stderr
, "Lookup of '%s' failed\n",argv
[0]);
1760 switch (name_types
[0])
1762 case SID_NAME_DOM_GRP
:
1763 result
= rpccli_samr_open_group(pipe_hnd
, mem_ctx
, &domain_pol
,
1764 MAXIMUM_ALLOWED_ACCESS
,
1765 group_rids
[0], &group_pol
);
1766 if (!NT_STATUS_IS_OK(result
)) {
1767 d_fprintf(stderr
, "Request open_group failed");
1771 group_rid
= group_rids
[0];
1773 result
= rpccli_samr_query_groupmem(pipe_hnd
, mem_ctx
, &group_pol
,
1774 &num_members
, &group_rids
,
1777 if (!NT_STATUS_IS_OK(result
)) {
1778 d_fprintf(stderr
, "Unable to query group members of %s",argv
[0]);
1783 d_printf("Domain Group %s (rid: %d) has %d members\n",
1784 argv
[0],group_rid
,num_members
);
1787 /* Check if group is anyone's primary group */
1788 for (i
= 0; i
< num_members
; i
++)
1790 result
= rpccli_samr_open_user(pipe_hnd
, mem_ctx
, &domain_pol
,
1791 MAXIMUM_ALLOWED_ACCESS
,
1792 group_rids
[i
], &user_pol
);
1794 if (!NT_STATUS_IS_OK(result
)) {
1795 d_fprintf(stderr
, "Unable to open group member %d\n",group_rids
[i
]);
1799 ZERO_STRUCT(user_ctr
);
1801 result
= rpccli_samr_query_userinfo(pipe_hnd
, mem_ctx
, &user_pol
,
1804 if (!NT_STATUS_IS_OK(result
)) {
1805 d_fprintf(stderr
, "Unable to lookup userinfo for group member %d\n",group_rids
[i
]);
1809 if (user_ctr
->info
.id21
->group_rid
== group_rid
) {
1810 unistr2_to_ascii(temp
, &(user_ctr
->info
.id21
)->uni_user_name
,
1813 d_printf("Group is primary group of %s\n",temp
);
1814 group_is_primary
= True
;
1817 rpccli_samr_close(pipe_hnd
, mem_ctx
, &user_pol
);
1820 if (group_is_primary
) {
1821 d_fprintf(stderr
, "Unable to delete group because some "
1822 "of it's members have it as primary group\n");
1823 result
= NT_STATUS_MEMBERS_PRIMARY_GROUP
;
1827 /* remove all group members */
1828 for (i
= 0; i
< num_members
; i
++)
1831 d_printf("Remove group member %d...",group_rids
[i
]);
1832 result
= rpccli_samr_del_groupmem(pipe_hnd
, mem_ctx
, &group_pol
, group_rids
[i
]);
1834 if (NT_STATUS_IS_OK(result
)) {
1839 d_printf("failed\n");
1844 result
= rpccli_samr_delete_dom_group(pipe_hnd
, mem_ctx
, &group_pol
);
1847 /* removing a local group is easier... */
1848 case SID_NAME_ALIAS
:
1849 result
= rpccli_samr_open_alias(pipe_hnd
, mem_ctx
, &domain_pol
,
1850 MAXIMUM_ALLOWED_ACCESS
,
1851 group_rids
[0], &group_pol
);
1853 if (!NT_STATUS_IS_OK(result
)) {
1854 d_fprintf(stderr
, "Request open_alias failed\n");
1858 result
= rpccli_samr_delete_dom_alias(pipe_hnd
, mem_ctx
, &group_pol
);
1861 d_fprintf(stderr
, "%s is of type %s. This command is only for deleting local or global groups\n",
1862 argv
[0],sid_type_lookup(name_types
[0]));
1863 result
= NT_STATUS_UNSUCCESSFUL
;
1868 if (NT_STATUS_IS_OK(result
)) {
1870 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types
[0]),argv
[0]);
1872 d_fprintf(stderr
, "Deleting of %s failed: %s\n",argv
[0],
1873 get_friendly_nt_error_msg(result
));
1881 static int rpc_group_delete(int argc
, const char **argv
)
1883 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_group_delete_internals
,
1887 static NTSTATUS
rpc_group_add_internals(const DOM_SID
*domain_sid
,
1888 const char *domain_name
,
1889 struct cli_state
*cli
,
1890 struct rpc_pipe_client
*pipe_hnd
,
1891 TALLOC_CTX
*mem_ctx
,
1895 POLICY_HND connect_pol
, domain_pol
, group_pol
;
1896 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1897 GROUP_INFO_CTR group_info
;
1900 d_printf("Group name must be specified\n");
1901 rpc_group_usage(argc
, argv
);
1902 return NT_STATUS_OK
;
1905 /* Get sam policy handle */
1907 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1909 if (!NT_STATUS_IS_OK(result
)) goto done
;
1911 /* Get domain policy handle */
1913 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
1914 MAXIMUM_ALLOWED_ACCESS
,
1915 domain_sid
, &domain_pol
);
1916 if (!NT_STATUS_IS_OK(result
)) goto done
;
1918 /* Create the group */
1920 result
= rpccli_samr_create_dom_group(pipe_hnd
, mem_ctx
, &domain_pol
,
1921 argv
[0], MAXIMUM_ALLOWED_ACCESS
,
1923 if (!NT_STATUS_IS_OK(result
)) goto done
;
1925 if (strlen(opt_comment
) == 0) goto done
;
1927 /* We've got a comment to set */
1929 group_info
.switch_value1
= 4;
1930 init_samr_group_info4(&group_info
.group
.info4
, opt_comment
);
1932 result
= rpccli_samr_set_groupinfo(pipe_hnd
, mem_ctx
, &group_pol
, &group_info
);
1933 if (!NT_STATUS_IS_OK(result
)) goto done
;
1936 if (NT_STATUS_IS_OK(result
))
1937 DEBUG(5, ("add group succeeded\n"));
1939 d_fprintf(stderr
, "add group failed: %s\n", nt_errstr(result
));
1944 static NTSTATUS
rpc_alias_add_internals(const DOM_SID
*domain_sid
,
1945 const char *domain_name
,
1946 struct cli_state
*cli
,
1947 struct rpc_pipe_client
*pipe_hnd
,
1948 TALLOC_CTX
*mem_ctx
,
1952 POLICY_HND connect_pol
, domain_pol
, alias_pol
;
1953 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1954 ALIAS_INFO_CTR alias_info
;
1957 d_printf("Alias name must be specified\n");
1958 rpc_group_usage(argc
, argv
);
1959 return NT_STATUS_OK
;
1962 /* Get sam policy handle */
1964 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
1966 if (!NT_STATUS_IS_OK(result
)) goto done
;
1968 /* Get domain policy handle */
1970 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
1971 MAXIMUM_ALLOWED_ACCESS
,
1972 domain_sid
, &domain_pol
);
1973 if (!NT_STATUS_IS_OK(result
)) goto done
;
1975 /* Create the group */
1977 result
= rpccli_samr_create_dom_alias(pipe_hnd
, mem_ctx
, &domain_pol
,
1978 argv
[0], &alias_pol
);
1979 if (!NT_STATUS_IS_OK(result
)) goto done
;
1981 if (strlen(opt_comment
) == 0) goto done
;
1983 /* We've got a comment to set */
1985 alias_info
.level
= 3;
1986 init_samr_alias_info3(&alias_info
.alias
.info3
, opt_comment
);
1988 result
= rpccli_samr_set_aliasinfo(pipe_hnd
, mem_ctx
, &alias_pol
, &alias_info
);
1989 if (!NT_STATUS_IS_OK(result
)) goto done
;
1992 if (NT_STATUS_IS_OK(result
))
1993 DEBUG(5, ("add alias succeeded\n"));
1995 d_fprintf(stderr
, "add alias failed: %s\n", nt_errstr(result
));
2000 static int rpc_group_add(int argc
, const char **argv
)
2003 return run_rpc_command(NULL
, PI_SAMR
, 0,
2004 rpc_alias_add_internals
,
2007 return run_rpc_command(NULL
, PI_SAMR
, 0,
2008 rpc_group_add_internals
,
2012 static NTSTATUS
get_sid_from_name(struct cli_state
*cli
,
2013 TALLOC_CTX
*mem_ctx
,
2016 enum lsa_SidType
*type
)
2018 DOM_SID
*sids
= NULL
;
2019 enum lsa_SidType
*types
= NULL
;
2020 struct rpc_pipe_client
*pipe_hnd
;
2022 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2024 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &result
);
2029 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, False
,
2030 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
2032 if (!NT_STATUS_IS_OK(result
)) {
2036 result
= rpccli_lsa_lookup_names(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
2037 &name
, NULL
, &sids
, &types
);
2039 if (NT_STATUS_IS_OK(result
)) {
2040 sid_copy(sid
, &sids
[0]);
2044 rpccli_lsa_close(pipe_hnd
, mem_ctx
, &lsa_pol
);
2048 cli_rpc_pipe_close(pipe_hnd
);
2051 if (!NT_STATUS_IS_OK(result
) && (StrnCaseCmp(name
, "S-", 2) == 0)) {
2053 /* Try as S-1-5-whatever */
2057 if (string_to_sid(&tmp_sid
, name
)) {
2058 sid_copy(sid
, &tmp_sid
);
2059 *type
= SID_NAME_UNKNOWN
;
2060 result
= NT_STATUS_OK
;
2067 static NTSTATUS
rpc_add_groupmem(struct rpc_pipe_client
*pipe_hnd
,
2068 TALLOC_CTX
*mem_ctx
,
2069 const DOM_SID
*group_sid
,
2072 POLICY_HND connect_pol
, domain_pol
;
2075 POLICY_HND group_pol
;
2078 uint32
*rids
= NULL
;
2079 uint32
*rid_types
= NULL
;
2083 sid_copy(&sid
, group_sid
);
2085 if (!sid_split_rid(&sid
, &group_rid
)) {
2086 return NT_STATUS_UNSUCCESSFUL
;
2089 /* Get sam policy handle */
2090 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
2092 if (!NT_STATUS_IS_OK(result
)) {
2096 /* Get domain policy handle */
2097 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
2098 MAXIMUM_ALLOWED_ACCESS
,
2100 if (!NT_STATUS_IS_OK(result
)) {
2104 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
, 1000,
2106 &num_rids
, &rids
, &rid_types
);
2108 if (!NT_STATUS_IS_OK(result
)) {
2109 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2113 result
= rpccli_samr_open_group(pipe_hnd
, mem_ctx
, &domain_pol
,
2114 MAXIMUM_ALLOWED_ACCESS
,
2115 group_rid
, &group_pol
);
2117 if (!NT_STATUS_IS_OK(result
)) {
2121 result
= rpccli_samr_add_groupmem(pipe_hnd
, mem_ctx
, &group_pol
, rids
[0]);
2124 rpccli_samr_close(pipe_hnd
, mem_ctx
, &connect_pol
);
2128 static NTSTATUS
rpc_add_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2129 TALLOC_CTX
*mem_ctx
,
2130 const DOM_SID
*alias_sid
,
2133 POLICY_HND connect_pol
, domain_pol
;
2136 POLICY_HND alias_pol
;
2139 enum lsa_SidType member_type
;
2143 sid_copy(&sid
, alias_sid
);
2145 if (!sid_split_rid(&sid
, &alias_rid
)) {
2146 return NT_STATUS_UNSUCCESSFUL
;
2149 result
= get_sid_from_name(pipe_hnd
->cli
, mem_ctx
, member
,
2150 &member_sid
, &member_type
);
2152 if (!NT_STATUS_IS_OK(result
)) {
2153 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2157 /* Get sam policy handle */
2158 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
2160 if (!NT_STATUS_IS_OK(result
)) {
2164 /* Get domain policy handle */
2165 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
2166 MAXIMUM_ALLOWED_ACCESS
,
2168 if (!NT_STATUS_IS_OK(result
)) {
2172 result
= rpccli_samr_open_alias(pipe_hnd
, mem_ctx
, &domain_pol
,
2173 MAXIMUM_ALLOWED_ACCESS
,
2174 alias_rid
, &alias_pol
);
2176 if (!NT_STATUS_IS_OK(result
)) {
2180 result
= rpccli_samr_add_aliasmem(pipe_hnd
, mem_ctx
, &alias_pol
, &member_sid
);
2182 if (!NT_STATUS_IS_OK(result
)) {
2187 rpccli_samr_close(pipe_hnd
, mem_ctx
, &connect_pol
);
2191 static NTSTATUS
rpc_group_addmem_internals(const DOM_SID
*domain_sid
,
2192 const char *domain_name
,
2193 struct cli_state
*cli
,
2194 struct rpc_pipe_client
*pipe_hnd
,
2195 TALLOC_CTX
*mem_ctx
,
2200 enum lsa_SidType group_type
;
2203 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2204 return NT_STATUS_UNSUCCESSFUL
;
2207 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2208 &group_sid
, &group_type
))) {
2209 d_fprintf(stderr
, "Could not lookup group name %s\n", argv
[0]);
2210 return NT_STATUS_UNSUCCESSFUL
;
2213 if (group_type
== SID_NAME_DOM_GRP
) {
2214 NTSTATUS result
= rpc_add_groupmem(pipe_hnd
, mem_ctx
,
2215 &group_sid
, argv
[1]);
2217 if (!NT_STATUS_IS_OK(result
)) {
2218 d_fprintf(stderr
, "Could not add %s to %s: %s\n",
2219 argv
[1], argv
[0], nt_errstr(result
));
2224 if (group_type
== SID_NAME_ALIAS
) {
2225 NTSTATUS result
= rpc_add_aliasmem(pipe_hnd
, mem_ctx
,
2226 &group_sid
, argv
[1]);
2228 if (!NT_STATUS_IS_OK(result
)) {
2229 d_fprintf(stderr
, "Could not add %s to %s: %s\n",
2230 argv
[1], argv
[0], nt_errstr(result
));
2235 d_fprintf(stderr
, "Can only add members to global or local groups "
2236 "which %s is not\n", argv
[0]);
2238 return NT_STATUS_UNSUCCESSFUL
;
2241 static int rpc_group_addmem(int argc
, const char **argv
)
2243 return run_rpc_command(NULL
, PI_SAMR
, 0,
2244 rpc_group_addmem_internals
,
2248 static NTSTATUS
rpc_del_groupmem(struct rpc_pipe_client
*pipe_hnd
,
2249 TALLOC_CTX
*mem_ctx
,
2250 const DOM_SID
*group_sid
,
2253 POLICY_HND connect_pol
, domain_pol
;
2256 POLICY_HND group_pol
;
2259 uint32
*rids
= NULL
;
2260 uint32
*rid_types
= NULL
;
2264 sid_copy(&sid
, group_sid
);
2266 if (!sid_split_rid(&sid
, &group_rid
))
2267 return NT_STATUS_UNSUCCESSFUL
;
2269 /* Get sam policy handle */
2270 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
2272 if (!NT_STATUS_IS_OK(result
))
2275 /* Get domain policy handle */
2276 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
2277 MAXIMUM_ALLOWED_ACCESS
,
2279 if (!NT_STATUS_IS_OK(result
))
2282 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
, 1000,
2284 &num_rids
, &rids
, &rid_types
);
2286 if (!NT_STATUS_IS_OK(result
)) {
2287 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2291 result
= rpccli_samr_open_group(pipe_hnd
, mem_ctx
, &domain_pol
,
2292 MAXIMUM_ALLOWED_ACCESS
,
2293 group_rid
, &group_pol
);
2295 if (!NT_STATUS_IS_OK(result
))
2298 result
= rpccli_samr_del_groupmem(pipe_hnd
, mem_ctx
, &group_pol
, rids
[0]);
2301 rpccli_samr_close(pipe_hnd
, mem_ctx
, &connect_pol
);
2305 static NTSTATUS
rpc_del_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2306 TALLOC_CTX
*mem_ctx
,
2307 const DOM_SID
*alias_sid
,
2310 POLICY_HND connect_pol
, domain_pol
;
2313 POLICY_HND alias_pol
;
2316 enum lsa_SidType member_type
;
2320 sid_copy(&sid
, alias_sid
);
2322 if (!sid_split_rid(&sid
, &alias_rid
))
2323 return NT_STATUS_UNSUCCESSFUL
;
2325 result
= get_sid_from_name(pipe_hnd
->cli
, mem_ctx
, member
,
2326 &member_sid
, &member_type
);
2328 if (!NT_STATUS_IS_OK(result
)) {
2329 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2333 /* Get sam policy handle */
2334 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
2336 if (!NT_STATUS_IS_OK(result
)) {
2340 /* Get domain policy handle */
2341 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
2342 MAXIMUM_ALLOWED_ACCESS
,
2344 if (!NT_STATUS_IS_OK(result
)) {
2348 result
= rpccli_samr_open_alias(pipe_hnd
, mem_ctx
, &domain_pol
,
2349 MAXIMUM_ALLOWED_ACCESS
,
2350 alias_rid
, &alias_pol
);
2352 if (!NT_STATUS_IS_OK(result
))
2355 result
= rpccli_samr_del_aliasmem(pipe_hnd
, mem_ctx
, &alias_pol
, &member_sid
);
2357 if (!NT_STATUS_IS_OK(result
))
2361 rpccli_samr_close(pipe_hnd
, mem_ctx
, &connect_pol
);
2365 static NTSTATUS
rpc_group_delmem_internals(const DOM_SID
*domain_sid
,
2366 const char *domain_name
,
2367 struct cli_state
*cli
,
2368 struct rpc_pipe_client
*pipe_hnd
,
2369 TALLOC_CTX
*mem_ctx
,
2374 enum lsa_SidType group_type
;
2377 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2378 return NT_STATUS_UNSUCCESSFUL
;
2381 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2382 &group_sid
, &group_type
))) {
2383 d_fprintf(stderr
, "Could not lookup group name %s\n", argv
[0]);
2384 return NT_STATUS_UNSUCCESSFUL
;
2387 if (group_type
== SID_NAME_DOM_GRP
) {
2388 NTSTATUS result
= rpc_del_groupmem(pipe_hnd
, mem_ctx
,
2389 &group_sid
, argv
[1]);
2391 if (!NT_STATUS_IS_OK(result
)) {
2392 d_fprintf(stderr
, "Could not del %s from %s: %s\n",
2393 argv
[1], argv
[0], nt_errstr(result
));
2398 if (group_type
== SID_NAME_ALIAS
) {
2399 NTSTATUS result
= rpc_del_aliasmem(pipe_hnd
, mem_ctx
,
2400 &group_sid
, argv
[1]);
2402 if (!NT_STATUS_IS_OK(result
)) {
2403 d_fprintf(stderr
, "Could not del %s from %s: %s\n",
2404 argv
[1], argv
[0], nt_errstr(result
));
2409 d_fprintf(stderr
, "Can only delete members from global or local groups "
2410 "which %s is not\n", argv
[0]);
2412 return NT_STATUS_UNSUCCESSFUL
;
2415 static int rpc_group_delmem(int argc
, const char **argv
)
2417 return run_rpc_command(NULL
, PI_SAMR
, 0,
2418 rpc_group_delmem_internals
,
2423 * List groups on a remote RPC server
2425 * All parameters are provided by the run_rpc_command function, except for
2426 * argc, argv which are passes through.
2428 * @param domain_sid The domain sid acquired from the remote server
2429 * @param cli A cli_state connected to the server.
2430 * @param mem_ctx Talloc context, destoyed on completion of the function.
2431 * @param argc Standard main() style argc
2432 * @param argv Standard main() style argv. Initial components are already
2435 * @return Normal NTSTATUS return.
2438 static NTSTATUS
rpc_group_list_internals(const DOM_SID
*domain_sid
,
2439 const char *domain_name
,
2440 struct cli_state
*cli
,
2441 struct rpc_pipe_client
*pipe_hnd
,
2442 TALLOC_CTX
*mem_ctx
,
2446 POLICY_HND connect_pol
, domain_pol
;
2447 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2448 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
2449 struct acct_info
*groups
;
2450 BOOL global
= False
;
2452 BOOL builtin
= False
;
2460 for (i
=0; i
<argc
; i
++) {
2461 if (strequal(argv
[i
], "global"))
2464 if (strequal(argv
[i
], "local"))
2467 if (strequal(argv
[i
], "builtin"))
2471 /* Get sam policy handle */
2473 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
2475 if (!NT_STATUS_IS_OK(result
)) {
2479 /* Get domain policy handle */
2481 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
2482 MAXIMUM_ALLOWED_ACCESS
,
2483 domain_sid
, &domain_pol
);
2484 if (!NT_STATUS_IS_OK(result
)) {
2488 /* Query domain groups */
2489 if (opt_long_list_entries
)
2490 d_printf("\nGroup name Comment"\
2491 "\n-----------------------------\n");
2493 SAM_DISPINFO_CTR ctr
;
2494 SAM_DISPINFO_3 info3
;
2499 ctr
.sam
.info3
= &info3
;
2503 get_query_dispinfo_params(
2504 loop_count
, &max_entries
, &max_size
);
2506 result
= rpccli_samr_query_dispinfo(pipe_hnd
, mem_ctx
, &domain_pol
,
2507 &start_idx
, 3, &num_entries
,
2508 max_entries
, max_size
, &ctr
);
2510 if (!NT_STATUS_IS_OK(result
) &&
2511 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2514 for (i
= 0; i
< num_entries
; i
++) {
2516 fstring group
, desc
;
2518 unistr2_to_ascii(group
, &(&ctr
.sam
.info3
->str
[i
])->uni_grp_name
, sizeof(group
)-1);
2519 unistr2_to_ascii(desc
, &(&ctr
.sam
.info3
->str
[i
])->uni_grp_desc
, sizeof(desc
)-1);
2521 if (opt_long_list_entries
)
2522 printf("%-21.21s %-50.50s\n",
2525 printf("%s\n", group
);
2527 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2528 /* query domain aliases */
2533 /* The max_size field in cli_samr_enum_als_groups is more like
2534 * an account_control field with indiviual bits what to
2535 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2536 * everything. I'm too lazy (sorry) to get this through to
2537 * rpc_parse/ etc. Volker */
2539 result
= rpccli_samr_enum_als_groups(pipe_hnd
, mem_ctx
, &domain_pol
,
2541 &groups
, &num_entries
);
2543 if (!NT_STATUS_IS_OK(result
) &&
2544 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2547 for (i
= 0; i
< num_entries
; i
++) {
2549 char *description
= NULL
;
2551 if (opt_long_list_entries
) {
2553 POLICY_HND alias_pol
;
2556 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd
, mem_ctx
,
2561 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd
, mem_ctx
,
2564 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd
, mem_ctx
,
2566 description
= unistr2_tdup(mem_ctx
,
2567 ctr
.alias
.info3
.description
.string
);
2571 if (description
!= NULL
) {
2572 printf("%-21.21s %-50.50s\n",
2573 groups
[i
].acct_name
,
2576 printf("%s\n", groups
[i
].acct_name
);
2579 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2580 rpccli_samr_close(pipe_hnd
, mem_ctx
, &domain_pol
);
2581 /* Get builtin policy handle */
2583 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
2584 MAXIMUM_ALLOWED_ACCESS
,
2585 &global_sid_Builtin
, &domain_pol
);
2586 if (!NT_STATUS_IS_OK(result
)) {
2589 /* query builtin aliases */
2592 if (!builtin
) break;
2594 result
= rpccli_samr_enum_als_groups(pipe_hnd
, mem_ctx
, &domain_pol
,
2595 &start_idx
, max_entries
,
2596 &groups
, &num_entries
);
2598 if (!NT_STATUS_IS_OK(result
) &&
2599 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2602 for (i
= 0; i
< num_entries
; i
++) {
2604 char *description
= NULL
;
2606 if (opt_long_list_entries
) {
2608 POLICY_HND alias_pol
;
2611 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd
, mem_ctx
,
2616 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd
, mem_ctx
,
2619 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd
, mem_ctx
,
2621 description
= unistr2_tdup(mem_ctx
,
2622 ctr
.alias
.info3
.description
.string
);
2626 if (description
!= NULL
) {
2627 printf("%-21.21s %-50.50s\n",
2628 groups
[i
].acct_name
,
2631 printf("%s\n", groups
[i
].acct_name
);
2634 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2640 static int rpc_group_list(int argc
, const char **argv
)
2642 return run_rpc_command(NULL
, PI_SAMR
, 0,
2643 rpc_group_list_internals
,
2647 static NTSTATUS
rpc_list_group_members(struct rpc_pipe_client
*pipe_hnd
,
2648 TALLOC_CTX
*mem_ctx
,
2649 const char *domain_name
,
2650 const DOM_SID
*domain_sid
,
2651 POLICY_HND
*domain_pol
,
2655 POLICY_HND group_pol
;
2656 uint32 num_members
, *group_rids
, *group_attrs
;
2663 sid_to_string(sid_str
, domain_sid
);
2665 result
= rpccli_samr_open_group(pipe_hnd
, mem_ctx
, domain_pol
,
2666 MAXIMUM_ALLOWED_ACCESS
,
2669 if (!NT_STATUS_IS_OK(result
))
2672 result
= rpccli_samr_query_groupmem(pipe_hnd
, mem_ctx
, &group_pol
,
2673 &num_members
, &group_rids
,
2676 if (!NT_STATUS_IS_OK(result
))
2679 while (num_members
> 0) {
2680 int this_time
= 512;
2682 if (num_members
< this_time
)
2683 this_time
= num_members
;
2685 result
= rpccli_samr_lookup_rids(pipe_hnd
, mem_ctx
, domain_pol
,
2686 this_time
, group_rids
,
2687 &num_names
, &names
, &name_types
);
2689 if (!NT_STATUS_IS_OK(result
))
2692 /* We only have users as members, but make the output
2693 the same as the output of alias members */
2695 for (i
= 0; i
< this_time
; i
++) {
2697 if (opt_long_list_entries
) {
2698 printf("%s-%d %s\\%s %d\n", sid_str
,
2699 group_rids
[i
], domain_name
, names
[i
],
2702 printf("%s\\%s\n", domain_name
, names
[i
]);
2706 num_members
-= this_time
;
2710 return NT_STATUS_OK
;
2713 static NTSTATUS
rpc_list_alias_members(struct rpc_pipe_client
*pipe_hnd
,
2714 TALLOC_CTX
*mem_ctx
,
2715 POLICY_HND
*domain_pol
,
2719 struct rpc_pipe_client
*lsa_pipe
;
2720 POLICY_HND alias_pol
, lsa_pol
;
2722 DOM_SID
*alias_sids
;
2725 enum lsa_SidType
*types
;
2728 result
= rpccli_samr_open_alias(pipe_hnd
, mem_ctx
, domain_pol
,
2729 MAXIMUM_ALLOWED_ACCESS
, rid
, &alias_pol
);
2731 if (!NT_STATUS_IS_OK(result
))
2734 result
= rpccli_samr_query_aliasmem(pipe_hnd
, mem_ctx
, &alias_pol
,
2735 &num_members
, &alias_sids
);
2737 if (!NT_STATUS_IS_OK(result
)) {
2738 d_fprintf(stderr
, "Couldn't list alias members\n");
2742 if (num_members
== 0) {
2743 return NT_STATUS_OK
;
2746 lsa_pipe
= cli_rpc_pipe_open_noauth(pipe_hnd
->cli
, PI_LSARPC
, &result
);
2748 d_fprintf(stderr
, "Couldn't open LSA pipe. Error was %s\n",
2749 nt_errstr(result
) );
2753 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, True
,
2754 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
2756 if (!NT_STATUS_IS_OK(result
)) {
2757 d_fprintf(stderr
, "Couldn't open LSA policy handle\n");
2758 cli_rpc_pipe_close(lsa_pipe
);
2762 result
= rpccli_lsa_lookup_sids(lsa_pipe
, mem_ctx
, &lsa_pol
, num_members
,
2764 &domains
, &names
, &types
);
2766 if (!NT_STATUS_IS_OK(result
) &&
2767 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
2768 d_fprintf(stderr
, "Couldn't lookup SIDs\n");
2769 cli_rpc_pipe_close(lsa_pipe
);
2773 for (i
= 0; i
< num_members
; i
++) {
2775 sid_to_string(sid_str
, &alias_sids
[i
]);
2777 if (opt_long_list_entries
) {
2778 printf("%s %s\\%s %d\n", sid_str
,
2779 domains
[i
] ? domains
[i
] : "*unknown*",
2780 names
[i
] ? names
[i
] : "*unknown*", types
[i
]);
2783 printf("%s\\%s\n", domains
[i
], names
[i
]);
2785 printf("%s\n", sid_str
);
2789 cli_rpc_pipe_close(lsa_pipe
);
2790 return NT_STATUS_OK
;
2793 static NTSTATUS
rpc_group_members_internals(const DOM_SID
*domain_sid
,
2794 const char *domain_name
,
2795 struct cli_state
*cli
,
2796 struct rpc_pipe_client
*pipe_hnd
,
2797 TALLOC_CTX
*mem_ctx
,
2802 POLICY_HND connect_pol
, domain_pol
;
2803 uint32 num_rids
, *rids
, *rid_types
;
2805 /* Get sam policy handle */
2807 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
2810 if (!NT_STATUS_IS_OK(result
))
2813 /* Get domain policy handle */
2815 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
2816 MAXIMUM_ALLOWED_ACCESS
,
2817 domain_sid
, &domain_pol
);
2819 if (!NT_STATUS_IS_OK(result
))
2822 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
, 1000,
2823 1, argv
, &num_rids
, &rids
, &rid_types
);
2825 if (!NT_STATUS_IS_OK(result
)) {
2827 /* Ok, did not find it in the global sam, try with builtin */
2829 DOM_SID sid_Builtin
;
2831 rpccli_samr_close(pipe_hnd
, mem_ctx
, &domain_pol
);
2833 string_to_sid(&sid_Builtin
, "S-1-5-32");
2835 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
2836 MAXIMUM_ALLOWED_ACCESS
,
2837 &sid_Builtin
, &domain_pol
);
2839 if (!NT_STATUS_IS_OK(result
)) {
2840 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2844 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
, 1000,
2848 if (!NT_STATUS_IS_OK(result
)) {
2849 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2854 if (num_rids
!= 1) {
2855 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2859 if (rid_types
[0] == SID_NAME_DOM_GRP
) {
2860 return rpc_list_group_members(pipe_hnd
, mem_ctx
, domain_name
,
2861 domain_sid
, &domain_pol
,
2865 if (rid_types
[0] == SID_NAME_ALIAS
) {
2866 return rpc_list_alias_members(pipe_hnd
, mem_ctx
, &domain_pol
,
2870 return NT_STATUS_NO_SUCH_GROUP
;
2873 static int rpc_group_members(int argc
, const char **argv
)
2876 return rpc_group_usage(argc
, argv
);
2879 return run_rpc_command(NULL
, PI_SAMR
, 0,
2880 rpc_group_members_internals
,
2884 static NTSTATUS
rpc_group_rename_internals(const DOM_SID
*domain_sid
,
2885 const char *domain_name
,
2886 struct cli_state
*cli
,
2887 struct rpc_pipe_client
*pipe_hnd
,
2888 TALLOC_CTX
*mem_ctx
,
2893 POLICY_HND connect_pol
, domain_pol
, group_pol
;
2894 uint32 num_rids
, *rids
, *rid_types
;
2898 d_printf("Usage: 'net rpc group rename group newname'\n");
2899 return NT_STATUS_UNSUCCESSFUL
;
2902 /* Get sam policy handle */
2904 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
2907 if (!NT_STATUS_IS_OK(result
))
2910 /* Get domain policy handle */
2912 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
2913 MAXIMUM_ALLOWED_ACCESS
,
2914 domain_sid
, &domain_pol
);
2916 if (!NT_STATUS_IS_OK(result
))
2919 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
, 1000,
2920 1, argv
, &num_rids
, &rids
, &rid_types
);
2922 if (num_rids
!= 1) {
2923 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2927 if (rid_types
[0] != SID_NAME_DOM_GRP
) {
2928 d_fprintf(stderr
, "Can only rename domain groups\n");
2929 return NT_STATUS_UNSUCCESSFUL
;
2932 result
= rpccli_samr_open_group(pipe_hnd
, mem_ctx
, &domain_pol
,
2933 MAXIMUM_ALLOWED_ACCESS
,
2934 rids
[0], &group_pol
);
2936 if (!NT_STATUS_IS_OK(result
))
2941 ctr
.switch_value1
= 2;
2942 init_samr_group_info2(&ctr
.group
.info2
, argv
[1]);
2944 result
= rpccli_samr_set_groupinfo(pipe_hnd
, mem_ctx
, &group_pol
, &ctr
);
2946 if (!NT_STATUS_IS_OK(result
))
2949 return NT_STATUS_NO_SUCH_GROUP
;
2952 static int rpc_group_rename(int argc
, const char **argv
)
2955 return rpc_group_usage(argc
, argv
);
2958 return run_rpc_command(NULL
, PI_SAMR
, 0,
2959 rpc_group_rename_internals
,
2964 * 'net rpc group' entrypoint.
2965 * @param argc Standard main() style argc
2966 * @param argc Standard main() style argv. Initial components are already
2970 int net_rpc_group(int argc
, const char **argv
)
2972 struct functable func
[] = {
2973 {"add", rpc_group_add
},
2974 {"delete", rpc_group_delete
},
2975 {"addmem", rpc_group_addmem
},
2976 {"delmem", rpc_group_delmem
},
2977 {"list", rpc_group_list
},
2978 {"members", rpc_group_members
},
2979 {"rename", rpc_group_rename
},
2984 return run_rpc_command(NULL
, PI_SAMR
, 0,
2985 rpc_group_list_internals
,
2989 return net_run_function(argc
, argv
, func
, rpc_group_usage
);
2992 /****************************************************************************/
2994 static int rpc_share_usage(int argc
, const char **argv
)
2996 return net_help_share(argc
, argv
);
3000 * Add a share on a remote RPC server
3002 * All parameters are provided by the run_rpc_command function, except for
3003 * argc, argv which are passes through.
3005 * @param domain_sid The domain sid acquired from the remote server
3006 * @param cli A cli_state connected to the server.
3007 * @param mem_ctx Talloc context, destoyed on completion of the function.
3008 * @param argc Standard main() style argc
3009 * @param argv Standard main() style argv. Initial components are already
3012 * @return Normal NTSTATUS return.
3014 static NTSTATUS
rpc_share_add_internals(const DOM_SID
*domain_sid
,
3015 const char *domain_name
,
3016 struct cli_state
*cli
,
3017 struct rpc_pipe_client
*pipe_hnd
,
3018 TALLOC_CTX
*mem_ctx
,int argc
,
3024 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
3025 uint32 num_users
=0, perms
=0;
3026 char *password
=NULL
; /* don't allow a share password */
3029 if ((sharename
= talloc_strdup(mem_ctx
, argv
[0])) == NULL
) {
3030 return NT_STATUS_NO_MEMORY
;
3033 path
= strchr(sharename
, '=');
3035 return NT_STATUS_UNSUCCESSFUL
;
3038 result
= rpccli_srvsvc_net_share_add(pipe_hnd
, mem_ctx
, sharename
, type
,
3039 opt_comment
, perms
, opt_maxusers
,
3040 num_users
, path
, password
,
3042 return werror_to_ntstatus(result
);
3045 static int rpc_share_add(int argc
, const char **argv
)
3047 if ((argc
< 1) || !strchr(argv
[0], '=')) {
3048 DEBUG(1,("Sharename or path not specified on add\n"));
3049 return rpc_share_usage(argc
, argv
);
3051 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
3052 rpc_share_add_internals
,
3057 * Delete a share on a remote RPC server
3059 * All parameters are provided by the run_rpc_command function, except for
3060 * argc, argv which are passes through.
3062 * @param domain_sid The domain sid acquired from the remote server
3063 * @param cli A cli_state connected to the server.
3064 * @param mem_ctx Talloc context, destoyed on completion of the function.
3065 * @param argc Standard main() style argc
3066 * @param argv Standard main() style argv. Initial components are already
3069 * @return Normal NTSTATUS return.
3071 static NTSTATUS
rpc_share_del_internals(const DOM_SID
*domain_sid
,
3072 const char *domain_name
,
3073 struct cli_state
*cli
,
3074 struct rpc_pipe_client
*pipe_hnd
,
3075 TALLOC_CTX
*mem_ctx
,
3081 result
= rpccli_srvsvc_net_share_del(pipe_hnd
, mem_ctx
, argv
[0]);
3082 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3086 * Delete a share on a remote RPC server
3088 * @param domain_sid The domain sid acquired from the remote server
3089 * @param argc Standard main() style argc
3090 * @param argv Standard main() style argv. Initial components are already
3093 * @return A shell status integer (0 for success)
3095 static int rpc_share_delete(int argc
, const char **argv
)
3098 DEBUG(1,("Sharename not specified on delete\n"));
3099 return rpc_share_usage(argc
, argv
);
3101 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
3102 rpc_share_del_internals
,
3107 * Formatted print of share info
3109 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3112 static void display_share_info_1(SRV_SHARE_INFO_1
*info1
)
3114 fstring netname
= "", remark
= "";
3116 rpcstr_pull_unistr2_fstring(netname
, &info1
->info_1_str
.uni_netname
);
3117 rpcstr_pull_unistr2_fstring(remark
, &info1
->info_1_str
.uni_remark
);
3119 if (opt_long_list_entries
) {
3120 d_printf("%-12s %-8.8s %-50s\n",
3121 netname
, share_type
[info1
->info_1
.type
& ~(STYPE_TEMPORARY
|STYPE_HIDDEN
)], remark
);
3123 d_printf("%s\n", netname
);
3128 static WERROR
get_share_info(struct rpc_pipe_client
*pipe_hnd
,
3129 TALLOC_CTX
*mem_ctx
,
3133 SRV_SHARE_INFO_CTR
*ctr
)
3136 SRV_SHARE_INFO info
;
3138 /* no specific share requested, enumerate all */
3142 uint32 preferred_len
= 0xffffffff;
3144 init_enum_hnd(&hnd
, 0);
3146 return rpccli_srvsvc_net_share_enum(pipe_hnd
, mem_ctx
, level
, ctr
,
3147 preferred_len
, &hnd
);
3150 /* request just one share */
3151 result
= rpccli_srvsvc_net_share_get_info(pipe_hnd
, mem_ctx
, argv
[0], level
, &info
);
3153 if (!W_ERROR_IS_OK(result
))
3159 ctr
->info_level
= ctr
->switch_value
= level
;
3160 ctr
->ptr_share_info
= ctr
->ptr_entries
= 1;
3161 ctr
->num_entries
= ctr
->num_entries2
= 1;
3167 SRV_SHARE_INFO_1
*info1
;
3169 ctr
->share
.info1
= TALLOC_ARRAY(mem_ctx
, SRV_SHARE_INFO_1
, 1);
3170 if (ctr
->share
.info1
== NULL
) {
3171 result
= WERR_NOMEM
;
3174 info1
= ctr
->share
.info1
;
3176 memset(ctr
->share
.info1
, 0, sizeof(SRV_SHARE_INFO_1
));
3178 /* Copy pointer crap */
3180 memcpy(&info1
->info_1
, &info
.share
.info1
.info_1
, sizeof(SH_INFO_1
));
3182 /* Duplicate strings */
3184 s
= unistr2_tdup(mem_ctx
, &info
.share
.info1
.info_1_str
.uni_netname
);
3186 init_unistr2(&info1
->info_1_str
.uni_netname
, s
, UNI_STR_TERMINATE
);
3188 s
= unistr2_tdup(mem_ctx
, &info
.share
.info1
.info_1_str
.uni_remark
);
3190 init_unistr2(&info1
->info_1_str
.uni_remark
, s
, UNI_STR_TERMINATE
);
3195 SRV_SHARE_INFO_2
*info2
;
3197 ctr
->share
.info2
= TALLOC_ARRAY(mem_ctx
, SRV_SHARE_INFO_2
, 1);
3198 if (ctr
->share
.info2
== NULL
) {
3199 result
= WERR_NOMEM
;
3202 info2
= ctr
->share
.info2
;
3204 memset(ctr
->share
.info2
, 0, sizeof(SRV_SHARE_INFO_2
));
3206 /* Copy pointer crap */
3208 memcpy(&info2
->info_2
, &info
.share
.info2
.info_2
, sizeof(SH_INFO_2
));
3210 /* Duplicate strings */
3212 s
= unistr2_tdup(mem_ctx
, &info
.share
.info2
.info_2_str
.uni_netname
);
3214 init_unistr2(&info2
->info_2_str
.uni_netname
, s
, UNI_STR_TERMINATE
);
3216 s
= unistr2_tdup(mem_ctx
, &info
.share
.info2
.info_2_str
.uni_remark
);
3218 init_unistr2(&info2
->info_2_str
.uni_remark
, s
, UNI_STR_TERMINATE
);
3220 s
= unistr2_tdup(mem_ctx
, &info
.share
.info2
.info_2_str
.uni_path
);
3222 init_unistr2(&info2
->info_2_str
.uni_path
, s
, UNI_STR_TERMINATE
);
3224 s
= unistr2_tdup(mem_ctx
, &info
.share
.info2
.info_2_str
.uni_passwd
);
3226 init_unistr2(&info2
->info_2_str
.uni_passwd
, s
, UNI_STR_TERMINATE
);
3231 SRV_SHARE_INFO_502
*info502
;
3233 ctr
->share
.info502
= TALLOC_ARRAY(mem_ctx
, SRV_SHARE_INFO_502
, 1);
3234 if (ctr
->share
.info502
== NULL
) {
3235 result
= WERR_NOMEM
;
3238 info502
= ctr
->share
.info502
;
3240 memset(ctr
->share
.info502
, 0, sizeof(SRV_SHARE_INFO_502
));
3242 /* Copy pointer crap */
3244 memcpy(&info502
->info_502
, &info
.share
.info502
.info_502
, sizeof(SH_INFO_502
));
3246 /* Duplicate strings */
3248 s
= unistr2_tdup(mem_ctx
, &info
.share
.info502
.info_502_str
.uni_netname
);
3250 init_unistr2(&info502
->info_502_str
.uni_netname
, s
, UNI_STR_TERMINATE
);
3252 s
= unistr2_tdup(mem_ctx
, &info
.share
.info502
.info_502_str
.uni_remark
);
3254 init_unistr2(&info502
->info_502_str
.uni_remark
, s
, UNI_STR_TERMINATE
);
3256 s
= unistr2_tdup(mem_ctx
, &info
.share
.info502
.info_502_str
.uni_path
);
3258 init_unistr2(&info502
->info_502_str
.uni_path
, s
, UNI_STR_TERMINATE
);
3260 s
= unistr2_tdup(mem_ctx
, &info
.share
.info502
.info_502_str
.uni_passwd
);
3262 init_unistr2(&info502
->info_502_str
.uni_passwd
, s
, UNI_STR_TERMINATE
);
3264 info502
->info_502_str
.sd
= dup_sec_desc(mem_ctx
, info
.share
.info502
.info_502_str
.sd
);
3275 * List shares on a remote RPC server
3277 * All parameters are provided by the run_rpc_command function, except for
3278 * argc, argv which are passes through.
3280 * @param domain_sid The domain sid acquired from the remote server
3281 * @param cli A cli_state connected to the server.
3282 * @param mem_ctx Talloc context, destoyed on completion of the function.
3283 * @param argc Standard main() style argc
3284 * @param argv Standard main() style argv. Initial components are already
3287 * @return Normal NTSTATUS return.
3290 static NTSTATUS
rpc_share_list_internals(const DOM_SID
*domain_sid
,
3291 const char *domain_name
,
3292 struct cli_state
*cli
,
3293 struct rpc_pipe_client
*pipe_hnd
,
3294 TALLOC_CTX
*mem_ctx
,
3298 SRV_SHARE_INFO_CTR ctr
;
3300 uint32 i
, level
= 1;
3302 result
= get_share_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &ctr
);
3303 if (!W_ERROR_IS_OK(result
))
3306 /* Display results */
3308 if (opt_long_list_entries
) {
3310 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3311 "\nShare name Type Description\n"\
3312 "---------- ---- -----------\n");
3314 for (i
= 0; i
< ctr
.num_entries
; i
++)
3315 display_share_info_1(&ctr
.share
.info1
[i
]);
3317 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3321 * 'net rpc share list' entrypoint.
3322 * @param argc Standard main() style argc
3323 * @param argv Standard main() style argv. Initial components are already
3326 static int rpc_share_list(int argc
, const char **argv
)
3328 return run_rpc_command(NULL
, PI_SRVSVC
, 0, rpc_share_list_internals
, argc
, argv
);
3331 static BOOL
check_share_availability(struct cli_state
*cli
, const char *netname
)
3333 if (!cli_send_tconX(cli
, netname
, "A:", "", 0)) {
3334 d_printf("skipping [%s]: not a file share.\n", netname
);
3344 static BOOL
check_share_sanity(struct cli_state
*cli
, fstring netname
, uint32 type
)
3346 /* only support disk shares */
3347 if (! ( type
== STYPE_DISKTREE
|| type
== (STYPE_DISKTREE
| STYPE_HIDDEN
)) ) {
3348 printf("share [%s] is not a diskshare (type: %x)\n", netname
, type
);
3352 /* skip builtin shares */
3353 /* FIXME: should print$ be added too ? */
3354 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$") ||
3355 strequal(netname
,"global"))
3358 if (opt_exclude
&& in_list(netname
, opt_exclude
, False
)) {
3359 printf("excluding [%s]\n", netname
);
3363 return check_share_availability(cli
, netname
);
3367 * Migrate shares from a remote RPC server to the local RPC srever
3369 * All parameters are provided by the run_rpc_command function, except for
3370 * argc, argv which are passes through.
3372 * @param domain_sid The domain sid acquired from the remote server
3373 * @param cli A cli_state connected to the server.
3374 * @param mem_ctx Talloc context, destoyed on completion of the function.
3375 * @param argc Standard main() style argc
3376 * @param argv Standard main() style argv. Initial components are already
3379 * @return Normal NTSTATUS return.
3382 static NTSTATUS
rpc_share_migrate_shares_internals(const DOM_SID
*domain_sid
,
3383 const char *domain_name
,
3384 struct cli_state
*cli
,
3385 struct rpc_pipe_client
*pipe_hnd
,
3386 TALLOC_CTX
*mem_ctx
,
3391 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3392 SRV_SHARE_INFO_CTR ctr_src
;
3393 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
3394 char *password
= NULL
; /* don't allow a share password */
3396 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3397 struct cli_state
*cli_dst
= NULL
;
3398 uint32 level
= 502; /* includes secdesc */
3400 result
= get_share_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &ctr_src
);
3401 if (!W_ERROR_IS_OK(result
))
3404 /* connect destination PI_SRVSVC */
3405 nt_status
= connect_dst_pipe(&cli_dst
, &srvsvc_pipe
, PI_SRVSVC
);
3406 if (!NT_STATUS_IS_OK(nt_status
))
3410 for (i
= 0; i
< ctr_src
.num_entries
; i
++) {
3412 fstring netname
= "", remark
= "", path
= "";
3413 /* reset error-code */
3414 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3416 rpcstr_pull_unistr2_fstring(
3417 netname
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_netname
);
3418 rpcstr_pull_unistr2_fstring(
3419 remark
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_remark
);
3420 rpcstr_pull_unistr2_fstring(
3421 path
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_path
);
3423 if (!check_share_sanity(cli
, netname
, ctr_src
.share
.info502
[i
].info_502
.type
))
3426 /* finally add the share on the dst server */
3428 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3429 netname
, path
, remark
);
3431 result
= rpccli_srvsvc_net_share_add(srvsvc_pipe
, mem_ctx
, netname
, type
, remark
,
3432 ctr_src
.share
.info502
[i
].info_502
.perms
,
3433 ctr_src
.share
.info502
[i
].info_502
.max_uses
,
3434 ctr_src
.share
.info502
[i
].info_502
.num_uses
,
3435 path
, password
, level
,
3438 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ALREADY_EXISTS
)) {
3439 printf(" [%s] does already exist\n", netname
);
3443 if (!W_ERROR_IS_OK(result
)) {
3444 printf("cannot add share: %s\n", dos_errstr(result
));
3450 nt_status
= NT_STATUS_OK
;
3454 cli_shutdown(cli_dst
);
3462 * Migrate shares from a rpc-server to another
3464 * @param argc Standard main() style argc
3465 * @param argv Standard main() style argv. Initial components are already
3468 * @return A shell status integer (0 for success)
3470 static int rpc_share_migrate_shares(int argc
, const char **argv
)
3474 printf("no server to migrate\n");
3478 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
3479 rpc_share_migrate_shares_internals
,
3486 * @param f file_info
3487 * @param mask current search mask
3488 * @param state arg-pointer
3491 static void copy_fn(const char *mnt
, file_info
*f
, const char *mask
, void *state
)
3493 static NTSTATUS nt_status
;
3494 static struct copy_clistate
*local_state
;
3495 static fstring filename
, new_mask
;
3499 local_state
= (struct copy_clistate
*)state
;
3500 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3502 if (strequal(f
->name
, ".") || strequal(f
->name
, ".."))
3505 DEBUG(3,("got mask: %s, name: %s\n", mask
, f
->name
));
3508 if (f
->mode
& aDIR
) {
3510 DEBUG(3,("got dir: %s\n", f
->name
));
3512 fstrcpy(dir
, local_state
->cwd
);
3514 fstrcat(dir
, f
->name
);
3516 switch (net_mode_share
)
3518 case NET_MODE_SHARE_MIGRATE
:
3519 /* create that directory */
3520 nt_status
= net_copy_file(local_state
->mem_ctx
,
3521 local_state
->cli_share_src
,
3522 local_state
->cli_share_dst
,
3524 opt_acls
? True
: False
,
3525 opt_attrs
? True
: False
,
3526 opt_timestamps
? True
: False
,
3530 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3534 if (!NT_STATUS_IS_OK(nt_status
))
3535 printf("could not handle dir %s: %s\n",
3536 dir
, nt_errstr(nt_status
));
3538 /* search below that directory */
3539 fstrcpy(new_mask
, dir
);
3540 fstrcat(new_mask
, "\\*");
3542 old_dir
= local_state
->cwd
;
3543 local_state
->cwd
= dir
;
3544 if (!sync_files(local_state
, new_mask
))
3545 printf("could not handle files\n");
3546 local_state
->cwd
= old_dir
;
3553 fstrcpy(filename
, local_state
->cwd
);
3554 fstrcat(filename
, "\\");
3555 fstrcat(filename
, f
->name
);
3557 DEBUG(3,("got file: %s\n", filename
));
3559 switch (net_mode_share
)
3561 case NET_MODE_SHARE_MIGRATE
:
3562 nt_status
= net_copy_file(local_state
->mem_ctx
,
3563 local_state
->cli_share_src
,
3564 local_state
->cli_share_dst
,
3566 opt_acls
? True
: False
,
3567 opt_attrs
? True
: False
,
3568 opt_timestamps
? True
: False
,
3572 d_fprintf(stderr
, "Unsupported file mode %d\n", net_mode_share
);
3576 if (!NT_STATUS_IS_OK(nt_status
))
3577 printf("could not handle file %s: %s\n",
3578 filename
, nt_errstr(nt_status
));
3583 * sync files, can be called recursivly to list files
3584 * and then call copy_fn for each file
3586 * @param cp_clistate pointer to the copy_clistate we work with
3587 * @param mask the current search mask
3589 * @return Boolean result
3591 BOOL
sync_files(struct copy_clistate
*cp_clistate
, pstring mask
)
3593 struct cli_state
*targetcli
;
3596 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
3598 if ( !cli_resolve_path( "", cp_clistate
->cli_share_src
, mask
, &targetcli
, targetpath
) ) {
3599 d_fprintf(stderr
, "cli_resolve_path %s failed with error: %s\n",
3600 mask
, cli_errstr(cp_clistate
->cli_share_src
));
3604 if (cli_list(targetcli
, targetpath
, cp_clistate
->attribute
, copy_fn
, cp_clistate
) == -1) {
3605 d_fprintf(stderr
, "listing %s failed with error: %s\n",
3606 mask
, cli_errstr(targetcli
));
3615 * Set the top level directory permissions before we do any further copies.
3616 * Should set up ACL inheritance.
3619 BOOL
copy_top_level_perms(struct copy_clistate
*cp_clistate
,
3620 const char *sharename
)
3622 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3624 switch (net_mode_share
) {
3625 case NET_MODE_SHARE_MIGRATE
:
3626 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename
));
3627 nt_status
= net_copy_fileattr(cp_clistate
->mem_ctx
,
3628 cp_clistate
->cli_share_src
,
3629 cp_clistate
->cli_share_dst
,
3631 opt_acls
? True
: False
,
3632 opt_attrs
? True
: False
,
3633 opt_timestamps
? True
: False
,
3637 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3641 if (!NT_STATUS_IS_OK(nt_status
)) {
3642 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3643 sharename
, nt_errstr(nt_status
));
3651 * Sync all files inside a remote share to another share (over smb)
3653 * All parameters are provided by the run_rpc_command function, except for
3654 * argc, argv which are passes through.
3656 * @param domain_sid The domain sid acquired from the remote server
3657 * @param cli A cli_state connected to the server.
3658 * @param mem_ctx Talloc context, destoyed on completion of the function.
3659 * @param argc Standard main() style argc
3660 * @param argv Standard main() style argv. Initial components are already
3663 * @return Normal NTSTATUS return.
3666 static NTSTATUS
rpc_share_migrate_files_internals(const DOM_SID
*domain_sid
,
3667 const char *domain_name
,
3668 struct cli_state
*cli
,
3669 struct rpc_pipe_client
*pipe_hnd
,
3670 TALLOC_CTX
*mem_ctx
,
3675 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3676 SRV_SHARE_INFO_CTR ctr_src
;
3679 struct copy_clistate cp_clistate
;
3680 BOOL got_src_share
= False
;
3681 BOOL got_dst_share
= False
;
3682 pstring mask
= "\\*";
3685 dst
= SMB_STRDUP(opt_destination
?opt_destination
:"127.0.0.1");
3687 result
= get_share_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &ctr_src
);
3689 if (!W_ERROR_IS_OK(result
))
3692 for (i
= 0; i
< ctr_src
.num_entries
; i
++) {
3694 fstring netname
= "";
3696 rpcstr_pull_unistr2_fstring(
3697 netname
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_netname
);
3699 if (!check_share_sanity(cli
, netname
, ctr_src
.share
.info502
[i
].info_502
.type
))
3702 /* one might not want to mirror whole discs :) */
3703 if (strequal(netname
, "print$") || netname
[1] == '$') {
3704 d_printf("skipping [%s]: builtin/hidden share\n", netname
);
3708 switch (net_mode_share
)
3710 case NET_MODE_SHARE_MIGRATE
:
3714 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3717 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3719 opt_acls
? "including" : "without",
3720 opt_attrs
? "including" : "without",
3721 opt_timestamps
? "(preserving timestamps)" : "");
3723 cp_clistate
.mem_ctx
= mem_ctx
;
3724 cp_clistate
.cli_share_src
= NULL
;
3725 cp_clistate
.cli_share_dst
= NULL
;
3726 cp_clistate
.cwd
= NULL
;
3727 cp_clistate
.attribute
= aSYSTEM
| aHIDDEN
| aDIR
;
3729 /* open share source */
3730 nt_status
= connect_to_service(&cp_clistate
.cli_share_src
,
3731 &cli
->dest_ip
, cli
->desthost
,
3733 if (!NT_STATUS_IS_OK(nt_status
))
3736 got_src_share
= True
;
3738 if (net_mode_share
== NET_MODE_SHARE_MIGRATE
) {
3739 /* open share destination */
3740 nt_status
= connect_to_service(&cp_clistate
.cli_share_dst
,
3741 NULL
, dst
, netname
, "A:");
3742 if (!NT_STATUS_IS_OK(nt_status
))
3745 got_dst_share
= True
;
3748 if (!copy_top_level_perms(&cp_clistate
, netname
)) {
3749 d_fprintf(stderr
, "Could not handle the top level directory permissions for the share: %s\n", netname
);
3750 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3754 if (!sync_files(&cp_clistate
, mask
)) {
3755 d_fprintf(stderr
, "could not handle files for share: %s\n", netname
);
3756 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3761 nt_status
= NT_STATUS_OK
;
3766 cli_shutdown(cp_clistate
.cli_share_src
);
3769 cli_shutdown(cp_clistate
.cli_share_dst
);
3775 static int rpc_share_migrate_files(int argc
, const char **argv
)
3779 printf("no server to migrate\n");
3783 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
3784 rpc_share_migrate_files_internals
,
3789 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3791 * All parameters are provided by the run_rpc_command function, except for
3792 * argc, argv which are passes through.
3794 * @param domain_sid The domain sid acquired from the remote server
3795 * @param cli A cli_state connected to the server.
3796 * @param mem_ctx Talloc context, destoyed on completion of the function.
3797 * @param argc Standard main() style argc
3798 * @param argv Standard main() style argv. Initial components are already
3801 * @return Normal NTSTATUS return.
3804 static NTSTATUS
rpc_share_migrate_security_internals(const DOM_SID
*domain_sid
,
3805 const char *domain_name
,
3806 struct cli_state
*cli
,
3807 struct rpc_pipe_client
*pipe_hnd
,
3808 TALLOC_CTX
*mem_ctx
,
3813 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3814 SRV_SHARE_INFO_CTR ctr_src
;
3815 SRV_SHARE_INFO info
;
3817 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3818 struct cli_state
*cli_dst
= NULL
;
3819 uint32 level
= 502; /* includes secdesc */
3821 result
= get_share_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &ctr_src
);
3823 if (!W_ERROR_IS_OK(result
))
3826 /* connect destination PI_SRVSVC */
3827 nt_status
= connect_dst_pipe(&cli_dst
, &srvsvc_pipe
, PI_SRVSVC
);
3828 if (!NT_STATUS_IS_OK(nt_status
))
3832 for (i
= 0; i
< ctr_src
.num_entries
; i
++) {
3834 fstring netname
= "", remark
= "", path
= "";
3835 /* reset error-code */
3836 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3838 rpcstr_pull_unistr2_fstring(
3839 netname
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_netname
);
3840 rpcstr_pull_unistr2_fstring(
3841 remark
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_remark
);
3842 rpcstr_pull_unistr2_fstring(
3843 path
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_path
);
3845 if (!check_share_sanity(cli
, netname
, ctr_src
.share
.info502
[i
].info_502
.type
))
3848 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3849 netname
, path
, remark
);
3852 display_sec_desc(ctr_src
.share
.info502
[i
].info_502_str
.sd
);
3857 info
.switch_value
= level
;
3858 info
.ptr_share_ctr
= 1;
3860 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3861 info
.share
.info502
= ctr_src
.share
.info502
[i
];
3863 /* finally modify the share on the dst server */
3864 result
= rpccli_srvsvc_net_share_set_info(srvsvc_pipe
, mem_ctx
, netname
, level
, &info
);
3866 if (!W_ERROR_IS_OK(result
)) {
3867 printf("cannot set share-acl: %s\n", dos_errstr(result
));
3873 nt_status
= NT_STATUS_OK
;
3877 cli_shutdown(cli_dst
);
3885 * Migrate share-acls from a rpc-server to another
3887 * @param argc Standard main() style argc
3888 * @param argv Standard main() style argv. Initial components are already
3891 * @return A shell status integer (0 for success)
3893 static int rpc_share_migrate_security(int argc
, const char **argv
)
3897 printf("no server to migrate\n");
3901 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
3902 rpc_share_migrate_security_internals
,
3907 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3908 * from one server to another
3910 * @param argc Standard main() style argc
3911 * @param argv Standard main() style argv. Initial components are already
3914 * @return A shell status integer (0 for success)
3917 static int rpc_share_migrate_all(int argc
, const char **argv
)
3922 printf("no server to migrate\n");
3926 /* order is important. we don't want to be locked out by the share-acl
3927 * before copying files - gd */
3929 ret
= run_rpc_command(NULL
, PI_SRVSVC
, 0, rpc_share_migrate_shares_internals
, argc
, argv
);
3933 ret
= run_rpc_command(NULL
, PI_SRVSVC
, 0, rpc_share_migrate_files_internals
, argc
, argv
);
3937 return run_rpc_command(NULL
, PI_SRVSVC
, 0, rpc_share_migrate_security_internals
, argc
, argv
);
3942 * 'net rpc share migrate' entrypoint.
3943 * @param argc Standard main() style argc
3944 * @param argv Standard main() style argv. Initial components are already
3947 static int rpc_share_migrate(int argc
, const char **argv
)
3950 struct functable func
[] = {
3951 {"all", rpc_share_migrate_all
},
3952 {"files", rpc_share_migrate_files
},
3953 {"help", rpc_share_usage
},
3954 {"security", rpc_share_migrate_security
},
3955 {"shares", rpc_share_migrate_shares
},
3959 net_mode_share
= NET_MODE_SHARE_MIGRATE
;
3961 return net_run_function(argc
, argv
, func
, rpc_share_usage
);
3970 static int num_server_aliases
;
3971 static struct full_alias
*server_aliases
;
3974 * Add an alias to the static list.
3976 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
3978 if (server_aliases
== NULL
)
3979 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
3981 server_aliases
[num_server_aliases
] = *alias
;
3982 num_server_aliases
+= 1;
3986 * For a specific domain on the server, fetch all the aliases
3987 * and their members. Add all of them to the server_aliases.
3990 static NTSTATUS
rpc_fetch_domain_aliases(struct rpc_pipe_client
*pipe_hnd
,
3991 TALLOC_CTX
*mem_ctx
,
3992 POLICY_HND
*connect_pol
,
3993 const DOM_SID
*domain_sid
)
3995 uint32 start_idx
, max_entries
, num_entries
, i
;
3996 struct acct_info
*groups
;
3998 POLICY_HND domain_pol
;
4000 /* Get domain policy handle */
4002 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, connect_pol
,
4003 MAXIMUM_ALLOWED_ACCESS
,
4004 domain_sid
, &domain_pol
);
4005 if (!NT_STATUS_IS_OK(result
))
4012 result
= rpccli_samr_enum_als_groups(pipe_hnd
, mem_ctx
, &domain_pol
,
4013 &start_idx
, max_entries
,
4014 &groups
, &num_entries
);
4016 for (i
= 0; i
< num_entries
; i
++) {
4018 POLICY_HND alias_pol
;
4019 struct full_alias alias
;
4023 result
= rpccli_samr_open_alias(pipe_hnd
, mem_ctx
, &domain_pol
,
4024 MAXIMUM_ALLOWED_ACCESS
,
4027 if (!NT_STATUS_IS_OK(result
))
4030 result
= rpccli_samr_query_aliasmem(pipe_hnd
, mem_ctx
,
4034 if (!NT_STATUS_IS_OK(result
))
4037 result
= rpccli_samr_close(pipe_hnd
, mem_ctx
, &alias_pol
);
4038 if (!NT_STATUS_IS_OK(result
))
4041 alias
.members
= NULL
;
4043 if (alias
.num_members
> 0) {
4044 alias
.members
= SMB_MALLOC_ARRAY(DOM_SID
, alias
.num_members
);
4046 for (j
= 0; j
< alias
.num_members
; j
++)
4047 sid_copy(&alias
.members
[j
],
4051 sid_copy(&alias
.sid
, domain_sid
);
4052 sid_append_rid(&alias
.sid
, groups
[i
].rid
);
4054 push_alias(mem_ctx
, &alias
);
4056 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
4058 result
= NT_STATUS_OK
;
4061 rpccli_samr_close(pipe_hnd
, mem_ctx
, &domain_pol
);
4067 * Dump server_aliases as names for debugging purposes.
4070 static NTSTATUS
rpc_aliaslist_dump(const DOM_SID
*domain_sid
,
4071 const char *domain_name
,
4072 struct cli_state
*cli
,
4073 struct rpc_pipe_client
*pipe_hnd
,
4074 TALLOC_CTX
*mem_ctx
,
4082 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, True
,
4083 SEC_RIGHTS_MAXIMUM_ALLOWED
,
4085 if (!NT_STATUS_IS_OK(result
))
4088 for (i
=0; i
<num_server_aliases
; i
++) {
4094 struct full_alias
*alias
= &server_aliases
[i
];
4096 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
4098 &domains
, &names
, &types
);
4099 if (!NT_STATUS_IS_OK(result
))
4102 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
4104 if (alias
->num_members
== 0) {
4109 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
,
4112 &domains
, &names
, &types
);
4114 if (!NT_STATUS_IS_OK(result
) &&
4115 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
4118 for (j
=0; j
<alias
->num_members
; j
++)
4119 DEBUG(1, ("%s\\%s (%d); ",
4120 domains
[j
] ? domains
[j
] : "*unknown*",
4121 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
4125 rpccli_lsa_close(pipe_hnd
, mem_ctx
, &lsa_pol
);
4127 return NT_STATUS_OK
;
4131 * Fetch a list of all server aliases and their members into
4135 static NTSTATUS
rpc_aliaslist_internals(const DOM_SID
*domain_sid
,
4136 const char *domain_name
,
4137 struct cli_state
*cli
,
4138 struct rpc_pipe_client
*pipe_hnd
,
4139 TALLOC_CTX
*mem_ctx
,
4144 POLICY_HND connect_pol
;
4146 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
4149 if (!NT_STATUS_IS_OK(result
))
4152 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4153 &global_sid_Builtin
);
4155 if (!NT_STATUS_IS_OK(result
))
4158 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4161 rpccli_samr_close(pipe_hnd
, mem_ctx
, &connect_pol
);
4166 static void init_user_token(NT_USER_TOKEN
*token
, DOM_SID
*user_sid
)
4168 token
->num_sids
= 4;
4170 token
->user_sids
= SMB_MALLOC_ARRAY(DOM_SID
, 4);
4172 token
->user_sids
[0] = *user_sid
;
4173 sid_copy(&token
->user_sids
[1], &global_sid_World
);
4174 sid_copy(&token
->user_sids
[2], &global_sid_Network
);
4175 sid_copy(&token
->user_sids
[3], &global_sid_Authenticated_Users
);
4178 static void free_user_token(NT_USER_TOKEN
*token
)
4180 SAFE_FREE(token
->user_sids
);
4183 static BOOL
is_sid_in_token(NT_USER_TOKEN
*token
, DOM_SID
*sid
)
4187 for (i
=0; i
<token
->num_sids
; i
++) {
4188 if (sid_compare(sid
, &token
->user_sids
[i
]) == 0)
4194 static void add_sid_to_token(NT_USER_TOKEN
*token
, DOM_SID
*sid
)
4196 if (is_sid_in_token(token
, sid
))
4199 token
->user_sids
= SMB_REALLOC_ARRAY(token
->user_sids
, DOM_SID
, token
->num_sids
+1);
4200 if (!token
->user_sids
) {
4204 sid_copy(&token
->user_sids
[token
->num_sids
], sid
);
4206 token
->num_sids
+= 1;
4211 NT_USER_TOKEN token
;
4214 static void dump_user_token(struct user_token
*token
)
4218 d_printf("%s\n", token
->name
);
4220 for (i
=0; i
<token
->token
.num_sids
; i
++) {
4221 d_printf(" %s\n", sid_string_static(&token
->token
.user_sids
[i
]));
4225 static BOOL
is_alias_member(DOM_SID
*sid
, struct full_alias
*alias
)
4229 for (i
=0; i
<alias
->num_members
; i
++) {
4230 if (sid_compare(sid
, &alias
->members
[i
]) == 0)
4237 static void collect_sid_memberships(NT_USER_TOKEN
*token
, DOM_SID sid
)
4241 for (i
=0; i
<num_server_aliases
; i
++) {
4242 if (is_alias_member(&sid
, &server_aliases
[i
]))
4243 add_sid_to_token(token
, &server_aliases
[i
].sid
);
4248 * We got a user token with all the SIDs we can know about without asking the
4249 * server directly. These are the user and domain group sids. All of these can
4250 * be members of aliases. So scan the list of aliases for each of the SIDs and
4251 * add them to the token.
4254 static void collect_alias_memberships(NT_USER_TOKEN
*token
)
4256 int num_global_sids
= token
->num_sids
;
4259 for (i
=0; i
<num_global_sids
; i
++) {
4260 collect_sid_memberships(token
, token
->user_sids
[i
]);
4264 static BOOL
get_user_sids(const char *domain
, const char *user
, NT_USER_TOKEN
*token
)
4266 struct winbindd_request request
;
4267 struct winbindd_response response
;
4275 fstr_sprintf(full_name
, "%s%c%s",
4276 domain
, *lp_winbind_separator(), user
);
4278 /* First let's find out the user sid */
4280 ZERO_STRUCT(request
);
4281 ZERO_STRUCT(response
);
4283 fstrcpy(request
.data
.name
.dom_name
, domain
);
4284 fstrcpy(request
.data
.name
.name
, user
);
4286 result
= winbindd_request_response(WINBINDD_LOOKUPNAME
, &request
, &response
);
4288 if (result
!= NSS_STATUS_SUCCESS
) {
4289 DEBUG(1, ("winbind could not find %s\n", full_name
));
4293 if (response
.data
.sid
.type
!= SID_NAME_USER
) {
4294 DEBUG(1, ("%s is not a user\n", full_name
));
4298 string_to_sid(&user_sid
, response
.data
.sid
.sid
);
4300 init_user_token(token
, &user_sid
);
4302 /* And now the groups winbind knows about */
4304 ZERO_STRUCT(response
);
4306 fstrcpy(request
.data
.username
, full_name
);
4308 result
= winbindd_request_response(WINBINDD_GETGROUPS
, &request
, &response
);
4310 if (result
!= NSS_STATUS_SUCCESS
) {
4311 DEBUG(1, ("winbind could not get groups of %s\n", full_name
));
4315 for (i
= 0; i
< response
.data
.num_entries
; i
++) {
4316 gid_t gid
= ((gid_t
*)response
.extra_data
.data
)[i
];
4319 struct winbindd_request sidrequest
;
4320 struct winbindd_response sidresponse
;
4322 ZERO_STRUCT(sidrequest
);
4323 ZERO_STRUCT(sidresponse
);
4325 sidrequest
.data
.gid
= gid
;
4327 result
= winbindd_request_response(WINBINDD_GID_TO_SID
,
4328 &sidrequest
, &sidresponse
);
4330 if (result
!= NSS_STATUS_SUCCESS
) {
4331 DEBUG(1, ("winbind could not find SID of gid %d\n",
4336 DEBUG(3, (" %s\n", sidresponse
.data
.sid
.sid
));
4338 string_to_sid(&sid
, sidresponse
.data
.sid
.sid
);
4339 add_sid_to_token(token
, &sid
);
4342 SAFE_FREE(response
.extra_data
.data
);
4348 * Get a list of all user tokens we want to look at
4351 static BOOL
get_user_tokens(int *num_tokens
, struct user_token
**user_tokens
)
4353 struct winbindd_request request
;
4354 struct winbindd_response response
;
4355 const char *extra_data
;
4358 struct user_token
*result
;
4360 if (lp_winbind_use_default_domain() &&
4361 (opt_target_workgroup
== NULL
)) {
4362 d_fprintf(stderr
, "winbind use default domain = yes set, "
4363 "please specify a workgroup\n");
4367 /* Send request to winbind daemon */
4369 ZERO_STRUCT(request
);
4370 ZERO_STRUCT(response
);
4372 if (winbindd_request_response(WINBINDD_LIST_USERS
, &request
, &response
) !=
4376 /* Look through extra data */
4378 if (!response
.extra_data
.data
)
4381 extra_data
= (const char *)response
.extra_data
.data
;
4384 while(next_token(&extra_data
, name
, ",", sizeof(fstring
))) {
4388 result
= SMB_MALLOC_ARRAY(struct user_token
, *num_tokens
);
4390 if (result
== NULL
) {
4391 DEBUG(1, ("Could not malloc sid array\n"));
4395 extra_data
= (const char *)response
.extra_data
.data
;
4398 while(next_token(&extra_data
, name
, ",", sizeof(fstring
))) {
4400 fstring domain
, user
;
4403 fstrcpy(result
[i
].name
, name
);
4405 p
= strchr(name
, *lp_winbind_separator());
4407 DEBUG(3, ("%s\n", name
));
4410 fstrcpy(domain
, opt_target_workgroup
);
4411 fstrcpy(user
, name
);
4414 fstrcpy(domain
, name
);
4419 get_user_sids(domain
, user
, &(result
[i
].token
));
4423 SAFE_FREE(response
.extra_data
.data
);
4425 *user_tokens
= result
;
4430 static BOOL
get_user_tokens_from_file(FILE *f
,
4432 struct user_token
**tokens
)
4434 struct user_token
*token
= NULL
;
4439 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4443 if (line
[strlen(line
)-1] == '\n')
4444 line
[strlen(line
)-1] = '\0';
4446 if (line
[0] == ' ') {
4450 string_to_sid(&sid
, &line
[1]);
4452 if (token
== NULL
) {
4453 DEBUG(0, ("File does not begin with username"));
4457 add_sid_to_token(&token
->token
, &sid
);
4461 /* And a new user... */
4464 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4465 if (*tokens
== NULL
) {
4466 DEBUG(0, ("Could not realloc tokens\n"));
4470 token
= &((*tokens
)[*num_tokens
-1]);
4472 fstrcpy(token
->name
, line
);
4473 token
->token
.num_sids
= 0;
4474 token
->token
.user_sids
= NULL
;
4483 * Show the list of all users that have access to a share
4486 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4487 TALLOC_CTX
*mem_ctx
,
4488 const char *netname
,
4490 struct user_token
*tokens
)
4493 SEC_DESC
*share_sd
= NULL
;
4494 SEC_DESC
*root_sd
= NULL
;
4495 struct cli_state
*cli
= pipe_hnd
->cli
;
4497 SRV_SHARE_INFO info
;
4501 result
= rpccli_srvsvc_net_share_get_info(pipe_hnd
, mem_ctx
, netname
,
4504 if (!W_ERROR_IS_OK(result
)) {
4505 DEBUG(1, ("Coult not query secdesc for share %s\n",
4510 share_sd
= info
.share
.info502
.info_502_str
.sd
;
4511 if (share_sd
== NULL
) {
4512 DEBUG(1, ("Got no secdesc for share %s\n",
4518 if (!cli_send_tconX(cli
, netname
, "A:", "", 0)) {
4522 fnum
= cli_nt_create(cli
, "\\", READ_CONTROL_ACCESS
);
4525 root_sd
= cli_query_secdesc(cli
, fnum
, mem_ctx
);
4528 for (i
=0; i
<num_tokens
; i
++) {
4532 if (share_sd
!= NULL
) {
4533 if (!se_access_check(share_sd
, &tokens
[i
].token
,
4534 1, &acc_granted
, &status
)) {
4535 DEBUG(1, ("Could not check share_sd for "
4541 if (!NT_STATUS_IS_OK(status
))
4545 if (root_sd
== NULL
) {
4546 d_printf(" %s\n", tokens
[i
].name
);
4550 if (!se_access_check(root_sd
, &tokens
[i
].token
,
4551 1, &acc_granted
, &status
)) {
4552 DEBUG(1, ("Could not check root_sd for user %s\n",
4557 if (!NT_STATUS_IS_OK(status
))
4560 d_printf(" %s\n", tokens
[i
].name
);
4564 cli_close(cli
, fnum
);
4576 static void collect_share(const char *name
, uint32 m
,
4577 const char *comment
, void *state
)
4579 struct share_list
*share_list
= (struct share_list
*)state
;
4581 if (m
!= STYPE_DISKTREE
)
4584 share_list
->num_shares
+= 1;
4585 share_list
->shares
= SMB_REALLOC_ARRAY(share_list
->shares
, char *, share_list
->num_shares
);
4586 if (!share_list
->shares
) {
4587 share_list
->num_shares
= 0;
4590 share_list
->shares
[share_list
->num_shares
-1] = SMB_STRDUP(name
);
4593 static void rpc_share_userlist_usage(void)
4599 * List shares on a remote RPC server, including the security descriptors
4601 * All parameters are provided by the run_rpc_command function, except for
4602 * argc, argv which are passes through.
4604 * @param domain_sid The domain sid acquired from the remote server
4605 * @param cli A cli_state connected to the server.
4606 * @param mem_ctx Talloc context, destoyed on completion of the function.
4607 * @param argc Standard main() style argc
4608 * @param argv Standard main() style argv. Initial components are already
4611 * @return Normal NTSTATUS return.
4614 static NTSTATUS
rpc_share_allowedusers_internals(const DOM_SID
*domain_sid
,
4615 const char *domain_name
,
4616 struct cli_state
*cli
,
4617 struct rpc_pipe_client
*pipe_hnd
,
4618 TALLOC_CTX
*mem_ctx
,
4628 struct user_token
*tokens
= NULL
;
4631 struct share_list share_list
;
4634 rpc_share_userlist_usage();
4635 return NT_STATUS_UNSUCCESSFUL
;
4641 f
= fopen(argv
[0], "r");
4645 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4646 return NT_STATUS_UNSUCCESSFUL
;
4649 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
4655 DEBUG(0, ("Could not read users from file\n"));
4656 return NT_STATUS_UNSUCCESSFUL
;
4659 for (i
=0; i
<num_tokens
; i
++)
4660 collect_alias_memberships(&tokens
[i
].token
);
4662 init_enum_hnd(&hnd
, 0);
4664 share_list
.num_shares
= 0;
4665 share_list
.shares
= NULL
;
4667 ret
= cli_RNetShareEnum(cli
, collect_share
, &share_list
);
4670 DEBUG(0, ("Error returning browse list: %s\n",
4675 for (i
= 0; i
< share_list
.num_shares
; i
++) {
4676 char *netname
= share_list
.shares
[i
];
4678 if (netname
[strlen(netname
)-1] == '$')
4681 d_printf("%s\n", netname
);
4683 show_userlist(pipe_hnd
, mem_ctx
, netname
,
4684 num_tokens
, tokens
);
4687 for (i
=0; i
<num_tokens
; i
++) {
4688 free_user_token(&tokens
[i
].token
);
4691 SAFE_FREE(share_list
.shares
);
4693 return NT_STATUS_OK
;
4696 static int rpc_share_allowedusers(int argc
, const char **argv
)
4700 result
= run_rpc_command(NULL
, PI_SAMR
, 0,
4701 rpc_aliaslist_internals
,
4706 result
= run_rpc_command(NULL
, PI_LSARPC
, 0,
4712 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
4713 rpc_share_allowedusers_internals
,
4717 int net_usersidlist(int argc
, const char **argv
)
4720 struct user_token
*tokens
= NULL
;
4724 net_usersidlist_usage(argc
, argv
);
4728 if (!get_user_tokens(&num_tokens
, &tokens
)) {
4729 DEBUG(0, ("Could not get the user/sid list\n"));
4733 for (i
=0; i
<num_tokens
; i
++) {
4734 dump_user_token(&tokens
[i
]);
4735 free_user_token(&tokens
[i
].token
);
4742 int net_usersidlist_usage(int argc
, const char **argv
)
4744 d_printf("net usersidlist\n"
4745 "\tprints out a list of all users the running winbind knows\n"
4746 "\tabout, together with all their SIDs. This is used as\n"
4747 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4749 net_common_flags_usage(argc
, argv
);
4754 * 'net rpc share' entrypoint.
4755 * @param argc Standard main() style argc
4756 * @param argv Standard main() style argv. Initial components are already
4760 int net_rpc_share(int argc
, const char **argv
)
4762 struct functable func
[] = {
4763 {"add", rpc_share_add
},
4764 {"delete", rpc_share_delete
},
4765 {"allowedusers", rpc_share_allowedusers
},
4766 {"migrate", rpc_share_migrate
},
4767 {"list", rpc_share_list
},
4772 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
4773 rpc_share_list_internals
,
4776 return net_run_function(argc
, argv
, func
, rpc_share_usage
);
4779 static NTSTATUS
rpc_sh_share_list(TALLOC_CTX
*mem_ctx
,
4780 struct rpc_sh_ctx
*ctx
,
4781 struct rpc_pipe_client
*pipe_hnd
,
4782 int argc
, const char **argv
)
4784 return rpc_share_list_internals(ctx
->domain_sid
, ctx
->domain_name
,
4785 ctx
->cli
, pipe_hnd
, mem_ctx
,
4789 static NTSTATUS
rpc_sh_share_add(TALLOC_CTX
*mem_ctx
,
4790 struct rpc_sh_ctx
*ctx
,
4791 struct rpc_pipe_client
*pipe_hnd
,
4792 int argc
, const char **argv
)
4796 if ((argc
< 2) || (argc
> 3)) {
4797 d_fprintf(stderr
, "usage: %s <share> <path> [comment]\n",
4799 return NT_STATUS_INVALID_PARAMETER
;
4802 result
= rpccli_srvsvc_net_share_add(
4803 pipe_hnd
, mem_ctx
, argv
[0], STYPE_DISKTREE
,
4804 (argc
== 3) ? argv
[2] : "",
4805 0, 0, 0, argv
[1], NULL
, 2, NULL
);
4807 return werror_to_ntstatus(result
);
4810 static NTSTATUS
rpc_sh_share_delete(TALLOC_CTX
*mem_ctx
,
4811 struct rpc_sh_ctx
*ctx
,
4812 struct rpc_pipe_client
*pipe_hnd
,
4813 int argc
, const char **argv
)
4818 d_fprintf(stderr
, "usage: %s <share>\n", ctx
->whoami
);
4819 return NT_STATUS_INVALID_PARAMETER
;
4822 result
= rpccli_srvsvc_net_share_del(pipe_hnd
, mem_ctx
, argv
[0]);
4823 return werror_to_ntstatus(result
);
4826 static NTSTATUS
rpc_sh_share_info(TALLOC_CTX
*mem_ctx
,
4827 struct rpc_sh_ctx
*ctx
,
4828 struct rpc_pipe_client
*pipe_hnd
,
4829 int argc
, const char **argv
)
4831 SRV_SHARE_INFO info
;
4832 SRV_SHARE_INFO_2
*info2
= &info
.share
.info2
;
4836 d_fprintf(stderr
, "usage: %s <share>\n", ctx
->whoami
);
4837 return NT_STATUS_INVALID_PARAMETER
;
4840 result
= rpccli_srvsvc_net_share_get_info(
4841 pipe_hnd
, mem_ctx
, argv
[0], 2, &info
);
4842 if (!W_ERROR_IS_OK(result
)) {
4846 d_printf("Name: %s\n",
4847 rpcstr_pull_unistr2_talloc(mem_ctx
,
4848 &info2
->info_2_str
.uni_netname
));
4849 d_printf("Comment: %s\n",
4850 rpcstr_pull_unistr2_talloc(mem_ctx
,
4851 &info2
->info_2_str
.uni_remark
));
4853 d_printf("Path: %s\n",
4854 rpcstr_pull_unistr2_talloc(mem_ctx
,
4855 &info2
->info_2_str
.uni_path
));
4856 d_printf("Password: %s\n",
4857 rpcstr_pull_unistr2_talloc(mem_ctx
,
4858 &info2
->info_2_str
.uni_passwd
));
4861 return werror_to_ntstatus(result
);
4864 struct rpc_sh_cmd
*net_rpc_share_cmds(TALLOC_CTX
*mem_ctx
,
4865 struct rpc_sh_ctx
*ctx
)
4867 static struct rpc_sh_cmd cmds
[] = {
4869 { "list", NULL
, PI_SRVSVC
, rpc_sh_share_list
,
4870 "List available shares" },
4872 { "add", NULL
, PI_SRVSVC
, rpc_sh_share_add
,
4875 { "delete", NULL
, PI_SRVSVC
, rpc_sh_share_delete
,
4878 { "info", NULL
, PI_SRVSVC
, rpc_sh_share_info
,
4879 "Get information about a share" },
4881 { NULL
, NULL
, 0, NULL
, NULL
}
4887 /****************************************************************************/
4889 static int rpc_file_usage(int argc
, const char **argv
)
4891 return net_help_file(argc
, argv
);
4895 * Close a file on a remote RPC server
4897 * All parameters are provided by the run_rpc_command function, except for
4898 * argc, argv which are passes through.
4900 * @param domain_sid The domain sid acquired from the remote server
4901 * @param cli A cli_state connected to the server.
4902 * @param mem_ctx Talloc context, destoyed on completion of the function.
4903 * @param argc Standard main() style argc
4904 * @param argv Standard main() style argv. Initial components are already
4907 * @return Normal NTSTATUS return.
4909 static NTSTATUS
rpc_file_close_internals(const DOM_SID
*domain_sid
,
4910 const char *domain_name
,
4911 struct cli_state
*cli
,
4912 struct rpc_pipe_client
*pipe_hnd
,
4913 TALLOC_CTX
*mem_ctx
,
4918 result
= rpccli_srvsvc_net_file_close(pipe_hnd
, mem_ctx
, atoi(argv
[0]));
4919 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
4923 * Close a file on a remote RPC server
4925 * @param argc Standard main() style argc
4926 * @param argv Standard main() style argv. Initial components are already
4929 * @return A shell status integer (0 for success)
4931 static int rpc_file_close(int argc
, const char **argv
)
4934 DEBUG(1, ("No fileid given on close\n"));
4935 return(rpc_file_usage(argc
, argv
));
4938 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
4939 rpc_file_close_internals
,
4944 * Formatted print of open file info
4946 * @param info3 FILE_INFO_3 contents
4947 * @param str3 strings for FILE_INFO_3
4950 static void display_file_info_3( FILE_INFO_3
*info3
)
4952 fstring user
= "", path
= "";
4954 rpcstr_pull_unistr2_fstring(user
, info3
->user
);
4955 rpcstr_pull_unistr2_fstring(path
, info3
->path
);
4957 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4958 info3
->id
, user
, info3
->perms
, info3
->num_locks
, path
);
4962 * List open files on a remote RPC server
4964 * All parameters are provided by the run_rpc_command function, except for
4965 * argc, argv which are passes through.
4967 * @param domain_sid The domain sid acquired from the remote server
4968 * @param cli A cli_state connected to the server.
4969 * @param mem_ctx Talloc context, destoyed on completion of the function.
4970 * @param argc Standard main() style argc
4971 * @param argv Standard main() style argv. Initial components are already
4974 * @return Normal NTSTATUS return.
4977 static NTSTATUS
rpc_file_list_internals(const DOM_SID
*domain_sid
,
4978 const char *domain_name
,
4979 struct cli_state
*cli
,
4980 struct rpc_pipe_client
*pipe_hnd
,
4981 TALLOC_CTX
*mem_ctx
,
4985 SRV_FILE_INFO_CTR ctr
;
4988 uint32 preferred_len
= 0xffffffff, i
;
4989 const char *username
=NULL
;
4991 init_enum_hnd(&hnd
, 0);
4993 /* if argc > 0, must be user command */
4995 username
= smb_xstrdup(argv
[0]);
4997 result
= rpccli_srvsvc_net_file_enum(pipe_hnd
,
4998 mem_ctx
, 3, username
, &ctr
, preferred_len
, &hnd
);
5000 if (!W_ERROR_IS_OK(result
))
5003 /* Display results */
5006 "\nEnumerating open files on remote server:\n\n"\
5007 "\nFileId Opened by Perms Locks Path"\
5008 "\n------ --------- ----- ----- ---- \n");
5009 for (i
= 0; i
< ctr
.num_entries
; i
++)
5010 display_file_info_3(&ctr
.file
.info3
[i
]);
5012 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
5016 * List files for a user on a remote RPC server
5018 * @param argc Standard main() style argc
5019 * @param argv Standard main() style argv. Initial components are already
5022 * @return A shell status integer (0 for success)
5025 static int rpc_file_user(int argc
, const char **argv
)
5028 DEBUG(1, ("No username given\n"));
5029 return(rpc_file_usage(argc
, argv
));
5032 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
5033 rpc_file_list_internals
,
5038 * 'net rpc file' entrypoint.
5039 * @param argc Standard main() style argc
5040 * @param argv Standard main() style argv. Initial components are already
5044 int net_rpc_file(int argc
, const char **argv
)
5046 struct functable func
[] = {
5047 {"close", rpc_file_close
},
5048 {"user", rpc_file_user
},
5050 {"info", rpc_file_info
},
5056 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
5057 rpc_file_list_internals
,
5060 return net_run_function(argc
, argv
, func
, rpc_file_usage
);
5064 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
5066 * All parameters are provided by the run_rpc_command function, except for
5067 * argc, argv which are passed through.
5069 * @param domain_sid The domain sid aquired from the remote server
5070 * @param cli A cli_state connected to the server.
5071 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5072 * @param argc Standard main() style argc
5073 * @param argv Standard main() style argv. Initial components are already
5076 * @return Normal NTSTATUS return.
5079 static NTSTATUS
rpc_shutdown_abort_internals(const DOM_SID
*domain_sid
,
5080 const char *domain_name
,
5081 struct cli_state
*cli
,
5082 struct rpc_pipe_client
*pipe_hnd
,
5083 TALLOC_CTX
*mem_ctx
,
5087 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5089 result
= rpccli_shutdown_abort(pipe_hnd
, mem_ctx
);
5091 if (NT_STATUS_IS_OK(result
)) {
5092 d_printf("\nShutdown successfully aborted\n");
5093 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5095 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5101 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5103 * All parameters are provided by the run_rpc_command function, except for
5104 * argc, argv which are passed through.
5106 * @param domain_sid The domain sid aquired from the remote server
5107 * @param cli A cli_state connected to the server.
5108 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5109 * @param argc Standard main() style argc
5110 * @param argv Standard main() style argv. Initial components are already
5113 * @return Normal NTSTATUS return.
5116 static NTSTATUS
rpc_reg_shutdown_abort_internals(const DOM_SID
*domain_sid
,
5117 const char *domain_name
,
5118 struct cli_state
*cli
,
5119 struct rpc_pipe_client
*pipe_hnd
,
5120 TALLOC_CTX
*mem_ctx
,
5124 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5126 result
= werror_to_ntstatus(rpccli_reg_abort_shutdown(pipe_hnd
, mem_ctx
));
5128 if (NT_STATUS_IS_OK(result
)) {
5129 d_printf("\nShutdown successfully aborted\n");
5130 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5132 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5138 * ABORT the Shut down of a remote RPC server
5140 * @param argc Standard main() style argc
5141 * @param argv Standard main() style argv. Initial components are already
5144 * @return A shell status integer (0 for success)
5147 static int rpc_shutdown_abort(int argc
, const char **argv
)
5149 int rc
= run_rpc_command(NULL
, PI_SHUTDOWN
, 0,
5150 rpc_shutdown_abort_internals
,
5156 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5158 return run_rpc_command(NULL
, PI_WINREG
, 0,
5159 rpc_reg_shutdown_abort_internals
,
5164 * Shut down a remote RPC Server via initshutdown pipe
5166 * All parameters are provided by the run_rpc_command function, except for
5167 * argc, argv which are passes through.
5169 * @param domain_sid The domain sid aquired from the remote server
5170 * @param cli A cli_state connected to the server.
5171 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5172 * @param argc Standard main() style argc
5173 * @param argc Standard main() style argv. Initial components are already
5176 * @return Normal NTSTATUS return.
5179 static NTSTATUS
rpc_init_shutdown_internals(const DOM_SID
*domain_sid
,
5180 const char *domain_name
,
5181 struct cli_state
*cli
,
5182 struct rpc_pipe_client
*pipe_hnd
,
5183 TALLOC_CTX
*mem_ctx
,
5187 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5188 const char *msg
= "This machine will be shutdown shortly";
5189 uint32 timeout
= 20;
5195 timeout
= opt_timeout
;
5198 /* create an entry */
5199 result
= rpccli_shutdown_init(pipe_hnd
, mem_ctx
, msg
, timeout
, opt_reboot
,
5202 if (NT_STATUS_IS_OK(result
)) {
5203 d_printf("\nShutdown of remote machine succeeded\n");
5204 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5206 DEBUG(1,("Shutdown of remote machine failed!\n"));
5212 * Shut down a remote RPC Server via winreg pipe
5214 * All parameters are provided by the run_rpc_command function, except for
5215 * argc, argv which are passes through.
5217 * @param domain_sid The domain sid aquired from the remote server
5218 * @param cli A cli_state connected to the server.
5219 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5220 * @param argc Standard main() style argc
5221 * @param argc Standard main() style argv. Initial components are already
5224 * @return Normal NTSTATUS return.
5227 static NTSTATUS
rpc_reg_shutdown_internals(const DOM_SID
*domain_sid
,
5228 const char *domain_name
,
5229 struct cli_state
*cli
,
5230 struct rpc_pipe_client
*pipe_hnd
,
5231 TALLOC_CTX
*mem_ctx
,
5236 const char *msg
= "This machine will be shutdown shortly";
5237 uint32 timeout
= 20;
5242 struct poptOption long_options
[] = {
5243 {"message", 'm', POPT_ARG_STRING
, &msg
},
5244 {"timeout", 't', POPT_ARG_INT
, &timeout
},
5245 {"reboot", 'r', POPT_ARG_NONE
, &reboot
},
5246 {"force", 'f', POPT_ARG_NONE
, &force
},
5250 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
,
5251 POPT_CONTEXT_KEEP_FIRST
);
5253 rc
= poptGetNextOpt(pc
);
5256 /* an error occurred during option processing */
5257 DEBUG(0, ("%s: %s\n",
5258 poptBadOption(pc
, POPT_BADOPTION_NOALIAS
),
5260 return NT_STATUS_INVALID_PARAMETER
;
5267 timeout
= opt_timeout
;
5270 /* create an entry */
5271 result
= rpccli_reg_shutdown(pipe_hnd
, mem_ctx
, msg
, timeout
, opt_reboot
, opt_force
);
5273 if (W_ERROR_IS_OK(result
)) {
5274 d_printf("\nShutdown of remote machine succeeded\n");
5276 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5277 if (W_ERROR_EQUAL(result
,WERR_MACHINE_LOCKED
))
5278 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5280 d_fprintf(stderr
, "\nresult was: %s\n", dos_errstr(result
));
5283 return werror_to_ntstatus(result
);
5287 * Shut down a remote RPC server
5289 * @param argc Standard main() style argc
5290 * @param argc Standard main() style argv. Initial components are already
5293 * @return A shell status integer (0 for success)
5296 static int rpc_shutdown(int argc
, const char **argv
)
5298 int rc
= run_rpc_command(NULL
, PI_SHUTDOWN
, 0,
5299 rpc_init_shutdown_internals
,
5303 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5304 rc
= run_rpc_command(NULL
, PI_WINREG
, 0,
5305 rpc_reg_shutdown_internals
, argc
, argv
);
5311 /***************************************************************************
5312 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5314 ***************************************************************************/
5317 * Add interdomain trust account to the RPC server.
5318 * All parameters (except for argc and argv) are passed by run_rpc_command
5321 * @param domain_sid The domain sid acquired from the server
5322 * @param cli A cli_state connected to the server.
5323 * @param mem_ctx Talloc context, destoyed on completion of the function.
5324 * @param argc Standard main() style argc
5325 * @param argc Standard main() style argv. Initial components are already
5328 * @return normal NTSTATUS return code
5331 static NTSTATUS
rpc_trustdom_add_internals(const DOM_SID
*domain_sid
,
5332 const char *domain_name
,
5333 struct cli_state
*cli
,
5334 struct rpc_pipe_client
*pipe_hnd
,
5335 TALLOC_CTX
*mem_ctx
,
5339 POLICY_HND connect_pol
, domain_pol
, user_pol
;
5340 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5344 uint32 acct_flags
=0;
5347 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5348 return NT_STATUS_INVALID_PARAMETER
;
5352 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5355 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5356 return NT_STATUS_NO_MEMORY
;
5359 strupper_m(acct_name
);
5361 /* Get samr policy handle */
5362 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
5364 if (!NT_STATUS_IS_OK(result
)) {
5368 /* Get domain policy handle */
5369 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
5370 MAXIMUM_ALLOWED_ACCESS
,
5371 domain_sid
, &domain_pol
);
5372 if (!NT_STATUS_IS_OK(result
)) {
5376 /* Create trusting domain's account */
5377 acb_info
= ACB_NORMAL
;
5378 acct_flags
= SAMR_GENERIC_READ
| SAMR_GENERIC_WRITE
|
5379 SAMR_GENERIC_EXECUTE
| SAMR_STANDARD_WRITEDAC
|
5380 SAMR_STANDARD_DELETE
| SAMR_USER_SETPASS
| SAMR_USER_GETATTR
|
5383 result
= rpccli_samr_create_dom_user(pipe_hnd
, mem_ctx
, &domain_pol
,
5384 acct_name
, acb_info
, acct_flags
,
5385 &user_pol
, &user_rid
);
5386 if (!NT_STATUS_IS_OK(result
)) {
5391 SAM_USERINFO_CTR ctr
;
5392 SAM_USER_INFO_23 p23
;
5398 encode_pw_buffer(pwbuf
, argv
[1], STR_UNICODE
);
5402 ZERO_STRUCT(notime
);
5406 memset(hrs
.hours
, 0xFF, sizeof(hrs
.hours
));
5407 acb_info
= ACB_DOMTRUST
;
5409 init_sam_user_info23A(&p23
, ¬ime
, ¬ime
, ¬ime
,
5410 ¬ime
, ¬ime
, ¬ime
,
5411 nostr
, nostr
, nostr
, nostr
, nostr
,
5412 nostr
, nostr
, nostr
, nostr
, nostr
,
5413 0, 0, acb_info
, ACCT_FLAGS
, 168, &hrs
,
5414 0, 0, (char *)pwbuf
);
5415 ctr
.switch_value
= 23;
5416 ctr
.info
.id23
= &p23
;
5417 p23
.passmustchange
= 0;
5419 result
= rpccli_samr_set_userinfo(pipe_hnd
, mem_ctx
, &user_pol
, 23,
5420 &cli
->user_session_key
, &ctr
);
5422 if (!NT_STATUS_IS_OK(result
)) {
5423 DEBUG(0,("Could not set trust account password: %s\n",
5424 nt_errstr(result
)));
5430 SAFE_FREE(acct_name
);
5435 * Create interdomain trust account for a remote domain.
5437 * @param argc standard argc
5438 * @param argv standard argv without initial components
5440 * @return Integer status (0 means success)
5443 static int rpc_trustdom_add(int argc
, const char **argv
)
5446 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_trustdom_add_internals
,
5449 d_printf("Usage: net rpc trustdom add <domain> <trust password>\n");
5456 * Remove interdomain trust account from the RPC server.
5457 * All parameters (except for argc and argv) are passed by run_rpc_command
5460 * @param domain_sid The domain sid acquired from the server
5461 * @param cli A cli_state connected to the server.
5462 * @param mem_ctx Talloc context, destoyed on completion of the function.
5463 * @param argc Standard main() style argc
5464 * @param argc Standard main() style argv. Initial components are already
5467 * @return normal NTSTATUS return code
5470 static NTSTATUS
rpc_trustdom_del_internals(const DOM_SID
*domain_sid
,
5471 const char *domain_name
,
5472 struct cli_state
*cli
,
5473 struct rpc_pipe_client
*pipe_hnd
,
5474 TALLOC_CTX
*mem_ctx
,
5478 POLICY_HND connect_pol
, domain_pol
, user_pol
;
5479 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5482 DOM_SID trust_acct_sid
;
5483 uint32
*user_rids
, num_rids
, *name_types
;
5484 uint32 flags
= 0x000003e8; /* Unknown */
5487 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5488 return NT_STATUS_INVALID_PARAMETER
;
5492 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5494 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
5496 if (acct_name
== NULL
)
5497 return NT_STATUS_NO_MEMORY
;
5499 strupper_m(acct_name
);
5501 if ((names
= TALLOC_ARRAY(mem_ctx
, const char *, 1)) == NULL
) {
5502 return NT_STATUS_NO_MEMORY
;
5504 names
[0] = acct_name
;
5507 /* Get samr policy handle */
5508 result
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, MAXIMUM_ALLOWED_ACCESS
,
5510 if (!NT_STATUS_IS_OK(result
)) {
5514 /* Get domain policy handle */
5515 result
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_pol
,
5516 MAXIMUM_ALLOWED_ACCESS
,
5517 domain_sid
, &domain_pol
);
5518 if (!NT_STATUS_IS_OK(result
)) {
5522 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
, flags
, 1,
5524 &user_rids
, &name_types
);
5526 if (!NT_STATUS_IS_OK(result
)) {
5530 result
= rpccli_samr_open_user(pipe_hnd
, mem_ctx
, &domain_pol
,
5531 MAXIMUM_ALLOWED_ACCESS
,
5532 user_rids
[0], &user_pol
);
5534 if (!NT_STATUS_IS_OK(result
)) {
5538 /* append the rid to the domain sid */
5539 sid_copy(&trust_acct_sid
, domain_sid
);
5540 if (!sid_append_rid(&trust_acct_sid
, user_rids
[0])) {
5544 /* remove the sid */
5546 result
= rpccli_samr_remove_sid_foreign_domain(pipe_hnd
, mem_ctx
, &user_pol
,
5549 if (!NT_STATUS_IS_OK(result
)) {
5555 result
= rpccli_samr_delete_dom_user(pipe_hnd
, mem_ctx
, &user_pol
);
5557 if (!NT_STATUS_IS_OK(result
)) {
5561 if (!NT_STATUS_IS_OK(result
)) {
5562 DEBUG(0,("Could not set trust account password: %s\n",
5563 nt_errstr(result
)));
5572 * Delete interdomain trust account for a remote domain.
5574 * @param argc standard argc
5575 * @param argv standard argv without initial components
5577 * @return Integer status (0 means success)
5580 static int rpc_trustdom_del(int argc
, const char **argv
)
5583 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_trustdom_del_internals
,
5586 d_printf("Usage: net rpc trustdom del <domain>\n");
5593 * Establish trust relationship to a trusting domain.
5594 * Interdomain account must already be created on remote PDC.
5596 * @param argc standard argc
5597 * @param argv standard argv without initial components
5599 * @return Integer status (0 means success)
5602 static int rpc_trustdom_establish(int argc
, const char **argv
)
5604 struct cli_state
*cli
= NULL
;
5605 struct in_addr server_ip
;
5606 struct rpc_pipe_client
*pipe_hnd
= NULL
;
5607 POLICY_HND connect_hnd
;
5608 TALLOC_CTX
*mem_ctx
;
5610 DOM_SID
*domain_sid
;
5613 char* domain_name_pol
;
5618 * Connect to \\server\ipc$ as 'our domain' account with password
5622 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5626 domain_name
= smb_xstrdup(argv
[0]);
5627 strupper_m(domain_name
);
5629 /* account name used at first is our domain's name with '$' */
5630 asprintf(&acct_name
, "%s$", lp_workgroup());
5631 strupper_m(acct_name
);
5634 * opt_workgroup will be used by connection functions further,
5635 * hence it should be set to remote domain name instead of ours
5637 if (opt_workgroup
) {
5638 opt_workgroup
= smb_xstrdup(domain_name
);
5641 opt_user_name
= acct_name
;
5643 /* find the domain controller */
5644 if (!net_find_pdc(&server_ip
, pdc_name
, domain_name
)) {
5645 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
5649 /* connect to ipc$ as username/password */
5650 nt_status
= connect_to_ipc(&cli
, &server_ip
, pdc_name
);
5651 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
5653 /* Is it trusting domain account for sure ? */
5654 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5655 nt_errstr(nt_status
)));
5659 /* store who we connected to */
5661 saf_store( domain_name
, pdc_name
);
5664 * Connect to \\server\ipc$ again (this time anonymously)
5667 nt_status
= connect_to_ipc_anonymous(&cli
, &server_ip
, (char*)pdc_name
);
5669 if (NT_STATUS_IS_ERR(nt_status
)) {
5670 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5671 domain_name
, nt_errstr(nt_status
)));
5676 * Use NetServerEnum2 to make sure we're talking to a proper server
5679 if (!cli_get_pdc_name(cli
, domain_name
, (char*)pdc_name
)) {
5680 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
5681 for domain %s\n", domain_name
));
5686 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
5687 "domain %s", domain_name
))) {
5688 DEBUG(0, ("talloc_init() failed\n"));
5694 * Call LsaOpenPolicy and LsaQueryInfo
5697 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &nt_status
);
5699 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
5701 talloc_destroy(mem_ctx
);
5705 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, True
, SEC_RIGHTS_QUERY_VALUE
,
5707 if (NT_STATUS_IS_ERR(nt_status
)) {
5708 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5709 nt_errstr(nt_status
)));
5711 talloc_destroy(mem_ctx
);
5715 /* Querying info level 5 */
5717 nt_status
= rpccli_lsa_query_info_policy(pipe_hnd
, mem_ctx
, &connect_hnd
,
5719 &domain_name_pol
, &domain_sid
);
5720 if (NT_STATUS_IS_ERR(nt_status
)) {
5721 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5722 nt_errstr(nt_status
)));
5724 talloc_destroy(mem_ctx
);
5728 /* There should be actually query info level 3 (following nt serv behaviour),
5729 but I still don't know if it's _really_ necessary */
5732 * Store the password in secrets db
5735 if (!secrets_store_trusted_domain_password(domain_name
,
5738 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5740 talloc_destroy(mem_ctx
);
5745 * Close the pipes and clean up
5748 nt_status
= rpccli_lsa_close(pipe_hnd
, mem_ctx
, &connect_hnd
);
5749 if (NT_STATUS_IS_ERR(nt_status
)) {
5750 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5751 nt_errstr(nt_status
)));
5753 talloc_destroy(mem_ctx
);
5759 talloc_destroy(mem_ctx
);
5761 d_printf("Trust to domain %s established\n", domain_name
);
5766 * Revoke trust relationship to the remote domain
5768 * @param argc standard argc
5769 * @param argv standard argv without initial components
5771 * @return Integer status (0 means success)
5774 static int rpc_trustdom_revoke(int argc
, const char **argv
)
5778 if (argc
< 1) return -1;
5780 /* generate upper cased domain name */
5781 domain_name
= smb_xstrdup(argv
[0]);
5782 strupper_m(domain_name
);
5784 /* delete password of the trust */
5785 if (!trusted_domain_password_delete(domain_name
)) {
5786 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5795 * Usage for 'net rpc trustdom' command
5797 * @param argc standard argc
5798 * @param argv standard argv without inital components
5800 * @return Integer status returned to shell
5803 static int rpc_trustdom_usage(int argc
, const char **argv
)
5805 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
5806 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
5807 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
5808 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5809 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
5810 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5815 static NTSTATUS
rpc_query_domain_sid(const DOM_SID
*domain_sid
,
5816 const char *domain_name
,
5817 struct cli_state
*cli
,
5818 struct rpc_pipe_client
*pipe_hnd
,
5819 TALLOC_CTX
*mem_ctx
,
5824 sid_to_string(str_sid
, domain_sid
);
5825 d_printf("%s\n", str_sid
);
5826 return NT_STATUS_OK
;
5829 static void print_trusted_domain(DOM_SID
*dom_sid
, const char *trusted_dom_name
)
5831 fstring ascii_sid
, padding
;
5832 int pad_len
, col_len
= 20;
5834 /* convert sid into ascii string */
5835 sid_to_string(ascii_sid
, dom_sid
);
5837 /* calculate padding space for d_printf to look nicer */
5838 pad_len
= col_len
- strlen(trusted_dom_name
);
5839 padding
[pad_len
] = 0;
5840 do padding
[--pad_len
] = ' '; while (pad_len
);
5842 d_printf("%s%s%s\n", trusted_dom_name
, padding
, ascii_sid
);
5845 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
5846 TALLOC_CTX
*mem_ctx
,
5849 const char *trusted_dom_name
)
5852 LSA_TRUSTED_DOMAIN_INFO
*info
;
5853 char *cleartextpwd
= NULL
;
5856 nt_status
= rpccli_lsa_query_trusted_domain_info_by_sid(pipe_hnd
, mem_ctx
, pol
, 4, &dom_sid
, &info
);
5858 if (NT_STATUS_IS_ERR(nt_status
)) {
5859 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5860 nt_errstr(nt_status
)));
5864 data
= data_blob(NULL
, info
->password
.password
.length
);
5866 memcpy(data
.data
, info
->password
.password
.data
, info
->password
.password
.length
);
5867 data
.length
= info
->password
.password
.length
;
5869 cleartextpwd
= decrypt_trustdom_secret(pipe_hnd
->cli
->pwd
.password
, &data
);
5871 if (cleartextpwd
== NULL
) {
5872 DEBUG(0,("retrieved NULL password\n"));
5873 nt_status
= NT_STATUS_UNSUCCESSFUL
;
5877 if (!secrets_store_trusted_domain_password(trusted_dom_name
,
5880 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5881 nt_status
= NT_STATUS_UNSUCCESSFUL
;
5885 #ifdef DEBUG_PASSWORD
5886 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], password: [%s]\n",
5887 trusted_dom_name
, sid_string_static(&dom_sid
), cleartextpwd
));
5891 SAFE_FREE(cleartextpwd
);
5892 data_blob_free(&data
);
5897 static int rpc_trustdom_vampire(int argc
, const char **argv
)
5899 /* common variables */
5900 TALLOC_CTX
* mem_ctx
;
5901 struct cli_state
*cli
= NULL
;
5902 struct rpc_pipe_client
*pipe_hnd
= NULL
;
5904 const char *domain_name
= NULL
;
5905 DOM_SID
*queried_dom_sid
;
5906 POLICY_HND connect_hnd
;
5908 /* trusted domains listing variables */
5909 unsigned int num_domains
, enum_ctx
= 0;
5911 DOM_SID
*domain_sids
;
5912 char **trusted_dom_names
;
5917 * Listing trusted domains (stored in secrets.tdb, if local)
5920 mem_ctx
= talloc_init("trust relationships vampire");
5923 * set domain and pdc name to local samba server (default)
5924 * or to remote one given in command line
5927 if (StrCaseCmp(opt_workgroup
, lp_workgroup())) {
5928 domain_name
= opt_workgroup
;
5929 opt_target_workgroup
= opt_workgroup
;
5931 fstrcpy(pdc_name
, global_myname());
5932 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
5933 opt_target_workgroup
= domain_name
;
5936 /* open \PIPE\lsarpc and open policy handle */
5937 if (!(cli
= net_make_ipc_connection(NET_FLAGS_PDC
))) {
5938 DEBUG(0, ("Couldn't connect to domain controller\n"));
5939 talloc_destroy(mem_ctx
);
5943 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &nt_status
);
5945 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5946 nt_errstr(nt_status
) ));
5948 talloc_destroy(mem_ctx
);
5952 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, False
, SEC_RIGHTS_QUERY_VALUE
,
5954 if (NT_STATUS_IS_ERR(nt_status
)) {
5955 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5956 nt_errstr(nt_status
)));
5958 talloc_destroy(mem_ctx
);
5962 /* query info level 5 to obtain sid of a domain being queried */
5963 nt_status
= rpccli_lsa_query_info_policy(
5964 pipe_hnd
, mem_ctx
, &connect_hnd
, 5 /* info level */,
5965 &dummy
, &queried_dom_sid
);
5967 if (NT_STATUS_IS_ERR(nt_status
)) {
5968 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5969 nt_errstr(nt_status
)));
5971 talloc_destroy(mem_ctx
);
5976 * Keep calling LsaEnumTrustdom over opened pipe until
5977 * the end of enumeration is reached
5980 d_printf("Vampire trusted domains:\n\n");
5983 nt_status
= rpccli_lsa_enum_trust_dom(pipe_hnd
, mem_ctx
, &connect_hnd
, &enum_ctx
,
5985 &trusted_dom_names
, &domain_sids
);
5987 if (NT_STATUS_IS_ERR(nt_status
)) {
5988 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5989 nt_errstr(nt_status
)));
5991 talloc_destroy(mem_ctx
);
5995 for (i
= 0; i
< num_domains
; i
++) {
5997 print_trusted_domain(&(domain_sids
[i
]), trusted_dom_names
[i
]);
5999 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
6000 domain_sids
[i
], trusted_dom_names
[i
]);
6001 if (!NT_STATUS_IS_OK(nt_status
)) {
6003 talloc_destroy(mem_ctx
);
6009 * in case of no trusted domains say something rather
6010 * than just display blank line
6012 if (!num_domains
) d_printf("none\n");
6014 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6016 /* close this connection before doing next one */
6017 nt_status
= rpccli_lsa_close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6018 if (NT_STATUS_IS_ERR(nt_status
)) {
6019 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6020 nt_errstr(nt_status
)));
6022 talloc_destroy(mem_ctx
);
6026 /* close lsarpc pipe and connection to IPC$ */
6029 talloc_destroy(mem_ctx
);
6033 static int rpc_trustdom_list(int argc
, const char **argv
)
6035 /* common variables */
6036 TALLOC_CTX
* mem_ctx
;
6037 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
6038 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6040 const char *domain_name
= NULL
;
6041 DOM_SID
*queried_dom_sid
;
6043 int ascii_dom_name_len
;
6044 POLICY_HND connect_hnd
;
6046 /* trusted domains listing variables */
6047 unsigned int num_domains
, enum_ctx
= 0;
6048 int i
, pad_len
, col_len
= 20;
6049 DOM_SID
*domain_sids
;
6050 char **trusted_dom_names
;
6054 /* trusting domains listing variables */
6055 POLICY_HND domain_hnd
;
6056 char **trusting_dom_names
;
6057 uint32
*trusting_dom_rids
;
6060 * Listing trusted domains (stored in secrets.tdb, if local)
6063 mem_ctx
= talloc_init("trust relationships listing");
6066 * set domain and pdc name to local samba server (default)
6067 * or to remote one given in command line
6070 if (StrCaseCmp(opt_workgroup
, lp_workgroup())) {
6071 domain_name
= opt_workgroup
;
6072 opt_target_workgroup
= opt_workgroup
;
6074 fstrcpy(pdc_name
, global_myname());
6075 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6076 opt_target_workgroup
= domain_name
;
6079 /* open \PIPE\lsarpc and open policy handle */
6080 if (!(cli
= net_make_ipc_connection(NET_FLAGS_PDC
))) {
6081 DEBUG(0, ("Couldn't connect to domain controller\n"));
6082 talloc_destroy(mem_ctx
);
6086 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &nt_status
);
6088 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6089 nt_errstr(nt_status
) ));
6091 talloc_destroy(mem_ctx
);
6095 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, False
, SEC_RIGHTS_QUERY_VALUE
,
6097 if (NT_STATUS_IS_ERR(nt_status
)) {
6098 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6099 nt_errstr(nt_status
)));
6101 talloc_destroy(mem_ctx
);
6105 /* query info level 5 to obtain sid of a domain being queried */
6106 nt_status
= rpccli_lsa_query_info_policy(
6107 pipe_hnd
, mem_ctx
, &connect_hnd
, 5 /* info level */,
6108 &dummy
, &queried_dom_sid
);
6110 if (NT_STATUS_IS_ERR(nt_status
)) {
6111 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6112 nt_errstr(nt_status
)));
6114 talloc_destroy(mem_ctx
);
6119 * Keep calling LsaEnumTrustdom over opened pipe until
6120 * the end of enumeration is reached
6123 d_printf("Trusted domains list:\n\n");
6126 nt_status
= rpccli_lsa_enum_trust_dom(pipe_hnd
, mem_ctx
, &connect_hnd
, &enum_ctx
,
6128 &trusted_dom_names
, &domain_sids
);
6130 if (NT_STATUS_IS_ERR(nt_status
)) {
6131 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6132 nt_errstr(nt_status
)));
6134 talloc_destroy(mem_ctx
);
6138 for (i
= 0; i
< num_domains
; i
++) {
6139 print_trusted_domain(&(domain_sids
[i
]), trusted_dom_names
[i
]);
6143 * in case of no trusted domains say something rather
6144 * than just display blank line
6146 if (!num_domains
) d_printf("none\n");
6148 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6150 /* close this connection before doing next one */
6151 nt_status
= rpccli_lsa_close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6152 if (NT_STATUS_IS_ERR(nt_status
)) {
6153 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6154 nt_errstr(nt_status
)));
6156 talloc_destroy(mem_ctx
);
6160 cli_rpc_pipe_close(pipe_hnd
);
6163 * Listing trusting domains (stored in passdb backend, if local)
6166 d_printf("\nTrusting domains list:\n\n");
6169 * Open \PIPE\samr and get needed policy handles
6171 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SAMR
, &nt_status
);
6173 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6175 talloc_destroy(mem_ctx
);
6180 nt_status
= rpccli_samr_connect(pipe_hnd
, mem_ctx
, SA_RIGHT_SAM_OPEN_DOMAIN
,
6182 if (!NT_STATUS_IS_OK(nt_status
)) {
6183 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6184 nt_errstr(nt_status
)));
6186 talloc_destroy(mem_ctx
);
6190 /* SamrOpenDomain - we have to open domain policy handle in order to be
6191 able to enumerate accounts*/
6192 nt_status
= rpccli_samr_open_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
6193 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS
,
6194 queried_dom_sid
, &domain_hnd
);
6195 if (!NT_STATUS_IS_OK(nt_status
)) {
6196 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6197 nt_errstr(nt_status
)));
6199 talloc_destroy(mem_ctx
);
6204 * perform actual enumeration
6207 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6210 nt_status
= rpccli_samr_enum_dom_users(pipe_hnd
, mem_ctx
, &domain_hnd
,
6211 &enum_ctx
, ACB_DOMTRUST
, 0xffff,
6212 &trusting_dom_names
, &trusting_dom_rids
,
6214 if (NT_STATUS_IS_ERR(nt_status
)) {
6215 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6216 nt_errstr(nt_status
)));
6218 talloc_destroy(mem_ctx
);
6222 for (i
= 0; i
< num_domains
; i
++) {
6225 * get each single domain's sid (do we _really_ need this ?):
6226 * 1) connect to domain's pdc
6227 * 2) query the pdc for domain's sid
6230 /* get rid of '$' tail */
6231 ascii_dom_name_len
= strlen(trusting_dom_names
[i
]);
6232 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
6233 trusting_dom_names
[i
][ascii_dom_name_len
- 1] = '\0';
6235 /* calculate padding space for d_printf to look nicer */
6236 pad_len
= col_len
- strlen(trusting_dom_names
[i
]);
6237 padding
[pad_len
] = 0;
6238 do padding
[--pad_len
] = ' '; while (pad_len
);
6240 /* set opt_* variables to remote domain */
6241 strupper_m(trusting_dom_names
[i
]);
6242 opt_workgroup
= talloc_strdup(mem_ctx
, trusting_dom_names
[i
]);
6243 opt_target_workgroup
= opt_workgroup
;
6245 d_printf("%s%s", trusting_dom_names
[i
], padding
);
6247 /* connect to remote domain controller */
6248 remote_cli
= net_make_ipc_connection(NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
);
6250 /* query for domain's sid */
6251 if (run_rpc_command(remote_cli
, PI_LSARPC
, 0, rpc_query_domain_sid
, argc
, argv
))
6252 d_fprintf(stderr
, "couldn't get domain's sid\n");
6254 cli_shutdown(remote_cli
);
6257 d_fprintf(stderr
, "domain controller is not responding\n");
6261 if (!num_domains
) d_printf("none\n");
6263 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6265 /* close opened samr and domain policy handles */
6266 nt_status
= rpccli_samr_close(pipe_hnd
, mem_ctx
, &domain_hnd
);
6267 if (!NT_STATUS_IS_OK(nt_status
)) {
6268 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
6271 nt_status
= rpccli_samr_close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6272 if (!NT_STATUS_IS_OK(nt_status
)) {
6273 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
6276 /* close samr pipe and connection to IPC$ */
6279 talloc_destroy(mem_ctx
);
6284 * Entrypoint for 'net rpc trustdom' code
6286 * @param argc standard argc
6287 * @param argv standard argv without initial components
6289 * @return Integer status (0 means success)
6292 static int rpc_trustdom(int argc
, const char **argv
)
6294 struct functable func
[] = {
6295 {"add", rpc_trustdom_add
},
6296 {"del", rpc_trustdom_del
},
6297 {"establish", rpc_trustdom_establish
},
6298 {"revoke", rpc_trustdom_revoke
},
6299 {"help", rpc_trustdom_usage
},
6300 {"list", rpc_trustdom_list
},
6301 {"vampire", rpc_trustdom_vampire
},
6306 rpc_trustdom_usage(argc
, argv
);
6310 return (net_run_function(argc
, argv
, func
, rpc_user_usage
));
6314 * Check if a server will take rpc commands
6315 * @param flags Type of server to connect to (PDC, DMB, localhost)
6316 * if the host is not explicitly specified
6317 * @return BOOL (true means rpc supported)
6319 BOOL
net_rpc_check(unsigned flags
)
6321 struct cli_state
*cli
;
6323 struct in_addr server_ip
;
6324 char *server_name
= NULL
;
6327 /* flags (i.e. server type) may depend on command */
6328 if (!net_find_server(NULL
, flags
, &server_ip
, &server_name
))
6331 if ((cli
= cli_initialise()) == NULL
) {
6335 status
= cli_connect(cli
, server_name
, &server_ip
);
6336 if (!NT_STATUS_IS_OK(status
))
6338 if (!attempt_netbios_session_request(&cli
, global_myname(),
6339 server_name
, &server_ip
))
6341 if (!cli_negprot(cli
))
6343 if (cli
->protocol
< PROTOCOL_NT1
)
6352 /* dump sam database via samsync rpc calls */
6353 static int rpc_samdump(int argc
, const char **argv
) {
6354 return run_rpc_command(NULL
, PI_NETLOGON
, NET_FLAGS_ANONYMOUS
, rpc_samdump_internals
,
6358 /* syncronise sam database via samsync rpc calls */
6359 static int rpc_vampire(int argc
, const char **argv
) {
6360 return run_rpc_command(NULL
, PI_NETLOGON
, NET_FLAGS_ANONYMOUS
, rpc_vampire_internals
,
6365 * Migrate everything from a print-server
6367 * @param argc Standard main() style argc
6368 * @param argv Standard main() style argv. Initial components are already
6371 * @return A shell status integer (0 for success)
6373 * The order is important !
6374 * To successfully add drivers the print-queues have to exist !
6375 * Applying ACLs should be the last step, because you're easily locked out
6378 static int rpc_printer_migrate_all(int argc
, const char **argv
)
6383 printf("no server to migrate\n");
6387 ret
= run_rpc_command(NULL
, PI_SPOOLSS
, 0, rpc_printer_migrate_printers_internals
, argc
, argv
);
6391 ret
= run_rpc_command(NULL
, PI_SPOOLSS
, 0, rpc_printer_migrate_drivers_internals
, argc
, argv
);
6395 ret
= run_rpc_command(NULL
, PI_SPOOLSS
, 0, rpc_printer_migrate_forms_internals
, argc
, argv
);
6399 ret
= run_rpc_command(NULL
, PI_SPOOLSS
, 0, rpc_printer_migrate_settings_internals
, argc
, argv
);
6403 return run_rpc_command(NULL
, PI_SPOOLSS
, 0, rpc_printer_migrate_security_internals
, argc
, argv
);
6408 * Migrate print-drivers from a print-server
6410 * @param argc Standard main() style argc
6411 * @param argv Standard main() style argv. Initial components are already
6414 * @return A shell status integer (0 for success)
6416 static int rpc_printer_migrate_drivers(int argc
, const char **argv
)
6419 printf("no server to migrate\n");
6423 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6424 rpc_printer_migrate_drivers_internals
,
6429 * Migrate print-forms from a print-server
6431 * @param argc Standard main() style argc
6432 * @param argv Standard main() style argv. Initial components are already
6435 * @return A shell status integer (0 for success)
6437 static int rpc_printer_migrate_forms(int argc
, const char **argv
)
6440 printf("no server to migrate\n");
6444 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6445 rpc_printer_migrate_forms_internals
,
6450 * Migrate printers from a print-server
6452 * @param argc Standard main() style argc
6453 * @param argv Standard main() style argv. Initial components are already
6456 * @return A shell status integer (0 for success)
6458 static int rpc_printer_migrate_printers(int argc
, const char **argv
)
6461 printf("no server to migrate\n");
6465 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6466 rpc_printer_migrate_printers_internals
,
6471 * Migrate printer-ACLs from a print-server
6473 * @param argc Standard main() style argc
6474 * @param argv Standard main() style argv. Initial components are already
6477 * @return A shell status integer (0 for success)
6479 static int rpc_printer_migrate_security(int argc
, const char **argv
)
6482 printf("no server to migrate\n");
6486 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6487 rpc_printer_migrate_security_internals
,
6492 * Migrate printer-settings from a print-server
6494 * @param argc Standard main() style argc
6495 * @param argv Standard main() style argv. Initial components are already
6498 * @return A shell status integer (0 for success)
6500 static int rpc_printer_migrate_settings(int argc
, const char **argv
)
6503 printf("no server to migrate\n");
6507 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6508 rpc_printer_migrate_settings_internals
,
6513 * 'net rpc printer' entrypoint.
6514 * @param argc Standard main() style argc
6515 * @param argv Standard main() style argv. Initial components are already
6519 int rpc_printer_migrate(int argc
, const char **argv
)
6522 /* ouch: when addriver and setdriver are called from within
6523 rpc_printer_migrate_drivers_internals, the printer-queue already
6526 struct functable func
[] = {
6527 {"all", rpc_printer_migrate_all
},
6528 {"drivers", rpc_printer_migrate_drivers
},
6529 {"forms", rpc_printer_migrate_forms
},
6530 {"help", rpc_printer_usage
},
6531 {"printers", rpc_printer_migrate_printers
},
6532 {"security", rpc_printer_migrate_security
},
6533 {"settings", rpc_printer_migrate_settings
},
6537 return net_run_function(argc
, argv
, func
, rpc_printer_usage
);
6542 * List printers on a remote RPC server
6544 * @param argc Standard main() style argc
6545 * @param argv Standard main() style argv. Initial components are already
6548 * @return A shell status integer (0 for success)
6550 static int rpc_printer_list(int argc
, const char **argv
)
6553 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6554 rpc_printer_list_internals
,
6559 * List printer-drivers on a remote RPC server
6561 * @param argc Standard main() style argc
6562 * @param argv Standard main() style argv. Initial components are already
6565 * @return A shell status integer (0 for success)
6567 static int rpc_printer_driver_list(int argc
, const char **argv
)
6570 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6571 rpc_printer_driver_list_internals
,
6576 * Publish printer in ADS via MSRPC
6578 * @param argc Standard main() style argc
6579 * @param argv Standard main() style argv. Initial components are already
6582 * @return A shell status integer (0 for success)
6584 static int rpc_printer_publish_publish(int argc
, const char **argv
)
6587 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6588 rpc_printer_publish_publish_internals
,
6593 * Update printer in ADS via MSRPC
6595 * @param argc Standard main() style argc
6596 * @param argv Standard main() style argv. Initial components are already
6599 * @return A shell status integer (0 for success)
6601 static int rpc_printer_publish_update(int argc
, const char **argv
)
6604 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6605 rpc_printer_publish_update_internals
,
6610 * UnPublish printer in ADS via MSRPC
6612 * @param argc Standard main() style argc
6613 * @param argv Standard main() style argv. Initial components are already
6616 * @return A shell status integer (0 for success)
6618 static int rpc_printer_publish_unpublish(int argc
, const char **argv
)
6621 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6622 rpc_printer_publish_unpublish_internals
,
6627 * List published printers via MSRPC
6629 * @param argc Standard main() style argc
6630 * @param argv Standard main() style argv. Initial components are already
6633 * @return A shell status integer (0 for success)
6635 static int rpc_printer_publish_list(int argc
, const char **argv
)
6638 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6639 rpc_printer_publish_list_internals
,
6645 * Publish printer in ADS
6647 * @param argc Standard main() style argc
6648 * @param argv Standard main() style argv. Initial components are already
6651 * @return A shell status integer (0 for success)
6653 static int rpc_printer_publish(int argc
, const char **argv
)
6656 struct functable func
[] = {
6657 {"publish", rpc_printer_publish_publish
},
6658 {"update", rpc_printer_publish_update
},
6659 {"unpublish", rpc_printer_publish_unpublish
},
6660 {"list", rpc_printer_publish_list
},
6661 {"help", rpc_printer_usage
},
6666 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6667 rpc_printer_publish_list_internals
,
6670 return net_run_function(argc
, argv
, func
, rpc_printer_usage
);
6676 * Display rpc printer help page.
6677 * @param argc Standard main() style argc
6678 * @param argv Standard main() style argv. Initial components are already
6681 int rpc_printer_usage(int argc
, const char **argv
)
6683 return net_help_printer(argc
, argv
);
6687 * 'net rpc printer' entrypoint.
6688 * @param argc Standard main() style argc
6689 * @param argv Standard main() style argv. Initial components are already
6692 int net_rpc_printer(int argc
, const char **argv
)
6694 struct functable func
[] = {
6695 {"list", rpc_printer_list
},
6696 {"migrate", rpc_printer_migrate
},
6697 {"driver", rpc_printer_driver_list
},
6698 {"publish", rpc_printer_publish
},
6703 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6704 rpc_printer_list_internals
,
6707 return net_run_function(argc
, argv
, func
, rpc_printer_usage
);
6710 /****************************************************************************/
6714 * Basic usage function for 'net rpc'
6715 * @param argc Standard main() style argc
6716 * @param argv Standard main() style argv. Initial components are already
6720 int net_rpc_usage(int argc
, const char **argv
)
6722 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
6723 d_printf(" net rpc join \t\t\tto join a domain \n");
6724 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n");
6725 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
6726 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
6727 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
6728 d_printf(" net rpc group \t\tto list groups\n");
6729 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
6730 d_printf(" net rpc printer \t\tto list and migrate printers\n");
6731 d_printf(" net rpc file \t\t\tto list open files\n");
6732 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
6733 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
6734 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
6735 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
6736 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
6737 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
6738 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
6739 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
6740 d_printf(" net rpc registry\t\tto manage registry hives\n");
6741 d_printf(" net rpc service\t\tto start, stop and query services\n");
6742 d_printf(" net rpc audit\t\t\tto modify global auditing settings\n");
6743 d_printf(" net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
6745 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
6746 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
6747 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
6748 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
6749 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
6755 * Help function for 'net rpc'. Calls command specific help if requested
6756 * or displays usage of net rpc
6757 * @param argc Standard main() style argc
6758 * @param argv Standard main() style argv. Initial components are already
6762 int net_rpc_help(int argc
, const char **argv
)
6764 struct functable func
[] = {
6765 {"join", rpc_join_usage
},
6766 {"user", rpc_user_usage
},
6767 {"group", rpc_group_usage
},
6768 {"share", rpc_share_usage
},
6769 /*{"changetrustpw", rpc_changetrustpw_usage}, */
6770 {"trustdom", rpc_trustdom_usage
},
6771 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
6772 /*{"shutdown", rpc_shutdown_usage}, */
6773 {"vampire", rpc_vampire_usage
},
6778 net_rpc_usage(argc
, argv
);
6782 return (net_run_function(argc
, argv
, func
, rpc_user_usage
));
6786 * 'net rpc' entrypoint.
6787 * @param argc Standard main() style argc
6788 * @param argv Standard main() style argv. Initial components are already
6792 int net_rpc(int argc
, const char **argv
)
6794 struct functable func
[] = {
6795 {"audit", net_rpc_audit
},
6796 {"info", net_rpc_info
},
6797 {"join", net_rpc_join
},
6798 {"oldjoin", net_rpc_oldjoin
},
6799 {"testjoin", net_rpc_testjoin
},
6800 {"user", net_rpc_user
},
6801 {"password", rpc_user_password
},
6802 {"group", net_rpc_group
},
6803 {"share", net_rpc_share
},
6804 {"file", net_rpc_file
},
6805 {"printer", net_rpc_printer
},
6806 {"changetrustpw", net_rpc_changetrustpw
},
6807 {"trustdom", rpc_trustdom
},
6808 {"abortshutdown", rpc_shutdown_abort
},
6809 {"shutdown", rpc_shutdown
},
6810 {"samdump", rpc_samdump
},
6811 {"vampire", rpc_vampire
},
6812 {"getsid", net_rpc_getsid
},
6813 {"rights", net_rpc_rights
},
6814 {"service", net_rpc_service
},
6815 {"registry", net_rpc_registry
},
6816 {"shell", net_rpc_shell
},
6817 {"help", net_rpc_help
},
6820 return net_run_function(argc
, argv
, func
, net_rpc_usage
);