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 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
64 if (!NT_STATUS_IS_OK(result
)) {
65 d_fprintf(stderr
, "Could not initialise lsa pipe\n");
69 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, false,
70 SEC_RIGHTS_MAXIMUM_ALLOWED
,
72 if (!NT_STATUS_IS_OK(result
)) {
73 d_fprintf(stderr
, "open_policy failed: %s\n",
78 result
= rpccli_lsa_QueryInfoPolicy(lsa_pipe
, mem_ctx
,
80 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
82 if (!NT_STATUS_IS_OK(result
)) {
83 d_fprintf(stderr
, "lsaquery failed: %s\n",
88 *domain_name
= info
->account_domain
.name
.string
;
89 *domain_sid
= info
->account_domain
.sid
;
91 rpccli_lsa_Close(lsa_pipe
, mem_ctx
, &pol
);
92 TALLOC_FREE(lsa_pipe
);
98 * Run a single RPC command, from start to finish.
100 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
101 * @param conn_flag a NET_FLAG_ combination. Passed to
102 * net_make_ipc_connection.
103 * @param argc Standard main() style argc.
104 * @param argv Standard main() style argv. Initial components are already
106 * @return A shell status integer (0 for success).
109 int run_rpc_command(struct net_context
*c
,
110 struct cli_state
*cli_arg
,
111 const struct ndr_syntax_id
*interface
,
117 struct cli_state
*cli
= NULL
;
118 struct rpc_pipe_client
*pipe_hnd
= NULL
;
122 const char *domain_name
;
124 /* make use of cli_state handed over as an argument, if possible */
126 nt_status
= net_make_ipc_connection(c
, conn_flags
, &cli
);
127 if (!NT_STATUS_IS_OK(nt_status
)) {
128 DEBUG(1, ("failed to make ipc connection: %s\n",
129 nt_errstr(nt_status
)));
142 if (!(mem_ctx
= talloc_init("run_rpc_command"))) {
143 DEBUG(0, ("talloc_init() failed\n"));
148 nt_status
= net_get_remote_domain_sid(cli
, mem_ctx
, &domain_sid
,
150 if (!NT_STATUS_IS_OK(nt_status
)) {
155 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
156 if (lp_client_schannel()
157 && (ndr_syntax_id_equal(interface
,
158 &ndr_table_netlogon
.syntax_id
))) {
159 /* Always try and create an schannel netlogon pipe. */
160 nt_status
= cli_rpc_pipe_open_schannel(
162 PIPE_AUTH_LEVEL_PRIVACY
, domain_name
,
164 if (!NT_STATUS_IS_OK(nt_status
)) {
165 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
166 nt_errstr(nt_status
) ));
171 if (conn_flags
& NET_FLAGS_SEAL
) {
172 nt_status
= cli_rpc_pipe_open_ntlmssp(
174 PIPE_AUTH_LEVEL_PRIVACY
,
175 lp_workgroup(), c
->opt_user_name
,
176 c
->opt_password
, &pipe_hnd
);
178 nt_status
= cli_rpc_pipe_open_noauth(
182 if (!NT_STATUS_IS_OK(nt_status
)) {
183 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
184 cli_get_pipe_name_from_iface(
185 debug_ctx(), cli
, interface
),
186 nt_errstr(nt_status
) ));
193 nt_status
= fn(c
, domain_sid
, domain_name
, cli
, pipe_hnd
, mem_ctx
, argc
, argv
);
195 if (!NT_STATUS_IS_OK(nt_status
)) {
196 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status
)));
198 DEBUG(5, ("rpc command function succedded\n"));
201 if (!(conn_flags
& NET_FLAGS_NO_PIPE
)) {
203 TALLOC_FREE(pipe_hnd
);
207 /* close the connection only if it was opened here */
212 talloc_destroy(mem_ctx
);
213 return (!NT_STATUS_IS_OK(nt_status
));
217 * Force a change of the trust acccount password.
219 * All parameters are provided by the run_rpc_command function, except for
220 * argc, argv which are passed through.
222 * @param domain_sid The domain sid acquired from the remote server.
223 * @param cli A cli_state connected to the server.
224 * @param mem_ctx Talloc context, destroyed on completion of the function.
225 * @param argc Standard main() style argc.
226 * @param argv Standard main() style argv. Initial components are already
229 * @return Normal NTSTATUS return.
232 static NTSTATUS
rpc_changetrustpw_internals(struct net_context
*c
,
233 const DOM_SID
*domain_sid
,
234 const char *domain_name
,
235 struct cli_state
*cli
,
236 struct rpc_pipe_client
*pipe_hnd
,
242 return trust_pw_find_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
);
246 * Force a change of the trust acccount password.
248 * @param argc Standard main() style argc.
249 * @param argv Standard main() style argv. Initial components are already
252 * @return A shell status integer (0 for success).
255 int net_rpc_changetrustpw(struct net_context
*c
, int argc
, const char **argv
)
257 if (c
->display_usage
) {
259 "net rpc changetrustpw\n"
260 " Change the machine trust password\n");
264 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
265 NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
266 rpc_changetrustpw_internals
,
271 * Join a domain, the old way.
273 * This uses 'machinename' as the inital password, and changes it.
275 * The password should be created with 'server manager' or equiv first.
277 * All parameters are provided by the run_rpc_command function, except for
278 * argc, argv which are passed through.
280 * @param domain_sid The domain sid acquired from the remote server.
281 * @param cli A cli_state connected to the server.
282 * @param mem_ctx Talloc context, destroyed on completion of the function.
283 * @param argc Standard main() style argc.
284 * @param argv Standard main() style argv. Initial components are already
287 * @return Normal NTSTATUS return.
290 static NTSTATUS
rpc_oldjoin_internals(struct net_context
*c
,
291 const DOM_SID
*domain_sid
,
292 const char *domain_name
,
293 struct cli_state
*cli
,
294 struct rpc_pipe_client
*pipe_hnd
,
300 fstring trust_passwd
;
301 unsigned char orig_trust_passwd_hash
[16];
303 uint32 sec_channel_type
;
305 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
307 if (!NT_STATUS_IS_OK(result
)) {
308 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
311 nt_errstr(result
) ));
316 check what type of join - if the user want's to join as
317 a BDC, the server must agree that we are a BDC.
320 sec_channel_type
= get_sec_channel_type(argv
[0]);
322 sec_channel_type
= get_sec_channel_type(NULL
);
325 fstrcpy(trust_passwd
, global_myname());
326 strlower_m(trust_passwd
);
329 * Machine names can be 15 characters, but the max length on
330 * a password is 14. --jerry
333 trust_passwd
[14] = '\0';
335 E_md4hash(trust_passwd
, orig_trust_passwd_hash
);
337 result
= trust_pw_change_and_store_it(pipe_hnd
, mem_ctx
, c
->opt_target_workgroup
,
338 orig_trust_passwd_hash
,
341 if (NT_STATUS_IS_OK(result
))
342 printf("Joined domain %s.\n", c
->opt_target_workgroup
);
345 if (!secrets_store_domain_sid(c
->opt_target_workgroup
, domain_sid
)) {
346 DEBUG(0, ("error storing domain sid for %s\n", c
->opt_target_workgroup
));
347 result
= NT_STATUS_UNSUCCESSFUL
;
354 * Join a domain, the old way.
356 * @param argc Standard main() style argc.
357 * @param argv Standard main() style argv. Initial components are already
360 * @return A shell status integer (0 for success).
363 static int net_rpc_perform_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
365 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
366 NET_FLAGS_NO_PIPE
| NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
367 rpc_oldjoin_internals
,
372 * Join a domain, the old way. This function exists to allow
373 * the message to be displayed when oldjoin was explicitly
374 * requested, but not when it was implied by "net rpc join".
376 * @param argc Standard main() style argc.
377 * @param argv Standard main() style argv. Initial components are already
380 * @return A shell status integer (0 for success).
383 static int net_rpc_oldjoin(struct net_context
*c
, int argc
, const char **argv
)
387 if (c
->display_usage
) {
390 " Join a domain the old way\n");
394 rc
= net_rpc_perform_oldjoin(c
, argc
, argv
);
397 d_fprintf(stderr
, "Failed to join domain\n");
404 * 'net rpc join' entrypoint.
405 * @param argc Standard main() style argc.
406 * @param argv Standard main() style argv. Initial components are already
409 * Main 'net_rpc_join()' (where the admin username/password is used) is
411 * Try to just change the password, but if that doesn't work, use/prompt
412 * for a username/password.
415 int net_rpc_join(struct net_context
*c
, int argc
, const char **argv
)
417 if (c
->display_usage
) {
419 "net rpc join -U <username>[%%password] <type>\n"
421 " username\tName of the admin user"
422 " password\tPassword of the admin user, will "
423 "prompt if not specified\n"
424 " type\tCan be one of the following:\n"
425 "\t\tMEMBER\tJoin as member server (default)\n"
426 "\t\tBDC\tJoin as BDC\n"
427 "\t\tPDC\tJoin as PDC\n");
431 if (lp_server_role() == ROLE_STANDALONE
) {
432 d_printf("cannot join as standalone machine\n");
436 if (strlen(global_myname()) > 15) {
437 d_printf("Our netbios name can be at most 15 chars long, "
438 "\"%s\" is %u chars long\n",
439 global_myname(), (unsigned int)strlen(global_myname()));
443 if ((net_rpc_perform_oldjoin(c
, argc
, argv
) == 0))
446 return net_rpc_join_newstyle(c
, argc
, argv
);
450 * display info about a rpc domain
452 * All parameters are provided by the run_rpc_command function, except for
453 * argc, argv which are passed through.
455 * @param domain_sid The domain sid acquired from the remote server
456 * @param cli A cli_state connected to the server.
457 * @param mem_ctx Talloc context, destroyed on completion of the function.
458 * @param argc Standard main() style argc.
459 * @param argv Standard main() style argv. Initial components are already
462 * @return Normal NTSTATUS return.
465 NTSTATUS
rpc_info_internals(struct net_context
*c
,
466 const DOM_SID
*domain_sid
,
467 const char *domain_name
,
468 struct cli_state
*cli
,
469 struct rpc_pipe_client
*pipe_hnd
,
474 POLICY_HND connect_pol
, domain_pol
;
475 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
476 union samr_DomainInfo
*info
= NULL
;
479 sid_to_fstring(sid_str
, domain_sid
);
481 /* Get sam policy handle */
482 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
484 MAXIMUM_ALLOWED_ACCESS
,
486 if (!NT_STATUS_IS_OK(result
)) {
487 d_fprintf(stderr
, "Could not connect to SAM: %s\n", nt_errstr(result
));
491 /* Get domain policy handle */
492 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
494 MAXIMUM_ALLOWED_ACCESS
,
495 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
497 if (!NT_STATUS_IS_OK(result
)) {
498 d_fprintf(stderr
, "Could not open domain: %s\n", nt_errstr(result
));
502 result
= rpccli_samr_QueryDomainInfo(pipe_hnd
, mem_ctx
,
506 if (NT_STATUS_IS_OK(result
)) {
507 d_printf("Domain Name: %s\n", info
->info2
.domain_name
.string
);
508 d_printf("Domain SID: %s\n", sid_str
);
509 d_printf("Sequence number: %llu\n",
510 (unsigned long long)info
->info2
.sequence_num
);
511 d_printf("Num users: %u\n", info
->info2
.num_users
);
512 d_printf("Num domain groups: %u\n", info
->info2
.num_groups
);
513 d_printf("Num local groups: %u\n", info
->info2
.num_aliases
);
521 * 'net rpc info' entrypoint.
522 * @param argc Standard main() style argc.
523 * @param argv Standard main() style argv. Initial components are already
527 int net_rpc_info(struct net_context
*c
, int argc
, const char **argv
)
529 if (c
->display_usage
) {
532 " Display information about the domain\n");
536 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
,
537 NET_FLAGS_PDC
, rpc_info_internals
,
542 * Fetch domain SID into the local secrets.tdb.
544 * All parameters are provided by the run_rpc_command function, except for
545 * argc, argv which are passed through.
547 * @param domain_sid The domain sid acquired from the remote server.
548 * @param cli A cli_state connected to the server.
549 * @param mem_ctx Talloc context, destroyed on completion of the function.
550 * @param argc Standard main() style argc.
551 * @param argv Standard main() style argv. Initial components are already
554 * @return Normal NTSTATUS return.
557 static NTSTATUS
rpc_getsid_internals(struct net_context
*c
,
558 const DOM_SID
*domain_sid
,
559 const char *domain_name
,
560 struct cli_state
*cli
,
561 struct rpc_pipe_client
*pipe_hnd
,
568 sid_to_fstring(sid_str
, domain_sid
);
569 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
570 sid_str
, domain_name
);
572 if (!secrets_store_domain_sid(domain_name
, domain_sid
)) {
573 DEBUG(0,("Can't store domain SID\n"));
574 return NT_STATUS_UNSUCCESSFUL
;
581 * 'net rpc getsid' entrypoint.
582 * @param argc Standard main() style argc.
583 * @param argv Standard main() style argv. Initial components are already
587 int net_rpc_getsid(struct net_context
*c
, int argc
, const char **argv
)
589 if (c
->display_usage
) {
592 " Fetch domain SID into local secrets.tdb\n");
596 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
,
597 NET_FLAGS_ANONYMOUS
| NET_FLAGS_PDC
,
598 rpc_getsid_internals
,
602 /****************************************************************************/
605 * Basic usage function for 'net rpc user'.
606 * @param argc Standard main() style argc.
607 * @param argv Standard main() style argv. Initial components are already
611 static int rpc_user_usage(struct net_context
*c
, int argc
, const char **argv
)
613 return net_user_usage(c
, argc
, argv
);
617 * Add a new user to a remote RPC server.
619 * @param argc Standard main() style argc.
620 * @param argv Standard main() style argv. Initial components are already
623 * @return A shell status integer (0 for success).
626 static int rpc_user_add(struct net_context
*c
, int argc
, const char **argv
)
628 NET_API_STATUS status
;
629 struct USER_INFO_1 info1
;
630 uint32_t parm_error
= 0;
632 if (argc
< 1 || c
->display_usage
) {
633 rpc_user_usage(c
, argc
, argv
);
639 info1
.usri1_name
= argv
[0];
641 info1
.usri1_password
= argv
[1];
644 status
= NetUserAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
647 d_fprintf(stderr
, "Failed to add user '%s' with: %s.\n",
648 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
652 d_printf("Added user '%s'.\n", argv
[0]);
659 * Rename a user on a remote RPC server.
661 * @param argc Standard main() style argc.
662 * @param argv Standard main() style argv. Initial components are already
665 * @return A shell status integer (0 for success).
668 static int rpc_user_rename(struct net_context
*c
, int argc
, const char **argv
)
670 NET_API_STATUS status
;
671 struct USER_INFO_0 u0
;
672 uint32_t parm_err
= 0;
674 if (argc
!= 2 || c
->display_usage
) {
675 rpc_user_usage(c
, argc
, argv
);
679 u0
.usri0_name
= argv
[1];
681 status
= NetUserSetInfo(c
->opt_host
, argv
[0],
682 0, (uint8_t *)&u0
, &parm_err
);
684 d_fprintf(stderr
, "Failed to rename user from %s to %s - %s\n",
686 libnetapi_get_error_string(c
->netapi_ctx
, status
));
688 d_printf("Renamed user from %s to %s\n", argv
[0], argv
[1]);
695 * Delete a user from a remote RPC server.
697 * @param argc Standard main() style argc.
698 * @param argv Standard main() style argv. Initial components are already
701 * @return A shell status integer (0 for success).
704 static int rpc_user_delete(struct net_context
*c
, int argc
, const char **argv
)
706 NET_API_STATUS status
;
708 if (argc
< 1 || c
->display_usage
) {
709 rpc_user_usage(c
, argc
, argv
);
713 status
= NetUserDel(c
->opt_host
, argv
[0]);
716 d_fprintf(stderr
, "Failed to delete user '%s' with: %s.\n",
718 libnetapi_get_error_string(c
->netapi_ctx
, status
));
721 d_printf("Deleted user '%s'.\n", argv
[0]);
728 * Set a user's password on a remote RPC server.
730 * @param argc Standard main() style argc.
731 * @param argv Standard main() style argv. Initial components are already
734 * @return A shell status integer (0 for success).
737 static int rpc_user_password(struct net_context
*c
, int argc
, const char **argv
)
739 NET_API_STATUS status
;
741 struct USER_INFO_1003 u1003
;
742 uint32_t parm_err
= 0;
744 if (argc
< 1 || c
->display_usage
) {
745 rpc_user_usage(c
, argc
, argv
);
750 u1003
.usri1003_password
= argv
[1];
752 asprintf(&prompt
, "Enter new password for %s:", argv
[0]);
753 u1003
.usri1003_password
= getpass(prompt
);
757 status
= NetUserSetInfo(c
->opt_host
, argv
[0], 1003, (uint8_t *)&u1003
, &parm_err
);
759 /* Display results */
761 d_fprintf(stderr
, "Failed to set password for '%s' with: %s.\n",
762 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
771 * List a user's groups from a remote RPC server.
773 * @param argc Standard main() style argc.
774 * @param argv Standard main() style argv. Initial components are already
777 * @return A shell status integer (0 for success)
780 static int rpc_user_info(struct net_context
*c
, int argc
, const char **argv
)
783 NET_API_STATUS status
;
784 struct GROUP_USERS_INFO_0
*u0
= NULL
;
785 uint32_t entries_read
= 0;
786 uint32_t total_entries
= 0;
790 if (argc
< 1 || c
->display_usage
) {
791 rpc_user_usage(c
, argc
, argv
);
795 status
= NetUserGetGroups(c
->opt_host
,
803 d_fprintf(stderr
, "Failed to get groups for '%s' with: %s.\n",
804 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
809 for (i
=0; i
< entries_read
; i
++) {
810 printf("%s\n", u0
->grui0_name
);
818 * List users on a remote RPC server.
820 * All parameters are provided by the run_rpc_command function, except for
821 * argc, argv which are passed through.
823 * @param domain_sid The domain sid acquired from the remote server.
824 * @param cli A cli_state connected to the server.
825 * @param mem_ctx Talloc context, destroyed on completion of the function.
826 * @param argc Standard main() style argc.
827 * @param argv Standard main() style argv. Initial components are already
830 * @return Normal NTSTATUS return.
833 static int rpc_user_list(struct net_context
*c
, int argc
, const char **argv
)
835 NET_API_STATUS status
;
836 uint32_t start_idx
=0, num_entries
, i
, loop_count
= 0;
837 struct NET_DISPLAY_USER
*info
= NULL
;
840 /* Query domain users */
841 if (c
->opt_long_list_entries
)
842 d_printf("\nUser name Comment"
843 "\n-----------------------------\n");
845 uint32_t max_entries
, max_size
;
847 get_query_dispinfo_params(
848 loop_count
, &max_entries
, &max_size
);
850 status
= NetQueryDisplayInformation(c
->opt_host
,
857 if (status
!= 0 && status
!= ERROR_MORE_DATA
) {
861 info
= (struct NET_DISPLAY_USER
*)buffer
;
863 for (i
= 0; i
< num_entries
; i
++) {
865 if (c
->opt_long_list_entries
)
866 printf("%-21.21s %s\n", info
->usri1_name
,
867 info
->usri1_comment
);
869 printf("%s\n", info
->usri1_name
);
873 NetApiBufferFree(buffer
);
876 start_idx
+= num_entries
;
878 } while (status
== ERROR_MORE_DATA
);
884 * 'net rpc user' entrypoint.
885 * @param argc Standard main() style argc.
886 * @param argv Standard main() style argv. Initial components are already
890 int net_rpc_user(struct net_context
*c
, int argc
, const char **argv
)
892 NET_API_STATUS status
;
894 struct functable func
[] = {
899 "Add specified user",
901 " Add specified user"
907 "List domain groups of user",
908 "net rpc user info\n"
909 " Lis domain groups of user"
915 "Remove specified user",
916 "net rpc user delete\n"
917 " Remove specified user"
923 "Change user password",
924 "net rpc user password\n"
925 " Change user password"
931 "Rename specified user",
932 "net rpc user rename\n"
933 " Rename specified user"
935 {NULL
, NULL
, 0, NULL
, NULL
}
938 status
= libnetapi_init(&c
->netapi_ctx
);
942 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
943 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
944 if (c
->opt_kerberos
) {
945 libnetapi_set_use_kerberos(c
->netapi_ctx
);
949 if (c
->display_usage
) {
950 d_printf("Usage:\n");
951 d_printf("net rpc user\n"
952 " List all users\n");
953 net_display_usage_from_functable(func
);
957 return rpc_user_list(c
, argc
, argv
);
960 return net_run_function(c
, argc
, argv
, "net rpc user", func
);
963 static NTSTATUS
rpc_sh_user_list(struct net_context
*c
,
965 struct rpc_sh_ctx
*ctx
,
966 struct rpc_pipe_client
*pipe_hnd
,
967 int argc
, const char **argv
)
969 return werror_to_ntstatus(W_ERROR(rpc_user_list(c
, argc
, argv
)));
972 static NTSTATUS
rpc_sh_user_info(struct net_context
*c
,
974 struct rpc_sh_ctx
*ctx
,
975 struct rpc_pipe_client
*pipe_hnd
,
976 int argc
, const char **argv
)
978 return werror_to_ntstatus(W_ERROR(rpc_user_info(c
, argc
, argv
)));
981 static NTSTATUS
rpc_sh_handle_user(struct net_context
*c
,
983 struct rpc_sh_ctx
*ctx
,
984 struct rpc_pipe_client
*pipe_hnd
,
985 int argc
, const char **argv
,
987 struct net_context
*c
,
989 struct rpc_sh_ctx
*ctx
,
990 struct rpc_pipe_client
*pipe_hnd
,
991 POLICY_HND
*user_hnd
,
992 int argc
, const char **argv
))
994 POLICY_HND connect_pol
, domain_pol
, user_pol
;
995 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
998 enum lsa_SidType type
;
1001 d_fprintf(stderr
, "usage: %s <username>\n", ctx
->whoami
);
1002 return NT_STATUS_INVALID_PARAMETER
;
1005 ZERO_STRUCT(connect_pol
);
1006 ZERO_STRUCT(domain_pol
);
1007 ZERO_STRUCT(user_pol
);
1009 result
= net_rpc_lookup_name(c
, mem_ctx
, rpc_pipe_np_smb_conn(pipe_hnd
),
1010 argv
[0], NULL
, NULL
, &sid
, &type
);
1011 if (!NT_STATUS_IS_OK(result
)) {
1012 d_fprintf(stderr
, "Could not lookup %s: %s\n", argv
[0],
1017 if (type
!= SID_NAME_USER
) {
1018 d_fprintf(stderr
, "%s is a %s, not a user\n", argv
[0],
1019 sid_type_lookup(type
));
1020 result
= NT_STATUS_NO_SUCH_USER
;
1024 if (!sid_peek_check_rid(ctx
->domain_sid
, &sid
, &rid
)) {
1025 d_fprintf(stderr
, "%s is not in our domain\n", argv
[0]);
1026 result
= NT_STATUS_NO_SUCH_USER
;
1030 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1032 MAXIMUM_ALLOWED_ACCESS
,
1034 if (!NT_STATUS_IS_OK(result
)) {
1038 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1040 MAXIMUM_ALLOWED_ACCESS
,
1043 if (!NT_STATUS_IS_OK(result
)) {
1047 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1049 MAXIMUM_ALLOWED_ACCESS
,
1052 if (!NT_STATUS_IS_OK(result
)) {
1056 result
= fn(c
, mem_ctx
, ctx
, pipe_hnd
, &user_pol
, argc
-1, argv
+1);
1059 if (is_valid_policy_hnd(&user_pol
)) {
1060 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1062 if (is_valid_policy_hnd(&domain_pol
)) {
1063 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1065 if (is_valid_policy_hnd(&connect_pol
)) {
1066 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1071 static NTSTATUS
rpc_sh_user_show_internals(struct net_context
*c
,
1072 TALLOC_CTX
*mem_ctx
,
1073 struct rpc_sh_ctx
*ctx
,
1074 struct rpc_pipe_client
*pipe_hnd
,
1075 POLICY_HND
*user_hnd
,
1076 int argc
, const char **argv
)
1079 union samr_UserInfo
*info
= NULL
;
1082 d_fprintf(stderr
, "usage: %s show <username>\n", ctx
->whoami
);
1083 return NT_STATUS_INVALID_PARAMETER
;
1086 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1090 if (!NT_STATUS_IS_OK(result
)) {
1094 d_printf("user rid: %d, group rid: %d\n",
1096 info
->info21
.primary_gid
);
1101 static NTSTATUS
rpc_sh_user_show(struct net_context
*c
,
1102 TALLOC_CTX
*mem_ctx
,
1103 struct rpc_sh_ctx
*ctx
,
1104 struct rpc_pipe_client
*pipe_hnd
,
1105 int argc
, const char **argv
)
1107 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1108 rpc_sh_user_show_internals
);
1111 #define FETCHSTR(name, rec) \
1112 do { if (strequal(ctx->thiscmd, name)) { \
1113 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1116 #define SETSTR(name, rec, flag) \
1117 do { if (strequal(ctx->thiscmd, name)) { \
1118 init_lsa_String(&(info->info21.rec), argv[0]); \
1119 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1122 static NTSTATUS
rpc_sh_user_str_edit_internals(struct net_context
*c
,
1123 TALLOC_CTX
*mem_ctx
,
1124 struct rpc_sh_ctx
*ctx
,
1125 struct rpc_pipe_client
*pipe_hnd
,
1126 POLICY_HND
*user_hnd
,
1127 int argc
, const char **argv
)
1130 const char *username
;
1131 const char *oldval
= "";
1132 union samr_UserInfo
*info
= NULL
;
1135 d_fprintf(stderr
, "usage: %s <username> [new value|NULL]\n",
1137 return NT_STATUS_INVALID_PARAMETER
;
1140 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1144 if (!NT_STATUS_IS_OK(result
)) {
1148 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1150 FETCHSTR("fullname", full_name
);
1151 FETCHSTR("homedir", home_directory
);
1152 FETCHSTR("homedrive", home_drive
);
1153 FETCHSTR("logonscript", logon_script
);
1154 FETCHSTR("profilepath", profile_path
);
1155 FETCHSTR("description", description
);
1158 d_printf("%s's %s: [%s]\n", username
, ctx
->thiscmd
, oldval
);
1162 if (strcmp(argv
[0], "NULL") == 0) {
1166 ZERO_STRUCT(info
->info21
);
1168 SETSTR("fullname", full_name
, FULL_NAME
);
1169 SETSTR("homedir", home_directory
, HOME_DIRECTORY
);
1170 SETSTR("homedrive", home_drive
, HOME_DRIVE
);
1171 SETSTR("logonscript", logon_script
, LOGON_SCRIPT
);
1172 SETSTR("profilepath", profile_path
, PROFILE_PATH
);
1173 SETSTR("description", description
, DESCRIPTION
);
1175 result
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1180 d_printf("Set %s's %s from [%s] to [%s]\n", username
,
1181 ctx
->thiscmd
, oldval
, argv
[0]);
1188 #define HANDLEFLG(name, rec) \
1189 do { if (strequal(ctx->thiscmd, name)) { \
1190 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1192 newflags = oldflags | ACB_##rec; \
1194 newflags = oldflags & ~ACB_##rec; \
1197 static NTSTATUS
rpc_sh_user_str_edit(struct net_context
*c
,
1198 TALLOC_CTX
*mem_ctx
,
1199 struct rpc_sh_ctx
*ctx
,
1200 struct rpc_pipe_client
*pipe_hnd
,
1201 int argc
, const char **argv
)
1203 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1204 rpc_sh_user_str_edit_internals
);
1207 static NTSTATUS
rpc_sh_user_flag_edit_internals(struct net_context
*c
,
1208 TALLOC_CTX
*mem_ctx
,
1209 struct rpc_sh_ctx
*ctx
,
1210 struct rpc_pipe_client
*pipe_hnd
,
1211 POLICY_HND
*user_hnd
,
1212 int argc
, const char **argv
)
1215 const char *username
;
1216 const char *oldval
= "unknown";
1217 uint32 oldflags
, newflags
;
1219 union samr_UserInfo
*info
= NULL
;
1222 ((argc
== 1) && !strequal(argv
[0], "yes") &&
1223 !strequal(argv
[0], "no"))) {
1224 d_fprintf(stderr
, "usage: %s <username> [yes|no]\n",
1226 return NT_STATUS_INVALID_PARAMETER
;
1229 newval
= strequal(argv
[0], "yes");
1231 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1235 if (!NT_STATUS_IS_OK(result
)) {
1239 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1240 oldflags
= info
->info21
.acct_flags
;
1241 newflags
= info
->info21
.acct_flags
;
1243 HANDLEFLG("disabled", DISABLED
);
1244 HANDLEFLG("pwnotreq", PWNOTREQ
);
1245 HANDLEFLG("autolock", AUTOLOCK
);
1246 HANDLEFLG("pwnoexp", PWNOEXP
);
1249 d_printf("%s's %s flag: %s\n", username
, ctx
->thiscmd
, oldval
);
1253 ZERO_STRUCT(info
->info21
);
1255 info
->info21
.acct_flags
= newflags
;
1256 info
->info21
.fields_present
= SAMR_FIELD_ACCT_FLAGS
;
1258 result
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1263 if (NT_STATUS_IS_OK(result
)) {
1264 d_printf("Set %s's %s flag from [%s] to [%s]\n", username
,
1265 ctx
->thiscmd
, oldval
, argv
[0]);
1273 static NTSTATUS
rpc_sh_user_flag_edit(struct net_context
*c
,
1274 TALLOC_CTX
*mem_ctx
,
1275 struct rpc_sh_ctx
*ctx
,
1276 struct rpc_pipe_client
*pipe_hnd
,
1277 int argc
, const char **argv
)
1279 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1280 rpc_sh_user_flag_edit_internals
);
1283 struct rpc_sh_cmd
*net_rpc_user_edit_cmds(struct net_context
*c
,
1284 TALLOC_CTX
*mem_ctx
,
1285 struct rpc_sh_ctx
*ctx
)
1287 static struct rpc_sh_cmd cmds
[] = {
1289 { "fullname", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1290 "Show/Set a user's full name" },
1292 { "homedir", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1293 "Show/Set a user's home directory" },
1295 { "homedrive", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1296 "Show/Set a user's home drive" },
1298 { "logonscript", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1299 "Show/Set a user's logon script" },
1301 { "profilepath", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1302 "Show/Set a user's profile path" },
1304 { "description", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1305 "Show/Set a user's description" },
1307 { "disabled", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1308 "Show/Set whether a user is disabled" },
1310 { "autolock", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1311 "Show/Set whether a user locked out" },
1313 { "pwnotreq", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1314 "Show/Set whether a user does not need a password" },
1316 { "pwnoexp", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1317 "Show/Set whether a user's password does not expire" },
1319 { NULL
, NULL
, 0, NULL
, NULL
}
1325 struct rpc_sh_cmd
*net_rpc_user_cmds(struct net_context
*c
,
1326 TALLOC_CTX
*mem_ctx
,
1327 struct rpc_sh_ctx
*ctx
)
1329 static struct rpc_sh_cmd cmds
[] = {
1331 { "list", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_list
,
1332 "List available users" },
1334 { "info", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_info
,
1335 "List the domain groups a user is member of" },
1337 { "show", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_show
,
1338 "Show info about a user" },
1340 { "edit", net_rpc_user_edit_cmds
, 0, NULL
,
1341 "Show/Modify a user's fields" },
1343 { NULL
, NULL
, 0, NULL
, NULL
}
1349 /****************************************************************************/
1352 * Basic usage function for 'net rpc group'.
1353 * @param argc Standard main() style argc.
1354 * @param argv Standard main() style argv. Initial components are already
1358 static int rpc_group_usage(struct net_context
*c
, int argc
, const char **argv
)
1360 return net_group_usage(c
, argc
, argv
);
1364 * Delete group on a remote RPC server.
1366 * All parameters are provided by the run_rpc_command function, except for
1367 * argc, argv which are passed through.
1369 * @param domain_sid The domain sid acquired from the remote server.
1370 * @param cli A cli_state connected to the server.
1371 * @param mem_ctx Talloc context, destroyed on completion of the function.
1372 * @param argc Standard main() style argc.
1373 * @param argv Standard main() style argv. Initial components are already
1376 * @return Normal NTSTATUS return.
1379 static NTSTATUS
rpc_group_delete_internals(struct net_context
*c
,
1380 const DOM_SID
*domain_sid
,
1381 const char *domain_name
,
1382 struct cli_state
*cli
,
1383 struct rpc_pipe_client
*pipe_hnd
,
1384 TALLOC_CTX
*mem_ctx
,
1388 POLICY_HND connect_pol
, domain_pol
, group_pol
, user_pol
;
1389 bool group_is_primary
= false;
1390 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1392 struct samr_RidTypeArray
*rids
= NULL
;
1395 /* DOM_GID *user_gids; */
1397 struct samr_Ids group_rids
, name_types
;
1398 struct lsa_String lsa_acct_name
;
1399 union samr_UserInfo
*info
= NULL
;
1401 if (argc
< 1 || c
->display_usage
) {
1402 rpc_group_usage(c
, argc
,argv
);
1403 return NT_STATUS_OK
; /* ok? */
1406 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1408 MAXIMUM_ALLOWED_ACCESS
,
1411 if (!NT_STATUS_IS_OK(result
)) {
1412 d_fprintf(stderr
, "Request samr_Connect2 failed\n");
1416 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1418 MAXIMUM_ALLOWED_ACCESS
,
1419 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1422 if (!NT_STATUS_IS_OK(result
)) {
1423 d_fprintf(stderr
, "Request open_domain failed\n");
1427 init_lsa_String(&lsa_acct_name
, argv
[0]);
1429 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1435 if (!NT_STATUS_IS_OK(result
)) {
1436 d_fprintf(stderr
, "Lookup of '%s' failed\n",argv
[0]);
1440 switch (name_types
.ids
[0])
1442 case SID_NAME_DOM_GRP
:
1443 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
1445 MAXIMUM_ALLOWED_ACCESS
,
1448 if (!NT_STATUS_IS_OK(result
)) {
1449 d_fprintf(stderr
, "Request open_group failed");
1453 group_rid
= group_rids
.ids
[0];
1455 result
= rpccli_samr_QueryGroupMember(pipe_hnd
, mem_ctx
,
1459 if (!NT_STATUS_IS_OK(result
)) {
1460 d_fprintf(stderr
, "Unable to query group members of %s",argv
[0]);
1464 if (c
->opt_verbose
) {
1465 d_printf("Domain Group %s (rid: %d) has %d members\n",
1466 argv
[0],group_rid
, rids
->count
);
1469 /* Check if group is anyone's primary group */
1470 for (i
= 0; i
< rids
->count
; i
++)
1472 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1474 MAXIMUM_ALLOWED_ACCESS
,
1478 if (!NT_STATUS_IS_OK(result
)) {
1479 d_fprintf(stderr
, "Unable to open group member %d\n",
1484 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1489 if (!NT_STATUS_IS_OK(result
)) {
1490 d_fprintf(stderr
, "Unable to lookup userinfo for group member %d\n",
1495 if (info
->info21
.primary_gid
== group_rid
) {
1496 if (c
->opt_verbose
) {
1497 d_printf("Group is primary group of %s\n",
1498 info
->info21
.account_name
.string
);
1500 group_is_primary
= true;
1503 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1506 if (group_is_primary
) {
1507 d_fprintf(stderr
, "Unable to delete group because some "
1508 "of it's members have it as primary group\n");
1509 result
= NT_STATUS_MEMBERS_PRIMARY_GROUP
;
1513 /* remove all group members */
1514 for (i
= 0; i
< rids
->count
; i
++)
1517 d_printf("Remove group member %d...",
1519 result
= rpccli_samr_DeleteGroupMember(pipe_hnd
, mem_ctx
,
1523 if (NT_STATUS_IS_OK(result
)) {
1528 d_printf("failed\n");
1533 result
= rpccli_samr_DeleteDomainGroup(pipe_hnd
, mem_ctx
,
1537 /* removing a local group is easier... */
1538 case SID_NAME_ALIAS
:
1539 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
1541 MAXIMUM_ALLOWED_ACCESS
,
1545 if (!NT_STATUS_IS_OK(result
)) {
1546 d_fprintf(stderr
, "Request open_alias failed\n");
1550 result
= rpccli_samr_DeleteDomAlias(pipe_hnd
, mem_ctx
,
1554 d_fprintf(stderr
, "%s is of type %s. This command is only for deleting local or global groups\n",
1555 argv
[0],sid_type_lookup(name_types
.ids
[0]));
1556 result
= NT_STATUS_UNSUCCESSFUL
;
1560 if (NT_STATUS_IS_OK(result
)) {
1562 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types
.ids
[0]),argv
[0]);
1564 d_fprintf(stderr
, "Deleting of %s failed: %s\n",argv
[0],
1565 get_friendly_nt_error_msg(result
));
1573 static int rpc_group_delete(struct net_context
*c
, int argc
, const char **argv
)
1575 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
1576 rpc_group_delete_internals
, argc
,argv
);
1579 static int rpc_group_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1581 NET_API_STATUS status
;
1582 struct GROUP_INFO_1 info1
;
1583 uint32_t parm_error
= 0;
1585 if (argc
!= 1 || c
->display_usage
) {
1586 rpc_group_usage(c
, argc
, argv
);
1592 info1
.grpi1_name
= argv
[0];
1593 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1594 info1
.grpi1_comment
= c
->opt_comment
;
1597 status
= NetGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1600 d_fprintf(stderr
, "Failed to add group '%s' with: %s.\n",
1601 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1605 d_printf("Added group '%s'.\n", argv
[0]);
1611 static int rpc_alias_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1613 NET_API_STATUS status
;
1614 struct LOCALGROUP_INFO_1 info1
;
1615 uint32_t parm_error
= 0;
1617 if (argc
!= 1 || c
->display_usage
) {
1618 rpc_group_usage(c
, argc
, argv
);
1624 info1
.lgrpi1_name
= argv
[0];
1625 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1626 info1
.lgrpi1_comment
= c
->opt_comment
;
1629 status
= NetLocalGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1632 d_fprintf(stderr
, "Failed to add alias '%s' with: %s.\n",
1633 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1637 d_printf("Added alias '%s'.\n", argv
[0]);
1643 static int rpc_group_add(struct net_context
*c
, int argc
, const char **argv
)
1645 if (c
->opt_localgroup
)
1646 return rpc_alias_add_internals(c
, argc
, argv
);
1648 return rpc_group_add_internals(c
, argc
, argv
);
1651 static NTSTATUS
get_sid_from_name(struct cli_state
*cli
,
1652 TALLOC_CTX
*mem_ctx
,
1655 enum lsa_SidType
*type
)
1657 DOM_SID
*sids
= NULL
;
1658 enum lsa_SidType
*types
= NULL
;
1659 struct rpc_pipe_client
*pipe_hnd
;
1661 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1663 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
1665 if (!NT_STATUS_IS_OK(result
)) {
1669 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, false,
1670 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
1672 if (!NT_STATUS_IS_OK(result
)) {
1676 result
= rpccli_lsa_lookup_names(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
1677 &name
, NULL
, 1, &sids
, &types
);
1679 if (NT_STATUS_IS_OK(result
)) {
1680 sid_copy(sid
, &sids
[0]);
1684 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
1688 TALLOC_FREE(pipe_hnd
);
1691 if (!NT_STATUS_IS_OK(result
) && (StrnCaseCmp(name
, "S-", 2) == 0)) {
1693 /* Try as S-1-5-whatever */
1697 if (string_to_sid(&tmp_sid
, name
)) {
1698 sid_copy(sid
, &tmp_sid
);
1699 *type
= SID_NAME_UNKNOWN
;
1700 result
= NT_STATUS_OK
;
1707 static NTSTATUS
rpc_add_groupmem(struct rpc_pipe_client
*pipe_hnd
,
1708 TALLOC_CTX
*mem_ctx
,
1709 const DOM_SID
*group_sid
,
1712 POLICY_HND connect_pol
, domain_pol
;
1715 POLICY_HND group_pol
;
1717 struct samr_Ids rids
, rid_types
;
1718 struct lsa_String lsa_acct_name
;
1722 sid_copy(&sid
, group_sid
);
1724 if (!sid_split_rid(&sid
, &group_rid
)) {
1725 return NT_STATUS_UNSUCCESSFUL
;
1728 /* Get sam policy handle */
1729 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1731 MAXIMUM_ALLOWED_ACCESS
,
1733 if (!NT_STATUS_IS_OK(result
)) {
1737 /* Get domain policy handle */
1738 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1740 MAXIMUM_ALLOWED_ACCESS
,
1743 if (!NT_STATUS_IS_OK(result
)) {
1747 init_lsa_String(&lsa_acct_name
, member
);
1749 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1756 if (!NT_STATUS_IS_OK(result
)) {
1757 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
1761 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
1763 MAXIMUM_ALLOWED_ACCESS
,
1767 if (!NT_STATUS_IS_OK(result
)) {
1771 result
= rpccli_samr_AddGroupMember(pipe_hnd
, mem_ctx
,
1774 0x0005); /* unknown flags */
1777 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1781 static NTSTATUS
rpc_add_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
1782 TALLOC_CTX
*mem_ctx
,
1783 const DOM_SID
*alias_sid
,
1786 POLICY_HND connect_pol
, domain_pol
;
1789 POLICY_HND alias_pol
;
1792 enum lsa_SidType member_type
;
1796 sid_copy(&sid
, alias_sid
);
1798 if (!sid_split_rid(&sid
, &alias_rid
)) {
1799 return NT_STATUS_UNSUCCESSFUL
;
1802 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
1803 member
, &member_sid
, &member_type
);
1805 if (!NT_STATUS_IS_OK(result
)) {
1806 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
1810 /* Get sam policy handle */
1811 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1813 MAXIMUM_ALLOWED_ACCESS
,
1815 if (!NT_STATUS_IS_OK(result
)) {
1819 /* Get domain policy handle */
1820 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1822 MAXIMUM_ALLOWED_ACCESS
,
1825 if (!NT_STATUS_IS_OK(result
)) {
1829 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
1831 MAXIMUM_ALLOWED_ACCESS
,
1835 if (!NT_STATUS_IS_OK(result
)) {
1839 result
= rpccli_samr_AddAliasMember(pipe_hnd
, mem_ctx
,
1843 if (!NT_STATUS_IS_OK(result
)) {
1848 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1852 static NTSTATUS
rpc_group_addmem_internals(struct net_context
*c
,
1853 const DOM_SID
*domain_sid
,
1854 const char *domain_name
,
1855 struct cli_state
*cli
,
1856 struct rpc_pipe_client
*pipe_hnd
,
1857 TALLOC_CTX
*mem_ctx
,
1862 enum lsa_SidType group_type
;
1864 if (argc
!= 2 || c
->display_usage
) {
1866 "net rpc group addmem <group> <member>\n"
1867 " Add a member to a group\n"
1868 " group\tGroup to add member to\n"
1869 " member\tMember to add to group\n");
1870 return NT_STATUS_UNSUCCESSFUL
;
1873 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
1874 &group_sid
, &group_type
))) {
1875 d_fprintf(stderr
, "Could not lookup group name %s\n", argv
[0]);
1876 return NT_STATUS_UNSUCCESSFUL
;
1879 if (group_type
== SID_NAME_DOM_GRP
) {
1880 NTSTATUS result
= rpc_add_groupmem(pipe_hnd
, mem_ctx
,
1881 &group_sid
, argv
[1]);
1883 if (!NT_STATUS_IS_OK(result
)) {
1884 d_fprintf(stderr
, "Could not add %s to %s: %s\n",
1885 argv
[1], argv
[0], nt_errstr(result
));
1890 if (group_type
== SID_NAME_ALIAS
) {
1891 NTSTATUS result
= rpc_add_aliasmem(pipe_hnd
, mem_ctx
,
1892 &group_sid
, argv
[1]);
1894 if (!NT_STATUS_IS_OK(result
)) {
1895 d_fprintf(stderr
, "Could not add %s to %s: %s\n",
1896 argv
[1], argv
[0], nt_errstr(result
));
1901 d_fprintf(stderr
, "Can only add members to global or local groups "
1902 "which %s is not\n", argv
[0]);
1904 return NT_STATUS_UNSUCCESSFUL
;
1907 static int rpc_group_addmem(struct net_context
*c
, int argc
, const char **argv
)
1909 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
1910 rpc_group_addmem_internals
,
1914 static NTSTATUS
rpc_del_groupmem(struct net_context
*c
,
1915 struct rpc_pipe_client
*pipe_hnd
,
1916 TALLOC_CTX
*mem_ctx
,
1917 const DOM_SID
*group_sid
,
1920 POLICY_HND connect_pol
, domain_pol
;
1923 POLICY_HND group_pol
;
1925 struct samr_Ids rids
, rid_types
;
1926 struct lsa_String lsa_acct_name
;
1930 sid_copy(&sid
, group_sid
);
1932 if (!sid_split_rid(&sid
, &group_rid
))
1933 return NT_STATUS_UNSUCCESSFUL
;
1935 /* Get sam policy handle */
1936 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1938 MAXIMUM_ALLOWED_ACCESS
,
1940 if (!NT_STATUS_IS_OK(result
))
1943 /* Get domain policy handle */
1944 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1946 MAXIMUM_ALLOWED_ACCESS
,
1949 if (!NT_STATUS_IS_OK(result
))
1952 init_lsa_String(&lsa_acct_name
, member
);
1954 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1960 if (!NT_STATUS_IS_OK(result
)) {
1961 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
1965 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
1967 MAXIMUM_ALLOWED_ACCESS
,
1971 if (!NT_STATUS_IS_OK(result
))
1974 result
= rpccli_samr_DeleteGroupMember(pipe_hnd
, mem_ctx
,
1979 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1983 static NTSTATUS
rpc_del_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
1984 TALLOC_CTX
*mem_ctx
,
1985 const DOM_SID
*alias_sid
,
1988 POLICY_HND connect_pol
, domain_pol
;
1991 POLICY_HND alias_pol
;
1994 enum lsa_SidType member_type
;
1998 sid_copy(&sid
, alias_sid
);
2000 if (!sid_split_rid(&sid
, &alias_rid
))
2001 return NT_STATUS_UNSUCCESSFUL
;
2003 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2004 member
, &member_sid
, &member_type
);
2006 if (!NT_STATUS_IS_OK(result
)) {
2007 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2011 /* Get sam policy handle */
2012 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2014 MAXIMUM_ALLOWED_ACCESS
,
2016 if (!NT_STATUS_IS_OK(result
)) {
2020 /* Get domain policy handle */
2021 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2023 MAXIMUM_ALLOWED_ACCESS
,
2026 if (!NT_STATUS_IS_OK(result
)) {
2030 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2032 MAXIMUM_ALLOWED_ACCESS
,
2036 if (!NT_STATUS_IS_OK(result
))
2039 result
= rpccli_samr_DeleteAliasMember(pipe_hnd
, mem_ctx
,
2043 if (!NT_STATUS_IS_OK(result
))
2047 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2051 static NTSTATUS
rpc_group_delmem_internals(struct net_context
*c
,
2052 const DOM_SID
*domain_sid
,
2053 const char *domain_name
,
2054 struct cli_state
*cli
,
2055 struct rpc_pipe_client
*pipe_hnd
,
2056 TALLOC_CTX
*mem_ctx
,
2061 enum lsa_SidType group_type
;
2063 if (argc
!= 2 || c
->display_usage
) {
2065 "net rpc group delmem <group> <member>\n"
2066 " Delete a member from a group\n"
2067 " group\tGroup to delete member from\n"
2068 " member\tMember to delete from group\n");
2069 return NT_STATUS_UNSUCCESSFUL
;
2072 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2073 &group_sid
, &group_type
))) {
2074 d_fprintf(stderr
, "Could not lookup group name %s\n", argv
[0]);
2075 return NT_STATUS_UNSUCCESSFUL
;
2078 if (group_type
== SID_NAME_DOM_GRP
) {
2079 NTSTATUS result
= rpc_del_groupmem(c
, pipe_hnd
, mem_ctx
,
2080 &group_sid
, argv
[1]);
2082 if (!NT_STATUS_IS_OK(result
)) {
2083 d_fprintf(stderr
, "Could not del %s from %s: %s\n",
2084 argv
[1], argv
[0], nt_errstr(result
));
2089 if (group_type
== SID_NAME_ALIAS
) {
2090 NTSTATUS result
= rpc_del_aliasmem(pipe_hnd
, mem_ctx
,
2091 &group_sid
, argv
[1]);
2093 if (!NT_STATUS_IS_OK(result
)) {
2094 d_fprintf(stderr
, "Could not del %s from %s: %s\n",
2095 argv
[1], argv
[0], nt_errstr(result
));
2100 d_fprintf(stderr
, "Can only delete members from global or local groups "
2101 "which %s is not\n", argv
[0]);
2103 return NT_STATUS_UNSUCCESSFUL
;
2106 static int rpc_group_delmem(struct net_context
*c
, int argc
, const char **argv
)
2108 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2109 rpc_group_delmem_internals
,
2114 * List groups on a remote RPC server.
2116 * All parameters are provided by the run_rpc_command function, except for
2117 * argc, argv which are passes through.
2119 * @param domain_sid The domain sid acquired from the remote server.
2120 * @param cli A cli_state connected to the server.
2121 * @param mem_ctx Talloc context, destroyed on completion of the function.
2122 * @param argc Standard main() style argc.
2123 * @param argv Standard main() style argv. Initial components are already
2126 * @return Normal NTSTATUS return.
2129 static NTSTATUS
rpc_group_list_internals(struct net_context
*c
,
2130 const DOM_SID
*domain_sid
,
2131 const char *domain_name
,
2132 struct cli_state
*cli
,
2133 struct rpc_pipe_client
*pipe_hnd
,
2134 TALLOC_CTX
*mem_ctx
,
2138 POLICY_HND connect_pol
, domain_pol
;
2139 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2140 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
2141 struct samr_SamArray
*groups
= NULL
;
2142 bool global
= false;
2144 bool builtin
= false;
2146 if (c
->display_usage
) {
2148 "net rpc group list [global] [local] [builtin]\n"
2149 " List groups on RPC server\n"
2150 " global\tList global groups\n"
2151 " local\tList local groups\n"
2152 " builtin\tList builtin groups\n"
2153 " If none of global, local or builtin is "
2154 "specified, all three options are considered set\n");
2155 return NT_STATUS_OK
;
2164 for (i
=0; i
<argc
; i
++) {
2165 if (strequal(argv
[i
], "global"))
2168 if (strequal(argv
[i
], "local"))
2171 if (strequal(argv
[i
], "builtin"))
2175 /* Get sam policy handle */
2177 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2179 MAXIMUM_ALLOWED_ACCESS
,
2181 if (!NT_STATUS_IS_OK(result
)) {
2185 /* Get domain policy handle */
2187 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2189 MAXIMUM_ALLOWED_ACCESS
,
2190 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2192 if (!NT_STATUS_IS_OK(result
)) {
2196 /* Query domain groups */
2197 if (c
->opt_long_list_entries
)
2198 d_printf("\nGroup name Comment"
2199 "\n-----------------------------\n");
2201 uint32_t max_size
, total_size
, returned_size
;
2202 union samr_DispInfo info
;
2206 get_query_dispinfo_params(
2207 loop_count
, &max_entries
, &max_size
);
2209 result
= rpccli_samr_QueryDisplayInfo(pipe_hnd
, mem_ctx
,
2218 num_entries
= info
.info3
.count
;
2219 start_idx
+= info
.info3
.count
;
2221 if (!NT_STATUS_IS_OK(result
) &&
2222 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2225 for (i
= 0; i
< num_entries
; i
++) {
2227 const char *group
= NULL
;
2228 const char *desc
= NULL
;
2230 group
= info
.info3
.entries
[i
].account_name
.string
;
2231 desc
= info
.info3
.entries
[i
].description
.string
;
2233 if (c
->opt_long_list_entries
)
2234 printf("%-21.21s %-50.50s\n",
2237 printf("%s\n", group
);
2239 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2240 /* query domain aliases */
2245 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
2251 if (!NT_STATUS_IS_OK(result
) &&
2252 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2255 for (i
= 0; i
< num_entries
; i
++) {
2257 const char *description
= NULL
;
2259 if (c
->opt_long_list_entries
) {
2261 POLICY_HND alias_pol
;
2262 union samr_AliasInfo
*info
= NULL
;
2264 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2267 groups
->entries
[i
].idx
,
2269 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd
, mem_ctx
,
2273 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd
, mem_ctx
,
2275 description
= info
->description
.string
;
2279 if (description
!= NULL
) {
2280 printf("%-21.21s %-50.50s\n",
2281 groups
->entries
[i
].name
.string
,
2284 printf("%s\n", groups
->entries
[i
].name
.string
);
2287 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2288 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
2289 /* Get builtin policy handle */
2291 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2293 MAXIMUM_ALLOWED_ACCESS
,
2294 CONST_DISCARD(struct dom_sid2
*, &global_sid_Builtin
),
2296 if (!NT_STATUS_IS_OK(result
)) {
2299 /* query builtin aliases */
2302 if (!builtin
) break;
2304 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
2310 if (!NT_STATUS_IS_OK(result
) &&
2311 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2314 for (i
= 0; i
< num_entries
; i
++) {
2316 const char *description
= NULL
;
2318 if (c
->opt_long_list_entries
) {
2320 POLICY_HND alias_pol
;
2321 union samr_AliasInfo
*info
= NULL
;
2323 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2326 groups
->entries
[i
].idx
,
2328 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd
, mem_ctx
,
2332 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd
, mem_ctx
,
2334 description
= info
->description
.string
;
2338 if (description
!= NULL
) {
2339 printf("%-21.21s %-50.50s\n",
2340 groups
->entries
[i
].name
.string
,
2343 printf("%s\n", groups
->entries
[i
].name
.string
);
2346 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2352 static int rpc_group_list(struct net_context
*c
, int argc
, const char **argv
)
2354 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2355 rpc_group_list_internals
,
2359 static NTSTATUS
rpc_list_group_members(struct net_context
*c
,
2360 struct rpc_pipe_client
*pipe_hnd
,
2361 TALLOC_CTX
*mem_ctx
,
2362 const char *domain_name
,
2363 const DOM_SID
*domain_sid
,
2364 POLICY_HND
*domain_pol
,
2368 POLICY_HND group_pol
;
2369 uint32 num_members
, *group_rids
;
2371 struct samr_RidTypeArray
*rids
= NULL
;
2372 struct lsa_Strings names
;
2373 struct samr_Ids types
;
2376 sid_to_fstring(sid_str
, domain_sid
);
2378 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2380 MAXIMUM_ALLOWED_ACCESS
,
2384 if (!NT_STATUS_IS_OK(result
))
2387 result
= rpccli_samr_QueryGroupMember(pipe_hnd
, mem_ctx
,
2391 if (!NT_STATUS_IS_OK(result
))
2394 num_members
= rids
->count
;
2395 group_rids
= rids
->rids
;
2397 while (num_members
> 0) {
2398 int this_time
= 512;
2400 if (num_members
< this_time
)
2401 this_time
= num_members
;
2403 result
= rpccli_samr_LookupRids(pipe_hnd
, mem_ctx
,
2410 if (!NT_STATUS_IS_OK(result
))
2413 /* We only have users as members, but make the output
2414 the same as the output of alias members */
2416 for (i
= 0; i
< this_time
; i
++) {
2418 if (c
->opt_long_list_entries
) {
2419 printf("%s-%d %s\\%s %d\n", sid_str
,
2420 group_rids
[i
], domain_name
,
2421 names
.names
[i
].string
,
2424 printf("%s\\%s\n", domain_name
,
2425 names
.names
[i
].string
);
2429 num_members
-= this_time
;
2433 return NT_STATUS_OK
;
2436 static NTSTATUS
rpc_list_alias_members(struct net_context
*c
,
2437 struct rpc_pipe_client
*pipe_hnd
,
2438 TALLOC_CTX
*mem_ctx
,
2439 POLICY_HND
*domain_pol
,
2443 struct rpc_pipe_client
*lsa_pipe
;
2444 POLICY_HND alias_pol
, lsa_pol
;
2446 DOM_SID
*alias_sids
;
2449 enum lsa_SidType
*types
;
2451 struct lsa_SidArray sid_array
;
2453 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2455 MAXIMUM_ALLOWED_ACCESS
,
2459 if (!NT_STATUS_IS_OK(result
))
2462 result
= rpccli_samr_GetMembersInAlias(pipe_hnd
, mem_ctx
,
2466 if (!NT_STATUS_IS_OK(result
)) {
2467 d_fprintf(stderr
, "Couldn't list alias members\n");
2471 num_members
= sid_array
.num_sids
;
2473 if (num_members
== 0) {
2474 return NT_STATUS_OK
;
2477 result
= cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd
),
2478 &ndr_table_lsarpc
.syntax_id
,
2480 if (!NT_STATUS_IS_OK(result
)) {
2481 d_fprintf(stderr
, "Couldn't open LSA pipe. Error was %s\n",
2482 nt_errstr(result
) );
2486 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, true,
2487 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
2489 if (!NT_STATUS_IS_OK(result
)) {
2490 d_fprintf(stderr
, "Couldn't open LSA policy handle\n");
2491 TALLOC_FREE(lsa_pipe
);
2495 alias_sids
= TALLOC_ZERO_ARRAY(mem_ctx
, DOM_SID
, num_members
);
2497 d_fprintf(stderr
, "Out of memory\n");
2498 TALLOC_FREE(lsa_pipe
);
2499 return NT_STATUS_NO_MEMORY
;
2502 for (i
=0; i
<num_members
; i
++) {
2503 sid_copy(&alias_sids
[i
], sid_array
.sids
[i
].sid
);
2506 result
= rpccli_lsa_lookup_sids(lsa_pipe
, mem_ctx
, &lsa_pol
,
2507 num_members
, alias_sids
,
2508 &domains
, &names
, &types
);
2510 if (!NT_STATUS_IS_OK(result
) &&
2511 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
2512 d_fprintf(stderr
, "Couldn't lookup SIDs\n");
2513 TALLOC_FREE(lsa_pipe
);
2517 for (i
= 0; i
< num_members
; i
++) {
2519 sid_to_fstring(sid_str
, &alias_sids
[i
]);
2521 if (c
->opt_long_list_entries
) {
2522 printf("%s %s\\%s %d\n", sid_str
,
2523 domains
[i
] ? domains
[i
] : "*unknown*",
2524 names
[i
] ? names
[i
] : "*unknown*", types
[i
]);
2527 printf("%s\\%s\n", domains
[i
], names
[i
]);
2529 printf("%s\n", sid_str
);
2533 TALLOC_FREE(lsa_pipe
);
2534 return NT_STATUS_OK
;
2537 static NTSTATUS
rpc_group_members_internals(struct net_context
*c
,
2538 const DOM_SID
*domain_sid
,
2539 const char *domain_name
,
2540 struct cli_state
*cli
,
2541 struct rpc_pipe_client
*pipe_hnd
,
2542 TALLOC_CTX
*mem_ctx
,
2547 POLICY_HND connect_pol
, domain_pol
;
2548 struct samr_Ids rids
, rid_types
;
2549 struct lsa_String lsa_acct_name
;
2551 /* Get sam policy handle */
2553 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2555 MAXIMUM_ALLOWED_ACCESS
,
2558 if (!NT_STATUS_IS_OK(result
))
2561 /* Get domain policy handle */
2563 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2565 MAXIMUM_ALLOWED_ACCESS
,
2566 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2569 if (!NT_STATUS_IS_OK(result
))
2572 init_lsa_String(&lsa_acct_name
, argv
[0]); /* sure? */
2574 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2581 if (!NT_STATUS_IS_OK(result
)) {
2583 /* Ok, did not find it in the global sam, try with builtin */
2585 DOM_SID sid_Builtin
;
2587 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
2589 sid_copy(&sid_Builtin
, &global_sid_Builtin
);
2591 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2593 MAXIMUM_ALLOWED_ACCESS
,
2597 if (!NT_STATUS_IS_OK(result
)) {
2598 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2602 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2609 if (!NT_STATUS_IS_OK(result
)) {
2610 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2615 if (rids
.count
!= 1) {
2616 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2620 if (rid_types
.ids
[0] == SID_NAME_DOM_GRP
) {
2621 return rpc_list_group_members(c
, pipe_hnd
, mem_ctx
, domain_name
,
2622 domain_sid
, &domain_pol
,
2626 if (rid_types
.ids
[0] == SID_NAME_ALIAS
) {
2627 return rpc_list_alias_members(c
, pipe_hnd
, mem_ctx
, &domain_pol
,
2631 return NT_STATUS_NO_SUCH_GROUP
;
2634 static int rpc_group_members(struct net_context
*c
, int argc
, const char **argv
)
2636 if (argc
!= 1 || c
->display_usage
) {
2637 return rpc_group_usage(c
, argc
, argv
);
2640 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2641 rpc_group_members_internals
,
2645 static int rpc_group_rename_internals(struct net_context
*c
, int argc
, const char **argv
)
2647 NET_API_STATUS status
;
2648 struct GROUP_INFO_0 g0
;
2652 d_printf("Usage: 'net rpc group rename group newname'\n");
2656 g0
.grpi0_name
= argv
[1];
2658 status
= NetGroupSetInfo(c
->opt_host
,
2665 d_fprintf(stderr
, "Renaming group %s failed with: %s\n",
2666 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
2674 static int rpc_group_rename(struct net_context
*c
, int argc
, const char **argv
)
2676 if (argc
!= 2 || c
->display_usage
) {
2677 return rpc_group_usage(c
, argc
, argv
);
2680 return rpc_group_rename_internals(c
, argc
, argv
);
2684 * 'net rpc group' entrypoint.
2685 * @param argc Standard main() style argc.
2686 * @param argv Standard main() style argv. Initial components are already
2690 int net_rpc_group(struct net_context
*c
, int argc
, const char **argv
)
2692 NET_API_STATUS status
;
2694 struct functable func
[] = {
2699 "Create specified group",
2700 "net rpc group add\n"
2701 " Create specified group"
2707 "Delete specified group",
2708 "net rpc group delete\n"
2709 " Delete specified group"
2715 "Add member to group",
2716 "net rpc group addmem\n"
2717 " Add member to group"
2723 "Remove member from group",
2724 "net rpc group delmem\n"
2725 " Remove member from group"
2732 "net rpc group list\n"
2739 "List group members",
2740 "net rpc group members\n"
2741 " List group members"
2748 "net rpc group rename\n"
2751 {NULL
, NULL
, 0, NULL
, NULL
}
2754 status
= libnetapi_init(&c
->netapi_ctx
);
2758 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
2759 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
2760 if (c
->opt_kerberos
) {
2761 libnetapi_set_use_kerberos(c
->netapi_ctx
);
2765 if (c
->display_usage
) {
2766 d_printf("Usage:\n");
2767 d_printf("net rpc group\n"
2768 " Alias for net rpc group list global local "
2770 net_display_usage_from_functable(func
);
2774 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2775 rpc_group_list_internals
,
2779 return net_run_function(c
, argc
, argv
, "net rpc group", func
);
2782 /****************************************************************************/
2784 static int rpc_share_usage(struct net_context
*c
, int argc
, const char **argv
)
2786 return net_share_usage(c
, argc
, argv
);
2790 * Add a share on a remote RPC server.
2792 * @param argc Standard main() style argc.
2793 * @param argv Standard main() style argv. Initial components are already
2796 * @return A shell status integer (0 for success).
2799 static int rpc_share_add(struct net_context
*c
, int argc
, const char **argv
)
2801 NET_API_STATUS status
;
2804 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
2805 uint32 num_users
=0, perms
=0;
2806 char *password
=NULL
; /* don't allow a share password */
2807 struct SHARE_INFO_2 i2
;
2808 uint32_t parm_error
= 0;
2810 if ((argc
< 1) || !strchr(argv
[0], '=') || c
->display_usage
) {
2811 return rpc_share_usage(c
, argc
, argv
);
2814 if ((sharename
= talloc_strdup(c
, argv
[0])) == NULL
) {
2818 path
= strchr(sharename
, '=');
2825 i2
.shi2_netname
= sharename
;
2826 i2
.shi2_type
= type
;
2827 i2
.shi2_remark
= c
->opt_comment
;
2828 i2
.shi2_permissions
= perms
;
2829 i2
.shi2_max_uses
= c
->opt_maxusers
;
2830 i2
.shi2_current_uses
= num_users
;
2831 i2
.shi2_path
= path
;
2832 i2
.shi2_passwd
= password
;
2834 status
= NetShareAdd(c
->opt_host
,
2839 printf("NetShareAdd failed with: %s\n",
2840 libnetapi_get_error_string(c
->netapi_ctx
, status
));
2847 * Delete a share on a remote RPC server.
2849 * @param domain_sid The domain sid acquired from the remote server.
2850 * @param argc Standard main() style argc.
2851 * @param argv Standard main() style argv. Initial components are already
2854 * @return A shell status integer (0 for success).
2856 static int rpc_share_delete(struct net_context
*c
, int argc
, const char **argv
)
2858 if (argc
< 1 || c
->display_usage
) {
2859 return rpc_share_usage(c
, argc
, argv
);
2862 return NetShareDel(c
->opt_host
, argv
[0], 0);
2866 * Formatted print of share info
2868 * @param r pointer to SHARE_INFO_1 to format
2871 static void display_share_info_1(struct net_context
*c
,
2872 struct SHARE_INFO_1
*r
)
2874 if (c
->opt_long_list_entries
) {
2875 d_printf("%-12s %-8.8s %-50s\n",
2877 net_share_type_str(r
->shi1_type
& ~(STYPE_TEMPORARY
|STYPE_HIDDEN
)),
2880 d_printf("%s\n", r
->shi1_netname
);
2884 static WERROR
get_share_info(struct net_context
*c
,
2885 struct rpc_pipe_client
*pipe_hnd
,
2886 TALLOC_CTX
*mem_ctx
,
2890 struct srvsvc_NetShareInfoCtr
*info_ctr
)
2894 union srvsvc_NetShareInfo info
;
2896 /* no specific share requested, enumerate all */
2899 uint32_t preferred_len
= 0xffffffff;
2900 uint32_t total_entries
= 0;
2901 uint32_t resume_handle
= 0;
2903 info_ctr
->level
= level
;
2905 status
= rpccli_srvsvc_NetShareEnumAll(pipe_hnd
, mem_ctx
,
2915 /* request just one share */
2916 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
2923 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
2928 ZERO_STRUCTP(info_ctr
);
2930 info_ctr
->level
= level
;
2935 struct srvsvc_NetShareCtr1
*ctr1
;
2937 ctr1
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr1
);
2938 W_ERROR_HAVE_NO_MEMORY(ctr1
);
2941 ctr1
->array
= info
.info1
;
2943 info_ctr
->ctr
.ctr1
= ctr1
;
2947 struct srvsvc_NetShareCtr2
*ctr2
;
2949 ctr2
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr2
);
2950 W_ERROR_HAVE_NO_MEMORY(ctr2
);
2953 ctr2
->array
= info
.info2
;
2955 info_ctr
->ctr
.ctr2
= ctr2
;
2959 struct srvsvc_NetShareCtr502
*ctr502
;
2961 ctr502
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr502
);
2962 W_ERROR_HAVE_NO_MEMORY(ctr502
);
2965 ctr502
->array
= info
.info502
;
2967 info_ctr
->ctr
.ctr502
= ctr502
;
2975 * 'net rpc share list' entrypoint.
2976 * @param argc Standard main() style argc.
2977 * @param argv Standard main() style argv. Initial components are already
2980 static int rpc_share_list(struct net_context
*c
, int argc
, const char **argv
)
2982 NET_API_STATUS status
;
2983 struct SHARE_INFO_1
*i1
= NULL
;
2984 uint32_t entries_read
= 0;
2985 uint32_t total_entries
= 0;
2986 uint32_t resume_handle
= 0;
2987 uint32_t i
, level
= 1;
2989 if (c
->display_usage
) {
2991 "net rpc share list\n"
2992 " List shares on remote server\n");
2996 status
= NetShareEnum(c
->opt_host
,
3007 /* Display results */
3009 if (c
->opt_long_list_entries
) {
3011 "\nEnumerating shared resources (exports) on remote server:\n\n"
3012 "\nShare name Type Description\n"
3013 "---------- ---- -----------\n");
3015 for (i
= 0; i
< entries_read
; i
++)
3016 display_share_info_1(c
, &i1
[i
]);
3021 static bool check_share_availability(struct cli_state
*cli
, const char *netname
)
3023 if (!cli_send_tconX(cli
, netname
, "A:", "", 0)) {
3024 d_printf("skipping [%s]: not a file share.\n", netname
);
3034 static bool check_share_sanity(struct net_context
*c
, struct cli_state
*cli
,
3035 const char *netname
, uint32 type
)
3037 /* only support disk shares */
3038 if (! ( type
== STYPE_DISKTREE
|| type
== (STYPE_DISKTREE
| STYPE_HIDDEN
)) ) {
3039 printf("share [%s] is not a diskshare (type: %x)\n", netname
, type
);
3043 /* skip builtin shares */
3044 /* FIXME: should print$ be added too ? */
3045 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$") ||
3046 strequal(netname
,"global"))
3049 if (c
->opt_exclude
&& in_list(netname
, c
->opt_exclude
, false)) {
3050 printf("excluding [%s]\n", netname
);
3054 return check_share_availability(cli
, netname
);
3058 * Migrate shares from a remote RPC server to the local RPC server.
3060 * All parameters are provided by the run_rpc_command function, except for
3061 * argc, argv which are passed through.
3063 * @param domain_sid The domain sid acquired from the remote server.
3064 * @param cli A cli_state connected to the server.
3065 * @param mem_ctx Talloc context, destroyed on completion of the function.
3066 * @param argc Standard main() style argc.
3067 * @param argv Standard main() style argv. Initial components are already
3070 * @return Normal NTSTATUS return.
3073 static NTSTATUS
rpc_share_migrate_shares_internals(struct net_context
*c
,
3074 const DOM_SID
*domain_sid
,
3075 const char *domain_name
,
3076 struct cli_state
*cli
,
3077 struct rpc_pipe_client
*pipe_hnd
,
3078 TALLOC_CTX
*mem_ctx
,
3083 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3084 struct srvsvc_NetShareInfoCtr ctr_src
;
3086 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3087 struct cli_state
*cli_dst
= NULL
;
3088 uint32 level
= 502; /* includes secdesc */
3089 uint32_t parm_error
= 0;
3091 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3093 if (!W_ERROR_IS_OK(result
))
3096 /* connect destination PI_SRVSVC */
3097 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3098 &ndr_table_srvsvc
.syntax_id
);
3099 if (!NT_STATUS_IS_OK(nt_status
))
3103 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3105 union srvsvc_NetShareInfo info
;
3106 struct srvsvc_NetShareInfo502 info502
=
3107 ctr_src
.ctr
.ctr502
->array
[i
];
3109 /* reset error-code */
3110 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3112 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3115 /* finally add the share on the dst server */
3117 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3118 info502
.name
, info502
.path
, info502
.comment
);
3120 info
.info502
= &info502
;
3122 nt_status
= rpccli_srvsvc_NetShareAdd(srvsvc_pipe
, mem_ctx
,
3123 srvsvc_pipe
->desthost
,
3129 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ALREADY_EXISTS
)) {
3130 printf(" [%s] does already exist\n",
3135 if (!NT_STATUS_IS_OK(nt_status
) || !W_ERROR_IS_OK(result
)) {
3136 printf("cannot add share: %s\n", dos_errstr(result
));
3142 nt_status
= NT_STATUS_OK
;
3146 cli_shutdown(cli_dst
);
3154 * Migrate shares from a RPC server to another.
3156 * @param argc Standard main() style argc.
3157 * @param argv Standard main() style argv. Initial components are already
3160 * @return A shell status integer (0 for success).
3162 static int rpc_share_migrate_shares(struct net_context
*c
, int argc
,
3165 if (c
->display_usage
) {
3167 "net rpc share migrate shares\n"
3168 " Migrate shares to local server\n");
3173 printf("no server to migrate\n");
3177 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3178 rpc_share_migrate_shares_internals
,
3185 * @param f file_info
3186 * @param mask current search mask
3187 * @param state arg-pointer
3190 static void copy_fn(const char *mnt
, file_info
*f
,
3191 const char *mask
, void *state
)
3193 static NTSTATUS nt_status
;
3194 static struct copy_clistate
*local_state
;
3195 static fstring filename
, new_mask
;
3198 struct net_context
*c
;
3200 local_state
= (struct copy_clistate
*)state
;
3201 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3205 if (strequal(f
->name
, ".") || strequal(f
->name
, ".."))
3208 DEBUG(3,("got mask: %s, name: %s\n", mask
, f
->name
));
3211 if (f
->mode
& aDIR
) {
3213 DEBUG(3,("got dir: %s\n", f
->name
));
3215 fstrcpy(dir
, local_state
->cwd
);
3217 fstrcat(dir
, f
->name
);
3219 switch (net_mode_share
)
3221 case NET_MODE_SHARE_MIGRATE
:
3222 /* create that directory */
3223 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3224 local_state
->cli_share_src
,
3225 local_state
->cli_share_dst
,
3227 c
->opt_acls
? true : false,
3228 c
->opt_attrs
? true : false,
3229 c
->opt_timestamps
? true:false,
3233 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3237 if (!NT_STATUS_IS_OK(nt_status
))
3238 printf("could not handle dir %s: %s\n",
3239 dir
, nt_errstr(nt_status
));
3241 /* search below that directory */
3242 fstrcpy(new_mask
, dir
);
3243 fstrcat(new_mask
, "\\*");
3245 old_dir
= local_state
->cwd
;
3246 local_state
->cwd
= dir
;
3247 if (!sync_files(local_state
, new_mask
))
3248 printf("could not handle files\n");
3249 local_state
->cwd
= old_dir
;
3256 fstrcpy(filename
, local_state
->cwd
);
3257 fstrcat(filename
, "\\");
3258 fstrcat(filename
, f
->name
);
3260 DEBUG(3,("got file: %s\n", filename
));
3262 switch (net_mode_share
)
3264 case NET_MODE_SHARE_MIGRATE
:
3265 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3266 local_state
->cli_share_src
,
3267 local_state
->cli_share_dst
,
3269 c
->opt_acls
? true : false,
3270 c
->opt_attrs
? true : false,
3271 c
->opt_timestamps
? true: false,
3275 d_fprintf(stderr
, "Unsupported file mode %d\n", net_mode_share
);
3279 if (!NT_STATUS_IS_OK(nt_status
))
3280 printf("could not handle file %s: %s\n",
3281 filename
, nt_errstr(nt_status
));
3286 * sync files, can be called recursivly to list files
3287 * and then call copy_fn for each file
3289 * @param cp_clistate pointer to the copy_clistate we work with
3290 * @param mask the current search mask
3292 * @return Boolean result
3294 static bool sync_files(struct copy_clistate
*cp_clistate
, const char *mask
)
3296 struct cli_state
*targetcli
;
3297 char *targetpath
= NULL
;
3299 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
3301 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate
->cli_share_src
,
3302 mask
, &targetcli
, &targetpath
) ) {
3303 d_fprintf(stderr
, "cli_resolve_path %s failed with error: %s\n",
3304 mask
, cli_errstr(cp_clistate
->cli_share_src
));
3308 if (cli_list(targetcli
, targetpath
, cp_clistate
->attribute
, copy_fn
, cp_clistate
) == -1) {
3309 d_fprintf(stderr
, "listing %s failed with error: %s\n",
3310 mask
, cli_errstr(targetcli
));
3319 * Set the top level directory permissions before we do any further copies.
3320 * Should set up ACL inheritance.
3323 bool copy_top_level_perms(struct net_context
*c
,
3324 struct copy_clistate
*cp_clistate
,
3325 const char *sharename
)
3327 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3329 switch (net_mode_share
) {
3330 case NET_MODE_SHARE_MIGRATE
:
3331 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename
));
3332 nt_status
= net_copy_fileattr(c
,
3333 cp_clistate
->mem_ctx
,
3334 cp_clistate
->cli_share_src
,
3335 cp_clistate
->cli_share_dst
,
3337 c
->opt_acls
? true : false,
3338 c
->opt_attrs
? true : false,
3339 c
->opt_timestamps
? true: false,
3343 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3347 if (!NT_STATUS_IS_OK(nt_status
)) {
3348 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3349 sharename
, nt_errstr(nt_status
));
3357 * Sync all files inside a remote share to another share (over smb).
3359 * All parameters are provided by the run_rpc_command function, except for
3360 * argc, argv which are passed through.
3362 * @param domain_sid The domain sid acquired from the remote server.
3363 * @param cli A cli_state connected to the server.
3364 * @param mem_ctx Talloc context, destroyed on completion of the function.
3365 * @param argc Standard main() style argc.
3366 * @param argv Standard main() style argv. Initial components are already
3369 * @return Normal NTSTATUS return.
3372 static NTSTATUS
rpc_share_migrate_files_internals(struct net_context
*c
,
3373 const DOM_SID
*domain_sid
,
3374 const char *domain_name
,
3375 struct cli_state
*cli
,
3376 struct rpc_pipe_client
*pipe_hnd
,
3377 TALLOC_CTX
*mem_ctx
,
3382 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3383 struct srvsvc_NetShareInfoCtr ctr_src
;
3386 struct copy_clistate cp_clistate
;
3387 bool got_src_share
= false;
3388 bool got_dst_share
= false;
3389 const char *mask
= "\\*";
3392 dst
= SMB_STRDUP(c
->opt_destination
?c
->opt_destination
:"127.0.0.1");
3394 nt_status
= NT_STATUS_NO_MEMORY
;
3398 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3401 if (!W_ERROR_IS_OK(result
))
3404 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3406 struct srvsvc_NetShareInfo502 info502
=
3407 ctr_src
.ctr
.ctr502
->array
[i
];
3409 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3412 /* one might not want to mirror whole discs :) */
3413 if (strequal(info502
.name
, "print$") || info502
.name
[1] == '$') {
3414 d_printf("skipping [%s]: builtin/hidden share\n", info502
.name
);
3418 switch (net_mode_share
)
3420 case NET_MODE_SHARE_MIGRATE
:
3424 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3427 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3429 c
->opt_acls
? "including" : "without",
3430 c
->opt_attrs
? "including" : "without",
3431 c
->opt_timestamps
? "(preserving timestamps)" : "");
3433 cp_clistate
.mem_ctx
= mem_ctx
;
3434 cp_clistate
.cli_share_src
= NULL
;
3435 cp_clistate
.cli_share_dst
= NULL
;
3436 cp_clistate
.cwd
= NULL
;
3437 cp_clistate
.attribute
= aSYSTEM
| aHIDDEN
| aDIR
;
3440 /* open share source */
3441 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_src
,
3442 &cli
->dest_ss
, cli
->desthost
,
3443 info502
.name
, "A:");
3444 if (!NT_STATUS_IS_OK(nt_status
))
3447 got_src_share
= true;
3449 if (net_mode_share
== NET_MODE_SHARE_MIGRATE
) {
3450 /* open share destination */
3451 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_dst
,
3452 NULL
, dst
, info502
.name
, "A:");
3453 if (!NT_STATUS_IS_OK(nt_status
))
3456 got_dst_share
= true;
3459 if (!copy_top_level_perms(c
, &cp_clistate
, info502
.name
)) {
3460 d_fprintf(stderr
, "Could not handle the top level directory permissions for the share: %s\n", info502
.name
);
3461 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3465 if (!sync_files(&cp_clistate
, mask
)) {
3466 d_fprintf(stderr
, "could not handle files for share: %s\n", info502
.name
);
3467 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3472 nt_status
= NT_STATUS_OK
;
3477 cli_shutdown(cp_clistate
.cli_share_src
);
3480 cli_shutdown(cp_clistate
.cli_share_dst
);
3487 static int rpc_share_migrate_files(struct net_context
*c
, int argc
, const char **argv
)
3489 if (c
->display_usage
) {
3491 "net share migrate files\n"
3492 " Migrate files to local server\n");
3497 d_printf("no server to migrate\n");
3501 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3502 rpc_share_migrate_files_internals
,
3507 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3509 * All parameters are provided by the run_rpc_command function, except for
3510 * argc, argv which are passed through.
3512 * @param domain_sid The domain sid acquired from the remote server.
3513 * @param cli A cli_state connected to the server.
3514 * @param mem_ctx Talloc context, destroyed on completion of the function.
3515 * @param argc Standard main() style argc.
3516 * @param argv Standard main() style argv. Initial components are already
3519 * @return Normal NTSTATUS return.
3522 static NTSTATUS
rpc_share_migrate_security_internals(struct net_context
*c
,
3523 const DOM_SID
*domain_sid
,
3524 const char *domain_name
,
3525 struct cli_state
*cli
,
3526 struct rpc_pipe_client
*pipe_hnd
,
3527 TALLOC_CTX
*mem_ctx
,
3532 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3533 struct srvsvc_NetShareInfoCtr ctr_src
;
3534 union srvsvc_NetShareInfo info
;
3536 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3537 struct cli_state
*cli_dst
= NULL
;
3538 uint32 level
= 502; /* includes secdesc */
3539 uint32_t parm_error
= 0;
3541 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3544 if (!W_ERROR_IS_OK(result
))
3547 /* connect destination PI_SRVSVC */
3548 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3549 &ndr_table_srvsvc
.syntax_id
);
3550 if (!NT_STATUS_IS_OK(nt_status
))
3554 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3556 struct srvsvc_NetShareInfo502 info502
=
3557 ctr_src
.ctr
.ctr502
->array
[i
];
3559 /* reset error-code */
3560 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3562 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3565 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3566 info502
.name
, info502
.path
, info502
.comment
);
3569 display_sec_desc(info502
.sd_buf
.sd
);
3571 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3572 info
.info502
= &info502
;
3574 /* finally modify the share on the dst server */
3575 nt_status
= rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe
, mem_ctx
,
3576 srvsvc_pipe
->desthost
,
3582 if (!NT_STATUS_IS_OK(nt_status
) || !W_ERROR_IS_OK(result
)) {
3583 printf("cannot set share-acl: %s\n", dos_errstr(result
));
3589 nt_status
= NT_STATUS_OK
;
3593 cli_shutdown(cli_dst
);
3601 * Migrate share-acls from a RPC server to another.
3603 * @param argc Standard main() style argc.
3604 * @param argv Standard main() style argv. Initial components are already
3607 * @return A shell status integer (0 for success).
3609 static int rpc_share_migrate_security(struct net_context
*c
, int argc
,
3612 if (c
->display_usage
) {
3614 "net rpc share migrate security\n"
3615 " Migrate share-acls to local server\n");
3620 d_printf("no server to migrate\n");
3624 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3625 rpc_share_migrate_security_internals
,
3630 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3631 * from one server to another.
3633 * @param argc Standard main() style argc.
3634 * @param argv Standard main() style argv. Initial components are already
3637 * @return A shell status integer (0 for success).
3640 static int rpc_share_migrate_all(struct net_context
*c
, int argc
,
3645 if (c
->display_usage
) {
3647 "net rpc share migrate all\n"
3648 " Migrates shares including all share settings\n");
3653 d_printf("no server to migrate\n");
3657 /* order is important. we don't want to be locked out by the share-acl
3658 * before copying files - gd */
3660 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3661 rpc_share_migrate_shares_internals
, argc
, argv
);
3665 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3666 rpc_share_migrate_files_internals
, argc
, argv
);
3670 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3671 rpc_share_migrate_security_internals
, argc
,
3677 * 'net rpc share migrate' entrypoint.
3678 * @param argc Standard main() style argc.
3679 * @param argv Standard main() style argv. Initial components are already
3682 static int rpc_share_migrate(struct net_context
*c
, int argc
, const char **argv
)
3685 struct functable func
[] = {
3688 rpc_share_migrate_all
,
3690 "Migrate shares from remote to local server",
3691 "net rpc share migrate all\n"
3692 " Migrate shares from remote to local server"
3696 rpc_share_migrate_files
,
3698 "Migrate files from remote to local server",
3699 "net rpc share migrate files\n"
3700 " Migrate files from remote to local server"
3704 rpc_share_migrate_security
,
3706 "Migrate share-ACLs from remote to local server",
3707 "net rpc share migrate security\n"
3708 " Migrate share-ACLs from remote to local server"
3712 rpc_share_migrate_shares
,
3714 "Migrate shares from remote to local server",
3715 "net rpc share migrate shares\n"
3716 " Migrate shares from remote to local server"
3718 {NULL
, NULL
, 0, NULL
, NULL
}
3721 net_mode_share
= NET_MODE_SHARE_MIGRATE
;
3723 return net_run_function(c
, argc
, argv
, "net rpc share migrate", func
);
3732 static int num_server_aliases
;
3733 static struct full_alias
*server_aliases
;
3736 * Add an alias to the static list.
3738 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
3740 if (server_aliases
== NULL
)
3741 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
3743 server_aliases
[num_server_aliases
] = *alias
;
3744 num_server_aliases
+= 1;
3748 * For a specific domain on the server, fetch all the aliases
3749 * and their members. Add all of them to the server_aliases.
3752 static NTSTATUS
rpc_fetch_domain_aliases(struct rpc_pipe_client
*pipe_hnd
,
3753 TALLOC_CTX
*mem_ctx
,
3754 POLICY_HND
*connect_pol
,
3755 const DOM_SID
*domain_sid
)
3757 uint32 start_idx
, max_entries
, num_entries
, i
;
3758 struct samr_SamArray
*groups
= NULL
;
3760 POLICY_HND domain_pol
;
3762 /* Get domain policy handle */
3764 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
3766 MAXIMUM_ALLOWED_ACCESS
,
3767 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
3769 if (!NT_STATUS_IS_OK(result
))
3776 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
3782 for (i
= 0; i
< num_entries
; i
++) {
3784 POLICY_HND alias_pol
;
3785 struct full_alias alias
;
3786 struct lsa_SidArray sid_array
;
3789 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
3791 MAXIMUM_ALLOWED_ACCESS
,
3792 groups
->entries
[i
].idx
,
3794 if (!NT_STATUS_IS_OK(result
))
3797 result
= rpccli_samr_GetMembersInAlias(pipe_hnd
, mem_ctx
,
3800 if (!NT_STATUS_IS_OK(result
))
3803 alias
.num_members
= sid_array
.num_sids
;
3805 result
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &alias_pol
);
3806 if (!NT_STATUS_IS_OK(result
))
3809 alias
.members
= NULL
;
3811 if (alias
.num_members
> 0) {
3812 alias
.members
= SMB_MALLOC_ARRAY(DOM_SID
, alias
.num_members
);
3814 for (j
= 0; j
< alias
.num_members
; j
++)
3815 sid_copy(&alias
.members
[j
],
3816 sid_array
.sids
[j
].sid
);
3819 sid_copy(&alias
.sid
, domain_sid
);
3820 sid_append_rid(&alias
.sid
, groups
->entries
[i
].idx
);
3822 push_alias(mem_ctx
, &alias
);
3824 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
3826 result
= NT_STATUS_OK
;
3829 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
3835 * Dump server_aliases as names for debugging purposes.
3838 static NTSTATUS
rpc_aliaslist_dump(struct net_context
*c
,
3839 const DOM_SID
*domain_sid
,
3840 const char *domain_name
,
3841 struct cli_state
*cli
,
3842 struct rpc_pipe_client
*pipe_hnd
,
3843 TALLOC_CTX
*mem_ctx
,
3851 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
3852 SEC_RIGHTS_MAXIMUM_ALLOWED
,
3854 if (!NT_STATUS_IS_OK(result
))
3857 for (i
=0; i
<num_server_aliases
; i
++) {
3860 enum lsa_SidType
*types
;
3863 struct full_alias
*alias
= &server_aliases
[i
];
3865 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
3867 &domains
, &names
, &types
);
3868 if (!NT_STATUS_IS_OK(result
))
3871 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
3873 if (alias
->num_members
== 0) {
3878 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
,
3881 &domains
, &names
, &types
);
3883 if (!NT_STATUS_IS_OK(result
) &&
3884 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
3887 for (j
=0; j
<alias
->num_members
; j
++)
3888 DEBUG(1, ("%s\\%s (%d); ",
3889 domains
[j
] ? domains
[j
] : "*unknown*",
3890 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
3894 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
3896 return NT_STATUS_OK
;
3900 * Fetch a list of all server aliases and their members into
3904 static NTSTATUS
rpc_aliaslist_internals(struct net_context
*c
,
3905 const DOM_SID
*domain_sid
,
3906 const char *domain_name
,
3907 struct cli_state
*cli
,
3908 struct rpc_pipe_client
*pipe_hnd
,
3909 TALLOC_CTX
*mem_ctx
,
3914 POLICY_HND connect_pol
;
3916 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
3918 MAXIMUM_ALLOWED_ACCESS
,
3921 if (!NT_STATUS_IS_OK(result
))
3924 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
3925 &global_sid_Builtin
);
3927 if (!NT_STATUS_IS_OK(result
))
3930 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
3933 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
3938 static void init_user_token(NT_USER_TOKEN
*token
, DOM_SID
*user_sid
)
3940 token
->num_sids
= 4;
3942 if (!(token
->user_sids
= SMB_MALLOC_ARRAY(DOM_SID
, 4))) {
3943 d_fprintf(stderr
, "malloc failed\n");
3944 token
->num_sids
= 0;
3948 token
->user_sids
[0] = *user_sid
;
3949 sid_copy(&token
->user_sids
[1], &global_sid_World
);
3950 sid_copy(&token
->user_sids
[2], &global_sid_Network
);
3951 sid_copy(&token
->user_sids
[3], &global_sid_Authenticated_Users
);
3954 static void free_user_token(NT_USER_TOKEN
*token
)
3956 SAFE_FREE(token
->user_sids
);
3959 static void add_sid_to_token(NT_USER_TOKEN
*token
, DOM_SID
*sid
)
3961 if (is_sid_in_token(token
, sid
))
3964 token
->user_sids
= SMB_REALLOC_ARRAY(token
->user_sids
, DOM_SID
, token
->num_sids
+1);
3965 if (!token
->user_sids
) {
3969 sid_copy(&token
->user_sids
[token
->num_sids
], sid
);
3971 token
->num_sids
+= 1;
3976 NT_USER_TOKEN token
;
3979 static void dump_user_token(struct user_token
*token
)
3983 d_printf("%s\n", token
->name
);
3985 for (i
=0; i
<token
->token
.num_sids
; i
++) {
3986 d_printf(" %s\n", sid_string_tos(&token
->token
.user_sids
[i
]));
3990 static bool is_alias_member(DOM_SID
*sid
, struct full_alias
*alias
)
3994 for (i
=0; i
<alias
->num_members
; i
++) {
3995 if (sid_compare(sid
, &alias
->members
[i
]) == 0)
4002 static void collect_sid_memberships(NT_USER_TOKEN
*token
, DOM_SID sid
)
4006 for (i
=0; i
<num_server_aliases
; i
++) {
4007 if (is_alias_member(&sid
, &server_aliases
[i
]))
4008 add_sid_to_token(token
, &server_aliases
[i
].sid
);
4013 * We got a user token with all the SIDs we can know about without asking the
4014 * server directly. These are the user and domain group sids. All of these can
4015 * be members of aliases. So scan the list of aliases for each of the SIDs and
4016 * add them to the token.
4019 static void collect_alias_memberships(NT_USER_TOKEN
*token
)
4021 int num_global_sids
= token
->num_sids
;
4024 for (i
=0; i
<num_global_sids
; i
++) {
4025 collect_sid_memberships(token
, token
->user_sids
[i
]);
4029 static bool get_user_sids(const char *domain
, const char *user
, NT_USER_TOKEN
*token
)
4031 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4032 enum wbcSidType type
;
4034 struct wbcDomainSid wsid
;
4035 char *sid_str
= NULL
;
4037 uint32_t num_groups
;
4038 gid_t
*groups
= NULL
;
4041 fstr_sprintf(full_name
, "%s%c%s",
4042 domain
, *lp_winbind_separator(), user
);
4044 /* First let's find out the user sid */
4046 wbc_status
= wbcLookupName(domain
, user
, &wsid
, &type
);
4048 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4049 DEBUG(1, ("winbind could not find %s: %s\n",
4050 full_name
, wbcErrorString(wbc_status
)));
4054 wbc_status
= wbcSidToString(&wsid
, &sid_str
);
4055 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4059 if (type
!= SID_NAME_USER
) {
4060 wbcFreeMemory(sid_str
);
4061 DEBUG(1, ("%s is not a user\n", full_name
));
4065 string_to_sid(&user_sid
, sid_str
);
4066 wbcFreeMemory(sid_str
);
4069 init_user_token(token
, &user_sid
);
4071 /* And now the groups winbind knows about */
4073 wbc_status
= wbcGetGroups(full_name
, &num_groups
, &groups
);
4074 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4075 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4076 full_name
, wbcErrorString(wbc_status
)));
4080 for (i
= 0; i
< num_groups
; i
++) {
4081 gid_t gid
= groups
[i
];
4084 wbc_status
= wbcGidToSid(gid
, &wsid
);
4085 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4086 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4087 gid
, wbcErrorString(wbc_status
)));
4088 wbcFreeMemory(groups
);
4092 wbc_status
= wbcSidToString(&wsid
, &sid_str
);
4093 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4094 wbcFreeMemory(groups
);
4098 DEBUG(3, (" %s\n", sid_str
));
4100 string_to_sid(&sid
, sid_str
);
4101 wbcFreeMemory(sid_str
);
4104 add_sid_to_token(token
, &sid
);
4106 wbcFreeMemory(groups
);
4112 * Get a list of all user tokens we want to look at
4115 static bool get_user_tokens(struct net_context
*c
, int *num_tokens
,
4116 struct user_token
**user_tokens
)
4118 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4119 uint32_t i
, num_users
;
4121 struct user_token
*result
;
4122 TALLOC_CTX
*frame
= NULL
;
4124 if (lp_winbind_use_default_domain() &&
4125 (c
->opt_target_workgroup
== NULL
)) {
4126 d_fprintf(stderr
, "winbind use default domain = yes set, "
4127 "please specify a workgroup\n");
4131 /* Send request to winbind daemon */
4133 wbc_status
= wbcListUsers(NULL
, &num_users
, &users
);
4134 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4135 DEBUG(1, ("winbind could not list users: %s\n",
4136 wbcErrorString(wbc_status
)));
4140 result
= SMB_MALLOC_ARRAY(struct user_token
, num_users
);
4142 if (result
== NULL
) {
4143 DEBUG(1, ("Could not malloc sid array\n"));
4144 wbcFreeMemory(users
);
4148 frame
= talloc_stackframe();
4149 for (i
=0; i
< num_users
; i
++) {
4150 fstring domain
, user
;
4153 fstrcpy(result
[i
].name
, users
[i
]);
4155 p
= strchr(users
[i
], *lp_winbind_separator());
4157 DEBUG(3, ("%s\n", users
[i
]));
4160 fstrcpy(domain
, c
->opt_target_workgroup
);
4161 fstrcpy(user
, users
[i
]);
4164 fstrcpy(domain
, users
[i
]);
4169 get_user_sids(domain
, user
, &(result
[i
].token
));
4173 wbcFreeMemory(users
);
4175 *num_tokens
= num_users
;
4176 *user_tokens
= result
;
4181 static bool get_user_tokens_from_file(FILE *f
,
4183 struct user_token
**tokens
)
4185 struct user_token
*token
= NULL
;
4190 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4194 if (line
[strlen(line
)-1] == '\n')
4195 line
[strlen(line
)-1] = '\0';
4197 if (line
[0] == ' ') {
4201 string_to_sid(&sid
, &line
[1]);
4203 if (token
== NULL
) {
4204 DEBUG(0, ("File does not begin with username"));
4208 add_sid_to_token(&token
->token
, &sid
);
4212 /* And a new user... */
4215 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4216 if (*tokens
== NULL
) {
4217 DEBUG(0, ("Could not realloc tokens\n"));
4221 token
= &((*tokens
)[*num_tokens
-1]);
4223 fstrcpy(token
->name
, line
);
4224 token
->token
.num_sids
= 0;
4225 token
->token
.user_sids
= NULL
;
4234 * Show the list of all users that have access to a share
4237 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4238 TALLOC_CTX
*mem_ctx
,
4239 const char *netname
,
4241 struct user_token
*tokens
)
4244 SEC_DESC
*share_sd
= NULL
;
4245 SEC_DESC
*root_sd
= NULL
;
4246 struct cli_state
*cli
= rpc_pipe_np_smb_conn(pipe_hnd
);
4248 union srvsvc_NetShareInfo info
;
4253 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
4260 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4261 DEBUG(1, ("Coult not query secdesc for share %s\n",
4266 share_sd
= info
.info502
->sd_buf
.sd
;
4267 if (share_sd
== NULL
) {
4268 DEBUG(1, ("Got no secdesc for share %s\n",
4274 if (!cli_send_tconX(cli
, netname
, "A:", "", 0)) {
4278 fnum
= cli_nt_create(cli
, "\\", READ_CONTROL_ACCESS
);
4281 root_sd
= cli_query_secdesc(cli
, fnum
, mem_ctx
);
4284 for (i
=0; i
<num_tokens
; i
++) {
4287 if (share_sd
!= NULL
) {
4288 status
= se_access_check(share_sd
, &tokens
[i
].token
,
4291 if (!NT_STATUS_IS_OK(status
)) {
4292 DEBUG(1, ("Could not check share_sd for "
4299 if (root_sd
== NULL
) {
4300 d_printf(" %s\n", tokens
[i
].name
);
4304 status
= se_access_check(root_sd
, &tokens
[i
].token
,
4306 if (!NT_STATUS_IS_OK(status
)) {
4307 DEBUG(1, ("Could not check root_sd for user %s\n",
4311 d_printf(" %s\n", tokens
[i
].name
);
4315 cli_close(cli
, fnum
);
4327 static void collect_share(const char *name
, uint32 m
,
4328 const char *comment
, void *state
)
4330 struct share_list
*share_list
= (struct share_list
*)state
;
4332 if (m
!= STYPE_DISKTREE
)
4335 share_list
->num_shares
+= 1;
4336 share_list
->shares
= SMB_REALLOC_ARRAY(share_list
->shares
, char *, share_list
->num_shares
);
4337 if (!share_list
->shares
) {
4338 share_list
->num_shares
= 0;
4341 share_list
->shares
[share_list
->num_shares
-1] = SMB_STRDUP(name
);
4345 * List shares on a remote RPC server, including the security descriptors.
4347 * All parameters are provided by the run_rpc_command function, except for
4348 * argc, argv which are passed through.
4350 * @param domain_sid The domain sid acquired from the remote server.
4351 * @param cli A cli_state connected to the server.
4352 * @param mem_ctx Talloc context, destroyed on completion of the function.
4353 * @param argc Standard main() style argc.
4354 * @param argv Standard main() style argv. Initial components are already
4357 * @return Normal NTSTATUS return.
4360 static NTSTATUS
rpc_share_allowedusers_internals(struct net_context
*c
,
4361 const DOM_SID
*domain_sid
,
4362 const char *domain_name
,
4363 struct cli_state
*cli
,
4364 struct rpc_pipe_client
*pipe_hnd
,
4365 TALLOC_CTX
*mem_ctx
,
4375 struct user_token
*tokens
= NULL
;
4378 struct share_list share_list
;
4383 f
= fopen(argv
[0], "r");
4387 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4388 return NT_STATUS_UNSUCCESSFUL
;
4391 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
4397 DEBUG(0, ("Could not read users from file\n"));
4398 return NT_STATUS_UNSUCCESSFUL
;
4401 for (i
=0; i
<num_tokens
; i
++)
4402 collect_alias_memberships(&tokens
[i
].token
);
4404 init_enum_hnd(&hnd
, 0);
4406 share_list
.num_shares
= 0;
4407 share_list
.shares
= NULL
;
4409 ret
= cli_RNetShareEnum(cli
, collect_share
, &share_list
);
4412 DEBUG(0, ("Error returning browse list: %s\n",
4417 for (i
= 0; i
< share_list
.num_shares
; i
++) {
4418 char *netname
= share_list
.shares
[i
];
4420 if (netname
[strlen(netname
)-1] == '$')
4423 d_printf("%s\n", netname
);
4425 show_userlist(pipe_hnd
, mem_ctx
, netname
,
4426 num_tokens
, tokens
);
4429 for (i
=0; i
<num_tokens
; i
++) {
4430 free_user_token(&tokens
[i
].token
);
4433 SAFE_FREE(share_list
.shares
);
4435 return NT_STATUS_OK
;
4438 static int rpc_share_allowedusers(struct net_context
*c
, int argc
,
4443 if (c
->display_usage
) {
4445 "net rpc share allowedusers\n"
4446 " List allowed users\n");
4450 result
= run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
4451 rpc_aliaslist_internals
,
4456 result
= run_rpc_command(c
, NULL
, &ndr_table_lsarpc
.syntax_id
, 0,
4462 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4463 rpc_share_allowedusers_internals
,
4467 int net_usersidlist(struct net_context
*c
, int argc
, const char **argv
)
4470 struct user_token
*tokens
= NULL
;
4474 net_usersidlist_usage(c
, argc
, argv
);
4478 if (!get_user_tokens(c
, &num_tokens
, &tokens
)) {
4479 DEBUG(0, ("Could not get the user/sid list\n"));
4483 for (i
=0; i
<num_tokens
; i
++) {
4484 dump_user_token(&tokens
[i
]);
4485 free_user_token(&tokens
[i
].token
);
4492 int net_usersidlist_usage(struct net_context
*c
, int argc
, const char **argv
)
4494 d_printf("net usersidlist\n"
4495 "\tprints out a list of all users the running winbind knows\n"
4496 "\tabout, together with all their SIDs. This is used as\n"
4497 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4499 net_common_flags_usage(c
, argc
, argv
);
4504 * 'net rpc share' entrypoint.
4505 * @param argc Standard main() style argc.
4506 * @param argv Standard main() style argv. Initial components are already
4510 int net_rpc_share(struct net_context
*c
, int argc
, const char **argv
)
4512 NET_API_STATUS status
;
4514 struct functable func
[] = {
4520 "net rpc share add\n"
4528 "net rpc share delete\n"
4533 rpc_share_allowedusers
,
4535 "Modify allowed users",
4536 "net rpc share allowedusers\n"
4537 " Modify allowed users"
4543 "Migrate share to local server",
4544 "net rpc share migrate\n"
4545 " Migrate share to local server"
4552 "net rpc share list\n"
4555 {NULL
, NULL
, 0, NULL
, NULL
}
4558 status
= libnetapi_init(&c
->netapi_ctx
);
4562 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
4563 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
4564 if (c
->opt_kerberos
) {
4565 libnetapi_set_use_kerberos(c
->netapi_ctx
);
4569 if (c
->display_usage
) {
4573 " Alias for net rpc share list\n");
4574 net_display_usage_from_functable(func
);
4578 return rpc_share_list(c
, argc
, argv
);
4581 return net_run_function(c
, argc
, argv
, "net rpc share", func
);
4584 static NTSTATUS
rpc_sh_share_list(struct net_context
*c
,
4585 TALLOC_CTX
*mem_ctx
,
4586 struct rpc_sh_ctx
*ctx
,
4587 struct rpc_pipe_client
*pipe_hnd
,
4588 int argc
, const char **argv
)
4591 return werror_to_ntstatus(W_ERROR(rpc_share_list(c
, argc
, argv
)));
4594 static NTSTATUS
rpc_sh_share_add(struct net_context
*c
,
4595 TALLOC_CTX
*mem_ctx
,
4596 struct rpc_sh_ctx
*ctx
,
4597 struct rpc_pipe_client
*pipe_hnd
,
4598 int argc
, const char **argv
)
4600 NET_API_STATUS status
;
4601 uint32_t parm_err
= 0;
4602 struct SHARE_INFO_2 i2
;
4604 if ((argc
< 2) || (argc
> 3)) {
4605 d_fprintf(stderr
, "usage: %s <share> <path> [comment]\n",
4607 return NT_STATUS_INVALID_PARAMETER
;
4610 i2
.shi2_netname
= argv
[0];
4611 i2
.shi2_type
= STYPE_DISKTREE
;
4612 i2
.shi2_remark
= (argc
== 3) ? argv
[2] : "";
4613 i2
.shi2_permissions
= 0;
4614 i2
.shi2_max_uses
= 0;
4615 i2
.shi2_current_uses
= 0;
4616 i2
.shi2_path
= argv
[1];
4617 i2
.shi2_passwd
= NULL
;
4619 status
= NetShareAdd(pipe_hnd
->desthost
,
4624 return werror_to_ntstatus(W_ERROR(status
));
4627 static NTSTATUS
rpc_sh_share_delete(struct net_context
*c
,
4628 TALLOC_CTX
*mem_ctx
,
4629 struct rpc_sh_ctx
*ctx
,
4630 struct rpc_pipe_client
*pipe_hnd
,
4631 int argc
, const char **argv
)
4634 d_fprintf(stderr
, "usage: %s <share>\n", ctx
->whoami
);
4635 return NT_STATUS_INVALID_PARAMETER
;
4638 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd
->desthost
, argv
[0], 0)));
4641 static NTSTATUS
rpc_sh_share_info(struct net_context
*c
,
4642 TALLOC_CTX
*mem_ctx
,
4643 struct rpc_sh_ctx
*ctx
,
4644 struct rpc_pipe_client
*pipe_hnd
,
4645 int argc
, const char **argv
)
4647 union srvsvc_NetShareInfo info
;
4652 d_fprintf(stderr
, "usage: %s <share>\n", ctx
->whoami
);
4653 return NT_STATUS_INVALID_PARAMETER
;
4656 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
4662 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4666 d_printf("Name: %s\n", info
.info2
->name
);
4667 d_printf("Comment: %s\n", info
.info2
->comment
);
4668 d_printf("Path: %s\n", info
.info2
->path
);
4669 d_printf("Password: %s\n", info
.info2
->password
);
4672 return werror_to_ntstatus(result
);
4675 struct rpc_sh_cmd
*net_rpc_share_cmds(struct net_context
*c
, TALLOC_CTX
*mem_ctx
,
4676 struct rpc_sh_ctx
*ctx
)
4678 static struct rpc_sh_cmd cmds
[] = {
4680 { "list", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_list
,
4681 "List available shares" },
4683 { "add", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_add
,
4686 { "delete", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_delete
,
4689 { "info", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_info
,
4690 "Get information about a share" },
4692 { NULL
, NULL
, 0, NULL
, NULL
}
4698 /****************************************************************************/
4700 static int rpc_file_usage(struct net_context
*c
, int argc
, const char **argv
)
4702 return net_file_usage(c
, argc
, argv
);
4706 * Close a file on a remote RPC server.
4708 * @param argc Standard main() style argc.
4709 * @param argv Standard main() style argv. Initial components are already
4712 * @return A shell status integer (0 for success).
4714 static int rpc_file_close(struct net_context
*c
, int argc
, const char **argv
)
4716 if (argc
< 1 || c
->display_usage
) {
4717 return rpc_file_usage(c
, argc
, argv
);
4720 return NetFileClose(c
->opt_host
, atoi(argv
[0]));
4724 * Formatted print of open file info
4726 * @param r struct FILE_INFO_3 contents
4729 static void display_file_info_3(struct FILE_INFO_3
*r
)
4731 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4732 r
->fi3_id
, r
->fi3_username
, r
->fi3_permissions
,
4733 r
->fi3_num_locks
, r
->fi3_pathname
);
4737 * List files for a user on a remote RPC server.
4739 * @param argc Standard main() style argc.
4740 * @param argv Standard main() style argv. Initial components are already
4743 * @return A shell status integer (0 for success)..
4746 static int rpc_file_user(struct net_context
*c
, int argc
, const char **argv
)
4748 NET_API_STATUS status
;
4749 uint32 preferred_len
= 0xffffffff, i
;
4750 const char *username
=NULL
;
4751 uint32_t total_entries
= 0;
4752 uint32_t entries_read
= 0;
4753 uint32_t resume_handle
= 0;
4754 struct FILE_INFO_3
*i3
= NULL
;
4756 if (c
->display_usage
) {
4757 return rpc_file_usage(c
, argc
, argv
);
4760 /* if argc > 0, must be user command */
4762 username
= smb_xstrdup(argv
[0]);
4765 status
= NetFileEnum(c
->opt_host
,
4779 /* Display results */
4782 "\nEnumerating open files on remote server:\n\n"
4783 "\nFileId Opened by Perms Locks Path"
4784 "\n------ --------- ----- ----- ---- \n");
4785 for (i
= 0; i
< entries_read
; i
++) {
4786 display_file_info_3(&i3
[i
]);
4793 * 'net rpc file' entrypoint.
4794 * @param argc Standard main() style argc.
4795 * @param argv Standard main() style argv. Initial components are already
4799 int net_rpc_file(struct net_context
*c
, int argc
, const char **argv
)
4801 NET_API_STATUS status
;
4803 struct functable func
[] = {
4808 "Close opened file",
4809 "net rpc file close\n"
4810 " Close opened file"
4816 "List files opened by user",
4817 "net rpc file user\n"
4818 " List files opened by user"
4825 "Display information about opened file",
4826 "net rpc file info\n"
4827 " Display information about opened file"
4830 {NULL
, NULL
, 0, NULL
, NULL
}
4833 status
= libnetapi_init(&c
->netapi_ctx
);
4837 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
4838 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
4839 if (c
->opt_kerberos
) {
4840 libnetapi_set_use_kerberos(c
->netapi_ctx
);
4844 if (c
->display_usage
) {
4845 d_printf("Usage:\n");
4846 d_printf("net rpc file\n"
4847 " List opened files\n");
4848 net_display_usage_from_functable(func
);
4852 return rpc_file_user(c
, argc
, argv
);
4855 return net_run_function(c
, argc
, argv
, "net rpc file", func
);
4859 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
4861 * All parameters are provided by the run_rpc_command function, except for
4862 * argc, argv which are passed through.
4864 * @param c A net_context structure.
4865 * @param domain_sid The domain sid acquired from the remote server.
4866 * @param cli A cli_state connected to the server.
4867 * @param mem_ctx Talloc context, destroyed on completion of the function.
4868 * @param argc Standard main() style argc.
4869 * @param argv Standard main() style argv. Initial components are already
4872 * @return Normal NTSTATUS return.
4875 static NTSTATUS
rpc_shutdown_abort_internals(struct net_context
*c
,
4876 const DOM_SID
*domain_sid
,
4877 const char *domain_name
,
4878 struct cli_state
*cli
,
4879 struct rpc_pipe_client
*pipe_hnd
,
4880 TALLOC_CTX
*mem_ctx
,
4884 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
4886 result
= rpccli_initshutdown_Abort(pipe_hnd
, mem_ctx
, NULL
, NULL
);
4888 if (NT_STATUS_IS_OK(result
)) {
4889 d_printf("\nShutdown successfully aborted\n");
4890 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4892 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4898 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
4900 * All parameters are provided by the run_rpc_command function, except for
4901 * argc, argv which are passed through.
4903 * @param c A net_context structure.
4904 * @param domain_sid The domain sid acquired from the remote server.
4905 * @param cli A cli_state connected to the server.
4906 * @param mem_ctx Talloc context, destroyed on completion of the function.
4907 * @param argc Standard main() style argc.
4908 * @param argv Standard main() style argv. Initial components are already
4911 * @return Normal NTSTATUS return.
4914 static NTSTATUS
rpc_reg_shutdown_abort_internals(struct net_context
*c
,
4915 const DOM_SID
*domain_sid
,
4916 const char *domain_name
,
4917 struct cli_state
*cli
,
4918 struct rpc_pipe_client
*pipe_hnd
,
4919 TALLOC_CTX
*mem_ctx
,
4923 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
4925 result
= rpccli_winreg_AbortSystemShutdown(pipe_hnd
, mem_ctx
, NULL
, NULL
);
4927 if (NT_STATUS_IS_OK(result
)) {
4928 d_printf("\nShutdown successfully aborted\n");
4929 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
4931 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
4937 * ABORT the shutdown of a remote RPC server.
4939 * @param argc Standard main() style argc.
4940 * @param argv Standard main() style argv. Initial components are already
4943 * @return A shell status integer (0 for success).
4946 static int rpc_shutdown_abort(struct net_context
*c
, int argc
,
4951 if (c
->display_usage
) {
4953 "net rpc abortshutdown\n"
4954 " Abort a scheduled shutdown\n");
4958 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
4959 rpc_shutdown_abort_internals
, argc
, argv
);
4964 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4966 return run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
4967 rpc_reg_shutdown_abort_internals
,
4972 * Shut down a remote RPC Server via initshutdown pipe.
4974 * All parameters are provided by the run_rpc_command function, except for
4975 * argc, argv which are passed through.
4977 * @param c A net_context structure.
4978 * @param domain_sid The domain sid acquired from the remote server.
4979 * @param cli A cli_state connected to the server.
4980 * @param mem_ctx Talloc context, destroyed on completion of the function.
4981 * @param argc Standard main() style argc.
4982 * @param argv Standard main() style argv. Initial components are already
4985 * @return Normal NTSTATUS return.
4988 NTSTATUS
rpc_init_shutdown_internals(struct net_context
*c
,
4989 const DOM_SID
*domain_sid
,
4990 const char *domain_name
,
4991 struct cli_state
*cli
,
4992 struct rpc_pipe_client
*pipe_hnd
,
4993 TALLOC_CTX
*mem_ctx
,
4997 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
4998 const char *msg
= "This machine will be shutdown shortly";
4999 uint32 timeout
= 20;
5000 struct initshutdown_String msg_string
;
5001 struct initshutdown_String_sub s
;
5003 if (c
->opt_comment
) {
5004 msg
= c
->opt_comment
;
5006 if (c
->opt_timeout
) {
5007 timeout
= c
->opt_timeout
;
5011 msg_string
.name
= &s
;
5013 /* create an entry */
5014 result
= rpccli_initshutdown_Init(pipe_hnd
, mem_ctx
, NULL
,
5015 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5018 if (NT_STATUS_IS_OK(result
)) {
5019 d_printf("\nShutdown of remote machine succeeded\n");
5020 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5022 DEBUG(1,("Shutdown of remote machine failed!\n"));
5028 * Shut down a remote RPC Server via winreg pipe.
5030 * All parameters are provided by the run_rpc_command function, except for
5031 * argc, argv which are passed through.
5033 * @param c A net_context structure.
5034 * @param domain_sid The domain sid acquired from the remote server.
5035 * @param cli A cli_state connected to the server.
5036 * @param mem_ctx Talloc context, destroyed on completion of the function.
5037 * @param argc Standard main() style argc.
5038 * @param argv Standard main() style argv. Initial components are already
5041 * @return Normal NTSTATUS return.
5044 NTSTATUS
rpc_reg_shutdown_internals(struct net_context
*c
,
5045 const DOM_SID
*domain_sid
,
5046 const char *domain_name
,
5047 struct cli_state
*cli
,
5048 struct rpc_pipe_client
*pipe_hnd
,
5049 TALLOC_CTX
*mem_ctx
,
5053 const char *msg
= "This machine will be shutdown shortly";
5054 uint32 timeout
= 20;
5055 struct initshutdown_String msg_string
;
5056 struct initshutdown_String_sub s
;
5060 if (c
->opt_comment
) {
5061 msg
= c
->opt_comment
;
5064 msg_string
.name
= &s
;
5066 if (c
->opt_timeout
) {
5067 timeout
= c
->opt_timeout
;
5070 /* create an entry */
5071 result
= rpccli_winreg_InitiateSystemShutdown(pipe_hnd
, mem_ctx
, NULL
,
5072 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5075 if (NT_STATUS_IS_OK(result
)) {
5076 d_printf("\nShutdown of remote machine succeeded\n");
5078 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5079 if ( W_ERROR_EQUAL(werr
, WERR_MACHINE_LOCKED
) )
5080 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5082 d_fprintf(stderr
, "\nresult was: %s\n", dos_errstr(werr
));
5089 * Shut down a remote RPC server.
5091 * @param argc Standard main() style argc.
5092 * @param argv Standard main() style argv. Initial components are already
5095 * @return A shell status integer (0 for success).
5098 static int rpc_shutdown(struct net_context
*c
, int argc
, const char **argv
)
5102 if (c
->display_usage
) {
5104 "net rpc shutdown\n"
5105 " Shut down a remote RPC server\n");
5109 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
5110 rpc_init_shutdown_internals
, argc
, argv
);
5113 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5114 rc
= run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
5115 rpc_reg_shutdown_internals
, argc
, argv
);
5121 /***************************************************************************
5122 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5123 ***************************************************************************/
5126 * Add interdomain trust account to the RPC server.
5127 * All parameters (except for argc and argv) are passed by run_rpc_command
5130 * @param c A net_context structure.
5131 * @param domain_sid The domain sid acquired from the server.
5132 * @param cli A cli_state connected to the server.
5133 * @param mem_ctx Talloc context, destroyed on completion of the function.
5134 * @param argc Standard main() style argc.
5135 * @param argv Standard main() style argv. Initial components are already
5138 * @return normal NTSTATUS return code.
5141 static NTSTATUS
rpc_trustdom_add_internals(struct net_context
*c
,
5142 const DOM_SID
*domain_sid
,
5143 const char *domain_name
,
5144 struct cli_state
*cli
,
5145 struct rpc_pipe_client
*pipe_hnd
,
5146 TALLOC_CTX
*mem_ctx
,
5150 POLICY_HND connect_pol
, domain_pol
, user_pol
;
5151 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5153 struct lsa_String lsa_acct_name
;
5155 uint32 acct_flags
=0;
5157 uint32_t access_granted
= 0;
5158 union samr_UserInfo info
;
5159 unsigned int orig_timeout
;
5162 d_printf("Usage: net rpc trustdom add <domain_name> "
5163 "<trust password>\n");
5164 return NT_STATUS_INVALID_PARAMETER
;
5168 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5171 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5172 return NT_STATUS_NO_MEMORY
;
5175 strupper_m(acct_name
);
5177 init_lsa_String(&lsa_acct_name
, acct_name
);
5179 /* Get samr policy handle */
5180 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
5182 MAXIMUM_ALLOWED_ACCESS
,
5184 if (!NT_STATUS_IS_OK(result
)) {
5188 /* Get domain policy handle */
5189 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
5191 MAXIMUM_ALLOWED_ACCESS
,
5192 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5194 if (!NT_STATUS_IS_OK(result
)) {
5198 /* This call can take a long time - allow the server to time out.
5199 * 35 seconds should do it. */
5201 orig_timeout
= rpccli_set_timeout(pipe_hnd
, 35000);
5203 /* Create trusting domain's account */
5204 acb_info
= ACB_NORMAL
;
5205 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
5206 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
5207 SAMR_USER_ACCESS_SET_PASSWORD
|
5208 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
5209 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
5211 result
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
5220 /* And restore our original timeout. */
5221 rpccli_set_timeout(pipe_hnd
, orig_timeout
);
5223 if (!NT_STATUS_IS_OK(result
)) {
5224 d_printf("net rpc trustdom add: create user %s failed %s\n",
5225 acct_name
, nt_errstr(result
));
5231 struct samr_LogonHours hours
;
5232 struct lsa_BinaryString parameters
;
5233 const int units_per_week
= 168;
5234 struct samr_CryptPassword crypt_pwd
;
5236 ZERO_STRUCT(notime
);
5238 ZERO_STRUCT(parameters
);
5240 hours
.bits
= talloc_array(mem_ctx
, uint8_t, units_per_week
);
5242 result
= NT_STATUS_NO_MEMORY
;
5245 hours
.units_per_week
= units_per_week
;
5246 memset(hours
.bits
, 0xFF, units_per_week
);
5248 init_samr_CryptPassword(argv
[1],
5249 &cli
->user_session_key
,
5252 init_samr_user_info23(&info
.info23
,
5253 notime
, notime
, notime
,
5254 notime
, notime
, notime
,
5255 NULL
, NULL
, NULL
, NULL
, NULL
,
5256 NULL
, NULL
, NULL
, NULL
, ¶meters
,
5258 SAMR_FIELD_ACCT_FLAGS
| SAMR_FIELD_NT_PASSWORD_PRESENT
,
5260 0, 0, 0, 0, 0, 0, 0,
5263 result
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
5268 if (!NT_STATUS_IS_OK(result
)) {
5269 DEBUG(0,("Could not set trust account password: %s\n",
5270 nt_errstr(result
)));
5276 SAFE_FREE(acct_name
);
5281 * Create interdomain trust account for a remote domain.
5283 * @param argc Standard argc.
5284 * @param argv Standard argv without initial components.
5286 * @return Integer status (0 means success).
5289 static int rpc_trustdom_add(struct net_context
*c
, int argc
, const char **argv
)
5291 if (argc
> 0 && !c
->display_usage
) {
5292 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5293 rpc_trustdom_add_internals
, argc
, argv
);
5296 "net rpc trustdom add <domain_name> <trust password>\n");
5303 * Remove interdomain trust account from the RPC server.
5304 * All parameters (except for argc and argv) are passed by run_rpc_command
5307 * @param c A net_context structure.
5308 * @param domain_sid The domain sid acquired from the server.
5309 * @param cli A cli_state connected to the server.
5310 * @param mem_ctx Talloc context, destroyed on completion of the function.
5311 * @param argc Standard main() style argc.
5312 * @param argv Standard main() style argv. Initial components are already
5315 * @return normal NTSTATUS return code.
5318 static NTSTATUS
rpc_trustdom_del_internals(struct net_context
*c
,
5319 const DOM_SID
*domain_sid
,
5320 const char *domain_name
,
5321 struct cli_state
*cli
,
5322 struct rpc_pipe_client
*pipe_hnd
,
5323 TALLOC_CTX
*mem_ctx
,
5327 POLICY_HND connect_pol
, domain_pol
, user_pol
;
5328 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5330 DOM_SID trust_acct_sid
;
5331 struct samr_Ids user_rids
, name_types
;
5332 struct lsa_String lsa_acct_name
;
5335 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5336 return NT_STATUS_INVALID_PARAMETER
;
5340 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5342 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
5344 if (acct_name
== NULL
)
5345 return NT_STATUS_NO_MEMORY
;
5347 strupper_m(acct_name
);
5349 /* Get samr policy handle */
5350 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
5352 MAXIMUM_ALLOWED_ACCESS
,
5354 if (!NT_STATUS_IS_OK(result
)) {
5358 /* Get domain policy handle */
5359 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
5361 MAXIMUM_ALLOWED_ACCESS
,
5362 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5364 if (!NT_STATUS_IS_OK(result
)) {
5368 init_lsa_String(&lsa_acct_name
, acct_name
);
5370 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
5377 if (!NT_STATUS_IS_OK(result
)) {
5378 d_printf("net rpc trustdom del: LookupNames on user %s failed %s\n",
5379 acct_name
, nt_errstr(result
) );
5383 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
5385 MAXIMUM_ALLOWED_ACCESS
,
5389 if (!NT_STATUS_IS_OK(result
)) {
5390 d_printf("net rpc trustdom del: OpenUser on user %s failed %s\n",
5391 acct_name
, nt_errstr(result
) );
5395 /* append the rid to the domain sid */
5396 sid_copy(&trust_acct_sid
, domain_sid
);
5397 if (!sid_append_rid(&trust_acct_sid
, user_rids
.ids
[0])) {
5401 /* remove the sid */
5403 result
= rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd
, mem_ctx
,
5406 if (!NT_STATUS_IS_OK(result
)) {
5407 d_printf("net rpc trustdom del: RemoveMemberFromForeignDomain on user %s failed %s\n",
5408 acct_name
, nt_errstr(result
) );
5414 result
= rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
5417 if (!NT_STATUS_IS_OK(result
)) {
5418 d_printf("net rpc trustdom del: DeleteUser on user %s failed %s\n",
5419 acct_name
, nt_errstr(result
) );
5423 if (!NT_STATUS_IS_OK(result
)) {
5424 d_printf("Could not set trust account password: %s\n",
5434 * Delete interdomain trust account for a remote domain.
5436 * @param argc Standard argc.
5437 * @param argv Standard argv without initial components.
5439 * @return Integer status (0 means success).
5442 static int rpc_trustdom_del(struct net_context
*c
, int argc
, const char **argv
)
5444 if (argc
> 0 && !c
->display_usage
) {
5445 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5446 rpc_trustdom_del_internals
, argc
, argv
);
5449 "net rpc trustdom del <domain>\n");
5454 static NTSTATUS
rpc_trustdom_get_pdc(struct net_context
*c
,
5455 struct cli_state
*cli
,
5456 TALLOC_CTX
*mem_ctx
,
5457 const char *domain_name
)
5459 char *dc_name
= NULL
;
5460 const char *buffer
= NULL
;
5461 struct rpc_pipe_client
*netr
;
5464 /* Use NetServerEnum2 */
5466 if (cli_get_pdc_name(cli
, domain_name
, &dc_name
)) {
5468 return NT_STATUS_OK
;
5471 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5472 for domain %s\n", domain_name
));
5474 /* Try netr_GetDcName */
5476 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
5478 if (!NT_STATUS_IS_OK(status
)) {
5482 status
= rpccli_netr_GetDcName(netr
, mem_ctx
,
5489 if (NT_STATUS_IS_OK(status
)) {
5493 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5494 for domain %s\n", domain_name
));
5500 * Establish trust relationship to a trusting domain.
5501 * Interdomain account must already be created on remote PDC.
5503 * @param c A net_context structure.
5504 * @param argc Standard argc.
5505 * @param argv Standard argv without initial components.
5507 * @return Integer status (0 means success).
5510 static int rpc_trustdom_establish(struct net_context
*c
, int argc
,
5513 struct cli_state
*cli
= NULL
;
5514 struct sockaddr_storage server_ss
;
5515 struct rpc_pipe_client
*pipe_hnd
= NULL
;
5516 POLICY_HND connect_hnd
;
5517 TALLOC_CTX
*mem_ctx
;
5519 DOM_SID
*domain_sid
;
5524 union lsa_PolicyInformation
*info
= NULL
;
5527 * Connect to \\server\ipc$ as 'our domain' account with password
5530 if (argc
!= 1 || c
->display_usage
) {
5532 "net rpc trustdom establish <domain_name>\n");
5536 domain_name
= smb_xstrdup(argv
[0]);
5537 strupper_m(domain_name
);
5539 /* account name used at first is our domain's name with '$' */
5540 asprintf(&acct_name
, "%s$", lp_workgroup());
5541 strupper_m(acct_name
);
5544 * opt_workgroup will be used by connection functions further,
5545 * hence it should be set to remote domain name instead of ours
5547 if (c
->opt_workgroup
) {
5548 c
->opt_workgroup
= smb_xstrdup(domain_name
);
5551 c
->opt_user_name
= acct_name
;
5553 /* find the domain controller */
5554 if (!net_find_pdc(&server_ss
, pdc_name
, domain_name
)) {
5555 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
5559 /* connect to ipc$ as username/password */
5560 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
, pdc_name
);
5561 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
5563 /* Is it trusting domain account for sure ? */
5564 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5565 nt_errstr(nt_status
)));
5569 /* store who we connected to */
5571 saf_store( domain_name
, pdc_name
);
5574 * Connect to \\server\ipc$ again (this time anonymously)
5577 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
5580 if (NT_STATUS_IS_ERR(nt_status
)) {
5581 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5582 domain_name
, nt_errstr(nt_status
)));
5586 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
5587 "domain %s", domain_name
))) {
5588 DEBUG(0, ("talloc_init() failed\n"));
5593 /* Make sure we're talking to a proper server */
5595 nt_status
= rpc_trustdom_get_pdc(c
, cli
, mem_ctx
, domain_name
);
5596 if (!NT_STATUS_IS_OK(nt_status
)) {
5598 talloc_destroy(mem_ctx
);
5603 * Call LsaOpenPolicy and LsaQueryInfo
5606 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
5608 if (!NT_STATUS_IS_OK(nt_status
)) {
5609 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
5611 talloc_destroy(mem_ctx
);
5615 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, true, SEC_RIGHTS_QUERY_VALUE
,
5617 if (NT_STATUS_IS_ERR(nt_status
)) {
5618 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5619 nt_errstr(nt_status
)));
5621 talloc_destroy(mem_ctx
);
5625 /* Querying info level 5 */
5627 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
5629 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
5631 if (NT_STATUS_IS_ERR(nt_status
)) {
5632 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5633 nt_errstr(nt_status
)));
5635 talloc_destroy(mem_ctx
);
5639 domain_sid
= info
->account_domain
.sid
;
5641 /* There should be actually query info level 3 (following nt serv behaviour),
5642 but I still don't know if it's _really_ necessary */
5645 * Store the password in secrets db
5648 if (!pdb_set_trusteddom_pw(domain_name
, c
->opt_password
, domain_sid
)) {
5649 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5651 talloc_destroy(mem_ctx
);
5656 * Close the pipes and clean up
5659 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
5660 if (NT_STATUS_IS_ERR(nt_status
)) {
5661 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5662 nt_errstr(nt_status
)));
5664 talloc_destroy(mem_ctx
);
5670 talloc_destroy(mem_ctx
);
5672 d_printf("Trust to domain %s established\n", domain_name
);
5677 * Revoke trust relationship to the remote domain.
5679 * @param c A net_context structure.
5680 * @param argc Standard argc.
5681 * @param argv Standard argv without initial components.
5683 * @return Integer status (0 means success).
5686 static int rpc_trustdom_revoke(struct net_context
*c
, int argc
,
5692 if (argc
< 1 || c
->display_usage
) {
5694 "net rpc trustdom revoke <domain_name>\n"
5695 " Revoke trust relationship\n"
5696 " domain_name\tName of domain to revoke trust\n");
5700 /* generate upper cased domain name */
5701 domain_name
= smb_xstrdup(argv
[0]);
5702 strupper_m(domain_name
);
5704 /* delete password of the trust */
5705 if (!pdb_del_trusteddom_pw(domain_name
)) {
5706 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5713 SAFE_FREE(domain_name
);
5717 static NTSTATUS
rpc_query_domain_sid(struct net_context
*c
,
5718 const DOM_SID
*domain_sid
,
5719 const char *domain_name
,
5720 struct cli_state
*cli
,
5721 struct rpc_pipe_client
*pipe_hnd
,
5722 TALLOC_CTX
*mem_ctx
,
5727 sid_to_fstring(str_sid
, domain_sid
);
5728 d_printf("%s\n", str_sid
);
5729 return NT_STATUS_OK
;
5732 static void print_trusted_domain(DOM_SID
*dom_sid
, const char *trusted_dom_name
)
5734 fstring ascii_sid
, padding
;
5735 int pad_len
, col_len
= 20;
5737 /* convert sid into ascii string */
5738 sid_to_fstring(ascii_sid
, dom_sid
);
5740 /* calculate padding space for d_printf to look nicer */
5741 pad_len
= col_len
- strlen(trusted_dom_name
);
5742 padding
[pad_len
] = 0;
5743 do padding
[--pad_len
] = ' '; while (pad_len
);
5745 d_printf("%s%s%s\n", trusted_dom_name
, padding
, ascii_sid
);
5748 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
5749 TALLOC_CTX
*mem_ctx
,
5752 const char *trusted_dom_name
)
5755 union lsa_TrustedDomainInfo
*info
= NULL
;
5756 char *cleartextpwd
= NULL
;
5757 uint8_t nt_hash
[16];
5760 nt_status
= rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd
, mem_ctx
,
5763 LSA_TRUSTED_DOMAIN_INFO_PASSWORD
,
5765 if (NT_STATUS_IS_ERR(nt_status
)) {
5766 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5767 nt_errstr(nt_status
)));
5771 data
= data_blob(info
->password
.password
->data
,
5772 info
->password
.password
->length
);
5774 if (!rpccli_get_pwd_hash(pipe_hnd
, nt_hash
)) {
5775 DEBUG(0, ("Could not retrieve password hash\n"));
5779 cleartextpwd
= decrypt_trustdom_secret(nt_hash
, &data
);
5781 if (cleartextpwd
== NULL
) {
5782 DEBUG(0,("retrieved NULL password\n"));
5783 nt_status
= NT_STATUS_UNSUCCESSFUL
;
5787 if (!pdb_set_trusteddom_pw(trusted_dom_name
, cleartextpwd
, &dom_sid
)) {
5788 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5789 nt_status
= NT_STATUS_UNSUCCESSFUL
;
5793 #ifdef DEBUG_PASSWORD
5794 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5795 "password: [%s]\n", trusted_dom_name
,
5796 sid_string_dbg(&dom_sid
), cleartextpwd
));
5800 SAFE_FREE(cleartextpwd
);
5801 data_blob_free(&data
);
5806 static int rpc_trustdom_vampire(struct net_context
*c
, int argc
,
5809 /* common variables */
5810 TALLOC_CTX
* mem_ctx
;
5811 struct cli_state
*cli
= NULL
;
5812 struct rpc_pipe_client
*pipe_hnd
= NULL
;
5814 const char *domain_name
= NULL
;
5815 DOM_SID
*queried_dom_sid
;
5816 POLICY_HND connect_hnd
;
5817 union lsa_PolicyInformation
*info
= NULL
;
5819 /* trusted domains listing variables */
5820 unsigned int enum_ctx
= 0;
5822 struct lsa_DomainList dom_list
;
5825 if (c
->display_usage
) {
5827 "net rpc trustdom vampire\n"
5828 " Vampire trust relationship from remote server\n");
5833 * Listing trusted domains (stored in secrets.tdb, if local)
5836 mem_ctx
= talloc_init("trust relationships vampire");
5839 * set domain and pdc name to local samba server (default)
5840 * or to remote one given in command line
5843 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
5844 domain_name
= c
->opt_workgroup
;
5845 c
->opt_target_workgroup
= c
->opt_workgroup
;
5847 fstrcpy(pdc_name
, global_myname());
5848 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
5849 c
->opt_target_workgroup
= domain_name
;
5852 /* open \PIPE\lsarpc and open policy handle */
5853 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
5854 if (!NT_STATUS_IS_OK(nt_status
)) {
5855 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
5856 nt_errstr(nt_status
)));
5857 talloc_destroy(mem_ctx
);
5861 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
5863 if (!NT_STATUS_IS_OK(nt_status
)) {
5864 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5865 nt_errstr(nt_status
) ));
5867 talloc_destroy(mem_ctx
);
5871 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, SEC_RIGHTS_QUERY_VALUE
,
5873 if (NT_STATUS_IS_ERR(nt_status
)) {
5874 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5875 nt_errstr(nt_status
)));
5877 talloc_destroy(mem_ctx
);
5881 /* query info level 5 to obtain sid of a domain being queried */
5882 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
5884 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
5887 if (NT_STATUS_IS_ERR(nt_status
)) {
5888 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5889 nt_errstr(nt_status
)));
5891 talloc_destroy(mem_ctx
);
5895 queried_dom_sid
= info
->account_domain
.sid
;
5898 * Keep calling LsaEnumTrustdom over opened pipe until
5899 * the end of enumeration is reached
5902 d_printf("Vampire trusted domains:\n\n");
5905 nt_status
= rpccli_lsa_EnumTrustDom(pipe_hnd
, mem_ctx
,
5910 if (NT_STATUS_IS_ERR(nt_status
)) {
5911 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5912 nt_errstr(nt_status
)));
5914 talloc_destroy(mem_ctx
);
5918 for (i
= 0; i
< dom_list
.count
; i
++) {
5920 print_trusted_domain(dom_list
.domains
[i
].sid
,
5921 dom_list
.domains
[i
].name
.string
);
5923 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
5924 *dom_list
.domains
[i
].sid
,
5925 dom_list
.domains
[i
].name
.string
);
5926 if (!NT_STATUS_IS_OK(nt_status
)) {
5928 talloc_destroy(mem_ctx
);
5934 * in case of no trusted domains say something rather
5935 * than just display blank line
5937 if (!dom_list
.count
) d_printf("none\n");
5939 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
5941 /* close this connection before doing next one */
5942 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
5943 if (NT_STATUS_IS_ERR(nt_status
)) {
5944 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5945 nt_errstr(nt_status
)));
5947 talloc_destroy(mem_ctx
);
5951 /* close lsarpc pipe and connection to IPC$ */
5954 talloc_destroy(mem_ctx
);
5958 static int rpc_trustdom_list(struct net_context
*c
, int argc
, const char **argv
)
5960 /* common variables */
5961 TALLOC_CTX
* mem_ctx
;
5962 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
5963 struct rpc_pipe_client
*pipe_hnd
= NULL
;
5965 const char *domain_name
= NULL
;
5966 DOM_SID
*queried_dom_sid
;
5968 int ascii_dom_name_len
;
5969 POLICY_HND connect_hnd
;
5970 union lsa_PolicyInformation
*info
= NULL
;
5972 /* trusted domains listing variables */
5973 unsigned int num_domains
, enum_ctx
= 0;
5974 int i
, pad_len
, col_len
= 20;
5975 struct lsa_DomainList dom_list
;
5978 /* trusting domains listing variables */
5979 POLICY_HND domain_hnd
;
5980 struct samr_SamArray
*trusts
= NULL
;
5982 if (c
->display_usage
) {
5984 "net rpc trustdom list\n"
5985 " List trust relationships\n");
5990 * Listing trusted domains (stored in secrets.tdb, if local)
5993 mem_ctx
= talloc_init("trust relationships listing");
5996 * set domain and pdc name to local samba server (default)
5997 * or to remote one given in command line
6000 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
6001 domain_name
= c
->opt_workgroup
;
6002 c
->opt_target_workgroup
= c
->opt_workgroup
;
6004 fstrcpy(pdc_name
, global_myname());
6005 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6006 c
->opt_target_workgroup
= domain_name
;
6009 /* open \PIPE\lsarpc and open policy handle */
6010 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6011 if (!NT_STATUS_IS_OK(nt_status
)) {
6012 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6013 nt_errstr(nt_status
)));
6014 talloc_destroy(mem_ctx
);
6018 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6020 if (!NT_STATUS_IS_OK(nt_status
)) {
6021 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6022 nt_errstr(nt_status
) ));
6024 talloc_destroy(mem_ctx
);
6028 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, SEC_RIGHTS_QUERY_VALUE
,
6030 if (NT_STATUS_IS_ERR(nt_status
)) {
6031 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6032 nt_errstr(nt_status
)));
6034 talloc_destroy(mem_ctx
);
6038 /* query info level 5 to obtain sid of a domain being queried */
6039 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
6041 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6044 if (NT_STATUS_IS_ERR(nt_status
)) {
6045 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6046 nt_errstr(nt_status
)));
6048 talloc_destroy(mem_ctx
);
6052 queried_dom_sid
= info
->account_domain
.sid
;
6055 * Keep calling LsaEnumTrustdom over opened pipe until
6056 * the end of enumeration is reached
6059 d_printf("Trusted domains list:\n\n");
6062 nt_status
= rpccli_lsa_EnumTrustDom(pipe_hnd
, mem_ctx
,
6067 if (NT_STATUS_IS_ERR(nt_status
)) {
6068 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6069 nt_errstr(nt_status
)));
6071 talloc_destroy(mem_ctx
);
6075 for (i
= 0; i
< dom_list
.count
; i
++) {
6076 print_trusted_domain(dom_list
.domains
[i
].sid
,
6077 dom_list
.domains
[i
].name
.string
);
6081 * in case of no trusted domains say something rather
6082 * than just display blank line
6084 if (!dom_list
.count
) d_printf("none\n");
6086 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6088 /* close this connection before doing next one */
6089 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6090 if (NT_STATUS_IS_ERR(nt_status
)) {
6091 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6092 nt_errstr(nt_status
)));
6094 talloc_destroy(mem_ctx
);
6098 TALLOC_FREE(pipe_hnd
);
6101 * Listing trusting domains (stored in passdb backend, if local)
6104 d_printf("\nTrusting domains list:\n\n");
6107 * Open \PIPE\samr and get needed policy handles
6109 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
6111 if (!NT_STATUS_IS_OK(nt_status
)) {
6112 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6114 talloc_destroy(mem_ctx
);
6119 nt_status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
6121 SAMR_ACCESS_OPEN_DOMAIN
,
6123 if (!NT_STATUS_IS_OK(nt_status
)) {
6124 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6125 nt_errstr(nt_status
)));
6127 talloc_destroy(mem_ctx
);
6131 /* SamrOpenDomain - we have to open domain policy handle in order to be
6132 able to enumerate accounts*/
6133 nt_status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
6135 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS
,
6138 if (!NT_STATUS_IS_OK(nt_status
)) {
6139 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6140 nt_errstr(nt_status
)));
6142 talloc_destroy(mem_ctx
);
6147 * perform actual enumeration
6150 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6153 nt_status
= rpccli_samr_EnumDomainUsers(pipe_hnd
, mem_ctx
,
6160 if (NT_STATUS_IS_ERR(nt_status
)) {
6161 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6162 nt_errstr(nt_status
)));
6164 talloc_destroy(mem_ctx
);
6168 for (i
= 0; i
< num_domains
; i
++) {
6170 char *str
= CONST_DISCARD(char *, trusts
->entries
[i
].name
.string
);
6173 * get each single domain's sid (do we _really_ need this ?):
6174 * 1) connect to domain's pdc
6175 * 2) query the pdc for domain's sid
6178 /* get rid of '$' tail */
6179 ascii_dom_name_len
= strlen(str
);
6180 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
6181 str
[ascii_dom_name_len
- 1] = '\0';
6183 /* calculate padding space for d_printf to look nicer */
6184 pad_len
= col_len
- strlen(str
);
6185 padding
[pad_len
] = 0;
6186 do padding
[--pad_len
] = ' '; while (pad_len
);
6188 /* set opt_* variables to remote domain */
6190 c
->opt_workgroup
= talloc_strdup(mem_ctx
, str
);
6191 c
->opt_target_workgroup
= c
->opt_workgroup
;
6193 d_printf("%s%s", str
, padding
);
6195 /* connect to remote domain controller */
6196 nt_status
= net_make_ipc_connection(c
,
6197 NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
,
6199 if (NT_STATUS_IS_OK(nt_status
)) {
6200 /* query for domain's sid */
6201 if (run_rpc_command(
6203 &ndr_table_lsarpc
.syntax_id
, 0,
6204 rpc_query_domain_sid
, argc
,
6206 d_fprintf(stderr
, "couldn't get domain's sid\n");
6208 cli_shutdown(remote_cli
);
6211 d_fprintf(stderr
, "domain controller is not "
6213 nt_errstr(nt_status
));
6217 if (!num_domains
) d_printf("none\n");
6219 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6221 /* close opened samr and domain policy handles */
6222 nt_status
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_hnd
);
6223 if (!NT_STATUS_IS_OK(nt_status
)) {
6224 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
6227 nt_status
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6228 if (!NT_STATUS_IS_OK(nt_status
)) {
6229 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
6232 /* close samr pipe and connection to IPC$ */
6235 talloc_destroy(mem_ctx
);
6240 * Entrypoint for 'net rpc trustdom' code.
6242 * @param argc Standard argc.
6243 * @param argv Standard argv without initial components.
6245 * @return Integer status (0 means success).
6248 static int rpc_trustdom(struct net_context
*c
, int argc
, const char **argv
)
6250 struct functable func
[] = {
6255 "Add trusted domain's account",
6256 "net rpc trustdom add\n"
6257 " Add trusted domain's account"
6263 "Remove trusted domain's account",
6264 "net rpc trustdom del\n"
6265 " Remove trusted domain's account"
6269 rpc_trustdom_establish
,
6271 "Establish trust relationship",
6272 "net rpc trustdom establish\n"
6273 " Establish trust relationship"
6277 rpc_trustdom_revoke
,
6279 "Revoke trust relationship",
6280 "net rpc trustdom revoke\n"
6281 " Revoke trust relationship"
6287 "List domain trusts",
6288 "net rpc trustdom list\n"
6289 " List domain trusts"
6293 rpc_trustdom_vampire
,
6295 "Vampire trusts from remote server",
6296 "net rpc trustdom vampire\n"
6297 " Vampire trusts from remote server"
6299 {NULL
, NULL
, 0, NULL
, NULL
}
6302 return net_run_function(c
, argc
, argv
, "net rpc trustdom", func
);
6306 * Check if a server will take rpc commands
6307 * @param flags Type of server to connect to (PDC, DMB, localhost)
6308 * if the host is not explicitly specified
6309 * @return bool (true means rpc supported)
6311 bool net_rpc_check(struct net_context
*c
, unsigned flags
)
6313 struct cli_state
*cli
;
6315 struct sockaddr_storage server_ss
;
6316 char *server_name
= NULL
;
6319 /* flags (i.e. server type) may depend on command */
6320 if (!net_find_server(c
, NULL
, flags
, &server_ss
, &server_name
))
6323 if ((cli
= cli_initialise()) == NULL
) {
6327 status
= cli_connect(cli
, server_name
, &server_ss
);
6328 if (!NT_STATUS_IS_OK(status
))
6330 if (!attempt_netbios_session_request(&cli
, global_myname(),
6331 server_name
, &server_ss
))
6333 if (!cli_negprot(cli
))
6335 if (cli
->protocol
< PROTOCOL_NT1
)
6344 /* dump sam database via samsync rpc calls */
6345 static int rpc_samdump(struct net_context
*c
, int argc
, const char **argv
) {
6346 if (c
->display_usage
) {
6349 " Dump remote SAM database\n");
6353 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
6354 NET_FLAGS_ANONYMOUS
,
6355 rpc_samdump_internals
, argc
, argv
);
6358 /* syncronise sam database via samsync rpc calls */
6359 static int rpc_vampire(struct net_context
*c
, int argc
, const char **argv
)
6361 struct functable func
[] = {
6366 "Dump remote SAM database to ldif",
6367 "net rpc vampire ldif\n"
6368 " Dump remote SAM database to LDIF file or stdout"
6374 "Dump remote SAM database to Kerberos Keytab",
6375 "net rpc vampire keytab\n"
6376 " Dump remote SAM database to Kerberos keytab file"
6382 "Dump remote SAM database to passdb",
6383 "net rpc vampire passdb\n"
6384 " Dump remote SAM database to passdb"
6387 {NULL
, NULL
, 0, NULL
, NULL
}
6391 if (c
->display_usage
) {
6394 " Vampire remote SAM database\n");
6398 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
6399 NET_FLAGS_ANONYMOUS
,
6400 rpc_vampire_internals
,
6404 return net_run_function(c
, argc
, argv
, "net rpc vampire", func
);
6408 * Migrate everything from a print server.
6410 * @param c A net_context structure.
6411 * @param argc Standard main() style argc.
6412 * @param argv Standard main() style argv. Initial components are already
6415 * @return A shell status integer (0 for success).
6417 * The order is important !
6418 * To successfully add drivers the print queues have to exist !
6419 * Applying ACLs should be the last step, because you're easily locked out.
6422 static int rpc_printer_migrate_all(struct net_context
*c
, int argc
,
6427 if (c
->display_usage
) {
6429 "net rpc printer migrate all\n"
6430 " Migrate everything from a print server\n");
6435 d_printf("no server to migrate\n");
6439 ret
= run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6440 rpc_printer_migrate_printers_internals
, argc
,
6445 ret
= run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6446 rpc_printer_migrate_drivers_internals
, argc
,
6451 ret
= run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6452 rpc_printer_migrate_forms_internals
, argc
, argv
);
6456 ret
= run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6457 rpc_printer_migrate_settings_internals
, argc
,
6462 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6463 rpc_printer_migrate_security_internals
, argc
,
6469 * Migrate print drivers from a print server.
6471 * @param c A net_context structure.
6472 * @param argc Standard main() style argc.
6473 * @param argv Standard main() style argv. Initial components are already
6476 * @return A shell status integer (0 for success).
6478 static int rpc_printer_migrate_drivers(struct net_context
*c
, int argc
,
6481 if (c
->display_usage
) {
6483 "net rpc printer migrate drivers\n"
6484 " Migrate print-drivers from a print-server\n");
6489 d_printf("no server to migrate\n");
6493 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6494 rpc_printer_migrate_drivers_internals
,
6499 * Migrate print-forms from a print-server.
6501 * @param c A net_context structure.
6502 * @param argc Standard main() style argc.
6503 * @param argv Standard main() style argv. Initial components are already
6506 * @return A shell status integer (0 for success).
6508 static int rpc_printer_migrate_forms(struct net_context
*c
, int argc
,
6511 if (c
->display_usage
) {
6513 "net rpc printer migrate forms\n"
6514 " Migrate print-forms from a print-server\n");
6519 d_printf("no server to migrate\n");
6523 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6524 rpc_printer_migrate_forms_internals
,
6529 * Migrate printers from a print-server.
6531 * @param c A net_context structure.
6532 * @param argc Standard main() style argc.
6533 * @param argv Standard main() style argv. Initial components are already
6536 * @return A shell status integer (0 for success).
6538 static int rpc_printer_migrate_printers(struct net_context
*c
, int argc
,
6541 if (c
->display_usage
) {
6543 "net rpc printer migrate printers\n"
6544 " Migrate printers from a print-server\n");
6549 d_printf("no server to migrate\n");
6553 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6554 rpc_printer_migrate_printers_internals
,
6559 * Migrate printer-ACLs from a print-server
6561 * @param c A net_context structure.
6562 * @param argc Standard main() style argc.
6563 * @param argv Standard main() style argv. Initial components are already
6566 * @return A shell status integer (0 for success).
6568 static int rpc_printer_migrate_security(struct net_context
*c
, int argc
,
6571 if (c
->display_usage
) {
6573 "net rpc printer migrate security\n"
6574 " Migrate printer-ACLs from a print-server\n");
6579 d_printf("no server to migrate\n");
6583 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6584 rpc_printer_migrate_security_internals
,
6589 * Migrate printer-settings from a print-server.
6591 * @param c A net_context structure.
6592 * @param argc Standard main() style argc.
6593 * @param argv Standard main() style argv. Initial components are already
6596 * @return A shell status integer (0 for success).
6598 static int rpc_printer_migrate_settings(struct net_context
*c
, int argc
,
6601 if (c
->display_usage
) {
6603 "net rpc printer migrate settings\n"
6604 " Migrate printer-settings from a print-server\n");
6609 d_printf("no server to migrate\n");
6613 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6614 rpc_printer_migrate_settings_internals
,
6619 * 'net rpc printer' entrypoint.
6621 * @param c A net_context structure.
6622 * @param argc Standard main() style argc.
6623 * @param argv Standard main() style argv. Initial components are already
6627 int rpc_printer_migrate(struct net_context
*c
, int argc
, const char **argv
)
6630 /* ouch: when addriver and setdriver are called from within
6631 rpc_printer_migrate_drivers_internals, the printer-queue already
6634 struct functable func
[] = {
6637 rpc_printer_migrate_all
,
6639 "Migrate all from remote to local print server",
6640 "net rpc printer migrate all\n"
6641 " Migrate all from remote to local print server"
6645 rpc_printer_migrate_drivers
,
6647 "Migrate drivers to local server",
6648 "net rpc printer migrate drivers\n"
6649 " Migrate drivers to local server"
6653 rpc_printer_migrate_forms
,
6655 "Migrate froms to local server",
6656 "net rpc printer migrate forms\n"
6657 " Migrate froms to local server"
6661 rpc_printer_migrate_printers
,
6663 "Migrate printers to local server",
6664 "net rpc printer migrate printers\n"
6665 " Migrate printers to local server"
6669 rpc_printer_migrate_security
,
6671 "Mirgate printer ACLs to local server",
6672 "net rpc printer migrate security\n"
6673 " Mirgate printer ACLs to local server"
6677 rpc_printer_migrate_settings
,
6679 "Migrate printer settings to local server",
6680 "net rpc printer migrate settings\n"
6681 " Migrate printer settings to local server"
6683 {NULL
, NULL
, 0, NULL
, NULL
}
6686 return net_run_function(c
, argc
, argv
, "net rpc printer migrate",func
);
6691 * List printers on a remote RPC server.
6693 * @param c A net_context structure.
6694 * @param argc Standard main() style argc.
6695 * @param argv Standard main() style argv. Initial components are already
6698 * @return A shell status integer (0 for success).
6700 static int rpc_printer_list(struct net_context
*c
, int argc
, const char **argv
)
6702 if (c
->display_usage
) {
6704 "net rpc printer list\n"
6705 " List printers on a remote RPC server\n");
6709 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6710 rpc_printer_list_internals
,
6715 * List printer-drivers on a remote RPC server.
6717 * @param c A net_context structure.
6718 * @param argc Standard main() style argc.
6719 * @param argv Standard main() style argv. Initial components are already
6722 * @return A shell status integer (0 for success).
6724 static int rpc_printer_driver_list(struct net_context
*c
, int argc
,
6727 if (c
->display_usage
) {
6729 "net rpc printer driver\n"
6730 " List printer-drivers on a remote RPC server\n");
6734 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6735 rpc_printer_driver_list_internals
,
6740 * Publish printer in ADS via MSRPC.
6742 * @param c A net_context structure.
6743 * @param argc Standard main() style argc.
6744 * @param argv Standard main() style argv. Initial components are already
6747 * @return A shell status integer (0 for success).
6749 static int rpc_printer_publish_publish(struct net_context
*c
, int argc
,
6752 if (c
->display_usage
) {
6754 "net rpc printer publish publish\n"
6755 " Publish printer in ADS via MSRPC\n");
6759 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6760 rpc_printer_publish_publish_internals
,
6765 * Update printer in ADS via MSRPC.
6767 * @param c A net_context structure.
6768 * @param argc Standard main() style argc.
6769 * @param argv Standard main() style argv. Initial components are already
6772 * @return A shell status integer (0 for success).
6774 static int rpc_printer_publish_update(struct net_context
*c
, int argc
, const char **argv
)
6776 if (c
->display_usage
) {
6778 "net rpc printer publish update\n"
6779 " Update printer in ADS via MSRPC\n");
6783 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6784 rpc_printer_publish_update_internals
,
6789 * UnPublish printer in ADS via MSRPC.
6791 * @param c A net_context structure.
6792 * @param argc Standard main() style argc.
6793 * @param argv Standard main() style argv. Initial components are already
6796 * @return A shell status integer (0 for success).
6798 static int rpc_printer_publish_unpublish(struct net_context
*c
, int argc
,
6801 if (c
->display_usage
) {
6803 "net rpc printer publish unpublish\n"
6804 " UnPublish printer in ADS via MSRPC\n");
6808 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6809 rpc_printer_publish_unpublish_internals
,
6814 * List published printers via MSRPC.
6816 * @param c A net_context structure.
6817 * @param argc Standard main() style argc.
6818 * @param argv Standard main() style argv. Initial components are already
6821 * @return A shell status integer (0 for success).
6823 static int rpc_printer_publish_list(struct net_context
*c
, int argc
,
6826 if (c
->display_usage
) {
6828 "net rpc printer publish list\n"
6829 " List published printers via MSRPC\n");
6833 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6834 rpc_printer_publish_list_internals
,
6840 * Publish printer in ADS.
6842 * @param c A net_context structure.
6843 * @param argc Standard main() style argc.
6844 * @param argv Standard main() style argv. Initial components are already
6847 * @return A shell status integer (0 for success).
6849 static int rpc_printer_publish(struct net_context
*c
, int argc
,
6853 struct functable func
[] = {
6856 rpc_printer_publish_publish
,
6858 "Publish printer in AD",
6859 "net rpc printer publish publish\n"
6860 " Publish printer in AD"
6864 rpc_printer_publish_update
,
6866 "Update printer in AD",
6867 "net rpc printer publish update\n"
6868 " Update printer in AD"
6872 rpc_printer_publish_unpublish
,
6874 "Unpublish printer",
6875 "net rpc printer publish unpublish\n"
6876 " Unpublish printer"
6880 rpc_printer_publish_list
,
6882 "List published printers",
6883 "net rpc printer publish list\n"
6884 " List published printers"
6886 {NULL
, NULL
, 0, NULL
, NULL
}
6890 if (c
->display_usage
) {
6891 d_printf("Usage:\n");
6892 d_printf("net rpc printer publish\n"
6893 " List published printers\n"
6894 " Alias of net rpc printer publish list\n");
6895 net_display_usage_from_functable(func
);
6898 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6899 rpc_printer_publish_list_internals
,
6903 return net_run_function(c
, argc
, argv
, "net rpc printer publish",func
);
6909 * Display rpc printer help page.
6911 * @param c A net_context structure.
6912 * @param argc Standard main() style argc.
6913 * @param argv Standard main() style argv. Initial components are already
6916 int rpc_printer_usage(struct net_context
*c
, int argc
, const char **argv
)
6918 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"
6919 "\tlists all printers on print-server\n\n");
6920 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
6921 "\tlists all printer-drivers on print-server\n\n");
6922 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
6923 "\tpublishes printer settings in Active Directory\n"
6924 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
6925 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
6926 "\n\tmigrates printers from remote to local server\n\n");
6927 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
6928 "\n\tmigrates printer-settings from remote to local server\n\n");
6929 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
6930 "\n\tmigrates printer-drivers from remote to local server\n\n");
6931 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
6932 "\n\tmigrates printer-forms from remote to local server\n\n");
6933 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
6934 "\n\tmigrates printer-ACLs from remote to local server\n\n");
6935 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
6936 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
6937 "\tremote to local print-server\n\n");
6938 net_common_methods_usage(c
, argc
, argv
);
6939 net_common_flags_usage(c
, argc
, argv
);
6941 "\t-v or --verbose\t\t\tgive verbose output\n"
6942 "\t --destination\t\tmigration target server (default: localhost)\n");
6948 * 'net rpc printer' entrypoint.
6950 * @param c A net_context structure.
6951 * @param argc Standard main() style argc.
6952 * @param argv Standard main() style argv. Initial components are already
6955 int net_rpc_printer(struct net_context
*c
, int argc
, const char **argv
)
6957 struct functable func
[] = {
6962 "List all printers on print server",
6963 "net rpc printer list\n"
6964 " List all printers on print server"
6968 rpc_printer_migrate
,
6970 "Migrate printer to local server",
6971 "net rpc printer migrate\n"
6972 " Migrate printer to local server"
6976 rpc_printer_driver_list
,
6978 "List printer drivers",
6979 "net rpc printer driver\n"
6980 " List printer drivers"
6984 rpc_printer_publish
,
6986 "Publish printer in AD",
6987 "net rpc printer publish\n"
6988 " Publish printer in AD"
6990 {NULL
, NULL
, 0, NULL
, NULL
}
6994 if (c
->display_usage
) {
6995 d_printf("Usage:\n");
6996 d_printf("net rpc printer\n"
6997 " List printers\n");
6998 net_display_usage_from_functable(func
);
7001 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
7002 rpc_printer_list_internals
,
7006 return net_run_function(c
, argc
, argv
, "net rpc printer", func
);
7010 * 'net rpc' entrypoint.
7012 * @param c A net_context structure.
7013 * @param argc Standard main() style argc.
7014 * @param argv Standard main() style argv. Initial components are already
7018 int net_rpc(struct net_context
*c
, int argc
, const char **argv
)
7020 struct functable func
[] = {
7025 "Modify global audit settings",
7027 " Modify global audit settings"
7033 "Show basic info about a domain",
7035 " Show basic info about a domain"
7049 "Join a domain created in server manager",
7051 " Join a domain created in server manager"
7057 "Test that a join is valid",
7058 "net rpc testjoin\n"
7059 " Test that a join is valid"
7065 "List/modify users",
7067 " List/modify users"
7073 "Change a user password",
7074 "net rpc password\n"
7075 " Change a user password\n"
7076 " Alias for net rpc user password"
7082 "List/modify groups",
7084 " List/modify groups"
7090 "List/modify shares",
7092 " List/modify shares"
7106 "List/modify printers",
7108 " List/modify printers"
7112 net_rpc_changetrustpw
,
7114 "Change trust account password",
7115 "net rpc changetrustpw\n"
7116 " Change trust account password"
7122 "Modify domain trusts",
7123 "net rpc trustdom\n"
7124 " Modify domain trusts"
7130 "Abort a remote shutdown",
7131 "net rpc abortshutdown\n"
7132 " Abort a remote shutdown"
7138 "Shutdown a remote server",
7139 "net rpc shutdown\n"
7140 " Shutdown a remote server"
7146 "Dump SAM data of remote NT PDC",
7148 " Dump SAM data of remote NT PDC"
7154 "Sync a remote NT PDC's data into local passdb",
7156 " Sync a remote NT PDC's data into local passdb"
7162 "Fetch the domain sid into local secrets.tdb",
7164 " Fetch the domain sid into local secrets.tdb"
7170 "Manage privileges assigned to SID",
7172 " Manage privileges assigned to SID"
7178 "Start/stop/query remote services",
7180 " Start/stop/query remote services"
7186 "Manage registry hives",
7187 "net rpc registry\n"
7188 " Manage registry hives"
7194 "Open interactive shell on remote server",
7196 " Open interactive shell on remote server"
7198 {NULL
, NULL
, 0, NULL
, NULL
}
7200 return net_run_function(c
, argc
, argv
, "net rpc", func
);