2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2004,2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "utils/net.h"
26 static int net_mode_share
;
27 static bool sync_files(struct copy_clistate
*cp_clistate
, const char *mask
);
32 * @brief RPC based subcommands for the 'net' utility.
34 * This file should contain much of the functionality that used to
35 * be found in rpcclient, execpt that the commands should change
36 * less often, and the fucntionality should be sane (the user is not
37 * expected to know a rid/sid before they conduct an operation etc.)
39 * @todo Perhaps eventually these should be split out into a number
40 * of files, as this could get quite big.
45 * Many of the RPC functions need the domain sid. This function gets
46 * it at the start of every run
48 * @param cli A cli_state already connected to the remote machine
50 * @return The Domain SID of the remote machine.
53 NTSTATUS
net_get_remote_domain_sid(struct cli_state
*cli
, TALLOC_CTX
*mem_ctx
,
55 const char **domain_name
)
57 struct rpc_pipe_client
*lsa_pipe
;
59 NTSTATUS result
= NT_STATUS_OK
;
60 union lsa_PolicyInformation
*info
= NULL
;
62 lsa_pipe
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &result
);
64 d_fprintf(stderr
, "Could not initialise lsa pipe\n");
68 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, false,
69 SEC_RIGHTS_MAXIMUM_ALLOWED
,
71 if (!NT_STATUS_IS_OK(result
)) {
72 d_fprintf(stderr
, "open_policy failed: %s\n",
77 result
= rpccli_lsa_QueryInfoPolicy(lsa_pipe
, mem_ctx
,
79 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
81 if (!NT_STATUS_IS_OK(result
)) {
82 d_fprintf(stderr
, "lsaquery failed: %s\n",
87 *domain_name
= info
->account_domain
.name
.string
;
88 *domain_sid
= info
->account_domain
.sid
;
90 rpccli_lsa_Close(lsa_pipe
, mem_ctx
, &pol
);
91 TALLOC_FREE(lsa_pipe
);
97 * Run a single RPC command, from start to finish.
99 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
100 * @param conn_flag a NET_FLAG_ combination. Passed to
101 * net_make_ipc_connection.
102 * @param argc Standard main() style argc
103 * @param argc Standard main() style argv. Initial components are already
105 * @return A shell status integer (0 for success)
108 int run_rpc_command(struct net_context
*c
,
109 struct cli_state
*cli_arg
,
116 struct cli_state
*cli
= NULL
;
117 struct rpc_pipe_client
*pipe_hnd
= NULL
;
121 const char *domain_name
;
123 /* make use of cli_state handed over as an argument, if possible */
125 nt_status
= net_make_ipc_connection(c
, conn_flags
, &cli
);
126 if (!NT_STATUS_IS_OK(nt_status
)) {
127 DEBUG(1, ("failed to make ipc connection: %s\n",
128 nt_errstr(nt_status
)));
141 if (!(mem_ctx
= talloc_init("run_rpc_command"))) {
142 DEBUG(0, ("talloc_init() failed\n"));
147 nt_status
= net_get_remote_domain_sid(cli
, mem_ctx
, &domain_sid
,
149 if (!NT_STATUS_IS_OK(nt_status
)) {
154 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
155 if (lp_client_schannel() && (pipe_idx
== PI_NETLOGON
)) {
156 /* Always try and create an schannel netlogon pipe. */
157 pipe_hnd
= cli_rpc_pipe_open_schannel(cli
, pipe_idx
,
158 PIPE_AUTH_LEVEL_PRIVACY
,
162 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
163 nt_errstr(nt_status
) ));
168 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, pipe_idx
, &nt_status
);
170 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
171 cli_get_pipe_name(pipe_idx
),
172 nt_errstr(nt_status
) ));
179 nt_status
= fn(c
, domain_sid
, domain_name
, cli
, pipe_hnd
, mem_ctx
, argc
, argv
);
181 if (!NT_STATUS_IS_OK(nt_status
)) {
182 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status
)));
184 DEBUG(5, ("rpc command function succedded\n"));
187 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
189 TALLOC_FREE(pipe_hnd
);
193 /* close the connection only if it was opened here */
198 talloc_destroy(mem_ctx
);
199 return (!NT_STATUS_IS_OK(nt_status
));
203 * Force a change of the trust acccount password.
205 * All parameters are provided by the run_rpc_command function, except for
206 * argc, argv which are passes through.
208 * @param domain_sid The domain sid aquired from the remote server
209 * @param cli A cli_state connected to the server.
210 * @param mem_ctx Talloc context, destoyed on compleation of the function.
211 * @param argc Standard main() style argc
212 * @param argc Standard main() style argv. Initial components are already
215 * @return Normal NTSTATUS return.
218 static NTSTATUS
rpc_changetrustpw_internals(struct net_context
*c
,
219 const DOM_SID
*domain_sid
,
220 const char *domain_name
,
221 struct cli_state
*cli
,
222 struct rpc_pipe_client
*pipe_hnd
,
228 return trust_pw_find_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
);
232 * Force a change of the trust acccount password.
234 * @param argc Standard main() style argc
235 * @param argc Standard main() style argv. Initial components are already
238 * @return A shell status integer (0 for success)
241 int net_rpc_changetrustpw(struct net_context
*c
, int argc
, const char **argv
)
243 return run_rpc_command(c
, NULL
, PI_NETLOGON
, NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
244 rpc_changetrustpw_internals
,
249 * Join a domain, the old way.
251 * This uses 'machinename' as the inital password, and changes it.
253 * The password should be created with 'server manager' or equiv first.
255 * All parameters are provided by the run_rpc_command function, except for
256 * argc, argv which are passes through.
258 * @param domain_sid The domain sid aquired from the remote server
259 * @param cli A cli_state connected to the server.
260 * @param mem_ctx Talloc context, destoyed on compleation of the function.
261 * @param argc Standard main() style argc
262 * @param argc Standard main() style argv. Initial components are already
265 * @return Normal NTSTATUS return.
268 static NTSTATUS
rpc_oldjoin_internals(struct net_context
*c
,
269 const DOM_SID
*domain_sid
,
270 const char *domain_name
,
271 struct cli_state
*cli
,
272 struct rpc_pipe_client
*pipe_hnd
,
278 fstring trust_passwd
;
279 unsigned char orig_trust_passwd_hash
[16];
281 uint32 sec_channel_type
;
283 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_NETLOGON
, &result
);
285 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
288 nt_errstr(result
) ));
293 check what type of join - if the user want's to join as
294 a BDC, the server must agree that we are a BDC.
297 sec_channel_type
= get_sec_channel_type(argv
[0]);
299 sec_channel_type
= get_sec_channel_type(NULL
);
302 fstrcpy(trust_passwd
, global_myname());
303 strlower_m(trust_passwd
);
306 * Machine names can be 15 characters, but the max length on
307 * a password is 14. --jerry
310 trust_passwd
[14] = '\0';
312 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
314 result
= trust_pw_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
,
315 orig_trust_passwd_hash
,
318 if (NT_STATUS_IS_OK(result
))
319 printf("Joined domain %s.\n", c
->opt_target_workgroup
);
322 if (!secrets_store_domain_sid(c
->opt_target_workgroup
, domain_sid
)) {
323 DEBUG(0, ("error storing domain sid for %s\n", c
->opt_target_workgroup
));
324 result
= NT_STATUS_UNSUCCESSFUL
;
331 * Join a domain, the old way.
333 * @param argc Standard main() style argc
334 * @param argc Standard main() style argv. Initial components are already
337 * @return A shell status integer (0 for success)
340 static int net_rpc_perform_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
342 return run_rpc_command(c
, NULL
, PI_NETLOGON
,
343 NET_FLAGS_NO_PIPE
| NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
344 rpc_oldjoin_internals
,
349 * Join a domain, the old way. This function exists to allow
350 * the message to be displayed when oldjoin was explicitly
351 * requested, but not when it was implied by "net rpc join"
353 * @param argc Standard main() style argc
354 * @param argc Standard main() style argv. Initial components are already
357 * @return A shell status integer (0 for success)
360 static int net_rpc_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
362 int rc
= net_rpc_perform_oldjoin(c
, argc
, argv
);
365 d_fprintf(stderr
, "Failed to join domain\n");
372 * Basic usage function for 'net rpc join'
373 * @param argc Standard main() style argc
374 * @param argc Standard main() style argv. Initial components are already
378 static int rpc_join_usage(struct net_context
*c
, int argc
, const char **argv
)
380 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
381 "\t to join a domain with admin username & password\n"\
382 "\t\t password will be prompted if needed and none is specified\n"\
383 "\t <type> can be (default MEMBER)\n"\
384 "\t\t BDC - Join as a BDC\n"\
385 "\t\t PDC - Join as a PDC\n"\
386 "\t\t MEMBER - Join as a MEMBER server\n");
388 net_common_flags_usage(c
, argc
, argv
);
393 * 'net rpc join' entrypoint.
394 * @param argc Standard main() style argc
395 * @param argc Standard main() style argv. Initial components are already
398 * Main 'net_rpc_join()' (where the admin username/password is used) is
400 * Try to just change the password, but if that doesn't work, use/prompt
401 * for a username/password.
404 int net_rpc_join(struct net_context
*c
, int argc
, const char **argv
)
406 if (lp_server_role() == ROLE_STANDALONE
) {
407 d_printf("cannot join as standalone machine\n");
411 if (strlen(global_myname()) > 15) {
412 d_printf("Our netbios name can be at most 15 chars long, "
413 "\"%s\" is %u chars long\n",
414 global_myname(), (unsigned int)strlen(global_myname()));
418 if ((net_rpc_perform_oldjoin(c
, argc
, argv
) == 0))
421 return net_rpc_join_newstyle(c
, argc
, argv
);
425 * display info about a rpc domain
427 * All parameters are provided by the run_rpc_command function, except for
428 * argc, argv which are passed through.
430 * @param domain_sid The domain sid acquired from the remote server
431 * @param cli A cli_state connected to the server.
432 * @param mem_ctx Talloc context, destoyed on completion of the function.
433 * @param argc Standard main() style argc
434 * @param argv Standard main() style argv. Initial components are already
437 * @return Normal NTSTATUS return.
440 NTSTATUS
rpc_info_internals(struct net_context
*c
,
441 const DOM_SID
*domain_sid
,
442 const char *domain_name
,
443 struct cli_state
*cli
,
444 struct rpc_pipe_client
*pipe_hnd
,
449 POLICY_HND connect_pol
, domain_pol
;
450 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
451 union samr_DomainInfo
*info
= NULL
;
454 sid_to_fstring(sid_str
, domain_sid
);
456 /* Get sam policy handle */
457 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
459 MAXIMUM_ALLOWED_ACCESS
,
461 if (!NT_STATUS_IS_OK(result
)) {
462 d_fprintf(stderr
, "Could not connect to SAM: %s\n", nt_errstr(result
));
466 /* Get domain policy handle */
467 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
469 MAXIMUM_ALLOWED_ACCESS
,
470 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
472 if (!NT_STATUS_IS_OK(result
)) {
473 d_fprintf(stderr
, "Could not open domain: %s\n", nt_errstr(result
));
477 result
= rpccli_samr_QueryDomainInfo(pipe_hnd
, mem_ctx
,
481 if (NT_STATUS_IS_OK(result
)) {
482 d_printf("Domain Name: %s\n", info
->info2
.domain_name
.string
);
483 d_printf("Domain SID: %s\n", sid_str
);
484 d_printf("Sequence number: %llu\n",
485 (unsigned long long)info
->info2
.sequence_num
);
486 d_printf("Num users: %u\n", info
->info2
.num_users
);
487 d_printf("Num domain groups: %u\n", info
->info2
.num_groups
);
488 d_printf("Num local groups: %u\n", info
->info2
.num_aliases
);
496 * 'net rpc info' entrypoint.
497 * @param argc Standard main() style argc
498 * @param argc Standard main() style argv. Initial components are already
502 int net_rpc_info(struct net_context
*c
, int argc
, const char **argv
)
504 return run_rpc_command(c
, NULL
, PI_SAMR
, NET_FLAGS_PDC
,
510 * Fetch domain SID into the local secrets.tdb
512 * All parameters are provided by the run_rpc_command function, except for
513 * argc, argv which are passes through.
515 * @param domain_sid The domain sid acquired from the remote server
516 * @param cli A cli_state connected to the server.
517 * @param mem_ctx Talloc context, destoyed on completion of thea function.
518 * @param argc Standard main() style argc
519 * @param argv Standard main() style argv. Initial components are already
522 * @return Normal NTSTATUS return.
525 static NTSTATUS
rpc_getsid_internals(struct net_context
*c
,
526 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(struct net_context
*c
, int argc
, const char **argv
)
557 return run_rpc_command(c
, NULL
, PI_SAMR
,
558 NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
559 rpc_getsid_internals
,
563 /****************************************************************************/
566 * Basic usage function for 'net rpc user'
567 * @param argc Standard main() style argc.
568 * @param argv Standard main() style argv. Initial components are already
572 static int rpc_user_usage(struct net_context
*c
, int argc
, const char **argv
)
574 return net_user_usage(c
, argc
, argv
);
578 * Add a new user to a remote RPC server
580 * @param argc Standard main() style argc
581 * @param argv Standard main() style argv. Initial components are already
584 * @return A shell status integer (0 for success)
587 static int rpc_user_add(struct net_context
*c
, int argc
, const char **argv
)
589 NET_API_STATUS status
;
590 struct USER_INFO_1 info1
;
591 uint32_t parm_error
= 0;
594 d_printf("User must be specified\n");
595 rpc_user_usage(c
, argc
, argv
);
601 info1
.usri1_name
= argv
[0];
603 info1
.usri1_password
= argv
[1];
606 status
= NetUserAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
609 d_fprintf(stderr
, "Failed to add user '%s' with: %s.\n",
610 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
614 d_printf("Added user '%s'.\n", argv
[0]);
621 * Rename a user on a remote RPC server
623 * All parameters are provided by the run_rpc_command function, except for
624 * argc, argv which are passes through.
626 * @param domain_sid The domain sid acquired from the remote server
627 * @param cli A cli_state connected to the server.
628 * @param mem_ctx Talloc context, destoyed on completion of the function.
629 * @param argc Standard main() style argc
630 * @param argv Standard main() style argv. Initial components are already
633 * @return Normal NTSTATUS return.
636 static NTSTATUS
rpc_user_rename_internals(struct net_context
*c
,
637 const DOM_SID
*domain_sid
,
638 const char *domain_name
,
639 struct cli_state
*cli
,
640 struct rpc_pipe_client
*pipe_hnd
,
645 POLICY_HND connect_pol
, domain_pol
, user_pol
;
646 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
647 uint32 info_level
= 7;
648 const char *old_name
, *new_name
;
649 struct samr_Ids user_rids
, name_types
;
650 struct lsa_String lsa_acct_name
;
651 union samr_UserInfo
*info
= NULL
;
654 d_printf("Old and new username must be specified\n");
655 rpc_user_usage(c
, argc
, argv
);
662 /* Get sam policy handle */
664 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
666 MAXIMUM_ALLOWED_ACCESS
,
669 if (!NT_STATUS_IS_OK(result
)) {
673 /* Get domain policy handle */
675 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
677 MAXIMUM_ALLOWED_ACCESS
,
678 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
680 if (!NT_STATUS_IS_OK(result
)) {
684 init_lsa_String(&lsa_acct_name
, old_name
);
686 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
692 if (!NT_STATUS_IS_OK(result
)) {
696 /* Open domain user */
697 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
699 MAXIMUM_ALLOWED_ACCESS
,
703 if (!NT_STATUS_IS_OK(result
)) {
707 /* Query user info */
708 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
713 if (!NT_STATUS_IS_OK(result
)) {
717 init_samr_user_info7(&info
->info7
, new_name
);
720 result
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
725 if (!NT_STATUS_IS_OK(result
)) {
730 if (!NT_STATUS_IS_OK(result
)) {
731 d_fprintf(stderr
, "Failed to rename user from %s to %s - %s\n", old_name
, new_name
,
734 d_printf("Renamed user from %s to %s\n", old_name
, new_name
);
740 * Rename a user on a remote RPC server
742 * @param argc Standard main() style argc
743 * @param argv Standard main() style argv. Initial components are already
746 * @return A shell status integer (0 for success)
749 static int rpc_user_rename(struct net_context
*c
, int argc
, const char **argv
)
751 return run_rpc_command(c
, NULL
, PI_SAMR
, 0, rpc_user_rename_internals
,
756 * Delete a user from a remote RPC server
758 * @param argc Standard main() style argc
759 * @param argv Standard main() style argv. Initial components are already
762 * @return A shell status integer (0 for success)
765 static int rpc_user_delete(struct net_context
*c
, int argc
, const char **argv
)
767 NET_API_STATUS status
;
770 d_printf("User must be specified\n");
771 rpc_user_usage(c
, argc
, argv
);
775 status
= NetUserDel(c
->opt_host
, argv
[0]);
778 d_fprintf(stderr
, "Failed to delete user '%s' with: %s.\n",
780 libnetapi_get_error_string(c
->netapi_ctx
, status
));
783 d_printf("Deleted user '%s'.\n", argv
[0]);
790 * Set a password for a user on a remote RPC server
792 * All parameters are provided by the run_rpc_command function, except for
793 * argc, argv which are passes through.
795 * @param domain_sid The domain sid acquired from the remote server
796 * @param cli A cli_state connected to the server.
797 * @param mem_ctx Talloc context, destoyed on completion of the function.
798 * @param argc Standard main() style argc
799 * @param argv Standard main() style argv. Initial components are already
802 * @return Normal NTSTATUS return.
805 static NTSTATUS
rpc_user_password_internals(struct net_context
*c
,
806 const DOM_SID
*domain_sid
,
807 const char *domain_name
,
808 struct cli_state
*cli
,
809 struct rpc_pipe_client
*pipe_hnd
,
814 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
815 POLICY_HND connect_pol
, domain_pol
, user_pol
;
818 const char *new_password
;
820 union samr_UserInfo info
;
823 d_printf("User must be specified\n");
824 rpc_user_usage(c
, argc
, argv
);
831 new_password
= argv
[1];
833 asprintf(&prompt
, "Enter new password for %s:", user
);
834 new_password
= getpass(prompt
);
838 /* Get sam policy and domain handles */
840 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
842 MAXIMUM_ALLOWED_ACCESS
,
845 if (!NT_STATUS_IS_OK(result
)) {
849 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
851 MAXIMUM_ALLOWED_ACCESS
,
852 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
855 if (!NT_STATUS_IS_OK(result
)) {
859 /* Get handle on user */
862 struct samr_Ids user_rids
, name_types
;
863 struct lsa_String lsa_acct_name
;
865 init_lsa_String(&lsa_acct_name
, user
);
867 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
873 if (!NT_STATUS_IS_OK(result
)) {
877 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
879 MAXIMUM_ALLOWED_ACCESS
,
883 if (!NT_STATUS_IS_OK(result
)) {
888 /* Set password on account */
890 encode_pw_buffer(pwbuf
, new_password
, STR_UNICODE
);
892 init_samr_user_info24(&info
.info24
, pwbuf
, 24);
894 SamOEMhashBlob(info
.info24
.password
.data
, 516,
895 &cli
->user_session_key
);
897 result
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
902 if (!NT_STATUS_IS_OK(result
)) {
906 /* Display results */
914 * Set a user's password on a remote RPC server
916 * @param argc Standard main() style argc
917 * @param argv Standard main() style argv. Initial components are already
920 * @return A shell status integer (0 for success)
923 static int rpc_user_password(struct net_context
*c
, int argc
, const char **argv
)
925 return run_rpc_command(c
, NULL
, PI_SAMR
, 0, rpc_user_password_internals
,
930 * List user's groups on a remote RPC server
932 * All parameters are provided by the run_rpc_command function, except for
933 * argc, argv which are passes through.
935 * @param domain_sid The domain sid acquired from the remote server
936 * @param cli A cli_state connected to the server.
937 * @param mem_ctx Talloc context, destoyed on completion of the function.
938 * @param argc Standard main() style argc
939 * @param argv Standard main() style argv. Initial components are already
942 * @return Normal NTSTATUS return.
945 static NTSTATUS
rpc_user_info_internals(struct net_context
*c
,
946 const DOM_SID
*domain_sid
,
947 const char *domain_name
,
948 struct cli_state
*cli
,
949 struct rpc_pipe_client
*pipe_hnd
,
954 POLICY_HND connect_pol
, domain_pol
, user_pol
;
955 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
957 struct samr_RidWithAttributeArray
*rid_array
= NULL
;
958 struct lsa_Strings names
;
959 struct samr_Ids types
;
960 uint32_t *lrids
= NULL
;
961 struct samr_Ids rids
, name_types
;
962 struct lsa_String lsa_acct_name
;
966 d_printf("User must be specified\n");
967 rpc_user_usage(c
, argc
, argv
);
970 /* Get sam policy handle */
972 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
974 MAXIMUM_ALLOWED_ACCESS
,
976 if (!NT_STATUS_IS_OK(result
)) goto done
;
978 /* Get domain policy handle */
980 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
982 MAXIMUM_ALLOWED_ACCESS
,
983 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
985 if (!NT_STATUS_IS_OK(result
)) goto done
;
987 /* Get handle on user */
989 init_lsa_String(&lsa_acct_name
, argv
[0]);
991 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
998 if (!NT_STATUS_IS_OK(result
)) goto done
;
1000 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1002 MAXIMUM_ALLOWED_ACCESS
,
1005 if (!NT_STATUS_IS_OK(result
)) goto done
;
1007 result
= rpccli_samr_GetGroupsForUser(pipe_hnd
, mem_ctx
,
1011 if (!NT_STATUS_IS_OK(result
)) goto done
;
1015 if (rid_array
->count
) {
1016 if ((lrids
= TALLOC_ARRAY(mem_ctx
, uint32
, rid_array
->count
)) == NULL
) {
1017 result
= NT_STATUS_NO_MEMORY
;
1021 for (i
= 0; i
< rid_array
->count
; i
++)
1022 lrids
[i
] = rid_array
->rids
[i
].rid
;
1024 result
= rpccli_samr_LookupRids(pipe_hnd
, mem_ctx
,
1031 if (!NT_STATUS_IS_OK(result
)) {
1035 /* Display results */
1037 for (i
= 0; i
< names
.count
; i
++)
1038 printf("%s\n", names
.names
[i
].string
);
1045 * List a user's groups from a remote RPC server
1047 * @param argc Standard main() style argc
1048 * @param argv Standard main() style argv. Initial components are already
1051 * @return A shell status integer (0 for success)
1054 static int rpc_user_info(struct net_context
*c
, int argc
, const char **argv
)
1056 return run_rpc_command(c
, NULL
, PI_SAMR
, 0, rpc_user_info_internals
,
1061 * List users on a remote RPC server
1063 * All parameters are provided by the run_rpc_command function, except for
1064 * argc, argv which are passes through.
1066 * @param domain_sid The domain sid acquired from the remote server
1067 * @param cli A cli_state connected to the server.
1068 * @param mem_ctx Talloc context, destoyed on completion of the function.
1069 * @param argc Standard main() style argc
1070 * @param argv Standard main() style argv. Initial components are already
1073 * @return Normal NTSTATUS return.
1076 static NTSTATUS
rpc_user_list_internals(struct net_context
*c
,
1077 const DOM_SID
*domain_sid
,
1078 const char *domain_name
,
1079 struct cli_state
*cli
,
1080 struct rpc_pipe_client
*pipe_hnd
,
1081 TALLOC_CTX
*mem_ctx
,
1085 POLICY_HND connect_pol
, domain_pol
;
1086 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1087 uint32 start_idx
=0, num_entries
, i
, loop_count
= 0;
1089 /* Get sam policy handle */
1091 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1093 MAXIMUM_ALLOWED_ACCESS
,
1095 if (!NT_STATUS_IS_OK(result
)) {
1099 /* Get domain policy handle */
1101 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1103 MAXIMUM_ALLOWED_ACCESS
,
1104 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1106 if (!NT_STATUS_IS_OK(result
)) {
1110 /* Query domain users */
1111 if (c
->opt_long_list_entries
)
1112 d_printf("\nUser name Comment"\
1113 "\n-----------------------------\n");
1115 const char *user
= NULL
;
1116 const char *desc
= NULL
;
1117 uint32 max_entries
, max_size
;
1118 uint32_t total_size
, returned_size
;
1119 union samr_DispInfo info
;
1121 get_query_dispinfo_params(
1122 loop_count
, &max_entries
, &max_size
);
1124 result
= rpccli_samr_QueryDisplayInfo(pipe_hnd
, mem_ctx
,
1134 start_idx
+= info
.info1
.count
;
1135 num_entries
= info
.info1
.count
;
1137 for (i
= 0; i
< num_entries
; i
++) {
1138 user
= info
.info1
.entries
[i
].account_name
.string
;
1139 if (c
->opt_long_list_entries
)
1140 desc
= info
.info1
.entries
[i
].description
.string
;
1141 if (c
->opt_long_list_entries
)
1142 printf("%-21.21s %s\n", user
, desc
);
1144 printf("%s\n", user
);
1146 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
1153 * 'net rpc user' entrypoint.
1154 * @param argc Standard main() style argc
1155 * @param argc Standard main() style argv. Initial components are already
1159 int net_rpc_user(struct net_context
*c
, int argc
, const char **argv
)
1161 NET_API_STATUS status
;
1163 struct functable func
[] = {
1164 {"add", rpc_user_add
},
1165 {"info", rpc_user_info
},
1166 {"delete", rpc_user_delete
},
1167 {"password", rpc_user_password
},
1168 {"rename", rpc_user_rename
},
1172 status
= libnetapi_init(&c
->netapi_ctx
);
1176 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
1177 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
1180 return run_rpc_command(c
, NULL
,PI_SAMR
, 0,
1181 rpc_user_list_internals
,
1185 return net_run_function(c
, argc
, argv
, func
, rpc_user_usage
);
1188 static NTSTATUS
rpc_sh_user_list(struct net_context
*c
,
1189 TALLOC_CTX
*mem_ctx
,
1190 struct rpc_sh_ctx
*ctx
,
1191 struct rpc_pipe_client
*pipe_hnd
,
1192 int argc
, const char **argv
)
1194 return rpc_user_list_internals(c
, ctx
->domain_sid
, ctx
->domain_name
,
1195 ctx
->cli
, pipe_hnd
, mem_ctx
,
1199 static NTSTATUS
rpc_sh_user_info(struct net_context
*c
,
1200 TALLOC_CTX
*mem_ctx
,
1201 struct rpc_sh_ctx
*ctx
,
1202 struct rpc_pipe_client
*pipe_hnd
,
1203 int argc
, const char **argv
)
1205 return rpc_user_info_internals(c
, ctx
->domain_sid
, ctx
->domain_name
,
1206 ctx
->cli
, pipe_hnd
, mem_ctx
,
1210 static NTSTATUS
rpc_sh_handle_user(struct net_context
*c
,
1211 TALLOC_CTX
*mem_ctx
,
1212 struct rpc_sh_ctx
*ctx
,
1213 struct rpc_pipe_client
*pipe_hnd
,
1214 int argc
, const char **argv
,
1216 struct net_context
*c
,
1217 TALLOC_CTX
*mem_ctx
,
1218 struct rpc_sh_ctx
*ctx
,
1219 struct rpc_pipe_client
*pipe_hnd
,
1220 POLICY_HND
*user_hnd
,
1221 int argc
, const char **argv
))
1223 POLICY_HND connect_pol
, domain_pol
, user_pol
;
1224 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1227 enum lsa_SidType type
;
1230 d_fprintf(stderr
, "usage: %s <username>\n", ctx
->whoami
);
1231 return NT_STATUS_INVALID_PARAMETER
;
1234 ZERO_STRUCT(connect_pol
);
1235 ZERO_STRUCT(domain_pol
);
1236 ZERO_STRUCT(user_pol
);
1238 result
= net_rpc_lookup_name(c
, mem_ctx
, rpc_pipe_np_smb_conn(pipe_hnd
),
1239 argv
[0], NULL
, NULL
, &sid
, &type
);
1240 if (!NT_STATUS_IS_OK(result
)) {
1241 d_fprintf(stderr
, "Could not lookup %s: %s\n", argv
[0],
1246 if (type
!= SID_NAME_USER
) {
1247 d_fprintf(stderr
, "%s is a %s, not a user\n", argv
[0],
1248 sid_type_lookup(type
));
1249 result
= NT_STATUS_NO_SUCH_USER
;
1253 if (!sid_peek_check_rid(ctx
->domain_sid
, &sid
, &rid
)) {
1254 d_fprintf(stderr
, "%s is not in our domain\n", argv
[0]);
1255 result
= NT_STATUS_NO_SUCH_USER
;
1259 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1261 MAXIMUM_ALLOWED_ACCESS
,
1263 if (!NT_STATUS_IS_OK(result
)) {
1267 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1269 MAXIMUM_ALLOWED_ACCESS
,
1272 if (!NT_STATUS_IS_OK(result
)) {
1276 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1278 MAXIMUM_ALLOWED_ACCESS
,
1281 if (!NT_STATUS_IS_OK(result
)) {
1285 result
= fn(c
, mem_ctx
, ctx
, pipe_hnd
, &user_pol
, argc
-1, argv
+1);
1288 if (is_valid_policy_hnd(&user_pol
)) {
1289 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1291 if (is_valid_policy_hnd(&domain_pol
)) {
1292 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1294 if (is_valid_policy_hnd(&connect_pol
)) {
1295 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1300 static NTSTATUS
rpc_sh_user_show_internals(struct net_context
*c
,
1301 TALLOC_CTX
*mem_ctx
,
1302 struct rpc_sh_ctx
*ctx
,
1303 struct rpc_pipe_client
*pipe_hnd
,
1304 POLICY_HND
*user_hnd
,
1305 int argc
, const char **argv
)
1308 union samr_UserInfo
*info
= NULL
;
1311 d_fprintf(stderr
, "usage: %s show <username>\n", ctx
->whoami
);
1312 return NT_STATUS_INVALID_PARAMETER
;
1315 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1319 if (!NT_STATUS_IS_OK(result
)) {
1323 d_printf("user rid: %d, group rid: %d\n",
1325 info
->info21
.primary_gid
);
1330 static NTSTATUS
rpc_sh_user_show(struct net_context
*c
,
1331 TALLOC_CTX
*mem_ctx
,
1332 struct rpc_sh_ctx
*ctx
,
1333 struct rpc_pipe_client
*pipe_hnd
,
1334 int argc
, const char **argv
)
1336 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1337 rpc_sh_user_show_internals
);
1340 #define FETCHSTR(name, rec) \
1341 do { if (strequal(ctx->thiscmd, name)) { \
1342 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1345 #define SETSTR(name, rec, flag) \
1346 do { if (strequal(ctx->thiscmd, name)) { \
1347 init_lsa_String(&(info->info21.rec), argv[0]); \
1348 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1351 static NTSTATUS
rpc_sh_user_str_edit_internals(struct net_context
*c
,
1352 TALLOC_CTX
*mem_ctx
,
1353 struct rpc_sh_ctx
*ctx
,
1354 struct rpc_pipe_client
*pipe_hnd
,
1355 POLICY_HND
*user_hnd
,
1356 int argc
, const char **argv
)
1359 const char *username
;
1360 const char *oldval
= "";
1361 union samr_UserInfo
*info
= NULL
;
1364 d_fprintf(stderr
, "usage: %s <username> [new value|NULL]\n",
1366 return NT_STATUS_INVALID_PARAMETER
;
1369 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1373 if (!NT_STATUS_IS_OK(result
)) {
1377 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1379 FETCHSTR("fullname", full_name
);
1380 FETCHSTR("homedir", home_directory
);
1381 FETCHSTR("homedrive", home_drive
);
1382 FETCHSTR("logonscript", logon_script
);
1383 FETCHSTR("profilepath", profile_path
);
1384 FETCHSTR("description", description
);
1387 d_printf("%s's %s: [%s]\n", username
, ctx
->thiscmd
, oldval
);
1391 if (strcmp(argv
[0], "NULL") == 0) {
1395 ZERO_STRUCT(info
->info21
);
1397 SETSTR("fullname", full_name
, FULL_NAME
);
1398 SETSTR("homedir", home_directory
, HOME_DIRECTORY
);
1399 SETSTR("homedrive", home_drive
, HOME_DRIVE
);
1400 SETSTR("logonscript", logon_script
, LOGON_SCRIPT
);
1401 SETSTR("profilepath", profile_path
, PROFILE_PATH
);
1402 SETSTR("description", description
, DESCRIPTION
);
1404 result
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1409 d_printf("Set %s's %s from [%s] to [%s]\n", username
,
1410 ctx
->thiscmd
, oldval
, argv
[0]);
1417 #define HANDLEFLG(name, rec) \
1418 do { if (strequal(ctx->thiscmd, name)) { \
1419 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1421 newflags = oldflags | ACB_##rec; \
1423 newflags = oldflags & ~ACB_##rec; \
1426 static NTSTATUS
rpc_sh_user_str_edit(struct net_context
*c
,
1427 TALLOC_CTX
*mem_ctx
,
1428 struct rpc_sh_ctx
*ctx
,
1429 struct rpc_pipe_client
*pipe_hnd
,
1430 int argc
, const char **argv
)
1432 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1433 rpc_sh_user_str_edit_internals
);
1436 static NTSTATUS
rpc_sh_user_flag_edit_internals(struct net_context
*c
,
1437 TALLOC_CTX
*mem_ctx
,
1438 struct rpc_sh_ctx
*ctx
,
1439 struct rpc_pipe_client
*pipe_hnd
,
1440 POLICY_HND
*user_hnd
,
1441 int argc
, const char **argv
)
1444 const char *username
;
1445 const char *oldval
= "unknown";
1446 uint32 oldflags
, newflags
;
1448 union samr_UserInfo
*info
= NULL
;
1451 ((argc
== 1) && !strequal(argv
[0], "yes") &&
1452 !strequal(argv
[0], "no"))) {
1453 d_fprintf(stderr
, "usage: %s <username> [yes|no]\n",
1455 return NT_STATUS_INVALID_PARAMETER
;
1458 newval
= strequal(argv
[0], "yes");
1460 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1464 if (!NT_STATUS_IS_OK(result
)) {
1468 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1469 oldflags
= info
->info21
.acct_flags
;
1470 newflags
= info
->info21
.acct_flags
;
1472 HANDLEFLG("disabled", DISABLED
);
1473 HANDLEFLG("pwnotreq", PWNOTREQ
);
1474 HANDLEFLG("autolock", AUTOLOCK
);
1475 HANDLEFLG("pwnoexp", PWNOEXP
);
1478 d_printf("%s's %s flag: %s\n", username
, ctx
->thiscmd
, oldval
);
1482 ZERO_STRUCT(info
->info21
);
1484 info
->info21
.acct_flags
= newflags
;
1485 info
->info21
.fields_present
= SAMR_FIELD_ACCT_FLAGS
;
1487 result
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1492 if (NT_STATUS_IS_OK(result
)) {
1493 d_printf("Set %s's %s flag from [%s] to [%s]\n", username
,
1494 ctx
->thiscmd
, oldval
, argv
[0]);
1502 static NTSTATUS
rpc_sh_user_flag_edit(struct net_context
*c
,
1503 TALLOC_CTX
*mem_ctx
,
1504 struct rpc_sh_ctx
*ctx
,
1505 struct rpc_pipe_client
*pipe_hnd
,
1506 int argc
, const char **argv
)
1508 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1509 rpc_sh_user_flag_edit_internals
);
1512 struct rpc_sh_cmd
*net_rpc_user_edit_cmds(struct net_context
*c
,
1513 TALLOC_CTX
*mem_ctx
,
1514 struct rpc_sh_ctx
*ctx
)
1516 static struct rpc_sh_cmd cmds
[] = {
1518 { "fullname", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1519 "Show/Set a user's full name" },
1521 { "homedir", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1522 "Show/Set a user's home directory" },
1524 { "homedrive", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1525 "Show/Set a user's home drive" },
1527 { "logonscript", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1528 "Show/Set a user's logon script" },
1530 { "profilepath", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1531 "Show/Set a user's profile path" },
1533 { "description", NULL
, PI_SAMR
, rpc_sh_user_str_edit
,
1534 "Show/Set a user's description" },
1536 { "disabled", NULL
, PI_SAMR
, rpc_sh_user_flag_edit
,
1537 "Show/Set whether a user is disabled" },
1539 { "autolock", NULL
, PI_SAMR
, rpc_sh_user_flag_edit
,
1540 "Show/Set whether a user locked out" },
1542 { "pwnotreq", NULL
, PI_SAMR
, rpc_sh_user_flag_edit
,
1543 "Show/Set whether a user does not need a password" },
1545 { "pwnoexp", NULL
, PI_SAMR
, rpc_sh_user_flag_edit
,
1546 "Show/Set whether a user's password does not expire" },
1548 { NULL
, NULL
, 0, NULL
, NULL
}
1554 struct rpc_sh_cmd
*net_rpc_user_cmds(struct net_context
*c
,
1555 TALLOC_CTX
*mem_ctx
,
1556 struct rpc_sh_ctx
*ctx
)
1558 static struct rpc_sh_cmd cmds
[] = {
1560 { "list", NULL
, PI_SAMR
, rpc_sh_user_list
,
1561 "List available users" },
1563 { "info", NULL
, PI_SAMR
, rpc_sh_user_info
,
1564 "List the domain groups a user is member of" },
1566 { "show", NULL
, PI_SAMR
, rpc_sh_user_show
,
1567 "Show info about a user" },
1569 { "edit", net_rpc_user_edit_cmds
, 0, NULL
,
1570 "Show/Modify a user's fields" },
1572 { NULL
, NULL
, 0, NULL
, NULL
}
1578 /****************************************************************************/
1581 * Basic usage function for 'net rpc group'
1582 * @param argc Standard main() style argc.
1583 * @param argv Standard main() style argv. Initial components are already
1587 static int rpc_group_usage(struct net_context
*c
, int argc
, const char **argv
)
1589 return net_group_usage(c
, argc
, argv
);
1593 * Delete group on a remote RPC server
1595 * All parameters are provided by the run_rpc_command function, except for
1596 * argc, argv which are passes through.
1598 * @param domain_sid The domain sid acquired from the remote server
1599 * @param cli A cli_state connected to the server.
1600 * @param mem_ctx Talloc context, destoyed on completion of the function.
1601 * @param argc Standard main() style argc
1602 * @param argv Standard main() style argv. Initial components are already
1605 * @return Normal NTSTATUS return.
1608 static NTSTATUS
rpc_group_delete_internals(struct net_context
*c
,
1609 const DOM_SID
*domain_sid
,
1610 const char *domain_name
,
1611 struct cli_state
*cli
,
1612 struct rpc_pipe_client
*pipe_hnd
,
1613 TALLOC_CTX
*mem_ctx
,
1617 POLICY_HND connect_pol
, domain_pol
, group_pol
, user_pol
;
1618 bool group_is_primary
= false;
1619 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1621 struct samr_RidTypeArray
*rids
= NULL
;
1624 /* DOM_GID *user_gids; */
1626 struct samr_Ids group_rids
, name_types
;
1627 struct lsa_String lsa_acct_name
;
1628 union samr_UserInfo
*info
= NULL
;
1631 d_printf("specify group\n");
1632 rpc_group_usage(c
, argc
,argv
);
1633 return NT_STATUS_OK
; /* ok? */
1636 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1638 MAXIMUM_ALLOWED_ACCESS
,
1641 if (!NT_STATUS_IS_OK(result
)) {
1642 d_fprintf(stderr
, "Request samr_Connect2 failed\n");
1646 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1648 MAXIMUM_ALLOWED_ACCESS
,
1649 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1652 if (!NT_STATUS_IS_OK(result
)) {
1653 d_fprintf(stderr
, "Request open_domain failed\n");
1657 init_lsa_String(&lsa_acct_name
, argv
[0]);
1659 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1665 if (!NT_STATUS_IS_OK(result
)) {
1666 d_fprintf(stderr
, "Lookup of '%s' failed\n",argv
[0]);
1670 switch (name_types
.ids
[0])
1672 case SID_NAME_DOM_GRP
:
1673 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
1675 MAXIMUM_ALLOWED_ACCESS
,
1678 if (!NT_STATUS_IS_OK(result
)) {
1679 d_fprintf(stderr
, "Request open_group failed");
1683 group_rid
= group_rids
.ids
[0];
1685 result
= rpccli_samr_QueryGroupMember(pipe_hnd
, mem_ctx
,
1689 if (!NT_STATUS_IS_OK(result
)) {
1690 d_fprintf(stderr
, "Unable to query group members of %s",argv
[0]);
1694 if (c
->opt_verbose
) {
1695 d_printf("Domain Group %s (rid: %d) has %d members\n",
1696 argv
[0],group_rid
, rids
->count
);
1699 /* Check if group is anyone's primary group */
1700 for (i
= 0; i
< rids
->count
; i
++)
1702 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1704 MAXIMUM_ALLOWED_ACCESS
,
1708 if (!NT_STATUS_IS_OK(result
)) {
1709 d_fprintf(stderr
, "Unable to open group member %d\n",
1714 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1719 if (!NT_STATUS_IS_OK(result
)) {
1720 d_fprintf(stderr
, "Unable to lookup userinfo for group member %d\n",
1725 if (info
->info21
.primary_gid
== group_rid
) {
1726 if (c
->opt_verbose
) {
1727 d_printf("Group is primary group of %s\n",
1728 info
->info21
.account_name
.string
);
1730 group_is_primary
= true;
1733 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1736 if (group_is_primary
) {
1737 d_fprintf(stderr
, "Unable to delete group because some "
1738 "of it's members have it as primary group\n");
1739 result
= NT_STATUS_MEMBERS_PRIMARY_GROUP
;
1743 /* remove all group members */
1744 for (i
= 0; i
< rids
->count
; i
++)
1747 d_printf("Remove group member %d...",
1749 result
= rpccli_samr_DeleteGroupMember(pipe_hnd
, mem_ctx
,
1753 if (NT_STATUS_IS_OK(result
)) {
1758 d_printf("failed\n");
1763 result
= rpccli_samr_DeleteDomainGroup(pipe_hnd
, mem_ctx
,
1767 /* removing a local group is easier... */
1768 case SID_NAME_ALIAS
:
1769 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
1771 MAXIMUM_ALLOWED_ACCESS
,
1775 if (!NT_STATUS_IS_OK(result
)) {
1776 d_fprintf(stderr
, "Request open_alias failed\n");
1780 result
= rpccli_samr_DeleteDomAlias(pipe_hnd
, mem_ctx
,
1784 d_fprintf(stderr
, "%s is of type %s. This command is only for deleting local or global groups\n",
1785 argv
[0],sid_type_lookup(name_types
.ids
[0]));
1786 result
= NT_STATUS_UNSUCCESSFUL
;
1790 if (NT_STATUS_IS_OK(result
)) {
1792 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types
.ids
[0]),argv
[0]);
1794 d_fprintf(stderr
, "Deleting of %s failed: %s\n",argv
[0],
1795 get_friendly_nt_error_msg(result
));
1803 static int rpc_group_delete(struct net_context
*c
, int argc
, const char **argv
)
1805 return run_rpc_command(c
, NULL
, PI_SAMR
, 0, rpc_group_delete_internals
,
1809 static NTSTATUS
rpc_group_add_internals(struct net_context
*c
,
1810 const DOM_SID
*domain_sid
,
1811 const char *domain_name
,
1812 struct cli_state
*cli
,
1813 struct rpc_pipe_client
*pipe_hnd
,
1814 TALLOC_CTX
*mem_ctx
,
1818 POLICY_HND connect_pol
, domain_pol
, group_pol
;
1819 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1820 union samr_GroupInfo group_info
;
1821 struct lsa_String grp_name
;
1825 d_printf("Group name must be specified\n");
1826 rpc_group_usage(c
, argc
, argv
);
1827 return NT_STATUS_OK
;
1830 init_lsa_String(&grp_name
, argv
[0]);
1832 /* Get sam policy handle */
1834 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1836 MAXIMUM_ALLOWED_ACCESS
,
1838 if (!NT_STATUS_IS_OK(result
)) goto done
;
1840 /* Get domain policy handle */
1842 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1844 MAXIMUM_ALLOWED_ACCESS
,
1845 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1847 if (!NT_STATUS_IS_OK(result
)) goto done
;
1849 /* Create the group */
1851 result
= rpccli_samr_CreateDomainGroup(pipe_hnd
, mem_ctx
,
1854 MAXIMUM_ALLOWED_ACCESS
,
1857 if (!NT_STATUS_IS_OK(result
)) goto done
;
1859 if (strlen(c
->opt_comment
) == 0) goto done
;
1861 /* We've got a comment to set */
1863 init_lsa_String(&group_info
.description
, c
->opt_comment
);
1865 result
= rpccli_samr_SetGroupInfo(pipe_hnd
, mem_ctx
,
1869 if (!NT_STATUS_IS_OK(result
)) goto done
;
1872 if (NT_STATUS_IS_OK(result
))
1873 DEBUG(5, ("add group succeeded\n"));
1875 d_fprintf(stderr
, "add group failed: %s\n", nt_errstr(result
));
1880 static NTSTATUS
rpc_alias_add_internals(struct net_context
*c
,
1881 const DOM_SID
*domain_sid
,
1882 const char *domain_name
,
1883 struct cli_state
*cli
,
1884 struct rpc_pipe_client
*pipe_hnd
,
1885 TALLOC_CTX
*mem_ctx
,
1889 POLICY_HND connect_pol
, domain_pol
, alias_pol
;
1890 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1891 union samr_AliasInfo alias_info
;
1892 struct lsa_String alias_name
;
1896 d_printf("Alias name must be specified\n");
1897 rpc_group_usage(c
, argc
, argv
);
1898 return NT_STATUS_OK
;
1901 init_lsa_String(&alias_name
, argv
[0]);
1903 /* Get sam policy handle */
1905 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1907 MAXIMUM_ALLOWED_ACCESS
,
1909 if (!NT_STATUS_IS_OK(result
)) goto done
;
1911 /* Get domain policy handle */
1913 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1915 MAXIMUM_ALLOWED_ACCESS
,
1916 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1918 if (!NT_STATUS_IS_OK(result
)) goto done
;
1920 /* Create the group */
1922 result
= rpccli_samr_CreateDomAlias(pipe_hnd
, mem_ctx
,
1925 MAXIMUM_ALLOWED_ACCESS
,
1928 if (!NT_STATUS_IS_OK(result
)) goto done
;
1930 if (strlen(c
->opt_comment
) == 0) goto done
;
1932 /* We've got a comment to set */
1934 init_lsa_String(&alias_info
.description
, c
->opt_comment
);
1936 result
= rpccli_samr_SetAliasInfo(pipe_hnd
, mem_ctx
,
1941 if (!NT_STATUS_IS_OK(result
)) goto done
;
1944 if (NT_STATUS_IS_OK(result
))
1945 DEBUG(5, ("add alias succeeded\n"));
1947 d_fprintf(stderr
, "add alias failed: %s\n", nt_errstr(result
));
1952 static int rpc_group_add(struct net_context
*c
, int argc
, const char **argv
)
1954 if (c
->opt_localgroup
)
1955 return run_rpc_command(c
, NULL
, PI_SAMR
, 0,
1956 rpc_alias_add_internals
,
1959 return run_rpc_command(c
, NULL
, PI_SAMR
, 0,
1960 rpc_group_add_internals
,
1964 static NTSTATUS
get_sid_from_name(struct cli_state
*cli
,
1965 TALLOC_CTX
*mem_ctx
,
1968 enum lsa_SidType
*type
)
1970 DOM_SID
*sids
= NULL
;
1971 enum lsa_SidType
*types
= NULL
;
1972 struct rpc_pipe_client
*pipe_hnd
;
1974 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1976 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &result
);
1981 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, false,
1982 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
1984 if (!NT_STATUS_IS_OK(result
)) {
1988 result
= rpccli_lsa_lookup_names(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
1989 &name
, NULL
, 1, &sids
, &types
);
1991 if (NT_STATUS_IS_OK(result
)) {
1992 sid_copy(sid
, &sids
[0]);
1996 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
2000 TALLOC_FREE(pipe_hnd
);
2003 if (!NT_STATUS_IS_OK(result
) && (StrnCaseCmp(name
, "S-", 2) == 0)) {
2005 /* Try as S-1-5-whatever */
2009 if (string_to_sid(&tmp_sid
, name
)) {
2010 sid_copy(sid
, &tmp_sid
);
2011 *type
= SID_NAME_UNKNOWN
;
2012 result
= NT_STATUS_OK
;
2019 static NTSTATUS
rpc_add_groupmem(struct rpc_pipe_client
*pipe_hnd
,
2020 TALLOC_CTX
*mem_ctx
,
2021 const DOM_SID
*group_sid
,
2024 POLICY_HND connect_pol
, domain_pol
;
2027 POLICY_HND group_pol
;
2029 struct samr_Ids rids
, rid_types
;
2030 struct lsa_String lsa_acct_name
;
2034 sid_copy(&sid
, group_sid
);
2036 if (!sid_split_rid(&sid
, &group_rid
)) {
2037 return NT_STATUS_UNSUCCESSFUL
;
2040 /* Get sam policy handle */
2041 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2043 MAXIMUM_ALLOWED_ACCESS
,
2045 if (!NT_STATUS_IS_OK(result
)) {
2049 /* Get domain policy handle */
2050 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2052 MAXIMUM_ALLOWED_ACCESS
,
2055 if (!NT_STATUS_IS_OK(result
)) {
2059 init_lsa_String(&lsa_acct_name
, member
);
2061 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2068 if (!NT_STATUS_IS_OK(result
)) {
2069 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2073 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2075 MAXIMUM_ALLOWED_ACCESS
,
2079 if (!NT_STATUS_IS_OK(result
)) {
2083 result
= rpccli_samr_AddGroupMember(pipe_hnd
, mem_ctx
,
2086 0x0005); /* unknown flags */
2089 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2093 static NTSTATUS
rpc_add_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2094 TALLOC_CTX
*mem_ctx
,
2095 const DOM_SID
*alias_sid
,
2098 POLICY_HND connect_pol
, domain_pol
;
2101 POLICY_HND alias_pol
;
2104 enum lsa_SidType member_type
;
2108 sid_copy(&sid
, alias_sid
);
2110 if (!sid_split_rid(&sid
, &alias_rid
)) {
2111 return NT_STATUS_UNSUCCESSFUL
;
2114 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2115 member
, &member_sid
, &member_type
);
2117 if (!NT_STATUS_IS_OK(result
)) {
2118 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2122 /* Get sam policy handle */
2123 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2125 MAXIMUM_ALLOWED_ACCESS
,
2127 if (!NT_STATUS_IS_OK(result
)) {
2131 /* Get domain policy handle */
2132 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2134 MAXIMUM_ALLOWED_ACCESS
,
2137 if (!NT_STATUS_IS_OK(result
)) {
2141 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2143 MAXIMUM_ALLOWED_ACCESS
,
2147 if (!NT_STATUS_IS_OK(result
)) {
2151 result
= rpccli_samr_AddAliasMember(pipe_hnd
, mem_ctx
,
2155 if (!NT_STATUS_IS_OK(result
)) {
2160 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2164 static NTSTATUS
rpc_group_addmem_internals(struct net_context
*c
,
2165 const DOM_SID
*domain_sid
,
2166 const char *domain_name
,
2167 struct cli_state
*cli
,
2168 struct rpc_pipe_client
*pipe_hnd
,
2169 TALLOC_CTX
*mem_ctx
,
2174 enum lsa_SidType group_type
;
2177 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2178 return NT_STATUS_UNSUCCESSFUL
;
2181 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2182 &group_sid
, &group_type
))) {
2183 d_fprintf(stderr
, "Could not lookup group name %s\n", argv
[0]);
2184 return NT_STATUS_UNSUCCESSFUL
;
2187 if (group_type
== SID_NAME_DOM_GRP
) {
2188 NTSTATUS result
= rpc_add_groupmem(pipe_hnd
, mem_ctx
,
2189 &group_sid
, argv
[1]);
2191 if (!NT_STATUS_IS_OK(result
)) {
2192 d_fprintf(stderr
, "Could not add %s to %s: %s\n",
2193 argv
[1], argv
[0], nt_errstr(result
));
2198 if (group_type
== SID_NAME_ALIAS
) {
2199 NTSTATUS result
= rpc_add_aliasmem(pipe_hnd
, mem_ctx
,
2200 &group_sid
, argv
[1]);
2202 if (!NT_STATUS_IS_OK(result
)) {
2203 d_fprintf(stderr
, "Could not add %s to %s: %s\n",
2204 argv
[1], argv
[0], nt_errstr(result
));
2209 d_fprintf(stderr
, "Can only add members to global or local groups "
2210 "which %s is not\n", argv
[0]);
2212 return NT_STATUS_UNSUCCESSFUL
;
2215 static int rpc_group_addmem(struct net_context
*c
, int argc
, const char **argv
)
2217 return run_rpc_command(c
, NULL
, PI_SAMR
, 0,
2218 rpc_group_addmem_internals
,
2222 static NTSTATUS
rpc_del_groupmem(struct net_context
*c
,
2223 struct rpc_pipe_client
*pipe_hnd
,
2224 TALLOC_CTX
*mem_ctx
,
2225 const DOM_SID
*group_sid
,
2228 POLICY_HND connect_pol
, domain_pol
;
2231 POLICY_HND group_pol
;
2233 struct samr_Ids rids
, rid_types
;
2234 struct lsa_String lsa_acct_name
;
2238 sid_copy(&sid
, group_sid
);
2240 if (!sid_split_rid(&sid
, &group_rid
))
2241 return NT_STATUS_UNSUCCESSFUL
;
2243 /* Get sam policy handle */
2244 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2246 MAXIMUM_ALLOWED_ACCESS
,
2248 if (!NT_STATUS_IS_OK(result
))
2251 /* Get domain policy handle */
2252 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2254 MAXIMUM_ALLOWED_ACCESS
,
2257 if (!NT_STATUS_IS_OK(result
))
2260 init_lsa_String(&lsa_acct_name
, member
);
2262 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2268 if (!NT_STATUS_IS_OK(result
)) {
2269 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2273 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2275 MAXIMUM_ALLOWED_ACCESS
,
2279 if (!NT_STATUS_IS_OK(result
))
2282 result
= rpccli_samr_DeleteGroupMember(pipe_hnd
, mem_ctx
,
2287 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2291 static NTSTATUS
rpc_del_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2292 TALLOC_CTX
*mem_ctx
,
2293 const DOM_SID
*alias_sid
,
2296 POLICY_HND connect_pol
, domain_pol
;
2299 POLICY_HND alias_pol
;
2302 enum lsa_SidType member_type
;
2306 sid_copy(&sid
, alias_sid
);
2308 if (!sid_split_rid(&sid
, &alias_rid
))
2309 return NT_STATUS_UNSUCCESSFUL
;
2311 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2312 member
, &member_sid
, &member_type
);
2314 if (!NT_STATUS_IS_OK(result
)) {
2315 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2319 /* Get sam policy handle */
2320 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2322 MAXIMUM_ALLOWED_ACCESS
,
2324 if (!NT_STATUS_IS_OK(result
)) {
2328 /* Get domain policy handle */
2329 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2331 MAXIMUM_ALLOWED_ACCESS
,
2334 if (!NT_STATUS_IS_OK(result
)) {
2338 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2340 MAXIMUM_ALLOWED_ACCESS
,
2344 if (!NT_STATUS_IS_OK(result
))
2347 result
= rpccli_samr_DeleteAliasMember(pipe_hnd
, mem_ctx
,
2351 if (!NT_STATUS_IS_OK(result
))
2355 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2359 static NTSTATUS
rpc_group_delmem_internals(struct net_context
*c
,
2360 const DOM_SID
*domain_sid
,
2361 const char *domain_name
,
2362 struct cli_state
*cli
,
2363 struct rpc_pipe_client
*pipe_hnd
,
2364 TALLOC_CTX
*mem_ctx
,
2369 enum lsa_SidType group_type
;
2372 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2373 return NT_STATUS_UNSUCCESSFUL
;
2376 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2377 &group_sid
, &group_type
))) {
2378 d_fprintf(stderr
, "Could not lookup group name %s\n", argv
[0]);
2379 return NT_STATUS_UNSUCCESSFUL
;
2382 if (group_type
== SID_NAME_DOM_GRP
) {
2383 NTSTATUS result
= rpc_del_groupmem(c
, pipe_hnd
, mem_ctx
,
2384 &group_sid
, argv
[1]);
2386 if (!NT_STATUS_IS_OK(result
)) {
2387 d_fprintf(stderr
, "Could not del %s from %s: %s\n",
2388 argv
[1], argv
[0], nt_errstr(result
));
2393 if (group_type
== SID_NAME_ALIAS
) {
2394 NTSTATUS result
= rpc_del_aliasmem(pipe_hnd
, mem_ctx
,
2395 &group_sid
, argv
[1]);
2397 if (!NT_STATUS_IS_OK(result
)) {
2398 d_fprintf(stderr
, "Could not del %s from %s: %s\n",
2399 argv
[1], argv
[0], nt_errstr(result
));
2404 d_fprintf(stderr
, "Can only delete members from global or local groups "
2405 "which %s is not\n", argv
[0]);
2407 return NT_STATUS_UNSUCCESSFUL
;
2410 static int rpc_group_delmem(struct net_context
*c
, int argc
, const char **argv
)
2412 return run_rpc_command(c
, NULL
, PI_SAMR
, 0,
2413 rpc_group_delmem_internals
,
2418 * List groups on a remote RPC server
2420 * All parameters are provided by the run_rpc_command function, except for
2421 * argc, argv which are passes through.
2423 * @param domain_sid The domain sid acquired from the remote server
2424 * @param cli A cli_state connected to the server.
2425 * @param mem_ctx Talloc context, destoyed on completion of the function.
2426 * @param argc Standard main() style argc
2427 * @param argv Standard main() style argv. Initial components are already
2430 * @return Normal NTSTATUS return.
2433 static NTSTATUS
rpc_group_list_internals(struct net_context
*c
,
2434 const DOM_SID
*domain_sid
,
2435 const char *domain_name
,
2436 struct cli_state
*cli
,
2437 struct rpc_pipe_client
*pipe_hnd
,
2438 TALLOC_CTX
*mem_ctx
,
2442 POLICY_HND connect_pol
, domain_pol
;
2443 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2444 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
2445 struct samr_SamArray
*groups
= NULL
;
2446 bool global
= false;
2448 bool builtin
= false;
2456 for (i
=0; i
<argc
; i
++) {
2457 if (strequal(argv
[i
], "global"))
2460 if (strequal(argv
[i
], "local"))
2463 if (strequal(argv
[i
], "builtin"))
2467 /* Get sam policy handle */
2469 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2471 MAXIMUM_ALLOWED_ACCESS
,
2473 if (!NT_STATUS_IS_OK(result
)) {
2477 /* Get domain policy handle */
2479 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2481 MAXIMUM_ALLOWED_ACCESS
,
2482 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2484 if (!NT_STATUS_IS_OK(result
)) {
2488 /* Query domain groups */
2489 if (c
->opt_long_list_entries
)
2490 d_printf("\nGroup name Comment"\
2491 "\n-----------------------------\n");
2493 uint32_t max_size
, total_size
, returned_size
;
2494 union samr_DispInfo info
;
2498 get_query_dispinfo_params(
2499 loop_count
, &max_entries
, &max_size
);
2501 result
= rpccli_samr_QueryDisplayInfo(pipe_hnd
, mem_ctx
,
2510 num_entries
= info
.info3
.count
;
2511 start_idx
+= info
.info3
.count
;
2513 if (!NT_STATUS_IS_OK(result
) &&
2514 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2517 for (i
= 0; i
< num_entries
; i
++) {
2519 const char *group
= NULL
;
2520 const char *desc
= NULL
;
2522 group
= info
.info3
.entries
[i
].account_name
.string
;
2523 desc
= info
.info3
.entries
[i
].description
.string
;
2525 if (c
->opt_long_list_entries
)
2526 printf("%-21.21s %-50.50s\n",
2529 printf("%s\n", group
);
2531 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2532 /* query domain aliases */
2537 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
2543 if (!NT_STATUS_IS_OK(result
) &&
2544 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2547 for (i
= 0; i
< num_entries
; i
++) {
2549 const char *description
= NULL
;
2551 if (c
->opt_long_list_entries
) {
2553 POLICY_HND alias_pol
;
2554 union samr_AliasInfo
*info
= NULL
;
2556 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2559 groups
->entries
[i
].idx
,
2561 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd
, mem_ctx
,
2565 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd
, mem_ctx
,
2567 description
= info
->description
.string
;
2571 if (description
!= NULL
) {
2572 printf("%-21.21s %-50.50s\n",
2573 groups
->entries
[i
].name
.string
,
2576 printf("%s\n", groups
->entries
[i
].name
.string
);
2579 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2580 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
2581 /* Get builtin policy handle */
2583 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2585 MAXIMUM_ALLOWED_ACCESS
,
2586 CONST_DISCARD(struct dom_sid2
*, &global_sid_Builtin
),
2588 if (!NT_STATUS_IS_OK(result
)) {
2591 /* query builtin aliases */
2594 if (!builtin
) break;
2596 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
2602 if (!NT_STATUS_IS_OK(result
) &&
2603 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2606 for (i
= 0; i
< num_entries
; i
++) {
2608 const char *description
= NULL
;
2610 if (c
->opt_long_list_entries
) {
2612 POLICY_HND alias_pol
;
2613 union samr_AliasInfo
*info
= NULL
;
2615 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2618 groups
->entries
[i
].idx
,
2620 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd
, mem_ctx
,
2624 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd
, mem_ctx
,
2626 description
= info
->description
.string
;
2630 if (description
!= NULL
) {
2631 printf("%-21.21s %-50.50s\n",
2632 groups
->entries
[i
].name
.string
,
2635 printf("%s\n", groups
->entries
[i
].name
.string
);
2638 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2644 static int rpc_group_list(struct net_context
*c
, int argc
, const char **argv
)
2646 return run_rpc_command(c
, NULL
, PI_SAMR
, 0,
2647 rpc_group_list_internals
,
2651 static NTSTATUS
rpc_list_group_members(struct net_context
*c
,
2652 struct rpc_pipe_client
*pipe_hnd
,
2653 TALLOC_CTX
*mem_ctx
,
2654 const char *domain_name
,
2655 const DOM_SID
*domain_sid
,
2656 POLICY_HND
*domain_pol
,
2660 POLICY_HND group_pol
;
2661 uint32 num_members
, *group_rids
;
2663 struct samr_RidTypeArray
*rids
= NULL
;
2664 struct lsa_Strings names
;
2665 struct samr_Ids types
;
2668 sid_to_fstring(sid_str
, domain_sid
);
2670 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2672 MAXIMUM_ALLOWED_ACCESS
,
2676 if (!NT_STATUS_IS_OK(result
))
2679 result
= rpccli_samr_QueryGroupMember(pipe_hnd
, mem_ctx
,
2683 if (!NT_STATUS_IS_OK(result
))
2686 num_members
= rids
->count
;
2687 group_rids
= rids
->rids
;
2689 while (num_members
> 0) {
2690 int this_time
= 512;
2692 if (num_members
< this_time
)
2693 this_time
= num_members
;
2695 result
= rpccli_samr_LookupRids(pipe_hnd
, mem_ctx
,
2702 if (!NT_STATUS_IS_OK(result
))
2705 /* We only have users as members, but make the output
2706 the same as the output of alias members */
2708 for (i
= 0; i
< this_time
; i
++) {
2710 if (c
->opt_long_list_entries
) {
2711 printf("%s-%d %s\\%s %d\n", sid_str
,
2712 group_rids
[i
], domain_name
,
2713 names
.names
[i
].string
,
2716 printf("%s\\%s\n", domain_name
,
2717 names
.names
[i
].string
);
2721 num_members
-= this_time
;
2725 return NT_STATUS_OK
;
2728 static NTSTATUS
rpc_list_alias_members(struct net_context
*c
,
2729 struct rpc_pipe_client
*pipe_hnd
,
2730 TALLOC_CTX
*mem_ctx
,
2731 POLICY_HND
*domain_pol
,
2735 struct rpc_pipe_client
*lsa_pipe
;
2736 POLICY_HND alias_pol
, lsa_pol
;
2738 DOM_SID
*alias_sids
;
2741 enum lsa_SidType
*types
;
2743 struct lsa_SidArray sid_array
;
2745 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2747 MAXIMUM_ALLOWED_ACCESS
,
2751 if (!NT_STATUS_IS_OK(result
))
2754 result
= rpccli_samr_GetMembersInAlias(pipe_hnd
, mem_ctx
,
2758 if (!NT_STATUS_IS_OK(result
)) {
2759 d_fprintf(stderr
, "Couldn't list alias members\n");
2763 num_members
= sid_array
.num_sids
;
2765 if (num_members
== 0) {
2766 return NT_STATUS_OK
;
2769 lsa_pipe
= cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd
),
2770 PI_LSARPC
, &result
);
2772 d_fprintf(stderr
, "Couldn't open LSA pipe. Error was %s\n",
2773 nt_errstr(result
) );
2777 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, true,
2778 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
2780 if (!NT_STATUS_IS_OK(result
)) {
2781 d_fprintf(stderr
, "Couldn't open LSA policy handle\n");
2782 TALLOC_FREE(lsa_pipe
);
2786 alias_sids
= TALLOC_ZERO_ARRAY(mem_ctx
, DOM_SID
, num_members
);
2788 d_fprintf(stderr
, "Out of memory\n");
2789 TALLOC_FREE(lsa_pipe
);
2790 return NT_STATUS_NO_MEMORY
;
2793 for (i
=0; i
<num_members
; i
++) {
2794 sid_copy(&alias_sids
[i
], sid_array
.sids
[i
].sid
);
2797 result
= rpccli_lsa_lookup_sids(lsa_pipe
, mem_ctx
, &lsa_pol
,
2798 num_members
, alias_sids
,
2799 &domains
, &names
, &types
);
2801 if (!NT_STATUS_IS_OK(result
) &&
2802 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
2803 d_fprintf(stderr
, "Couldn't lookup SIDs\n");
2804 TALLOC_FREE(lsa_pipe
);
2808 for (i
= 0; i
< num_members
; i
++) {
2810 sid_to_fstring(sid_str
, &alias_sids
[i
]);
2812 if (c
->opt_long_list_entries
) {
2813 printf("%s %s\\%s %d\n", sid_str
,
2814 domains
[i
] ? domains
[i
] : "*unknown*",
2815 names
[i
] ? names
[i
] : "*unknown*", types
[i
]);
2818 printf("%s\\%s\n", domains
[i
], names
[i
]);
2820 printf("%s\n", sid_str
);
2824 TALLOC_FREE(lsa_pipe
);
2825 return NT_STATUS_OK
;
2828 static NTSTATUS
rpc_group_members_internals(struct net_context
*c
,
2829 const DOM_SID
*domain_sid
,
2830 const char *domain_name
,
2831 struct cli_state
*cli
,
2832 struct rpc_pipe_client
*pipe_hnd
,
2833 TALLOC_CTX
*mem_ctx
,
2838 POLICY_HND connect_pol
, domain_pol
;
2839 struct samr_Ids rids
, rid_types
;
2840 struct lsa_String lsa_acct_name
;
2842 /* Get sam policy handle */
2844 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2846 MAXIMUM_ALLOWED_ACCESS
,
2849 if (!NT_STATUS_IS_OK(result
))
2852 /* Get domain policy handle */
2854 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2856 MAXIMUM_ALLOWED_ACCESS
,
2857 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2860 if (!NT_STATUS_IS_OK(result
))
2863 init_lsa_String(&lsa_acct_name
, argv
[0]); /* sure? */
2865 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2872 if (!NT_STATUS_IS_OK(result
)) {
2874 /* Ok, did not find it in the global sam, try with builtin */
2876 DOM_SID sid_Builtin
;
2878 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
2880 sid_copy(&sid_Builtin
, &global_sid_Builtin
);
2882 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2884 MAXIMUM_ALLOWED_ACCESS
,
2888 if (!NT_STATUS_IS_OK(result
)) {
2889 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2893 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2900 if (!NT_STATUS_IS_OK(result
)) {
2901 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2906 if (rids
.count
!= 1) {
2907 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2911 if (rid_types
.ids
[0] == SID_NAME_DOM_GRP
) {
2912 return rpc_list_group_members(c
, pipe_hnd
, mem_ctx
, domain_name
,
2913 domain_sid
, &domain_pol
,
2917 if (rid_types
.ids
[0] == SID_NAME_ALIAS
) {
2918 return rpc_list_alias_members(c
, pipe_hnd
, mem_ctx
, &domain_pol
,
2922 return NT_STATUS_NO_SUCH_GROUP
;
2925 static int rpc_group_members(struct net_context
*c
, int argc
, const char **argv
)
2928 return rpc_group_usage(c
, argc
, argv
);
2931 return run_rpc_command(c
, NULL
, PI_SAMR
, 0,
2932 rpc_group_members_internals
,
2936 static NTSTATUS
rpc_group_rename_internals(struct net_context
*c
,
2937 const DOM_SID
*domain_sid
,
2938 const char *domain_name
,
2939 struct cli_state
*cli
,
2940 struct rpc_pipe_client
*pipe_hnd
,
2941 TALLOC_CTX
*mem_ctx
,
2946 POLICY_HND connect_pol
, domain_pol
, group_pol
;
2947 union samr_GroupInfo group_info
;
2948 struct samr_Ids rids
, rid_types
;
2949 struct lsa_String lsa_acct_name
;
2952 d_printf("Usage: 'net rpc group rename group newname'\n");
2953 return NT_STATUS_UNSUCCESSFUL
;
2956 /* Get sam policy handle */
2958 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2960 MAXIMUM_ALLOWED_ACCESS
,
2963 if (!NT_STATUS_IS_OK(result
))
2966 /* Get domain policy handle */
2968 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2970 MAXIMUM_ALLOWED_ACCESS
,
2971 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2974 if (!NT_STATUS_IS_OK(result
))
2977 init_lsa_String(&lsa_acct_name
, argv
[0]);
2979 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2986 if (rids
.count
!= 1) {
2987 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2991 if (rid_types
.ids
[0] != SID_NAME_DOM_GRP
) {
2992 d_fprintf(stderr
, "Can only rename domain groups\n");
2993 return NT_STATUS_UNSUCCESSFUL
;
2996 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2998 MAXIMUM_ALLOWED_ACCESS
,
3002 if (!NT_STATUS_IS_OK(result
))
3005 init_lsa_String(&group_info
.name
, argv
[1]);
3007 result
= rpccli_samr_SetGroupInfo(pipe_hnd
, mem_ctx
,
3012 if (!NT_STATUS_IS_OK(result
))
3015 return NT_STATUS_NO_SUCH_GROUP
;
3018 static int rpc_group_rename(struct net_context
*c
, int argc
, const char **argv
)
3021 return rpc_group_usage(c
, argc
, argv
);
3024 return run_rpc_command(c
, NULL
, PI_SAMR
, 0,
3025 rpc_group_rename_internals
,
3030 * 'net rpc group' entrypoint.
3031 * @param argc Standard main() style argc
3032 * @param argc Standard main() style argv. Initial components are already
3036 int net_rpc_group(struct net_context
*c
, int argc
, const char **argv
)
3038 struct functable func
[] = {
3039 {"add", rpc_group_add
},
3040 {"delete", rpc_group_delete
},
3041 {"addmem", rpc_group_addmem
},
3042 {"delmem", rpc_group_delmem
},
3043 {"list", rpc_group_list
},
3044 {"members", rpc_group_members
},
3045 {"rename", rpc_group_rename
},
3050 return run_rpc_command(c
, NULL
, PI_SAMR
, 0,
3051 rpc_group_list_internals
,
3055 return net_run_function(c
, argc
, argv
, func
, rpc_group_usage
);
3058 /****************************************************************************/
3060 static int rpc_share_usage(struct net_context
*c
, int argc
, const char **argv
)
3062 return net_share_usage(c
, argc
, argv
);
3066 * Add a share on a remote RPC server
3068 * All parameters are provided by the run_rpc_command function, except for
3069 * argc, argv which are passes through.
3071 * @param domain_sid The domain sid acquired from the remote server
3072 * @param cli A cli_state connected to the server.
3073 * @param mem_ctx Talloc context, destoyed on completion of the function.
3074 * @param argc Standard main() style argc
3075 * @param argv Standard main() style argv. Initial components are already
3078 * @return Normal NTSTATUS return.
3080 static NTSTATUS
rpc_share_add_internals(struct net_context
*c
,
3081 const DOM_SID
*domain_sid
,
3082 const char *domain_name
,
3083 struct cli_state
*cli
,
3084 struct rpc_pipe_client
*pipe_hnd
,
3085 TALLOC_CTX
*mem_ctx
,int argc
,
3092 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
3093 uint32 num_users
=0, perms
=0;
3094 char *password
=NULL
; /* don't allow a share password */
3096 union srvsvc_NetShareInfo info
;
3097 struct srvsvc_NetShareInfo2 info2
;
3098 uint32_t parm_error
= 0;
3100 if ((sharename
= talloc_strdup(mem_ctx
, argv
[0])) == NULL
) {
3101 return NT_STATUS_NO_MEMORY
;
3104 path
= strchr(sharename
, '=');
3106 return NT_STATUS_UNSUCCESSFUL
;
3109 info2
.name
= sharename
;
3111 info2
.comment
= c
->opt_comment
;
3112 info2
.permissions
= perms
;
3113 info2
.max_users
= c
->opt_maxusers
;
3114 info2
.current_users
= num_users
;
3116 info2
.password
= password
;
3118 info
.info2
= &info2
;
3120 status
= rpccli_srvsvc_NetShareAdd(pipe_hnd
, mem_ctx
,
3129 static int rpc_share_add(struct net_context
*c
, int argc
, const char **argv
)
3131 if ((argc
< 1) || !strchr(argv
[0], '=')) {
3132 DEBUG(1,("Sharename or path not specified on add\n"));
3133 return rpc_share_usage(c
, argc
, argv
);
3135 return run_rpc_command(c
, NULL
, PI_SRVSVC
, 0,
3136 rpc_share_add_internals
,
3141 * Delete a share on a remote RPC server
3143 * All parameters are provided by the run_rpc_command function, except for
3144 * argc, argv which are passes through.
3146 * @param domain_sid The domain sid acquired from the remote server
3147 * @param cli A cli_state connected to the server.
3148 * @param mem_ctx Talloc context, destoyed on completion of the function.
3149 * @param argc Standard main() style argc
3150 * @param argv Standard main() style argv. Initial components are already
3153 * @return Normal NTSTATUS return.
3155 static NTSTATUS
rpc_share_del_internals(struct net_context
*c
,
3156 const DOM_SID
*domain_sid
,
3157 const char *domain_name
,
3158 struct cli_state
*cli
,
3159 struct rpc_pipe_client
*pipe_hnd
,
3160 TALLOC_CTX
*mem_ctx
,
3166 return rpccli_srvsvc_NetShareDel(pipe_hnd
, mem_ctx
,
3174 * Delete a share on a remote RPC server
3176 * @param domain_sid The domain sid acquired from the remote server
3177 * @param argc Standard main() style argc
3178 * @param argv Standard main() style argv. Initial components are already
3181 * @return A shell status integer (0 for success)
3183 static int rpc_share_delete(struct net_context
*c
, int argc
, const char **argv
)
3186 DEBUG(1,("Sharename not specified on delete\n"));
3187 return rpc_share_usage(c
, argc
, argv
);
3189 return run_rpc_command(c
, NULL
, PI_SRVSVC
, 0,
3190 rpc_share_del_internals
,
3195 * Formatted print of share info
3197 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3200 static void display_share_info_1(struct net_context
*c
,
3201 struct srvsvc_NetShareInfo1
*r
)
3203 if (c
->opt_long_list_entries
) {
3204 d_printf("%-12s %-8.8s %-50s\n",
3206 c
->share_type
[r
->type
& ~(STYPE_TEMPORARY
|STYPE_HIDDEN
)],
3209 d_printf("%s\n", r
->name
);
3213 static WERROR
get_share_info(struct net_context
*c
,
3214 struct rpc_pipe_client
*pipe_hnd
,
3215 TALLOC_CTX
*mem_ctx
,
3219 struct srvsvc_NetShareInfoCtr
*info_ctr
)
3223 union srvsvc_NetShareInfo info
;
3225 /* no specific share requested, enumerate all */
3228 uint32_t preferred_len
= 0xffffffff;
3229 uint32_t total_entries
= 0;
3230 uint32_t resume_handle
= 0;
3232 info_ctr
->level
= level
;
3234 status
= rpccli_srvsvc_NetShareEnumAll(pipe_hnd
, mem_ctx
,
3244 /* request just one share */
3245 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
3252 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
3257 ZERO_STRUCTP(info_ctr
);
3259 info_ctr
->level
= level
;
3264 struct srvsvc_NetShareCtr1
*ctr1
;
3266 ctr1
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr1
);
3267 W_ERROR_HAVE_NO_MEMORY(ctr1
);
3270 ctr1
->array
= info
.info1
;
3272 info_ctr
->ctr
.ctr1
= ctr1
;
3276 struct srvsvc_NetShareCtr2
*ctr2
;
3278 ctr2
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr2
);
3279 W_ERROR_HAVE_NO_MEMORY(ctr2
);
3282 ctr2
->array
= info
.info2
;
3284 info_ctr
->ctr
.ctr2
= ctr2
;
3288 struct srvsvc_NetShareCtr502
*ctr502
;
3290 ctr502
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr502
);
3291 W_ERROR_HAVE_NO_MEMORY(ctr502
);
3294 ctr502
->array
= info
.info502
;
3296 info_ctr
->ctr
.ctr502
= ctr502
;
3304 * List shares on a remote RPC server
3306 * All parameters are provided by the run_rpc_command function, except for
3307 * argc, argv which are passes through.
3309 * @param domain_sid The domain sid acquired from the remote server
3310 * @param cli A cli_state connected to the server.
3311 * @param mem_ctx Talloc context, destoyed on completion of the function.
3312 * @param argc Standard main() style argc
3313 * @param argv Standard main() style argv. Initial components are already
3316 * @return Normal NTSTATUS return.
3319 static NTSTATUS
rpc_share_list_internals(struct net_context
*c
,
3320 const DOM_SID
*domain_sid
,
3321 const char *domain_name
,
3322 struct cli_state
*cli
,
3323 struct rpc_pipe_client
*pipe_hnd
,
3324 TALLOC_CTX
*mem_ctx
,
3328 struct srvsvc_NetShareInfoCtr info_ctr
;
3329 struct srvsvc_NetShareCtr1 ctr1
;
3331 uint32 i
, level
= 1;
3333 ZERO_STRUCT(info_ctr
);
3337 info_ctr
.ctr
.ctr1
= &ctr1
;
3339 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3341 if (!W_ERROR_IS_OK(result
))
3344 /* Display results */
3346 if (c
->opt_long_list_entries
) {
3348 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3349 "\nShare name Type Description\n"\
3350 "---------- ---- -----------\n");
3352 for (i
= 0; i
< info_ctr
.ctr
.ctr1
->count
; i
++)
3353 display_share_info_1(c
, &info_ctr
.ctr
.ctr1
->array
[i
]);
3355 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3359 * 'net rpc share list' entrypoint.
3360 * @param argc Standard main() style argc
3361 * @param argv Standard main() style argv. Initial components are already
3364 static int rpc_share_list(struct net_context
*c
, int argc
, const char **argv
)
3366 return run_rpc_command(c
, NULL
, PI_SRVSVC
, 0, rpc_share_list_internals
,
3370 static bool check_share_availability(struct cli_state
*cli
, const char *netname
)
3372 if (!cli_send_tconX(cli
, netname
, "A:", "", 0)) {
3373 d_printf("skipping [%s]: not a file share.\n", netname
);
3383 static bool check_share_sanity(struct net_context
*c
, struct cli_state
*cli
,
3384 const char *netname
, uint32 type
)
3386 /* only support disk shares */
3387 if (! ( type
== STYPE_DISKTREE
|| type
== (STYPE_DISKTREE
| STYPE_HIDDEN
)) ) {
3388 printf("share [%s] is not a diskshare (type: %x)\n", netname
, type
);
3392 /* skip builtin shares */
3393 /* FIXME: should print$ be added too ? */
3394 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$") ||
3395 strequal(netname
,"global"))
3398 if (c
->opt_exclude
&& in_list(netname
, c
->opt_exclude
, false)) {
3399 printf("excluding [%s]\n", netname
);
3403 return check_share_availability(cli
, netname
);
3407 * Migrate shares from a remote RPC server to the local RPC server
3409 * All parameters are provided by the run_rpc_command function, except for
3410 * argc, argv which are passed through.
3412 * @param domain_sid The domain sid acquired from the remote server
3413 * @param cli A cli_state connected to the server.
3414 * @param mem_ctx Talloc context, destroyed on completion of the function.
3415 * @param argc Standard main() style argc
3416 * @param argv Standard main() style argv. Initial components are already
3419 * @return Normal NTSTATUS return.
3422 static NTSTATUS
rpc_share_migrate_shares_internals(struct net_context
*c
,
3423 const DOM_SID
*domain_sid
,
3424 const char *domain_name
,
3425 struct cli_state
*cli
,
3426 struct rpc_pipe_client
*pipe_hnd
,
3427 TALLOC_CTX
*mem_ctx
,
3432 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3433 struct srvsvc_NetShareInfoCtr ctr_src
;
3435 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3436 struct cli_state
*cli_dst
= NULL
;
3437 uint32 level
= 502; /* includes secdesc */
3438 uint32_t parm_error
= 0;
3440 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3442 if (!W_ERROR_IS_OK(result
))
3445 /* connect destination PI_SRVSVC */
3446 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
, PI_SRVSVC
);
3447 if (!NT_STATUS_IS_OK(nt_status
))
3451 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3453 union srvsvc_NetShareInfo info
;
3454 struct srvsvc_NetShareInfo502 info502
=
3455 ctr_src
.ctr
.ctr502
->array
[i
];
3457 /* reset error-code */
3458 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3460 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3463 /* finally add the share on the dst server */
3465 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3466 info502
.name
, info502
.path
, info502
.comment
);
3468 info
.info502
= &info502
;
3470 nt_status
= rpccli_srvsvc_NetShareAdd(srvsvc_pipe
, mem_ctx
,
3471 srvsvc_pipe
->desthost
,
3477 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ALREADY_EXISTS
)) {
3478 printf(" [%s] does already exist\n",
3483 if (!NT_STATUS_IS_OK(nt_status
) || !W_ERROR_IS_OK(result
)) {
3484 printf("cannot add share: %s\n", dos_errstr(result
));
3490 nt_status
= NT_STATUS_OK
;
3494 cli_shutdown(cli_dst
);
3502 * Migrate shares from a rpc-server to another
3504 * @param argc Standard main() style argc
3505 * @param argv Standard main() style argv. Initial components are already
3508 * @return A shell status integer (0 for success)
3510 static int rpc_share_migrate_shares(struct net_context
*c
, int argc
,
3515 printf("no server to migrate\n");
3519 return run_rpc_command(c
, NULL
, PI_SRVSVC
, 0,
3520 rpc_share_migrate_shares_internals
,
3527 * @param f file_info
3528 * @param mask current search mask
3529 * @param state arg-pointer
3532 static void copy_fn(const char *mnt
, file_info
*f
,
3533 const char *mask
, void *state
)
3535 static NTSTATUS nt_status
;
3536 static struct copy_clistate
*local_state
;
3537 static fstring filename
, new_mask
;
3540 struct net_context
*c
;
3542 local_state
= (struct copy_clistate
*)state
;
3543 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3547 if (strequal(f
->name
, ".") || strequal(f
->name
, ".."))
3550 DEBUG(3,("got mask: %s, name: %s\n", mask
, f
->name
));
3553 if (f
->mode
& aDIR
) {
3555 DEBUG(3,("got dir: %s\n", f
->name
));
3557 fstrcpy(dir
, local_state
->cwd
);
3559 fstrcat(dir
, f
->name
);
3561 switch (net_mode_share
)
3563 case NET_MODE_SHARE_MIGRATE
:
3564 /* create that directory */
3565 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3566 local_state
->cli_share_src
,
3567 local_state
->cli_share_dst
,
3569 c
->opt_acls
? true : false,
3570 c
->opt_attrs
? true : false,
3571 c
->opt_timestamps
? true:false,
3575 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3579 if (!NT_STATUS_IS_OK(nt_status
))
3580 printf("could not handle dir %s: %s\n",
3581 dir
, nt_errstr(nt_status
));
3583 /* search below that directory */
3584 fstrcpy(new_mask
, dir
);
3585 fstrcat(new_mask
, "\\*");
3587 old_dir
= local_state
->cwd
;
3588 local_state
->cwd
= dir
;
3589 if (!sync_files(local_state
, new_mask
))
3590 printf("could not handle files\n");
3591 local_state
->cwd
= old_dir
;
3598 fstrcpy(filename
, local_state
->cwd
);
3599 fstrcat(filename
, "\\");
3600 fstrcat(filename
, f
->name
);
3602 DEBUG(3,("got file: %s\n", filename
));
3604 switch (net_mode_share
)
3606 case NET_MODE_SHARE_MIGRATE
:
3607 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3608 local_state
->cli_share_src
,
3609 local_state
->cli_share_dst
,
3611 c
->opt_acls
? true : false,
3612 c
->opt_attrs
? true : false,
3613 c
->opt_timestamps
? true: false,
3617 d_fprintf(stderr
, "Unsupported file mode %d\n", net_mode_share
);
3621 if (!NT_STATUS_IS_OK(nt_status
))
3622 printf("could not handle file %s: %s\n",
3623 filename
, nt_errstr(nt_status
));
3628 * sync files, can be called recursivly to list files
3629 * and then call copy_fn for each file
3631 * @param cp_clistate pointer to the copy_clistate we work with
3632 * @param mask the current search mask
3634 * @return Boolean result
3636 static bool sync_files(struct copy_clistate
*cp_clistate
, const char *mask
)
3638 struct cli_state
*targetcli
;
3639 char *targetpath
= NULL
;
3641 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
3643 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate
->cli_share_src
,
3644 mask
, &targetcli
, &targetpath
) ) {
3645 d_fprintf(stderr
, "cli_resolve_path %s failed with error: %s\n",
3646 mask
, cli_errstr(cp_clistate
->cli_share_src
));
3650 if (cli_list(targetcli
, targetpath
, cp_clistate
->attribute
, copy_fn
, cp_clistate
) == -1) {
3651 d_fprintf(stderr
, "listing %s failed with error: %s\n",
3652 mask
, cli_errstr(targetcli
));
3661 * Set the top level directory permissions before we do any further copies.
3662 * Should set up ACL inheritance.
3665 bool copy_top_level_perms(struct net_context
*c
,
3666 struct copy_clistate
*cp_clistate
,
3667 const char *sharename
)
3669 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3671 switch (net_mode_share
) {
3672 case NET_MODE_SHARE_MIGRATE
:
3673 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename
));
3674 nt_status
= net_copy_fileattr(c
,
3675 cp_clistate
->mem_ctx
,
3676 cp_clistate
->cli_share_src
,
3677 cp_clistate
->cli_share_dst
,
3679 c
->opt_acls
? true : false,
3680 c
->opt_attrs
? true : false,
3681 c
->opt_timestamps
? true: false,
3685 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3689 if (!NT_STATUS_IS_OK(nt_status
)) {
3690 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3691 sharename
, nt_errstr(nt_status
));
3699 * Sync all files inside a remote share to another share (over smb)
3701 * All parameters are provided by the run_rpc_command function, except for
3702 * argc, argv which are passes through.
3704 * @param domain_sid The domain sid acquired from the remote server
3705 * @param cli A cli_state connected to the server.
3706 * @param mem_ctx Talloc context, destoyed on completion of the function.
3707 * @param argc Standard main() style argc
3708 * @param argv Standard main() style argv. Initial components are already
3711 * @return Normal NTSTATUS return.
3714 static NTSTATUS
rpc_share_migrate_files_internals(struct net_context
*c
,
3715 const DOM_SID
*domain_sid
,
3716 const char *domain_name
,
3717 struct cli_state
*cli
,
3718 struct rpc_pipe_client
*pipe_hnd
,
3719 TALLOC_CTX
*mem_ctx
,
3724 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3725 struct srvsvc_NetShareInfoCtr ctr_src
;
3728 struct copy_clistate cp_clistate
;
3729 bool got_src_share
= false;
3730 bool got_dst_share
= false;
3731 const char *mask
= "\\*";
3734 dst
= SMB_STRDUP(c
->opt_destination
?c
->opt_destination
:"127.0.0.1");
3736 nt_status
= NT_STATUS_NO_MEMORY
;
3740 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3743 if (!W_ERROR_IS_OK(result
))
3746 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3748 struct srvsvc_NetShareInfo502 info502
=
3749 ctr_src
.ctr
.ctr502
->array
[i
];
3751 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3754 /* one might not want to mirror whole discs :) */
3755 if (strequal(info502
.name
, "print$") || info502
.name
[1] == '$') {
3756 d_printf("skipping [%s]: builtin/hidden share\n", info502
.name
);
3760 switch (net_mode_share
)
3762 case NET_MODE_SHARE_MIGRATE
:
3766 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3769 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3771 c
->opt_acls
? "including" : "without",
3772 c
->opt_attrs
? "including" : "without",
3773 c
->opt_timestamps
? "(preserving timestamps)" : "");
3775 cp_clistate
.mem_ctx
= mem_ctx
;
3776 cp_clistate
.cli_share_src
= NULL
;
3777 cp_clistate
.cli_share_dst
= NULL
;
3778 cp_clistate
.cwd
= NULL
;
3779 cp_clistate
.attribute
= aSYSTEM
| aHIDDEN
| aDIR
;
3782 /* open share source */
3783 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_src
,
3784 &cli
->dest_ss
, cli
->desthost
,
3785 info502
.name
, "A:");
3786 if (!NT_STATUS_IS_OK(nt_status
))
3789 got_src_share
= true;
3791 if (net_mode_share
== NET_MODE_SHARE_MIGRATE
) {
3792 /* open share destination */
3793 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_dst
,
3794 NULL
, dst
, info502
.name
, "A:");
3795 if (!NT_STATUS_IS_OK(nt_status
))
3798 got_dst_share
= true;
3801 if (!copy_top_level_perms(c
, &cp_clistate
, info502
.name
)) {
3802 d_fprintf(stderr
, "Could not handle the top level directory permissions for the share: %s\n", info502
.name
);
3803 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3807 if (!sync_files(&cp_clistate
, mask
)) {
3808 d_fprintf(stderr
, "could not handle files for share: %s\n", info502
.name
);
3809 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3814 nt_status
= NT_STATUS_OK
;
3819 cli_shutdown(cp_clistate
.cli_share_src
);
3822 cli_shutdown(cp_clistate
.cli_share_dst
);
3829 static int rpc_share_migrate_files(struct net_context
*c
, int argc
, const char **argv
)
3833 printf("no server to migrate\n");
3837 return run_rpc_command(c
, NULL
, PI_SRVSVC
, 0,
3838 rpc_share_migrate_files_internals
,
3843 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3845 * All parameters are provided by the run_rpc_command function, except for
3846 * argc, argv which are passes through.
3848 * @param domain_sid The domain sid acquired from the remote server
3849 * @param cli A cli_state connected to the server.
3850 * @param mem_ctx Talloc context, destoyed on completion of the function.
3851 * @param argc Standard main() style argc
3852 * @param argv Standard main() style argv. Initial components are already
3855 * @return Normal NTSTATUS return.
3858 static NTSTATUS
rpc_share_migrate_security_internals(struct net_context
*c
,
3859 const DOM_SID
*domain_sid
,
3860 const char *domain_name
,
3861 struct cli_state
*cli
,
3862 struct rpc_pipe_client
*pipe_hnd
,
3863 TALLOC_CTX
*mem_ctx
,
3868 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3869 struct srvsvc_NetShareInfoCtr ctr_src
;
3870 union srvsvc_NetShareInfo info
;
3872 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3873 struct cli_state
*cli_dst
= NULL
;
3874 uint32 level
= 502; /* includes secdesc */
3875 uint32_t parm_error
= 0;
3877 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3880 if (!W_ERROR_IS_OK(result
))
3883 /* connect destination PI_SRVSVC */
3884 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
, PI_SRVSVC
);
3885 if (!NT_STATUS_IS_OK(nt_status
))
3889 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3891 struct srvsvc_NetShareInfo502 info502
=
3892 ctr_src
.ctr
.ctr502
->array
[i
];
3894 /* reset error-code */
3895 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3897 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3900 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3901 info502
.name
, info502
.path
, info502
.comment
);
3904 display_sec_desc(info502
.sd_buf
.sd
);
3906 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3907 info
.info502
= &info502
;
3909 /* finally modify the share on the dst server */
3910 nt_status
= rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe
, mem_ctx
,
3911 srvsvc_pipe
->desthost
,
3917 if (!NT_STATUS_IS_OK(nt_status
) || !W_ERROR_IS_OK(result
)) {
3918 printf("cannot set share-acl: %s\n", dos_errstr(result
));
3924 nt_status
= NT_STATUS_OK
;
3928 cli_shutdown(cli_dst
);
3936 * Migrate share-acls from a rpc-server to another
3938 * @param argc Standard main() style argc
3939 * @param argv Standard main() style argv. Initial components are already
3942 * @return A shell status integer (0 for success)
3944 static int rpc_share_migrate_security(struct net_context
*c
, int argc
,
3949 printf("no server to migrate\n");
3953 return run_rpc_command(c
, NULL
, PI_SRVSVC
, 0,
3954 rpc_share_migrate_security_internals
,
3959 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3960 * from one server to another
3962 * @param argc Standard main() style argc
3963 * @param argv Standard main() style argv. Initial components are already
3966 * @return A shell status integer (0 for success)
3969 static int rpc_share_migrate_all(struct net_context
*c
, int argc
,
3975 printf("no server to migrate\n");
3979 /* order is important. we don't want to be locked out by the share-acl
3980 * before copying files - gd */
3982 ret
= run_rpc_command(c
, NULL
, PI_SRVSVC
, 0,
3983 rpc_share_migrate_shares_internals
, argc
, argv
);
3987 ret
= run_rpc_command(c
, NULL
, PI_SRVSVC
, 0,
3988 rpc_share_migrate_files_internals
, argc
, argv
);
3992 return run_rpc_command(c
, NULL
, PI_SRVSVC
, 0,
3993 rpc_share_migrate_security_internals
, argc
,
3999 * 'net rpc share migrate' entrypoint.
4000 * @param argc Standard main() style argc
4001 * @param argv Standard main() style argv. Initial components are already
4004 static int rpc_share_migrate(struct net_context
*c
, int argc
, const char **argv
)
4007 struct functable func
[] = {
4008 {"all", rpc_share_migrate_all
},
4009 {"files", rpc_share_migrate_files
},
4010 {"help", rpc_share_usage
},
4011 {"security", rpc_share_migrate_security
},
4012 {"shares", rpc_share_migrate_shares
},
4016 net_mode_share
= NET_MODE_SHARE_MIGRATE
;
4018 return net_run_function(c
, argc
, argv
, func
, rpc_share_usage
);
4027 static int num_server_aliases
;
4028 static struct full_alias
*server_aliases
;
4031 * Add an alias to the static list.
4033 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
4035 if (server_aliases
== NULL
)
4036 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
4038 server_aliases
[num_server_aliases
] = *alias
;
4039 num_server_aliases
+= 1;
4043 * For a specific domain on the server, fetch all the aliases
4044 * and their members. Add all of them to the server_aliases.
4047 static NTSTATUS
rpc_fetch_domain_aliases(struct rpc_pipe_client
*pipe_hnd
,
4048 TALLOC_CTX
*mem_ctx
,
4049 POLICY_HND
*connect_pol
,
4050 const DOM_SID
*domain_sid
)
4052 uint32 start_idx
, max_entries
, num_entries
, i
;
4053 struct samr_SamArray
*groups
= NULL
;
4055 POLICY_HND domain_pol
;
4057 /* Get domain policy handle */
4059 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
4061 MAXIMUM_ALLOWED_ACCESS
,
4062 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
4064 if (!NT_STATUS_IS_OK(result
))
4071 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
4077 for (i
= 0; i
< num_entries
; i
++) {
4079 POLICY_HND alias_pol
;
4080 struct full_alias alias
;
4081 struct lsa_SidArray sid_array
;
4084 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
4086 MAXIMUM_ALLOWED_ACCESS
,
4087 groups
->entries
[i
].idx
,
4089 if (!NT_STATUS_IS_OK(result
))
4092 result
= rpccli_samr_GetMembersInAlias(pipe_hnd
, mem_ctx
,
4095 if (!NT_STATUS_IS_OK(result
))
4098 alias
.num_members
= sid_array
.num_sids
;
4100 result
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &alias_pol
);
4101 if (!NT_STATUS_IS_OK(result
))
4104 alias
.members
= NULL
;
4106 if (alias
.num_members
> 0) {
4107 alias
.members
= SMB_MALLOC_ARRAY(DOM_SID
, alias
.num_members
);
4109 for (j
= 0; j
< alias
.num_members
; j
++)
4110 sid_copy(&alias
.members
[j
],
4111 sid_array
.sids
[j
].sid
);
4114 sid_copy(&alias
.sid
, domain_sid
);
4115 sid_append_rid(&alias
.sid
, groups
->entries
[i
].idx
);
4117 push_alias(mem_ctx
, &alias
);
4119 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
4121 result
= NT_STATUS_OK
;
4124 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
4130 * Dump server_aliases as names for debugging purposes.
4133 static NTSTATUS
rpc_aliaslist_dump(struct net_context
*c
,
4134 const DOM_SID
*domain_sid
,
4135 const char *domain_name
,
4136 struct cli_state
*cli
,
4137 struct rpc_pipe_client
*pipe_hnd
,
4138 TALLOC_CTX
*mem_ctx
,
4146 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
4147 SEC_RIGHTS_MAXIMUM_ALLOWED
,
4149 if (!NT_STATUS_IS_OK(result
))
4152 for (i
=0; i
<num_server_aliases
; i
++) {
4155 enum lsa_SidType
*types
;
4158 struct full_alias
*alias
= &server_aliases
[i
];
4160 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
4162 &domains
, &names
, &types
);
4163 if (!NT_STATUS_IS_OK(result
))
4166 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
4168 if (alias
->num_members
== 0) {
4173 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
,
4176 &domains
, &names
, &types
);
4178 if (!NT_STATUS_IS_OK(result
) &&
4179 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
4182 for (j
=0; j
<alias
->num_members
; j
++)
4183 DEBUG(1, ("%s\\%s (%d); ",
4184 domains
[j
] ? domains
[j
] : "*unknown*",
4185 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
4189 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
4191 return NT_STATUS_OK
;
4195 * Fetch a list of all server aliases and their members into
4199 static NTSTATUS
rpc_aliaslist_internals(struct net_context
*c
,
4200 const DOM_SID
*domain_sid
,
4201 const char *domain_name
,
4202 struct cli_state
*cli
,
4203 struct rpc_pipe_client
*pipe_hnd
,
4204 TALLOC_CTX
*mem_ctx
,
4209 POLICY_HND connect_pol
;
4211 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
4213 MAXIMUM_ALLOWED_ACCESS
,
4216 if (!NT_STATUS_IS_OK(result
))
4219 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4220 &global_sid_Builtin
);
4222 if (!NT_STATUS_IS_OK(result
))
4225 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4228 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
4233 static void init_user_token(NT_USER_TOKEN
*token
, DOM_SID
*user_sid
)
4235 token
->num_sids
= 4;
4237 if (!(token
->user_sids
= SMB_MALLOC_ARRAY(DOM_SID
, 4))) {
4238 d_fprintf(stderr
, "malloc failed\n");
4239 token
->num_sids
= 0;
4243 token
->user_sids
[0] = *user_sid
;
4244 sid_copy(&token
->user_sids
[1], &global_sid_World
);
4245 sid_copy(&token
->user_sids
[2], &global_sid_Network
);
4246 sid_copy(&token
->user_sids
[3], &global_sid_Authenticated_Users
);
4249 static void free_user_token(NT_USER_TOKEN
*token
)
4251 SAFE_FREE(token
->user_sids
);
4254 static bool is_sid_in_token(NT_USER_TOKEN
*token
, DOM_SID
*sid
)
4258 for (i
=0; i
<token
->num_sids
; i
++) {
4259 if (sid_compare(sid
, &token
->user_sids
[i
]) == 0)
4265 static void add_sid_to_token(NT_USER_TOKEN
*token
, DOM_SID
*sid
)
4267 if (is_sid_in_token(token
, sid
))
4270 token
->user_sids
= SMB_REALLOC_ARRAY(token
->user_sids
, DOM_SID
, token
->num_sids
+1);
4271 if (!token
->user_sids
) {
4275 sid_copy(&token
->user_sids
[token
->num_sids
], sid
);
4277 token
->num_sids
+= 1;
4282 NT_USER_TOKEN token
;
4285 static void dump_user_token(struct user_token
*token
)
4289 d_printf("%s\n", token
->name
);
4291 for (i
=0; i
<token
->token
.num_sids
; i
++) {
4292 d_printf(" %s\n", sid_string_tos(&token
->token
.user_sids
[i
]));
4296 static bool is_alias_member(DOM_SID
*sid
, struct full_alias
*alias
)
4300 for (i
=0; i
<alias
->num_members
; i
++) {
4301 if (sid_compare(sid
, &alias
->members
[i
]) == 0)
4308 static void collect_sid_memberships(NT_USER_TOKEN
*token
, DOM_SID sid
)
4312 for (i
=0; i
<num_server_aliases
; i
++) {
4313 if (is_alias_member(&sid
, &server_aliases
[i
]))
4314 add_sid_to_token(token
, &server_aliases
[i
].sid
);
4319 * We got a user token with all the SIDs we can know about without asking the
4320 * server directly. These are the user and domain group sids. All of these can
4321 * be members of aliases. So scan the list of aliases for each of the SIDs and
4322 * add them to the token.
4325 static void collect_alias_memberships(NT_USER_TOKEN
*token
)
4327 int num_global_sids
= token
->num_sids
;
4330 for (i
=0; i
<num_global_sids
; i
++) {
4331 collect_sid_memberships(token
, token
->user_sids
[i
]);
4335 static bool get_user_sids(const char *domain
, const char *user
, NT_USER_TOKEN
*token
)
4337 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4338 enum wbcSidType type
;
4340 struct wbcDomainSid wsid
;
4341 char *sid_str
= NULL
;
4343 uint32_t num_groups
;
4344 gid_t
*groups
= NULL
;
4347 fstr_sprintf(full_name
, "%s%c%s",
4348 domain
, *lp_winbind_separator(), user
);
4350 /* First let's find out the user sid */
4352 wbc_status
= wbcLookupName(domain
, user
, &wsid
, &type
);
4354 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4355 DEBUG(1, ("winbind could not find %s: %s\n",
4356 full_name
, wbcErrorString(wbc_status
)));
4360 wbc_status
= wbcSidToString(&wsid
, &sid_str
);
4361 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4365 if (type
!= SID_NAME_USER
) {
4366 wbcFreeMemory(sid_str
);
4367 DEBUG(1, ("%s is not a user\n", full_name
));
4371 string_to_sid(&user_sid
, sid_str
);
4372 wbcFreeMemory(sid_str
);
4375 init_user_token(token
, &user_sid
);
4377 /* And now the groups winbind knows about */
4379 wbc_status
= wbcGetGroups(full_name
, &num_groups
, &groups
);
4380 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4381 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4382 full_name
, wbcErrorString(wbc_status
)));
4386 for (i
= 0; i
< num_groups
; i
++) {
4387 gid_t gid
= groups
[i
];
4390 wbc_status
= wbcGidToSid(gid
, &wsid
);
4391 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4392 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4393 gid
, wbcErrorString(wbc_status
)));
4394 wbcFreeMemory(groups
);
4398 wbc_status
= wbcSidToString(&wsid
, &sid_str
);
4399 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4400 wbcFreeMemory(groups
);
4404 DEBUG(3, (" %s\n", sid_str
));
4406 string_to_sid(&sid
, sid_str
);
4407 wbcFreeMemory(sid_str
);
4410 add_sid_to_token(token
, &sid
);
4412 wbcFreeMemory(groups
);
4418 * Get a list of all user tokens we want to look at
4421 static bool get_user_tokens(struct net_context
*c
, int *num_tokens
,
4422 struct user_token
**user_tokens
)
4424 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4425 uint32_t i
, num_users
;
4427 struct user_token
*result
;
4428 TALLOC_CTX
*frame
= NULL
;
4430 if (lp_winbind_use_default_domain() &&
4431 (c
->opt_target_workgroup
== NULL
)) {
4432 d_fprintf(stderr
, "winbind use default domain = yes set, "
4433 "please specify a workgroup\n");
4437 /* Send request to winbind daemon */
4439 wbc_status
= wbcListUsers(NULL
, &num_users
, &users
);
4440 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4441 DEBUG(1, ("winbind could not list users: %s\n",
4442 wbcErrorString(wbc_status
)));
4446 result
= SMB_MALLOC_ARRAY(struct user_token
, num_users
);
4448 if (result
== NULL
) {
4449 DEBUG(1, ("Could not malloc sid array\n"));
4450 wbcFreeMemory(users
);
4454 frame
= talloc_stackframe();
4455 for (i
=0; i
< num_users
; i
++) {
4456 fstring domain
, user
;
4459 fstrcpy(result
[i
].name
, users
[i
]);
4461 p
= strchr(users
[i
], *lp_winbind_separator());
4463 DEBUG(3, ("%s\n", users
[i
]));
4466 fstrcpy(domain
, c
->opt_target_workgroup
);
4467 fstrcpy(user
, users
[i
]);
4470 fstrcpy(domain
, users
[i
]);
4475 get_user_sids(domain
, user
, &(result
[i
].token
));
4479 wbcFreeMemory(users
);
4481 *num_tokens
= num_users
;
4482 *user_tokens
= result
;
4487 static bool get_user_tokens_from_file(FILE *f
,
4489 struct user_token
**tokens
)
4491 struct user_token
*token
= NULL
;
4496 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4500 if (line
[strlen(line
)-1] == '\n')
4501 line
[strlen(line
)-1] = '\0';
4503 if (line
[0] == ' ') {
4507 string_to_sid(&sid
, &line
[1]);
4509 if (token
== NULL
) {
4510 DEBUG(0, ("File does not begin with username"));
4514 add_sid_to_token(&token
->token
, &sid
);
4518 /* And a new user... */
4521 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4522 if (*tokens
== NULL
) {
4523 DEBUG(0, ("Could not realloc tokens\n"));
4527 token
= &((*tokens
)[*num_tokens
-1]);
4529 fstrcpy(token
->name
, line
);
4530 token
->token
.num_sids
= 0;
4531 token
->token
.user_sids
= NULL
;
4540 * Show the list of all users that have access to a share
4543 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4544 TALLOC_CTX
*mem_ctx
,
4545 const char *netname
,
4547 struct user_token
*tokens
)
4550 SEC_DESC
*share_sd
= NULL
;
4551 SEC_DESC
*root_sd
= NULL
;
4552 struct cli_state
*cli
= rpc_pipe_np_smb_conn(pipe_hnd
);
4554 union srvsvc_NetShareInfo info
;
4559 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
4566 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4567 DEBUG(1, ("Coult not query secdesc for share %s\n",
4572 share_sd
= info
.info502
->sd_buf
.sd
;
4573 if (share_sd
== NULL
) {
4574 DEBUG(1, ("Got no secdesc for share %s\n",
4580 if (!cli_send_tconX(cli
, netname
, "A:", "", 0)) {
4584 fnum
= cli_nt_create(cli
, "\\", READ_CONTROL_ACCESS
);
4587 root_sd
= cli_query_secdesc(cli
, fnum
, mem_ctx
);
4590 for (i
=0; i
<num_tokens
; i
++) {
4593 if (share_sd
!= NULL
) {
4594 if (!se_access_check(share_sd
, &tokens
[i
].token
,
4595 1, &acc_granted
, &status
)) {
4596 DEBUG(1, ("Could not check share_sd for "
4602 if (!NT_STATUS_IS_OK(status
))
4606 if (root_sd
== NULL
) {
4607 d_printf(" %s\n", tokens
[i
].name
);
4611 if (!se_access_check(root_sd
, &tokens
[i
].token
,
4612 1, &acc_granted
, &status
)) {
4613 DEBUG(1, ("Could not check root_sd for user %s\n",
4618 if (!NT_STATUS_IS_OK(status
))
4621 d_printf(" %s\n", tokens
[i
].name
);
4625 cli_close(cli
, fnum
);
4637 static void collect_share(const char *name
, uint32 m
,
4638 const char *comment
, void *state
)
4640 struct share_list
*share_list
= (struct share_list
*)state
;
4642 if (m
!= STYPE_DISKTREE
)
4645 share_list
->num_shares
+= 1;
4646 share_list
->shares
= SMB_REALLOC_ARRAY(share_list
->shares
, char *, share_list
->num_shares
);
4647 if (!share_list
->shares
) {
4648 share_list
->num_shares
= 0;
4651 share_list
->shares
[share_list
->num_shares
-1] = SMB_STRDUP(name
);
4654 static void rpc_share_userlist_usage(void)
4660 * List shares on a remote RPC server, including the security descriptors
4662 * All parameters are provided by the run_rpc_command function, except for
4663 * argc, argv which are passes through.
4665 * @param domain_sid The domain sid acquired from the remote server
4666 * @param cli A cli_state connected to the server.
4667 * @param mem_ctx Talloc context, destoyed on completion of the function.
4668 * @param argc Standard main() style argc
4669 * @param argv Standard main() style argv. Initial components are already
4672 * @return Normal NTSTATUS return.
4675 static NTSTATUS
rpc_share_allowedusers_internals(struct net_context
*c
,
4676 const DOM_SID
*domain_sid
,
4677 const char *domain_name
,
4678 struct cli_state
*cli
,
4679 struct rpc_pipe_client
*pipe_hnd
,
4680 TALLOC_CTX
*mem_ctx
,
4690 struct user_token
*tokens
= NULL
;
4693 struct share_list share_list
;
4696 rpc_share_userlist_usage();
4697 return NT_STATUS_UNSUCCESSFUL
;
4703 f
= fopen(argv
[0], "r");
4707 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4708 return NT_STATUS_UNSUCCESSFUL
;
4711 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
4717 DEBUG(0, ("Could not read users from file\n"));
4718 return NT_STATUS_UNSUCCESSFUL
;
4721 for (i
=0; i
<num_tokens
; i
++)
4722 collect_alias_memberships(&tokens
[i
].token
);
4724 init_enum_hnd(&hnd
, 0);
4726 share_list
.num_shares
= 0;
4727 share_list
.shares
= NULL
;
4729 ret
= cli_RNetShareEnum(cli
, collect_share
, &share_list
);
4732 DEBUG(0, ("Error returning browse list: %s\n",
4737 for (i
= 0; i
< share_list
.num_shares
; i
++) {
4738 char *netname
= share_list
.shares
[i
];
4740 if (netname
[strlen(netname
)-1] == '$')
4743 d_printf("%s\n", netname
);
4745 show_userlist(pipe_hnd
, mem_ctx
, netname
,
4746 num_tokens
, tokens
);
4749 for (i
=0; i
<num_tokens
; i
++) {
4750 free_user_token(&tokens
[i
].token
);
4753 SAFE_FREE(share_list
.shares
);
4755 return NT_STATUS_OK
;
4758 static int rpc_share_allowedusers(struct net_context
*c
, int argc
,
4763 result
= run_rpc_command(c
, NULL
, PI_SAMR
, 0,
4764 rpc_aliaslist_internals
,
4769 result
= run_rpc_command(c
, NULL
, PI_LSARPC
, 0,
4775 return run_rpc_command(c
, NULL
, PI_SRVSVC
, 0,
4776 rpc_share_allowedusers_internals
,
4780 int net_usersidlist(struct net_context
*c
, int argc
, const char **argv
)
4783 struct user_token
*tokens
= NULL
;
4787 net_usersidlist_usage(c
, argc
, argv
);
4791 if (!get_user_tokens(c
, &num_tokens
, &tokens
)) {
4792 DEBUG(0, ("Could not get the user/sid list\n"));
4796 for (i
=0; i
<num_tokens
; i
++) {
4797 dump_user_token(&tokens
[i
]);
4798 free_user_token(&tokens
[i
].token
);
4805 int net_usersidlist_usage(struct net_context
*c
, int argc
, const char **argv
)
4807 d_printf("net usersidlist\n"
4808 "\tprints out a list of all users the running winbind knows\n"
4809 "\tabout, together with all their SIDs. This is used as\n"
4810 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4812 net_common_flags_usage(c
, argc
, argv
);
4817 * 'net rpc share' entrypoint.
4818 * @param argc Standard main() style argc
4819 * @param argv Standard main() style argv. Initial components are already
4823 int net_rpc_share(struct net_context
*c
, int argc
, const char **argv
)
4825 struct functable func
[] = {
4826 {"add", rpc_share_add
},
4827 {"delete", rpc_share_delete
},
4828 {"allowedusers", rpc_share_allowedusers
},
4829 {"migrate", rpc_share_migrate
},
4830 {"list", rpc_share_list
},
4835 return run_rpc_command(c
, NULL
, PI_SRVSVC
, 0,
4836 rpc_share_list_internals
,
4839 return net_run_function(c
, argc
, argv
, func
, rpc_share_usage
);
4842 static NTSTATUS
rpc_sh_share_list(struct net_context
*c
,
4843 TALLOC_CTX
*mem_ctx
,
4844 struct rpc_sh_ctx
*ctx
,
4845 struct rpc_pipe_client
*pipe_hnd
,
4846 int argc
, const char **argv
)
4848 return rpc_share_list_internals(c
, ctx
->domain_sid
, ctx
->domain_name
,
4849 ctx
->cli
, pipe_hnd
, mem_ctx
,
4853 static NTSTATUS
rpc_sh_share_add(struct net_context
*c
,
4854 TALLOC_CTX
*mem_ctx
,
4855 struct rpc_sh_ctx
*ctx
,
4856 struct rpc_pipe_client
*pipe_hnd
,
4857 int argc
, const char **argv
)
4861 uint32_t parm_err
= 0;
4862 union srvsvc_NetShareInfo info
;
4863 struct srvsvc_NetShareInfo2 info2
;
4865 if ((argc
< 2) || (argc
> 3)) {
4866 d_fprintf(stderr
, "usage: %s <share> <path> [comment]\n",
4868 return NT_STATUS_INVALID_PARAMETER
;
4871 info2
.name
= argv
[0];
4872 info2
.type
= STYPE_DISKTREE
;
4873 info2
.comment
= (argc
== 3) ? argv
[2] : "";
4874 info2
.permissions
= 0;
4875 info2
.max_users
= 0;
4876 info2
.current_users
= 0;
4877 info2
.path
= argv
[1];
4878 info2
.password
= NULL
;
4880 info
.info2
= &info2
;
4882 status
= rpccli_srvsvc_NetShareAdd(pipe_hnd
, mem_ctx
,
4892 static NTSTATUS
rpc_sh_share_delete(struct net_context
*c
,
4893 TALLOC_CTX
*mem_ctx
,
4894 struct rpc_sh_ctx
*ctx
,
4895 struct rpc_pipe_client
*pipe_hnd
,
4896 int argc
, const char **argv
)
4902 d_fprintf(stderr
, "usage: %s <share>\n", ctx
->whoami
);
4903 return NT_STATUS_INVALID_PARAMETER
;
4906 status
= rpccli_srvsvc_NetShareDel(pipe_hnd
, mem_ctx
,
4915 static NTSTATUS
rpc_sh_share_info(struct net_context
*c
,
4916 TALLOC_CTX
*mem_ctx
,
4917 struct rpc_sh_ctx
*ctx
,
4918 struct rpc_pipe_client
*pipe_hnd
,
4919 int argc
, const char **argv
)
4921 union srvsvc_NetShareInfo info
;
4926 d_fprintf(stderr
, "usage: %s <share>\n", ctx
->whoami
);
4927 return NT_STATUS_INVALID_PARAMETER
;
4930 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
4936 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4940 d_printf("Name: %s\n", info
.info2
->name
);
4941 d_printf("Comment: %s\n", info
.info2
->comment
);
4942 d_printf("Path: %s\n", info
.info2
->path
);
4943 d_printf("Password: %s\n", info
.info2
->password
);
4946 return werror_to_ntstatus(result
);
4949 struct rpc_sh_cmd
*net_rpc_share_cmds(struct net_context
*c
, TALLOC_CTX
*mem_ctx
,
4950 struct rpc_sh_ctx
*ctx
)
4952 static struct rpc_sh_cmd cmds
[] = {
4954 { "list", NULL
, PI_SRVSVC
, rpc_sh_share_list
,
4955 "List available shares" },
4957 { "add", NULL
, PI_SRVSVC
, rpc_sh_share_add
,
4960 { "delete", NULL
, PI_SRVSVC
, rpc_sh_share_delete
,
4963 { "info", NULL
, PI_SRVSVC
, rpc_sh_share_info
,
4964 "Get information about a share" },
4966 { NULL
, NULL
, 0, NULL
, NULL
}
4972 /****************************************************************************/
4974 static int rpc_file_usage(struct net_context
*c
, int argc
, const char **argv
)
4976 return net_file_usage(c
, argc
, argv
);
4980 * Close a file on a remote RPC server
4982 * All parameters are provided by the run_rpc_command function, except for
4983 * argc, argv which are passes through.
4985 * @param c A net_context structure
4986 * @param domain_sid The domain sid acquired from the remote server
4987 * @param cli A cli_state connected to the server.
4988 * @param mem_ctx Talloc context, destoyed on completion of the function.
4989 * @param argc Standard main() style argc
4990 * @param argv Standard main() style argv. Initial components are already
4993 * @return Normal NTSTATUS return.
4995 static NTSTATUS
rpc_file_close_internals(struct net_context
*c
,
4996 const DOM_SID
*domain_sid
,
4997 const char *domain_name
,
4998 struct cli_state
*cli
,
4999 struct rpc_pipe_client
*pipe_hnd
,
5000 TALLOC_CTX
*mem_ctx
,
5004 return rpccli_srvsvc_NetFileClose(pipe_hnd
, mem_ctx
,
5006 atoi(argv
[0]), NULL
);
5010 * Close a file on a remote RPC server
5012 * @param argc Standard main() style argc
5013 * @param argv Standard main() style argv. Initial components are already
5016 * @return A shell status integer (0 for success)
5018 static int rpc_file_close(struct net_context
*c
, int argc
, const char **argv
)
5021 DEBUG(1, ("No fileid given on close\n"));
5022 return rpc_file_usage(c
, argc
, argv
);
5025 return run_rpc_command(c
, NULL
, PI_SRVSVC
, 0,
5026 rpc_file_close_internals
,
5031 * Formatted print of open file info
5033 * @param r struct srvsvc_NetFileInfo3 contents
5036 static void display_file_info_3(struct srvsvc_NetFileInfo3
*r
)
5038 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5039 r
->fid
, r
->user
, r
->permissions
, r
->num_locks
, r
->path
);
5043 * List open files on a remote RPC server
5045 * All parameters are provided by the run_rpc_command function, except for
5046 * argc, argv which are passes through.
5048 * @param c A net_context structure
5049 * @param domain_sid The domain sid acquired from the remote server
5050 * @param cli A cli_state connected to the server.
5051 * @param mem_ctx Talloc context, destoyed on completion of the function.
5052 * @param argc Standard main() style argc
5053 * @param argv Standard main() style argv. Initial components are already
5056 * @return Normal NTSTATUS return.
5059 static NTSTATUS
rpc_file_list_internals(struct net_context
*c
,
5060 const DOM_SID
*domain_sid
,
5061 const char *domain_name
,
5062 struct cli_state
*cli
,
5063 struct rpc_pipe_client
*pipe_hnd
,
5064 TALLOC_CTX
*mem_ctx
,
5068 struct srvsvc_NetFileInfoCtr info_ctr
;
5069 struct srvsvc_NetFileCtr3 ctr3
;
5072 uint32 preferred_len
= 0xffffffff, i
;
5073 const char *username
=NULL
;
5074 uint32_t total_entries
= 0;
5075 uint32_t resume_handle
= 0;
5077 /* if argc > 0, must be user command */
5079 username
= smb_xstrdup(argv
[0]);
5081 ZERO_STRUCT(info_ctr
);
5085 info_ctr
.ctr
.ctr3
= &ctr3
;
5087 status
= rpccli_srvsvc_NetFileEnum(pipe_hnd
, mem_ctx
,
5097 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
))
5100 /* Display results */
5103 "\nEnumerating open files on remote server:\n\n"\
5104 "\nFileId Opened by Perms Locks Path"\
5105 "\n------ --------- ----- ----- ---- \n");
5106 for (i
= 0; i
< total_entries
; i
++)
5107 display_file_info_3(&info_ctr
.ctr
.ctr3
->array
[i
]);
5109 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
5113 * List files for a user on a remote RPC server
5115 * @param argc Standard main() style argc
5116 * @param argv Standard main() style argv. Initial components are already
5119 * @return A shell status integer (0 for success)
5122 static int rpc_file_user(struct net_context
*c
, int argc
, const char **argv
)
5125 DEBUG(1, ("No username given\n"));
5126 return rpc_file_usage(c
, argc
, argv
);
5129 return run_rpc_command(c
, NULL
, PI_SRVSVC
, 0,
5130 rpc_file_list_internals
,
5135 * 'net rpc file' entrypoint.
5136 * @param argc Standard main() style argc
5137 * @param argv Standard main() style argv. Initial components are already
5141 int net_rpc_file(struct net_context
*c
, int argc
, const char **argv
)
5143 struct functable func
[] = {
5144 {"close", rpc_file_close
},
5145 {"user", rpc_file_user
},
5147 {"info", rpc_file_info
},
5153 return run_rpc_command(c
, NULL
, PI_SRVSVC
, 0,
5154 rpc_file_list_internals
,
5157 return net_run_function(c
, argc
, argv
, func
, rpc_file_usage
);
5161 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
5163 * All parameters are provided by the run_rpc_command function, except for
5164 * argc, argv which are passed through.
5166 * @param c A net_context structure
5167 * @param domain_sid The domain sid aquired from the remote server
5168 * @param cli A cli_state connected to the server.
5169 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5170 * @param argc Standard main() style argc
5171 * @param argv Standard main() style argv. Initial components are already
5174 * @return Normal NTSTATUS return.
5177 static NTSTATUS
rpc_shutdown_abort_internals(struct net_context
*c
,
5178 const DOM_SID
*domain_sid
,
5179 const char *domain_name
,
5180 struct cli_state
*cli
,
5181 struct rpc_pipe_client
*pipe_hnd
,
5182 TALLOC_CTX
*mem_ctx
,
5186 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5188 result
= rpccli_initshutdown_Abort(pipe_hnd
, mem_ctx
, NULL
, NULL
);
5190 if (NT_STATUS_IS_OK(result
)) {
5191 d_printf("\nShutdown successfully aborted\n");
5192 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5194 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5200 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5202 * All parameters are provided by the run_rpc_command function, except for
5203 * argc, argv which are passed through.
5205 * @param c A net_context structure
5206 * @param domain_sid The domain sid aquired from the remote server
5207 * @param cli A cli_state connected to the server.
5208 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5209 * @param argc Standard main() style argc
5210 * @param argv Standard main() style argv. Initial components are already
5213 * @return Normal NTSTATUS return.
5216 static NTSTATUS
rpc_reg_shutdown_abort_internals(struct net_context
*c
,
5217 const DOM_SID
*domain_sid
,
5218 const char *domain_name
,
5219 struct cli_state
*cli
,
5220 struct rpc_pipe_client
*pipe_hnd
,
5221 TALLOC_CTX
*mem_ctx
,
5225 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5227 result
= rpccli_winreg_AbortSystemShutdown(pipe_hnd
, mem_ctx
, NULL
, NULL
);
5229 if (NT_STATUS_IS_OK(result
)) {
5230 d_printf("\nShutdown successfully aborted\n");
5231 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5233 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5239 * ABORT the Shut down of a remote RPC server
5241 * @param argc Standard main() style argc
5242 * @param argv Standard main() style argv. Initial components are already
5245 * @return A shell status integer (0 for success)
5248 static int rpc_shutdown_abort(struct net_context
*c
, int argc
,
5251 int rc
= run_rpc_command(c
, NULL
, PI_INITSHUTDOWN
, 0,
5252 rpc_shutdown_abort_internals
,
5258 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5260 return run_rpc_command(c
, NULL
, PI_WINREG
, 0,
5261 rpc_reg_shutdown_abort_internals
,
5266 * Shut down a remote RPC Server via initshutdown pipe
5268 * All parameters are provided by the run_rpc_command function, except for
5269 * argc, argv which are passes through.
5271 * @param c A net_context structure
5272 * @param domain_sid The domain sid aquired from the remote server
5273 * @param cli A cli_state connected to the server.
5274 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5275 * @param argc Standard main() style argc
5276 * @param argc Standard main() style argv. Initial components are already
5279 * @return Normal NTSTATUS return.
5282 NTSTATUS
rpc_init_shutdown_internals(struct net_context
*c
,
5283 const DOM_SID
*domain_sid
,
5284 const char *domain_name
,
5285 struct cli_state
*cli
,
5286 struct rpc_pipe_client
*pipe_hnd
,
5287 TALLOC_CTX
*mem_ctx
,
5291 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5292 const char *msg
= "This machine will be shutdown shortly";
5293 uint32 timeout
= 20;
5294 struct initshutdown_String msg_string
;
5295 struct initshutdown_String_sub s
;
5297 if (c
->opt_comment
) {
5298 msg
= c
->opt_comment
;
5300 if (c
->opt_timeout
) {
5301 timeout
= c
->opt_timeout
;
5305 msg_string
.name
= &s
;
5307 /* create an entry */
5308 result
= rpccli_initshutdown_Init(pipe_hnd
, mem_ctx
, NULL
,
5309 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5312 if (NT_STATUS_IS_OK(result
)) {
5313 d_printf("\nShutdown of remote machine succeeded\n");
5314 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5316 DEBUG(1,("Shutdown of remote machine failed!\n"));
5322 * Shut down a remote RPC Server via winreg pipe
5324 * All parameters are provided by the run_rpc_command function, except for
5325 * argc, argv which are passes through.
5327 * @param c A net_context structure
5328 * @param domain_sid The domain sid aquired from the remote server
5329 * @param cli A cli_state connected to the server.
5330 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5331 * @param argc Standard main() style argc
5332 * @param argc Standard main() style argv. Initial components are already
5335 * @return Normal NTSTATUS return.
5338 NTSTATUS
rpc_reg_shutdown_internals(struct net_context
*c
,
5339 const DOM_SID
*domain_sid
,
5340 const char *domain_name
,
5341 struct cli_state
*cli
,
5342 struct rpc_pipe_client
*pipe_hnd
,
5343 TALLOC_CTX
*mem_ctx
,
5347 const char *msg
= "This machine will be shutdown shortly";
5348 uint32 timeout
= 20;
5349 struct initshutdown_String msg_string
;
5350 struct initshutdown_String_sub s
;
5354 if (c
->opt_comment
) {
5355 msg
= c
->opt_comment
;
5358 msg_string
.name
= &s
;
5360 if (c
->opt_timeout
) {
5361 timeout
= c
->opt_timeout
;
5364 /* create an entry */
5365 result
= rpccli_winreg_InitiateSystemShutdown(pipe_hnd
, mem_ctx
, NULL
,
5366 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5369 if (NT_STATUS_IS_OK(result
)) {
5370 d_printf("\nShutdown of remote machine succeeded\n");
5372 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5373 if ( W_ERROR_EQUAL(werr
, WERR_MACHINE_LOCKED
) )
5374 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5376 d_fprintf(stderr
, "\nresult was: %s\n", dos_errstr(werr
));
5383 * Shut down a remote RPC server
5385 * @param argc Standard main() style argc
5386 * @param argc Standard main() style argv. Initial components are already
5389 * @return A shell status integer (0 for success)
5392 static int rpc_shutdown(struct net_context
*c
, int argc
, const char **argv
)
5394 int rc
= run_rpc_command(c
, NULL
, PI_INITSHUTDOWN
, 0,
5395 rpc_init_shutdown_internals
,
5399 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5400 rc
= run_rpc_command(c
, NULL
, PI_WINREG
, 0,
5401 rpc_reg_shutdown_internals
, argc
, argv
);
5407 /***************************************************************************
5408 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5409 ***************************************************************************/
5412 * Add interdomain trust account to the RPC server.
5413 * All parameters (except for argc and argv) are passed by run_rpc_command
5416 * @param c A net_context structure
5417 * @param domain_sid The domain sid acquired from the server
5418 * @param cli A cli_state connected to the server.
5419 * @param mem_ctx Talloc context, destoyed on completion of the function.
5420 * @param argc Standard main() style argc
5421 * @param argc Standard main() style argv. Initial components are already
5424 * @return normal NTSTATUS return code
5427 static NTSTATUS
rpc_trustdom_add_internals(struct net_context
*c
,
5428 const DOM_SID
*domain_sid
,
5429 const char *domain_name
,
5430 struct cli_state
*cli
,
5431 struct rpc_pipe_client
*pipe_hnd
,
5432 TALLOC_CTX
*mem_ctx
,
5436 POLICY_HND connect_pol
, domain_pol
, user_pol
;
5437 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5439 struct lsa_String lsa_acct_name
;
5441 uint32 acct_flags
=0;
5443 uint32_t access_granted
= 0;
5444 union samr_UserInfo info
;
5447 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5448 return NT_STATUS_INVALID_PARAMETER
;
5452 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5455 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5456 return NT_STATUS_NO_MEMORY
;
5459 strupper_m(acct_name
);
5461 init_lsa_String(&lsa_acct_name
, acct_name
);
5463 /* Get samr policy handle */
5464 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
5466 MAXIMUM_ALLOWED_ACCESS
,
5468 if (!NT_STATUS_IS_OK(result
)) {
5472 /* Get domain policy handle */
5473 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
5475 MAXIMUM_ALLOWED_ACCESS
,
5476 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5478 if (!NT_STATUS_IS_OK(result
)) {
5482 /* Create trusting domain's account */
5483 acb_info
= ACB_NORMAL
;
5484 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
5485 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
5486 SAMR_USER_ACCESS_SET_PASSWORD
|
5487 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
5488 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
5490 result
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
5498 if (!NT_STATUS_IS_OK(result
)) {
5504 struct samr_LogonHours hours
;
5505 struct lsa_BinaryString parameters
;
5506 const int units_per_week
= 168;
5509 encode_pw_buffer(pwbuf
, argv
[1], STR_UNICODE
);
5511 ZERO_STRUCT(notime
);
5513 ZERO_STRUCT(parameters
);
5515 hours
.bits
= talloc_array(mem_ctx
, uint8_t, units_per_week
);
5517 result
= NT_STATUS_NO_MEMORY
;
5520 hours
.units_per_week
= units_per_week
;
5521 memset(hours
.bits
, 0xFF, units_per_week
);
5523 init_samr_user_info23(&info
.info23
,
5524 notime
, notime
, notime
,
5525 notime
, notime
, notime
,
5526 NULL
, NULL
, NULL
, NULL
, NULL
,
5527 NULL
, NULL
, NULL
, NULL
, ¶meters
,
5528 0, 0, ACB_DOMTRUST
, SAMR_FIELD_ACCT_FLAGS
,
5530 0, 0, 0, 0, 0, 0, 0,
5533 SamOEMhashBlob(info
.info23
.password
.data
, 516,
5534 &cli
->user_session_key
);
5536 result
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
5541 if (!NT_STATUS_IS_OK(result
)) {
5542 DEBUG(0,("Could not set trust account password: %s\n",
5543 nt_errstr(result
)));
5549 SAFE_FREE(acct_name
);
5554 * Create interdomain trust account for a remote domain.
5556 * @param argc standard argc
5557 * @param argv standard argv without initial components
5559 * @return Integer status (0 means success)
5562 static int rpc_trustdom_add(struct net_context
*c
, int argc
, const char **argv
)
5565 return run_rpc_command(c
, NULL
, PI_SAMR
, 0,
5566 rpc_trustdom_add_internals
, argc
, argv
);
5568 d_printf("Usage: net rpc trustdom add <domain>\n");
5575 * Remove interdomain trust account from the RPC server.
5576 * All parameters (except for argc and argv) are passed by run_rpc_command
5579 * @param c A net_context structure
5580 * @param domain_sid The domain sid acquired from the server
5581 * @param cli A cli_state connected to the server.
5582 * @param mem_ctx Talloc context, destoyed on completion of the function.
5583 * @param argc Standard main() style argc
5584 * @param argc Standard main() style argv. Initial components are already
5587 * @return normal NTSTATUS return code
5590 static NTSTATUS
rpc_trustdom_del_internals(struct net_context
*c
,
5591 const DOM_SID
*domain_sid
,
5592 const char *domain_name
,
5593 struct cli_state
*cli
,
5594 struct rpc_pipe_client
*pipe_hnd
,
5595 TALLOC_CTX
*mem_ctx
,
5599 POLICY_HND connect_pol
, domain_pol
, user_pol
;
5600 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5602 DOM_SID trust_acct_sid
;
5603 struct samr_Ids user_rids
, name_types
;
5604 struct lsa_String lsa_acct_name
;
5607 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5608 return NT_STATUS_INVALID_PARAMETER
;
5612 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5614 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
5616 if (acct_name
== NULL
)
5617 return NT_STATUS_NO_MEMORY
;
5619 strupper_m(acct_name
);
5621 /* Get samr policy handle */
5622 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
5624 MAXIMUM_ALLOWED_ACCESS
,
5626 if (!NT_STATUS_IS_OK(result
)) {
5630 /* Get domain policy handle */
5631 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
5633 MAXIMUM_ALLOWED_ACCESS
,
5634 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5636 if (!NT_STATUS_IS_OK(result
)) {
5640 init_lsa_String(&lsa_acct_name
, acct_name
);
5642 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
5649 if (!NT_STATUS_IS_OK(result
)) {
5653 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
5655 MAXIMUM_ALLOWED_ACCESS
,
5659 if (!NT_STATUS_IS_OK(result
)) {
5663 /* append the rid to the domain sid */
5664 sid_copy(&trust_acct_sid
, domain_sid
);
5665 if (!sid_append_rid(&trust_acct_sid
, user_rids
.ids
[0])) {
5669 /* remove the sid */
5671 result
= rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd
, mem_ctx
,
5674 if (!NT_STATUS_IS_OK(result
)) {
5680 result
= rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
5683 if (!NT_STATUS_IS_OK(result
)) {
5687 if (!NT_STATUS_IS_OK(result
)) {
5688 DEBUG(0,("Could not set trust account password: %s\n",
5689 nt_errstr(result
)));
5698 * Delete interdomain trust account for a remote domain.
5700 * @param argc standard argc
5701 * @param argv standard argv without initial components
5703 * @return Integer status (0 means success)
5706 static int rpc_trustdom_del(struct net_context
*c
, int argc
, const char **argv
)
5709 return run_rpc_command(c
, NULL
, PI_SAMR
, 0,
5710 rpc_trustdom_del_internals
, argc
, argv
);
5712 d_printf("Usage: net rpc trustdom del <domain>\n");
5717 static NTSTATUS
rpc_trustdom_get_pdc(struct net_context
*c
,
5718 struct cli_state
*cli
,
5719 TALLOC_CTX
*mem_ctx
,
5720 const char *domain_name
)
5722 char *dc_name
= NULL
;
5723 const char *buffer
= NULL
;
5724 struct rpc_pipe_client
*netr
;
5727 /* Use NetServerEnum2 */
5729 if (cli_get_pdc_name(cli
, domain_name
, &dc_name
)) {
5731 return NT_STATUS_OK
;
5734 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5735 for domain %s\n", domain_name
));
5737 /* Try netr_GetDcName */
5739 netr
= cli_rpc_pipe_open_noauth(cli
, PI_NETLOGON
, &status
);
5744 status
= rpccli_netr_GetDcName(netr
, mem_ctx
,
5751 if (NT_STATUS_IS_OK(status
)) {
5755 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5756 for domain %s\n", domain_name
));
5762 * Establish trust relationship to a trusting domain.
5763 * Interdomain account must already be created on remote PDC.
5765 * @param c A net_context structure
5766 * @param argc standard argc
5767 * @param argv standard argv without initial components
5769 * @return Integer status (0 means success)
5772 static int rpc_trustdom_establish(struct net_context
*c
, int argc
,
5775 struct cli_state
*cli
= NULL
;
5776 struct sockaddr_storage server_ss
;
5777 struct rpc_pipe_client
*pipe_hnd
= NULL
;
5778 POLICY_HND connect_hnd
;
5779 TALLOC_CTX
*mem_ctx
;
5781 DOM_SID
*domain_sid
;
5786 union lsa_PolicyInformation
*info
= NULL
;
5789 * Connect to \\server\ipc$ as 'our domain' account with password
5793 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5797 domain_name
= smb_xstrdup(argv
[0]);
5798 strupper_m(domain_name
);
5800 /* account name used at first is our domain's name with '$' */
5801 asprintf(&acct_name
, "%s$", lp_workgroup());
5802 strupper_m(acct_name
);
5805 * opt_workgroup will be used by connection functions further,
5806 * hence it should be set to remote domain name instead of ours
5808 if (c
->opt_workgroup
) {
5809 c
->opt_workgroup
= smb_xstrdup(domain_name
);
5812 c
->opt_user_name
= acct_name
;
5814 /* find the domain controller */
5815 if (!net_find_pdc(&server_ss
, pdc_name
, domain_name
)) {
5816 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
5820 /* connect to ipc$ as username/password */
5821 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
, pdc_name
);
5822 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
5824 /* Is it trusting domain account for sure ? */
5825 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5826 nt_errstr(nt_status
)));
5830 /* store who we connected to */
5832 saf_store( domain_name
, pdc_name
);
5835 * Connect to \\server\ipc$ again (this time anonymously)
5838 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
5841 if (NT_STATUS_IS_ERR(nt_status
)) {
5842 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5843 domain_name
, nt_errstr(nt_status
)));
5847 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
5848 "domain %s", domain_name
))) {
5849 DEBUG(0, ("talloc_init() failed\n"));
5854 /* Make sure we're talking to a proper server */
5856 nt_status
= rpc_trustdom_get_pdc(c
, cli
, mem_ctx
, domain_name
);
5857 if (!NT_STATUS_IS_OK(nt_status
)) {
5859 talloc_destroy(mem_ctx
);
5864 * Call LsaOpenPolicy and LsaQueryInfo
5867 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &nt_status
);
5869 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
5871 talloc_destroy(mem_ctx
);
5875 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, true, SEC_RIGHTS_QUERY_VALUE
,
5877 if (NT_STATUS_IS_ERR(nt_status
)) {
5878 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5879 nt_errstr(nt_status
)));
5881 talloc_destroy(mem_ctx
);
5885 /* Querying info level 5 */
5887 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
5889 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
5891 if (NT_STATUS_IS_ERR(nt_status
)) {
5892 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5893 nt_errstr(nt_status
)));
5895 talloc_destroy(mem_ctx
);
5899 domain_sid
= info
->account_domain
.sid
;
5901 /* There should be actually query info level 3 (following nt serv behaviour),
5902 but I still don't know if it's _really_ necessary */
5905 * Store the password in secrets db
5908 if (!pdb_set_trusteddom_pw(domain_name
, c
->opt_password
, domain_sid
)) {
5909 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5911 talloc_destroy(mem_ctx
);
5916 * Close the pipes and clean up
5919 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
5920 if (NT_STATUS_IS_ERR(nt_status
)) {
5921 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5922 nt_errstr(nt_status
)));
5924 talloc_destroy(mem_ctx
);
5930 talloc_destroy(mem_ctx
);
5932 d_printf("Trust to domain %s established\n", domain_name
);
5937 * Revoke trust relationship to the remote domain
5939 * @param c A net_context structure
5940 * @param argc standard argc
5941 * @param argv standard argv without initial components
5943 * @return Integer status (0 means success)
5946 static int rpc_trustdom_revoke(struct net_context
*c
, int argc
,
5952 if (argc
< 1) return -1;
5954 /* generate upper cased domain name */
5955 domain_name
= smb_xstrdup(argv
[0]);
5956 strupper_m(domain_name
);
5958 /* delete password of the trust */
5959 if (!pdb_del_trusteddom_pw(domain_name
)) {
5960 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5967 SAFE_FREE(domain_name
);
5972 * Usage for 'net rpc trustdom' command
5974 * @param argc standard argc
5975 * @param argv standard argv without inital components
5977 * @return Integer status returned to shell
5980 static int rpc_trustdom_usage(struct net_context
*c
, int argc
,
5983 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
5984 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
5985 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
5986 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5987 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
5988 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5993 static NTSTATUS
rpc_query_domain_sid(struct net_context
*c
,
5994 const DOM_SID
*domain_sid
,
5995 const char *domain_name
,
5996 struct cli_state
*cli
,
5997 struct rpc_pipe_client
*pipe_hnd
,
5998 TALLOC_CTX
*mem_ctx
,
6003 sid_to_fstring(str_sid
, domain_sid
);
6004 d_printf("%s\n", str_sid
);
6005 return NT_STATUS_OK
;
6008 static void print_trusted_domain(DOM_SID
*dom_sid
, const char *trusted_dom_name
)
6010 fstring ascii_sid
, padding
;
6011 int pad_len
, col_len
= 20;
6013 /* convert sid into ascii string */
6014 sid_to_fstring(ascii_sid
, dom_sid
);
6016 /* calculate padding space for d_printf to look nicer */
6017 pad_len
= col_len
- strlen(trusted_dom_name
);
6018 padding
[pad_len
] = 0;
6019 do padding
[--pad_len
] = ' '; while (pad_len
);
6021 d_printf("%s%s%s\n", trusted_dom_name
, padding
, ascii_sid
);
6024 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
6025 TALLOC_CTX
*mem_ctx
,
6028 const char *trusted_dom_name
)
6031 union lsa_TrustedDomainInfo
*info
= NULL
;
6032 char *cleartextpwd
= NULL
;
6033 uint8_t nt_hash
[16];
6036 nt_status
= rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd
, mem_ctx
,
6039 LSA_TRUSTED_DOMAIN_INFO_PASSWORD
,
6041 if (NT_STATUS_IS_ERR(nt_status
)) {
6042 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6043 nt_errstr(nt_status
)));
6047 data
= data_blob(info
->password
.password
->data
,
6048 info
->password
.password
->length
);
6050 if (!rpccli_get_pwd_hash(pipe_hnd
, nt_hash
)) {
6051 DEBUG(0, ("Could not retrieve password hash\n"));
6055 cleartextpwd
= decrypt_trustdom_secret(nt_hash
, &data
);
6057 if (cleartextpwd
== NULL
) {
6058 DEBUG(0,("retrieved NULL password\n"));
6059 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6063 if (!pdb_set_trusteddom_pw(trusted_dom_name
, cleartextpwd
, &dom_sid
)) {
6064 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6065 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6069 #ifdef DEBUG_PASSWORD
6070 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6071 "password: [%s]\n", trusted_dom_name
,
6072 sid_string_dbg(&dom_sid
), cleartextpwd
));
6076 SAFE_FREE(cleartextpwd
);
6077 data_blob_free(&data
);
6082 static int rpc_trustdom_vampire(struct net_context
*c
, int argc
,
6085 /* common variables */
6086 TALLOC_CTX
* mem_ctx
;
6087 struct cli_state
*cli
= NULL
;
6088 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6090 const char *domain_name
= NULL
;
6091 DOM_SID
*queried_dom_sid
;
6092 POLICY_HND connect_hnd
;
6093 union lsa_PolicyInformation
*info
= NULL
;
6095 /* trusted domains listing variables */
6096 unsigned int enum_ctx
= 0;
6098 struct lsa_DomainList dom_list
;
6102 * Listing trusted domains (stored in secrets.tdb, if local)
6105 mem_ctx
= talloc_init("trust relationships vampire");
6108 * set domain and pdc name to local samba server (default)
6109 * or to remote one given in command line
6112 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
6113 domain_name
= c
->opt_workgroup
;
6114 c
->opt_target_workgroup
= c
->opt_workgroup
;
6116 fstrcpy(pdc_name
, global_myname());
6117 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6118 c
->opt_target_workgroup
= domain_name
;
6121 /* open \PIPE\lsarpc and open policy handle */
6122 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6123 if (!NT_STATUS_IS_OK(nt_status
)) {
6124 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6125 nt_errstr(nt_status
)));
6126 talloc_destroy(mem_ctx
);
6130 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &nt_status
);
6132 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6133 nt_errstr(nt_status
) ));
6135 talloc_destroy(mem_ctx
);
6139 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, SEC_RIGHTS_QUERY_VALUE
,
6141 if (NT_STATUS_IS_ERR(nt_status
)) {
6142 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6143 nt_errstr(nt_status
)));
6145 talloc_destroy(mem_ctx
);
6149 /* query info level 5 to obtain sid of a domain being queried */
6150 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
6152 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6155 if (NT_STATUS_IS_ERR(nt_status
)) {
6156 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6157 nt_errstr(nt_status
)));
6159 talloc_destroy(mem_ctx
);
6163 queried_dom_sid
= info
->account_domain
.sid
;
6166 * Keep calling LsaEnumTrustdom over opened pipe until
6167 * the end of enumeration is reached
6170 d_printf("Vampire trusted domains:\n\n");
6173 nt_status
= rpccli_lsa_EnumTrustDom(pipe_hnd
, mem_ctx
,
6178 if (NT_STATUS_IS_ERR(nt_status
)) {
6179 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6180 nt_errstr(nt_status
)));
6182 talloc_destroy(mem_ctx
);
6186 for (i
= 0; i
< dom_list
.count
; i
++) {
6188 print_trusted_domain(dom_list
.domains
[i
].sid
,
6189 dom_list
.domains
[i
].name
.string
);
6191 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
6192 *dom_list
.domains
[i
].sid
,
6193 dom_list
.domains
[i
].name
.string
);
6194 if (!NT_STATUS_IS_OK(nt_status
)) {
6196 talloc_destroy(mem_ctx
);
6202 * in case of no trusted domains say something rather
6203 * than just display blank line
6205 if (!dom_list
.count
) d_printf("none\n");
6207 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6209 /* close this connection before doing next one */
6210 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6211 if (NT_STATUS_IS_ERR(nt_status
)) {
6212 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6213 nt_errstr(nt_status
)));
6215 talloc_destroy(mem_ctx
);
6219 /* close lsarpc pipe and connection to IPC$ */
6222 talloc_destroy(mem_ctx
);
6226 static int rpc_trustdom_list(struct net_context
*c
, int argc
, const char **argv
)
6228 /* common variables */
6229 TALLOC_CTX
* mem_ctx
;
6230 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
6231 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6233 const char *domain_name
= NULL
;
6234 DOM_SID
*queried_dom_sid
;
6236 int ascii_dom_name_len
;
6237 POLICY_HND connect_hnd
;
6238 union lsa_PolicyInformation
*info
= NULL
;
6240 /* trusted domains listing variables */
6241 unsigned int num_domains
, enum_ctx
= 0;
6242 int i
, pad_len
, col_len
= 20;
6243 struct lsa_DomainList dom_list
;
6246 /* trusting domains listing variables */
6247 POLICY_HND domain_hnd
;
6248 struct samr_SamArray
*trusts
= NULL
;
6251 * Listing trusted domains (stored in secrets.tdb, if local)
6254 mem_ctx
= talloc_init("trust relationships listing");
6257 * set domain and pdc name to local samba server (default)
6258 * or to remote one given in command line
6261 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
6262 domain_name
= c
->opt_workgroup
;
6263 c
->opt_target_workgroup
= c
->opt_workgroup
;
6265 fstrcpy(pdc_name
, global_myname());
6266 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6267 c
->opt_target_workgroup
= domain_name
;
6270 /* open \PIPE\lsarpc and open policy handle */
6271 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6272 if (!NT_STATUS_IS_OK(nt_status
)) {
6273 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6274 nt_errstr(nt_status
)));
6275 talloc_destroy(mem_ctx
);
6279 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_LSARPC
, &nt_status
);
6281 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6282 nt_errstr(nt_status
) ));
6284 talloc_destroy(mem_ctx
);
6288 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, SEC_RIGHTS_QUERY_VALUE
,
6290 if (NT_STATUS_IS_ERR(nt_status
)) {
6291 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6292 nt_errstr(nt_status
)));
6294 talloc_destroy(mem_ctx
);
6298 /* query info level 5 to obtain sid of a domain being queried */
6299 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
6301 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6304 if (NT_STATUS_IS_ERR(nt_status
)) {
6305 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6306 nt_errstr(nt_status
)));
6308 talloc_destroy(mem_ctx
);
6312 queried_dom_sid
= info
->account_domain
.sid
;
6315 * Keep calling LsaEnumTrustdom over opened pipe until
6316 * the end of enumeration is reached
6319 d_printf("Trusted domains list:\n\n");
6322 nt_status
= rpccli_lsa_EnumTrustDom(pipe_hnd
, mem_ctx
,
6327 if (NT_STATUS_IS_ERR(nt_status
)) {
6328 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6329 nt_errstr(nt_status
)));
6331 talloc_destroy(mem_ctx
);
6335 for (i
= 0; i
< dom_list
.count
; i
++) {
6336 print_trusted_domain(dom_list
.domains
[i
].sid
,
6337 dom_list
.domains
[i
].name
.string
);
6341 * in case of no trusted domains say something rather
6342 * than just display blank line
6344 if (!dom_list
.count
) d_printf("none\n");
6346 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6348 /* close this connection before doing next one */
6349 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6350 if (NT_STATUS_IS_ERR(nt_status
)) {
6351 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6352 nt_errstr(nt_status
)));
6354 talloc_destroy(mem_ctx
);
6358 TALLOC_FREE(pipe_hnd
);
6361 * Listing trusting domains (stored in passdb backend, if local)
6364 d_printf("\nTrusting domains list:\n\n");
6367 * Open \PIPE\samr and get needed policy handles
6369 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SAMR
, &nt_status
);
6371 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6373 talloc_destroy(mem_ctx
);
6378 nt_status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
6380 SA_RIGHT_SAM_OPEN_DOMAIN
,
6382 if (!NT_STATUS_IS_OK(nt_status
)) {
6383 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6384 nt_errstr(nt_status
)));
6386 talloc_destroy(mem_ctx
);
6390 /* SamrOpenDomain - we have to open domain policy handle in order to be
6391 able to enumerate accounts*/
6392 nt_status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
6394 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS
,
6397 if (!NT_STATUS_IS_OK(nt_status
)) {
6398 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6399 nt_errstr(nt_status
)));
6401 talloc_destroy(mem_ctx
);
6406 * perform actual enumeration
6409 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6412 nt_status
= rpccli_samr_EnumDomainUsers(pipe_hnd
, mem_ctx
,
6419 if (NT_STATUS_IS_ERR(nt_status
)) {
6420 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6421 nt_errstr(nt_status
)));
6423 talloc_destroy(mem_ctx
);
6427 for (i
= 0; i
< num_domains
; i
++) {
6429 char *str
= CONST_DISCARD(char *, trusts
->entries
[i
].name
.string
);
6432 * get each single domain's sid (do we _really_ need this ?):
6433 * 1) connect to domain's pdc
6434 * 2) query the pdc for domain's sid
6437 /* get rid of '$' tail */
6438 ascii_dom_name_len
= strlen(str
);
6439 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
6440 str
[ascii_dom_name_len
- 1] = '\0';
6442 /* calculate padding space for d_printf to look nicer */
6443 pad_len
= col_len
- strlen(str
);
6444 padding
[pad_len
] = 0;
6445 do padding
[--pad_len
] = ' '; while (pad_len
);
6447 /* set opt_* variables to remote domain */
6449 c
->opt_workgroup
= talloc_strdup(mem_ctx
, str
);
6450 c
->opt_target_workgroup
= c
->opt_workgroup
;
6452 d_printf("%s%s", str
, padding
);
6454 /* connect to remote domain controller */
6455 nt_status
= net_make_ipc_connection(c
,
6456 NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
,
6458 if (NT_STATUS_IS_OK(nt_status
)) {
6459 /* query for domain's sid */
6460 if (run_rpc_command(c
, remote_cli
, PI_LSARPC
, 0,
6461 rpc_query_domain_sid
, argc
,
6463 d_fprintf(stderr
, "couldn't get domain's sid\n");
6465 cli_shutdown(remote_cli
);
6468 d_fprintf(stderr
, "domain controller is not "
6470 nt_errstr(nt_status
));
6474 if (!num_domains
) d_printf("none\n");
6476 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6478 /* close opened samr and domain policy handles */
6479 nt_status
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_hnd
);
6480 if (!NT_STATUS_IS_OK(nt_status
)) {
6481 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
6484 nt_status
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6485 if (!NT_STATUS_IS_OK(nt_status
)) {
6486 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
6489 /* close samr pipe and connection to IPC$ */
6492 talloc_destroy(mem_ctx
);
6497 * Entrypoint for 'net rpc trustdom' code
6499 * @param argc standard argc
6500 * @param argv standard argv without initial components
6502 * @return Integer status (0 means success)
6505 static int rpc_trustdom(struct net_context
*c
, int argc
, const char **argv
)
6507 struct functable func
[] = {
6508 {"add", rpc_trustdom_add
},
6509 {"del", rpc_trustdom_del
},
6510 {"establish", rpc_trustdom_establish
},
6511 {"revoke", rpc_trustdom_revoke
},
6512 {"help", rpc_trustdom_usage
},
6513 {"list", rpc_trustdom_list
},
6514 {"vampire", rpc_trustdom_vampire
},
6519 rpc_trustdom_usage(c
, argc
, argv
);
6523 return net_run_function(c
, argc
, argv
, func
, rpc_user_usage
);
6527 * Check if a server will take rpc commands
6528 * @param flags Type of server to connect to (PDC, DMB, localhost)
6529 * if the host is not explicitly specified
6530 * @return bool (true means rpc supported)
6532 bool net_rpc_check(struct net_context
*c
, unsigned flags
)
6534 struct cli_state
*cli
;
6536 struct sockaddr_storage server_ss
;
6537 char *server_name
= NULL
;
6540 /* flags (i.e. server type) may depend on command */
6541 if (!net_find_server(c
, NULL
, flags
, &server_ss
, &server_name
))
6544 if ((cli
= cli_initialise()) == NULL
) {
6548 status
= cli_connect(cli
, server_name
, &server_ss
);
6549 if (!NT_STATUS_IS_OK(status
))
6551 if (!attempt_netbios_session_request(&cli
, global_myname(),
6552 server_name
, &server_ss
))
6554 if (!cli_negprot(cli
))
6556 if (cli
->protocol
< PROTOCOL_NT1
)
6565 /* dump sam database via samsync rpc calls */
6566 static int rpc_samdump(struct net_context
*c
, int argc
, const char **argv
) {
6567 return run_rpc_command(c
, NULL
, PI_NETLOGON
, NET_FLAGS_ANONYMOUS
,
6568 rpc_samdump_internals
, argc
, argv
);
6571 /* syncronise sam database via samsync rpc calls */
6572 static int rpc_vampire(struct net_context
*c
, int argc
, const char **argv
) {
6573 return run_rpc_command(c
, NULL
, PI_NETLOGON
, NET_FLAGS_ANONYMOUS
,
6574 rpc_vampire_internals
, argc
, argv
);
6578 * Migrate everything from a print-server
6580 * @param c A net_context structure
6581 * @param argc Standard main() style argc
6582 * @param argv Standard main() style argv. Initial components are already
6585 * @return A shell status integer (0 for success)
6587 * The order is important !
6588 * To successfully add drivers the print-queues have to exist !
6589 * Applying ACLs should be the last step, because you're easily locked out
6592 static int rpc_printer_migrate_all(struct net_context
*c
, int argc
,
6598 printf("no server to migrate\n");
6602 ret
= run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6603 rpc_printer_migrate_printers_internals
, argc
,
6608 ret
= run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6609 rpc_printer_migrate_drivers_internals
, argc
,
6614 ret
= run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6615 rpc_printer_migrate_forms_internals
, argc
, argv
);
6619 ret
= run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6620 rpc_printer_migrate_settings_internals
, argc
,
6625 return run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6626 rpc_printer_migrate_security_internals
, argc
,
6632 * Migrate print-drivers from a print-server
6634 * @param c A net_context structure
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 static int rpc_printer_migrate_drivers(struct net_context
*c
, int argc
,
6645 printf("no server to migrate\n");
6649 return run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6650 rpc_printer_migrate_drivers_internals
,
6655 * Migrate print-forms from a print-server
6657 * @param c A net_context structure
6658 * @param argc Standard main() style argc
6659 * @param argv Standard main() style argv. Initial components are already
6662 * @return A shell status integer (0 for success)
6664 static int rpc_printer_migrate_forms(struct net_context
*c
, int argc
,
6668 printf("no server to migrate\n");
6672 return run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6673 rpc_printer_migrate_forms_internals
,
6678 * Migrate printers from a print-server
6680 * @param c A net_context structure
6681 * @param argc Standard main() style argc
6682 * @param argv Standard main() style argv. Initial components are already
6685 * @return A shell status integer (0 for success)
6687 static int rpc_printer_migrate_printers(struct net_context
*c
, int argc
,
6691 printf("no server to migrate\n");
6695 return run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6696 rpc_printer_migrate_printers_internals
,
6701 * Migrate printer-ACLs from a print-server
6703 * @param c A net_context structure
6704 * @param argc Standard main() style argc
6705 * @param argv Standard main() style argv. Initial components are already
6708 * @return A shell status integer (0 for success)
6710 static int rpc_printer_migrate_security(struct net_context
*c
, int argc
,
6714 printf("no server to migrate\n");
6718 return run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6719 rpc_printer_migrate_security_internals
,
6724 * Migrate printer-settings from a print-server
6726 * @param c A net_context structure
6727 * @param argc Standard main() style argc
6728 * @param argv Standard main() style argv. Initial components are already
6731 * @return A shell status integer (0 for success)
6733 static int rpc_printer_migrate_settings(struct net_context
*c
, int argc
,
6737 printf("no server to migrate\n");
6741 return run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6742 rpc_printer_migrate_settings_internals
,
6747 * 'net rpc printer' entrypoint.
6749 * @param c A net_context structure
6750 * @param argc Standard main() style argc
6751 * @param argv Standard main() style argv. Initial components are already
6755 int rpc_printer_migrate(struct net_context
*c
, int argc
, const char **argv
)
6758 /* ouch: when addriver and setdriver are called from within
6759 rpc_printer_migrate_drivers_internals, the printer-queue already
6762 struct functable func
[] = {
6763 {"all", rpc_printer_migrate_all
},
6764 {"drivers", rpc_printer_migrate_drivers
},
6765 {"forms", rpc_printer_migrate_forms
},
6766 {"help", rpc_printer_usage
},
6767 {"printers", rpc_printer_migrate_printers
},
6768 {"security", rpc_printer_migrate_security
},
6769 {"settings", rpc_printer_migrate_settings
},
6773 return net_run_function(c
, argc
, argv
, func
, rpc_printer_usage
);
6778 * List printers on a remote RPC server
6780 * @param c A net_context structure
6781 * @param argc Standard main() style argc
6782 * @param argv Standard main() style argv. Initial components are already
6785 * @return A shell status integer (0 for success)
6787 static int rpc_printer_list(struct net_context
*c
, int argc
, const char **argv
)
6790 return run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6791 rpc_printer_list_internals
,
6796 * List printer-drivers on a remote RPC server
6798 * @param c A net_context structure
6799 * @param argc Standard main() style argc
6800 * @param argv Standard main() style argv. Initial components are already
6803 * @return A shell status integer (0 for success)
6805 static int rpc_printer_driver_list(struct net_context
*c
, int argc
,
6809 return run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6810 rpc_printer_driver_list_internals
,
6815 * Publish printer in ADS via MSRPC
6817 * @param c A net_context structure
6818 * @param argc Standard main() style argc
6819 * @param argv Standard main() style argv. Initial components are already
6822 * @return A shell status integer (0 for success)
6824 static int rpc_printer_publish_publish(struct net_context
*c
, int argc
,
6828 return run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6829 rpc_printer_publish_publish_internals
,
6834 * Update printer in ADS via MSRPC
6836 * @param c A net_context structure
6837 * @param argc Standard main() style argc
6838 * @param argv Standard main() style argv. Initial components are already
6841 * @return A shell status integer (0 for success)
6843 static int rpc_printer_publish_update(struct net_context
*c
, int argc
, const char **argv
)
6846 return run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6847 rpc_printer_publish_update_internals
,
6852 * UnPublish printer in ADS via MSRPC
6854 * @param c A net_context structure
6855 * @param argc Standard main() style argc
6856 * @param argv Standard main() style argv. Initial components are already
6859 * @return A shell status integer (0 for success)
6861 static int rpc_printer_publish_unpublish(struct net_context
*c
, int argc
,
6865 return run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6866 rpc_printer_publish_unpublish_internals
,
6871 * List published printers via MSRPC
6873 * @param c A net_context structure
6874 * @param argc Standard main() style argc
6875 * @param argv Standard main() style argv. Initial components are already
6878 * @return A shell status integer (0 for success)
6880 static int rpc_printer_publish_list(struct net_context
*c
, int argc
,
6884 return run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6885 rpc_printer_publish_list_internals
,
6891 * Publish printer in ADS
6893 * @param c A net_context structure
6894 * @param argc Standard main() style argc
6895 * @param argv Standard main() style argv. Initial components are already
6898 * @return A shell status integer (0 for success)
6900 static int rpc_printer_publish(struct net_context
*c
, int argc
,
6904 struct functable func
[] = {
6905 {"publish", rpc_printer_publish_publish
},
6906 {"update", rpc_printer_publish_update
},
6907 {"unpublish", rpc_printer_publish_unpublish
},
6908 {"list", rpc_printer_publish_list
},
6909 {"help", rpc_printer_usage
},
6914 return run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6915 rpc_printer_publish_list_internals
,
6918 return net_run_function(c
, argc
, argv
, func
, rpc_printer_usage
);
6924 * Display rpc printer help page.
6926 * @param c A net_context structure
6927 * @param argc Standard main() style argc
6928 * @param argv Standard main() style argv. Initial components are already
6931 int rpc_printer_usage(struct net_context
*c
, int argc
, const char **argv
)
6933 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"\
6934 "\tlists all printers on print-server\n\n");
6935 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"\
6936 "\tlists all printer-drivers on print-server\n\n");
6937 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"\
6938 "\tpublishes printer settings in Active Directory\n"
6939 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
6940 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"\
6941 "\n\tmigrates printers from remote to local server\n\n");
6942 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"\
6943 "\n\tmigrates printer-settings from remote to local server\n\n");
6944 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"\
6945 "\n\tmigrates printer-drivers from remote to local server\n\n");
6946 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"\
6947 "\n\tmigrates printer-forms from remote to local server\n\n");
6948 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"\
6949 "\n\tmigrates printer-ACLs from remote to local server\n\n");
6950 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"\
6951 "\n\tmigrates drivers, forms, queues, settings and acls from\n"\
6952 "\tremote to local print-server\n\n");
6953 net_common_methods_usage(c
, argc
, argv
);
6954 net_common_flags_usage(c
, argc
, argv
);
6956 "\t-v or --verbose\t\t\tgive verbose output\n"
6957 "\t --destination\t\tmigration target server (default: localhost)\n");
6963 * 'net rpc printer' entrypoint.
6965 * @param c A net_context structure
6966 * @param argc Standard main() style argc
6967 * @param argv Standard main() style argv. Initial components are already
6970 int net_rpc_printer(struct net_context
*c
, int argc
, const char **argv
)
6972 struct functable func
[] = {
6973 {"list", rpc_printer_list
},
6974 {"migrate", rpc_printer_migrate
},
6975 {"driver", rpc_printer_driver_list
},
6976 {"publish", rpc_printer_publish
},
6981 return run_rpc_command(c
, NULL
, PI_SPOOLSS
, 0,
6982 rpc_printer_list_internals
,
6985 return net_run_function(c
, argc
, argv
, func
, rpc_printer_usage
);
6988 /****************************************************************************/
6992 * Basic help function for 'net rpc'
6994 * @param c A net_context structure
6995 * @param argc Standard main() style argc
6996 * @param argv Standard main() style argv. Initial components are already
7000 int net_rpc_help(struct net_context
*c
, int argc
, const char **argv
)
7002 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
7003 d_printf(" net rpc join \t\t\tto join a domain \n");
7004 d_printf(" net rpc oldjoin \t\tto join a domain created in server manager\n");
7005 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
7006 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
7007 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
7008 d_printf(" net rpc group \t\tto list groups\n");
7009 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
7010 d_printf(" net rpc printer \t\tto list and migrate printers\n");
7011 d_printf(" net rpc file \t\t\tto list open files\n");
7012 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
7013 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
7014 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
7015 d_printf(" net rpc samdump \t\tdisplay an NT PDC's users, groups and other data\n");
7016 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
7017 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
7018 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
7019 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
7020 d_printf(" net rpc registry\t\tto manage registry hives\n");
7021 d_printf(" net rpc service\t\tto start, stop and query services\n");
7022 d_printf(" net rpc audit\t\t\tto modify global auditing settings\n");
7023 d_printf(" net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
7025 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
7026 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
7027 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
7028 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
7029 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
7035 * Help function for 'net rpc'. Calls command specific help if requested
7036 * or displays usage of net rpc
7038 * @param c A net_context structure
7039 * @param argc Standard main() style argc
7040 * @param argv Standard main() style argv. Initial components are already
7044 int net_rpc_usage(struct net_context
*c
, int argc
, const char **argv
)
7046 struct functable func
[] = {
7047 {"join", rpc_join_usage
},
7048 {"user", rpc_user_usage
},
7049 {"group", rpc_group_usage
},
7050 {"share", rpc_share_usage
},
7051 /*{"changetrustpw", rpc_changetrustpw_usage}, */
7052 {"trustdom", rpc_trustdom_usage
},
7053 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
7054 /*{"shutdown", rpc_shutdown_usage}, */
7055 {"vampire", rpc_vampire_usage
},
7056 {"help", net_rpc_help
},
7061 net_rpc_help(c
, argc
, argv
);
7065 return net_run_function(c
, argc
, argv
, func
, net_rpc_help
);
7069 * 'net rpc' entrypoint.
7071 * @param c A net_context structure
7072 * @param argc Standard main() style argc
7073 * @param argv Standard main() style argv. Initial components are already
7077 int net_rpc(struct net_context
*c
, int argc
, const char **argv
)
7079 struct functable func
[] = {
7080 {"audit", net_rpc_audit
},
7081 {"info", net_rpc_info
},
7082 {"join", net_rpc_join
},
7083 {"oldjoin", net_rpc_oldjoin
},
7084 {"testjoin", net_rpc_testjoin
},
7085 {"user", net_rpc_user
},
7086 {"password", rpc_user_password
},
7087 {"group", net_rpc_group
},
7088 {"share", net_rpc_share
},
7089 {"file", net_rpc_file
},
7090 {"printer", net_rpc_printer
},
7091 {"changetrustpw", net_rpc_changetrustpw
},
7092 {"trustdom", rpc_trustdom
},
7093 {"abortshutdown", rpc_shutdown_abort
},
7094 {"shutdown", rpc_shutdown
},
7095 {"samdump", rpc_samdump
},
7096 {"vampire", rpc_vampire
},
7097 {"getsid", net_rpc_getsid
},
7098 {"rights", net_rpc_rights
},
7099 {"service", net_rpc_service
},
7100 {"registry", net_rpc_registry
},
7101 {"shell", net_rpc_shell
},
7102 {"help", net_rpc_help
},
7105 return net_run_function(c
, argc
, argv
, func
, net_rpc_usage
);