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)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "utils/net.h"
26 static void init_lsa_String(struct lsa_String
*name
, const char *s
)
31 static int net_mode_share
;
32 static bool sync_files(struct copy_clistate
*cp_clistate
, const char *mask
);
37 * @brief RPC based subcommands for the 'net' utility.
39 * This file should contain much of the functionality that used to
40 * be found in rpcclient, execpt that the commands should change
41 * less often, and the fucntionality should be sane (the user is not
42 * expected to know a rid/sid before they conduct an operation etc.)
44 * @todo Perhaps eventually these should be split out into a number
45 * of files, as this could get quite big.
50 * Many of the RPC functions need the domain sid. This function gets
51 * it at the start of every run
53 * @param cli A cli_state already connected to the remote machine
55 * @return The Domain SID of the remote machine.
58 NTSTATUS
net_get_remote_domain_sid(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
60 const char **domain_name
)
62 struct rpc_pipe_client
*lsa_pipe
;
64 NTSTATUS result
= NT_STATUS_OK
;
65 union lsa_PolicyInformation
*info
= NULL
;
67 lsa_pipe
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &result
);
69 d_fprintf(stderr
, "Could not initialise lsa pipe\n");
73 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, False
,
74 SEC_RIGHTS_MAXIMUM_ALLOWED
,
76 if (!NT_STATUS_IS_OK(result
)) {
77 d_fprintf(stderr
, "open_policy failed: %s\n",
82 result
= rpccli_lsa_QueryInfoPolicy(lsa_pipe
, mem_ctx
,
84 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
86 if (!NT_STATUS_IS_OK(result
)) {
87 d_fprintf(stderr
, "lsaquery failed: %s\n",
92 *domain_name
= info
->account_domain
.name
.string
;
93 *domain_sid
= info
->account_domain
.sid
;
95 rpccli_lsa_Close(lsa_pipe
, mem_ctx
, &pol
);
96 cli_rpc_pipe_close(lsa_pipe
);
102 * Run a single RPC command, from start to finish.
104 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
105 * @param conn_flag a NET_FLAG_ combination. Passed to
106 * net_make_ipc_connection.
107 * @param argc Standard main() style argc
108 * @param argc Standard main() style argv. Initial components are already
110 * @return A shell status integer (0 for success)
113 int run_rpc_command(struct cli_state
*cli_arg
,
120 struct cli_state
*cli
= NULL
;
121 struct rpc_pipe_client
*pipe_hnd
= NULL
;
125 const char *domain_name
;
127 /* make use of cli_state handed over as an argument, if possible */
129 nt_status
= net_make_ipc_connection(conn_flags
, &cli
);
130 if (!NT_STATUS_IS_OK(nt_status
)) {
131 DEBUG(1, ("failed to make ipc connection: %s\n",
132 nt_errstr(nt_status
)));
145 if (!(mem_ctx
= talloc_init("run_rpc_command"))) {
146 DEBUG(0, ("talloc_init() failed\n"));
151 nt_status
= net_get_remote_domain_sid(cli
, mem_ctx
, &domain_sid
,
153 if (!NT_STATUS_IS_OK(nt_status
)) {
158 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
159 if (lp_client_schannel() && (pipe_idx
== PI_NETLOGON
)) {
160 /* Always try and create an schannel netlogon pipe. */
161 pipe_hnd
= cli_rpc_pipe_open_schannel(cli
, pipe_idx
,
162 PIPE_AUTH_LEVEL_PRIVACY
,
166 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
167 nt_errstr(nt_status
) ));
172 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, pipe_idx
, &nt_status
);
174 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
175 cli_get_pipe_name(pipe_idx
),
176 nt_errstr(nt_status
) ));
183 nt_status
= fn(domain_sid
, domain_name
, cli
, pipe_hnd
, mem_ctx
, argc
, argv
);
185 if (!NT_STATUS_IS_OK(nt_status
)) {
186 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status
)));
188 DEBUG(5, ("rpc command function succedded\n"));
191 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
193 cli_rpc_pipe_close(pipe_hnd
);
197 /* close the connection only if it was opened here */
202 talloc_destroy(mem_ctx
);
203 return (!NT_STATUS_IS_OK(nt_status
));
207 * Force a change of the trust acccount password.
209 * All parameters are provided by the run_rpc_command function, except for
210 * argc, argv which are passes through.
212 * @param domain_sid The domain sid aquired from the remote server
213 * @param cli A cli_state connected to the server.
214 * @param mem_ctx Talloc context, destoyed on compleation of the function.
215 * @param argc Standard main() style argc
216 * @param argc Standard main() style argv. Initial components are already
219 * @return Normal NTSTATUS return.
222 static NTSTATUS
rpc_changetrustpw_internals(const DOM_SID
*domain_sid
,
223 const char *domain_name
,
224 struct cli_state
*cli
,
225 struct rpc_pipe_client
*pipe_hnd
,
231 return trust_pw_find_change_and_store_it(pipe_hnd
, mem_ctx
, opt_target_workgroup
);
235 * Force a change of the trust acccount password.
237 * @param argc Standard main() style argc
238 * @param argc Standard main() style argv. Initial components are already
241 * @return A shell status integer (0 for success)
244 int net_rpc_changetrustpw(int argc
, const char **argv
)
246 return run_rpc_command(NULL
, PI_NETLOGON
, NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
247 rpc_changetrustpw_internals
,
252 * Join a domain, the old way.
254 * This uses 'machinename' as the inital password, and changes it.
256 * The password should be created with 'server manager' or equiv first.
258 * All parameters are provided by the run_rpc_command function, except for
259 * argc, argv which are passes through.
261 * @param domain_sid The domain sid aquired from the remote server
262 * @param cli A cli_state connected to the server.
263 * @param mem_ctx Talloc context, destoyed on compleation of the function.
264 * @param argc Standard main() style argc
265 * @param argc Standard main() style argv. Initial components are already
268 * @return Normal NTSTATUS return.
271 static NTSTATUS
rpc_oldjoin_internals(const DOM_SID
*domain_sid
,
272 const char *domain_name
,
273 struct cli_state
*cli
,
274 struct rpc_pipe_client
*pipe_hnd
,
280 fstring trust_passwd
;
281 unsigned char orig_trust_passwd_hash
[16];
283 uint32 sec_channel_type
;
285 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_NETLOGON
, &result
);
287 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
290 nt_errstr(result
) ));
295 check what type of join - if the user want's to join as
296 a BDC, the server must agree that we are a BDC.
299 sec_channel_type
= get_sec_channel_type(argv
[0]);
301 sec_channel_type
= get_sec_channel_type(NULL
);
304 fstrcpy(trust_passwd
, global_myname());
305 strlower_m(trust_passwd
);
308 * Machine names can be 15 characters, but the max length on
309 * a password is 14. --jerry
312 trust_passwd
[14] = '\0';
314 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
316 result
= trust_pw_change_and_store_it(pipe_hnd
, mem_ctx
, opt_target_workgroup
,
317 orig_trust_passwd_hash
,
320 if (NT_STATUS_IS_OK(result
))
321 printf("Joined domain %s.\n",opt_target_workgroup
);
324 if (!secrets_store_domain_sid(opt_target_workgroup
, domain_sid
)) {
325 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup
));
326 result
= NT_STATUS_UNSUCCESSFUL
;
333 * Join a domain, the old way.
335 * @param argc Standard main() style argc
336 * @param argc Standard main() style argv. Initial components are already
339 * @return A shell status integer (0 for success)
342 static int net_rpc_perform_oldjoin(int argc
, const char **argv
)
344 return run_rpc_command(NULL
, PI_NETLOGON
,
345 NET_FLAGS_NO_PIPE
| NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
346 rpc_oldjoin_internals
,
351 * Join a domain, the old way. This function exists to allow
352 * the message to be displayed when oldjoin was explicitly
353 * requested, but not when it was implied by "net rpc join"
355 * @param argc Standard main() style argc
356 * @param argc Standard main() style argv. Initial components are already
359 * @return A shell status integer (0 for success)
362 static int net_rpc_oldjoin(int argc
, const char **argv
)
364 int rc
= net_rpc_perform_oldjoin(argc
, argv
);
367 d_fprintf(stderr
, "Failed to join domain\n");
374 * Basic usage function for 'net rpc join'
375 * @param argc Standard main() style argc
376 * @param argc Standard main() style argv. Initial components are already
380 static int rpc_join_usage(int argc
, const char **argv
)
382 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
383 "\t to join a domain with admin username & password\n"\
384 "\t\t password will be prompted if needed and none is specified\n"\
385 "\t <type> can be (default MEMBER)\n"\
386 "\t\t BDC - Join as a BDC\n"\
387 "\t\t PDC - Join as a PDC\n"\
388 "\t\t MEMBER - Join as a MEMBER server\n");
390 net_common_flags_usage(argc
, argv
);
395 * 'net rpc join' entrypoint.
396 * @param argc Standard main() style argc
397 * @param argc Standard main() style argv. Initial components are already
400 * Main 'net_rpc_join()' (where the admin username/password is used) is
402 * Try to just change the password, but if that doesn't work, use/prompt
403 * for a username/password.
406 int net_rpc_join(int argc
, const char **argv
)
408 if (lp_server_role() == ROLE_STANDALONE
) {
409 d_printf("cannot join as standalone machine\n");
413 if (strlen(global_myname()) > 15) {
414 d_printf("Our netbios name can be at most 15 chars long, "
415 "\"%s\" is %u chars long\n",
416 global_myname(), (unsigned int)strlen(global_myname()));
420 if ((net_rpc_perform_oldjoin(argc
, argv
) == 0))
423 return net_rpc_join_newstyle(argc
, argv
);
427 * display info about a rpc domain
429 * All parameters are provided by the run_rpc_command function, except for
430 * argc, argv which are passed through.
432 * @param domain_sid The domain sid acquired from the remote server
433 * @param cli A cli_state connected to the server.
434 * @param mem_ctx Talloc context, destoyed on completion of the function.
435 * @param argc Standard main() style argc
436 * @param argv Standard main() style argv. Initial components are already
439 * @return Normal NTSTATUS return.
442 NTSTATUS
rpc_info_internals(const DOM_SID
*domain_sid
,
443 const char *domain_name
,
444 struct cli_state
*cli
,
445 struct rpc_pipe_client
*pipe_hnd
,
450 POLICY_HND connect_pol
, domain_pol
;
451 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
452 union samr_DomainInfo
*info
= NULL
;
455 sid_to_fstring(sid_str
, domain_sid
);
457 /* Get sam policy handle */
458 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
459 pipe_hnd
->cli
->desthost
,
460 MAXIMUM_ALLOWED_ACCESS
,
462 if (!NT_STATUS_IS_OK(result
)) {
463 d_fprintf(stderr
, "Could not connect to SAM: %s\n", nt_errstr(result
));
467 /* Get domain policy handle */
468 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
470 MAXIMUM_ALLOWED_ACCESS
,
471 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
473 if (!NT_STATUS_IS_OK(result
)) {
474 d_fprintf(stderr
, "Could not open domain: %s\n", nt_errstr(result
));
478 result
= rpccli_samr_QueryDomainInfo(pipe_hnd
, mem_ctx
,
482 if (NT_STATUS_IS_OK(result
)) {
483 d_printf("Domain Name: %s\n", info
->info2
.domain_name
.string
);
484 d_printf("Domain SID: %s\n", sid_str
);
485 d_printf("Sequence number: %llu\n",
486 (unsigned long long)info
->info2
.sequence_num
);
487 d_printf("Num users: %u\n", info
->info2
.num_users
);
488 d_printf("Num domain groups: %u\n", info
->info2
.num_groups
);
489 d_printf("Num local groups: %u\n", info
->info2
.num_aliases
);
497 * 'net rpc info' entrypoint.
498 * @param argc Standard main() style argc
499 * @param argc Standard main() style argv. Initial components are already
503 int net_rpc_info(int argc
, const char **argv
)
505 return run_rpc_command(NULL
, PI_SAMR
, NET_FLAGS_PDC
,
511 * Fetch domain SID into the local secrets.tdb
513 * All parameters are provided by the run_rpc_command function, except for
514 * argc, argv which are passes through.
516 * @param domain_sid The domain sid acquired from the remote server
517 * @param cli A cli_state connected to the server.
518 * @param mem_ctx Talloc context, destoyed on completion of the function.
519 * @param argc Standard main() style argc
520 * @param argv Standard main() style argv. Initial components are already
523 * @return Normal NTSTATUS return.
526 static NTSTATUS
rpc_getsid_internals(const DOM_SID
*domain_sid
,
527 const char *domain_name
,
528 struct cli_state
*cli
,
529 struct rpc_pipe_client
*pipe_hnd
,
536 sid_to_fstring(sid_str
, domain_sid
);
537 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
538 sid_str
, domain_name
);
540 if (!secrets_store_domain_sid(domain_name
, domain_sid
)) {
541 DEBUG(0,("Can't store domain SID\n"));
542 return NT_STATUS_UNSUCCESSFUL
;
549 * 'net rpc getsid' entrypoint.
550 * @param argc Standard main() style argc
551 * @param argc Standard main() style argv. Initial components are already
555 int net_rpc_getsid(int argc
, const char **argv
)
557 return run_rpc_command(NULL
, PI_SAMR
, NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
558 rpc_getsid_internals
,
562 /****************************************************************************/
565 * Basic usage function for 'net rpc user'
566 * @param argc Standard main() style argc.
567 * @param argv Standard main() style argv. Initial components are already
571 static int rpc_user_usage(int argc
, const char **argv
)
573 return net_help_user(argc
, argv
);
577 * Add a new user to a remote RPC server
579 * All parameters are provided by the run_rpc_command function, except for
580 * argc, argv which are passes through.
582 * @param domain_sid The domain sid acquired from the remote server
583 * @param cli A cli_state connected to the server.
584 * @param mem_ctx Talloc context, destoyed on completion of the function.
585 * @param argc Standard main() style argc
586 * @param argv Standard main() style argv. Initial components are already
589 * @return Normal NTSTATUS return.
592 static NTSTATUS
rpc_user_add_internals(const DOM_SID
*domain_sid
,
593 const char *domain_name
,
594 struct cli_state
*cli
,
595 struct rpc_pipe_client
*pipe_hnd
,
597 int argc
, const char **argv
)
600 POLICY_HND connect_pol
, domain_pol
, user_pol
;
601 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
602 const char *acct_name
;
603 struct lsa_String lsa_acct_name
;
605 uint32 acct_flags
, user_rid
;
606 uint32_t access_granted
= 0;
609 d_printf("User must be specified\n");
610 rpc_user_usage(argc
, argv
);
615 init_lsa_String(&lsa_acct_name
, acct_name
);
617 /* Get sam policy handle */
619 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
620 pipe_hnd
->cli
->desthost
,
621 MAXIMUM_ALLOWED_ACCESS
,
623 if (!NT_STATUS_IS_OK(result
)) {
627 /* Get domain policy handle */
629 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
631 MAXIMUM_ALLOWED_ACCESS
,
632 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
634 if (!NT_STATUS_IS_OK(result
)) {
638 /* Create domain user */
640 acb_info
= ACB_NORMAL
;
641 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
642 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
643 SAMR_USER_ACCESS_SET_PASSWORD
|
644 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
645 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
647 result
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
656 if (!NT_STATUS_IS_OK(result
)) {
662 uint32
*user_rids
, num_rids
, *name_types
;
663 uint32 flags
= 0x000003e8; /* Unknown */
664 SAM_USERINFO_CTR ctr
;
665 SAM_USER_INFO_24 p24
;
668 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
,
669 flags
, 1, &acct_name
,
670 &num_rids
, &user_rids
,
673 if (!NT_STATUS_IS_OK(result
)) {
677 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
679 MAXIMUM_ALLOWED_ACCESS
,
683 if (!NT_STATUS_IS_OK(result
)) {
687 /* Set password on account */
692 encode_pw_buffer(pwbuf
, argv
[1], STR_UNICODE
);
694 init_sam_user_info24(&p24
, (char *)pwbuf
,24);
696 ctr
.switch_value
= 24;
697 ctr
.info
.id24
= &p24
;
699 result
= rpccli_samr_set_userinfo(pipe_hnd
, mem_ctx
, &user_pol
, 24,
700 &cli
->user_session_key
, &ctr
);
702 if (!NT_STATUS_IS_OK(result
)) {
703 d_fprintf(stderr
, "Failed to set password for user %s - %s\n",
704 acct_name
, nt_errstr(result
));
706 result
= rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
709 if (!NT_STATUS_IS_OK(result
)) {
710 d_fprintf(stderr
, "Failed to delete user %s - %s\n",
711 acct_name
, nt_errstr(result
));
718 if (!NT_STATUS_IS_OK(result
)) {
719 d_fprintf(stderr
, "Failed to add user '%s' with %s.\n",
720 acct_name
, nt_errstr(result
));
722 d_printf("Added user '%s'.\n", acct_name
);
728 * Add a new user to a remote RPC server
730 * @param argc Standard main() style argc
731 * @param argv Standard main() style argv. Initial components are already
734 * @return A shell status integer (0 for success)
737 static int rpc_user_add(int argc
, const char **argv
)
739 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_add_internals
,
744 * Delete a user from a remote RPC server
746 * All parameters are provided by the run_rpc_command function, except for
747 * argc, argv which are passes through.
749 * @param domain_sid The domain sid acquired from the remote server
750 * @param cli A cli_state connected to the server.
751 * @param mem_ctx Talloc context, destoyed on completion of the function.
752 * @param argc Standard main() style argc
753 * @param argv Standard main() style argv. Initial components are already
756 * @return Normal NTSTATUS return.
759 static NTSTATUS
rpc_user_del_internals(const DOM_SID
*domain_sid
,
760 const char *domain_name
,
761 struct cli_state
*cli
,
762 struct rpc_pipe_client
*pipe_hnd
,
767 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
768 POLICY_HND connect_pol
, domain_pol
, user_pol
;
769 const char *acct_name
;
772 d_printf("User must be specified\n");
773 rpc_user_usage(argc
, argv
);
779 /* Get sam policy and domain handles */
781 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
782 pipe_hnd
->cli
->desthost
,
783 MAXIMUM_ALLOWED_ACCESS
,
786 if (!NT_STATUS_IS_OK(result
)) {
790 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
792 MAXIMUM_ALLOWED_ACCESS
,
793 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
796 if (!NT_STATUS_IS_OK(result
)) {
800 /* Get handle on user */
803 uint32
*user_rids
, num_rids
, *name_types
;
804 uint32 flags
= 0x000003e8; /* Unknown */
806 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
,
807 flags
, 1, &acct_name
,
808 &num_rids
, &user_rids
,
811 if (!NT_STATUS_IS_OK(result
)) {
815 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
817 MAXIMUM_ALLOWED_ACCESS
,
821 if (!NT_STATUS_IS_OK(result
)) {
828 result
= rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
831 if (!NT_STATUS_IS_OK(result
)) {
836 if (!NT_STATUS_IS_OK(result
)) {
837 d_fprintf(stderr
, "Failed to delete user '%s' with %s.\n",
838 acct_name
, nt_errstr(result
));
840 d_printf("Deleted user '%s'.\n", acct_name
);
847 * Rename a user on a remote RPC server
849 * All parameters are provided by the run_rpc_command function, except for
850 * argc, argv which are passes through.
852 * @param domain_sid The domain sid acquired from the remote server
853 * @param cli A cli_state connected to the server.
854 * @param mem_ctx Talloc context, destoyed on completion of the function.
855 * @param argc Standard main() style argc
856 * @param argv Standard main() style argv. Initial components are already
859 * @return Normal NTSTATUS return.
862 static NTSTATUS
rpc_user_rename_internals(const DOM_SID
*domain_sid
,
863 const char *domain_name
,
864 struct cli_state
*cli
,
865 struct rpc_pipe_client
*pipe_hnd
,
870 POLICY_HND connect_pol
, domain_pol
, user_pol
;
871 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
872 uint32 info_level
= 7;
873 const char *old_name
, *new_name
;
875 uint32 flags
= 0x000003e8; /* Unknown */
876 uint32 num_rids
, *name_types
;
877 uint32 num_names
= 1;
879 SAM_USERINFO_CTR
*user_ctr
;
880 SAM_USERINFO_CTR ctr
;
881 SAM_USER_INFO_7 info7
;
884 d_printf("Old and new username must be specified\n");
885 rpc_user_usage(argc
, argv
);
893 ZERO_STRUCT(user_ctr
);
895 /* Get sam policy handle */
897 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
898 pipe_hnd
->cli
->desthost
,
899 MAXIMUM_ALLOWED_ACCESS
,
902 if (!NT_STATUS_IS_OK(result
)) {
906 /* Get domain policy handle */
908 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
910 MAXIMUM_ALLOWED_ACCESS
,
911 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
913 if (!NT_STATUS_IS_OK(result
)) {
917 if ((names
= TALLOC_ARRAY(mem_ctx
, const char *, num_names
)) == NULL
) {
918 result
= NT_STATUS_NO_MEMORY
;
922 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
,
923 flags
, num_names
, names
,
924 &num_rids
, &user_rid
, &name_types
);
925 if (!NT_STATUS_IS_OK(result
)) {
929 /* Open domain user */
930 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
932 MAXIMUM_ALLOWED_ACCESS
,
936 if (!NT_STATUS_IS_OK(result
)) {
940 /* Query user info */
941 result
= rpccli_samr_query_userinfo(pipe_hnd
, mem_ctx
, &user_pol
,
942 info_level
, &user_ctr
);
944 if (!NT_STATUS_IS_OK(result
)) {
948 ctr
.switch_value
= info_level
;
949 ctr
.info
.id7
= &info7
;
951 init_sam_user_info7(&info7
, new_name
);
954 result
= rpccli_samr_set_userinfo(pipe_hnd
, mem_ctx
, &user_pol
,
955 info_level
, &cli
->user_session_key
, &ctr
);
957 if (!NT_STATUS_IS_OK(result
)) {
962 if (!NT_STATUS_IS_OK(result
)) {
963 d_fprintf(stderr
, "Failed to rename user from %s to %s - %s\n", old_name
, new_name
,
966 d_printf("Renamed user from %s to %s\n", old_name
, new_name
);
972 * Rename a user on a remote RPC server
974 * @param argc Standard main() style argc
975 * @param argv Standard main() style argv. Initial components are already
978 * @return A shell status integer (0 for success)
981 static int rpc_user_rename(int argc
, const char **argv
)
983 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_rename_internals
,
988 * Delete a user from a remote RPC server
990 * @param argc Standard main() style argc
991 * @param argv Standard main() style argv. Initial components are already
994 * @return A shell status integer (0 for success)
997 static int rpc_user_delete(int argc
, const char **argv
)
999 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_del_internals
,
1004 * Set a password for a user on a remote RPC server
1006 * All parameters are provided by the run_rpc_command function, except for
1007 * argc, argv which are passes through.
1009 * @param domain_sid The domain sid acquired from the remote server
1010 * @param cli A cli_state connected to the server.
1011 * @param mem_ctx Talloc context, destoyed on completion of the function.
1012 * @param argc Standard main() style argc
1013 * @param argv Standard main() style argv. Initial components are already
1016 * @return Normal NTSTATUS return.
1019 static NTSTATUS
rpc_user_password_internals(const DOM_SID
*domain_sid
,
1020 const char *domain_name
,
1021 struct cli_state
*cli
,
1022 struct rpc_pipe_client
*pipe_hnd
,
1023 TALLOC_CTX
*mem_ctx
,
1027 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1028 POLICY_HND connect_pol
, domain_pol
, user_pol
;
1029 SAM_USERINFO_CTR ctr
;
1030 SAM_USER_INFO_24 p24
;
1033 const char *new_password
;
1034 char *prompt
= NULL
;
1037 d_printf("User must be specified\n");
1038 rpc_user_usage(argc
, argv
);
1039 return NT_STATUS_OK
;
1045 new_password
= argv
[1];
1047 asprintf(&prompt
, "Enter new password for %s:", user
);
1048 new_password
= getpass(prompt
);
1052 /* Get sam policy and domain handles */
1054 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1055 pipe_hnd
->cli
->desthost
,
1056 MAXIMUM_ALLOWED_ACCESS
,
1059 if (!NT_STATUS_IS_OK(result
)) {
1063 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1065 MAXIMUM_ALLOWED_ACCESS
,
1066 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1069 if (!NT_STATUS_IS_OK(result
)) {
1073 /* Get handle on user */
1076 uint32
*user_rids
, num_rids
, *name_types
;
1077 uint32 flags
= 0x000003e8; /* Unknown */
1079 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
,
1081 &num_rids
, &user_rids
,
1084 if (!NT_STATUS_IS_OK(result
)) {
1088 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1090 MAXIMUM_ALLOWED_ACCESS
,
1094 if (!NT_STATUS_IS_OK(result
)) {
1099 /* Set password on account */
1104 encode_pw_buffer(pwbuf
, new_password
, STR_UNICODE
);
1106 init_sam_user_info24(&p24
, (char *)pwbuf
,24);
1108 ctr
.switch_value
= 24;
1109 ctr
.info
.id24
= &p24
;
1111 result
= rpccli_samr_set_userinfo(pipe_hnd
, mem_ctx
, &user_pol
, 24,
1112 &cli
->user_session_key
, &ctr
);
1114 if (!NT_STATUS_IS_OK(result
)) {
1118 /* Display results */
1126 * Set a user's password on a remote RPC server
1128 * @param argc Standard main() style argc
1129 * @param argv Standard main() style argv. Initial components are already
1132 * @return A shell status integer (0 for success)
1135 static int rpc_user_password(int argc
, const char **argv
)
1137 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_password_internals
,
1142 * List user's groups on a remote RPC server
1144 * All parameters are provided by the run_rpc_command function, except for
1145 * argc, argv which are passes through.
1147 * @param domain_sid The domain sid acquired from the remote server
1148 * @param cli A cli_state connected to the server.
1149 * @param mem_ctx Talloc context, destoyed on completion of the function.
1150 * @param argc Standard main() style argc
1151 * @param argv Standard main() style argv. Initial components are already
1154 * @return Normal NTSTATUS return.
1157 static NTSTATUS
rpc_user_info_internals(const DOM_SID
*domain_sid
,
1158 const char *domain_name
,
1159 struct cli_state
*cli
,
1160 struct rpc_pipe_client
*pipe_hnd
,
1161 TALLOC_CTX
*mem_ctx
,
1165 POLICY_HND connect_pol
, domain_pol
, user_pol
;
1166 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1167 uint32
*rids
, num_rids
, *name_types
, num_names
;
1168 uint32 flags
= 0x000003e8; /* Unknown */
1170 struct samr_RidWithAttributeArray
*rid_array
= NULL
;
1171 struct lsa_Strings names
;
1172 struct samr_Ids types
;
1175 d_printf("User must be specified\n");
1176 rpc_user_usage(argc
, argv
);
1177 return NT_STATUS_OK
;
1179 /* Get sam policy handle */
1181 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1182 pipe_hnd
->cli
->desthost
,
1183 MAXIMUM_ALLOWED_ACCESS
,
1185 if (!NT_STATUS_IS_OK(result
)) goto done
;
1187 /* Get domain policy handle */
1189 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1191 MAXIMUM_ALLOWED_ACCESS
,
1192 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1194 if (!NT_STATUS_IS_OK(result
)) goto done
;
1196 /* Get handle on user */
1198 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
,
1200 &num_rids
, &rids
, &name_types
);
1202 if (!NT_STATUS_IS_OK(result
)) goto done
;
1204 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1206 MAXIMUM_ALLOWED_ACCESS
,
1209 if (!NT_STATUS_IS_OK(result
)) goto done
;
1211 result
= rpccli_samr_GetGroupsForUser(pipe_hnd
, mem_ctx
,
1215 if (!NT_STATUS_IS_OK(result
)) goto done
;
1217 num_rids
= rid_array
->count
;
1222 if ((rids
= TALLOC_ARRAY(mem_ctx
, uint32
, num_rids
)) == NULL
) {
1223 result
= NT_STATUS_NO_MEMORY
;
1227 for (i
= 0; i
< num_rids
; i
++)
1228 rids
[i
] = rid_array
->rids
[i
].rid
;
1230 result
= rpccli_samr_LookupRids(pipe_hnd
, mem_ctx
,
1237 if (!NT_STATUS_IS_OK(result
)) {
1241 /* Display results */
1243 for (i
= 0; i
< num_names
; i
++)
1244 printf("%s\n", names
.names
[i
].string
);
1251 * List a user's groups from a remote RPC server
1253 * @param argc Standard main() style argc
1254 * @param argv Standard main() style argv. Initial components are already
1257 * @return A shell status integer (0 for success)
1260 static int rpc_user_info(int argc
, const char **argv
)
1262 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_user_info_internals
,
1267 * List users on a remote RPC server
1269 * All parameters are provided by the run_rpc_command function, except for
1270 * argc, argv which are passes through.
1272 * @param domain_sid The domain sid acquired from the remote server
1273 * @param cli A cli_state connected to the server.
1274 * @param mem_ctx Talloc context, destoyed on completion of the function.
1275 * @param argc Standard main() style argc
1276 * @param argv Standard main() style argv. Initial components are already
1279 * @return Normal NTSTATUS return.
1282 static NTSTATUS
rpc_user_list_internals(const DOM_SID
*domain_sid
,
1283 const char *domain_name
,
1284 struct cli_state
*cli
,
1285 struct rpc_pipe_client
*pipe_hnd
,
1286 TALLOC_CTX
*mem_ctx
,
1290 POLICY_HND connect_pol
, domain_pol
;
1291 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1292 uint32 start_idx
=0, num_entries
, i
, loop_count
= 0;
1294 /* Get sam policy handle */
1296 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1297 pipe_hnd
->cli
->desthost
,
1298 MAXIMUM_ALLOWED_ACCESS
,
1300 if (!NT_STATUS_IS_OK(result
)) {
1304 /* Get domain policy handle */
1306 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1308 MAXIMUM_ALLOWED_ACCESS
,
1309 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1311 if (!NT_STATUS_IS_OK(result
)) {
1315 /* Query domain users */
1316 if (opt_long_list_entries
)
1317 d_printf("\nUser name Comment"\
1318 "\n-----------------------------\n");
1320 const char *user
= NULL
;
1321 const char *desc
= NULL
;
1322 uint32 max_entries
, max_size
;
1323 uint32_t total_size
, returned_size
;
1324 union samr_DispInfo info
;
1326 get_query_dispinfo_params(
1327 loop_count
, &max_entries
, &max_size
);
1329 result
= rpccli_samr_QueryDisplayInfo(pipe_hnd
, mem_ctx
,
1339 start_idx
+= info
.info1
.count
;
1340 num_entries
= info
.info1
.count
;
1342 for (i
= 0; i
< num_entries
; i
++) {
1343 user
= info
.info1
.entries
[i
].account_name
.string
;
1344 if (opt_long_list_entries
)
1345 desc
= info
.info1
.entries
[i
].description
.string
;
1346 if (opt_long_list_entries
)
1347 printf("%-21.21s %s\n", user
, desc
);
1349 printf("%s\n", user
);
1351 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
1358 * 'net rpc user' entrypoint.
1359 * @param argc Standard main() style argc
1360 * @param argc Standard main() style argv. Initial components are already
1364 int net_rpc_user(int argc
, const char **argv
)
1366 struct functable func
[] = {
1367 {"add", rpc_user_add
},
1368 {"info", rpc_user_info
},
1369 {"delete", rpc_user_delete
},
1370 {"password", rpc_user_password
},
1371 {"rename", rpc_user_rename
},
1376 return run_rpc_command(NULL
,PI_SAMR
, 0,
1377 rpc_user_list_internals
,
1381 return net_run_function(argc
, argv
, func
, rpc_user_usage
);
1384 static NTSTATUS
rpc_sh_user_list(TALLOC_CTX
*mem_ctx
,
1385 struct rpc_sh_ctx
*ctx
,
1386 struct rpc_pipe_client
*pipe_hnd
,
1387 int argc
, const char **argv
)
1389 return rpc_user_list_internals(ctx
->domain_sid
, ctx
->domain_name
,
1390 ctx
->cli
, pipe_hnd
, mem_ctx
,
1394 static NTSTATUS
rpc_sh_user_info(TALLOC_CTX
*mem_ctx
,
1395 struct rpc_sh_ctx
*ctx
,
1396 struct rpc_pipe_client
*pipe_hnd
,
1397 int argc
, const char **argv
)
1399 return rpc_user_info_internals(ctx
->domain_sid
, ctx
->domain_name
,
1400 ctx
->cli
, pipe_hnd
, mem_ctx
,
1404 static NTSTATUS
rpc_sh_handle_user(TALLOC_CTX
*mem_ctx
,
1405 struct rpc_sh_ctx
*ctx
,
1406 struct rpc_pipe_client
*pipe_hnd
,
1407 int argc
, const char **argv
,
1409 TALLOC_CTX
*mem_ctx
,
1410 struct rpc_sh_ctx
*ctx
,
1411 struct rpc_pipe_client
*pipe_hnd
,
1412 const POLICY_HND
*user_hnd
,
1413 int argc
, const char **argv
))
1416 POLICY_HND connect_pol
, domain_pol
, user_pol
;
1417 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1420 enum lsa_SidType type
;
1423 d_fprintf(stderr
, "usage: %s <username>\n", ctx
->whoami
);
1424 return NT_STATUS_INVALID_PARAMETER
;
1427 ZERO_STRUCT(connect_pol
);
1428 ZERO_STRUCT(domain_pol
);
1429 ZERO_STRUCT(user_pol
);
1431 result
= net_rpc_lookup_name(mem_ctx
, pipe_hnd
->cli
, argv
[0],
1432 NULL
, NULL
, &sid
, &type
);
1433 if (!NT_STATUS_IS_OK(result
)) {
1434 d_fprintf(stderr
, "Could not lookup %s: %s\n", argv
[0],
1439 if (type
!= SID_NAME_USER
) {
1440 d_fprintf(stderr
, "%s is a %s, not a user\n", argv
[0],
1441 sid_type_lookup(type
));
1442 result
= NT_STATUS_NO_SUCH_USER
;
1446 if (!sid_peek_check_rid(ctx
->domain_sid
, &sid
, &rid
)) {
1447 d_fprintf(stderr
, "%s is not in our domain\n", argv
[0]);
1448 result
= NT_STATUS_NO_SUCH_USER
;
1452 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1453 pipe_hnd
->cli
->desthost
,
1454 MAXIMUM_ALLOWED_ACCESS
,
1456 if (!NT_STATUS_IS_OK(result
)) {
1460 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1462 MAXIMUM_ALLOWED_ACCESS
,
1465 if (!NT_STATUS_IS_OK(result
)) {
1469 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1471 MAXIMUM_ALLOWED_ACCESS
,
1474 if (!NT_STATUS_IS_OK(result
)) {
1478 result
= fn(mem_ctx
, ctx
, pipe_hnd
, &user_pol
, argc
-1, argv
+1);
1481 if (is_valid_policy_hnd(&user_pol
)) {
1482 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1484 if (is_valid_policy_hnd(&domain_pol
)) {
1485 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1487 if (is_valid_policy_hnd(&connect_pol
)) {
1488 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1493 static NTSTATUS
rpc_sh_user_show_internals(TALLOC_CTX
*mem_ctx
,
1494 struct rpc_sh_ctx
*ctx
,
1495 struct rpc_pipe_client
*pipe_hnd
,
1496 const POLICY_HND
*user_hnd
,
1497 int argc
, const char **argv
)
1500 SAM_USERINFO_CTR
*ctr
;
1501 SAM_USER_INFO_21
*info
;
1504 d_fprintf(stderr
, "usage: %s show <username>\n", ctx
->whoami
);
1505 return NT_STATUS_INVALID_PARAMETER
;
1508 result
= rpccli_samr_query_userinfo(pipe_hnd
, mem_ctx
, user_hnd
,
1510 if (!NT_STATUS_IS_OK(result
)) {
1514 info
= ctr
->info
.id21
;
1516 d_printf("user rid: %d, group rid: %d\n", info
->user_rid
,
1522 static NTSTATUS
rpc_sh_user_show(TALLOC_CTX
*mem_ctx
,
1523 struct rpc_sh_ctx
*ctx
,
1524 struct rpc_pipe_client
*pipe_hnd
,
1525 int argc
, const char **argv
)
1527 return rpc_sh_handle_user(mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1528 rpc_sh_user_show_internals
);
1531 #define FETCHSTR(name, rec) \
1532 do { if (strequal(ctx->thiscmd, name)) { \
1533 oldval = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_##rec); } \
1536 #define SETSTR(name, rec, flag) \
1537 do { if (strequal(ctx->thiscmd, name)) { \
1538 init_unistr2(&usr->uni_##rec, argv[0], UNI_STR_TERMINATE); \
1539 init_uni_hdr(&usr->hdr_##rec, &usr->uni_##rec); \
1540 usr->fields_present |= ACCT_##flag; } \
1543 static NTSTATUS
rpc_sh_user_str_edit_internals(TALLOC_CTX
*mem_ctx
,
1544 struct rpc_sh_ctx
*ctx
,
1545 struct rpc_pipe_client
*pipe_hnd
,
1546 const POLICY_HND
*user_hnd
,
1547 int argc
, const char **argv
)
1550 SAM_USERINFO_CTR
*ctr
;
1551 SAM_USER_INFO_21
*usr
;
1552 const char *username
;
1553 const char *oldval
= "";
1556 d_fprintf(stderr
, "usage: %s <username> [new value|NULL]\n",
1558 return NT_STATUS_INVALID_PARAMETER
;
1561 result
= rpccli_samr_query_userinfo(pipe_hnd
, mem_ctx
, user_hnd
,
1563 if (!NT_STATUS_IS_OK(result
)) {
1567 usr
= ctr
->info
.id21
;
1569 username
= rpcstr_pull_unistr2_talloc(mem_ctx
, &usr
->uni_user_name
);
1571 FETCHSTR("fullname", full_name
);
1572 FETCHSTR("homedir", home_dir
);
1573 FETCHSTR("homedrive", dir_drive
);
1574 FETCHSTR("logonscript", logon_script
);
1575 FETCHSTR("profilepath", profile_path
);
1576 FETCHSTR("description", acct_desc
);
1579 d_printf("%s's %s: [%s]\n", username
, ctx
->thiscmd
, oldval
);
1585 if (strcmp(argv
[0], "NULL") == 0) {
1589 SETSTR("fullname", full_name
, FULL_NAME
);
1590 SETSTR("homedir", home_dir
, HOME_DIR
);
1591 SETSTR("homedrive", dir_drive
, HOME_DRIVE
);
1592 SETSTR("logonscript", logon_script
, LOGON_SCRIPT
);
1593 SETSTR("profilepath", profile_path
, PROFILE
);
1594 SETSTR("description", acct_desc
, DESCRIPTION
);
1596 result
= rpccli_samr_set_userinfo2(
1597 pipe_hnd
, mem_ctx
, user_hnd
, 21,
1598 &pipe_hnd
->cli
->user_session_key
, ctr
);
1600 d_printf("Set %s's %s from [%s] to [%s]\n", username
,
1601 ctx
->thiscmd
, oldval
, argv
[0]);
1608 #define HANDLEFLG(name, rec) \
1609 do { if (strequal(ctx->thiscmd, name)) { \
1610 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1612 newflags = oldflags | ACB_##rec; \
1614 newflags = oldflags & ~ACB_##rec; \
1617 static NTSTATUS
rpc_sh_user_str_edit(TALLOC_CTX
*mem_ctx
,
1618 struct rpc_sh_ctx
*ctx
,
1619 struct rpc_pipe_client
*pipe_hnd
,
1620 int argc
, const char **argv
)
1622 return rpc_sh_handle_user(mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1623 rpc_sh_user_str_edit_internals
);
1626 static NTSTATUS
rpc_sh_user_flag_edit_internals(TALLOC_CTX
*mem_ctx
,
1627 struct rpc_sh_ctx
*ctx
,
1628 struct rpc_pipe_client
*pipe_hnd
,
1629 const POLICY_HND
*user_hnd
,
1630 int argc
, const char **argv
)
1633 SAM_USERINFO_CTR
*ctr
;
1634 SAM_USER_INFO_21
*usr
;
1635 const char *username
;
1636 const char *oldval
= "unknown";
1637 uint32 oldflags
, newflags
;
1641 ((argc
== 1) && !strequal(argv
[0], "yes") &&
1642 !strequal(argv
[0], "no"))) {
1643 d_fprintf(stderr
, "usage: %s <username> [yes|no]\n",
1645 return NT_STATUS_INVALID_PARAMETER
;
1648 newval
= strequal(argv
[0], "yes");
1650 result
= rpccli_samr_query_userinfo(pipe_hnd
, mem_ctx
, user_hnd
,
1652 if (!NT_STATUS_IS_OK(result
)) {
1656 usr
= ctr
->info
.id21
;
1658 username
= rpcstr_pull_unistr2_talloc(mem_ctx
, &usr
->uni_user_name
);
1659 oldflags
= usr
->acb_info
;
1660 newflags
= usr
->acb_info
;
1662 HANDLEFLG("disabled", DISABLED
);
1663 HANDLEFLG("pwnotreq", PWNOTREQ
);
1664 HANDLEFLG("autolock", AUTOLOCK
);
1665 HANDLEFLG("pwnoexp", PWNOEXP
);
1668 d_printf("%s's %s flag: %s\n", username
, ctx
->thiscmd
, oldval
);
1674 usr
->acb_info
= newflags
;
1675 usr
->fields_present
= ACCT_FLAGS
;
1677 result
= rpccli_samr_set_userinfo2(
1678 pipe_hnd
, mem_ctx
, user_hnd
, 21,
1679 &pipe_hnd
->cli
->user_session_key
, ctr
);
1681 if (NT_STATUS_IS_OK(result
)) {
1682 d_printf("Set %s's %s flag from [%s] to [%s]\n", username
,
1683 ctx
->thiscmd
, oldval
, argv
[0]);
1691 static NTSTATUS
rpc_sh_user_flag_edit(TALLOC_CTX
*mem_ctx
,
1692 struct rpc_sh_ctx
*ctx
,
1693 struct rpc_pipe_client
*pipe_hnd
,
1694 int argc
, const char **argv
)
1696 return rpc_sh_handle_user(mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1697 rpc_sh_user_flag_edit_internals
);
1700 struct rpc_sh_cmd
*net_rpc_user_edit_cmds(TALLOC_CTX
*mem_ctx
,
1701 struct rpc_sh_ctx
*ctx
)
1703 static struct rpc_sh_cmd cmds
[] = {
1705 { "fullname", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1706 "Show/Set a user's full name" },
1708 { "homedir", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1709 "Show/Set a user's home directory" },
1711 { "homedrive", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1712 "Show/Set a user's home drive" },
1714 { "logonscript", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1715 "Show/Set a user's logon script" },
1717 { "profilepath", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1718 "Show/Set a user's profile path" },
1720 { "description", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1721 "Show/Set a user's description" },
1723 { "disabled", NULL
, PI_SAMR
, rpc_sh_user_flag_edit
,
1724 "Show/Set whether a user is disabled" },
1726 { "autolock", NULL
, PI_SAMR
, rpc_sh_user_flag_edit
,
1727 "Show/Set whether a user locked out" },
1729 { "pwnotreq", NULL
, PI_SAMR
, rpc_sh_user_flag_edit
,
1730 "Show/Set whether a user does not need a password" },
1732 { "pwnoexp", NULL
, PI_SAMR
, rpc_sh_user_flag_edit
,
1733 "Show/Set whether a user's password does not expire" },
1735 { NULL
, NULL
, 0, NULL
, NULL
}
1741 struct rpc_sh_cmd
*net_rpc_user_cmds(TALLOC_CTX
*mem_ctx
,
1742 struct rpc_sh_ctx
*ctx
)
1744 static struct rpc_sh_cmd cmds
[] = {
1746 { "list", NULL
, PI_SAMR
, rpc_sh_user_list
,
1747 "List available users" },
1749 { "info", NULL
, PI_SAMR
, rpc_sh_user_info
,
1750 "List the domain groups a user is member of" },
1752 { "show", NULL
, PI_SAMR
, rpc_sh_user_show
,
1753 "Show info about a user" },
1755 { "edit", net_rpc_user_edit_cmds
, 0, NULL
,
1756 "Show/Modify a user's fields" },
1758 { NULL
, NULL
, 0, NULL
, NULL
}
1764 /****************************************************************************/
1767 * Basic usage function for 'net rpc group'
1768 * @param argc Standard main() style argc.
1769 * @param argv Standard main() style argv. Initial components are already
1773 static int rpc_group_usage(int argc
, const char **argv
)
1775 return net_help_group(argc
, argv
);
1779 * Delete group on a remote RPC server
1781 * All parameters are provided by the run_rpc_command function, except for
1782 * argc, argv which are passes through.
1784 * @param domain_sid The domain sid acquired from the remote server
1785 * @param cli A cli_state connected to the server.
1786 * @param mem_ctx Talloc context, destoyed on completion of the function.
1787 * @param argc Standard main() style argc
1788 * @param argv Standard main() style argv. Initial components are already
1791 * @return Normal NTSTATUS return.
1794 static NTSTATUS
rpc_group_delete_internals(const DOM_SID
*domain_sid
,
1795 const char *domain_name
,
1796 struct cli_state
*cli
,
1797 struct rpc_pipe_client
*pipe_hnd
,
1798 TALLOC_CTX
*mem_ctx
,
1802 POLICY_HND connect_pol
, domain_pol
, group_pol
, user_pol
;
1803 bool group_is_primary
= False
;
1804 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1806 uint32
*group_rids
, num_rids
, *name_types
, group_rid
;
1807 struct samr_RidTypeArray
*rids
= NULL
;
1808 uint32 flags
= 0x000003e8; /* Unknown */
1811 /* DOM_GID *user_gids; */
1812 SAM_USERINFO_CTR
*user_ctr
;
1816 d_printf("specify group\n");
1817 rpc_group_usage(argc
,argv
);
1818 return NT_STATUS_OK
; /* ok? */
1821 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1822 pipe_hnd
->cli
->desthost
,
1823 MAXIMUM_ALLOWED_ACCESS
,
1826 if (!NT_STATUS_IS_OK(result
)) {
1827 d_fprintf(stderr
, "Request samr_Connect2 failed\n");
1831 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1833 MAXIMUM_ALLOWED_ACCESS
,
1834 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1837 if (!NT_STATUS_IS_OK(result
)) {
1838 d_fprintf(stderr
, "Request open_domain failed\n");
1842 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
,
1844 &num_rids
, &group_rids
,
1847 if (!NT_STATUS_IS_OK(result
)) {
1848 d_fprintf(stderr
, "Lookup of '%s' failed\n",argv
[0]);
1852 switch (name_types
[0])
1854 case SID_NAME_DOM_GRP
:
1855 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
1857 MAXIMUM_ALLOWED_ACCESS
,
1860 if (!NT_STATUS_IS_OK(result
)) {
1861 d_fprintf(stderr
, "Request open_group failed");
1865 group_rid
= group_rids
[0];
1867 result
= rpccli_samr_QueryGroupMember(pipe_hnd
, mem_ctx
,
1871 if (!NT_STATUS_IS_OK(result
)) {
1872 d_fprintf(stderr
, "Unable to query group members of %s",argv
[0]);
1877 d_printf("Domain Group %s (rid: %d) has %d members\n",
1878 argv
[0],group_rid
, rids
->count
);
1881 /* Check if group is anyone's primary group */
1882 for (i
= 0; i
< rids
->count
; i
++)
1884 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1886 MAXIMUM_ALLOWED_ACCESS
,
1890 if (!NT_STATUS_IS_OK(result
)) {
1891 d_fprintf(stderr
, "Unable to open group member %d\n",
1896 ZERO_STRUCT(user_ctr
);
1898 result
= rpccli_samr_query_userinfo(pipe_hnd
, mem_ctx
, &user_pol
,
1901 if (!NT_STATUS_IS_OK(result
)) {
1902 d_fprintf(stderr
, "Unable to lookup userinfo for group member %d\n",
1907 if (user_ctr
->info
.id21
->group_rid
== group_rid
) {
1908 unistr2_to_ascii(temp
, &(user_ctr
->info
.id21
)->uni_user_name
,
1911 d_printf("Group is primary group of %s\n",temp
);
1912 group_is_primary
= True
;
1915 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1918 if (group_is_primary
) {
1919 d_fprintf(stderr
, "Unable to delete group because some "
1920 "of it's members have it as primary group\n");
1921 result
= NT_STATUS_MEMBERS_PRIMARY_GROUP
;
1925 /* remove all group members */
1926 for (i
= 0; i
< rids
->count
; i
++)
1929 d_printf("Remove group member %d...",
1931 result
= rpccli_samr_DeleteGroupMember(pipe_hnd
, mem_ctx
,
1935 if (NT_STATUS_IS_OK(result
)) {
1940 d_printf("failed\n");
1945 result
= rpccli_samr_DeleteDomainGroup(pipe_hnd
, mem_ctx
,
1949 /* removing a local group is easier... */
1950 case SID_NAME_ALIAS
:
1951 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
1953 MAXIMUM_ALLOWED_ACCESS
,
1957 if (!NT_STATUS_IS_OK(result
)) {
1958 d_fprintf(stderr
, "Request open_alias failed\n");
1962 result
= rpccli_samr_DeleteDomAlias(pipe_hnd
, mem_ctx
,
1966 d_fprintf(stderr
, "%s is of type %s. This command is only for deleting local or global groups\n",
1967 argv
[0],sid_type_lookup(name_types
[0]));
1968 result
= NT_STATUS_UNSUCCESSFUL
;
1973 if (NT_STATUS_IS_OK(result
)) {
1975 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types
[0]),argv
[0]);
1977 d_fprintf(stderr
, "Deleting of %s failed: %s\n",argv
[0],
1978 get_friendly_nt_error_msg(result
));
1986 static int rpc_group_delete(int argc
, const char **argv
)
1988 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_group_delete_internals
,
1992 static NTSTATUS
rpc_group_add_internals(const DOM_SID
*domain_sid
,
1993 const char *domain_name
,
1994 struct cli_state
*cli
,
1995 struct rpc_pipe_client
*pipe_hnd
,
1996 TALLOC_CTX
*mem_ctx
,
2000 POLICY_HND connect_pol
, domain_pol
, group_pol
;
2001 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2002 union samr_GroupInfo group_info
;
2003 struct lsa_String grp_name
;
2007 d_printf("Group name must be specified\n");
2008 rpc_group_usage(argc
, argv
);
2009 return NT_STATUS_OK
;
2012 init_lsa_String(&grp_name
, argv
[0]);
2014 /* Get sam policy handle */
2016 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2017 pipe_hnd
->cli
->desthost
,
2018 MAXIMUM_ALLOWED_ACCESS
,
2020 if (!NT_STATUS_IS_OK(result
)) goto done
;
2022 /* Get domain policy handle */
2024 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2026 MAXIMUM_ALLOWED_ACCESS
,
2027 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2029 if (!NT_STATUS_IS_OK(result
)) goto done
;
2031 /* Create the group */
2033 result
= rpccli_samr_CreateDomainGroup(pipe_hnd
, mem_ctx
,
2036 MAXIMUM_ALLOWED_ACCESS
,
2039 if (!NT_STATUS_IS_OK(result
)) goto done
;
2041 if (strlen(opt_comment
) == 0) goto done
;
2043 /* We've got a comment to set */
2045 init_lsa_String(&group_info
.description
, opt_comment
);
2047 result
= rpccli_samr_SetGroupInfo(pipe_hnd
, mem_ctx
,
2051 if (!NT_STATUS_IS_OK(result
)) goto done
;
2054 if (NT_STATUS_IS_OK(result
))
2055 DEBUG(5, ("add group succeeded\n"));
2057 d_fprintf(stderr
, "add group failed: %s\n", nt_errstr(result
));
2062 static NTSTATUS
rpc_alias_add_internals(const DOM_SID
*domain_sid
,
2063 const char *domain_name
,
2064 struct cli_state
*cli
,
2065 struct rpc_pipe_client
*pipe_hnd
,
2066 TALLOC_CTX
*mem_ctx
,
2070 POLICY_HND connect_pol
, domain_pol
, alias_pol
;
2071 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2072 union samr_AliasInfo alias_info
;
2073 struct lsa_String alias_name
;
2077 d_printf("Alias name must be specified\n");
2078 rpc_group_usage(argc
, argv
);
2079 return NT_STATUS_OK
;
2082 init_lsa_String(&alias_name
, argv
[0]);
2084 /* Get sam policy handle */
2086 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2087 pipe_hnd
->cli
->desthost
,
2088 MAXIMUM_ALLOWED_ACCESS
,
2090 if (!NT_STATUS_IS_OK(result
)) goto done
;
2092 /* Get domain policy handle */
2094 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2096 MAXIMUM_ALLOWED_ACCESS
,
2097 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2099 if (!NT_STATUS_IS_OK(result
)) goto done
;
2101 /* Create the group */
2103 result
= rpccli_samr_CreateDomAlias(pipe_hnd
, mem_ctx
,
2106 MAXIMUM_ALLOWED_ACCESS
,
2109 if (!NT_STATUS_IS_OK(result
)) goto done
;
2111 if (strlen(opt_comment
) == 0) goto done
;
2113 /* We've got a comment to set */
2115 init_lsa_String(&alias_info
.description
, opt_comment
);
2117 result
= rpccli_samr_SetAliasInfo(pipe_hnd
, mem_ctx
,
2122 if (!NT_STATUS_IS_OK(result
)) goto done
;
2125 if (NT_STATUS_IS_OK(result
))
2126 DEBUG(5, ("add alias succeeded\n"));
2128 d_fprintf(stderr
, "add alias failed: %s\n", nt_errstr(result
));
2133 static int rpc_group_add(int argc
, const char **argv
)
2136 return run_rpc_command(NULL
, PI_SAMR
, 0,
2137 rpc_alias_add_internals
,
2140 return run_rpc_command(NULL
, PI_SAMR
, 0,
2141 rpc_group_add_internals
,
2145 static NTSTATUS
get_sid_from_name(struct cli_state
*cli
,
2146 TALLOC_CTX
*mem_ctx
,
2149 enum lsa_SidType
*type
)
2151 DOM_SID
*sids
= NULL
;
2152 enum lsa_SidType
*types
= NULL
;
2153 struct rpc_pipe_client
*pipe_hnd
;
2155 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2157 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &result
);
2162 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, False
,
2163 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
2165 if (!NT_STATUS_IS_OK(result
)) {
2169 result
= rpccli_lsa_lookup_names(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
2170 &name
, NULL
, 1, &sids
, &types
);
2172 if (NT_STATUS_IS_OK(result
)) {
2173 sid_copy(sid
, &sids
[0]);
2177 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
2181 cli_rpc_pipe_close(pipe_hnd
);
2184 if (!NT_STATUS_IS_OK(result
) && (StrnCaseCmp(name
, "S-", 2) == 0)) {
2186 /* Try as S-1-5-whatever */
2190 if (string_to_sid(&tmp_sid
, name
)) {
2191 sid_copy(sid
, &tmp_sid
);
2192 *type
= SID_NAME_UNKNOWN
;
2193 result
= NT_STATUS_OK
;
2200 static NTSTATUS
rpc_add_groupmem(struct rpc_pipe_client
*pipe_hnd
,
2201 TALLOC_CTX
*mem_ctx
,
2202 const DOM_SID
*group_sid
,
2205 POLICY_HND connect_pol
, domain_pol
;
2208 POLICY_HND group_pol
;
2211 uint32
*rids
= NULL
;
2212 uint32
*rid_types
= NULL
;
2216 sid_copy(&sid
, group_sid
);
2218 if (!sid_split_rid(&sid
, &group_rid
)) {
2219 return NT_STATUS_UNSUCCESSFUL
;
2222 /* Get sam policy handle */
2223 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2224 pipe_hnd
->cli
->desthost
,
2225 MAXIMUM_ALLOWED_ACCESS
,
2227 if (!NT_STATUS_IS_OK(result
)) {
2231 /* Get domain policy handle */
2232 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2234 MAXIMUM_ALLOWED_ACCESS
,
2237 if (!NT_STATUS_IS_OK(result
)) {
2241 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
, 1000,
2243 &num_rids
, &rids
, &rid_types
);
2245 if (!NT_STATUS_IS_OK(result
)) {
2246 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2250 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2252 MAXIMUM_ALLOWED_ACCESS
,
2256 if (!NT_STATUS_IS_OK(result
)) {
2260 result
= rpccli_samr_AddGroupMember(pipe_hnd
, mem_ctx
,
2263 0x0005); /* unknown flags */
2266 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2270 static NTSTATUS
rpc_add_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2271 TALLOC_CTX
*mem_ctx
,
2272 const DOM_SID
*alias_sid
,
2275 POLICY_HND connect_pol
, domain_pol
;
2278 POLICY_HND alias_pol
;
2281 enum lsa_SidType member_type
;
2285 sid_copy(&sid
, alias_sid
);
2287 if (!sid_split_rid(&sid
, &alias_rid
)) {
2288 return NT_STATUS_UNSUCCESSFUL
;
2291 result
= get_sid_from_name(pipe_hnd
->cli
, mem_ctx
, member
,
2292 &member_sid
, &member_type
);
2294 if (!NT_STATUS_IS_OK(result
)) {
2295 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2299 /* Get sam policy handle */
2300 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2301 pipe_hnd
->cli
->desthost
,
2302 MAXIMUM_ALLOWED_ACCESS
,
2304 if (!NT_STATUS_IS_OK(result
)) {
2308 /* Get domain policy handle */
2309 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2311 MAXIMUM_ALLOWED_ACCESS
,
2314 if (!NT_STATUS_IS_OK(result
)) {
2318 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2320 MAXIMUM_ALLOWED_ACCESS
,
2324 if (!NT_STATUS_IS_OK(result
)) {
2328 result
= rpccli_samr_AddAliasMember(pipe_hnd
, mem_ctx
,
2332 if (!NT_STATUS_IS_OK(result
)) {
2337 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2341 static NTSTATUS
rpc_group_addmem_internals(const DOM_SID
*domain_sid
,
2342 const char *domain_name
,
2343 struct cli_state
*cli
,
2344 struct rpc_pipe_client
*pipe_hnd
,
2345 TALLOC_CTX
*mem_ctx
,
2350 enum lsa_SidType group_type
;
2353 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2354 return NT_STATUS_UNSUCCESSFUL
;
2357 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2358 &group_sid
, &group_type
))) {
2359 d_fprintf(stderr
, "Could not lookup group name %s\n", argv
[0]);
2360 return NT_STATUS_UNSUCCESSFUL
;
2363 if (group_type
== SID_NAME_DOM_GRP
) {
2364 NTSTATUS result
= rpc_add_groupmem(pipe_hnd
, mem_ctx
,
2365 &group_sid
, argv
[1]);
2367 if (!NT_STATUS_IS_OK(result
)) {
2368 d_fprintf(stderr
, "Could not add %s to %s: %s\n",
2369 argv
[1], argv
[0], nt_errstr(result
));
2374 if (group_type
== SID_NAME_ALIAS
) {
2375 NTSTATUS result
= rpc_add_aliasmem(pipe_hnd
, mem_ctx
,
2376 &group_sid
, argv
[1]);
2378 if (!NT_STATUS_IS_OK(result
)) {
2379 d_fprintf(stderr
, "Could not add %s to %s: %s\n",
2380 argv
[1], argv
[0], nt_errstr(result
));
2385 d_fprintf(stderr
, "Can only add members to global or local groups "
2386 "which %s is not\n", argv
[0]);
2388 return NT_STATUS_UNSUCCESSFUL
;
2391 static int rpc_group_addmem(int argc
, const char **argv
)
2393 return run_rpc_command(NULL
, PI_SAMR
, 0,
2394 rpc_group_addmem_internals
,
2398 static NTSTATUS
rpc_del_groupmem(struct rpc_pipe_client
*pipe_hnd
,
2399 TALLOC_CTX
*mem_ctx
,
2400 const DOM_SID
*group_sid
,
2403 POLICY_HND connect_pol
, domain_pol
;
2406 POLICY_HND group_pol
;
2409 uint32
*rids
= NULL
;
2410 uint32
*rid_types
= NULL
;
2414 sid_copy(&sid
, group_sid
);
2416 if (!sid_split_rid(&sid
, &group_rid
))
2417 return NT_STATUS_UNSUCCESSFUL
;
2419 /* Get sam policy handle */
2420 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2421 pipe_hnd
->cli
->desthost
,
2422 MAXIMUM_ALLOWED_ACCESS
,
2424 if (!NT_STATUS_IS_OK(result
))
2427 /* Get domain policy handle */
2428 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2430 MAXIMUM_ALLOWED_ACCESS
,
2433 if (!NT_STATUS_IS_OK(result
))
2436 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
, 1000,
2438 &num_rids
, &rids
, &rid_types
);
2440 if (!NT_STATUS_IS_OK(result
)) {
2441 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2445 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2447 MAXIMUM_ALLOWED_ACCESS
,
2451 if (!NT_STATUS_IS_OK(result
))
2454 result
= rpccli_samr_DeleteGroupMember(pipe_hnd
, mem_ctx
,
2459 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2463 static NTSTATUS
rpc_del_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2464 TALLOC_CTX
*mem_ctx
,
2465 const DOM_SID
*alias_sid
,
2468 POLICY_HND connect_pol
, domain_pol
;
2471 POLICY_HND alias_pol
;
2474 enum lsa_SidType member_type
;
2478 sid_copy(&sid
, alias_sid
);
2480 if (!sid_split_rid(&sid
, &alias_rid
))
2481 return NT_STATUS_UNSUCCESSFUL
;
2483 result
= get_sid_from_name(pipe_hnd
->cli
, mem_ctx
, member
,
2484 &member_sid
, &member_type
);
2486 if (!NT_STATUS_IS_OK(result
)) {
2487 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2491 /* Get sam policy handle */
2492 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2493 pipe_hnd
->cli
->desthost
,
2494 MAXIMUM_ALLOWED_ACCESS
,
2496 if (!NT_STATUS_IS_OK(result
)) {
2500 /* Get domain policy handle */
2501 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2503 MAXIMUM_ALLOWED_ACCESS
,
2506 if (!NT_STATUS_IS_OK(result
)) {
2510 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2512 MAXIMUM_ALLOWED_ACCESS
,
2516 if (!NT_STATUS_IS_OK(result
))
2519 result
= rpccli_samr_DeleteAliasMember(pipe_hnd
, mem_ctx
,
2523 if (!NT_STATUS_IS_OK(result
))
2527 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2531 static NTSTATUS
rpc_group_delmem_internals(const DOM_SID
*domain_sid
,
2532 const char *domain_name
,
2533 struct cli_state
*cli
,
2534 struct rpc_pipe_client
*pipe_hnd
,
2535 TALLOC_CTX
*mem_ctx
,
2540 enum lsa_SidType group_type
;
2543 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2544 return NT_STATUS_UNSUCCESSFUL
;
2547 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2548 &group_sid
, &group_type
))) {
2549 d_fprintf(stderr
, "Could not lookup group name %s\n", argv
[0]);
2550 return NT_STATUS_UNSUCCESSFUL
;
2553 if (group_type
== SID_NAME_DOM_GRP
) {
2554 NTSTATUS result
= rpc_del_groupmem(pipe_hnd
, mem_ctx
,
2555 &group_sid
, argv
[1]);
2557 if (!NT_STATUS_IS_OK(result
)) {
2558 d_fprintf(stderr
, "Could not del %s from %s: %s\n",
2559 argv
[1], argv
[0], nt_errstr(result
));
2564 if (group_type
== SID_NAME_ALIAS
) {
2565 NTSTATUS result
= rpc_del_aliasmem(pipe_hnd
, mem_ctx
,
2566 &group_sid
, argv
[1]);
2568 if (!NT_STATUS_IS_OK(result
)) {
2569 d_fprintf(stderr
, "Could not del %s from %s: %s\n",
2570 argv
[1], argv
[0], nt_errstr(result
));
2575 d_fprintf(stderr
, "Can only delete members from global or local groups "
2576 "which %s is not\n", argv
[0]);
2578 return NT_STATUS_UNSUCCESSFUL
;
2581 static int rpc_group_delmem(int argc
, const char **argv
)
2583 return run_rpc_command(NULL
, PI_SAMR
, 0,
2584 rpc_group_delmem_internals
,
2589 * List groups on a remote RPC server
2591 * All parameters are provided by the run_rpc_command function, except for
2592 * argc, argv which are passes through.
2594 * @param domain_sid The domain sid acquired from the remote server
2595 * @param cli A cli_state connected to the server.
2596 * @param mem_ctx Talloc context, destoyed on completion of the function.
2597 * @param argc Standard main() style argc
2598 * @param argv Standard main() style argv. Initial components are already
2601 * @return Normal NTSTATUS return.
2604 static NTSTATUS
rpc_group_list_internals(const DOM_SID
*domain_sid
,
2605 const char *domain_name
,
2606 struct cli_state
*cli
,
2607 struct rpc_pipe_client
*pipe_hnd
,
2608 TALLOC_CTX
*mem_ctx
,
2612 POLICY_HND connect_pol
, domain_pol
;
2613 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2614 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
2615 struct acct_info
*groups
;
2616 bool global
= False
;
2618 bool builtin
= False
;
2626 for (i
=0; i
<argc
; i
++) {
2627 if (strequal(argv
[i
], "global"))
2630 if (strequal(argv
[i
], "local"))
2633 if (strequal(argv
[i
], "builtin"))
2637 /* Get sam policy handle */
2639 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2640 pipe_hnd
->cli
->desthost
,
2641 MAXIMUM_ALLOWED_ACCESS
,
2643 if (!NT_STATUS_IS_OK(result
)) {
2647 /* Get domain policy handle */
2649 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2651 MAXIMUM_ALLOWED_ACCESS
,
2652 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2654 if (!NT_STATUS_IS_OK(result
)) {
2658 /* Query domain groups */
2659 if (opt_long_list_entries
)
2660 d_printf("\nGroup name Comment"\
2661 "\n-----------------------------\n");
2663 uint32_t max_size
, total_size
, returned_size
;
2664 union samr_DispInfo info
;
2668 get_query_dispinfo_params(
2669 loop_count
, &max_entries
, &max_size
);
2671 result
= rpccli_samr_QueryDisplayInfo(pipe_hnd
, mem_ctx
,
2680 num_entries
= info
.info3
.count
;
2681 start_idx
+= info
.info3
.count
;
2683 if (!NT_STATUS_IS_OK(result
) &&
2684 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2687 for (i
= 0; i
< num_entries
; i
++) {
2689 const char *group
= NULL
;
2690 const char *desc
= NULL
;
2692 group
= info
.info3
.entries
[i
].account_name
.string
;
2693 desc
= info
.info3
.entries
[i
].description
.string
;
2695 if (opt_long_list_entries
)
2696 printf("%-21.21s %-50.50s\n",
2699 printf("%s\n", group
);
2701 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2702 /* query domain aliases */
2707 /* The max_size field in cli_samr_enum_als_groups is more like
2708 * an account_control field with indiviual bits what to
2709 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2710 * everything. I'm too lazy (sorry) to get this through to
2711 * rpc_parse/ etc. Volker */
2713 result
= rpccli_samr_enum_als_groups(pipe_hnd
, mem_ctx
, &domain_pol
,
2715 &groups
, &num_entries
);
2717 if (!NT_STATUS_IS_OK(result
) &&
2718 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2721 for (i
= 0; i
< num_entries
; i
++) {
2723 const char *description
= NULL
;
2725 if (opt_long_list_entries
) {
2727 POLICY_HND alias_pol
;
2728 union samr_AliasInfo
*info
= NULL
;
2730 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2735 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd
, mem_ctx
,
2739 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd
, mem_ctx
,
2741 description
= info
->description
.string
;
2745 if (description
!= NULL
) {
2746 printf("%-21.21s %-50.50s\n",
2747 groups
[i
].acct_name
,
2750 printf("%s\n", groups
[i
].acct_name
);
2753 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2754 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
2755 /* Get builtin policy handle */
2757 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2759 MAXIMUM_ALLOWED_ACCESS
,
2760 CONST_DISCARD(struct dom_sid2
*, &global_sid_Builtin
),
2762 if (!NT_STATUS_IS_OK(result
)) {
2765 /* query builtin aliases */
2768 if (!builtin
) break;
2770 result
= rpccli_samr_enum_als_groups(pipe_hnd
, mem_ctx
, &domain_pol
,
2771 &start_idx
, max_entries
,
2772 &groups
, &num_entries
);
2774 if (!NT_STATUS_IS_OK(result
) &&
2775 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2778 for (i
= 0; i
< num_entries
; i
++) {
2780 const char *description
= NULL
;
2782 if (opt_long_list_entries
) {
2784 POLICY_HND alias_pol
;
2785 union samr_AliasInfo
*info
= NULL
;
2787 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2792 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd
, mem_ctx
,
2796 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd
, mem_ctx
,
2798 description
= info
->description
.string
;
2802 if (description
!= NULL
) {
2803 printf("%-21.21s %-50.50s\n",
2804 groups
[i
].acct_name
,
2807 printf("%s\n", groups
[i
].acct_name
);
2810 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2816 static int rpc_group_list(int argc
, const char **argv
)
2818 return run_rpc_command(NULL
, PI_SAMR
, 0,
2819 rpc_group_list_internals
,
2823 static NTSTATUS
rpc_list_group_members(struct rpc_pipe_client
*pipe_hnd
,
2824 TALLOC_CTX
*mem_ctx
,
2825 const char *domain_name
,
2826 const DOM_SID
*domain_sid
,
2827 POLICY_HND
*domain_pol
,
2831 POLICY_HND group_pol
;
2832 uint32 num_members
, *group_rids
;
2834 struct samr_RidTypeArray
*rids
= NULL
;
2835 struct lsa_Strings names
;
2836 struct samr_Ids types
;
2839 sid_to_fstring(sid_str
, domain_sid
);
2841 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2843 MAXIMUM_ALLOWED_ACCESS
,
2847 if (!NT_STATUS_IS_OK(result
))
2850 result
= rpccli_samr_QueryGroupMember(pipe_hnd
, mem_ctx
,
2854 if (!NT_STATUS_IS_OK(result
))
2857 num_members
= rids
->count
;
2858 group_rids
= rids
->rids
;
2860 while (num_members
> 0) {
2861 int this_time
= 512;
2863 if (num_members
< this_time
)
2864 this_time
= num_members
;
2866 result
= rpccli_samr_LookupRids(pipe_hnd
, mem_ctx
,
2873 if (!NT_STATUS_IS_OK(result
))
2876 /* We only have users as members, but make the output
2877 the same as the output of alias members */
2879 for (i
= 0; i
< this_time
; i
++) {
2881 if (opt_long_list_entries
) {
2882 printf("%s-%d %s\\%s %d\n", sid_str
,
2883 group_rids
[i
], domain_name
,
2884 names
.names
[i
].string
,
2887 printf("%s\\%s\n", domain_name
,
2888 names
.names
[i
].string
);
2892 num_members
-= this_time
;
2896 return NT_STATUS_OK
;
2899 static NTSTATUS
rpc_list_alias_members(struct rpc_pipe_client
*pipe_hnd
,
2900 TALLOC_CTX
*mem_ctx
,
2901 POLICY_HND
*domain_pol
,
2905 struct rpc_pipe_client
*lsa_pipe
;
2906 POLICY_HND alias_pol
, lsa_pol
;
2908 DOM_SID
*alias_sids
;
2911 enum lsa_SidType
*types
;
2913 struct lsa_SidArray sid_array
;
2915 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2917 MAXIMUM_ALLOWED_ACCESS
,
2921 if (!NT_STATUS_IS_OK(result
))
2924 result
= rpccli_samr_GetMembersInAlias(pipe_hnd
, mem_ctx
,
2928 if (!NT_STATUS_IS_OK(result
)) {
2929 d_fprintf(stderr
, "Couldn't list alias members\n");
2933 num_members
= sid_array
.num_sids
;
2935 if (num_members
== 0) {
2936 return NT_STATUS_OK
;
2939 lsa_pipe
= cli_rpc_pipe_open_noauth(pipe_hnd
->cli
, PI_LSARPC
, &result
);
2941 d_fprintf(stderr
, "Couldn't open LSA pipe. Error was %s\n",
2942 nt_errstr(result
) );
2946 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, True
,
2947 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
2949 if (!NT_STATUS_IS_OK(result
)) {
2950 d_fprintf(stderr
, "Couldn't open LSA policy handle\n");
2951 cli_rpc_pipe_close(lsa_pipe
);
2955 alias_sids
= TALLOC_ZERO_ARRAY(mem_ctx
, DOM_SID
, num_members
);
2957 d_fprintf(stderr
, "Out of memory\n");
2958 cli_rpc_pipe_close(lsa_pipe
);
2959 return NT_STATUS_NO_MEMORY
;
2962 for (i
=0; i
<num_members
; i
++) {
2963 sid_copy(&alias_sids
[i
], sid_array
.sids
[i
].sid
);
2966 result
= rpccli_lsa_lookup_sids(lsa_pipe
, mem_ctx
, &lsa_pol
, num_members
,
2968 &domains
, &names
, &types
);
2970 if (!NT_STATUS_IS_OK(result
) &&
2971 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
2972 d_fprintf(stderr
, "Couldn't lookup SIDs\n");
2973 cli_rpc_pipe_close(lsa_pipe
);
2977 for (i
= 0; i
< num_members
; i
++) {
2979 sid_to_fstring(sid_str
, &alias_sids
[i
]);
2981 if (opt_long_list_entries
) {
2982 printf("%s %s\\%s %d\n", sid_str
,
2983 domains
[i
] ? domains
[i
] : "*unknown*",
2984 names
[i
] ? names
[i
] : "*unknown*", types
[i
]);
2987 printf("%s\\%s\n", domains
[i
], names
[i
]);
2989 printf("%s\n", sid_str
);
2993 cli_rpc_pipe_close(lsa_pipe
);
2994 return NT_STATUS_OK
;
2997 static NTSTATUS
rpc_group_members_internals(const DOM_SID
*domain_sid
,
2998 const char *domain_name
,
2999 struct cli_state
*cli
,
3000 struct rpc_pipe_client
*pipe_hnd
,
3001 TALLOC_CTX
*mem_ctx
,
3006 POLICY_HND connect_pol
, domain_pol
;
3007 uint32 num_rids
, *rids
, *rid_types
;
3009 /* Get sam policy handle */
3011 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
3012 pipe_hnd
->cli
->desthost
,
3013 MAXIMUM_ALLOWED_ACCESS
,
3016 if (!NT_STATUS_IS_OK(result
))
3019 /* Get domain policy handle */
3021 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
3023 MAXIMUM_ALLOWED_ACCESS
,
3024 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
3027 if (!NT_STATUS_IS_OK(result
))
3030 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
, 1000,
3031 1, argv
, &num_rids
, &rids
, &rid_types
);
3033 if (!NT_STATUS_IS_OK(result
)) {
3035 /* Ok, did not find it in the global sam, try with builtin */
3037 DOM_SID sid_Builtin
;
3039 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
3041 string_to_sid(&sid_Builtin
, "S-1-5-32");
3043 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
3045 MAXIMUM_ALLOWED_ACCESS
,
3049 if (!NT_STATUS_IS_OK(result
)) {
3050 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
3054 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
, 1000,
3058 if (!NT_STATUS_IS_OK(result
)) {
3059 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
3064 if (num_rids
!= 1) {
3065 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
3069 if (rid_types
[0] == SID_NAME_DOM_GRP
) {
3070 return rpc_list_group_members(pipe_hnd
, mem_ctx
, domain_name
,
3071 domain_sid
, &domain_pol
,
3075 if (rid_types
[0] == SID_NAME_ALIAS
) {
3076 return rpc_list_alias_members(pipe_hnd
, mem_ctx
, &domain_pol
,
3080 return NT_STATUS_NO_SUCH_GROUP
;
3083 static int rpc_group_members(int argc
, const char **argv
)
3086 return rpc_group_usage(argc
, argv
);
3089 return run_rpc_command(NULL
, PI_SAMR
, 0,
3090 rpc_group_members_internals
,
3094 static NTSTATUS
rpc_group_rename_internals(const DOM_SID
*domain_sid
,
3095 const char *domain_name
,
3096 struct cli_state
*cli
,
3097 struct rpc_pipe_client
*pipe_hnd
,
3098 TALLOC_CTX
*mem_ctx
,
3103 POLICY_HND connect_pol
, domain_pol
, group_pol
;
3104 uint32 num_rids
, *rids
, *rid_types
;
3105 union samr_GroupInfo group_info
;
3108 d_printf("Usage: 'net rpc group rename group newname'\n");
3109 return NT_STATUS_UNSUCCESSFUL
;
3112 /* Get sam policy handle */
3114 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
3115 pipe_hnd
->cli
->desthost
,
3116 MAXIMUM_ALLOWED_ACCESS
,
3119 if (!NT_STATUS_IS_OK(result
))
3122 /* Get domain policy handle */
3124 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
3126 MAXIMUM_ALLOWED_ACCESS
,
3127 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
3130 if (!NT_STATUS_IS_OK(result
))
3133 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
, 1000,
3134 1, argv
, &num_rids
, &rids
, &rid_types
);
3136 if (num_rids
!= 1) {
3137 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
3141 if (rid_types
[0] != SID_NAME_DOM_GRP
) {
3142 d_fprintf(stderr
, "Can only rename domain groups\n");
3143 return NT_STATUS_UNSUCCESSFUL
;
3146 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
3148 MAXIMUM_ALLOWED_ACCESS
,
3152 if (!NT_STATUS_IS_OK(result
))
3155 init_lsa_String(&group_info
.name
, argv
[1]);
3157 result
= rpccli_samr_SetGroupInfo(pipe_hnd
, mem_ctx
,
3162 if (!NT_STATUS_IS_OK(result
))
3165 return NT_STATUS_NO_SUCH_GROUP
;
3168 static int rpc_group_rename(int argc
, const char **argv
)
3171 return rpc_group_usage(argc
, argv
);
3174 return run_rpc_command(NULL
, PI_SAMR
, 0,
3175 rpc_group_rename_internals
,
3180 * 'net rpc group' entrypoint.
3181 * @param argc Standard main() style argc
3182 * @param argc Standard main() style argv. Initial components are already
3186 int net_rpc_group(int argc
, const char **argv
)
3188 struct functable func
[] = {
3189 {"add", rpc_group_add
},
3190 {"delete", rpc_group_delete
},
3191 {"addmem", rpc_group_addmem
},
3192 {"delmem", rpc_group_delmem
},
3193 {"list", rpc_group_list
},
3194 {"members", rpc_group_members
},
3195 {"rename", rpc_group_rename
},
3200 return run_rpc_command(NULL
, PI_SAMR
, 0,
3201 rpc_group_list_internals
,
3205 return net_run_function(argc
, argv
, func
, rpc_group_usage
);
3208 /****************************************************************************/
3210 static int rpc_share_usage(int argc
, const char **argv
)
3212 return net_help_share(argc
, argv
);
3216 * Add a share on a remote RPC server
3218 * All parameters are provided by the run_rpc_command function, except for
3219 * argc, argv which are passes through.
3221 * @param domain_sid The domain sid acquired from the remote server
3222 * @param cli A cli_state connected to the server.
3223 * @param mem_ctx Talloc context, destoyed on completion of the function.
3224 * @param argc Standard main() style argc
3225 * @param argv Standard main() style argv. Initial components are already
3228 * @return Normal NTSTATUS return.
3230 static NTSTATUS
rpc_share_add_internals(const DOM_SID
*domain_sid
,
3231 const char *domain_name
,
3232 struct cli_state
*cli
,
3233 struct rpc_pipe_client
*pipe_hnd
,
3234 TALLOC_CTX
*mem_ctx
,int argc
,
3240 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
3241 uint32 num_users
=0, perms
=0;
3242 char *password
=NULL
; /* don't allow a share password */
3245 if ((sharename
= talloc_strdup(mem_ctx
, argv
[0])) == NULL
) {
3246 return NT_STATUS_NO_MEMORY
;
3249 path
= strchr(sharename
, '=');
3251 return NT_STATUS_UNSUCCESSFUL
;
3254 result
= rpccli_srvsvc_net_share_add(pipe_hnd
, mem_ctx
, sharename
, type
,
3255 opt_comment
, perms
, opt_maxusers
,
3256 num_users
, path
, password
,
3258 return werror_to_ntstatus(result
);
3261 static int rpc_share_add(int argc
, const char **argv
)
3263 if ((argc
< 1) || !strchr(argv
[0], '=')) {
3264 DEBUG(1,("Sharename or path not specified on add\n"));
3265 return rpc_share_usage(argc
, argv
);
3267 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
3268 rpc_share_add_internals
,
3273 * Delete a share on a remote RPC server
3275 * All parameters are provided by the run_rpc_command function, except for
3276 * argc, argv which are passes through.
3278 * @param domain_sid The domain sid acquired from the remote server
3279 * @param cli A cli_state connected to the server.
3280 * @param mem_ctx Talloc context, destoyed on completion of the function.
3281 * @param argc Standard main() style argc
3282 * @param argv Standard main() style argv. Initial components are already
3285 * @return Normal NTSTATUS return.
3287 static NTSTATUS
rpc_share_del_internals(const DOM_SID
*domain_sid
,
3288 const char *domain_name
,
3289 struct cli_state
*cli
,
3290 struct rpc_pipe_client
*pipe_hnd
,
3291 TALLOC_CTX
*mem_ctx
,
3297 result
= rpccli_srvsvc_net_share_del(pipe_hnd
, mem_ctx
, argv
[0]);
3298 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3302 * Delete a share on a remote RPC server
3304 * @param domain_sid The domain sid acquired from the remote server
3305 * @param argc Standard main() style argc
3306 * @param argv Standard main() style argv. Initial components are already
3309 * @return A shell status integer (0 for success)
3311 static int rpc_share_delete(int argc
, const char **argv
)
3314 DEBUG(1,("Sharename not specified on delete\n"));
3315 return rpc_share_usage(argc
, argv
);
3317 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
3318 rpc_share_del_internals
,
3323 * Formatted print of share info
3325 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3328 static void display_share_info_1(SRV_SHARE_INFO_1
*info1
)
3330 fstring netname
= "", remark
= "";
3332 rpcstr_pull_unistr2_fstring(netname
, &info1
->info_1_str
.uni_netname
);
3333 rpcstr_pull_unistr2_fstring(remark
, &info1
->info_1_str
.uni_remark
);
3335 if (opt_long_list_entries
) {
3336 d_printf("%-12s %-8.8s %-50s\n",
3337 netname
, share_type
[info1
->info_1
.type
& ~(STYPE_TEMPORARY
|STYPE_HIDDEN
)], remark
);
3339 d_printf("%s\n", netname
);
3344 static WERROR
get_share_info(struct rpc_pipe_client
*pipe_hnd
,
3345 TALLOC_CTX
*mem_ctx
,
3349 SRV_SHARE_INFO_CTR
*ctr
)
3352 SRV_SHARE_INFO info
;
3354 /* no specific share requested, enumerate all */
3358 uint32 preferred_len
= 0xffffffff;
3360 init_enum_hnd(&hnd
, 0);
3362 return rpccli_srvsvc_net_share_enum(pipe_hnd
, mem_ctx
, level
, ctr
,
3363 preferred_len
, &hnd
);
3366 /* request just one share */
3367 result
= rpccli_srvsvc_net_share_get_info(pipe_hnd
, mem_ctx
, argv
[0], level
, &info
);
3369 if (!W_ERROR_IS_OK(result
))
3375 ctr
->info_level
= ctr
->switch_value
= level
;
3376 ctr
->ptr_share_info
= ctr
->ptr_entries
= 1;
3377 ctr
->num_entries
= ctr
->num_entries2
= 1;
3383 SRV_SHARE_INFO_1
*info1
;
3385 ctr
->share
.info1
= TALLOC_ARRAY(mem_ctx
, SRV_SHARE_INFO_1
, 1);
3386 if (ctr
->share
.info1
== NULL
) {
3387 result
= WERR_NOMEM
;
3390 info1
= ctr
->share
.info1
;
3392 memset(ctr
->share
.info1
, 0, sizeof(SRV_SHARE_INFO_1
));
3394 /* Copy pointer crap */
3396 memcpy(&info1
->info_1
, &info
.share
.info1
.info_1
, sizeof(SH_INFO_1
));
3398 /* Duplicate strings */
3400 s
= unistr2_to_ascii_talloc(mem_ctx
, &info
.share
.info1
.info_1_str
.uni_netname
);
3402 init_unistr2(&info1
->info_1_str
.uni_netname
, s
, UNI_STR_TERMINATE
);
3404 s
= unistr2_to_ascii_talloc(mem_ctx
, &info
.share
.info1
.info_1_str
.uni_remark
);
3406 init_unistr2(&info1
->info_1_str
.uni_remark
, s
, UNI_STR_TERMINATE
);
3411 SRV_SHARE_INFO_2
*info2
;
3413 ctr
->share
.info2
= TALLOC_ARRAY(mem_ctx
, SRV_SHARE_INFO_2
, 1);
3414 if (ctr
->share
.info2
== NULL
) {
3415 result
= WERR_NOMEM
;
3418 info2
= ctr
->share
.info2
;
3420 memset(ctr
->share
.info2
, 0, sizeof(SRV_SHARE_INFO_2
));
3422 /* Copy pointer crap */
3424 memcpy(&info2
->info_2
, &info
.share
.info2
.info_2
, sizeof(SH_INFO_2
));
3426 /* Duplicate strings */
3428 s
= unistr2_to_ascii_talloc(mem_ctx
, &info
.share
.info2
.info_2_str
.uni_netname
);
3430 init_unistr2(&info2
->info_2_str
.uni_netname
, s
, UNI_STR_TERMINATE
);
3432 s
= unistr2_to_ascii_talloc(mem_ctx
, &info
.share
.info2
.info_2_str
.uni_remark
);
3434 init_unistr2(&info2
->info_2_str
.uni_remark
, s
, UNI_STR_TERMINATE
);
3436 s
= unistr2_to_ascii_talloc(mem_ctx
, &info
.share
.info2
.info_2_str
.uni_path
);
3438 init_unistr2(&info2
->info_2_str
.uni_path
, s
, UNI_STR_TERMINATE
);
3440 s
= unistr2_to_ascii_talloc(mem_ctx
, &info
.share
.info2
.info_2_str
.uni_passwd
);
3442 init_unistr2(&info2
->info_2_str
.uni_passwd
, s
, UNI_STR_TERMINATE
);
3447 SRV_SHARE_INFO_502
*info502
;
3449 ctr
->share
.info502
= TALLOC_ARRAY(mem_ctx
, SRV_SHARE_INFO_502
, 1);
3450 if (ctr
->share
.info502
== NULL
) {
3451 result
= WERR_NOMEM
;
3454 info502
= ctr
->share
.info502
;
3456 memset(ctr
->share
.info502
, 0, sizeof(SRV_SHARE_INFO_502
));
3458 /* Copy pointer crap */
3460 memcpy(&info502
->info_502
, &info
.share
.info502
.info_502
, sizeof(SH_INFO_502
));
3462 /* Duplicate strings */
3464 s
= unistr2_to_ascii_talloc(mem_ctx
, &info
.share
.info502
.info_502_str
.uni_netname
);
3466 init_unistr2(&info502
->info_502_str
.uni_netname
, s
, UNI_STR_TERMINATE
);
3468 s
= unistr2_to_ascii_talloc(mem_ctx
, &info
.share
.info502
.info_502_str
.uni_remark
);
3470 init_unistr2(&info502
->info_502_str
.uni_remark
, s
, UNI_STR_TERMINATE
);
3472 s
= unistr2_to_ascii_talloc(mem_ctx
, &info
.share
.info502
.info_502_str
.uni_path
);
3474 init_unistr2(&info502
->info_502_str
.uni_path
, s
, UNI_STR_TERMINATE
);
3476 s
= unistr2_to_ascii_talloc(mem_ctx
, &info
.share
.info502
.info_502_str
.uni_passwd
);
3478 init_unistr2(&info502
->info_502_str
.uni_passwd
, s
, UNI_STR_TERMINATE
);
3480 info502
->info_502_str
.sd
= dup_sec_desc(mem_ctx
, info
.share
.info502
.info_502_str
.sd
);
3491 * List shares on a remote RPC server
3493 * All parameters are provided by the run_rpc_command function, except for
3494 * argc, argv which are passes through.
3496 * @param domain_sid The domain sid acquired from the remote server
3497 * @param cli A cli_state connected to the server.
3498 * @param mem_ctx Talloc context, destoyed on completion of the function.
3499 * @param argc Standard main() style argc
3500 * @param argv Standard main() style argv. Initial components are already
3503 * @return Normal NTSTATUS return.
3506 static NTSTATUS
rpc_share_list_internals(const DOM_SID
*domain_sid
,
3507 const char *domain_name
,
3508 struct cli_state
*cli
,
3509 struct rpc_pipe_client
*pipe_hnd
,
3510 TALLOC_CTX
*mem_ctx
,
3514 SRV_SHARE_INFO_CTR ctr
;
3516 uint32 i
, level
= 1;
3518 result
= get_share_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &ctr
);
3519 if (!W_ERROR_IS_OK(result
))
3522 /* Display results */
3524 if (opt_long_list_entries
) {
3526 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3527 "\nShare name Type Description\n"\
3528 "---------- ---- -----------\n");
3530 for (i
= 0; i
< ctr
.num_entries
; i
++)
3531 display_share_info_1(&ctr
.share
.info1
[i
]);
3533 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3537 * 'net rpc share list' entrypoint.
3538 * @param argc Standard main() style argc
3539 * @param argv Standard main() style argv. Initial components are already
3542 static int rpc_share_list(int argc
, const char **argv
)
3544 return run_rpc_command(NULL
, PI_SRVSVC
, 0, rpc_share_list_internals
, argc
, argv
);
3547 static bool check_share_availability(struct cli_state
*cli
, const char *netname
)
3549 if (!cli_send_tconX(cli
, netname
, "A:", "", 0)) {
3550 d_printf("skipping [%s]: not a file share.\n", netname
);
3560 static bool check_share_sanity(struct cli_state
*cli
, fstring netname
, uint32 type
)
3562 /* only support disk shares */
3563 if (! ( type
== STYPE_DISKTREE
|| type
== (STYPE_DISKTREE
| STYPE_HIDDEN
)) ) {
3564 printf("share [%s] is not a diskshare (type: %x)\n", netname
, type
);
3568 /* skip builtin shares */
3569 /* FIXME: should print$ be added too ? */
3570 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$") ||
3571 strequal(netname
,"global"))
3574 if (opt_exclude
&& in_list(netname
, opt_exclude
, False
)) {
3575 printf("excluding [%s]\n", netname
);
3579 return check_share_availability(cli
, netname
);
3583 * Migrate shares from a remote RPC server to the local RPC srever
3585 * All parameters are provided by the run_rpc_command function, except for
3586 * argc, argv which are passes through.
3588 * @param domain_sid The domain sid acquired from the remote server
3589 * @param cli A cli_state connected to the server.
3590 * @param mem_ctx Talloc context, destoyed on completion of the function.
3591 * @param argc Standard main() style argc
3592 * @param argv Standard main() style argv. Initial components are already
3595 * @return Normal NTSTATUS return.
3598 static NTSTATUS
rpc_share_migrate_shares_internals(const DOM_SID
*domain_sid
,
3599 const char *domain_name
,
3600 struct cli_state
*cli
,
3601 struct rpc_pipe_client
*pipe_hnd
,
3602 TALLOC_CTX
*mem_ctx
,
3607 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3608 SRV_SHARE_INFO_CTR ctr_src
;
3609 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
3610 char *password
= NULL
; /* don't allow a share password */
3612 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3613 struct cli_state
*cli_dst
= NULL
;
3614 uint32 level
= 502; /* includes secdesc */
3616 result
= get_share_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &ctr_src
);
3617 if (!W_ERROR_IS_OK(result
))
3620 /* connect destination PI_SRVSVC */
3621 nt_status
= connect_dst_pipe(&cli_dst
, &srvsvc_pipe
, PI_SRVSVC
);
3622 if (!NT_STATUS_IS_OK(nt_status
))
3626 for (i
= 0; i
< ctr_src
.num_entries
; i
++) {
3628 fstring netname
= "", remark
= "", path
= "";
3629 /* reset error-code */
3630 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3632 rpcstr_pull_unistr2_fstring(
3633 netname
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_netname
);
3634 rpcstr_pull_unistr2_fstring(
3635 remark
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_remark
);
3636 rpcstr_pull_unistr2_fstring(
3637 path
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_path
);
3639 if (!check_share_sanity(cli
, netname
, ctr_src
.share
.info502
[i
].info_502
.type
))
3642 /* finally add the share on the dst server */
3644 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3645 netname
, path
, remark
);
3647 result
= rpccli_srvsvc_net_share_add(srvsvc_pipe
, mem_ctx
, netname
, type
, remark
,
3648 ctr_src
.share
.info502
[i
].info_502
.perms
,
3649 ctr_src
.share
.info502
[i
].info_502
.max_uses
,
3650 ctr_src
.share
.info502
[i
].info_502
.num_uses
,
3651 path
, password
, level
,
3654 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ALREADY_EXISTS
)) {
3655 printf(" [%s] does already exist\n", netname
);
3659 if (!W_ERROR_IS_OK(result
)) {
3660 printf("cannot add share: %s\n", dos_errstr(result
));
3666 nt_status
= NT_STATUS_OK
;
3670 cli_shutdown(cli_dst
);
3678 * Migrate shares from a rpc-server to another
3680 * @param argc Standard main() style argc
3681 * @param argv Standard main() style argv. Initial components are already
3684 * @return A shell status integer (0 for success)
3686 static int rpc_share_migrate_shares(int argc
, const char **argv
)
3690 printf("no server to migrate\n");
3694 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
3695 rpc_share_migrate_shares_internals
,
3702 * @param f file_info
3703 * @param mask current search mask
3704 * @param state arg-pointer
3707 static void copy_fn(const char *mnt
, file_info
*f
, const char *mask
, void *state
)
3709 static NTSTATUS nt_status
;
3710 static struct copy_clistate
*local_state
;
3711 static fstring filename
, new_mask
;
3715 local_state
= (struct copy_clistate
*)state
;
3716 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3718 if (strequal(f
->name
, ".") || strequal(f
->name
, ".."))
3721 DEBUG(3,("got mask: %s, name: %s\n", mask
, f
->name
));
3724 if (f
->mode
& aDIR
) {
3726 DEBUG(3,("got dir: %s\n", f
->name
));
3728 fstrcpy(dir
, local_state
->cwd
);
3730 fstrcat(dir
, f
->name
);
3732 switch (net_mode_share
)
3734 case NET_MODE_SHARE_MIGRATE
:
3735 /* create that directory */
3736 nt_status
= net_copy_file(local_state
->mem_ctx
,
3737 local_state
->cli_share_src
,
3738 local_state
->cli_share_dst
,
3740 opt_acls
? True
: False
,
3741 opt_attrs
? True
: False
,
3742 opt_timestamps
? True
: False
,
3746 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3750 if (!NT_STATUS_IS_OK(nt_status
))
3751 printf("could not handle dir %s: %s\n",
3752 dir
, nt_errstr(nt_status
));
3754 /* search below that directory */
3755 fstrcpy(new_mask
, dir
);
3756 fstrcat(new_mask
, "\\*");
3758 old_dir
= local_state
->cwd
;
3759 local_state
->cwd
= dir
;
3760 if (!sync_files(local_state
, new_mask
))
3761 printf("could not handle files\n");
3762 local_state
->cwd
= old_dir
;
3769 fstrcpy(filename
, local_state
->cwd
);
3770 fstrcat(filename
, "\\");
3771 fstrcat(filename
, f
->name
);
3773 DEBUG(3,("got file: %s\n", filename
));
3775 switch (net_mode_share
)
3777 case NET_MODE_SHARE_MIGRATE
:
3778 nt_status
= net_copy_file(local_state
->mem_ctx
,
3779 local_state
->cli_share_src
,
3780 local_state
->cli_share_dst
,
3782 opt_acls
? True
: False
,
3783 opt_attrs
? True
: False
,
3784 opt_timestamps
? True
: False
,
3788 d_fprintf(stderr
, "Unsupported file mode %d\n", net_mode_share
);
3792 if (!NT_STATUS_IS_OK(nt_status
))
3793 printf("could not handle file %s: %s\n",
3794 filename
, nt_errstr(nt_status
));
3799 * sync files, can be called recursivly to list files
3800 * and then call copy_fn for each file
3802 * @param cp_clistate pointer to the copy_clistate we work with
3803 * @param mask the current search mask
3805 * @return Boolean result
3807 static bool sync_files(struct copy_clistate
*cp_clistate
, const char *mask
)
3809 struct cli_state
*targetcli
;
3810 char *targetpath
= NULL
;
3812 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
3814 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate
->cli_share_src
,
3815 mask
, &targetcli
, &targetpath
) ) {
3816 d_fprintf(stderr
, "cli_resolve_path %s failed with error: %s\n",
3817 mask
, cli_errstr(cp_clistate
->cli_share_src
));
3821 if (cli_list(targetcli
, targetpath
, cp_clistate
->attribute
, copy_fn
, cp_clistate
) == -1) {
3822 d_fprintf(stderr
, "listing %s failed with error: %s\n",
3823 mask
, cli_errstr(targetcli
));
3832 * Set the top level directory permissions before we do any further copies.
3833 * Should set up ACL inheritance.
3836 bool copy_top_level_perms(struct copy_clistate
*cp_clistate
,
3837 const char *sharename
)
3839 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3841 switch (net_mode_share
) {
3842 case NET_MODE_SHARE_MIGRATE
:
3843 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename
));
3844 nt_status
= net_copy_fileattr(cp_clistate
->mem_ctx
,
3845 cp_clistate
->cli_share_src
,
3846 cp_clistate
->cli_share_dst
,
3848 opt_acls
? True
: False
,
3849 opt_attrs
? True
: False
,
3850 opt_timestamps
? True
: False
,
3854 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3858 if (!NT_STATUS_IS_OK(nt_status
)) {
3859 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3860 sharename
, nt_errstr(nt_status
));
3868 * Sync all files inside a remote share to another share (over smb)
3870 * All parameters are provided by the run_rpc_command function, except for
3871 * argc, argv which are passes through.
3873 * @param domain_sid The domain sid acquired from the remote server
3874 * @param cli A cli_state connected to the server.
3875 * @param mem_ctx Talloc context, destoyed on completion of the function.
3876 * @param argc Standard main() style argc
3877 * @param argv Standard main() style argv. Initial components are already
3880 * @return Normal NTSTATUS return.
3883 static NTSTATUS
rpc_share_migrate_files_internals(const DOM_SID
*domain_sid
,
3884 const char *domain_name
,
3885 struct cli_state
*cli
,
3886 struct rpc_pipe_client
*pipe_hnd
,
3887 TALLOC_CTX
*mem_ctx
,
3892 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3893 SRV_SHARE_INFO_CTR ctr_src
;
3896 struct copy_clistate cp_clistate
;
3897 bool got_src_share
= False
;
3898 bool got_dst_share
= False
;
3899 const char *mask
= "\\*";
3902 dst
= SMB_STRDUP(opt_destination
?opt_destination
:"127.0.0.1");
3904 result
= get_share_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &ctr_src
);
3906 if (!W_ERROR_IS_OK(result
))
3909 for (i
= 0; i
< ctr_src
.num_entries
; i
++) {
3911 fstring netname
= "";
3913 rpcstr_pull_unistr2_fstring(
3914 netname
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_netname
);
3916 if (!check_share_sanity(cli
, netname
, ctr_src
.share
.info502
[i
].info_502
.type
))
3919 /* one might not want to mirror whole discs :) */
3920 if (strequal(netname
, "print$") || netname
[1] == '$') {
3921 d_printf("skipping [%s]: builtin/hidden share\n", netname
);
3925 switch (net_mode_share
)
3927 case NET_MODE_SHARE_MIGRATE
:
3931 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3934 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3936 opt_acls
? "including" : "without",
3937 opt_attrs
? "including" : "without",
3938 opt_timestamps
? "(preserving timestamps)" : "");
3940 cp_clistate
.mem_ctx
= mem_ctx
;
3941 cp_clistate
.cli_share_src
= NULL
;
3942 cp_clistate
.cli_share_dst
= NULL
;
3943 cp_clistate
.cwd
= NULL
;
3944 cp_clistate
.attribute
= aSYSTEM
| aHIDDEN
| aDIR
;
3946 /* open share source */
3947 nt_status
= connect_to_service(&cp_clistate
.cli_share_src
,
3948 &cli
->dest_ss
, cli
->desthost
,
3950 if (!NT_STATUS_IS_OK(nt_status
))
3953 got_src_share
= True
;
3955 if (net_mode_share
== NET_MODE_SHARE_MIGRATE
) {
3956 /* open share destination */
3957 nt_status
= connect_to_service(&cp_clistate
.cli_share_dst
,
3958 NULL
, dst
, netname
, "A:");
3959 if (!NT_STATUS_IS_OK(nt_status
))
3962 got_dst_share
= True
;
3965 if (!copy_top_level_perms(&cp_clistate
, netname
)) {
3966 d_fprintf(stderr
, "Could not handle the top level directory permissions for the share: %s\n", netname
);
3967 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3971 if (!sync_files(&cp_clistate
, mask
)) {
3972 d_fprintf(stderr
, "could not handle files for share: %s\n", netname
);
3973 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3978 nt_status
= NT_STATUS_OK
;
3983 cli_shutdown(cp_clistate
.cli_share_src
);
3986 cli_shutdown(cp_clistate
.cli_share_dst
);
3992 static int rpc_share_migrate_files(int argc
, const char **argv
)
3996 printf("no server to migrate\n");
4000 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
4001 rpc_share_migrate_files_internals
,
4006 * Migrate share-ACLs from a remote RPC server to the local RPC srever
4008 * All parameters are provided by the run_rpc_command function, except for
4009 * argc, argv which are passes through.
4011 * @param domain_sid The domain sid acquired from the remote server
4012 * @param cli A cli_state connected to the server.
4013 * @param mem_ctx Talloc context, destoyed on completion of the function.
4014 * @param argc Standard main() style argc
4015 * @param argv Standard main() style argv. Initial components are already
4018 * @return Normal NTSTATUS return.
4021 static NTSTATUS
rpc_share_migrate_security_internals(const DOM_SID
*domain_sid
,
4022 const char *domain_name
,
4023 struct cli_state
*cli
,
4024 struct rpc_pipe_client
*pipe_hnd
,
4025 TALLOC_CTX
*mem_ctx
,
4030 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
4031 SRV_SHARE_INFO_CTR ctr_src
;
4032 SRV_SHARE_INFO info
;
4034 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
4035 struct cli_state
*cli_dst
= NULL
;
4036 uint32 level
= 502; /* includes secdesc */
4038 result
= get_share_info(pipe_hnd
, mem_ctx
, level
, argc
, argv
, &ctr_src
);
4040 if (!W_ERROR_IS_OK(result
))
4043 /* connect destination PI_SRVSVC */
4044 nt_status
= connect_dst_pipe(&cli_dst
, &srvsvc_pipe
, PI_SRVSVC
);
4045 if (!NT_STATUS_IS_OK(nt_status
))
4049 for (i
= 0; i
< ctr_src
.num_entries
; i
++) {
4051 fstring netname
= "", remark
= "", path
= "";
4052 /* reset error-code */
4053 nt_status
= NT_STATUS_UNSUCCESSFUL
;
4055 rpcstr_pull_unistr2_fstring(
4056 netname
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_netname
);
4057 rpcstr_pull_unistr2_fstring(
4058 remark
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_remark
);
4059 rpcstr_pull_unistr2_fstring(
4060 path
, &ctr_src
.share
.info502
[i
].info_502_str
.uni_path
);
4062 if (!check_share_sanity(cli
, netname
, ctr_src
.share
.info502
[i
].info_502
.type
))
4065 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
4066 netname
, path
, remark
);
4069 display_sec_desc(ctr_src
.share
.info502
[i
].info_502_str
.sd
);
4074 info
.switch_value
= level
;
4075 info
.ptr_share_ctr
= 1;
4077 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4078 info
.share
.info502
= ctr_src
.share
.info502
[i
];
4080 /* finally modify the share on the dst server */
4081 result
= rpccli_srvsvc_net_share_set_info(srvsvc_pipe
, mem_ctx
, netname
, level
, &info
);
4083 if (!W_ERROR_IS_OK(result
)) {
4084 printf("cannot set share-acl: %s\n", dos_errstr(result
));
4090 nt_status
= NT_STATUS_OK
;
4094 cli_shutdown(cli_dst
);
4102 * Migrate share-acls from a rpc-server to another
4104 * @param argc Standard main() style argc
4105 * @param argv Standard main() style argv. Initial components are already
4108 * @return A shell status integer (0 for success)
4110 static int rpc_share_migrate_security(int argc
, const char **argv
)
4114 printf("no server to migrate\n");
4118 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
4119 rpc_share_migrate_security_internals
,
4124 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4125 * from one server to another
4127 * @param argc Standard main() style argc
4128 * @param argv Standard main() style argv. Initial components are already
4131 * @return A shell status integer (0 for success)
4134 static int rpc_share_migrate_all(int argc
, const char **argv
)
4139 printf("no server to migrate\n");
4143 /* order is important. we don't want to be locked out by the share-acl
4144 * before copying files - gd */
4146 ret
= run_rpc_command(NULL
, PI_SRVSVC
, 0, rpc_share_migrate_shares_internals
, argc
, argv
);
4150 ret
= run_rpc_command(NULL
, PI_SRVSVC
, 0, rpc_share_migrate_files_internals
, argc
, argv
);
4154 return run_rpc_command(NULL
, PI_SRVSVC
, 0, rpc_share_migrate_security_internals
, argc
, argv
);
4159 * 'net rpc share migrate' entrypoint.
4160 * @param argc Standard main() style argc
4161 * @param argv Standard main() style argv. Initial components are already
4164 static int rpc_share_migrate(int argc
, const char **argv
)
4167 struct functable func
[] = {
4168 {"all", rpc_share_migrate_all
},
4169 {"files", rpc_share_migrate_files
},
4170 {"help", rpc_share_usage
},
4171 {"security", rpc_share_migrate_security
},
4172 {"shares", rpc_share_migrate_shares
},
4176 net_mode_share
= NET_MODE_SHARE_MIGRATE
;
4178 return net_run_function(argc
, argv
, func
, rpc_share_usage
);
4187 static int num_server_aliases
;
4188 static struct full_alias
*server_aliases
;
4191 * Add an alias to the static list.
4193 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
4195 if (server_aliases
== NULL
)
4196 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
4198 server_aliases
[num_server_aliases
] = *alias
;
4199 num_server_aliases
+= 1;
4203 * For a specific domain on the server, fetch all the aliases
4204 * and their members. Add all of them to the server_aliases.
4207 static NTSTATUS
rpc_fetch_domain_aliases(struct rpc_pipe_client
*pipe_hnd
,
4208 TALLOC_CTX
*mem_ctx
,
4209 POLICY_HND
*connect_pol
,
4210 const DOM_SID
*domain_sid
)
4212 uint32 start_idx
, max_entries
, num_entries
, i
;
4213 struct acct_info
*groups
;
4215 POLICY_HND domain_pol
;
4217 /* Get domain policy handle */
4219 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
4221 MAXIMUM_ALLOWED_ACCESS
,
4222 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
4224 if (!NT_STATUS_IS_OK(result
))
4231 result
= rpccli_samr_enum_als_groups(pipe_hnd
, mem_ctx
, &domain_pol
,
4232 &start_idx
, max_entries
,
4233 &groups
, &num_entries
);
4235 for (i
= 0; i
< num_entries
; i
++) {
4237 POLICY_HND alias_pol
;
4238 struct full_alias alias
;
4239 struct lsa_SidArray sid_array
;
4242 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
4244 MAXIMUM_ALLOWED_ACCESS
,
4247 if (!NT_STATUS_IS_OK(result
))
4250 result
= rpccli_samr_GetMembersInAlias(pipe_hnd
, mem_ctx
,
4253 if (!NT_STATUS_IS_OK(result
))
4256 alias
.num_members
= sid_array
.num_sids
;
4258 result
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &alias_pol
);
4259 if (!NT_STATUS_IS_OK(result
))
4262 alias
.members
= NULL
;
4264 if (alias
.num_members
> 0) {
4265 alias
.members
= SMB_MALLOC_ARRAY(DOM_SID
, alias
.num_members
);
4267 for (j
= 0; j
< alias
.num_members
; j
++)
4268 sid_copy(&alias
.members
[j
],
4269 sid_array
.sids
[j
].sid
);
4272 sid_copy(&alias
.sid
, domain_sid
);
4273 sid_append_rid(&alias
.sid
, groups
[i
].rid
);
4275 push_alias(mem_ctx
, &alias
);
4277 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
4279 result
= NT_STATUS_OK
;
4282 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
4288 * Dump server_aliases as names for debugging purposes.
4291 static NTSTATUS
rpc_aliaslist_dump(const DOM_SID
*domain_sid
,
4292 const char *domain_name
,
4293 struct cli_state
*cli
,
4294 struct rpc_pipe_client
*pipe_hnd
,
4295 TALLOC_CTX
*mem_ctx
,
4303 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, True
,
4304 SEC_RIGHTS_MAXIMUM_ALLOWED
,
4306 if (!NT_STATUS_IS_OK(result
))
4309 for (i
=0; i
<num_server_aliases
; i
++) {
4312 enum lsa_SidType
*types
;
4315 struct full_alias
*alias
= &server_aliases
[i
];
4317 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
4319 &domains
, &names
, &types
);
4320 if (!NT_STATUS_IS_OK(result
))
4323 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
4325 if (alias
->num_members
== 0) {
4330 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
,
4333 &domains
, &names
, &types
);
4335 if (!NT_STATUS_IS_OK(result
) &&
4336 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
4339 for (j
=0; j
<alias
->num_members
; j
++)
4340 DEBUG(1, ("%s\\%s (%d); ",
4341 domains
[j
] ? domains
[j
] : "*unknown*",
4342 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
4346 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
4348 return NT_STATUS_OK
;
4352 * Fetch a list of all server aliases and their members into
4356 static NTSTATUS
rpc_aliaslist_internals(const DOM_SID
*domain_sid
,
4357 const char *domain_name
,
4358 struct cli_state
*cli
,
4359 struct rpc_pipe_client
*pipe_hnd
,
4360 TALLOC_CTX
*mem_ctx
,
4365 POLICY_HND connect_pol
;
4367 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
4368 pipe_hnd
->cli
->desthost
,
4369 MAXIMUM_ALLOWED_ACCESS
,
4372 if (!NT_STATUS_IS_OK(result
))
4375 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4376 &global_sid_Builtin
);
4378 if (!NT_STATUS_IS_OK(result
))
4381 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4384 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
4389 static void init_user_token(NT_USER_TOKEN
*token
, DOM_SID
*user_sid
)
4391 token
->num_sids
= 4;
4393 if (!(token
->user_sids
= SMB_MALLOC_ARRAY(DOM_SID
, 4))) {
4394 d_fprintf(stderr
, "malloc failed\n");
4395 token
->num_sids
= 0;
4399 token
->user_sids
[0] = *user_sid
;
4400 sid_copy(&token
->user_sids
[1], &global_sid_World
);
4401 sid_copy(&token
->user_sids
[2], &global_sid_Network
);
4402 sid_copy(&token
->user_sids
[3], &global_sid_Authenticated_Users
);
4405 static void free_user_token(NT_USER_TOKEN
*token
)
4407 SAFE_FREE(token
->user_sids
);
4410 static bool is_sid_in_token(NT_USER_TOKEN
*token
, DOM_SID
*sid
)
4414 for (i
=0; i
<token
->num_sids
; i
++) {
4415 if (sid_compare(sid
, &token
->user_sids
[i
]) == 0)
4421 static void add_sid_to_token(NT_USER_TOKEN
*token
, DOM_SID
*sid
)
4423 if (is_sid_in_token(token
, sid
))
4426 token
->user_sids
= SMB_REALLOC_ARRAY(token
->user_sids
, DOM_SID
, token
->num_sids
+1);
4427 if (!token
->user_sids
) {
4431 sid_copy(&token
->user_sids
[token
->num_sids
], sid
);
4433 token
->num_sids
+= 1;
4438 NT_USER_TOKEN token
;
4441 static void dump_user_token(struct user_token
*token
)
4445 d_printf("%s\n", token
->name
);
4447 for (i
=0; i
<token
->token
.num_sids
; i
++) {
4448 d_printf(" %s\n", sid_string_tos(&token
->token
.user_sids
[i
]));
4452 static bool is_alias_member(DOM_SID
*sid
, struct full_alias
*alias
)
4456 for (i
=0; i
<alias
->num_members
; i
++) {
4457 if (sid_compare(sid
, &alias
->members
[i
]) == 0)
4464 static void collect_sid_memberships(NT_USER_TOKEN
*token
, DOM_SID sid
)
4468 for (i
=0; i
<num_server_aliases
; i
++) {
4469 if (is_alias_member(&sid
, &server_aliases
[i
]))
4470 add_sid_to_token(token
, &server_aliases
[i
].sid
);
4475 * We got a user token with all the SIDs we can know about without asking the
4476 * server directly. These are the user and domain group sids. All of these can
4477 * be members of aliases. So scan the list of aliases for each of the SIDs and
4478 * add them to the token.
4481 static void collect_alias_memberships(NT_USER_TOKEN
*token
)
4483 int num_global_sids
= token
->num_sids
;
4486 for (i
=0; i
<num_global_sids
; i
++) {
4487 collect_sid_memberships(token
, token
->user_sids
[i
]);
4491 static bool get_user_sids(const char *domain
, const char *user
, NT_USER_TOKEN
*token
)
4493 struct winbindd_request request
;
4494 struct winbindd_response response
;
4502 fstr_sprintf(full_name
, "%s%c%s",
4503 domain
, *lp_winbind_separator(), user
);
4505 /* First let's find out the user sid */
4507 ZERO_STRUCT(request
);
4508 ZERO_STRUCT(response
);
4510 fstrcpy(request
.data
.name
.dom_name
, domain
);
4511 fstrcpy(request
.data
.name
.name
, user
);
4513 result
= winbindd_request_response(WINBINDD_LOOKUPNAME
, &request
, &response
);
4515 if (result
!= NSS_STATUS_SUCCESS
) {
4516 DEBUG(1, ("winbind could not find %s\n", full_name
));
4520 if (response
.data
.sid
.type
!= SID_NAME_USER
) {
4521 DEBUG(1, ("%s is not a user\n", full_name
));
4525 string_to_sid(&user_sid
, response
.data
.sid
.sid
);
4527 init_user_token(token
, &user_sid
);
4529 /* And now the groups winbind knows about */
4531 ZERO_STRUCT(response
);
4533 fstrcpy(request
.data
.username
, full_name
);
4535 result
= winbindd_request_response(WINBINDD_GETGROUPS
, &request
, &response
);
4537 if (result
!= NSS_STATUS_SUCCESS
) {
4538 DEBUG(1, ("winbind could not get groups of %s\n", full_name
));
4542 for (i
= 0; i
< response
.data
.num_entries
; i
++) {
4543 gid_t gid
= ((gid_t
*)response
.extra_data
.data
)[i
];
4546 struct winbindd_request sidrequest
;
4547 struct winbindd_response sidresponse
;
4549 ZERO_STRUCT(sidrequest
);
4550 ZERO_STRUCT(sidresponse
);
4552 sidrequest
.data
.gid
= gid
;
4554 result
= winbindd_request_response(WINBINDD_GID_TO_SID
,
4555 &sidrequest
, &sidresponse
);
4557 if (result
!= NSS_STATUS_SUCCESS
) {
4558 DEBUG(1, ("winbind could not find SID of gid %d\n",
4563 DEBUG(3, (" %s\n", sidresponse
.data
.sid
.sid
));
4565 string_to_sid(&sid
, sidresponse
.data
.sid
.sid
);
4566 add_sid_to_token(token
, &sid
);
4569 SAFE_FREE(response
.extra_data
.data
);
4575 * Get a list of all user tokens we want to look at
4578 static bool get_user_tokens(int *num_tokens
, struct user_token
**user_tokens
)
4580 struct winbindd_request request
;
4581 struct winbindd_response response
;
4582 const char *extra_data
;
4585 struct user_token
*result
;
4586 TALLOC_CTX
*frame
= NULL
;
4588 if (lp_winbind_use_default_domain() &&
4589 (opt_target_workgroup
== NULL
)) {
4590 d_fprintf(stderr
, "winbind use default domain = yes set, "
4591 "please specify a workgroup\n");
4595 /* Send request to winbind daemon */
4597 ZERO_STRUCT(request
);
4598 ZERO_STRUCT(response
);
4600 if (winbindd_request_response(WINBINDD_LIST_USERS
, &request
, &response
) !=
4604 /* Look through extra data */
4606 if (!response
.extra_data
.data
)
4609 extra_data
= (const char *)response
.extra_data
.data
;
4612 frame
= talloc_stackframe();
4613 while(next_token_talloc(frame
, &extra_data
, &name
, ",")) {
4617 result
= SMB_MALLOC_ARRAY(struct user_token
, *num_tokens
);
4619 if (result
== NULL
) {
4620 DEBUG(1, ("Could not malloc sid array\n"));
4625 extra_data
= (const char *)response
.extra_data
.data
;
4628 while(next_token_talloc(frame
, &extra_data
, &name
, ",")) {
4629 fstring domain
, user
;
4632 fstrcpy(result
[i
].name
, name
);
4634 p
= strchr(name
, *lp_winbind_separator());
4636 DEBUG(3, ("%s\n", name
));
4639 fstrcpy(domain
, opt_target_workgroup
);
4640 fstrcpy(user
, name
);
4643 fstrcpy(domain
, name
);
4648 get_user_sids(domain
, user
, &(result
[i
].token
));
4652 SAFE_FREE(response
.extra_data
.data
);
4654 *user_tokens
= result
;
4659 static bool get_user_tokens_from_file(FILE *f
,
4661 struct user_token
**tokens
)
4663 struct user_token
*token
= NULL
;
4668 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4672 if (line
[strlen(line
)-1] == '\n')
4673 line
[strlen(line
)-1] = '\0';
4675 if (line
[0] == ' ') {
4679 string_to_sid(&sid
, &line
[1]);
4681 if (token
== NULL
) {
4682 DEBUG(0, ("File does not begin with username"));
4686 add_sid_to_token(&token
->token
, &sid
);
4690 /* And a new user... */
4693 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4694 if (*tokens
== NULL
) {
4695 DEBUG(0, ("Could not realloc tokens\n"));
4699 token
= &((*tokens
)[*num_tokens
-1]);
4701 fstrcpy(token
->name
, line
);
4702 token
->token
.num_sids
= 0;
4703 token
->token
.user_sids
= NULL
;
4712 * Show the list of all users that have access to a share
4715 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4716 TALLOC_CTX
*mem_ctx
,
4717 const char *netname
,
4719 struct user_token
*tokens
)
4722 SEC_DESC
*share_sd
= NULL
;
4723 SEC_DESC
*root_sd
= NULL
;
4724 struct cli_state
*cli
= pipe_hnd
->cli
;
4726 SRV_SHARE_INFO info
;
4730 result
= rpccli_srvsvc_net_share_get_info(pipe_hnd
, mem_ctx
, netname
,
4733 if (!W_ERROR_IS_OK(result
)) {
4734 DEBUG(1, ("Coult not query secdesc for share %s\n",
4739 share_sd
= info
.share
.info502
.info_502_str
.sd
;
4740 if (share_sd
== NULL
) {
4741 DEBUG(1, ("Got no secdesc for share %s\n",
4747 if (!cli_send_tconX(cli
, netname
, "A:", "", 0)) {
4751 fnum
= cli_nt_create(cli
, "\\", READ_CONTROL_ACCESS
);
4754 root_sd
= cli_query_secdesc(cli
, fnum
, mem_ctx
);
4757 for (i
=0; i
<num_tokens
; i
++) {
4761 if (share_sd
!= NULL
) {
4762 if (!se_access_check(share_sd
, &tokens
[i
].token
,
4763 1, &acc_granted
, &status
)) {
4764 DEBUG(1, ("Could not check share_sd for "
4770 if (!NT_STATUS_IS_OK(status
))
4774 if (root_sd
== NULL
) {
4775 d_printf(" %s\n", tokens
[i
].name
);
4779 if (!se_access_check(root_sd
, &tokens
[i
].token
,
4780 1, &acc_granted
, &status
)) {
4781 DEBUG(1, ("Could not check root_sd for user %s\n",
4786 if (!NT_STATUS_IS_OK(status
))
4789 d_printf(" %s\n", tokens
[i
].name
);
4793 cli_close(cli
, fnum
);
4805 static void collect_share(const char *name
, uint32 m
,
4806 const char *comment
, void *state
)
4808 struct share_list
*share_list
= (struct share_list
*)state
;
4810 if (m
!= STYPE_DISKTREE
)
4813 share_list
->num_shares
+= 1;
4814 share_list
->shares
= SMB_REALLOC_ARRAY(share_list
->shares
, char *, share_list
->num_shares
);
4815 if (!share_list
->shares
) {
4816 share_list
->num_shares
= 0;
4819 share_list
->shares
[share_list
->num_shares
-1] = SMB_STRDUP(name
);
4822 static void rpc_share_userlist_usage(void)
4828 * List shares on a remote RPC server, including the security descriptors
4830 * All parameters are provided by the run_rpc_command function, except for
4831 * argc, argv which are passes through.
4833 * @param domain_sid The domain sid acquired from the remote server
4834 * @param cli A cli_state connected to the server.
4835 * @param mem_ctx Talloc context, destoyed on completion of the function.
4836 * @param argc Standard main() style argc
4837 * @param argv Standard main() style argv. Initial components are already
4840 * @return Normal NTSTATUS return.
4843 static NTSTATUS
rpc_share_allowedusers_internals(const DOM_SID
*domain_sid
,
4844 const char *domain_name
,
4845 struct cli_state
*cli
,
4846 struct rpc_pipe_client
*pipe_hnd
,
4847 TALLOC_CTX
*mem_ctx
,
4857 struct user_token
*tokens
= NULL
;
4860 struct share_list share_list
;
4863 rpc_share_userlist_usage();
4864 return NT_STATUS_UNSUCCESSFUL
;
4870 f
= fopen(argv
[0], "r");
4874 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4875 return NT_STATUS_UNSUCCESSFUL
;
4878 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
4884 DEBUG(0, ("Could not read users from file\n"));
4885 return NT_STATUS_UNSUCCESSFUL
;
4888 for (i
=0; i
<num_tokens
; i
++)
4889 collect_alias_memberships(&tokens
[i
].token
);
4891 init_enum_hnd(&hnd
, 0);
4893 share_list
.num_shares
= 0;
4894 share_list
.shares
= NULL
;
4896 ret
= cli_RNetShareEnum(cli
, collect_share
, &share_list
);
4899 DEBUG(0, ("Error returning browse list: %s\n",
4904 for (i
= 0; i
< share_list
.num_shares
; i
++) {
4905 char *netname
= share_list
.shares
[i
];
4907 if (netname
[strlen(netname
)-1] == '$')
4910 d_printf("%s\n", netname
);
4912 show_userlist(pipe_hnd
, mem_ctx
, netname
,
4913 num_tokens
, tokens
);
4916 for (i
=0; i
<num_tokens
; i
++) {
4917 free_user_token(&tokens
[i
].token
);
4920 SAFE_FREE(share_list
.shares
);
4922 return NT_STATUS_OK
;
4925 static int rpc_share_allowedusers(int argc
, const char **argv
)
4929 result
= run_rpc_command(NULL
, PI_SAMR
, 0,
4930 rpc_aliaslist_internals
,
4935 result
= run_rpc_command(NULL
, PI_LSARPC
, 0,
4941 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
4942 rpc_share_allowedusers_internals
,
4946 int net_usersidlist(int argc
, const char **argv
)
4949 struct user_token
*tokens
= NULL
;
4953 net_usersidlist_usage(argc
, argv
);
4957 if (!get_user_tokens(&num_tokens
, &tokens
)) {
4958 DEBUG(0, ("Could not get the user/sid list\n"));
4962 for (i
=0; i
<num_tokens
; i
++) {
4963 dump_user_token(&tokens
[i
]);
4964 free_user_token(&tokens
[i
].token
);
4971 int net_usersidlist_usage(int argc
, const char **argv
)
4973 d_printf("net usersidlist\n"
4974 "\tprints out a list of all users the running winbind knows\n"
4975 "\tabout, together with all their SIDs. This is used as\n"
4976 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4978 net_common_flags_usage(argc
, argv
);
4983 * 'net rpc share' entrypoint.
4984 * @param argc Standard main() style argc
4985 * @param argv Standard main() style argv. Initial components are already
4989 int net_rpc_share(int argc
, const char **argv
)
4991 struct functable func
[] = {
4992 {"add", rpc_share_add
},
4993 {"delete", rpc_share_delete
},
4994 {"allowedusers", rpc_share_allowedusers
},
4995 {"migrate", rpc_share_migrate
},
4996 {"list", rpc_share_list
},
5001 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
5002 rpc_share_list_internals
,
5005 return net_run_function(argc
, argv
, func
, rpc_share_usage
);
5008 static NTSTATUS
rpc_sh_share_list(TALLOC_CTX
*mem_ctx
,
5009 struct rpc_sh_ctx
*ctx
,
5010 struct rpc_pipe_client
*pipe_hnd
,
5011 int argc
, const char **argv
)
5013 return rpc_share_list_internals(ctx
->domain_sid
, ctx
->domain_name
,
5014 ctx
->cli
, pipe_hnd
, mem_ctx
,
5018 static NTSTATUS
rpc_sh_share_add(TALLOC_CTX
*mem_ctx
,
5019 struct rpc_sh_ctx
*ctx
,
5020 struct rpc_pipe_client
*pipe_hnd
,
5021 int argc
, const char **argv
)
5025 if ((argc
< 2) || (argc
> 3)) {
5026 d_fprintf(stderr
, "usage: %s <share> <path> [comment]\n",
5028 return NT_STATUS_INVALID_PARAMETER
;
5031 result
= rpccli_srvsvc_net_share_add(
5032 pipe_hnd
, mem_ctx
, argv
[0], STYPE_DISKTREE
,
5033 (argc
== 3) ? argv
[2] : "",
5034 0, 0, 0, argv
[1], NULL
, 2, NULL
);
5036 return werror_to_ntstatus(result
);
5039 static NTSTATUS
rpc_sh_share_delete(TALLOC_CTX
*mem_ctx
,
5040 struct rpc_sh_ctx
*ctx
,
5041 struct rpc_pipe_client
*pipe_hnd
,
5042 int argc
, const char **argv
)
5047 d_fprintf(stderr
, "usage: %s <share>\n", ctx
->whoami
);
5048 return NT_STATUS_INVALID_PARAMETER
;
5051 result
= rpccli_srvsvc_net_share_del(pipe_hnd
, mem_ctx
, argv
[0]);
5052 return werror_to_ntstatus(result
);
5055 static NTSTATUS
rpc_sh_share_info(TALLOC_CTX
*mem_ctx
,
5056 struct rpc_sh_ctx
*ctx
,
5057 struct rpc_pipe_client
*pipe_hnd
,
5058 int argc
, const char **argv
)
5060 SRV_SHARE_INFO info
;
5061 SRV_SHARE_INFO_2
*info2
= &info
.share
.info2
;
5065 d_fprintf(stderr
, "usage: %s <share>\n", ctx
->whoami
);
5066 return NT_STATUS_INVALID_PARAMETER
;
5069 result
= rpccli_srvsvc_net_share_get_info(
5070 pipe_hnd
, mem_ctx
, argv
[0], 2, &info
);
5071 if (!W_ERROR_IS_OK(result
)) {
5075 d_printf("Name: %s\n",
5076 rpcstr_pull_unistr2_talloc(mem_ctx
,
5077 &info2
->info_2_str
.uni_netname
));
5078 d_printf("Comment: %s\n",
5079 rpcstr_pull_unistr2_talloc(mem_ctx
,
5080 &info2
->info_2_str
.uni_remark
));
5082 d_printf("Path: %s\n",
5083 rpcstr_pull_unistr2_talloc(mem_ctx
,
5084 &info2
->info_2_str
.uni_path
));
5085 d_printf("Password: %s\n",
5086 rpcstr_pull_unistr2_talloc(mem_ctx
,
5087 &info2
->info_2_str
.uni_passwd
));
5090 return werror_to_ntstatus(result
);
5093 struct rpc_sh_cmd
*net_rpc_share_cmds(TALLOC_CTX
*mem_ctx
,
5094 struct rpc_sh_ctx
*ctx
)
5096 static struct rpc_sh_cmd cmds
[] = {
5098 { "list", NULL
, PI_SRVSVC
, rpc_sh_share_list
,
5099 "List available shares" },
5101 { "add", NULL
, PI_SRVSVC
, rpc_sh_share_add
,
5104 { "delete", NULL
, PI_SRVSVC
, rpc_sh_share_delete
,
5107 { "info", NULL
, PI_SRVSVC
, rpc_sh_share_info
,
5108 "Get information about a share" },
5110 { NULL
, NULL
, 0, NULL
, NULL
}
5116 /****************************************************************************/
5118 static int rpc_file_usage(int argc
, const char **argv
)
5120 return net_help_file(argc
, argv
);
5124 * Close a file on a remote RPC server
5126 * All parameters are provided by the run_rpc_command function, except for
5127 * argc, argv which are passes through.
5129 * @param domain_sid The domain sid acquired from the remote server
5130 * @param cli A cli_state connected to the server.
5131 * @param mem_ctx Talloc context, destoyed on completion of the function.
5132 * @param argc Standard main() style argc
5133 * @param argv Standard main() style argv. Initial components are already
5136 * @return Normal NTSTATUS return.
5138 static NTSTATUS
rpc_file_close_internals(const DOM_SID
*domain_sid
,
5139 const char *domain_name
,
5140 struct cli_state
*cli
,
5141 struct rpc_pipe_client
*pipe_hnd
,
5142 TALLOC_CTX
*mem_ctx
,
5146 return rpccli_srvsvc_NetFileClose(pipe_hnd
, mem_ctx
,
5147 pipe_hnd
->cli
->desthost
,
5148 atoi(argv
[0]), NULL
);
5152 * Close a file on a remote RPC server
5154 * @param argc Standard main() style argc
5155 * @param argv Standard main() style argv. Initial components are already
5158 * @return A shell status integer (0 for success)
5160 static int rpc_file_close(int argc
, const char **argv
)
5163 DEBUG(1, ("No fileid given on close\n"));
5164 return(rpc_file_usage(argc
, argv
));
5167 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
5168 rpc_file_close_internals
,
5173 * Formatted print of open file info
5175 * @param info3 FILE_INFO_3 contents
5176 * @param str3 strings for FILE_INFO_3
5179 static void display_file_info_3( FILE_INFO_3
*info3
)
5181 fstring user
= "", path
= "";
5183 rpcstr_pull_unistr2_fstring(user
, info3
->user
);
5184 rpcstr_pull_unistr2_fstring(path
, info3
->path
);
5186 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5187 info3
->id
, user
, info3
->perms
, info3
->num_locks
, path
);
5191 * List open files on a remote RPC server
5193 * All parameters are provided by the run_rpc_command function, except for
5194 * argc, argv which are passes through.
5196 * @param domain_sid The domain sid acquired from the remote server
5197 * @param cli A cli_state connected to the server.
5198 * @param mem_ctx Talloc context, destoyed on completion of the function.
5199 * @param argc Standard main() style argc
5200 * @param argv Standard main() style argv. Initial components are already
5203 * @return Normal NTSTATUS return.
5206 static NTSTATUS
rpc_file_list_internals(const DOM_SID
*domain_sid
,
5207 const char *domain_name
,
5208 struct cli_state
*cli
,
5209 struct rpc_pipe_client
*pipe_hnd
,
5210 TALLOC_CTX
*mem_ctx
,
5214 SRV_FILE_INFO_CTR ctr
;
5217 uint32 preferred_len
= 0xffffffff, i
;
5218 const char *username
=NULL
;
5220 init_enum_hnd(&hnd
, 0);
5222 /* if argc > 0, must be user command */
5224 username
= smb_xstrdup(argv
[0]);
5226 result
= rpccli_srvsvc_net_file_enum(pipe_hnd
,
5227 mem_ctx
, 3, username
, &ctr
, preferred_len
, &hnd
);
5229 if (!W_ERROR_IS_OK(result
))
5232 /* Display results */
5235 "\nEnumerating open files on remote server:\n\n"\
5236 "\nFileId Opened by Perms Locks Path"\
5237 "\n------ --------- ----- ----- ---- \n");
5238 for (i
= 0; i
< ctr
.num_entries
; i
++)
5239 display_file_info_3(&ctr
.file
.info3
[i
]);
5241 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
5245 * List files for a user on a remote RPC server
5247 * @param argc Standard main() style argc
5248 * @param argv Standard main() style argv. Initial components are already
5251 * @return A shell status integer (0 for success)
5254 static int rpc_file_user(int argc
, const char **argv
)
5257 DEBUG(1, ("No username given\n"));
5258 return(rpc_file_usage(argc
, argv
));
5261 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
5262 rpc_file_list_internals
,
5267 * 'net rpc file' entrypoint.
5268 * @param argc Standard main() style argc
5269 * @param argv Standard main() style argv. Initial components are already
5273 int net_rpc_file(int argc
, const char **argv
)
5275 struct functable func
[] = {
5276 {"close", rpc_file_close
},
5277 {"user", rpc_file_user
},
5279 {"info", rpc_file_info
},
5285 return run_rpc_command(NULL
, PI_SRVSVC
, 0,
5286 rpc_file_list_internals
,
5289 return net_run_function(argc
, argv
, func
, rpc_file_usage
);
5293 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
5295 * All parameters are provided by the run_rpc_command function, except for
5296 * argc, argv which are passed through.
5298 * @param domain_sid The domain sid aquired from the remote server
5299 * @param cli A cli_state connected to the server.
5300 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5301 * @param argc Standard main() style argc
5302 * @param argv Standard main() style argv. Initial components are already
5305 * @return Normal NTSTATUS return.
5308 static NTSTATUS
rpc_shutdown_abort_internals(const DOM_SID
*domain_sid
,
5309 const char *domain_name
,
5310 struct cli_state
*cli
,
5311 struct rpc_pipe_client
*pipe_hnd
,
5312 TALLOC_CTX
*mem_ctx
,
5316 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5318 result
= rpccli_initshutdown_Abort(pipe_hnd
, mem_ctx
, NULL
, NULL
);
5320 if (NT_STATUS_IS_OK(result
)) {
5321 d_printf("\nShutdown successfully aborted\n");
5322 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5324 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5330 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5332 * All parameters are provided by the run_rpc_command function, except for
5333 * argc, argv which are passed through.
5335 * @param domain_sid The domain sid aquired from the remote server
5336 * @param cli A cli_state connected to the server.
5337 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5338 * @param argc Standard main() style argc
5339 * @param argv Standard main() style argv. Initial components are already
5342 * @return Normal NTSTATUS return.
5345 static NTSTATUS
rpc_reg_shutdown_abort_internals(const DOM_SID
*domain_sid
,
5346 const char *domain_name
,
5347 struct cli_state
*cli
,
5348 struct rpc_pipe_client
*pipe_hnd
,
5349 TALLOC_CTX
*mem_ctx
,
5353 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5355 result
= rpccli_winreg_AbortSystemShutdown(pipe_hnd
, mem_ctx
, NULL
, NULL
);
5357 if (NT_STATUS_IS_OK(result
)) {
5358 d_printf("\nShutdown successfully aborted\n");
5359 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5361 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5367 * ABORT the Shut down of a remote RPC server
5369 * @param argc Standard main() style argc
5370 * @param argv Standard main() style argv. Initial components are already
5373 * @return A shell status integer (0 for success)
5376 static int rpc_shutdown_abort(int argc
, const char **argv
)
5378 int rc
= run_rpc_command(NULL
, PI_INITSHUTDOWN
, 0,
5379 rpc_shutdown_abort_internals
,
5385 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5387 return run_rpc_command(NULL
, PI_WINREG
, 0,
5388 rpc_reg_shutdown_abort_internals
,
5393 * Shut down a remote RPC Server via initshutdown pipe
5395 * All parameters are provided by the run_rpc_command function, except for
5396 * argc, argv which are passes through.
5398 * @param domain_sid The domain sid aquired from the remote server
5399 * @param cli A cli_state connected to the server.
5400 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5401 * @param argc Standard main() style argc
5402 * @param argc Standard main() style argv. Initial components are already
5405 * @return Normal NTSTATUS return.
5408 NTSTATUS
rpc_init_shutdown_internals(const DOM_SID
*domain_sid
,
5409 const char *domain_name
,
5410 struct cli_state
*cli
,
5411 struct rpc_pipe_client
*pipe_hnd
,
5412 TALLOC_CTX
*mem_ctx
,
5416 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5417 const char *msg
= "This machine will be shutdown shortly";
5418 uint32 timeout
= 20;
5419 struct initshutdown_String msg_string
;
5420 struct initshutdown_String_sub s
;
5426 timeout
= opt_timeout
;
5430 msg_string
.name
= &s
;
5432 /* create an entry */
5433 result
= rpccli_initshutdown_Init(pipe_hnd
, mem_ctx
, NULL
,
5434 &msg_string
, timeout
, opt_force
, opt_reboot
, NULL
);
5436 if (NT_STATUS_IS_OK(result
)) {
5437 d_printf("\nShutdown of remote machine succeeded\n");
5438 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5440 DEBUG(1,("Shutdown of remote machine failed!\n"));
5446 * Shut down a remote RPC Server via winreg pipe
5448 * All parameters are provided by the run_rpc_command function, except for
5449 * argc, argv which are passes through.
5451 * @param domain_sid The domain sid aquired from the remote server
5452 * @param cli A cli_state connected to the server.
5453 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5454 * @param argc Standard main() style argc
5455 * @param argc Standard main() style argv. Initial components are already
5458 * @return Normal NTSTATUS return.
5461 NTSTATUS
rpc_reg_shutdown_internals(const DOM_SID
*domain_sid
,
5462 const char *domain_name
,
5463 struct cli_state
*cli
,
5464 struct rpc_pipe_client
*pipe_hnd
,
5465 TALLOC_CTX
*mem_ctx
,
5469 const char *msg
= "This machine will be shutdown shortly";
5470 uint32 timeout
= 20;
5471 struct initshutdown_String msg_string
;
5472 struct initshutdown_String_sub s
;
5480 msg_string
.name
= &s
;
5483 timeout
= opt_timeout
;
5486 /* create an entry */
5487 result
= rpccli_winreg_InitiateSystemShutdown(pipe_hnd
, mem_ctx
, NULL
,
5488 &msg_string
, timeout
, opt_force
, opt_reboot
, &werr
);
5490 if (NT_STATUS_IS_OK(result
)) {
5491 d_printf("\nShutdown of remote machine succeeded\n");
5493 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5494 if ( W_ERROR_EQUAL(werr
, WERR_MACHINE_LOCKED
) )
5495 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5497 d_fprintf(stderr
, "\nresult was: %s\n", dos_errstr(werr
));
5504 * Shut down a remote RPC server
5506 * @param argc Standard main() style argc
5507 * @param argc Standard main() style argv. Initial components are already
5510 * @return A shell status integer (0 for success)
5513 static int rpc_shutdown(int argc
, const char **argv
)
5515 int rc
= run_rpc_command(NULL
, PI_INITSHUTDOWN
, 0,
5516 rpc_init_shutdown_internals
,
5520 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5521 rc
= run_rpc_command(NULL
, PI_WINREG
, 0,
5522 rpc_reg_shutdown_internals
, argc
, argv
);
5528 /***************************************************************************
5529 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5531 ***************************************************************************/
5534 * Add interdomain trust account to the RPC server.
5535 * All parameters (except for argc and argv) are passed by run_rpc_command
5538 * @param domain_sid The domain sid acquired from the server
5539 * @param cli A cli_state connected to the server.
5540 * @param mem_ctx Talloc context, destoyed on completion of the function.
5541 * @param argc Standard main() style argc
5542 * @param argc Standard main() style argv. Initial components are already
5545 * @return normal NTSTATUS return code
5548 static NTSTATUS
rpc_trustdom_add_internals(const DOM_SID
*domain_sid
,
5549 const char *domain_name
,
5550 struct cli_state
*cli
,
5551 struct rpc_pipe_client
*pipe_hnd
,
5552 TALLOC_CTX
*mem_ctx
,
5556 POLICY_HND connect_pol
, domain_pol
, user_pol
;
5557 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5559 struct lsa_String lsa_acct_name
;
5561 uint32 acct_flags
=0;
5563 uint32_t access_granted
= 0;
5566 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5567 return NT_STATUS_INVALID_PARAMETER
;
5571 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5574 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5575 return NT_STATUS_NO_MEMORY
;
5578 strupper_m(acct_name
);
5580 init_lsa_String(&lsa_acct_name
, acct_name
);
5582 /* Get samr policy handle */
5583 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
5584 pipe_hnd
->cli
->desthost
,
5585 MAXIMUM_ALLOWED_ACCESS
,
5587 if (!NT_STATUS_IS_OK(result
)) {
5591 /* Get domain policy handle */
5592 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
5594 MAXIMUM_ALLOWED_ACCESS
,
5595 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5597 if (!NT_STATUS_IS_OK(result
)) {
5601 /* Create trusting domain's account */
5602 acb_info
= ACB_NORMAL
;
5603 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
5604 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
5605 SAMR_USER_ACCESS_SET_PASSWORD
|
5606 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
5607 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
5609 result
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
5617 if (!NT_STATUS_IS_OK(result
)) {
5622 SAM_USERINFO_CTR ctr
;
5623 SAM_USER_INFO_23 p23
;
5629 encode_pw_buffer(pwbuf
, argv
[1], STR_UNICODE
);
5633 ZERO_STRUCT(notime
);
5637 memset(hrs
.hours
, 0xFF, sizeof(hrs
.hours
));
5638 acb_info
= ACB_DOMTRUST
;
5640 init_sam_user_info23A(&p23
, ¬ime
, ¬ime
, ¬ime
,
5641 ¬ime
, ¬ime
, ¬ime
,
5642 nostr
, nostr
, nostr
, nostr
, nostr
,
5643 nostr
, nostr
, nostr
, nostr
, nostr
,
5644 0, 0, acb_info
, ACCT_FLAGS
, 168, &hrs
,
5645 0, 0, (char *)pwbuf
);
5646 ctr
.switch_value
= 23;
5647 ctr
.info
.id23
= &p23
;
5648 p23
.passmustchange
= 0;
5650 result
= rpccli_samr_set_userinfo(pipe_hnd
, mem_ctx
, &user_pol
, 23,
5651 &cli
->user_session_key
, &ctr
);
5653 if (!NT_STATUS_IS_OK(result
)) {
5654 DEBUG(0,("Could not set trust account password: %s\n",
5655 nt_errstr(result
)));
5661 SAFE_FREE(acct_name
);
5666 * Create interdomain trust account for a remote domain.
5668 * @param argc standard argc
5669 * @param argv standard argv without initial components
5671 * @return Integer status (0 means success)
5674 static int rpc_trustdom_add(int argc
, const char **argv
)
5677 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_trustdom_add_internals
,
5680 d_printf("Usage: net rpc trustdom add <domain>\n");
5687 * Remove interdomain trust account from the RPC server.
5688 * All parameters (except for argc and argv) are passed by run_rpc_command
5691 * @param domain_sid The domain sid acquired from the server
5692 * @param cli A cli_state connected to the server.
5693 * @param mem_ctx Talloc context, destoyed on completion of the function.
5694 * @param argc Standard main() style argc
5695 * @param argc Standard main() style argv. Initial components are already
5698 * @return normal NTSTATUS return code
5701 static NTSTATUS
rpc_trustdom_del_internals(const DOM_SID
*domain_sid
,
5702 const char *domain_name
,
5703 struct cli_state
*cli
,
5704 struct rpc_pipe_client
*pipe_hnd
,
5705 TALLOC_CTX
*mem_ctx
,
5709 POLICY_HND connect_pol
, domain_pol
, user_pol
;
5710 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5713 DOM_SID trust_acct_sid
;
5714 uint32
*user_rids
, num_rids
, *name_types
;
5715 uint32 flags
= 0x000003e8; /* Unknown */
5718 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5719 return NT_STATUS_INVALID_PARAMETER
;
5723 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5725 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
5727 if (acct_name
== NULL
)
5728 return NT_STATUS_NO_MEMORY
;
5730 strupper_m(acct_name
);
5732 if ((names
= TALLOC_ARRAY(mem_ctx
, const char *, 1)) == NULL
) {
5733 return NT_STATUS_NO_MEMORY
;
5735 names
[0] = acct_name
;
5738 /* Get samr policy handle */
5739 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
5740 pipe_hnd
->cli
->desthost
,
5741 MAXIMUM_ALLOWED_ACCESS
,
5743 if (!NT_STATUS_IS_OK(result
)) {
5747 /* Get domain policy handle */
5748 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
5750 MAXIMUM_ALLOWED_ACCESS
,
5751 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5753 if (!NT_STATUS_IS_OK(result
)) {
5757 result
= rpccli_samr_lookup_names(pipe_hnd
, mem_ctx
, &domain_pol
, flags
, 1,
5759 &user_rids
, &name_types
);
5761 if (!NT_STATUS_IS_OK(result
)) {
5765 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
5767 MAXIMUM_ALLOWED_ACCESS
,
5771 if (!NT_STATUS_IS_OK(result
)) {
5775 /* append the rid to the domain sid */
5776 sid_copy(&trust_acct_sid
, domain_sid
);
5777 if (!sid_append_rid(&trust_acct_sid
, user_rids
[0])) {
5781 /* remove the sid */
5783 result
= rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd
, mem_ctx
,
5786 if (!NT_STATUS_IS_OK(result
)) {
5792 result
= rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
5795 if (!NT_STATUS_IS_OK(result
)) {
5799 if (!NT_STATUS_IS_OK(result
)) {
5800 DEBUG(0,("Could not set trust account password: %s\n",
5801 nt_errstr(result
)));
5810 * Delete interdomain trust account for a remote domain.
5812 * @param argc standard argc
5813 * @param argv standard argv without initial components
5815 * @return Integer status (0 means success)
5818 static int rpc_trustdom_del(int argc
, const char **argv
)
5821 return run_rpc_command(NULL
, PI_SAMR
, 0, rpc_trustdom_del_internals
,
5824 d_printf("Usage: net rpc trustdom del <domain>\n");
5831 * Establish trust relationship to a trusting domain.
5832 * Interdomain account must already be created on remote PDC.
5834 * @param argc standard argc
5835 * @param argv standard argv without initial components
5837 * @return Integer status (0 means success)
5840 static int rpc_trustdom_establish(int argc
, const char **argv
)
5842 struct cli_state
*cli
= NULL
;
5843 struct sockaddr_storage server_ss
;
5844 struct rpc_pipe_client
*pipe_hnd
= NULL
;
5845 POLICY_HND connect_hnd
;
5846 TALLOC_CTX
*mem_ctx
;
5848 DOM_SID
*domain_sid
;
5854 union lsa_PolicyInformation
*info
= NULL
;
5857 * Connect to \\server\ipc$ as 'our domain' account with password
5861 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5865 domain_name
= smb_xstrdup(argv
[0]);
5866 strupper_m(domain_name
);
5868 /* account name used at first is our domain's name with '$' */
5869 asprintf(&acct_name
, "%s$", lp_workgroup());
5870 strupper_m(acct_name
);
5873 * opt_workgroup will be used by connection functions further,
5874 * hence it should be set to remote domain name instead of ours
5876 if (opt_workgroup
) {
5877 opt_workgroup
= smb_xstrdup(domain_name
);
5880 opt_user_name
= acct_name
;
5882 /* find the domain controller */
5883 if (!net_find_pdc(&server_ss
, pdc_name
, domain_name
)) {
5884 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
5888 /* connect to ipc$ as username/password */
5889 nt_status
= connect_to_ipc(&cli
, &server_ss
, pdc_name
);
5890 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
5892 /* Is it trusting domain account for sure ? */
5893 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5894 nt_errstr(nt_status
)));
5898 /* store who we connected to */
5900 saf_store( domain_name
, pdc_name
);
5903 * Connect to \\server\ipc$ again (this time anonymously)
5906 nt_status
= connect_to_ipc_anonymous(&cli
, &server_ss
, (char*)pdc_name
);
5908 if (NT_STATUS_IS_ERR(nt_status
)) {
5909 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5910 domain_name
, nt_errstr(nt_status
)));
5915 * Use NetServerEnum2 to make sure we're talking to a proper server
5918 if (!cli_get_pdc_name(cli
, domain_name
, &dc_name
)) {
5919 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
5920 for domain %s\n", domain_name
));
5926 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
5927 "domain %s", domain_name
))) {
5928 DEBUG(0, ("talloc_init() failed\n"));
5934 * Call LsaOpenPolicy and LsaQueryInfo
5937 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &nt_status
);
5939 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
5941 talloc_destroy(mem_ctx
);
5945 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, True
, SEC_RIGHTS_QUERY_VALUE
,
5947 if (NT_STATUS_IS_ERR(nt_status
)) {
5948 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5949 nt_errstr(nt_status
)));
5951 talloc_destroy(mem_ctx
);
5955 /* Querying info level 5 */
5957 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
5959 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
5961 if (NT_STATUS_IS_ERR(nt_status
)) {
5962 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5963 nt_errstr(nt_status
)));
5965 talloc_destroy(mem_ctx
);
5969 domain_sid
= info
->account_domain
.sid
;
5971 /* There should be actually query info level 3 (following nt serv behaviour),
5972 but I still don't know if it's _really_ necessary */
5975 * Store the password in secrets db
5978 if (!pdb_set_trusteddom_pw(domain_name
, opt_password
, domain_sid
)) {
5979 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5981 talloc_destroy(mem_ctx
);
5986 * Close the pipes and clean up
5989 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
5990 if (NT_STATUS_IS_ERR(nt_status
)) {
5991 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5992 nt_errstr(nt_status
)));
5994 talloc_destroy(mem_ctx
);
6000 talloc_destroy(mem_ctx
);
6002 d_printf("Trust to domain %s established\n", domain_name
);
6007 * Revoke trust relationship to the remote domain
6009 * @param argc standard argc
6010 * @param argv standard argv without initial components
6012 * @return Integer status (0 means success)
6015 static int rpc_trustdom_revoke(int argc
, const char **argv
)
6020 if (argc
< 1) return -1;
6022 /* generate upper cased domain name */
6023 domain_name
= smb_xstrdup(argv
[0]);
6024 strupper_m(domain_name
);
6026 /* delete password of the trust */
6027 if (!pdb_del_trusteddom_pw(domain_name
)) {
6028 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6035 SAFE_FREE(domain_name
);
6040 * Usage for 'net rpc trustdom' command
6042 * @param argc standard argc
6043 * @param argv standard argv without inital components
6045 * @return Integer status returned to shell
6048 static int rpc_trustdom_usage(int argc
, const char **argv
)
6050 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
6051 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
6052 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
6053 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
6054 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
6055 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
6060 static NTSTATUS
rpc_query_domain_sid(const DOM_SID
*domain_sid
,
6061 const char *domain_name
,
6062 struct cli_state
*cli
,
6063 struct rpc_pipe_client
*pipe_hnd
,
6064 TALLOC_CTX
*mem_ctx
,
6069 sid_to_fstring(str_sid
, domain_sid
);
6070 d_printf("%s\n", str_sid
);
6071 return NT_STATUS_OK
;
6074 static void print_trusted_domain(DOM_SID
*dom_sid
, const char *trusted_dom_name
)
6076 fstring ascii_sid
, padding
;
6077 int pad_len
, col_len
= 20;
6079 /* convert sid into ascii string */
6080 sid_to_fstring(ascii_sid
, dom_sid
);
6082 /* calculate padding space for d_printf to look nicer */
6083 pad_len
= col_len
- strlen(trusted_dom_name
);
6084 padding
[pad_len
] = 0;
6085 do padding
[--pad_len
] = ' '; while (pad_len
);
6087 d_printf("%s%s%s\n", trusted_dom_name
, padding
, ascii_sid
);
6090 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
6091 TALLOC_CTX
*mem_ctx
,
6094 const char *trusted_dom_name
)
6097 union lsa_TrustedDomainInfo info
;
6098 char *cleartextpwd
= NULL
;
6101 nt_status
= rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd
, mem_ctx
,
6104 LSA_TRUSTED_DOMAIN_INFO_PASSWORD
,
6106 if (NT_STATUS_IS_ERR(nt_status
)) {
6107 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6108 nt_errstr(nt_status
)));
6112 data
= data_blob(NULL
, info
.password
.password
->length
);
6115 info
.password
.password
->data
,
6116 info
.password
.password
->length
);
6117 data
.length
= info
.password
.password
->length
;
6119 cleartextpwd
= decrypt_trustdom_secret(pipe_hnd
->cli
->pwd
.password
,
6122 if (cleartextpwd
== NULL
) {
6123 DEBUG(0,("retrieved NULL password\n"));
6124 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6128 if (!pdb_set_trusteddom_pw(trusted_dom_name
, cleartextpwd
, &dom_sid
)) {
6129 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6130 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6134 #ifdef DEBUG_PASSWORD
6135 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], "
6136 "password: [%s]\n", trusted_dom_name
,
6137 sid_string_dbg(&dom_sid
), cleartextpwd
));
6141 SAFE_FREE(cleartextpwd
);
6142 data_blob_free(&data
);
6147 static int rpc_trustdom_vampire(int argc
, const char **argv
)
6149 /* common variables */
6150 TALLOC_CTX
* mem_ctx
;
6151 struct cli_state
*cli
= NULL
;
6152 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6154 const char *domain_name
= NULL
;
6155 DOM_SID
*queried_dom_sid
;
6156 POLICY_HND connect_hnd
;
6157 union lsa_PolicyInformation
*info
= NULL
;
6159 /* trusted domains listing variables */
6160 unsigned int num_domains
, enum_ctx
= 0;
6162 DOM_SID
*domain_sids
;
6163 char **trusted_dom_names
;
6167 * Listing trusted domains (stored in secrets.tdb, if local)
6170 mem_ctx
= talloc_init("trust relationships vampire");
6173 * set domain and pdc name to local samba server (default)
6174 * or to remote one given in command line
6177 if (StrCaseCmp(opt_workgroup
, lp_workgroup())) {
6178 domain_name
= opt_workgroup
;
6179 opt_target_workgroup
= opt_workgroup
;
6181 fstrcpy(pdc_name
, global_myname());
6182 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6183 opt_target_workgroup
= domain_name
;
6186 /* open \PIPE\lsarpc and open policy handle */
6187 nt_status
= net_make_ipc_connection(NET_FLAGS_PDC
, &cli
);
6188 if (!NT_STATUS_IS_OK(nt_status
)) {
6189 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6190 nt_errstr(nt_status
)));
6191 talloc_destroy(mem_ctx
);
6195 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &nt_status
);
6197 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6198 nt_errstr(nt_status
) ));
6200 talloc_destroy(mem_ctx
);
6204 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, False
, SEC_RIGHTS_QUERY_VALUE
,
6206 if (NT_STATUS_IS_ERR(nt_status
)) {
6207 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6208 nt_errstr(nt_status
)));
6210 talloc_destroy(mem_ctx
);
6214 /* query info level 5 to obtain sid of a domain being queried */
6215 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
6217 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6220 if (NT_STATUS_IS_ERR(nt_status
)) {
6221 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6222 nt_errstr(nt_status
)));
6224 talloc_destroy(mem_ctx
);
6228 queried_dom_sid
= info
->account_domain
.sid
;
6231 * Keep calling LsaEnumTrustdom over opened pipe until
6232 * the end of enumeration is reached
6235 d_printf("Vampire trusted domains:\n\n");
6238 nt_status
= rpccli_lsa_enum_trust_dom(pipe_hnd
, mem_ctx
, &connect_hnd
, &enum_ctx
,
6240 &trusted_dom_names
, &domain_sids
);
6242 if (NT_STATUS_IS_ERR(nt_status
)) {
6243 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6244 nt_errstr(nt_status
)));
6246 talloc_destroy(mem_ctx
);
6250 for (i
= 0; i
< num_domains
; i
++) {
6252 print_trusted_domain(&(domain_sids
[i
]), trusted_dom_names
[i
]);
6254 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
6255 domain_sids
[i
], trusted_dom_names
[i
]);
6256 if (!NT_STATUS_IS_OK(nt_status
)) {
6258 talloc_destroy(mem_ctx
);
6264 * in case of no trusted domains say something rather
6265 * than just display blank line
6267 if (!num_domains
) d_printf("none\n");
6269 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6271 /* close this connection before doing next one */
6272 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6273 if (NT_STATUS_IS_ERR(nt_status
)) {
6274 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6275 nt_errstr(nt_status
)));
6277 talloc_destroy(mem_ctx
);
6281 /* close lsarpc pipe and connection to IPC$ */
6284 talloc_destroy(mem_ctx
);
6288 static int rpc_trustdom_list(int argc
, const char **argv
)
6290 /* common variables */
6291 TALLOC_CTX
* mem_ctx
;
6292 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
6293 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6295 const char *domain_name
= NULL
;
6296 DOM_SID
*queried_dom_sid
;
6298 int ascii_dom_name_len
;
6299 POLICY_HND connect_hnd
;
6300 union lsa_PolicyInformation
*info
= NULL
;
6302 /* trusted domains listing variables */
6303 unsigned int num_domains
, enum_ctx
= 0;
6304 int i
, pad_len
, col_len
= 20;
6305 DOM_SID
*domain_sids
;
6306 char **trusted_dom_names
;
6309 /* trusting domains listing variables */
6310 POLICY_HND domain_hnd
;
6311 char **trusting_dom_names
;
6312 uint32
*trusting_dom_rids
;
6315 * Listing trusted domains (stored in secrets.tdb, if local)
6318 mem_ctx
= talloc_init("trust relationships listing");
6321 * set domain and pdc name to local samba server (default)
6322 * or to remote one given in command line
6325 if (StrCaseCmp(opt_workgroup
, lp_workgroup())) {
6326 domain_name
= opt_workgroup
;
6327 opt_target_workgroup
= opt_workgroup
;
6329 fstrcpy(pdc_name
, global_myname());
6330 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6331 opt_target_workgroup
= domain_name
;
6334 /* open \PIPE\lsarpc and open policy handle */
6335 nt_status
= net_make_ipc_connection(NET_FLAGS_PDC
, &cli
);
6336 if (!NT_STATUS_IS_OK(nt_status
)) {
6337 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6338 nt_errstr(nt_status
)));
6339 talloc_destroy(mem_ctx
);
6343 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &nt_status
);
6345 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6346 nt_errstr(nt_status
) ));
6348 talloc_destroy(mem_ctx
);
6352 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, False
, SEC_RIGHTS_QUERY_VALUE
,
6354 if (NT_STATUS_IS_ERR(nt_status
)) {
6355 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6356 nt_errstr(nt_status
)));
6358 talloc_destroy(mem_ctx
);
6362 /* query info level 5 to obtain sid of a domain being queried */
6363 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
6365 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6368 if (NT_STATUS_IS_ERR(nt_status
)) {
6369 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6370 nt_errstr(nt_status
)));
6372 talloc_destroy(mem_ctx
);
6376 queried_dom_sid
= info
->account_domain
.sid
;
6379 * Keep calling LsaEnumTrustdom over opened pipe until
6380 * the end of enumeration is reached
6383 d_printf("Trusted domains list:\n\n");
6386 nt_status
= rpccli_lsa_enum_trust_dom(pipe_hnd
, mem_ctx
, &connect_hnd
, &enum_ctx
,
6388 &trusted_dom_names
, &domain_sids
);
6390 if (NT_STATUS_IS_ERR(nt_status
)) {
6391 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6392 nt_errstr(nt_status
)));
6394 talloc_destroy(mem_ctx
);
6398 for (i
= 0; i
< num_domains
; i
++) {
6399 print_trusted_domain(&(domain_sids
[i
]), trusted_dom_names
[i
]);
6403 * in case of no trusted domains say something rather
6404 * than just display blank line
6406 if (!num_domains
) d_printf("none\n");
6408 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6410 /* close this connection before doing next one */
6411 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6412 if (NT_STATUS_IS_ERR(nt_status
)) {
6413 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6414 nt_errstr(nt_status
)));
6416 talloc_destroy(mem_ctx
);
6420 cli_rpc_pipe_close(pipe_hnd
);
6423 * Listing trusting domains (stored in passdb backend, if local)
6426 d_printf("\nTrusting domains list:\n\n");
6429 * Open \PIPE\samr and get needed policy handles
6431 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SAMR
, &nt_status
);
6433 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6435 talloc_destroy(mem_ctx
);
6440 nt_status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
6441 pipe_hnd
->cli
->desthost
,
6442 SA_RIGHT_SAM_OPEN_DOMAIN
,
6444 if (!NT_STATUS_IS_OK(nt_status
)) {
6445 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6446 nt_errstr(nt_status
)));
6448 talloc_destroy(mem_ctx
);
6452 /* SamrOpenDomain - we have to open domain policy handle in order to be
6453 able to enumerate accounts*/
6454 nt_status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
6456 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS
,
6459 if (!NT_STATUS_IS_OK(nt_status
)) {
6460 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6461 nt_errstr(nt_status
)));
6463 talloc_destroy(mem_ctx
);
6468 * perform actual enumeration
6471 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6474 nt_status
= rpccli_samr_enum_dom_users(pipe_hnd
, mem_ctx
, &domain_hnd
,
6475 &enum_ctx
, ACB_DOMTRUST
, 0xffff,
6476 &trusting_dom_names
, &trusting_dom_rids
,
6478 if (NT_STATUS_IS_ERR(nt_status
)) {
6479 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6480 nt_errstr(nt_status
)));
6482 talloc_destroy(mem_ctx
);
6486 for (i
= 0; i
< num_domains
; i
++) {
6489 * get each single domain's sid (do we _really_ need this ?):
6490 * 1) connect to domain's pdc
6491 * 2) query the pdc for domain's sid
6494 /* get rid of '$' tail */
6495 ascii_dom_name_len
= strlen(trusting_dom_names
[i
]);
6496 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
6497 trusting_dom_names
[i
][ascii_dom_name_len
- 1] = '\0';
6499 /* calculate padding space for d_printf to look nicer */
6500 pad_len
= col_len
- strlen(trusting_dom_names
[i
]);
6501 padding
[pad_len
] = 0;
6502 do padding
[--pad_len
] = ' '; while (pad_len
);
6504 /* set opt_* variables to remote domain */
6505 strupper_m(trusting_dom_names
[i
]);
6506 opt_workgroup
= talloc_strdup(mem_ctx
, trusting_dom_names
[i
]);
6507 opt_target_workgroup
= opt_workgroup
;
6509 d_printf("%s%s", trusting_dom_names
[i
], padding
);
6511 /* connect to remote domain controller */
6512 nt_status
= net_make_ipc_connection(
6513 NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
,
6515 if (NT_STATUS_IS_OK(nt_status
)) {
6516 /* query for domain's sid */
6517 if (run_rpc_command(remote_cli
, PI_LSARPC
, 0, rpc_query_domain_sid
, argc
, argv
))
6518 d_fprintf(stderr
, "couldn't get domain's sid\n");
6520 cli_shutdown(remote_cli
);
6523 d_fprintf(stderr
, "domain controller is not "
6525 nt_errstr(nt_status
));
6529 if (!num_domains
) d_printf("none\n");
6531 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6533 /* close opened samr and domain policy handles */
6534 nt_status
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_hnd
);
6535 if (!NT_STATUS_IS_OK(nt_status
)) {
6536 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
6539 nt_status
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6540 if (!NT_STATUS_IS_OK(nt_status
)) {
6541 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
6544 /* close samr pipe and connection to IPC$ */
6547 talloc_destroy(mem_ctx
);
6552 * Entrypoint for 'net rpc trustdom' code
6554 * @param argc standard argc
6555 * @param argv standard argv without initial components
6557 * @return Integer status (0 means success)
6560 static int rpc_trustdom(int argc
, const char **argv
)
6562 struct functable func
[] = {
6563 {"add", rpc_trustdom_add
},
6564 {"del", rpc_trustdom_del
},
6565 {"establish", rpc_trustdom_establish
},
6566 {"revoke", rpc_trustdom_revoke
},
6567 {"help", rpc_trustdom_usage
},
6568 {"list", rpc_trustdom_list
},
6569 {"vampire", rpc_trustdom_vampire
},
6574 rpc_trustdom_usage(argc
, argv
);
6578 return (net_run_function(argc
, argv
, func
, rpc_user_usage
));
6582 * Check if a server will take rpc commands
6583 * @param flags Type of server to connect to (PDC, DMB, localhost)
6584 * if the host is not explicitly specified
6585 * @return bool (true means rpc supported)
6587 bool net_rpc_check(unsigned flags
)
6589 struct cli_state
*cli
;
6591 struct sockaddr_storage server_ss
;
6592 char *server_name
= NULL
;
6595 /* flags (i.e. server type) may depend on command */
6596 if (!net_find_server(NULL
, flags
, &server_ss
, &server_name
))
6599 if ((cli
= cli_initialise()) == NULL
) {
6603 status
= cli_connect(cli
, server_name
, &server_ss
);
6604 if (!NT_STATUS_IS_OK(status
))
6606 if (!attempt_netbios_session_request(&cli
, global_myname(),
6607 server_name
, &server_ss
))
6609 if (!cli_negprot(cli
))
6611 if (cli
->protocol
< PROTOCOL_NT1
)
6620 /* dump sam database via samsync rpc calls */
6621 static int rpc_samdump(int argc
, const char **argv
) {
6622 return run_rpc_command(NULL
, PI_NETLOGON
, NET_FLAGS_ANONYMOUS
, rpc_samdump_internals
,
6626 /* syncronise sam database via samsync rpc calls */
6627 static int rpc_vampire(int argc
, const char **argv
) {
6628 return run_rpc_command(NULL
, PI_NETLOGON
, NET_FLAGS_ANONYMOUS
, rpc_vampire_internals
,
6633 * Migrate everything from a print-server
6635 * @param argc Standard main() style argc
6636 * @param argv Standard main() style argv. Initial components are already
6639 * @return A shell status integer (0 for success)
6641 * The order is important !
6642 * To successfully add drivers the print-queues have to exist !
6643 * Applying ACLs should be the last step, because you're easily locked out
6646 static int rpc_printer_migrate_all(int argc
, const char **argv
)
6651 printf("no server to migrate\n");
6655 ret
= run_rpc_command(NULL
, PI_SPOOLSS
, 0, rpc_printer_migrate_printers_internals
, argc
, argv
);
6659 ret
= run_rpc_command(NULL
, PI_SPOOLSS
, 0, rpc_printer_migrate_drivers_internals
, argc
, argv
);
6663 ret
= run_rpc_command(NULL
, PI_SPOOLSS
, 0, rpc_printer_migrate_forms_internals
, argc
, argv
);
6667 ret
= run_rpc_command(NULL
, PI_SPOOLSS
, 0, rpc_printer_migrate_settings_internals
, argc
, argv
);
6671 return run_rpc_command(NULL
, PI_SPOOLSS
, 0, rpc_printer_migrate_security_internals
, argc
, argv
);
6676 * Migrate print-drivers from a print-server
6678 * @param argc Standard main() style argc
6679 * @param argv Standard main() style argv. Initial components are already
6682 * @return A shell status integer (0 for success)
6684 static int rpc_printer_migrate_drivers(int argc
, const char **argv
)
6687 printf("no server to migrate\n");
6691 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6692 rpc_printer_migrate_drivers_internals
,
6697 * Migrate print-forms from a print-server
6699 * @param argc Standard main() style argc
6700 * @param argv Standard main() style argv. Initial components are already
6703 * @return A shell status integer (0 for success)
6705 static int rpc_printer_migrate_forms(int argc
, const char **argv
)
6708 printf("no server to migrate\n");
6712 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6713 rpc_printer_migrate_forms_internals
,
6718 * Migrate printers from a print-server
6720 * @param argc Standard main() style argc
6721 * @param argv Standard main() style argv. Initial components are already
6724 * @return A shell status integer (0 for success)
6726 static int rpc_printer_migrate_printers(int argc
, const char **argv
)
6729 printf("no server to migrate\n");
6733 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6734 rpc_printer_migrate_printers_internals
,
6739 * Migrate printer-ACLs from a print-server
6741 * @param argc Standard main() style argc
6742 * @param argv Standard main() style argv. Initial components are already
6745 * @return A shell status integer (0 for success)
6747 static int rpc_printer_migrate_security(int argc
, const char **argv
)
6750 printf("no server to migrate\n");
6754 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6755 rpc_printer_migrate_security_internals
,
6760 * Migrate printer-settings from a print-server
6762 * @param argc Standard main() style argc
6763 * @param argv Standard main() style argv. Initial components are already
6766 * @return A shell status integer (0 for success)
6768 static int rpc_printer_migrate_settings(int argc
, const char **argv
)
6771 printf("no server to migrate\n");
6775 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6776 rpc_printer_migrate_settings_internals
,
6781 * 'net rpc printer' entrypoint.
6782 * @param argc Standard main() style argc
6783 * @param argv Standard main() style argv. Initial components are already
6787 int rpc_printer_migrate(int argc
, const char **argv
)
6790 /* ouch: when addriver and setdriver are called from within
6791 rpc_printer_migrate_drivers_internals, the printer-queue already
6794 struct functable func
[] = {
6795 {"all", rpc_printer_migrate_all
},
6796 {"drivers", rpc_printer_migrate_drivers
},
6797 {"forms", rpc_printer_migrate_forms
},
6798 {"help", rpc_printer_usage
},
6799 {"printers", rpc_printer_migrate_printers
},
6800 {"security", rpc_printer_migrate_security
},
6801 {"settings", rpc_printer_migrate_settings
},
6805 return net_run_function(argc
, argv
, func
, rpc_printer_usage
);
6810 * List printers on a remote RPC server
6812 * @param argc Standard main() style argc
6813 * @param argv Standard main() style argv. Initial components are already
6816 * @return A shell status integer (0 for success)
6818 static int rpc_printer_list(int argc
, const char **argv
)
6821 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6822 rpc_printer_list_internals
,
6827 * List printer-drivers on a remote RPC server
6829 * @param argc Standard main() style argc
6830 * @param argv Standard main() style argv. Initial components are already
6833 * @return A shell status integer (0 for success)
6835 static int rpc_printer_driver_list(int argc
, const char **argv
)
6838 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6839 rpc_printer_driver_list_internals
,
6844 * Publish printer in ADS via MSRPC
6846 * @param argc Standard main() style argc
6847 * @param argv Standard main() style argv. Initial components are already
6850 * @return A shell status integer (0 for success)
6852 static int rpc_printer_publish_publish(int argc
, const char **argv
)
6855 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6856 rpc_printer_publish_publish_internals
,
6861 * Update printer in ADS via MSRPC
6863 * @param argc Standard main() style argc
6864 * @param argv Standard main() style argv. Initial components are already
6867 * @return A shell status integer (0 for success)
6869 static int rpc_printer_publish_update(int argc
, const char **argv
)
6872 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6873 rpc_printer_publish_update_internals
,
6878 * UnPublish printer in ADS via MSRPC
6880 * @param argc Standard main() style argc
6881 * @param argv Standard main() style argv. Initial components are already
6884 * @return A shell status integer (0 for success)
6886 static int rpc_printer_publish_unpublish(int argc
, const char **argv
)
6889 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6890 rpc_printer_publish_unpublish_internals
,
6895 * List published printers via MSRPC
6897 * @param argc Standard main() style argc
6898 * @param argv Standard main() style argv. Initial components are already
6901 * @return A shell status integer (0 for success)
6903 static int rpc_printer_publish_list(int argc
, const char **argv
)
6906 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6907 rpc_printer_publish_list_internals
,
6913 * Publish printer in ADS
6915 * @param argc Standard main() style argc
6916 * @param argv Standard main() style argv. Initial components are already
6919 * @return A shell status integer (0 for success)
6921 static int rpc_printer_publish(int argc
, const char **argv
)
6924 struct functable func
[] = {
6925 {"publish", rpc_printer_publish_publish
},
6926 {"update", rpc_printer_publish_update
},
6927 {"unpublish", rpc_printer_publish_unpublish
},
6928 {"list", rpc_printer_publish_list
},
6929 {"help", rpc_printer_usage
},
6934 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6935 rpc_printer_publish_list_internals
,
6938 return net_run_function(argc
, argv
, func
, rpc_printer_usage
);
6944 * Display rpc printer help page.
6945 * @param argc Standard main() style argc
6946 * @param argv Standard main() style argv. Initial components are already
6949 int rpc_printer_usage(int argc
, const char **argv
)
6951 return net_help_printer(argc
, argv
);
6955 * 'net rpc printer' entrypoint.
6956 * @param argc Standard main() style argc
6957 * @param argv Standard main() style argv. Initial components are already
6960 int net_rpc_printer(int argc
, const char **argv
)
6962 struct functable func
[] = {
6963 {"list", rpc_printer_list
},
6964 {"migrate", rpc_printer_migrate
},
6965 {"driver", rpc_printer_driver_list
},
6966 {"publish", rpc_printer_publish
},
6971 return run_rpc_command(NULL
, PI_SPOOLSS
, 0,
6972 rpc_printer_list_internals
,
6975 return net_run_function(argc
, argv
, func
, rpc_printer_usage
);
6978 /****************************************************************************/
6982 * Basic usage function for 'net rpc'
6983 * @param argc Standard main() style argc
6984 * @param argv Standard main() style argv. Initial components are already
6988 int net_rpc_usage(int argc
, const char **argv
)
6990 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
6991 d_printf(" net rpc join \t\t\tto join a domain \n");
6992 d_printf(" net rpc oldjoin \t\tto join a domain created in server manager\n");
6993 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
6994 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
6995 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
6996 d_printf(" net rpc group \t\tto list groups\n");
6997 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
6998 d_printf(" net rpc printer \t\tto list and migrate printers\n");
6999 d_printf(" net rpc file \t\t\tto list open files\n");
7000 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
7001 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
7002 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
7003 d_printf(" net rpc samdump \t\tdisplay an NT PDC's users, groups and other data\n");
7004 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
7005 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
7006 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
7007 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
7008 d_printf(" net rpc registry\t\tto manage registry hives\n");
7009 d_printf(" net rpc service\t\tto start, stop and query services\n");
7010 d_printf(" net rpc audit\t\t\tto modify global auditing settings\n");
7011 d_printf(" net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
7013 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
7014 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
7015 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
7016 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
7017 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
7023 * Help function for 'net rpc'. Calls command specific help if requested
7024 * or displays usage of net rpc
7025 * @param argc Standard main() style argc
7026 * @param argv Standard main() style argv. Initial components are already
7030 int net_rpc_help(int argc
, const char **argv
)
7032 struct functable func
[] = {
7033 {"join", rpc_join_usage
},
7034 {"user", rpc_user_usage
},
7035 {"group", rpc_group_usage
},
7036 {"share", rpc_share_usage
},
7037 /*{"changetrustpw", rpc_changetrustpw_usage}, */
7038 {"trustdom", rpc_trustdom_usage
},
7039 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
7040 /*{"shutdown", rpc_shutdown_usage}, */
7041 {"vampire", rpc_vampire_usage
},
7046 net_rpc_usage(argc
, argv
);
7050 return (net_run_function(argc
, argv
, func
, rpc_user_usage
));
7054 * 'net rpc' entrypoint.
7055 * @param argc Standard main() style argc
7056 * @param argv Standard main() style argv. Initial components are already
7060 int net_rpc(int argc
, const char **argv
)
7062 struct functable func
[] = {
7063 {"audit", net_rpc_audit
},
7064 {"info", net_rpc_info
},
7065 {"join", net_rpc_join
},
7066 {"oldjoin", net_rpc_oldjoin
},
7067 {"testjoin", net_rpc_testjoin
},
7068 {"user", net_rpc_user
},
7069 {"password", rpc_user_password
},
7070 {"group", net_rpc_group
},
7071 {"share", net_rpc_share
},
7072 {"file", net_rpc_file
},
7073 {"printer", net_rpc_printer
},
7074 {"changetrustpw", net_rpc_changetrustpw
},
7075 {"trustdom", rpc_trustdom
},
7076 {"abortshutdown", rpc_shutdown_abort
},
7077 {"shutdown", rpc_shutdown
},
7078 {"samdump", rpc_samdump
},
7079 {"vampire", rpc_vampire
},
7080 {"getsid", net_rpc_getsid
},
7081 {"rights", net_rpc_rights
},
7082 {"service", net_rpc_service
},
7083 {"registry", net_rpc_registry
},
7084 {"shell", net_rpc_shell
},
7085 {"help", net_rpc_help
},
7088 return net_run_function(argc
, argv
, func
, net_rpc_usage
);