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 if (asprintf(&prompt
, "Enter new password for %s:", argv
[0]) == -1) {
755 u1003
.usri1003_password
= getpass(prompt
);
759 status
= NetUserSetInfo(c
->opt_host
, argv
[0], 1003, (uint8_t *)&u1003
, &parm_err
);
761 /* Display results */
763 d_fprintf(stderr
, "Failed to set password for '%s' with: %s.\n",
764 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
773 * List a user's groups from a remote RPC server.
775 * @param argc Standard main() style argc.
776 * @param argv Standard main() style argv. Initial components are already
779 * @return A shell status integer (0 for success)
782 static int rpc_user_info(struct net_context
*c
, int argc
, const char **argv
)
785 NET_API_STATUS status
;
786 struct GROUP_USERS_INFO_0
*u0
= NULL
;
787 uint32_t entries_read
= 0;
788 uint32_t total_entries
= 0;
792 if (argc
< 1 || c
->display_usage
) {
793 rpc_user_usage(c
, argc
, argv
);
797 status
= NetUserGetGroups(c
->opt_host
,
805 d_fprintf(stderr
, "Failed to get groups for '%s' with: %s.\n",
806 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
811 for (i
=0; i
< entries_read
; i
++) {
812 printf("%s\n", u0
->grui0_name
);
820 * List users on a remote RPC server.
822 * All parameters are provided by the run_rpc_command function, except for
823 * argc, argv which are passed through.
825 * @param domain_sid The domain sid acquired from the remote server.
826 * @param cli A cli_state connected to the server.
827 * @param mem_ctx Talloc context, destroyed on completion of the function.
828 * @param argc Standard main() style argc.
829 * @param argv Standard main() style argv. Initial components are already
832 * @return Normal NTSTATUS return.
835 static int rpc_user_list(struct net_context
*c
, int argc
, const char **argv
)
837 NET_API_STATUS status
;
838 uint32_t start_idx
=0, num_entries
, i
, loop_count
= 0;
839 struct NET_DISPLAY_USER
*info
= NULL
;
842 /* Query domain users */
843 if (c
->opt_long_list_entries
)
844 d_printf("\nUser name Comment"
845 "\n-----------------------------\n");
847 uint32_t max_entries
, max_size
;
849 get_query_dispinfo_params(
850 loop_count
, &max_entries
, &max_size
);
852 status
= NetQueryDisplayInformation(c
->opt_host
,
859 if (status
!= 0 && status
!= ERROR_MORE_DATA
) {
863 info
= (struct NET_DISPLAY_USER
*)buffer
;
865 for (i
= 0; i
< num_entries
; i
++) {
867 if (c
->opt_long_list_entries
)
868 printf("%-21.21s %s\n", info
->usri1_name
,
869 info
->usri1_comment
);
871 printf("%s\n", info
->usri1_name
);
875 NetApiBufferFree(buffer
);
878 start_idx
+= num_entries
;
880 } while (status
== ERROR_MORE_DATA
);
886 * 'net rpc user' entrypoint.
887 * @param argc Standard main() style argc.
888 * @param argv Standard main() style argv. Initial components are already
892 int net_rpc_user(struct net_context
*c
, int argc
, const char **argv
)
894 NET_API_STATUS status
;
896 struct functable func
[] = {
901 "Add specified user",
903 " Add specified user"
909 "List domain groups of user",
910 "net rpc user info\n"
911 " Lis domain groups of user"
917 "Remove specified user",
918 "net rpc user delete\n"
919 " Remove specified user"
925 "Change user password",
926 "net rpc user password\n"
927 " Change user password"
933 "Rename specified user",
934 "net rpc user rename\n"
935 " Rename specified user"
937 {NULL
, NULL
, 0, NULL
, NULL
}
940 status
= libnetapi_init(&c
->netapi_ctx
);
944 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
945 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
946 if (c
->opt_kerberos
) {
947 libnetapi_set_use_kerberos(c
->netapi_ctx
);
951 if (c
->display_usage
) {
952 d_printf("Usage:\n");
953 d_printf("net rpc user\n"
954 " List all users\n");
955 net_display_usage_from_functable(func
);
959 return rpc_user_list(c
, argc
, argv
);
962 return net_run_function(c
, argc
, argv
, "net rpc user", func
);
965 static NTSTATUS
rpc_sh_user_list(struct net_context
*c
,
967 struct rpc_sh_ctx
*ctx
,
968 struct rpc_pipe_client
*pipe_hnd
,
969 int argc
, const char **argv
)
971 return werror_to_ntstatus(W_ERROR(rpc_user_list(c
, argc
, argv
)));
974 static NTSTATUS
rpc_sh_user_info(struct net_context
*c
,
976 struct rpc_sh_ctx
*ctx
,
977 struct rpc_pipe_client
*pipe_hnd
,
978 int argc
, const char **argv
)
980 return werror_to_ntstatus(W_ERROR(rpc_user_info(c
, argc
, argv
)));
983 static NTSTATUS
rpc_sh_handle_user(struct net_context
*c
,
985 struct rpc_sh_ctx
*ctx
,
986 struct rpc_pipe_client
*pipe_hnd
,
987 int argc
, const char **argv
,
989 struct net_context
*c
,
991 struct rpc_sh_ctx
*ctx
,
992 struct rpc_pipe_client
*pipe_hnd
,
993 POLICY_HND
*user_hnd
,
994 int argc
, const char **argv
))
996 POLICY_HND connect_pol
, domain_pol
, user_pol
;
997 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1000 enum lsa_SidType type
;
1003 d_fprintf(stderr
, "usage: %s <username>\n", ctx
->whoami
);
1004 return NT_STATUS_INVALID_PARAMETER
;
1007 ZERO_STRUCT(connect_pol
);
1008 ZERO_STRUCT(domain_pol
);
1009 ZERO_STRUCT(user_pol
);
1011 result
= net_rpc_lookup_name(c
, mem_ctx
, rpc_pipe_np_smb_conn(pipe_hnd
),
1012 argv
[0], NULL
, NULL
, &sid
, &type
);
1013 if (!NT_STATUS_IS_OK(result
)) {
1014 d_fprintf(stderr
, "Could not lookup %s: %s\n", argv
[0],
1019 if (type
!= SID_NAME_USER
) {
1020 d_fprintf(stderr
, "%s is a %s, not a user\n", argv
[0],
1021 sid_type_lookup(type
));
1022 result
= NT_STATUS_NO_SUCH_USER
;
1026 if (!sid_peek_check_rid(ctx
->domain_sid
, &sid
, &rid
)) {
1027 d_fprintf(stderr
, "%s is not in our domain\n", argv
[0]);
1028 result
= NT_STATUS_NO_SUCH_USER
;
1032 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1034 MAXIMUM_ALLOWED_ACCESS
,
1036 if (!NT_STATUS_IS_OK(result
)) {
1040 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1042 MAXIMUM_ALLOWED_ACCESS
,
1045 if (!NT_STATUS_IS_OK(result
)) {
1049 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1051 MAXIMUM_ALLOWED_ACCESS
,
1054 if (!NT_STATUS_IS_OK(result
)) {
1058 result
= fn(c
, mem_ctx
, ctx
, pipe_hnd
, &user_pol
, argc
-1, argv
+1);
1061 if (is_valid_policy_hnd(&user_pol
)) {
1062 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1064 if (is_valid_policy_hnd(&domain_pol
)) {
1065 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1067 if (is_valid_policy_hnd(&connect_pol
)) {
1068 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1073 static NTSTATUS
rpc_sh_user_show_internals(struct net_context
*c
,
1074 TALLOC_CTX
*mem_ctx
,
1075 struct rpc_sh_ctx
*ctx
,
1076 struct rpc_pipe_client
*pipe_hnd
,
1077 POLICY_HND
*user_hnd
,
1078 int argc
, const char **argv
)
1081 union samr_UserInfo
*info
= NULL
;
1084 d_fprintf(stderr
, "usage: %s show <username>\n", ctx
->whoami
);
1085 return NT_STATUS_INVALID_PARAMETER
;
1088 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1092 if (!NT_STATUS_IS_OK(result
)) {
1096 d_printf("user rid: %d, group rid: %d\n",
1098 info
->info21
.primary_gid
);
1103 static NTSTATUS
rpc_sh_user_show(struct net_context
*c
,
1104 TALLOC_CTX
*mem_ctx
,
1105 struct rpc_sh_ctx
*ctx
,
1106 struct rpc_pipe_client
*pipe_hnd
,
1107 int argc
, const char **argv
)
1109 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1110 rpc_sh_user_show_internals
);
1113 #define FETCHSTR(name, rec) \
1114 do { if (strequal(ctx->thiscmd, name)) { \
1115 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1118 #define SETSTR(name, rec, flag) \
1119 do { if (strequal(ctx->thiscmd, name)) { \
1120 init_lsa_String(&(info->info21.rec), argv[0]); \
1121 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1124 static NTSTATUS
rpc_sh_user_str_edit_internals(struct net_context
*c
,
1125 TALLOC_CTX
*mem_ctx
,
1126 struct rpc_sh_ctx
*ctx
,
1127 struct rpc_pipe_client
*pipe_hnd
,
1128 POLICY_HND
*user_hnd
,
1129 int argc
, const char **argv
)
1132 const char *username
;
1133 const char *oldval
= "";
1134 union samr_UserInfo
*info
= NULL
;
1137 d_fprintf(stderr
, "usage: %s <username> [new value|NULL]\n",
1139 return NT_STATUS_INVALID_PARAMETER
;
1142 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1146 if (!NT_STATUS_IS_OK(result
)) {
1150 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1152 FETCHSTR("fullname", full_name
);
1153 FETCHSTR("homedir", home_directory
);
1154 FETCHSTR("homedrive", home_drive
);
1155 FETCHSTR("logonscript", logon_script
);
1156 FETCHSTR("profilepath", profile_path
);
1157 FETCHSTR("description", description
);
1160 d_printf("%s's %s: [%s]\n", username
, ctx
->thiscmd
, oldval
);
1164 if (strcmp(argv
[0], "NULL") == 0) {
1168 ZERO_STRUCT(info
->info21
);
1170 SETSTR("fullname", full_name
, FULL_NAME
);
1171 SETSTR("homedir", home_directory
, HOME_DIRECTORY
);
1172 SETSTR("homedrive", home_drive
, HOME_DRIVE
);
1173 SETSTR("logonscript", logon_script
, LOGON_SCRIPT
);
1174 SETSTR("profilepath", profile_path
, PROFILE_PATH
);
1175 SETSTR("description", description
, DESCRIPTION
);
1177 result
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1182 d_printf("Set %s's %s from [%s] to [%s]\n", username
,
1183 ctx
->thiscmd
, oldval
, argv
[0]);
1190 #define HANDLEFLG(name, rec) \
1191 do { if (strequal(ctx->thiscmd, name)) { \
1192 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1194 newflags = oldflags | ACB_##rec; \
1196 newflags = oldflags & ~ACB_##rec; \
1199 static NTSTATUS
rpc_sh_user_str_edit(struct net_context
*c
,
1200 TALLOC_CTX
*mem_ctx
,
1201 struct rpc_sh_ctx
*ctx
,
1202 struct rpc_pipe_client
*pipe_hnd
,
1203 int argc
, const char **argv
)
1205 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1206 rpc_sh_user_str_edit_internals
);
1209 static NTSTATUS
rpc_sh_user_flag_edit_internals(struct net_context
*c
,
1210 TALLOC_CTX
*mem_ctx
,
1211 struct rpc_sh_ctx
*ctx
,
1212 struct rpc_pipe_client
*pipe_hnd
,
1213 POLICY_HND
*user_hnd
,
1214 int argc
, const char **argv
)
1217 const char *username
;
1218 const char *oldval
= "unknown";
1219 uint32 oldflags
, newflags
;
1221 union samr_UserInfo
*info
= NULL
;
1224 ((argc
== 1) && !strequal(argv
[0], "yes") &&
1225 !strequal(argv
[0], "no"))) {
1226 d_fprintf(stderr
, "usage: %s <username> [yes|no]\n",
1228 return NT_STATUS_INVALID_PARAMETER
;
1231 newval
= strequal(argv
[0], "yes");
1233 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1237 if (!NT_STATUS_IS_OK(result
)) {
1241 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1242 oldflags
= info
->info21
.acct_flags
;
1243 newflags
= info
->info21
.acct_flags
;
1245 HANDLEFLG("disabled", DISABLED
);
1246 HANDLEFLG("pwnotreq", PWNOTREQ
);
1247 HANDLEFLG("autolock", AUTOLOCK
);
1248 HANDLEFLG("pwnoexp", PWNOEXP
);
1251 d_printf("%s's %s flag: %s\n", username
, ctx
->thiscmd
, oldval
);
1255 ZERO_STRUCT(info
->info21
);
1257 info
->info21
.acct_flags
= newflags
;
1258 info
->info21
.fields_present
= SAMR_FIELD_ACCT_FLAGS
;
1260 result
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1265 if (NT_STATUS_IS_OK(result
)) {
1266 d_printf("Set %s's %s flag from [%s] to [%s]\n", username
,
1267 ctx
->thiscmd
, oldval
, argv
[0]);
1275 static NTSTATUS
rpc_sh_user_flag_edit(struct net_context
*c
,
1276 TALLOC_CTX
*mem_ctx
,
1277 struct rpc_sh_ctx
*ctx
,
1278 struct rpc_pipe_client
*pipe_hnd
,
1279 int argc
, const char **argv
)
1281 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1282 rpc_sh_user_flag_edit_internals
);
1285 struct rpc_sh_cmd
*net_rpc_user_edit_cmds(struct net_context
*c
,
1286 TALLOC_CTX
*mem_ctx
,
1287 struct rpc_sh_ctx
*ctx
)
1289 static struct rpc_sh_cmd cmds
[] = {
1291 { "fullname", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1292 "Show/Set a user's full name" },
1294 { "homedir", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1295 "Show/Set a user's home directory" },
1297 { "homedrive", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1298 "Show/Set a user's home drive" },
1300 { "logonscript", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1301 "Show/Set a user's logon script" },
1303 { "profilepath", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1304 "Show/Set a user's profile path" },
1306 { "description", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1307 "Show/Set a user's description" },
1309 { "disabled", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1310 "Show/Set whether a user is disabled" },
1312 { "autolock", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1313 "Show/Set whether a user locked out" },
1315 { "pwnotreq", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1316 "Show/Set whether a user does not need a password" },
1318 { "pwnoexp", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1319 "Show/Set whether a user's password does not expire" },
1321 { NULL
, NULL
, 0, NULL
, NULL
}
1327 struct rpc_sh_cmd
*net_rpc_user_cmds(struct net_context
*c
,
1328 TALLOC_CTX
*mem_ctx
,
1329 struct rpc_sh_ctx
*ctx
)
1331 static struct rpc_sh_cmd cmds
[] = {
1333 { "list", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_list
,
1334 "List available users" },
1336 { "info", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_info
,
1337 "List the domain groups a user is member of" },
1339 { "show", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_show
,
1340 "Show info about a user" },
1342 { "edit", net_rpc_user_edit_cmds
, 0, NULL
,
1343 "Show/Modify a user's fields" },
1345 { NULL
, NULL
, 0, NULL
, NULL
}
1351 /****************************************************************************/
1354 * Basic usage function for 'net rpc group'.
1355 * @param argc Standard main() style argc.
1356 * @param argv Standard main() style argv. Initial components are already
1360 static int rpc_group_usage(struct net_context
*c
, int argc
, const char **argv
)
1362 return net_group_usage(c
, argc
, argv
);
1366 * Delete group on a remote RPC server.
1368 * All parameters are provided by the run_rpc_command function, except for
1369 * argc, argv which are passed through.
1371 * @param domain_sid The domain sid acquired from the remote server.
1372 * @param cli A cli_state connected to the server.
1373 * @param mem_ctx Talloc context, destroyed on completion of the function.
1374 * @param argc Standard main() style argc.
1375 * @param argv Standard main() style argv. Initial components are already
1378 * @return Normal NTSTATUS return.
1381 static NTSTATUS
rpc_group_delete_internals(struct net_context
*c
,
1382 const DOM_SID
*domain_sid
,
1383 const char *domain_name
,
1384 struct cli_state
*cli
,
1385 struct rpc_pipe_client
*pipe_hnd
,
1386 TALLOC_CTX
*mem_ctx
,
1390 POLICY_HND connect_pol
, domain_pol
, group_pol
, user_pol
;
1391 bool group_is_primary
= false;
1392 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1394 struct samr_RidTypeArray
*rids
= NULL
;
1397 /* DOM_GID *user_gids; */
1399 struct samr_Ids group_rids
, name_types
;
1400 struct lsa_String lsa_acct_name
;
1401 union samr_UserInfo
*info
= NULL
;
1403 if (argc
< 1 || c
->display_usage
) {
1404 rpc_group_usage(c
, argc
,argv
);
1405 return NT_STATUS_OK
; /* ok? */
1408 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1410 MAXIMUM_ALLOWED_ACCESS
,
1413 if (!NT_STATUS_IS_OK(result
)) {
1414 d_fprintf(stderr
, "Request samr_Connect2 failed\n");
1418 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1420 MAXIMUM_ALLOWED_ACCESS
,
1421 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1424 if (!NT_STATUS_IS_OK(result
)) {
1425 d_fprintf(stderr
, "Request open_domain failed\n");
1429 init_lsa_String(&lsa_acct_name
, argv
[0]);
1431 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1437 if (!NT_STATUS_IS_OK(result
)) {
1438 d_fprintf(stderr
, "Lookup of '%s' failed\n",argv
[0]);
1442 switch (name_types
.ids
[0])
1444 case SID_NAME_DOM_GRP
:
1445 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
1447 MAXIMUM_ALLOWED_ACCESS
,
1450 if (!NT_STATUS_IS_OK(result
)) {
1451 d_fprintf(stderr
, "Request open_group failed");
1455 group_rid
= group_rids
.ids
[0];
1457 result
= rpccli_samr_QueryGroupMember(pipe_hnd
, mem_ctx
,
1461 if (!NT_STATUS_IS_OK(result
)) {
1462 d_fprintf(stderr
, "Unable to query group members of %s",argv
[0]);
1466 if (c
->opt_verbose
) {
1467 d_printf("Domain Group %s (rid: %d) has %d members\n",
1468 argv
[0],group_rid
, rids
->count
);
1471 /* Check if group is anyone's primary group */
1472 for (i
= 0; i
< rids
->count
; i
++)
1474 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1476 MAXIMUM_ALLOWED_ACCESS
,
1480 if (!NT_STATUS_IS_OK(result
)) {
1481 d_fprintf(stderr
, "Unable to open group member %d\n",
1486 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1491 if (!NT_STATUS_IS_OK(result
)) {
1492 d_fprintf(stderr
, "Unable to lookup userinfo for group member %d\n",
1497 if (info
->info21
.primary_gid
== group_rid
) {
1498 if (c
->opt_verbose
) {
1499 d_printf("Group is primary group of %s\n",
1500 info
->info21
.account_name
.string
);
1502 group_is_primary
= true;
1505 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1508 if (group_is_primary
) {
1509 d_fprintf(stderr
, "Unable to delete group because some "
1510 "of it's members have it as primary group\n");
1511 result
= NT_STATUS_MEMBERS_PRIMARY_GROUP
;
1515 /* remove all group members */
1516 for (i
= 0; i
< rids
->count
; i
++)
1519 d_printf("Remove group member %d...",
1521 result
= rpccli_samr_DeleteGroupMember(pipe_hnd
, mem_ctx
,
1525 if (NT_STATUS_IS_OK(result
)) {
1530 d_printf("failed\n");
1535 result
= rpccli_samr_DeleteDomainGroup(pipe_hnd
, mem_ctx
,
1539 /* removing a local group is easier... */
1540 case SID_NAME_ALIAS
:
1541 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
1543 MAXIMUM_ALLOWED_ACCESS
,
1547 if (!NT_STATUS_IS_OK(result
)) {
1548 d_fprintf(stderr
, "Request open_alias failed\n");
1552 result
= rpccli_samr_DeleteDomAlias(pipe_hnd
, mem_ctx
,
1556 d_fprintf(stderr
, "%s is of type %s. This command is only for deleting local or global groups\n",
1557 argv
[0],sid_type_lookup(name_types
.ids
[0]));
1558 result
= NT_STATUS_UNSUCCESSFUL
;
1562 if (NT_STATUS_IS_OK(result
)) {
1564 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types
.ids
[0]),argv
[0]);
1566 d_fprintf(stderr
, "Deleting of %s failed: %s\n",argv
[0],
1567 get_friendly_nt_error_msg(result
));
1575 static int rpc_group_delete(struct net_context
*c
, int argc
, const char **argv
)
1577 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
1578 rpc_group_delete_internals
, argc
,argv
);
1581 static int rpc_group_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1583 NET_API_STATUS status
;
1584 struct GROUP_INFO_1 info1
;
1585 uint32_t parm_error
= 0;
1587 if (argc
!= 1 || c
->display_usage
) {
1588 rpc_group_usage(c
, argc
, argv
);
1594 info1
.grpi1_name
= argv
[0];
1595 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1596 info1
.grpi1_comment
= c
->opt_comment
;
1599 status
= NetGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1602 d_fprintf(stderr
, "Failed to add group '%s' with: %s.\n",
1603 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1607 d_printf("Added group '%s'.\n", argv
[0]);
1613 static int rpc_alias_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1615 NET_API_STATUS status
;
1616 struct LOCALGROUP_INFO_1 info1
;
1617 uint32_t parm_error
= 0;
1619 if (argc
!= 1 || c
->display_usage
) {
1620 rpc_group_usage(c
, argc
, argv
);
1626 info1
.lgrpi1_name
= argv
[0];
1627 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1628 info1
.lgrpi1_comment
= c
->opt_comment
;
1631 status
= NetLocalGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1634 d_fprintf(stderr
, "Failed to add alias '%s' with: %s.\n",
1635 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1639 d_printf("Added alias '%s'.\n", argv
[0]);
1645 static int rpc_group_add(struct net_context
*c
, int argc
, const char **argv
)
1647 if (c
->opt_localgroup
)
1648 return rpc_alias_add_internals(c
, argc
, argv
);
1650 return rpc_group_add_internals(c
, argc
, argv
);
1653 static NTSTATUS
get_sid_from_name(struct cli_state
*cli
,
1654 TALLOC_CTX
*mem_ctx
,
1657 enum lsa_SidType
*type
)
1659 DOM_SID
*sids
= NULL
;
1660 enum lsa_SidType
*types
= NULL
;
1661 struct rpc_pipe_client
*pipe_hnd
;
1663 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1665 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
1667 if (!NT_STATUS_IS_OK(result
)) {
1671 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, false,
1672 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
1674 if (!NT_STATUS_IS_OK(result
)) {
1678 result
= rpccli_lsa_lookup_names(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
1679 &name
, NULL
, 1, &sids
, &types
);
1681 if (NT_STATUS_IS_OK(result
)) {
1682 sid_copy(sid
, &sids
[0]);
1686 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
1690 TALLOC_FREE(pipe_hnd
);
1693 if (!NT_STATUS_IS_OK(result
) && (StrnCaseCmp(name
, "S-", 2) == 0)) {
1695 /* Try as S-1-5-whatever */
1699 if (string_to_sid(&tmp_sid
, name
)) {
1700 sid_copy(sid
, &tmp_sid
);
1701 *type
= SID_NAME_UNKNOWN
;
1702 result
= NT_STATUS_OK
;
1709 static NTSTATUS
rpc_add_groupmem(struct rpc_pipe_client
*pipe_hnd
,
1710 TALLOC_CTX
*mem_ctx
,
1711 const DOM_SID
*group_sid
,
1714 POLICY_HND connect_pol
, domain_pol
;
1717 POLICY_HND group_pol
;
1719 struct samr_Ids rids
, rid_types
;
1720 struct lsa_String lsa_acct_name
;
1724 sid_copy(&sid
, group_sid
);
1726 if (!sid_split_rid(&sid
, &group_rid
)) {
1727 return NT_STATUS_UNSUCCESSFUL
;
1730 /* Get sam policy handle */
1731 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1733 MAXIMUM_ALLOWED_ACCESS
,
1735 if (!NT_STATUS_IS_OK(result
)) {
1739 /* Get domain policy handle */
1740 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1742 MAXIMUM_ALLOWED_ACCESS
,
1745 if (!NT_STATUS_IS_OK(result
)) {
1749 init_lsa_String(&lsa_acct_name
, member
);
1751 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1758 if (!NT_STATUS_IS_OK(result
)) {
1759 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
1763 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
1765 MAXIMUM_ALLOWED_ACCESS
,
1769 if (!NT_STATUS_IS_OK(result
)) {
1773 result
= rpccli_samr_AddGroupMember(pipe_hnd
, mem_ctx
,
1776 0x0005); /* unknown flags */
1779 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1783 static NTSTATUS
rpc_add_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
1784 TALLOC_CTX
*mem_ctx
,
1785 const DOM_SID
*alias_sid
,
1788 POLICY_HND connect_pol
, domain_pol
;
1791 POLICY_HND alias_pol
;
1794 enum lsa_SidType member_type
;
1798 sid_copy(&sid
, alias_sid
);
1800 if (!sid_split_rid(&sid
, &alias_rid
)) {
1801 return NT_STATUS_UNSUCCESSFUL
;
1804 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
1805 member
, &member_sid
, &member_type
);
1807 if (!NT_STATUS_IS_OK(result
)) {
1808 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
1812 /* Get sam policy handle */
1813 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1815 MAXIMUM_ALLOWED_ACCESS
,
1817 if (!NT_STATUS_IS_OK(result
)) {
1821 /* Get domain policy handle */
1822 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1824 MAXIMUM_ALLOWED_ACCESS
,
1827 if (!NT_STATUS_IS_OK(result
)) {
1831 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
1833 MAXIMUM_ALLOWED_ACCESS
,
1837 if (!NT_STATUS_IS_OK(result
)) {
1841 result
= rpccli_samr_AddAliasMember(pipe_hnd
, mem_ctx
,
1845 if (!NT_STATUS_IS_OK(result
)) {
1850 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1854 static NTSTATUS
rpc_group_addmem_internals(struct net_context
*c
,
1855 const DOM_SID
*domain_sid
,
1856 const char *domain_name
,
1857 struct cli_state
*cli
,
1858 struct rpc_pipe_client
*pipe_hnd
,
1859 TALLOC_CTX
*mem_ctx
,
1864 enum lsa_SidType group_type
;
1866 if (argc
!= 2 || c
->display_usage
) {
1868 "net rpc group addmem <group> <member>\n"
1869 " Add a member to a group\n"
1870 " group\tGroup to add member to\n"
1871 " member\tMember to add to group\n");
1872 return NT_STATUS_UNSUCCESSFUL
;
1875 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
1876 &group_sid
, &group_type
))) {
1877 d_fprintf(stderr
, "Could not lookup group name %s\n", argv
[0]);
1878 return NT_STATUS_UNSUCCESSFUL
;
1881 if (group_type
== SID_NAME_DOM_GRP
) {
1882 NTSTATUS result
= rpc_add_groupmem(pipe_hnd
, mem_ctx
,
1883 &group_sid
, argv
[1]);
1885 if (!NT_STATUS_IS_OK(result
)) {
1886 d_fprintf(stderr
, "Could not add %s to %s: %s\n",
1887 argv
[1], argv
[0], nt_errstr(result
));
1892 if (group_type
== SID_NAME_ALIAS
) {
1893 NTSTATUS result
= rpc_add_aliasmem(pipe_hnd
, mem_ctx
,
1894 &group_sid
, argv
[1]);
1896 if (!NT_STATUS_IS_OK(result
)) {
1897 d_fprintf(stderr
, "Could not add %s to %s: %s\n",
1898 argv
[1], argv
[0], nt_errstr(result
));
1903 d_fprintf(stderr
, "Can only add members to global or local groups "
1904 "which %s is not\n", argv
[0]);
1906 return NT_STATUS_UNSUCCESSFUL
;
1909 static int rpc_group_addmem(struct net_context
*c
, int argc
, const char **argv
)
1911 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
1912 rpc_group_addmem_internals
,
1916 static NTSTATUS
rpc_del_groupmem(struct net_context
*c
,
1917 struct rpc_pipe_client
*pipe_hnd
,
1918 TALLOC_CTX
*mem_ctx
,
1919 const DOM_SID
*group_sid
,
1922 POLICY_HND connect_pol
, domain_pol
;
1925 POLICY_HND group_pol
;
1927 struct samr_Ids rids
, rid_types
;
1928 struct lsa_String lsa_acct_name
;
1932 sid_copy(&sid
, group_sid
);
1934 if (!sid_split_rid(&sid
, &group_rid
))
1935 return NT_STATUS_UNSUCCESSFUL
;
1937 /* Get sam policy handle */
1938 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1940 MAXIMUM_ALLOWED_ACCESS
,
1942 if (!NT_STATUS_IS_OK(result
))
1945 /* Get domain policy handle */
1946 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1948 MAXIMUM_ALLOWED_ACCESS
,
1951 if (!NT_STATUS_IS_OK(result
))
1954 init_lsa_String(&lsa_acct_name
, member
);
1956 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1962 if (!NT_STATUS_IS_OK(result
)) {
1963 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
1967 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
1969 MAXIMUM_ALLOWED_ACCESS
,
1973 if (!NT_STATUS_IS_OK(result
))
1976 result
= rpccli_samr_DeleteGroupMember(pipe_hnd
, mem_ctx
,
1981 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1985 static NTSTATUS
rpc_del_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
1986 TALLOC_CTX
*mem_ctx
,
1987 const DOM_SID
*alias_sid
,
1990 POLICY_HND connect_pol
, domain_pol
;
1993 POLICY_HND alias_pol
;
1996 enum lsa_SidType member_type
;
2000 sid_copy(&sid
, alias_sid
);
2002 if (!sid_split_rid(&sid
, &alias_rid
))
2003 return NT_STATUS_UNSUCCESSFUL
;
2005 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2006 member
, &member_sid
, &member_type
);
2008 if (!NT_STATUS_IS_OK(result
)) {
2009 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2013 /* Get sam policy handle */
2014 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2016 MAXIMUM_ALLOWED_ACCESS
,
2018 if (!NT_STATUS_IS_OK(result
)) {
2022 /* Get domain policy handle */
2023 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2025 MAXIMUM_ALLOWED_ACCESS
,
2028 if (!NT_STATUS_IS_OK(result
)) {
2032 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2034 MAXIMUM_ALLOWED_ACCESS
,
2038 if (!NT_STATUS_IS_OK(result
))
2041 result
= rpccli_samr_DeleteAliasMember(pipe_hnd
, mem_ctx
,
2045 if (!NT_STATUS_IS_OK(result
))
2049 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2053 static NTSTATUS
rpc_group_delmem_internals(struct net_context
*c
,
2054 const DOM_SID
*domain_sid
,
2055 const char *domain_name
,
2056 struct cli_state
*cli
,
2057 struct rpc_pipe_client
*pipe_hnd
,
2058 TALLOC_CTX
*mem_ctx
,
2063 enum lsa_SidType group_type
;
2065 if (argc
!= 2 || c
->display_usage
) {
2067 "net rpc group delmem <group> <member>\n"
2068 " Delete a member from a group\n"
2069 " group\tGroup to delete member from\n"
2070 " member\tMember to delete from group\n");
2071 return NT_STATUS_UNSUCCESSFUL
;
2074 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2075 &group_sid
, &group_type
))) {
2076 d_fprintf(stderr
, "Could not lookup group name %s\n", argv
[0]);
2077 return NT_STATUS_UNSUCCESSFUL
;
2080 if (group_type
== SID_NAME_DOM_GRP
) {
2081 NTSTATUS result
= rpc_del_groupmem(c
, pipe_hnd
, mem_ctx
,
2082 &group_sid
, argv
[1]);
2084 if (!NT_STATUS_IS_OK(result
)) {
2085 d_fprintf(stderr
, "Could not del %s from %s: %s\n",
2086 argv
[1], argv
[0], nt_errstr(result
));
2091 if (group_type
== SID_NAME_ALIAS
) {
2092 NTSTATUS result
= rpc_del_aliasmem(pipe_hnd
, mem_ctx
,
2093 &group_sid
, argv
[1]);
2095 if (!NT_STATUS_IS_OK(result
)) {
2096 d_fprintf(stderr
, "Could not del %s from %s: %s\n",
2097 argv
[1], argv
[0], nt_errstr(result
));
2102 d_fprintf(stderr
, "Can only delete members from global or local groups "
2103 "which %s is not\n", argv
[0]);
2105 return NT_STATUS_UNSUCCESSFUL
;
2108 static int rpc_group_delmem(struct net_context
*c
, int argc
, const char **argv
)
2110 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2111 rpc_group_delmem_internals
,
2116 * List groups on a remote RPC server.
2118 * All parameters are provided by the run_rpc_command function, except for
2119 * argc, argv which are passes through.
2121 * @param domain_sid The domain sid acquired from the remote server.
2122 * @param cli A cli_state connected to the server.
2123 * @param mem_ctx Talloc context, destroyed on completion of the function.
2124 * @param argc Standard main() style argc.
2125 * @param argv Standard main() style argv. Initial components are already
2128 * @return Normal NTSTATUS return.
2131 static NTSTATUS
rpc_group_list_internals(struct net_context
*c
,
2132 const DOM_SID
*domain_sid
,
2133 const char *domain_name
,
2134 struct cli_state
*cli
,
2135 struct rpc_pipe_client
*pipe_hnd
,
2136 TALLOC_CTX
*mem_ctx
,
2140 POLICY_HND connect_pol
, domain_pol
;
2141 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2142 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
2143 struct samr_SamArray
*groups
= NULL
;
2144 bool global
= false;
2146 bool builtin
= false;
2148 if (c
->display_usage
) {
2150 "net rpc group list [global] [local] [builtin]\n"
2151 " List groups on RPC server\n"
2152 " global\tList global groups\n"
2153 " local\tList local groups\n"
2154 " builtin\tList builtin groups\n"
2155 " If none of global, local or builtin is "
2156 "specified, all three options are considered set\n");
2157 return NT_STATUS_OK
;
2166 for (i
=0; i
<argc
; i
++) {
2167 if (strequal(argv
[i
], "global"))
2170 if (strequal(argv
[i
], "local"))
2173 if (strequal(argv
[i
], "builtin"))
2177 /* Get sam policy handle */
2179 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2181 MAXIMUM_ALLOWED_ACCESS
,
2183 if (!NT_STATUS_IS_OK(result
)) {
2187 /* Get domain policy handle */
2189 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2191 MAXIMUM_ALLOWED_ACCESS
,
2192 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2194 if (!NT_STATUS_IS_OK(result
)) {
2198 /* Query domain groups */
2199 if (c
->opt_long_list_entries
)
2200 d_printf("\nGroup name Comment"
2201 "\n-----------------------------\n");
2203 uint32_t max_size
, total_size
, returned_size
;
2204 union samr_DispInfo info
;
2208 get_query_dispinfo_params(
2209 loop_count
, &max_entries
, &max_size
);
2211 result
= rpccli_samr_QueryDisplayInfo(pipe_hnd
, mem_ctx
,
2220 num_entries
= info
.info3
.count
;
2221 start_idx
+= info
.info3
.count
;
2223 if (!NT_STATUS_IS_OK(result
) &&
2224 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2227 for (i
= 0; i
< num_entries
; i
++) {
2229 const char *group
= NULL
;
2230 const char *desc
= NULL
;
2232 group
= info
.info3
.entries
[i
].account_name
.string
;
2233 desc
= info
.info3
.entries
[i
].description
.string
;
2235 if (c
->opt_long_list_entries
)
2236 printf("%-21.21s %-50.50s\n",
2239 printf("%s\n", group
);
2241 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2242 /* query domain aliases */
2247 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
2253 if (!NT_STATUS_IS_OK(result
) &&
2254 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2257 for (i
= 0; i
< num_entries
; i
++) {
2259 const char *description
= NULL
;
2261 if (c
->opt_long_list_entries
) {
2263 POLICY_HND alias_pol
;
2264 union samr_AliasInfo
*info
= NULL
;
2266 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2269 groups
->entries
[i
].idx
,
2271 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd
, mem_ctx
,
2275 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd
, mem_ctx
,
2277 description
= info
->description
.string
;
2281 if (description
!= NULL
) {
2282 printf("%-21.21s %-50.50s\n",
2283 groups
->entries
[i
].name
.string
,
2286 printf("%s\n", groups
->entries
[i
].name
.string
);
2289 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2290 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
2291 /* Get builtin policy handle */
2293 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2295 MAXIMUM_ALLOWED_ACCESS
,
2296 CONST_DISCARD(struct dom_sid2
*, &global_sid_Builtin
),
2298 if (!NT_STATUS_IS_OK(result
)) {
2301 /* query builtin aliases */
2304 if (!builtin
) break;
2306 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
2312 if (!NT_STATUS_IS_OK(result
) &&
2313 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2316 for (i
= 0; i
< num_entries
; i
++) {
2318 const char *description
= NULL
;
2320 if (c
->opt_long_list_entries
) {
2322 POLICY_HND alias_pol
;
2323 union samr_AliasInfo
*info
= NULL
;
2325 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2328 groups
->entries
[i
].idx
,
2330 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd
, mem_ctx
,
2334 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd
, mem_ctx
,
2336 description
= info
->description
.string
;
2340 if (description
!= NULL
) {
2341 printf("%-21.21s %-50.50s\n",
2342 groups
->entries
[i
].name
.string
,
2345 printf("%s\n", groups
->entries
[i
].name
.string
);
2348 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2354 static int rpc_group_list(struct net_context
*c
, int argc
, const char **argv
)
2356 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2357 rpc_group_list_internals
,
2361 static NTSTATUS
rpc_list_group_members(struct net_context
*c
,
2362 struct rpc_pipe_client
*pipe_hnd
,
2363 TALLOC_CTX
*mem_ctx
,
2364 const char *domain_name
,
2365 const DOM_SID
*domain_sid
,
2366 POLICY_HND
*domain_pol
,
2370 POLICY_HND group_pol
;
2371 uint32 num_members
, *group_rids
;
2373 struct samr_RidTypeArray
*rids
= NULL
;
2374 struct lsa_Strings names
;
2375 struct samr_Ids types
;
2378 sid_to_fstring(sid_str
, domain_sid
);
2380 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2382 MAXIMUM_ALLOWED_ACCESS
,
2386 if (!NT_STATUS_IS_OK(result
))
2389 result
= rpccli_samr_QueryGroupMember(pipe_hnd
, mem_ctx
,
2393 if (!NT_STATUS_IS_OK(result
))
2396 num_members
= rids
->count
;
2397 group_rids
= rids
->rids
;
2399 while (num_members
> 0) {
2400 int this_time
= 512;
2402 if (num_members
< this_time
)
2403 this_time
= num_members
;
2405 result
= rpccli_samr_LookupRids(pipe_hnd
, mem_ctx
,
2412 if (!NT_STATUS_IS_OK(result
))
2415 /* We only have users as members, but make the output
2416 the same as the output of alias members */
2418 for (i
= 0; i
< this_time
; i
++) {
2420 if (c
->opt_long_list_entries
) {
2421 printf("%s-%d %s\\%s %d\n", sid_str
,
2422 group_rids
[i
], domain_name
,
2423 names
.names
[i
].string
,
2426 printf("%s\\%s\n", domain_name
,
2427 names
.names
[i
].string
);
2431 num_members
-= this_time
;
2435 return NT_STATUS_OK
;
2438 static NTSTATUS
rpc_list_alias_members(struct net_context
*c
,
2439 struct rpc_pipe_client
*pipe_hnd
,
2440 TALLOC_CTX
*mem_ctx
,
2441 POLICY_HND
*domain_pol
,
2445 struct rpc_pipe_client
*lsa_pipe
;
2446 POLICY_HND alias_pol
, lsa_pol
;
2448 DOM_SID
*alias_sids
;
2451 enum lsa_SidType
*types
;
2453 struct lsa_SidArray sid_array
;
2455 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2457 MAXIMUM_ALLOWED_ACCESS
,
2461 if (!NT_STATUS_IS_OK(result
))
2464 result
= rpccli_samr_GetMembersInAlias(pipe_hnd
, mem_ctx
,
2468 if (!NT_STATUS_IS_OK(result
)) {
2469 d_fprintf(stderr
, "Couldn't list alias members\n");
2473 num_members
= sid_array
.num_sids
;
2475 if (num_members
== 0) {
2476 return NT_STATUS_OK
;
2479 result
= cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd
),
2480 &ndr_table_lsarpc
.syntax_id
,
2482 if (!NT_STATUS_IS_OK(result
)) {
2483 d_fprintf(stderr
, "Couldn't open LSA pipe. Error was %s\n",
2484 nt_errstr(result
) );
2488 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, true,
2489 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
2491 if (!NT_STATUS_IS_OK(result
)) {
2492 d_fprintf(stderr
, "Couldn't open LSA policy handle\n");
2493 TALLOC_FREE(lsa_pipe
);
2497 alias_sids
= TALLOC_ZERO_ARRAY(mem_ctx
, DOM_SID
, num_members
);
2499 d_fprintf(stderr
, "Out of memory\n");
2500 TALLOC_FREE(lsa_pipe
);
2501 return NT_STATUS_NO_MEMORY
;
2504 for (i
=0; i
<num_members
; i
++) {
2505 sid_copy(&alias_sids
[i
], sid_array
.sids
[i
].sid
);
2508 result
= rpccli_lsa_lookup_sids(lsa_pipe
, mem_ctx
, &lsa_pol
,
2509 num_members
, alias_sids
,
2510 &domains
, &names
, &types
);
2512 if (!NT_STATUS_IS_OK(result
) &&
2513 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
2514 d_fprintf(stderr
, "Couldn't lookup SIDs\n");
2515 TALLOC_FREE(lsa_pipe
);
2519 for (i
= 0; i
< num_members
; i
++) {
2521 sid_to_fstring(sid_str
, &alias_sids
[i
]);
2523 if (c
->opt_long_list_entries
) {
2524 printf("%s %s\\%s %d\n", sid_str
,
2525 domains
[i
] ? domains
[i
] : "*unknown*",
2526 names
[i
] ? names
[i
] : "*unknown*", types
[i
]);
2529 printf("%s\\%s\n", domains
[i
], names
[i
]);
2531 printf("%s\n", sid_str
);
2535 TALLOC_FREE(lsa_pipe
);
2536 return NT_STATUS_OK
;
2539 static NTSTATUS
rpc_group_members_internals(struct net_context
*c
,
2540 const DOM_SID
*domain_sid
,
2541 const char *domain_name
,
2542 struct cli_state
*cli
,
2543 struct rpc_pipe_client
*pipe_hnd
,
2544 TALLOC_CTX
*mem_ctx
,
2549 POLICY_HND connect_pol
, domain_pol
;
2550 struct samr_Ids rids
, rid_types
;
2551 struct lsa_String lsa_acct_name
;
2553 /* Get sam policy handle */
2555 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2557 MAXIMUM_ALLOWED_ACCESS
,
2560 if (!NT_STATUS_IS_OK(result
))
2563 /* Get domain policy handle */
2565 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2567 MAXIMUM_ALLOWED_ACCESS
,
2568 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2571 if (!NT_STATUS_IS_OK(result
))
2574 init_lsa_String(&lsa_acct_name
, argv
[0]); /* sure? */
2576 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2583 if (!NT_STATUS_IS_OK(result
)) {
2585 /* Ok, did not find it in the global sam, try with builtin */
2587 DOM_SID sid_Builtin
;
2589 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
2591 sid_copy(&sid_Builtin
, &global_sid_Builtin
);
2593 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2595 MAXIMUM_ALLOWED_ACCESS
,
2599 if (!NT_STATUS_IS_OK(result
)) {
2600 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2604 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2611 if (!NT_STATUS_IS_OK(result
)) {
2612 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2617 if (rids
.count
!= 1) {
2618 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2622 if (rid_types
.ids
[0] == SID_NAME_DOM_GRP
) {
2623 return rpc_list_group_members(c
, pipe_hnd
, mem_ctx
, domain_name
,
2624 domain_sid
, &domain_pol
,
2628 if (rid_types
.ids
[0] == SID_NAME_ALIAS
) {
2629 return rpc_list_alias_members(c
, pipe_hnd
, mem_ctx
, &domain_pol
,
2633 return NT_STATUS_NO_SUCH_GROUP
;
2636 static int rpc_group_members(struct net_context
*c
, int argc
, const char **argv
)
2638 if (argc
!= 1 || c
->display_usage
) {
2639 return rpc_group_usage(c
, argc
, argv
);
2642 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2643 rpc_group_members_internals
,
2647 static int rpc_group_rename_internals(struct net_context
*c
, int argc
, const char **argv
)
2649 NET_API_STATUS status
;
2650 struct GROUP_INFO_0 g0
;
2654 d_printf("Usage: 'net rpc group rename group newname'\n");
2658 g0
.grpi0_name
= argv
[1];
2660 status
= NetGroupSetInfo(c
->opt_host
,
2667 d_fprintf(stderr
, "Renaming group %s failed with: %s\n",
2668 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
2676 static int rpc_group_rename(struct net_context
*c
, int argc
, const char **argv
)
2678 if (argc
!= 2 || c
->display_usage
) {
2679 return rpc_group_usage(c
, argc
, argv
);
2682 return rpc_group_rename_internals(c
, argc
, argv
);
2686 * 'net rpc group' entrypoint.
2687 * @param argc Standard main() style argc.
2688 * @param argv Standard main() style argv. Initial components are already
2692 int net_rpc_group(struct net_context
*c
, int argc
, const char **argv
)
2694 NET_API_STATUS status
;
2696 struct functable func
[] = {
2701 "Create specified group",
2702 "net rpc group add\n"
2703 " Create specified group"
2709 "Delete specified group",
2710 "net rpc group delete\n"
2711 " Delete specified group"
2717 "Add member to group",
2718 "net rpc group addmem\n"
2719 " Add member to group"
2725 "Remove member from group",
2726 "net rpc group delmem\n"
2727 " Remove member from group"
2734 "net rpc group list\n"
2741 "List group members",
2742 "net rpc group members\n"
2743 " List group members"
2750 "net rpc group rename\n"
2753 {NULL
, NULL
, 0, NULL
, NULL
}
2756 status
= libnetapi_init(&c
->netapi_ctx
);
2760 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
2761 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
2762 if (c
->opt_kerberos
) {
2763 libnetapi_set_use_kerberos(c
->netapi_ctx
);
2767 if (c
->display_usage
) {
2768 d_printf("Usage:\n");
2769 d_printf("net rpc group\n"
2770 " Alias for net rpc group list global local "
2772 net_display_usage_from_functable(func
);
2776 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2777 rpc_group_list_internals
,
2781 return net_run_function(c
, argc
, argv
, "net rpc group", func
);
2784 /****************************************************************************/
2786 static int rpc_share_usage(struct net_context
*c
, int argc
, const char **argv
)
2788 return net_share_usage(c
, argc
, argv
);
2792 * Add a share on a remote RPC server.
2794 * @param argc Standard main() style argc.
2795 * @param argv Standard main() style argv. Initial components are already
2798 * @return A shell status integer (0 for success).
2801 static int rpc_share_add(struct net_context
*c
, int argc
, const char **argv
)
2803 NET_API_STATUS status
;
2806 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
2807 uint32 num_users
=0, perms
=0;
2808 char *password
=NULL
; /* don't allow a share password */
2809 struct SHARE_INFO_2 i2
;
2810 uint32_t parm_error
= 0;
2812 if ((argc
< 1) || !strchr(argv
[0], '=') || c
->display_usage
) {
2813 return rpc_share_usage(c
, argc
, argv
);
2816 if ((sharename
= talloc_strdup(c
, argv
[0])) == NULL
) {
2820 path
= strchr(sharename
, '=');
2827 i2
.shi2_netname
= sharename
;
2828 i2
.shi2_type
= type
;
2829 i2
.shi2_remark
= c
->opt_comment
;
2830 i2
.shi2_permissions
= perms
;
2831 i2
.shi2_max_uses
= c
->opt_maxusers
;
2832 i2
.shi2_current_uses
= num_users
;
2833 i2
.shi2_path
= path
;
2834 i2
.shi2_passwd
= password
;
2836 status
= NetShareAdd(c
->opt_host
,
2841 printf("NetShareAdd failed with: %s\n",
2842 libnetapi_get_error_string(c
->netapi_ctx
, status
));
2849 * Delete a share on a remote RPC server.
2851 * @param domain_sid The domain sid acquired from the remote server.
2852 * @param argc Standard main() style argc.
2853 * @param argv Standard main() style argv. Initial components are already
2856 * @return A shell status integer (0 for success).
2858 static int rpc_share_delete(struct net_context
*c
, int argc
, const char **argv
)
2860 if (argc
< 1 || c
->display_usage
) {
2861 return rpc_share_usage(c
, argc
, argv
);
2864 return NetShareDel(c
->opt_host
, argv
[0], 0);
2868 * Formatted print of share info
2870 * @param r pointer to SHARE_INFO_1 to format
2873 static void display_share_info_1(struct net_context
*c
,
2874 struct SHARE_INFO_1
*r
)
2876 if (c
->opt_long_list_entries
) {
2877 d_printf("%-12s %-8.8s %-50s\n",
2879 net_share_type_str(r
->shi1_type
& ~(STYPE_TEMPORARY
|STYPE_HIDDEN
)),
2882 d_printf("%s\n", r
->shi1_netname
);
2886 static WERROR
get_share_info(struct net_context
*c
,
2887 struct rpc_pipe_client
*pipe_hnd
,
2888 TALLOC_CTX
*mem_ctx
,
2892 struct srvsvc_NetShareInfoCtr
*info_ctr
)
2896 union srvsvc_NetShareInfo info
;
2898 /* no specific share requested, enumerate all */
2901 uint32_t preferred_len
= 0xffffffff;
2902 uint32_t total_entries
= 0;
2903 uint32_t resume_handle
= 0;
2905 info_ctr
->level
= level
;
2907 status
= rpccli_srvsvc_NetShareEnumAll(pipe_hnd
, mem_ctx
,
2917 /* request just one share */
2918 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
2925 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
2930 ZERO_STRUCTP(info_ctr
);
2932 info_ctr
->level
= level
;
2937 struct srvsvc_NetShareCtr1
*ctr1
;
2939 ctr1
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr1
);
2940 W_ERROR_HAVE_NO_MEMORY(ctr1
);
2943 ctr1
->array
= info
.info1
;
2945 info_ctr
->ctr
.ctr1
= ctr1
;
2949 struct srvsvc_NetShareCtr2
*ctr2
;
2951 ctr2
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr2
);
2952 W_ERROR_HAVE_NO_MEMORY(ctr2
);
2955 ctr2
->array
= info
.info2
;
2957 info_ctr
->ctr
.ctr2
= ctr2
;
2961 struct srvsvc_NetShareCtr502
*ctr502
;
2963 ctr502
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr502
);
2964 W_ERROR_HAVE_NO_MEMORY(ctr502
);
2967 ctr502
->array
= info
.info502
;
2969 info_ctr
->ctr
.ctr502
= ctr502
;
2977 * 'net rpc share list' entrypoint.
2978 * @param argc Standard main() style argc.
2979 * @param argv Standard main() style argv. Initial components are already
2982 static int rpc_share_list(struct net_context
*c
, int argc
, const char **argv
)
2984 NET_API_STATUS status
;
2985 struct SHARE_INFO_1
*i1
= NULL
;
2986 uint32_t entries_read
= 0;
2987 uint32_t total_entries
= 0;
2988 uint32_t resume_handle
= 0;
2989 uint32_t i
, level
= 1;
2991 if (c
->display_usage
) {
2993 "net rpc share list\n"
2994 " List shares on remote server\n");
2998 status
= NetShareEnum(c
->opt_host
,
3009 /* Display results */
3011 if (c
->opt_long_list_entries
) {
3013 "\nEnumerating shared resources (exports) on remote server:\n\n"
3014 "\nShare name Type Description\n"
3015 "---------- ---- -----------\n");
3017 for (i
= 0; i
< entries_read
; i
++)
3018 display_share_info_1(c
, &i1
[i
]);
3023 static bool check_share_availability(struct cli_state
*cli
, const char *netname
)
3025 if (!cli_send_tconX(cli
, netname
, "A:", "", 0)) {
3026 d_printf("skipping [%s]: not a file share.\n", netname
);
3036 static bool check_share_sanity(struct net_context
*c
, struct cli_state
*cli
,
3037 const char *netname
, uint32 type
)
3039 /* only support disk shares */
3040 if (! ( type
== STYPE_DISKTREE
|| type
== (STYPE_DISKTREE
| STYPE_HIDDEN
)) ) {
3041 printf("share [%s] is not a diskshare (type: %x)\n", netname
, type
);
3045 /* skip builtin shares */
3046 /* FIXME: should print$ be added too ? */
3047 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$") ||
3048 strequal(netname
,"global"))
3051 if (c
->opt_exclude
&& in_list(netname
, c
->opt_exclude
, false)) {
3052 printf("excluding [%s]\n", netname
);
3056 return check_share_availability(cli
, netname
);
3060 * Migrate shares from a remote RPC server to the local RPC server.
3062 * All parameters are provided by the run_rpc_command function, except for
3063 * argc, argv which are passed through.
3065 * @param domain_sid The domain sid acquired from the remote server.
3066 * @param cli A cli_state connected to the server.
3067 * @param mem_ctx Talloc context, destroyed on completion of the function.
3068 * @param argc Standard main() style argc.
3069 * @param argv Standard main() style argv. Initial components are already
3072 * @return Normal NTSTATUS return.
3075 static NTSTATUS
rpc_share_migrate_shares_internals(struct net_context
*c
,
3076 const DOM_SID
*domain_sid
,
3077 const char *domain_name
,
3078 struct cli_state
*cli
,
3079 struct rpc_pipe_client
*pipe_hnd
,
3080 TALLOC_CTX
*mem_ctx
,
3085 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3086 struct srvsvc_NetShareInfoCtr ctr_src
;
3088 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3089 struct cli_state
*cli_dst
= NULL
;
3090 uint32 level
= 502; /* includes secdesc */
3091 uint32_t parm_error
= 0;
3093 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3095 if (!W_ERROR_IS_OK(result
))
3098 /* connect destination PI_SRVSVC */
3099 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3100 &ndr_table_srvsvc
.syntax_id
);
3101 if (!NT_STATUS_IS_OK(nt_status
))
3105 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3107 union srvsvc_NetShareInfo info
;
3108 struct srvsvc_NetShareInfo502 info502
=
3109 ctr_src
.ctr
.ctr502
->array
[i
];
3111 /* reset error-code */
3112 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3114 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3117 /* finally add the share on the dst server */
3119 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3120 info502
.name
, info502
.path
, info502
.comment
);
3122 info
.info502
= &info502
;
3124 nt_status
= rpccli_srvsvc_NetShareAdd(srvsvc_pipe
, mem_ctx
,
3125 srvsvc_pipe
->desthost
,
3131 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ALREADY_EXISTS
)) {
3132 printf(" [%s] does already exist\n",
3137 if (!NT_STATUS_IS_OK(nt_status
) || !W_ERROR_IS_OK(result
)) {
3138 printf("cannot add share: %s\n", dos_errstr(result
));
3144 nt_status
= NT_STATUS_OK
;
3148 cli_shutdown(cli_dst
);
3156 * Migrate shares from a RPC server to another.
3158 * @param argc Standard main() style argc.
3159 * @param argv Standard main() style argv. Initial components are already
3162 * @return A shell status integer (0 for success).
3164 static int rpc_share_migrate_shares(struct net_context
*c
, int argc
,
3167 if (c
->display_usage
) {
3169 "net rpc share migrate shares\n"
3170 " Migrate shares to local server\n");
3175 printf("no server to migrate\n");
3179 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3180 rpc_share_migrate_shares_internals
,
3187 * @param f file_info
3188 * @param mask current search mask
3189 * @param state arg-pointer
3192 static void copy_fn(const char *mnt
, file_info
*f
,
3193 const char *mask
, void *state
)
3195 static NTSTATUS nt_status
;
3196 static struct copy_clistate
*local_state
;
3197 static fstring filename
, new_mask
;
3200 struct net_context
*c
;
3202 local_state
= (struct copy_clistate
*)state
;
3203 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3207 if (strequal(f
->name
, ".") || strequal(f
->name
, ".."))
3210 DEBUG(3,("got mask: %s, name: %s\n", mask
, f
->name
));
3213 if (f
->mode
& aDIR
) {
3215 DEBUG(3,("got dir: %s\n", f
->name
));
3217 fstrcpy(dir
, local_state
->cwd
);
3219 fstrcat(dir
, f
->name
);
3221 switch (net_mode_share
)
3223 case NET_MODE_SHARE_MIGRATE
:
3224 /* create that directory */
3225 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3226 local_state
->cli_share_src
,
3227 local_state
->cli_share_dst
,
3229 c
->opt_acls
? true : false,
3230 c
->opt_attrs
? true : false,
3231 c
->opt_timestamps
? true:false,
3235 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3239 if (!NT_STATUS_IS_OK(nt_status
))
3240 printf("could not handle dir %s: %s\n",
3241 dir
, nt_errstr(nt_status
));
3243 /* search below that directory */
3244 fstrcpy(new_mask
, dir
);
3245 fstrcat(new_mask
, "\\*");
3247 old_dir
= local_state
->cwd
;
3248 local_state
->cwd
= dir
;
3249 if (!sync_files(local_state
, new_mask
))
3250 printf("could not handle files\n");
3251 local_state
->cwd
= old_dir
;
3258 fstrcpy(filename
, local_state
->cwd
);
3259 fstrcat(filename
, "\\");
3260 fstrcat(filename
, f
->name
);
3262 DEBUG(3,("got file: %s\n", filename
));
3264 switch (net_mode_share
)
3266 case NET_MODE_SHARE_MIGRATE
:
3267 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3268 local_state
->cli_share_src
,
3269 local_state
->cli_share_dst
,
3271 c
->opt_acls
? true : false,
3272 c
->opt_attrs
? true : false,
3273 c
->opt_timestamps
? true: false,
3277 d_fprintf(stderr
, "Unsupported file mode %d\n", net_mode_share
);
3281 if (!NT_STATUS_IS_OK(nt_status
))
3282 printf("could not handle file %s: %s\n",
3283 filename
, nt_errstr(nt_status
));
3288 * sync files, can be called recursivly to list files
3289 * and then call copy_fn for each file
3291 * @param cp_clistate pointer to the copy_clistate we work with
3292 * @param mask the current search mask
3294 * @return Boolean result
3296 static bool sync_files(struct copy_clistate
*cp_clistate
, const char *mask
)
3298 struct cli_state
*targetcli
;
3299 char *targetpath
= NULL
;
3301 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
3303 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate
->cli_share_src
,
3304 mask
, &targetcli
, &targetpath
) ) {
3305 d_fprintf(stderr
, "cli_resolve_path %s failed with error: %s\n",
3306 mask
, cli_errstr(cp_clistate
->cli_share_src
));
3310 if (cli_list(targetcli
, targetpath
, cp_clistate
->attribute
, copy_fn
, cp_clistate
) == -1) {
3311 d_fprintf(stderr
, "listing %s failed with error: %s\n",
3312 mask
, cli_errstr(targetcli
));
3321 * Set the top level directory permissions before we do any further copies.
3322 * Should set up ACL inheritance.
3325 bool copy_top_level_perms(struct net_context
*c
,
3326 struct copy_clistate
*cp_clistate
,
3327 const char *sharename
)
3329 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3331 switch (net_mode_share
) {
3332 case NET_MODE_SHARE_MIGRATE
:
3333 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename
));
3334 nt_status
= net_copy_fileattr(c
,
3335 cp_clistate
->mem_ctx
,
3336 cp_clistate
->cli_share_src
,
3337 cp_clistate
->cli_share_dst
,
3339 c
->opt_acls
? true : false,
3340 c
->opt_attrs
? true : false,
3341 c
->opt_timestamps
? true: false,
3345 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3349 if (!NT_STATUS_IS_OK(nt_status
)) {
3350 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3351 sharename
, nt_errstr(nt_status
));
3359 * Sync all files inside a remote share to another share (over smb).
3361 * All parameters are provided by the run_rpc_command function, except for
3362 * argc, argv which are passed through.
3364 * @param domain_sid The domain sid acquired from the remote server.
3365 * @param cli A cli_state connected to the server.
3366 * @param mem_ctx Talloc context, destroyed on completion of the function.
3367 * @param argc Standard main() style argc.
3368 * @param argv Standard main() style argv. Initial components are already
3371 * @return Normal NTSTATUS return.
3374 static NTSTATUS
rpc_share_migrate_files_internals(struct net_context
*c
,
3375 const DOM_SID
*domain_sid
,
3376 const char *domain_name
,
3377 struct cli_state
*cli
,
3378 struct rpc_pipe_client
*pipe_hnd
,
3379 TALLOC_CTX
*mem_ctx
,
3384 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3385 struct srvsvc_NetShareInfoCtr ctr_src
;
3388 struct copy_clistate cp_clistate
;
3389 bool got_src_share
= false;
3390 bool got_dst_share
= false;
3391 const char *mask
= "\\*";
3394 dst
= SMB_STRDUP(c
->opt_destination
?c
->opt_destination
:"127.0.0.1");
3396 nt_status
= NT_STATUS_NO_MEMORY
;
3400 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3403 if (!W_ERROR_IS_OK(result
))
3406 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3408 struct srvsvc_NetShareInfo502 info502
=
3409 ctr_src
.ctr
.ctr502
->array
[i
];
3411 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3414 /* one might not want to mirror whole discs :) */
3415 if (strequal(info502
.name
, "print$") || info502
.name
[1] == '$') {
3416 d_printf("skipping [%s]: builtin/hidden share\n", info502
.name
);
3420 switch (net_mode_share
)
3422 case NET_MODE_SHARE_MIGRATE
:
3426 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3429 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3431 c
->opt_acls
? "including" : "without",
3432 c
->opt_attrs
? "including" : "without",
3433 c
->opt_timestamps
? "(preserving timestamps)" : "");
3435 cp_clistate
.mem_ctx
= mem_ctx
;
3436 cp_clistate
.cli_share_src
= NULL
;
3437 cp_clistate
.cli_share_dst
= NULL
;
3438 cp_clistate
.cwd
= NULL
;
3439 cp_clistate
.attribute
= aSYSTEM
| aHIDDEN
| aDIR
;
3442 /* open share source */
3443 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_src
,
3444 &cli
->dest_ss
, cli
->desthost
,
3445 info502
.name
, "A:");
3446 if (!NT_STATUS_IS_OK(nt_status
))
3449 got_src_share
= true;
3451 if (net_mode_share
== NET_MODE_SHARE_MIGRATE
) {
3452 /* open share destination */
3453 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_dst
,
3454 NULL
, dst
, info502
.name
, "A:");
3455 if (!NT_STATUS_IS_OK(nt_status
))
3458 got_dst_share
= true;
3461 if (!copy_top_level_perms(c
, &cp_clistate
, info502
.name
)) {
3462 d_fprintf(stderr
, "Could not handle the top level directory permissions for the share: %s\n", info502
.name
);
3463 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3467 if (!sync_files(&cp_clistate
, mask
)) {
3468 d_fprintf(stderr
, "could not handle files for share: %s\n", info502
.name
);
3469 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3474 nt_status
= NT_STATUS_OK
;
3479 cli_shutdown(cp_clistate
.cli_share_src
);
3482 cli_shutdown(cp_clistate
.cli_share_dst
);
3489 static int rpc_share_migrate_files(struct net_context
*c
, int argc
, const char **argv
)
3491 if (c
->display_usage
) {
3493 "net share migrate files\n"
3494 " Migrate files to local server\n");
3499 d_printf("no server to migrate\n");
3503 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3504 rpc_share_migrate_files_internals
,
3509 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3511 * All parameters are provided by the run_rpc_command function, except for
3512 * argc, argv which are passed through.
3514 * @param domain_sid The domain sid acquired from the remote server.
3515 * @param cli A cli_state connected to the server.
3516 * @param mem_ctx Talloc context, destroyed on completion of the function.
3517 * @param argc Standard main() style argc.
3518 * @param argv Standard main() style argv. Initial components are already
3521 * @return Normal NTSTATUS return.
3524 static NTSTATUS
rpc_share_migrate_security_internals(struct net_context
*c
,
3525 const DOM_SID
*domain_sid
,
3526 const char *domain_name
,
3527 struct cli_state
*cli
,
3528 struct rpc_pipe_client
*pipe_hnd
,
3529 TALLOC_CTX
*mem_ctx
,
3534 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3535 struct srvsvc_NetShareInfoCtr ctr_src
;
3536 union srvsvc_NetShareInfo info
;
3538 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3539 struct cli_state
*cli_dst
= NULL
;
3540 uint32 level
= 502; /* includes secdesc */
3541 uint32_t parm_error
= 0;
3543 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3546 if (!W_ERROR_IS_OK(result
))
3549 /* connect destination PI_SRVSVC */
3550 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3551 &ndr_table_srvsvc
.syntax_id
);
3552 if (!NT_STATUS_IS_OK(nt_status
))
3556 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3558 struct srvsvc_NetShareInfo502 info502
=
3559 ctr_src
.ctr
.ctr502
->array
[i
];
3561 /* reset error-code */
3562 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3564 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3567 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3568 info502
.name
, info502
.path
, info502
.comment
);
3571 display_sec_desc(info502
.sd_buf
.sd
);
3573 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3574 info
.info502
= &info502
;
3576 /* finally modify the share on the dst server */
3577 nt_status
= rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe
, mem_ctx
,
3578 srvsvc_pipe
->desthost
,
3584 if (!NT_STATUS_IS_OK(nt_status
) || !W_ERROR_IS_OK(result
)) {
3585 printf("cannot set share-acl: %s\n", dos_errstr(result
));
3591 nt_status
= NT_STATUS_OK
;
3595 cli_shutdown(cli_dst
);
3603 * Migrate share-acls from a RPC server to another.
3605 * @param argc Standard main() style argc.
3606 * @param argv Standard main() style argv. Initial components are already
3609 * @return A shell status integer (0 for success).
3611 static int rpc_share_migrate_security(struct net_context
*c
, int argc
,
3614 if (c
->display_usage
) {
3616 "net rpc share migrate security\n"
3617 " Migrate share-acls to local server\n");
3622 d_printf("no server to migrate\n");
3626 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3627 rpc_share_migrate_security_internals
,
3632 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3633 * from one server to another.
3635 * @param argc Standard main() style argc.
3636 * @param argv Standard main() style argv. Initial components are already
3639 * @return A shell status integer (0 for success).
3642 static int rpc_share_migrate_all(struct net_context
*c
, int argc
,
3647 if (c
->display_usage
) {
3649 "net rpc share migrate all\n"
3650 " Migrates shares including all share settings\n");
3655 d_printf("no server to migrate\n");
3659 /* order is important. we don't want to be locked out by the share-acl
3660 * before copying files - gd */
3662 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3663 rpc_share_migrate_shares_internals
, argc
, argv
);
3667 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3668 rpc_share_migrate_files_internals
, argc
, argv
);
3672 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3673 rpc_share_migrate_security_internals
, argc
,
3679 * 'net rpc share migrate' entrypoint.
3680 * @param argc Standard main() style argc.
3681 * @param argv Standard main() style argv. Initial components are already
3684 static int rpc_share_migrate(struct net_context
*c
, int argc
, const char **argv
)
3687 struct functable func
[] = {
3690 rpc_share_migrate_all
,
3692 "Migrate shares from remote to local server",
3693 "net rpc share migrate all\n"
3694 " Migrate shares from remote to local server"
3698 rpc_share_migrate_files
,
3700 "Migrate files from remote to local server",
3701 "net rpc share migrate files\n"
3702 " Migrate files from remote to local server"
3706 rpc_share_migrate_security
,
3708 "Migrate share-ACLs from remote to local server",
3709 "net rpc share migrate security\n"
3710 " Migrate share-ACLs from remote to local server"
3714 rpc_share_migrate_shares
,
3716 "Migrate shares from remote to local server",
3717 "net rpc share migrate shares\n"
3718 " Migrate shares from remote to local server"
3720 {NULL
, NULL
, 0, NULL
, NULL
}
3723 net_mode_share
= NET_MODE_SHARE_MIGRATE
;
3725 return net_run_function(c
, argc
, argv
, "net rpc share migrate", func
);
3734 static int num_server_aliases
;
3735 static struct full_alias
*server_aliases
;
3738 * Add an alias to the static list.
3740 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
3742 if (server_aliases
== NULL
)
3743 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
3745 server_aliases
[num_server_aliases
] = *alias
;
3746 num_server_aliases
+= 1;
3750 * For a specific domain on the server, fetch all the aliases
3751 * and their members. Add all of them to the server_aliases.
3754 static NTSTATUS
rpc_fetch_domain_aliases(struct rpc_pipe_client
*pipe_hnd
,
3755 TALLOC_CTX
*mem_ctx
,
3756 POLICY_HND
*connect_pol
,
3757 const DOM_SID
*domain_sid
)
3759 uint32 start_idx
, max_entries
, num_entries
, i
;
3760 struct samr_SamArray
*groups
= NULL
;
3762 POLICY_HND domain_pol
;
3764 /* Get domain policy handle */
3766 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
3768 MAXIMUM_ALLOWED_ACCESS
,
3769 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
3771 if (!NT_STATUS_IS_OK(result
))
3778 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
3784 for (i
= 0; i
< num_entries
; i
++) {
3786 POLICY_HND alias_pol
;
3787 struct full_alias alias
;
3788 struct lsa_SidArray sid_array
;
3791 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
3793 MAXIMUM_ALLOWED_ACCESS
,
3794 groups
->entries
[i
].idx
,
3796 if (!NT_STATUS_IS_OK(result
))
3799 result
= rpccli_samr_GetMembersInAlias(pipe_hnd
, mem_ctx
,
3802 if (!NT_STATUS_IS_OK(result
))
3805 alias
.num_members
= sid_array
.num_sids
;
3807 result
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &alias_pol
);
3808 if (!NT_STATUS_IS_OK(result
))
3811 alias
.members
= NULL
;
3813 if (alias
.num_members
> 0) {
3814 alias
.members
= SMB_MALLOC_ARRAY(DOM_SID
, alias
.num_members
);
3816 for (j
= 0; j
< alias
.num_members
; j
++)
3817 sid_copy(&alias
.members
[j
],
3818 sid_array
.sids
[j
].sid
);
3821 sid_copy(&alias
.sid
, domain_sid
);
3822 sid_append_rid(&alias
.sid
, groups
->entries
[i
].idx
);
3824 push_alias(mem_ctx
, &alias
);
3826 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
3828 result
= NT_STATUS_OK
;
3831 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
3837 * Dump server_aliases as names for debugging purposes.
3840 static NTSTATUS
rpc_aliaslist_dump(struct net_context
*c
,
3841 const DOM_SID
*domain_sid
,
3842 const char *domain_name
,
3843 struct cli_state
*cli
,
3844 struct rpc_pipe_client
*pipe_hnd
,
3845 TALLOC_CTX
*mem_ctx
,
3853 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
3854 SEC_RIGHTS_MAXIMUM_ALLOWED
,
3856 if (!NT_STATUS_IS_OK(result
))
3859 for (i
=0; i
<num_server_aliases
; i
++) {
3862 enum lsa_SidType
*types
;
3865 struct full_alias
*alias
= &server_aliases
[i
];
3867 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
3869 &domains
, &names
, &types
);
3870 if (!NT_STATUS_IS_OK(result
))
3873 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
3875 if (alias
->num_members
== 0) {
3880 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
,
3883 &domains
, &names
, &types
);
3885 if (!NT_STATUS_IS_OK(result
) &&
3886 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
3889 for (j
=0; j
<alias
->num_members
; j
++)
3890 DEBUG(1, ("%s\\%s (%d); ",
3891 domains
[j
] ? domains
[j
] : "*unknown*",
3892 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
3896 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
3898 return NT_STATUS_OK
;
3902 * Fetch a list of all server aliases and their members into
3906 static NTSTATUS
rpc_aliaslist_internals(struct net_context
*c
,
3907 const DOM_SID
*domain_sid
,
3908 const char *domain_name
,
3909 struct cli_state
*cli
,
3910 struct rpc_pipe_client
*pipe_hnd
,
3911 TALLOC_CTX
*mem_ctx
,
3916 POLICY_HND connect_pol
;
3918 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
3920 MAXIMUM_ALLOWED_ACCESS
,
3923 if (!NT_STATUS_IS_OK(result
))
3926 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
3927 &global_sid_Builtin
);
3929 if (!NT_STATUS_IS_OK(result
))
3932 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
3935 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
3940 static void init_user_token(NT_USER_TOKEN
*token
, DOM_SID
*user_sid
)
3942 token
->num_sids
= 4;
3944 if (!(token
->user_sids
= SMB_MALLOC_ARRAY(DOM_SID
, 4))) {
3945 d_fprintf(stderr
, "malloc failed\n");
3946 token
->num_sids
= 0;
3950 token
->user_sids
[0] = *user_sid
;
3951 sid_copy(&token
->user_sids
[1], &global_sid_World
);
3952 sid_copy(&token
->user_sids
[2], &global_sid_Network
);
3953 sid_copy(&token
->user_sids
[3], &global_sid_Authenticated_Users
);
3956 static void free_user_token(NT_USER_TOKEN
*token
)
3958 SAFE_FREE(token
->user_sids
);
3961 static void add_sid_to_token(NT_USER_TOKEN
*token
, DOM_SID
*sid
)
3963 if (is_sid_in_token(token
, sid
))
3966 token
->user_sids
= SMB_REALLOC_ARRAY(token
->user_sids
, DOM_SID
, token
->num_sids
+1);
3967 if (!token
->user_sids
) {
3971 sid_copy(&token
->user_sids
[token
->num_sids
], sid
);
3973 token
->num_sids
+= 1;
3978 NT_USER_TOKEN token
;
3981 static void dump_user_token(struct user_token
*token
)
3985 d_printf("%s\n", token
->name
);
3987 for (i
=0; i
<token
->token
.num_sids
; i
++) {
3988 d_printf(" %s\n", sid_string_tos(&token
->token
.user_sids
[i
]));
3992 static bool is_alias_member(DOM_SID
*sid
, struct full_alias
*alias
)
3996 for (i
=0; i
<alias
->num_members
; i
++) {
3997 if (sid_compare(sid
, &alias
->members
[i
]) == 0)
4004 static void collect_sid_memberships(NT_USER_TOKEN
*token
, DOM_SID sid
)
4008 for (i
=0; i
<num_server_aliases
; i
++) {
4009 if (is_alias_member(&sid
, &server_aliases
[i
]))
4010 add_sid_to_token(token
, &server_aliases
[i
].sid
);
4015 * We got a user token with all the SIDs we can know about without asking the
4016 * server directly. These are the user and domain group sids. All of these can
4017 * be members of aliases. So scan the list of aliases for each of the SIDs and
4018 * add them to the token.
4021 static void collect_alias_memberships(NT_USER_TOKEN
*token
)
4023 int num_global_sids
= token
->num_sids
;
4026 for (i
=0; i
<num_global_sids
; i
++) {
4027 collect_sid_memberships(token
, token
->user_sids
[i
]);
4031 static bool get_user_sids(const char *domain
, const char *user
, NT_USER_TOKEN
*token
)
4033 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4034 enum wbcSidType type
;
4036 struct wbcDomainSid wsid
;
4037 char *sid_str
= NULL
;
4039 uint32_t num_groups
;
4040 gid_t
*groups
= NULL
;
4043 fstr_sprintf(full_name
, "%s%c%s",
4044 domain
, *lp_winbind_separator(), user
);
4046 /* First let's find out the user sid */
4048 wbc_status
= wbcLookupName(domain
, user
, &wsid
, &type
);
4050 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4051 DEBUG(1, ("winbind could not find %s: %s\n",
4052 full_name
, wbcErrorString(wbc_status
)));
4056 wbc_status
= wbcSidToString(&wsid
, &sid_str
);
4057 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4061 if (type
!= SID_NAME_USER
) {
4062 wbcFreeMemory(sid_str
);
4063 DEBUG(1, ("%s is not a user\n", full_name
));
4067 if (!string_to_sid(&user_sid
, sid_str
)) {
4068 DEBUG(1,("Could not convert sid %s from string\n", sid_str
));
4072 wbcFreeMemory(sid_str
);
4075 init_user_token(token
, &user_sid
);
4077 /* And now the groups winbind knows about */
4079 wbc_status
= wbcGetGroups(full_name
, &num_groups
, &groups
);
4080 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4081 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4082 full_name
, wbcErrorString(wbc_status
)));
4086 for (i
= 0; i
< num_groups
; i
++) {
4087 gid_t gid
= groups
[i
];
4090 wbc_status
= wbcGidToSid(gid
, &wsid
);
4091 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4092 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4093 gid
, wbcErrorString(wbc_status
)));
4094 wbcFreeMemory(groups
);
4098 wbc_status
= wbcSidToString(&wsid
, &sid_str
);
4099 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4100 wbcFreeMemory(groups
);
4104 DEBUG(3, (" %s\n", sid_str
));
4106 string_to_sid(&sid
, sid_str
);
4107 wbcFreeMemory(sid_str
);
4110 add_sid_to_token(token
, &sid
);
4112 wbcFreeMemory(groups
);
4118 * Get a list of all user tokens we want to look at
4121 static bool get_user_tokens(struct net_context
*c
, int *num_tokens
,
4122 struct user_token
**user_tokens
)
4124 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4125 uint32_t i
, num_users
;
4127 struct user_token
*result
;
4128 TALLOC_CTX
*frame
= NULL
;
4130 if (lp_winbind_use_default_domain() &&
4131 (c
->opt_target_workgroup
== NULL
)) {
4132 d_fprintf(stderr
, "winbind use default domain = yes set, "
4133 "please specify a workgroup\n");
4137 /* Send request to winbind daemon */
4139 wbc_status
= wbcListUsers(NULL
, &num_users
, &users
);
4140 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4141 DEBUG(1, ("winbind could not list users: %s\n",
4142 wbcErrorString(wbc_status
)));
4146 result
= SMB_MALLOC_ARRAY(struct user_token
, num_users
);
4148 if (result
== NULL
) {
4149 DEBUG(1, ("Could not malloc sid array\n"));
4150 wbcFreeMemory(users
);
4154 frame
= talloc_stackframe();
4155 for (i
=0; i
< num_users
; i
++) {
4156 fstring domain
, user
;
4159 fstrcpy(result
[i
].name
, users
[i
]);
4161 p
= strchr(users
[i
], *lp_winbind_separator());
4163 DEBUG(3, ("%s\n", users
[i
]));
4166 fstrcpy(domain
, c
->opt_target_workgroup
);
4167 fstrcpy(user
, users
[i
]);
4170 fstrcpy(domain
, users
[i
]);
4175 get_user_sids(domain
, user
, &(result
[i
].token
));
4179 wbcFreeMemory(users
);
4181 *num_tokens
= num_users
;
4182 *user_tokens
= result
;
4187 static bool get_user_tokens_from_file(FILE *f
,
4189 struct user_token
**tokens
)
4191 struct user_token
*token
= NULL
;
4196 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4200 if (line
[strlen(line
)-1] == '\n')
4201 line
[strlen(line
)-1] = '\0';
4203 if (line
[0] == ' ') {
4207 if(!string_to_sid(&sid
, &line
[1])) {
4208 DEBUG(1,("get_user_tokens_from_file: Could "
4209 "not convert sid %s \n",&line
[1]));
4213 if (token
== NULL
) {
4214 DEBUG(0, ("File does not begin with username"));
4218 add_sid_to_token(&token
->token
, &sid
);
4222 /* And a new user... */
4225 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4226 if (*tokens
== NULL
) {
4227 DEBUG(0, ("Could not realloc tokens\n"));
4231 token
= &((*tokens
)[*num_tokens
-1]);
4233 fstrcpy(token
->name
, line
);
4234 token
->token
.num_sids
= 0;
4235 token
->token
.user_sids
= NULL
;
4244 * Show the list of all users that have access to a share
4247 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4248 TALLOC_CTX
*mem_ctx
,
4249 const char *netname
,
4251 struct user_token
*tokens
)
4254 SEC_DESC
*share_sd
= NULL
;
4255 SEC_DESC
*root_sd
= NULL
;
4256 struct cli_state
*cli
= rpc_pipe_np_smb_conn(pipe_hnd
);
4258 union srvsvc_NetShareInfo info
;
4263 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
4270 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4271 DEBUG(1, ("Coult not query secdesc for share %s\n",
4276 share_sd
= info
.info502
->sd_buf
.sd
;
4277 if (share_sd
== NULL
) {
4278 DEBUG(1, ("Got no secdesc for share %s\n",
4284 if (!cli_send_tconX(cli
, netname
, "A:", "", 0)) {
4288 fnum
= cli_nt_create(cli
, "\\", READ_CONTROL_ACCESS
);
4291 root_sd
= cli_query_secdesc(cli
, fnum
, mem_ctx
);
4294 for (i
=0; i
<num_tokens
; i
++) {
4297 if (share_sd
!= NULL
) {
4298 status
= se_access_check(share_sd
, &tokens
[i
].token
,
4301 if (!NT_STATUS_IS_OK(status
)) {
4302 DEBUG(1, ("Could not check share_sd for "
4309 if (root_sd
== NULL
) {
4310 d_printf(" %s\n", tokens
[i
].name
);
4314 status
= se_access_check(root_sd
, &tokens
[i
].token
,
4316 if (!NT_STATUS_IS_OK(status
)) {
4317 DEBUG(1, ("Could not check root_sd for user %s\n",
4321 d_printf(" %s\n", tokens
[i
].name
);
4325 cli_close(cli
, fnum
);
4337 static void collect_share(const char *name
, uint32 m
,
4338 const char *comment
, void *state
)
4340 struct share_list
*share_list
= (struct share_list
*)state
;
4342 if (m
!= STYPE_DISKTREE
)
4345 share_list
->num_shares
+= 1;
4346 share_list
->shares
= SMB_REALLOC_ARRAY(share_list
->shares
, char *, share_list
->num_shares
);
4347 if (!share_list
->shares
) {
4348 share_list
->num_shares
= 0;
4351 share_list
->shares
[share_list
->num_shares
-1] = SMB_STRDUP(name
);
4355 * List shares on a remote RPC server, including the security descriptors.
4357 * All parameters are provided by the run_rpc_command function, except for
4358 * argc, argv which are passed through.
4360 * @param domain_sid The domain sid acquired from the remote server.
4361 * @param cli A cli_state connected to the server.
4362 * @param mem_ctx Talloc context, destroyed on completion of the function.
4363 * @param argc Standard main() style argc.
4364 * @param argv Standard main() style argv. Initial components are already
4367 * @return Normal NTSTATUS return.
4370 static NTSTATUS
rpc_share_allowedusers_internals(struct net_context
*c
,
4371 const DOM_SID
*domain_sid
,
4372 const char *domain_name
,
4373 struct cli_state
*cli
,
4374 struct rpc_pipe_client
*pipe_hnd
,
4375 TALLOC_CTX
*mem_ctx
,
4385 struct user_token
*tokens
= NULL
;
4388 struct share_list share_list
;
4393 f
= fopen(argv
[0], "r");
4397 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4398 return NT_STATUS_UNSUCCESSFUL
;
4401 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
4407 DEBUG(0, ("Could not read users from file\n"));
4408 return NT_STATUS_UNSUCCESSFUL
;
4411 for (i
=0; i
<num_tokens
; i
++)
4412 collect_alias_memberships(&tokens
[i
].token
);
4414 init_enum_hnd(&hnd
, 0);
4416 share_list
.num_shares
= 0;
4417 share_list
.shares
= NULL
;
4419 ret
= cli_RNetShareEnum(cli
, collect_share
, &share_list
);
4422 DEBUG(0, ("Error returning browse list: %s\n",
4427 for (i
= 0; i
< share_list
.num_shares
; i
++) {
4428 char *netname
= share_list
.shares
[i
];
4430 if (netname
[strlen(netname
)-1] == '$')
4433 d_printf("%s\n", netname
);
4435 show_userlist(pipe_hnd
, mem_ctx
, netname
,
4436 num_tokens
, tokens
);
4439 for (i
=0; i
<num_tokens
; i
++) {
4440 free_user_token(&tokens
[i
].token
);
4443 SAFE_FREE(share_list
.shares
);
4445 return NT_STATUS_OK
;
4448 static int rpc_share_allowedusers(struct net_context
*c
, int argc
,
4453 if (c
->display_usage
) {
4455 "net rpc share allowedusers\n"
4456 " List allowed users\n");
4460 result
= run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
4461 rpc_aliaslist_internals
,
4466 result
= run_rpc_command(c
, NULL
, &ndr_table_lsarpc
.syntax_id
, 0,
4472 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4473 rpc_share_allowedusers_internals
,
4477 int net_usersidlist(struct net_context
*c
, int argc
, const char **argv
)
4480 struct user_token
*tokens
= NULL
;
4484 net_usersidlist_usage(c
, argc
, argv
);
4488 if (!get_user_tokens(c
, &num_tokens
, &tokens
)) {
4489 DEBUG(0, ("Could not get the user/sid list\n"));
4493 for (i
=0; i
<num_tokens
; i
++) {
4494 dump_user_token(&tokens
[i
]);
4495 free_user_token(&tokens
[i
].token
);
4502 int net_usersidlist_usage(struct net_context
*c
, int argc
, const char **argv
)
4504 d_printf("net usersidlist\n"
4505 "\tprints out a list of all users the running winbind knows\n"
4506 "\tabout, together with all their SIDs. This is used as\n"
4507 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4509 net_common_flags_usage(c
, argc
, argv
);
4514 * 'net rpc share' entrypoint.
4515 * @param argc Standard main() style argc.
4516 * @param argv Standard main() style argv. Initial components are already
4520 int net_rpc_share(struct net_context
*c
, int argc
, const char **argv
)
4522 NET_API_STATUS status
;
4524 struct functable func
[] = {
4530 "net rpc share add\n"
4538 "net rpc share delete\n"
4543 rpc_share_allowedusers
,
4545 "Modify allowed users",
4546 "net rpc share allowedusers\n"
4547 " Modify allowed users"
4553 "Migrate share to local server",
4554 "net rpc share migrate\n"
4555 " Migrate share to local server"
4562 "net rpc share list\n"
4565 {NULL
, NULL
, 0, NULL
, NULL
}
4568 status
= libnetapi_init(&c
->netapi_ctx
);
4572 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
4573 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
4574 if (c
->opt_kerberos
) {
4575 libnetapi_set_use_kerberos(c
->netapi_ctx
);
4579 if (c
->display_usage
) {
4583 " Alias for net rpc share list\n");
4584 net_display_usage_from_functable(func
);
4588 return rpc_share_list(c
, argc
, argv
);
4591 return net_run_function(c
, argc
, argv
, "net rpc share", func
);
4594 static NTSTATUS
rpc_sh_share_list(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
)
4601 return werror_to_ntstatus(W_ERROR(rpc_share_list(c
, argc
, argv
)));
4604 static NTSTATUS
rpc_sh_share_add(struct net_context
*c
,
4605 TALLOC_CTX
*mem_ctx
,
4606 struct rpc_sh_ctx
*ctx
,
4607 struct rpc_pipe_client
*pipe_hnd
,
4608 int argc
, const char **argv
)
4610 NET_API_STATUS status
;
4611 uint32_t parm_err
= 0;
4612 struct SHARE_INFO_2 i2
;
4614 if ((argc
< 2) || (argc
> 3)) {
4615 d_fprintf(stderr
, "usage: %s <share> <path> [comment]\n",
4617 return NT_STATUS_INVALID_PARAMETER
;
4620 i2
.shi2_netname
= argv
[0];
4621 i2
.shi2_type
= STYPE_DISKTREE
;
4622 i2
.shi2_remark
= (argc
== 3) ? argv
[2] : "";
4623 i2
.shi2_permissions
= 0;
4624 i2
.shi2_max_uses
= 0;
4625 i2
.shi2_current_uses
= 0;
4626 i2
.shi2_path
= argv
[1];
4627 i2
.shi2_passwd
= NULL
;
4629 status
= NetShareAdd(pipe_hnd
->desthost
,
4634 return werror_to_ntstatus(W_ERROR(status
));
4637 static NTSTATUS
rpc_sh_share_delete(struct net_context
*c
,
4638 TALLOC_CTX
*mem_ctx
,
4639 struct rpc_sh_ctx
*ctx
,
4640 struct rpc_pipe_client
*pipe_hnd
,
4641 int argc
, const char **argv
)
4644 d_fprintf(stderr
, "usage: %s <share>\n", ctx
->whoami
);
4645 return NT_STATUS_INVALID_PARAMETER
;
4648 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd
->desthost
, argv
[0], 0)));
4651 static NTSTATUS
rpc_sh_share_info(struct net_context
*c
,
4652 TALLOC_CTX
*mem_ctx
,
4653 struct rpc_sh_ctx
*ctx
,
4654 struct rpc_pipe_client
*pipe_hnd
,
4655 int argc
, const char **argv
)
4657 union srvsvc_NetShareInfo info
;
4662 d_fprintf(stderr
, "usage: %s <share>\n", ctx
->whoami
);
4663 return NT_STATUS_INVALID_PARAMETER
;
4666 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
4672 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4676 d_printf("Name: %s\n", info
.info2
->name
);
4677 d_printf("Comment: %s\n", info
.info2
->comment
);
4678 d_printf("Path: %s\n", info
.info2
->path
);
4679 d_printf("Password: %s\n", info
.info2
->password
);
4682 return werror_to_ntstatus(result
);
4685 struct rpc_sh_cmd
*net_rpc_share_cmds(struct net_context
*c
, TALLOC_CTX
*mem_ctx
,
4686 struct rpc_sh_ctx
*ctx
)
4688 static struct rpc_sh_cmd cmds
[] = {
4690 { "list", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_list
,
4691 "List available shares" },
4693 { "add", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_add
,
4696 { "delete", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_delete
,
4699 { "info", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_info
,
4700 "Get information about a share" },
4702 { NULL
, NULL
, 0, NULL
, NULL
}
4708 /****************************************************************************/
4710 static int rpc_file_usage(struct net_context
*c
, int argc
, const char **argv
)
4712 return net_file_usage(c
, argc
, argv
);
4716 * Close a file on a remote RPC server.
4718 * @param argc Standard main() style argc.
4719 * @param argv Standard main() style argv. Initial components are already
4722 * @return A shell status integer (0 for success).
4724 static int rpc_file_close(struct net_context
*c
, int argc
, const char **argv
)
4726 if (argc
< 1 || c
->display_usage
) {
4727 return rpc_file_usage(c
, argc
, argv
);
4730 return NetFileClose(c
->opt_host
, atoi(argv
[0]));
4734 * Formatted print of open file info
4736 * @param r struct FILE_INFO_3 contents
4739 static void display_file_info_3(struct FILE_INFO_3
*r
)
4741 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4742 r
->fi3_id
, r
->fi3_username
, r
->fi3_permissions
,
4743 r
->fi3_num_locks
, r
->fi3_pathname
);
4747 * List files for a user on a remote RPC server.
4749 * @param argc Standard main() style argc.
4750 * @param argv Standard main() style argv. Initial components are already
4753 * @return A shell status integer (0 for success)..
4756 static int rpc_file_user(struct net_context
*c
, int argc
, const char **argv
)
4758 NET_API_STATUS status
;
4759 uint32 preferred_len
= 0xffffffff, i
;
4760 const char *username
=NULL
;
4761 uint32_t total_entries
= 0;
4762 uint32_t entries_read
= 0;
4763 uint32_t resume_handle
= 0;
4764 struct FILE_INFO_3
*i3
= NULL
;
4766 if (c
->display_usage
) {
4767 return rpc_file_usage(c
, argc
, argv
);
4770 /* if argc > 0, must be user command */
4772 username
= smb_xstrdup(argv
[0]);
4775 status
= NetFileEnum(c
->opt_host
,
4789 /* Display results */
4792 "\nEnumerating open files on remote server:\n\n"
4793 "\nFileId Opened by Perms Locks Path"
4794 "\n------ --------- ----- ----- ---- \n");
4795 for (i
= 0; i
< entries_read
; i
++) {
4796 display_file_info_3(&i3
[i
]);
4803 * 'net rpc file' entrypoint.
4804 * @param argc Standard main() style argc.
4805 * @param argv Standard main() style argv. Initial components are already
4809 int net_rpc_file(struct net_context
*c
, int argc
, const char **argv
)
4811 NET_API_STATUS status
;
4813 struct functable func
[] = {
4818 "Close opened file",
4819 "net rpc file close\n"
4820 " Close opened file"
4826 "List files opened by user",
4827 "net rpc file user\n"
4828 " List files opened by user"
4835 "Display information about opened file",
4836 "net rpc file info\n"
4837 " Display information about opened file"
4840 {NULL
, NULL
, 0, NULL
, NULL
}
4843 status
= libnetapi_init(&c
->netapi_ctx
);
4847 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
4848 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
4849 if (c
->opt_kerberos
) {
4850 libnetapi_set_use_kerberos(c
->netapi_ctx
);
4854 if (c
->display_usage
) {
4855 d_printf("Usage:\n");
4856 d_printf("net rpc file\n"
4857 " List opened files\n");
4858 net_display_usage_from_functable(func
);
4862 return rpc_file_user(c
, argc
, argv
);
4865 return net_run_function(c
, argc
, argv
, "net rpc file", func
);
4869 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
4871 * All parameters are provided by the run_rpc_command function, except for
4872 * argc, argv which are passed through.
4874 * @param c A net_context structure.
4875 * @param domain_sid The domain sid acquired from the remote server.
4876 * @param cli A cli_state connected to the server.
4877 * @param mem_ctx Talloc context, destroyed on completion of the function.
4878 * @param argc Standard main() style argc.
4879 * @param argv Standard main() style argv. Initial components are already
4882 * @return Normal NTSTATUS return.
4885 static NTSTATUS
rpc_shutdown_abort_internals(struct net_context
*c
,
4886 const DOM_SID
*domain_sid
,
4887 const char *domain_name
,
4888 struct cli_state
*cli
,
4889 struct rpc_pipe_client
*pipe_hnd
,
4890 TALLOC_CTX
*mem_ctx
,
4894 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
4896 result
= rpccli_initshutdown_Abort(pipe_hnd
, mem_ctx
, NULL
, NULL
);
4898 if (NT_STATUS_IS_OK(result
)) {
4899 d_printf("\nShutdown successfully aborted\n");
4900 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4902 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4908 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
4910 * All parameters are provided by the run_rpc_command function, except for
4911 * argc, argv which are passed through.
4913 * @param c A net_context structure.
4914 * @param domain_sid The domain sid acquired from the remote server.
4915 * @param cli A cli_state connected to the server.
4916 * @param mem_ctx Talloc context, destroyed on completion of the function.
4917 * @param argc Standard main() style argc.
4918 * @param argv Standard main() style argv. Initial components are already
4921 * @return Normal NTSTATUS return.
4924 static NTSTATUS
rpc_reg_shutdown_abort_internals(struct net_context
*c
,
4925 const DOM_SID
*domain_sid
,
4926 const char *domain_name
,
4927 struct cli_state
*cli
,
4928 struct rpc_pipe_client
*pipe_hnd
,
4929 TALLOC_CTX
*mem_ctx
,
4933 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
4935 result
= rpccli_winreg_AbortSystemShutdown(pipe_hnd
, mem_ctx
, NULL
, NULL
);
4937 if (NT_STATUS_IS_OK(result
)) {
4938 d_printf("\nShutdown successfully aborted\n");
4939 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
4941 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
4947 * ABORT the shutdown of a remote RPC server.
4949 * @param argc Standard main() style argc.
4950 * @param argv Standard main() style argv. Initial components are already
4953 * @return A shell status integer (0 for success).
4956 static int rpc_shutdown_abort(struct net_context
*c
, int argc
,
4961 if (c
->display_usage
) {
4963 "net rpc abortshutdown\n"
4964 " Abort a scheduled shutdown\n");
4968 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
4969 rpc_shutdown_abort_internals
, argc
, argv
);
4974 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4976 return run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
4977 rpc_reg_shutdown_abort_internals
,
4982 * Shut down a remote RPC Server via initshutdown pipe.
4984 * All parameters are provided by the run_rpc_command function, except for
4985 * argc, argv which are passed through.
4987 * @param c A net_context structure.
4988 * @param domain_sid The domain sid acquired from the remote server.
4989 * @param cli A cli_state connected to the server.
4990 * @param mem_ctx Talloc context, destroyed on completion of the function.
4991 * @param argc Standard main() style argc.
4992 * @param argv Standard main() style argv. Initial components are already
4995 * @return Normal NTSTATUS return.
4998 NTSTATUS
rpc_init_shutdown_internals(struct net_context
*c
,
4999 const DOM_SID
*domain_sid
,
5000 const char *domain_name
,
5001 struct cli_state
*cli
,
5002 struct rpc_pipe_client
*pipe_hnd
,
5003 TALLOC_CTX
*mem_ctx
,
5007 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5008 const char *msg
= "This machine will be shutdown shortly";
5009 uint32 timeout
= 20;
5010 struct initshutdown_String msg_string
;
5011 struct initshutdown_String_sub s
;
5013 if (c
->opt_comment
) {
5014 msg
= c
->opt_comment
;
5016 if (c
->opt_timeout
) {
5017 timeout
= c
->opt_timeout
;
5021 msg_string
.name
= &s
;
5023 /* create an entry */
5024 result
= rpccli_initshutdown_Init(pipe_hnd
, mem_ctx
, NULL
,
5025 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5028 if (NT_STATUS_IS_OK(result
)) {
5029 d_printf("\nShutdown of remote machine succeeded\n");
5030 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5032 DEBUG(1,("Shutdown of remote machine failed!\n"));
5038 * Shut down a remote RPC Server via winreg pipe.
5040 * All parameters are provided by the run_rpc_command function, except for
5041 * argc, argv which are passed through.
5043 * @param c A net_context structure.
5044 * @param domain_sid The domain sid acquired from the remote server.
5045 * @param cli A cli_state connected to the server.
5046 * @param mem_ctx Talloc context, destroyed on completion of the function.
5047 * @param argc Standard main() style argc.
5048 * @param argv Standard main() style argv. Initial components are already
5051 * @return Normal NTSTATUS return.
5054 NTSTATUS
rpc_reg_shutdown_internals(struct net_context
*c
,
5055 const DOM_SID
*domain_sid
,
5056 const char *domain_name
,
5057 struct cli_state
*cli
,
5058 struct rpc_pipe_client
*pipe_hnd
,
5059 TALLOC_CTX
*mem_ctx
,
5063 const char *msg
= "This machine will be shutdown shortly";
5064 uint32 timeout
= 20;
5065 struct initshutdown_String msg_string
;
5066 struct initshutdown_String_sub s
;
5070 if (c
->opt_comment
) {
5071 msg
= c
->opt_comment
;
5074 msg_string
.name
= &s
;
5076 if (c
->opt_timeout
) {
5077 timeout
= c
->opt_timeout
;
5080 /* create an entry */
5081 result
= rpccli_winreg_InitiateSystemShutdown(pipe_hnd
, mem_ctx
, NULL
,
5082 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5085 if (NT_STATUS_IS_OK(result
)) {
5086 d_printf("\nShutdown of remote machine succeeded\n");
5088 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5089 if ( W_ERROR_EQUAL(werr
, WERR_MACHINE_LOCKED
) )
5090 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5092 d_fprintf(stderr
, "\nresult was: %s\n", dos_errstr(werr
));
5099 * Shut down a remote RPC server.
5101 * @param argc Standard main() style argc.
5102 * @param argv Standard main() style argv. Initial components are already
5105 * @return A shell status integer (0 for success).
5108 static int rpc_shutdown(struct net_context
*c
, int argc
, const char **argv
)
5112 if (c
->display_usage
) {
5114 "net rpc shutdown\n"
5115 " Shut down a remote RPC server\n");
5119 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
5120 rpc_init_shutdown_internals
, argc
, argv
);
5123 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5124 rc
= run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
5125 rpc_reg_shutdown_internals
, argc
, argv
);
5131 /***************************************************************************
5132 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5133 ***************************************************************************/
5136 * Add interdomain trust account to the RPC server.
5137 * All parameters (except for argc and argv) are passed by run_rpc_command
5140 * @param c A net_context structure.
5141 * @param domain_sid The domain sid acquired from the server.
5142 * @param cli A cli_state connected to the server.
5143 * @param mem_ctx Talloc context, destroyed on completion of the function.
5144 * @param argc Standard main() style argc.
5145 * @param argv Standard main() style argv. Initial components are already
5148 * @return normal NTSTATUS return code.
5151 static NTSTATUS
rpc_trustdom_add_internals(struct net_context
*c
,
5152 const DOM_SID
*domain_sid
,
5153 const char *domain_name
,
5154 struct cli_state
*cli
,
5155 struct rpc_pipe_client
*pipe_hnd
,
5156 TALLOC_CTX
*mem_ctx
,
5160 POLICY_HND connect_pol
, domain_pol
, user_pol
;
5161 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5163 struct lsa_String lsa_acct_name
;
5165 uint32 acct_flags
=0;
5167 uint32_t access_granted
= 0;
5168 union samr_UserInfo info
;
5169 unsigned int orig_timeout
;
5172 d_printf("Usage: net rpc trustdom add <domain_name> "
5173 "<trust password>\n");
5174 return NT_STATUS_INVALID_PARAMETER
;
5178 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5181 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5182 return NT_STATUS_NO_MEMORY
;
5185 strupper_m(acct_name
);
5187 init_lsa_String(&lsa_acct_name
, acct_name
);
5189 /* Get samr policy handle */
5190 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
5192 MAXIMUM_ALLOWED_ACCESS
,
5194 if (!NT_STATUS_IS_OK(result
)) {
5198 /* Get domain policy handle */
5199 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
5201 MAXIMUM_ALLOWED_ACCESS
,
5202 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5204 if (!NT_STATUS_IS_OK(result
)) {
5208 /* This call can take a long time - allow the server to time out.
5209 * 35 seconds should do it. */
5211 orig_timeout
= rpccli_set_timeout(pipe_hnd
, 35000);
5213 /* Create trusting domain's account */
5214 acb_info
= ACB_NORMAL
;
5215 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
5216 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
5217 SAMR_USER_ACCESS_SET_PASSWORD
|
5218 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
5219 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
5221 result
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
5230 /* And restore our original timeout. */
5231 rpccli_set_timeout(pipe_hnd
, orig_timeout
);
5233 if (!NT_STATUS_IS_OK(result
)) {
5234 d_printf("net rpc trustdom add: create user %s failed %s\n",
5235 acct_name
, nt_errstr(result
));
5241 struct samr_LogonHours hours
;
5242 struct lsa_BinaryString parameters
;
5243 const int units_per_week
= 168;
5244 struct samr_CryptPassword crypt_pwd
;
5246 ZERO_STRUCT(notime
);
5248 ZERO_STRUCT(parameters
);
5250 hours
.bits
= talloc_array(mem_ctx
, uint8_t, units_per_week
);
5252 result
= NT_STATUS_NO_MEMORY
;
5255 hours
.units_per_week
= units_per_week
;
5256 memset(hours
.bits
, 0xFF, units_per_week
);
5258 init_samr_CryptPassword(argv
[1],
5259 &cli
->user_session_key
,
5262 init_samr_user_info23(&info
.info23
,
5263 notime
, notime
, notime
,
5264 notime
, notime
, notime
,
5265 NULL
, NULL
, NULL
, NULL
, NULL
,
5266 NULL
, NULL
, NULL
, NULL
, ¶meters
,
5268 SAMR_FIELD_ACCT_FLAGS
| SAMR_FIELD_NT_PASSWORD_PRESENT
,
5270 0, 0, 0, 0, 0, 0, 0,
5273 result
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
5278 if (!NT_STATUS_IS_OK(result
)) {
5279 DEBUG(0,("Could not set trust account password: %s\n",
5280 nt_errstr(result
)));
5286 SAFE_FREE(acct_name
);
5291 * Create interdomain trust account for a remote domain.
5293 * @param argc Standard argc.
5294 * @param argv Standard argv without initial components.
5296 * @return Integer status (0 means success).
5299 static int rpc_trustdom_add(struct net_context
*c
, int argc
, const char **argv
)
5301 if (argc
> 0 && !c
->display_usage
) {
5302 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5303 rpc_trustdom_add_internals
, argc
, argv
);
5306 "net rpc trustdom add <domain_name> <trust password>\n");
5313 * Remove interdomain trust account from the RPC server.
5314 * All parameters (except for argc and argv) are passed by run_rpc_command
5317 * @param c A net_context structure.
5318 * @param domain_sid The domain sid acquired from the server.
5319 * @param cli A cli_state connected to the server.
5320 * @param mem_ctx Talloc context, destroyed on completion of the function.
5321 * @param argc Standard main() style argc.
5322 * @param argv Standard main() style argv. Initial components are already
5325 * @return normal NTSTATUS return code.
5328 static NTSTATUS
rpc_trustdom_del_internals(struct net_context
*c
,
5329 const DOM_SID
*domain_sid
,
5330 const char *domain_name
,
5331 struct cli_state
*cli
,
5332 struct rpc_pipe_client
*pipe_hnd
,
5333 TALLOC_CTX
*mem_ctx
,
5337 POLICY_HND connect_pol
, domain_pol
, user_pol
;
5338 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5340 DOM_SID trust_acct_sid
;
5341 struct samr_Ids user_rids
, name_types
;
5342 struct lsa_String lsa_acct_name
;
5345 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5346 return NT_STATUS_INVALID_PARAMETER
;
5350 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5352 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
5354 if (acct_name
== NULL
)
5355 return NT_STATUS_NO_MEMORY
;
5357 strupper_m(acct_name
);
5359 /* Get samr policy handle */
5360 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
5362 MAXIMUM_ALLOWED_ACCESS
,
5364 if (!NT_STATUS_IS_OK(result
)) {
5368 /* Get domain policy handle */
5369 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
5371 MAXIMUM_ALLOWED_ACCESS
,
5372 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5374 if (!NT_STATUS_IS_OK(result
)) {
5378 init_lsa_String(&lsa_acct_name
, acct_name
);
5380 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
5387 if (!NT_STATUS_IS_OK(result
)) {
5388 d_printf("net rpc trustdom del: LookupNames on user %s failed %s\n",
5389 acct_name
, nt_errstr(result
) );
5393 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
5395 MAXIMUM_ALLOWED_ACCESS
,
5399 if (!NT_STATUS_IS_OK(result
)) {
5400 d_printf("net rpc trustdom del: OpenUser on user %s failed %s\n",
5401 acct_name
, nt_errstr(result
) );
5405 /* append the rid to the domain sid */
5406 sid_copy(&trust_acct_sid
, domain_sid
);
5407 if (!sid_append_rid(&trust_acct_sid
, user_rids
.ids
[0])) {
5411 /* remove the sid */
5413 result
= rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd
, mem_ctx
,
5416 if (!NT_STATUS_IS_OK(result
)) {
5417 d_printf("net rpc trustdom del: RemoveMemberFromForeignDomain on user %s failed %s\n",
5418 acct_name
, nt_errstr(result
) );
5424 result
= rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
5427 if (!NT_STATUS_IS_OK(result
)) {
5428 d_printf("net rpc trustdom del: DeleteUser on user %s failed %s\n",
5429 acct_name
, nt_errstr(result
) );
5433 if (!NT_STATUS_IS_OK(result
)) {
5434 d_printf("Could not set trust account password: %s\n",
5444 * Delete interdomain trust account for a remote domain.
5446 * @param argc Standard argc.
5447 * @param argv Standard argv without initial components.
5449 * @return Integer status (0 means success).
5452 static int rpc_trustdom_del(struct net_context
*c
, int argc
, const char **argv
)
5454 if (argc
> 0 && !c
->display_usage
) {
5455 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5456 rpc_trustdom_del_internals
, argc
, argv
);
5459 "net rpc trustdom del <domain>\n");
5464 static NTSTATUS
rpc_trustdom_get_pdc(struct net_context
*c
,
5465 struct cli_state
*cli
,
5466 TALLOC_CTX
*mem_ctx
,
5467 const char *domain_name
)
5469 char *dc_name
= NULL
;
5470 const char *buffer
= NULL
;
5471 struct rpc_pipe_client
*netr
;
5474 /* Use NetServerEnum2 */
5476 if (cli_get_pdc_name(cli
, domain_name
, &dc_name
)) {
5478 return NT_STATUS_OK
;
5481 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5482 for domain %s\n", domain_name
));
5484 /* Try netr_GetDcName */
5486 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
5488 if (!NT_STATUS_IS_OK(status
)) {
5492 status
= rpccli_netr_GetDcName(netr
, mem_ctx
,
5499 if (NT_STATUS_IS_OK(status
)) {
5503 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5504 for domain %s\n", domain_name
));
5510 * Establish trust relationship to a trusting domain.
5511 * Interdomain account must already be created on remote PDC.
5513 * @param c A net_context structure.
5514 * @param argc Standard argc.
5515 * @param argv Standard argv without initial components.
5517 * @return Integer status (0 means success).
5520 static int rpc_trustdom_establish(struct net_context
*c
, int argc
,
5523 struct cli_state
*cli
= NULL
;
5524 struct sockaddr_storage server_ss
;
5525 struct rpc_pipe_client
*pipe_hnd
= NULL
;
5526 POLICY_HND connect_hnd
;
5527 TALLOC_CTX
*mem_ctx
;
5529 DOM_SID
*domain_sid
;
5534 union lsa_PolicyInformation
*info
= NULL
;
5537 * Connect to \\server\ipc$ as 'our domain' account with password
5540 if (argc
!= 1 || c
->display_usage
) {
5542 "net rpc trustdom establish <domain_name>\n");
5546 domain_name
= smb_xstrdup(argv
[0]);
5547 strupper_m(domain_name
);
5549 /* account name used at first is our domain's name with '$' */
5550 if (asprintf(&acct_name
, "%s$", lp_workgroup()) == -1) {
5553 strupper_m(acct_name
);
5556 * opt_workgroup will be used by connection functions further,
5557 * hence it should be set to remote domain name instead of ours
5559 if (c
->opt_workgroup
) {
5560 c
->opt_workgroup
= smb_xstrdup(domain_name
);
5563 c
->opt_user_name
= acct_name
;
5565 /* find the domain controller */
5566 if (!net_find_pdc(&server_ss
, pdc_name
, domain_name
)) {
5567 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
5571 /* connect to ipc$ as username/password */
5572 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
, pdc_name
);
5573 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
5575 /* Is it trusting domain account for sure ? */
5576 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5577 nt_errstr(nt_status
)));
5581 /* store who we connected to */
5583 saf_store( domain_name
, pdc_name
);
5586 * Connect to \\server\ipc$ again (this time anonymously)
5589 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
5592 if (NT_STATUS_IS_ERR(nt_status
)) {
5593 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5594 domain_name
, nt_errstr(nt_status
)));
5598 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
5599 "domain %s", domain_name
))) {
5600 DEBUG(0, ("talloc_init() failed\n"));
5605 /* Make sure we're talking to a proper server */
5607 nt_status
= rpc_trustdom_get_pdc(c
, cli
, mem_ctx
, domain_name
);
5608 if (!NT_STATUS_IS_OK(nt_status
)) {
5610 talloc_destroy(mem_ctx
);
5615 * Call LsaOpenPolicy and LsaQueryInfo
5618 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
5620 if (!NT_STATUS_IS_OK(nt_status
)) {
5621 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
5623 talloc_destroy(mem_ctx
);
5627 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, true, SEC_RIGHTS_QUERY_VALUE
,
5629 if (NT_STATUS_IS_ERR(nt_status
)) {
5630 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5631 nt_errstr(nt_status
)));
5633 talloc_destroy(mem_ctx
);
5637 /* Querying info level 5 */
5639 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
5641 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
5643 if (NT_STATUS_IS_ERR(nt_status
)) {
5644 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5645 nt_errstr(nt_status
)));
5647 talloc_destroy(mem_ctx
);
5651 domain_sid
= info
->account_domain
.sid
;
5653 /* There should be actually query info level 3 (following nt serv behaviour),
5654 but I still don't know if it's _really_ necessary */
5657 * Store the password in secrets db
5660 if (!pdb_set_trusteddom_pw(domain_name
, c
->opt_password
, domain_sid
)) {
5661 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5663 talloc_destroy(mem_ctx
);
5668 * Close the pipes and clean up
5671 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
5672 if (NT_STATUS_IS_ERR(nt_status
)) {
5673 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5674 nt_errstr(nt_status
)));
5676 talloc_destroy(mem_ctx
);
5682 talloc_destroy(mem_ctx
);
5684 d_printf("Trust to domain %s established\n", domain_name
);
5689 * Revoke trust relationship to the remote domain.
5691 * @param c A net_context structure.
5692 * @param argc Standard argc.
5693 * @param argv Standard argv without initial components.
5695 * @return Integer status (0 means success).
5698 static int rpc_trustdom_revoke(struct net_context
*c
, int argc
,
5704 if (argc
< 1 || c
->display_usage
) {
5706 "net rpc trustdom revoke <domain_name>\n"
5707 " Revoke trust relationship\n"
5708 " domain_name\tName of domain to revoke trust\n");
5712 /* generate upper cased domain name */
5713 domain_name
= smb_xstrdup(argv
[0]);
5714 strupper_m(domain_name
);
5716 /* delete password of the trust */
5717 if (!pdb_del_trusteddom_pw(domain_name
)) {
5718 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5725 SAFE_FREE(domain_name
);
5729 static NTSTATUS
rpc_query_domain_sid(struct net_context
*c
,
5730 const DOM_SID
*domain_sid
,
5731 const char *domain_name
,
5732 struct cli_state
*cli
,
5733 struct rpc_pipe_client
*pipe_hnd
,
5734 TALLOC_CTX
*mem_ctx
,
5739 sid_to_fstring(str_sid
, domain_sid
);
5740 d_printf("%s\n", str_sid
);
5741 return NT_STATUS_OK
;
5744 static void print_trusted_domain(DOM_SID
*dom_sid
, const char *trusted_dom_name
)
5746 fstring ascii_sid
, padding
;
5747 int pad_len
, col_len
= 20;
5749 /* convert sid into ascii string */
5750 sid_to_fstring(ascii_sid
, dom_sid
);
5752 /* calculate padding space for d_printf to look nicer */
5753 pad_len
= col_len
- strlen(trusted_dom_name
);
5754 padding
[pad_len
] = 0;
5755 do padding
[--pad_len
] = ' '; while (pad_len
);
5757 d_printf("%s%s%s\n", trusted_dom_name
, padding
, ascii_sid
);
5760 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
5761 TALLOC_CTX
*mem_ctx
,
5764 const char *trusted_dom_name
)
5767 union lsa_TrustedDomainInfo
*info
= NULL
;
5768 char *cleartextpwd
= NULL
;
5769 uint8_t nt_hash
[16];
5772 nt_status
= rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd
, mem_ctx
,
5775 LSA_TRUSTED_DOMAIN_INFO_PASSWORD
,
5777 if (NT_STATUS_IS_ERR(nt_status
)) {
5778 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5779 nt_errstr(nt_status
)));
5783 data
= data_blob(info
->password
.password
->data
,
5784 info
->password
.password
->length
);
5786 if (!rpccli_get_pwd_hash(pipe_hnd
, nt_hash
)) {
5787 DEBUG(0, ("Could not retrieve password hash\n"));
5791 cleartextpwd
= decrypt_trustdom_secret(nt_hash
, &data
);
5793 if (cleartextpwd
== NULL
) {
5794 DEBUG(0,("retrieved NULL password\n"));
5795 nt_status
= NT_STATUS_UNSUCCESSFUL
;
5799 if (!pdb_set_trusteddom_pw(trusted_dom_name
, cleartextpwd
, &dom_sid
)) {
5800 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5801 nt_status
= NT_STATUS_UNSUCCESSFUL
;
5805 #ifdef DEBUG_PASSWORD
5806 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5807 "password: [%s]\n", trusted_dom_name
,
5808 sid_string_dbg(&dom_sid
), cleartextpwd
));
5812 SAFE_FREE(cleartextpwd
);
5813 data_blob_free(&data
);
5818 static int rpc_trustdom_vampire(struct net_context
*c
, int argc
,
5821 /* common variables */
5822 TALLOC_CTX
* mem_ctx
;
5823 struct cli_state
*cli
= NULL
;
5824 struct rpc_pipe_client
*pipe_hnd
= NULL
;
5826 const char *domain_name
= NULL
;
5827 DOM_SID
*queried_dom_sid
;
5828 POLICY_HND connect_hnd
;
5829 union lsa_PolicyInformation
*info
= NULL
;
5831 /* trusted domains listing variables */
5832 unsigned int enum_ctx
= 0;
5834 struct lsa_DomainList dom_list
;
5837 if (c
->display_usage
) {
5839 "net rpc trustdom vampire\n"
5840 " Vampire trust relationship from remote server\n");
5845 * Listing trusted domains (stored in secrets.tdb, if local)
5848 mem_ctx
= talloc_init("trust relationships vampire");
5851 * set domain and pdc name to local samba server (default)
5852 * or to remote one given in command line
5855 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
5856 domain_name
= c
->opt_workgroup
;
5857 c
->opt_target_workgroup
= c
->opt_workgroup
;
5859 fstrcpy(pdc_name
, global_myname());
5860 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
5861 c
->opt_target_workgroup
= domain_name
;
5864 /* open \PIPE\lsarpc and open policy handle */
5865 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
5866 if (!NT_STATUS_IS_OK(nt_status
)) {
5867 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
5868 nt_errstr(nt_status
)));
5869 talloc_destroy(mem_ctx
);
5873 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
5875 if (!NT_STATUS_IS_OK(nt_status
)) {
5876 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5877 nt_errstr(nt_status
) ));
5879 talloc_destroy(mem_ctx
);
5883 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, SEC_RIGHTS_QUERY_VALUE
,
5885 if (NT_STATUS_IS_ERR(nt_status
)) {
5886 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5887 nt_errstr(nt_status
)));
5889 talloc_destroy(mem_ctx
);
5893 /* query info level 5 to obtain sid of a domain being queried */
5894 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
5896 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
5899 if (NT_STATUS_IS_ERR(nt_status
)) {
5900 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5901 nt_errstr(nt_status
)));
5903 talloc_destroy(mem_ctx
);
5907 queried_dom_sid
= info
->account_domain
.sid
;
5910 * Keep calling LsaEnumTrustdom over opened pipe until
5911 * the end of enumeration is reached
5914 d_printf("Vampire trusted domains:\n\n");
5917 nt_status
= rpccli_lsa_EnumTrustDom(pipe_hnd
, mem_ctx
,
5922 if (NT_STATUS_IS_ERR(nt_status
)) {
5923 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5924 nt_errstr(nt_status
)));
5926 talloc_destroy(mem_ctx
);
5930 for (i
= 0; i
< dom_list
.count
; i
++) {
5932 print_trusted_domain(dom_list
.domains
[i
].sid
,
5933 dom_list
.domains
[i
].name
.string
);
5935 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
5936 *dom_list
.domains
[i
].sid
,
5937 dom_list
.domains
[i
].name
.string
);
5938 if (!NT_STATUS_IS_OK(nt_status
)) {
5940 talloc_destroy(mem_ctx
);
5946 * in case of no trusted domains say something rather
5947 * than just display blank line
5949 if (!dom_list
.count
) d_printf("none\n");
5951 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
5953 /* close this connection before doing next one */
5954 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
5955 if (NT_STATUS_IS_ERR(nt_status
)) {
5956 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5957 nt_errstr(nt_status
)));
5959 talloc_destroy(mem_ctx
);
5963 /* close lsarpc pipe and connection to IPC$ */
5966 talloc_destroy(mem_ctx
);
5970 static int rpc_trustdom_list(struct net_context
*c
, int argc
, const char **argv
)
5972 /* common variables */
5973 TALLOC_CTX
* mem_ctx
;
5974 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
5975 struct rpc_pipe_client
*pipe_hnd
= NULL
;
5977 const char *domain_name
= NULL
;
5978 DOM_SID
*queried_dom_sid
;
5980 int ascii_dom_name_len
;
5981 POLICY_HND connect_hnd
;
5982 union lsa_PolicyInformation
*info
= NULL
;
5984 /* trusted domains listing variables */
5985 unsigned int num_domains
, enum_ctx
= 0;
5986 int i
, pad_len
, col_len
= 20;
5987 struct lsa_DomainList dom_list
;
5990 /* trusting domains listing variables */
5991 POLICY_HND domain_hnd
;
5992 struct samr_SamArray
*trusts
= NULL
;
5994 if (c
->display_usage
) {
5996 "net rpc trustdom list\n"
5997 " List trust relationships\n");
6002 * Listing trusted domains (stored in secrets.tdb, if local)
6005 mem_ctx
= talloc_init("trust relationships listing");
6008 * set domain and pdc name to local samba server (default)
6009 * or to remote one given in command line
6012 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
6013 domain_name
= c
->opt_workgroup
;
6014 c
->opt_target_workgroup
= c
->opt_workgroup
;
6016 fstrcpy(pdc_name
, global_myname());
6017 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6018 c
->opt_target_workgroup
= domain_name
;
6021 /* open \PIPE\lsarpc and open policy handle */
6022 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6023 if (!NT_STATUS_IS_OK(nt_status
)) {
6024 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6025 nt_errstr(nt_status
)));
6026 talloc_destroy(mem_ctx
);
6030 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6032 if (!NT_STATUS_IS_OK(nt_status
)) {
6033 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6034 nt_errstr(nt_status
) ));
6036 talloc_destroy(mem_ctx
);
6040 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, SEC_RIGHTS_QUERY_VALUE
,
6042 if (NT_STATUS_IS_ERR(nt_status
)) {
6043 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6044 nt_errstr(nt_status
)));
6046 talloc_destroy(mem_ctx
);
6050 /* query info level 5 to obtain sid of a domain being queried */
6051 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
6053 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6056 if (NT_STATUS_IS_ERR(nt_status
)) {
6057 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6058 nt_errstr(nt_status
)));
6060 talloc_destroy(mem_ctx
);
6064 queried_dom_sid
= info
->account_domain
.sid
;
6067 * Keep calling LsaEnumTrustdom over opened pipe until
6068 * the end of enumeration is reached
6071 d_printf("Trusted domains list:\n\n");
6074 nt_status
= rpccli_lsa_EnumTrustDom(pipe_hnd
, mem_ctx
,
6079 if (NT_STATUS_IS_ERR(nt_status
)) {
6080 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6081 nt_errstr(nt_status
)));
6083 talloc_destroy(mem_ctx
);
6087 for (i
= 0; i
< dom_list
.count
; i
++) {
6088 print_trusted_domain(dom_list
.domains
[i
].sid
,
6089 dom_list
.domains
[i
].name
.string
);
6093 * in case of no trusted domains say something rather
6094 * than just display blank line
6096 if (!dom_list
.count
) d_printf("none\n");
6098 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6100 /* close this connection before doing next one */
6101 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6102 if (NT_STATUS_IS_ERR(nt_status
)) {
6103 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6104 nt_errstr(nt_status
)));
6106 talloc_destroy(mem_ctx
);
6110 TALLOC_FREE(pipe_hnd
);
6113 * Listing trusting domains (stored in passdb backend, if local)
6116 d_printf("\nTrusting domains list:\n\n");
6119 * Open \PIPE\samr and get needed policy handles
6121 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
6123 if (!NT_STATUS_IS_OK(nt_status
)) {
6124 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6126 talloc_destroy(mem_ctx
);
6131 nt_status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
6133 SAMR_ACCESS_OPEN_DOMAIN
,
6135 if (!NT_STATUS_IS_OK(nt_status
)) {
6136 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6137 nt_errstr(nt_status
)));
6139 talloc_destroy(mem_ctx
);
6143 /* SamrOpenDomain - we have to open domain policy handle in order to be
6144 able to enumerate accounts*/
6145 nt_status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
6147 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS
,
6150 if (!NT_STATUS_IS_OK(nt_status
)) {
6151 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6152 nt_errstr(nt_status
)));
6154 talloc_destroy(mem_ctx
);
6159 * perform actual enumeration
6162 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6165 nt_status
= rpccli_samr_EnumDomainUsers(pipe_hnd
, mem_ctx
,
6172 if (NT_STATUS_IS_ERR(nt_status
)) {
6173 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6174 nt_errstr(nt_status
)));
6176 talloc_destroy(mem_ctx
);
6180 for (i
= 0; i
< num_domains
; i
++) {
6182 char *str
= CONST_DISCARD(char *, trusts
->entries
[i
].name
.string
);
6185 * get each single domain's sid (do we _really_ need this ?):
6186 * 1) connect to domain's pdc
6187 * 2) query the pdc for domain's sid
6190 /* get rid of '$' tail */
6191 ascii_dom_name_len
= strlen(str
);
6192 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
6193 str
[ascii_dom_name_len
- 1] = '\0';
6195 /* calculate padding space for d_printf to look nicer */
6196 pad_len
= col_len
- strlen(str
);
6197 padding
[pad_len
] = 0;
6198 do padding
[--pad_len
] = ' '; while (pad_len
);
6200 /* set opt_* variables to remote domain */
6202 c
->opt_workgroup
= talloc_strdup(mem_ctx
, str
);
6203 c
->opt_target_workgroup
= c
->opt_workgroup
;
6205 d_printf("%s%s", str
, padding
);
6207 /* connect to remote domain controller */
6208 nt_status
= net_make_ipc_connection(c
,
6209 NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
,
6211 if (NT_STATUS_IS_OK(nt_status
)) {
6212 /* query for domain's sid */
6213 if (run_rpc_command(
6215 &ndr_table_lsarpc
.syntax_id
, 0,
6216 rpc_query_domain_sid
, argc
,
6218 d_fprintf(stderr
, "couldn't get domain's sid\n");
6220 cli_shutdown(remote_cli
);
6223 d_fprintf(stderr
, "domain controller is not "
6225 nt_errstr(nt_status
));
6229 if (!num_domains
) d_printf("none\n");
6231 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6233 /* close opened samr and domain policy handles */
6234 nt_status
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_hnd
);
6235 if (!NT_STATUS_IS_OK(nt_status
)) {
6236 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
6239 nt_status
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6240 if (!NT_STATUS_IS_OK(nt_status
)) {
6241 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
6244 /* close samr pipe and connection to IPC$ */
6247 talloc_destroy(mem_ctx
);
6252 * Entrypoint for 'net rpc trustdom' code.
6254 * @param argc Standard argc.
6255 * @param argv Standard argv without initial components.
6257 * @return Integer status (0 means success).
6260 static int rpc_trustdom(struct net_context
*c
, int argc
, const char **argv
)
6262 struct functable func
[] = {
6267 "Add trusted domain's account",
6268 "net rpc trustdom add\n"
6269 " Add trusted domain's account"
6275 "Remove trusted domain's account",
6276 "net rpc trustdom del\n"
6277 " Remove trusted domain's account"
6281 rpc_trustdom_establish
,
6283 "Establish trust relationship",
6284 "net rpc trustdom establish\n"
6285 " Establish trust relationship"
6289 rpc_trustdom_revoke
,
6291 "Revoke trust relationship",
6292 "net rpc trustdom revoke\n"
6293 " Revoke trust relationship"
6299 "List domain trusts",
6300 "net rpc trustdom list\n"
6301 " List domain trusts"
6305 rpc_trustdom_vampire
,
6307 "Vampire trusts from remote server",
6308 "net rpc trustdom vampire\n"
6309 " Vampire trusts from remote server"
6311 {NULL
, NULL
, 0, NULL
, NULL
}
6314 return net_run_function(c
, argc
, argv
, "net rpc trustdom", func
);
6318 * Check if a server will take rpc commands
6319 * @param flags Type of server to connect to (PDC, DMB, localhost)
6320 * if the host is not explicitly specified
6321 * @return bool (true means rpc supported)
6323 bool net_rpc_check(struct net_context
*c
, unsigned flags
)
6325 struct cli_state
*cli
;
6327 struct sockaddr_storage server_ss
;
6328 char *server_name
= NULL
;
6331 /* flags (i.e. server type) may depend on command */
6332 if (!net_find_server(c
, NULL
, flags
, &server_ss
, &server_name
))
6335 if ((cli
= cli_initialise()) == NULL
) {
6339 status
= cli_connect(cli
, server_name
, &server_ss
);
6340 if (!NT_STATUS_IS_OK(status
))
6342 if (!attempt_netbios_session_request(&cli
, global_myname(),
6343 server_name
, &server_ss
))
6345 if (!cli_negprot(cli
))
6347 if (cli
->protocol
< PROTOCOL_NT1
)
6356 /* dump sam database via samsync rpc calls */
6357 static int rpc_samdump(struct net_context
*c
, int argc
, const char **argv
) {
6358 if (c
->display_usage
) {
6361 " Dump remote SAM database\n");
6365 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
6366 NET_FLAGS_ANONYMOUS
,
6367 rpc_samdump_internals
, argc
, argv
);
6370 /* syncronise sam database via samsync rpc calls */
6371 static int rpc_vampire(struct net_context
*c
, int argc
, const char **argv
)
6373 struct functable func
[] = {
6378 "Dump remote SAM database to ldif",
6379 "net rpc vampire ldif\n"
6380 " Dump remote SAM database to LDIF file or stdout"
6386 "Dump remote SAM database to Kerberos Keytab",
6387 "net rpc vampire keytab\n"
6388 " Dump remote SAM database to Kerberos keytab file"
6394 "Dump remote SAM database to passdb",
6395 "net rpc vampire passdb\n"
6396 " Dump remote SAM database to passdb"
6399 {NULL
, NULL
, 0, NULL
, NULL
}
6403 if (c
->display_usage
) {
6406 " Vampire remote SAM database\n");
6410 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
6411 NET_FLAGS_ANONYMOUS
,
6412 rpc_vampire_internals
,
6416 return net_run_function(c
, argc
, argv
, "net rpc vampire", func
);
6420 * Migrate everything from a print server.
6422 * @param c A net_context structure.
6423 * @param argc Standard main() style argc.
6424 * @param argv Standard main() style argv. Initial components are already
6427 * @return A shell status integer (0 for success).
6429 * The order is important !
6430 * To successfully add drivers the print queues have to exist !
6431 * Applying ACLs should be the last step, because you're easily locked out.
6434 static int rpc_printer_migrate_all(struct net_context
*c
, int argc
,
6439 if (c
->display_usage
) {
6441 "net rpc printer migrate all\n"
6442 " Migrate everything from a print server\n");
6447 d_printf("no server to migrate\n");
6451 ret
= run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6452 rpc_printer_migrate_printers_internals
, argc
,
6457 ret
= run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6458 rpc_printer_migrate_drivers_internals
, argc
,
6463 ret
= run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6464 rpc_printer_migrate_forms_internals
, argc
, argv
);
6468 ret
= run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6469 rpc_printer_migrate_settings_internals
, argc
,
6474 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6475 rpc_printer_migrate_security_internals
, argc
,
6481 * Migrate print drivers from a print server.
6483 * @param c A net_context structure.
6484 * @param argc Standard main() style argc.
6485 * @param argv Standard main() style argv. Initial components are already
6488 * @return A shell status integer (0 for success).
6490 static int rpc_printer_migrate_drivers(struct net_context
*c
, int argc
,
6493 if (c
->display_usage
) {
6495 "net rpc printer migrate drivers\n"
6496 " Migrate print-drivers from a print-server\n");
6501 d_printf("no server to migrate\n");
6505 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6506 rpc_printer_migrate_drivers_internals
,
6511 * Migrate print-forms from a print-server.
6513 * @param c A net_context structure.
6514 * @param argc Standard main() style argc.
6515 * @param argv Standard main() style argv. Initial components are already
6518 * @return A shell status integer (0 for success).
6520 static int rpc_printer_migrate_forms(struct net_context
*c
, int argc
,
6523 if (c
->display_usage
) {
6525 "net rpc printer migrate forms\n"
6526 " Migrate print-forms from a print-server\n");
6531 d_printf("no server to migrate\n");
6535 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6536 rpc_printer_migrate_forms_internals
,
6541 * Migrate printers from a print-server.
6543 * @param c A net_context structure.
6544 * @param argc Standard main() style argc.
6545 * @param argv Standard main() style argv. Initial components are already
6548 * @return A shell status integer (0 for success).
6550 static int rpc_printer_migrate_printers(struct net_context
*c
, int argc
,
6553 if (c
->display_usage
) {
6555 "net rpc printer migrate printers\n"
6556 " Migrate printers from a print-server\n");
6561 d_printf("no server to migrate\n");
6565 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6566 rpc_printer_migrate_printers_internals
,
6571 * Migrate printer-ACLs from a print-server
6573 * @param c A net_context structure.
6574 * @param argc Standard main() style argc.
6575 * @param argv Standard main() style argv. Initial components are already
6578 * @return A shell status integer (0 for success).
6580 static int rpc_printer_migrate_security(struct net_context
*c
, int argc
,
6583 if (c
->display_usage
) {
6585 "net rpc printer migrate security\n"
6586 " Migrate printer-ACLs from a print-server\n");
6591 d_printf("no server to migrate\n");
6595 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6596 rpc_printer_migrate_security_internals
,
6601 * Migrate printer-settings from a print-server.
6603 * @param c A net_context structure.
6604 * @param argc Standard main() style argc.
6605 * @param argv Standard main() style argv. Initial components are already
6608 * @return A shell status integer (0 for success).
6610 static int rpc_printer_migrate_settings(struct net_context
*c
, int argc
,
6613 if (c
->display_usage
) {
6615 "net rpc printer migrate settings\n"
6616 " Migrate printer-settings from a print-server\n");
6621 d_printf("no server to migrate\n");
6625 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6626 rpc_printer_migrate_settings_internals
,
6631 * 'net rpc printer' entrypoint.
6633 * @param c A net_context structure.
6634 * @param argc Standard main() style argc.
6635 * @param argv Standard main() style argv. Initial components are already
6639 int rpc_printer_migrate(struct net_context
*c
, int argc
, const char **argv
)
6642 /* ouch: when addriver and setdriver are called from within
6643 rpc_printer_migrate_drivers_internals, the printer-queue already
6646 struct functable func
[] = {
6649 rpc_printer_migrate_all
,
6651 "Migrate all from remote to local print server",
6652 "net rpc printer migrate all\n"
6653 " Migrate all from remote to local print server"
6657 rpc_printer_migrate_drivers
,
6659 "Migrate drivers to local server",
6660 "net rpc printer migrate drivers\n"
6661 " Migrate drivers to local server"
6665 rpc_printer_migrate_forms
,
6667 "Migrate froms to local server",
6668 "net rpc printer migrate forms\n"
6669 " Migrate froms to local server"
6673 rpc_printer_migrate_printers
,
6675 "Migrate printers to local server",
6676 "net rpc printer migrate printers\n"
6677 " Migrate printers to local server"
6681 rpc_printer_migrate_security
,
6683 "Mirgate printer ACLs to local server",
6684 "net rpc printer migrate security\n"
6685 " Mirgate printer ACLs to local server"
6689 rpc_printer_migrate_settings
,
6691 "Migrate printer settings to local server",
6692 "net rpc printer migrate settings\n"
6693 " Migrate printer settings to local server"
6695 {NULL
, NULL
, 0, NULL
, NULL
}
6698 return net_run_function(c
, argc
, argv
, "net rpc printer migrate",func
);
6703 * List printers on a remote RPC server.
6705 * @param c A net_context structure.
6706 * @param argc Standard main() style argc.
6707 * @param argv Standard main() style argv. Initial components are already
6710 * @return A shell status integer (0 for success).
6712 static int rpc_printer_list(struct net_context
*c
, int argc
, const char **argv
)
6714 if (c
->display_usage
) {
6716 "net rpc printer list\n"
6717 " List printers on a remote RPC server\n");
6721 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6722 rpc_printer_list_internals
,
6727 * List printer-drivers on a remote RPC server.
6729 * @param c A net_context structure.
6730 * @param argc Standard main() style argc.
6731 * @param argv Standard main() style argv. Initial components are already
6734 * @return A shell status integer (0 for success).
6736 static int rpc_printer_driver_list(struct net_context
*c
, int argc
,
6739 if (c
->display_usage
) {
6741 "net rpc printer driver\n"
6742 " List printer-drivers on a remote RPC server\n");
6746 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6747 rpc_printer_driver_list_internals
,
6752 * Publish printer in ADS via MSRPC.
6754 * @param c A net_context structure.
6755 * @param argc Standard main() style argc.
6756 * @param argv Standard main() style argv. Initial components are already
6759 * @return A shell status integer (0 for success).
6761 static int rpc_printer_publish_publish(struct net_context
*c
, int argc
,
6764 if (c
->display_usage
) {
6766 "net rpc printer publish publish\n"
6767 " Publish printer in ADS via MSRPC\n");
6771 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6772 rpc_printer_publish_publish_internals
,
6777 * Update printer in ADS via MSRPC.
6779 * @param c A net_context structure.
6780 * @param argc Standard main() style argc.
6781 * @param argv Standard main() style argv. Initial components are already
6784 * @return A shell status integer (0 for success).
6786 static int rpc_printer_publish_update(struct net_context
*c
, int argc
, const char **argv
)
6788 if (c
->display_usage
) {
6790 "net rpc printer publish update\n"
6791 " Update printer in ADS via MSRPC\n");
6795 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6796 rpc_printer_publish_update_internals
,
6801 * UnPublish printer in ADS via MSRPC.
6803 * @param c A net_context structure.
6804 * @param argc Standard main() style argc.
6805 * @param argv Standard main() style argv. Initial components are already
6808 * @return A shell status integer (0 for success).
6810 static int rpc_printer_publish_unpublish(struct net_context
*c
, int argc
,
6813 if (c
->display_usage
) {
6815 "net rpc printer publish unpublish\n"
6816 " UnPublish printer in ADS via MSRPC\n");
6820 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6821 rpc_printer_publish_unpublish_internals
,
6826 * List published printers via MSRPC.
6828 * @param c A net_context structure.
6829 * @param argc Standard main() style argc.
6830 * @param argv Standard main() style argv. Initial components are already
6833 * @return A shell status integer (0 for success).
6835 static int rpc_printer_publish_list(struct net_context
*c
, int argc
,
6838 if (c
->display_usage
) {
6840 "net rpc printer publish list\n"
6841 " List published printers via MSRPC\n");
6845 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6846 rpc_printer_publish_list_internals
,
6852 * Publish printer in ADS.
6854 * @param c A net_context structure.
6855 * @param argc Standard main() style argc.
6856 * @param argv Standard main() style argv. Initial components are already
6859 * @return A shell status integer (0 for success).
6861 static int rpc_printer_publish(struct net_context
*c
, int argc
,
6865 struct functable func
[] = {
6868 rpc_printer_publish_publish
,
6870 "Publish printer in AD",
6871 "net rpc printer publish publish\n"
6872 " Publish printer in AD"
6876 rpc_printer_publish_update
,
6878 "Update printer in AD",
6879 "net rpc printer publish update\n"
6880 " Update printer in AD"
6884 rpc_printer_publish_unpublish
,
6886 "Unpublish printer",
6887 "net rpc printer publish unpublish\n"
6888 " Unpublish printer"
6892 rpc_printer_publish_list
,
6894 "List published printers",
6895 "net rpc printer publish list\n"
6896 " List published printers"
6898 {NULL
, NULL
, 0, NULL
, NULL
}
6902 if (c
->display_usage
) {
6903 d_printf("Usage:\n");
6904 d_printf("net rpc printer publish\n"
6905 " List published printers\n"
6906 " Alias of net rpc printer publish list\n");
6907 net_display_usage_from_functable(func
);
6910 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6911 rpc_printer_publish_list_internals
,
6915 return net_run_function(c
, argc
, argv
, "net rpc printer publish",func
);
6921 * Display rpc printer help page.
6923 * @param c A net_context structure.
6924 * @param argc Standard main() style argc.
6925 * @param argv Standard main() style argv. Initial components are already
6928 int rpc_printer_usage(struct net_context
*c
, int argc
, const char **argv
)
6930 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"
6931 "\tlists all printers on print-server\n\n");
6932 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
6933 "\tlists all printer-drivers on print-server\n\n");
6934 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
6935 "\tpublishes printer settings in Active Directory\n"
6936 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
6937 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
6938 "\n\tmigrates printers from remote to local server\n\n");
6939 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
6940 "\n\tmigrates printer-settings from remote to local server\n\n");
6941 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
6942 "\n\tmigrates printer-drivers from remote to local server\n\n");
6943 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
6944 "\n\tmigrates printer-forms from remote to local server\n\n");
6945 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
6946 "\n\tmigrates printer-ACLs from remote to local server\n\n");
6947 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
6948 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
6949 "\tremote to local print-server\n\n");
6950 net_common_methods_usage(c
, argc
, argv
);
6951 net_common_flags_usage(c
, argc
, argv
);
6953 "\t-v or --verbose\t\t\tgive verbose output\n"
6954 "\t --destination\t\tmigration target server (default: localhost)\n");
6960 * 'net rpc printer' entrypoint.
6962 * @param c A net_context structure.
6963 * @param argc Standard main() style argc.
6964 * @param argv Standard main() style argv. Initial components are already
6967 int net_rpc_printer(struct net_context
*c
, int argc
, const char **argv
)
6969 struct functable func
[] = {
6974 "List all printers on print server",
6975 "net rpc printer list\n"
6976 " List all printers on print server"
6980 rpc_printer_migrate
,
6982 "Migrate printer to local server",
6983 "net rpc printer migrate\n"
6984 " Migrate printer to local server"
6988 rpc_printer_driver_list
,
6990 "List printer drivers",
6991 "net rpc printer driver\n"
6992 " List printer drivers"
6996 rpc_printer_publish
,
6998 "Publish printer in AD",
6999 "net rpc printer publish\n"
7000 " Publish printer in AD"
7002 {NULL
, NULL
, 0, NULL
, NULL
}
7006 if (c
->display_usage
) {
7007 d_printf("Usage:\n");
7008 d_printf("net rpc printer\n"
7009 " List printers\n");
7010 net_display_usage_from_functable(func
);
7013 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
7014 rpc_printer_list_internals
,
7018 return net_run_function(c
, argc
, argv
, "net rpc printer", func
);
7022 * 'net rpc' entrypoint.
7024 * @param c A net_context structure.
7025 * @param argc Standard main() style argc.
7026 * @param argv Standard main() style argv. Initial components are already
7030 int net_rpc(struct net_context
*c
, int argc
, const char **argv
)
7032 struct functable func
[] = {
7037 "Modify global audit settings",
7039 " Modify global audit settings"
7045 "Show basic info about a domain",
7047 " Show basic info about a domain"
7061 "Join a domain created in server manager",
7063 " Join a domain created in server manager"
7069 "Test that a join is valid",
7070 "net rpc testjoin\n"
7071 " Test that a join is valid"
7077 "List/modify users",
7079 " List/modify users"
7085 "Change a user password",
7086 "net rpc password\n"
7087 " Change a user password\n"
7088 " Alias for net rpc user password"
7094 "List/modify groups",
7096 " List/modify groups"
7102 "List/modify shares",
7104 " List/modify shares"
7118 "List/modify printers",
7120 " List/modify printers"
7124 net_rpc_changetrustpw
,
7126 "Change trust account password",
7127 "net rpc changetrustpw\n"
7128 " Change trust account password"
7134 "Modify domain trusts",
7135 "net rpc trustdom\n"
7136 " Modify domain trusts"
7142 "Abort a remote shutdown",
7143 "net rpc abortshutdown\n"
7144 " Abort a remote shutdown"
7150 "Shutdown a remote server",
7151 "net rpc shutdown\n"
7152 " Shutdown a remote server"
7158 "Dump SAM data of remote NT PDC",
7160 " Dump SAM data of remote NT PDC"
7166 "Sync a remote NT PDC's data into local passdb",
7168 " Sync a remote NT PDC's data into local passdb"
7174 "Fetch the domain sid into local secrets.tdb",
7176 " Fetch the domain sid into local secrets.tdb"
7182 "Manage privileges assigned to SID",
7184 " Manage privileges assigned to SID"
7190 "Start/stop/query remote services",
7192 " Start/stop/query remote services"
7198 "Manage registry hives",
7199 "net rpc registry\n"
7200 " Manage registry hives"
7206 "Open interactive shell on remote server",
7208 " Open interactive shell on remote server"
7210 {NULL
, NULL
, 0, NULL
, NULL
}
7212 return net_run_function(c
, argc
, argv
, "net rpc", func
);