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 * All parameters are provided by the run_rpc_command function, except for
662 * argc, argv which are passed through.
664 * @param domain_sid The domain sid acquired from the remote server.
665 * @param cli A cli_state connected to the server.
666 * @param mem_ctx Talloc context, destroyed on completion of the function.
667 * @param argc Standard main() style argc.
668 * @param argv Standard main() style argv. Initial components are already
671 * @return Normal NTSTATUS return.
674 static NTSTATUS
rpc_user_rename_internals(struct net_context
*c
,
675 const DOM_SID
*domain_sid
,
676 const char *domain_name
,
677 struct cli_state
*cli
,
678 struct rpc_pipe_client
*pipe_hnd
,
683 POLICY_HND connect_pol
, domain_pol
, user_pol
;
684 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
685 uint32 info_level
= 7;
686 const char *old_name
, *new_name
;
687 struct samr_Ids user_rids
, name_types
;
688 struct lsa_String lsa_acct_name
;
689 union samr_UserInfo
*info
= NULL
;
691 if (argc
!= 2 || c
->display_usage
) {
692 rpc_user_usage(c
, argc
, argv
);
699 /* Get sam policy handle */
701 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
703 MAXIMUM_ALLOWED_ACCESS
,
706 if (!NT_STATUS_IS_OK(result
)) {
710 /* Get domain policy handle */
712 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
714 MAXIMUM_ALLOWED_ACCESS
,
715 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
717 if (!NT_STATUS_IS_OK(result
)) {
721 init_lsa_String(&lsa_acct_name
, old_name
);
723 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
729 if (!NT_STATUS_IS_OK(result
)) {
733 /* Open domain user */
734 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
736 MAXIMUM_ALLOWED_ACCESS
,
740 if (!NT_STATUS_IS_OK(result
)) {
744 /* Query user info */
745 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
750 if (!NT_STATUS_IS_OK(result
)) {
754 init_samr_user_info7(&info
->info7
, new_name
);
757 result
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
762 if (!NT_STATUS_IS_OK(result
)) {
767 if (!NT_STATUS_IS_OK(result
)) {
768 d_fprintf(stderr
, "Failed to rename user from %s to %s - %s\n", old_name
, new_name
,
771 d_printf("Renamed user from %s to %s\n", old_name
, new_name
);
777 * Rename a user on a remote RPC server.
779 * @param argc Standard main() style argc.
780 * @param argv Standard main() style argv. Initial components are already
783 * @return A shell status integer (0 for success).
786 static int rpc_user_rename(struct net_context
*c
, int argc
, const char **argv
)
788 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
789 rpc_user_rename_internals
, argc
, argv
);
793 * Delete a user from a remote RPC server.
795 * @param argc Standard main() style argc.
796 * @param argv Standard main() style argv. Initial components are already
799 * @return A shell status integer (0 for success).
802 static int rpc_user_delete(struct net_context
*c
, int argc
, const char **argv
)
804 NET_API_STATUS status
;
806 if (argc
< 1 || c
->display_usage
) {
807 rpc_user_usage(c
, argc
, argv
);
811 status
= NetUserDel(c
->opt_host
, argv
[0]);
814 d_fprintf(stderr
, "Failed to delete user '%s' with: %s.\n",
816 libnetapi_get_error_string(c
->netapi_ctx
, status
));
819 d_printf("Deleted user '%s'.\n", argv
[0]);
826 * Set a password for a user on a remote RPC server.
828 * All parameters are provided by the run_rpc_command function, except for
829 * argc, argv which are passed through.
831 * @param domain_sid The domain sid acquired from the remote server.
832 * @param cli A cli_state connected to the server.
833 * @param mem_ctx Talloc context, destroyed on completion of the function.
834 * @param argc Standard main() style argc.
835 * @param argv Standard main() style argv. Initial components are already
838 * @return Normal NTSTATUS return.
841 static NTSTATUS
rpc_user_password_internals(struct net_context
*c
,
842 const DOM_SID
*domain_sid
,
843 const char *domain_name
,
844 struct cli_state
*cli
,
845 struct rpc_pipe_client
*pipe_hnd
,
850 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
851 POLICY_HND connect_pol
, domain_pol
, user_pol
;
854 const char *new_password
;
856 union samr_UserInfo info
;
858 if (argc
< 1 || c
->display_usage
) {
859 rpc_user_usage(c
, argc
, argv
);
866 new_password
= argv
[1];
868 asprintf(&prompt
, "Enter new password for %s:", user
);
869 new_password
= getpass(prompt
);
873 /* Get sam policy and domain handles */
875 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
877 MAXIMUM_ALLOWED_ACCESS
,
880 if (!NT_STATUS_IS_OK(result
)) {
884 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
886 MAXIMUM_ALLOWED_ACCESS
,
887 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
890 if (!NT_STATUS_IS_OK(result
)) {
894 /* Get handle on user */
897 struct samr_Ids user_rids
, name_types
;
898 struct lsa_String lsa_acct_name
;
900 init_lsa_String(&lsa_acct_name
, user
);
902 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
908 if (!NT_STATUS_IS_OK(result
)) {
912 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
914 MAXIMUM_ALLOWED_ACCESS
,
918 if (!NT_STATUS_IS_OK(result
)) {
923 /* Set password on account */
925 encode_pw_buffer(pwbuf
, new_password
, STR_UNICODE
);
927 init_samr_user_info24(&info
.info24
, pwbuf
, 24);
929 SamOEMhashBlob(info
.info24
.password
.data
, 516,
930 &cli
->user_session_key
);
932 result
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
937 if (!NT_STATUS_IS_OK(result
)) {
941 /* Display results */
949 * Set a user's password on a remote RPC server.
951 * @param argc Standard main() style argc.
952 * @param argv Standard main() style argv. Initial components are already
955 * @return A shell status integer (0 for success).
958 static int rpc_user_password(struct net_context
*c
, int argc
, const char **argv
)
960 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
961 rpc_user_password_internals
, argc
, argv
);
965 * List user's groups on a remote RPC server.
967 * All parameters are provided by the run_rpc_command function, except for
968 * argc, argv which are passed through.
970 * @param domain_sid The domain sid acquired from the remote server.
971 * @param cli A cli_state connected to the server.
972 * @param mem_ctx Talloc context, destroyed on completion of the function.
973 * @param argc Standard main() style argc.
974 * @param argv Standard main() style argv. Initial components are already
977 * @return Normal NTSTATUS return.
980 static NTSTATUS
rpc_user_info_internals(struct net_context
*c
,
981 const DOM_SID
*domain_sid
,
982 const char *domain_name
,
983 struct cli_state
*cli
,
984 struct rpc_pipe_client
*pipe_hnd
,
989 POLICY_HND connect_pol
, domain_pol
, user_pol
;
990 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
992 struct samr_RidWithAttributeArray
*rid_array
= NULL
;
993 struct lsa_Strings names
;
994 struct samr_Ids types
;
995 uint32_t *lrids
= NULL
;
996 struct samr_Ids rids
, name_types
;
997 struct lsa_String lsa_acct_name
;
1000 if (argc
< 1 || c
->display_usage
) {
1001 rpc_user_usage(c
, argc
, argv
);
1002 return NT_STATUS_OK
;
1004 /* Get sam policy handle */
1006 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1008 MAXIMUM_ALLOWED_ACCESS
,
1010 if (!NT_STATUS_IS_OK(result
)) goto done
;
1012 /* Get domain policy handle */
1014 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1016 MAXIMUM_ALLOWED_ACCESS
,
1017 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1019 if (!NT_STATUS_IS_OK(result
)) goto done
;
1021 /* Get handle on user */
1023 init_lsa_String(&lsa_acct_name
, argv
[0]);
1025 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1032 if (!NT_STATUS_IS_OK(result
)) goto done
;
1034 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1036 MAXIMUM_ALLOWED_ACCESS
,
1039 if (!NT_STATUS_IS_OK(result
)) goto done
;
1041 result
= rpccli_samr_GetGroupsForUser(pipe_hnd
, mem_ctx
,
1045 if (!NT_STATUS_IS_OK(result
)) goto done
;
1049 if (rid_array
->count
) {
1050 if ((lrids
= TALLOC_ARRAY(mem_ctx
, uint32
, rid_array
->count
)) == NULL
) {
1051 result
= NT_STATUS_NO_MEMORY
;
1055 for (i
= 0; i
< rid_array
->count
; i
++)
1056 lrids
[i
] = rid_array
->rids
[i
].rid
;
1058 result
= rpccli_samr_LookupRids(pipe_hnd
, mem_ctx
,
1065 if (!NT_STATUS_IS_OK(result
)) {
1069 /* Display results */
1071 for (i
= 0; i
< names
.count
; i
++)
1072 printf("%s\n", names
.names
[i
].string
);
1079 * List a user's groups from a remote RPC server.
1081 * @param argc Standard main() style argc.
1082 * @param argv Standard main() style argv. Initial components are already
1085 * @return A shell status integer (0 for success)
1088 static int rpc_user_info(struct net_context
*c
, int argc
, const char **argv
)
1090 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
1091 rpc_user_info_internals
, argc
, argv
);
1095 * List users on a remote RPC server.
1097 * All parameters are provided by the run_rpc_command function, except for
1098 * argc, argv which are passed through.
1100 * @param domain_sid The domain sid acquired from the remote server.
1101 * @param cli A cli_state connected to the server.
1102 * @param mem_ctx Talloc context, destroyed on completion of the function.
1103 * @param argc Standard main() style argc.
1104 * @param argv Standard main() style argv. Initial components are already
1107 * @return Normal NTSTATUS return.
1110 static NTSTATUS
rpc_user_list_internals(struct net_context
*c
,
1111 const DOM_SID
*domain_sid
,
1112 const char *domain_name
,
1113 struct cli_state
*cli
,
1114 struct rpc_pipe_client
*pipe_hnd
,
1115 TALLOC_CTX
*mem_ctx
,
1119 POLICY_HND connect_pol
, domain_pol
;
1120 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1121 uint32 start_idx
=0, num_entries
, i
, loop_count
= 0;
1123 /* Get sam policy handle */
1125 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1127 MAXIMUM_ALLOWED_ACCESS
,
1129 if (!NT_STATUS_IS_OK(result
)) {
1133 /* Get domain policy handle */
1135 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1137 MAXIMUM_ALLOWED_ACCESS
,
1138 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1140 if (!NT_STATUS_IS_OK(result
)) {
1144 /* Query domain users */
1145 if (c
->opt_long_list_entries
)
1146 d_printf("\nUser name Comment"
1147 "\n-----------------------------\n");
1149 const char *user
= NULL
;
1150 const char *desc
= NULL
;
1151 uint32 max_entries
, max_size
;
1152 uint32_t total_size
, returned_size
;
1153 union samr_DispInfo info
;
1155 get_query_dispinfo_params(
1156 loop_count
, &max_entries
, &max_size
);
1158 result
= rpccli_samr_QueryDisplayInfo(pipe_hnd
, mem_ctx
,
1168 start_idx
+= info
.info1
.count
;
1169 num_entries
= info
.info1
.count
;
1171 for (i
= 0; i
< num_entries
; i
++) {
1172 user
= info
.info1
.entries
[i
].account_name
.string
;
1173 if (c
->opt_long_list_entries
)
1174 desc
= info
.info1
.entries
[i
].description
.string
;
1175 if (c
->opt_long_list_entries
)
1176 printf("%-21.21s %s\n", user
, desc
);
1178 printf("%s\n", user
);
1180 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
1187 * 'net rpc user' entrypoint.
1188 * @param argc Standard main() style argc.
1189 * @param argv Standard main() style argv. Initial components are already
1193 int net_rpc_user(struct net_context
*c
, int argc
, const char **argv
)
1195 NET_API_STATUS status
;
1197 struct functable func
[] = {
1202 "Add specified user",
1203 "net rpc user add\n"
1204 " Add specified user"
1210 "List domain groups of user",
1211 "net rpc user info\n"
1212 " Lis domain groups of user"
1218 "Remove specified user",
1219 "net rpc user delete\n"
1220 " Remove specified user"
1226 "Change user password",
1227 "net rpc user password\n"
1228 " Change user password"
1234 "Rename specified user",
1235 "net rpc user rename\n"
1236 " Rename specified user"
1238 {NULL
, NULL
, 0, NULL
, NULL
}
1241 status
= libnetapi_init(&c
->netapi_ctx
);
1245 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
1246 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
1249 if (c
->display_usage
) {
1250 d_printf("Usage:\n");
1251 d_printf("net rpc user\n"
1252 " List all users\n");
1253 net_display_usage_from_functable(func
);
1257 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
1258 rpc_user_list_internals
,
1262 return net_run_function(c
, argc
, argv
, "net rpc user", func
);
1265 static NTSTATUS
rpc_sh_user_list(struct net_context
*c
,
1266 TALLOC_CTX
*mem_ctx
,
1267 struct rpc_sh_ctx
*ctx
,
1268 struct rpc_pipe_client
*pipe_hnd
,
1269 int argc
, const char **argv
)
1271 return rpc_user_list_internals(c
, ctx
->domain_sid
, ctx
->domain_name
,
1272 ctx
->cli
, pipe_hnd
, mem_ctx
,
1276 static NTSTATUS
rpc_sh_user_info(struct net_context
*c
,
1277 TALLOC_CTX
*mem_ctx
,
1278 struct rpc_sh_ctx
*ctx
,
1279 struct rpc_pipe_client
*pipe_hnd
,
1280 int argc
, const char **argv
)
1282 return rpc_user_info_internals(c
, ctx
->domain_sid
, ctx
->domain_name
,
1283 ctx
->cli
, pipe_hnd
, mem_ctx
,
1287 static NTSTATUS
rpc_sh_handle_user(struct net_context
*c
,
1288 TALLOC_CTX
*mem_ctx
,
1289 struct rpc_sh_ctx
*ctx
,
1290 struct rpc_pipe_client
*pipe_hnd
,
1291 int argc
, const char **argv
,
1293 struct net_context
*c
,
1294 TALLOC_CTX
*mem_ctx
,
1295 struct rpc_sh_ctx
*ctx
,
1296 struct rpc_pipe_client
*pipe_hnd
,
1297 POLICY_HND
*user_hnd
,
1298 int argc
, const char **argv
))
1300 POLICY_HND connect_pol
, domain_pol
, user_pol
;
1301 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1304 enum lsa_SidType type
;
1307 d_fprintf(stderr
, "usage: %s <username>\n", ctx
->whoami
);
1308 return NT_STATUS_INVALID_PARAMETER
;
1311 ZERO_STRUCT(connect_pol
);
1312 ZERO_STRUCT(domain_pol
);
1313 ZERO_STRUCT(user_pol
);
1315 result
= net_rpc_lookup_name(c
, mem_ctx
, rpc_pipe_np_smb_conn(pipe_hnd
),
1316 argv
[0], NULL
, NULL
, &sid
, &type
);
1317 if (!NT_STATUS_IS_OK(result
)) {
1318 d_fprintf(stderr
, "Could not lookup %s: %s\n", argv
[0],
1323 if (type
!= SID_NAME_USER
) {
1324 d_fprintf(stderr
, "%s is a %s, not a user\n", argv
[0],
1325 sid_type_lookup(type
));
1326 result
= NT_STATUS_NO_SUCH_USER
;
1330 if (!sid_peek_check_rid(ctx
->domain_sid
, &sid
, &rid
)) {
1331 d_fprintf(stderr
, "%s is not in our domain\n", argv
[0]);
1332 result
= NT_STATUS_NO_SUCH_USER
;
1336 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1338 MAXIMUM_ALLOWED_ACCESS
,
1340 if (!NT_STATUS_IS_OK(result
)) {
1344 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1346 MAXIMUM_ALLOWED_ACCESS
,
1349 if (!NT_STATUS_IS_OK(result
)) {
1353 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1355 MAXIMUM_ALLOWED_ACCESS
,
1358 if (!NT_STATUS_IS_OK(result
)) {
1362 result
= fn(c
, mem_ctx
, ctx
, pipe_hnd
, &user_pol
, argc
-1, argv
+1);
1365 if (is_valid_policy_hnd(&user_pol
)) {
1366 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1368 if (is_valid_policy_hnd(&domain_pol
)) {
1369 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
1371 if (is_valid_policy_hnd(&connect_pol
)) {
1372 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
1377 static NTSTATUS
rpc_sh_user_show_internals(struct net_context
*c
,
1378 TALLOC_CTX
*mem_ctx
,
1379 struct rpc_sh_ctx
*ctx
,
1380 struct rpc_pipe_client
*pipe_hnd
,
1381 POLICY_HND
*user_hnd
,
1382 int argc
, const char **argv
)
1385 union samr_UserInfo
*info
= NULL
;
1388 d_fprintf(stderr
, "usage: %s show <username>\n", ctx
->whoami
);
1389 return NT_STATUS_INVALID_PARAMETER
;
1392 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1396 if (!NT_STATUS_IS_OK(result
)) {
1400 d_printf("user rid: %d, group rid: %d\n",
1402 info
->info21
.primary_gid
);
1407 static NTSTATUS
rpc_sh_user_show(struct net_context
*c
,
1408 TALLOC_CTX
*mem_ctx
,
1409 struct rpc_sh_ctx
*ctx
,
1410 struct rpc_pipe_client
*pipe_hnd
,
1411 int argc
, const char **argv
)
1413 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1414 rpc_sh_user_show_internals
);
1417 #define FETCHSTR(name, rec) \
1418 do { if (strequal(ctx->thiscmd, name)) { \
1419 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1422 #define SETSTR(name, rec, flag) \
1423 do { if (strequal(ctx->thiscmd, name)) { \
1424 init_lsa_String(&(info->info21.rec), argv[0]); \
1425 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1428 static NTSTATUS
rpc_sh_user_str_edit_internals(struct net_context
*c
,
1429 TALLOC_CTX
*mem_ctx
,
1430 struct rpc_sh_ctx
*ctx
,
1431 struct rpc_pipe_client
*pipe_hnd
,
1432 POLICY_HND
*user_hnd
,
1433 int argc
, const char **argv
)
1436 const char *username
;
1437 const char *oldval
= "";
1438 union samr_UserInfo
*info
= NULL
;
1441 d_fprintf(stderr
, "usage: %s <username> [new value|NULL]\n",
1443 return NT_STATUS_INVALID_PARAMETER
;
1446 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1450 if (!NT_STATUS_IS_OK(result
)) {
1454 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1456 FETCHSTR("fullname", full_name
);
1457 FETCHSTR("homedir", home_directory
);
1458 FETCHSTR("homedrive", home_drive
);
1459 FETCHSTR("logonscript", logon_script
);
1460 FETCHSTR("profilepath", profile_path
);
1461 FETCHSTR("description", description
);
1464 d_printf("%s's %s: [%s]\n", username
, ctx
->thiscmd
, oldval
);
1468 if (strcmp(argv
[0], "NULL") == 0) {
1472 ZERO_STRUCT(info
->info21
);
1474 SETSTR("fullname", full_name
, FULL_NAME
);
1475 SETSTR("homedir", home_directory
, HOME_DIRECTORY
);
1476 SETSTR("homedrive", home_drive
, HOME_DRIVE
);
1477 SETSTR("logonscript", logon_script
, LOGON_SCRIPT
);
1478 SETSTR("profilepath", profile_path
, PROFILE_PATH
);
1479 SETSTR("description", description
, DESCRIPTION
);
1481 result
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1486 d_printf("Set %s's %s from [%s] to [%s]\n", username
,
1487 ctx
->thiscmd
, oldval
, argv
[0]);
1494 #define HANDLEFLG(name, rec) \
1495 do { if (strequal(ctx->thiscmd, name)) { \
1496 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1498 newflags = oldflags | ACB_##rec; \
1500 newflags = oldflags & ~ACB_##rec; \
1503 static NTSTATUS
rpc_sh_user_str_edit(struct net_context
*c
,
1504 TALLOC_CTX
*mem_ctx
,
1505 struct rpc_sh_ctx
*ctx
,
1506 struct rpc_pipe_client
*pipe_hnd
,
1507 int argc
, const char **argv
)
1509 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1510 rpc_sh_user_str_edit_internals
);
1513 static NTSTATUS
rpc_sh_user_flag_edit_internals(struct net_context
*c
,
1514 TALLOC_CTX
*mem_ctx
,
1515 struct rpc_sh_ctx
*ctx
,
1516 struct rpc_pipe_client
*pipe_hnd
,
1517 POLICY_HND
*user_hnd
,
1518 int argc
, const char **argv
)
1521 const char *username
;
1522 const char *oldval
= "unknown";
1523 uint32 oldflags
, newflags
;
1525 union samr_UserInfo
*info
= NULL
;
1528 ((argc
== 1) && !strequal(argv
[0], "yes") &&
1529 !strequal(argv
[0], "no"))) {
1530 d_fprintf(stderr
, "usage: %s <username> [yes|no]\n",
1532 return NT_STATUS_INVALID_PARAMETER
;
1535 newval
= strequal(argv
[0], "yes");
1537 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1541 if (!NT_STATUS_IS_OK(result
)) {
1545 username
= talloc_strdup(mem_ctx
, info
->info21
.account_name
.string
);
1546 oldflags
= info
->info21
.acct_flags
;
1547 newflags
= info
->info21
.acct_flags
;
1549 HANDLEFLG("disabled", DISABLED
);
1550 HANDLEFLG("pwnotreq", PWNOTREQ
);
1551 HANDLEFLG("autolock", AUTOLOCK
);
1552 HANDLEFLG("pwnoexp", PWNOEXP
);
1555 d_printf("%s's %s flag: %s\n", username
, ctx
->thiscmd
, oldval
);
1559 ZERO_STRUCT(info
->info21
);
1561 info
->info21
.acct_flags
= newflags
;
1562 info
->info21
.fields_present
= SAMR_FIELD_ACCT_FLAGS
;
1564 result
= rpccli_samr_SetUserInfo(pipe_hnd
, mem_ctx
,
1569 if (NT_STATUS_IS_OK(result
)) {
1570 d_printf("Set %s's %s flag from [%s] to [%s]\n", username
,
1571 ctx
->thiscmd
, oldval
, argv
[0]);
1579 static NTSTATUS
rpc_sh_user_flag_edit(struct net_context
*c
,
1580 TALLOC_CTX
*mem_ctx
,
1581 struct rpc_sh_ctx
*ctx
,
1582 struct rpc_pipe_client
*pipe_hnd
,
1583 int argc
, const char **argv
)
1585 return rpc_sh_handle_user(c
, mem_ctx
, ctx
, pipe_hnd
, argc
, argv
,
1586 rpc_sh_user_flag_edit_internals
);
1589 struct rpc_sh_cmd
*net_rpc_user_edit_cmds(struct net_context
*c
,
1590 TALLOC_CTX
*mem_ctx
,
1591 struct rpc_sh_ctx
*ctx
)
1593 static struct rpc_sh_cmd cmds
[] = {
1595 { "fullname", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1596 "Show/Set a user's full name" },
1598 { "homedir", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1599 "Show/Set a user's home directory" },
1601 { "homedrive", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1602 "Show/Set a user's home drive" },
1604 { "logonscript", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1605 "Show/Set a user's logon script" },
1607 { "profilepath", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1608 "Show/Set a user's profile path" },
1610 { "description", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_str_edit
,
1611 "Show/Set a user's description" },
1613 { "disabled", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1614 "Show/Set whether a user is disabled" },
1616 { "autolock", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1617 "Show/Set whether a user locked out" },
1619 { "pwnotreq", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1620 "Show/Set whether a user does not need a password" },
1622 { "pwnoexp", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_flag_edit
,
1623 "Show/Set whether a user's password does not expire" },
1625 { NULL
, NULL
, 0, NULL
, NULL
}
1631 struct rpc_sh_cmd
*net_rpc_user_cmds(struct net_context
*c
,
1632 TALLOC_CTX
*mem_ctx
,
1633 struct rpc_sh_ctx
*ctx
)
1635 static struct rpc_sh_cmd cmds
[] = {
1637 { "list", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_list
,
1638 "List available users" },
1640 { "info", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_info
,
1641 "List the domain groups a user is member of" },
1643 { "show", NULL
, &ndr_table_samr
.syntax_id
, rpc_sh_user_show
,
1644 "Show info about a user" },
1646 { "edit", net_rpc_user_edit_cmds
, 0, NULL
,
1647 "Show/Modify a user's fields" },
1649 { NULL
, NULL
, 0, NULL
, NULL
}
1655 /****************************************************************************/
1658 * Basic usage function for 'net rpc group'.
1659 * @param argc Standard main() style argc.
1660 * @param argv Standard main() style argv. Initial components are already
1664 static int rpc_group_usage(struct net_context
*c
, int argc
, const char **argv
)
1666 return net_group_usage(c
, argc
, argv
);
1670 * Delete group on a remote RPC server.
1672 * All parameters are provided by the run_rpc_command function, except for
1673 * argc, argv which are passed through.
1675 * @param domain_sid The domain sid acquired from the remote server.
1676 * @param cli A cli_state connected to the server.
1677 * @param mem_ctx Talloc context, destroyed on completion of the function.
1678 * @param argc Standard main() style argc.
1679 * @param argv Standard main() style argv. Initial components are already
1682 * @return Normal NTSTATUS return.
1685 static NTSTATUS
rpc_group_delete_internals(struct net_context
*c
,
1686 const DOM_SID
*domain_sid
,
1687 const char *domain_name
,
1688 struct cli_state
*cli
,
1689 struct rpc_pipe_client
*pipe_hnd
,
1690 TALLOC_CTX
*mem_ctx
,
1694 POLICY_HND connect_pol
, domain_pol
, group_pol
, user_pol
;
1695 bool group_is_primary
= false;
1696 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1698 struct samr_RidTypeArray
*rids
= NULL
;
1701 /* DOM_GID *user_gids; */
1703 struct samr_Ids group_rids
, name_types
;
1704 struct lsa_String lsa_acct_name
;
1705 union samr_UserInfo
*info
= NULL
;
1707 if (argc
< 1 || c
->display_usage
) {
1708 rpc_group_usage(c
, argc
,argv
);
1709 return NT_STATUS_OK
; /* ok? */
1712 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1714 MAXIMUM_ALLOWED_ACCESS
,
1717 if (!NT_STATUS_IS_OK(result
)) {
1718 d_fprintf(stderr
, "Request samr_Connect2 failed\n");
1722 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1724 MAXIMUM_ALLOWED_ACCESS
,
1725 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1728 if (!NT_STATUS_IS_OK(result
)) {
1729 d_fprintf(stderr
, "Request open_domain failed\n");
1733 init_lsa_String(&lsa_acct_name
, argv
[0]);
1735 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
1741 if (!NT_STATUS_IS_OK(result
)) {
1742 d_fprintf(stderr
, "Lookup of '%s' failed\n",argv
[0]);
1746 switch (name_types
.ids
[0])
1748 case SID_NAME_DOM_GRP
:
1749 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
1751 MAXIMUM_ALLOWED_ACCESS
,
1754 if (!NT_STATUS_IS_OK(result
)) {
1755 d_fprintf(stderr
, "Request open_group failed");
1759 group_rid
= group_rids
.ids
[0];
1761 result
= rpccli_samr_QueryGroupMember(pipe_hnd
, mem_ctx
,
1765 if (!NT_STATUS_IS_OK(result
)) {
1766 d_fprintf(stderr
, "Unable to query group members of %s",argv
[0]);
1770 if (c
->opt_verbose
) {
1771 d_printf("Domain Group %s (rid: %d) has %d members\n",
1772 argv
[0],group_rid
, rids
->count
);
1775 /* Check if group is anyone's primary group */
1776 for (i
= 0; i
< rids
->count
; i
++)
1778 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
1780 MAXIMUM_ALLOWED_ACCESS
,
1784 if (!NT_STATUS_IS_OK(result
)) {
1785 d_fprintf(stderr
, "Unable to open group member %d\n",
1790 result
= rpccli_samr_QueryUserInfo(pipe_hnd
, mem_ctx
,
1795 if (!NT_STATUS_IS_OK(result
)) {
1796 d_fprintf(stderr
, "Unable to lookup userinfo for group member %d\n",
1801 if (info
->info21
.primary_gid
== group_rid
) {
1802 if (c
->opt_verbose
) {
1803 d_printf("Group is primary group of %s\n",
1804 info
->info21
.account_name
.string
);
1806 group_is_primary
= true;
1809 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &user_pol
);
1812 if (group_is_primary
) {
1813 d_fprintf(stderr
, "Unable to delete group because some "
1814 "of it's members have it as primary group\n");
1815 result
= NT_STATUS_MEMBERS_PRIMARY_GROUP
;
1819 /* remove all group members */
1820 for (i
= 0; i
< rids
->count
; i
++)
1823 d_printf("Remove group member %d...",
1825 result
= rpccli_samr_DeleteGroupMember(pipe_hnd
, mem_ctx
,
1829 if (NT_STATUS_IS_OK(result
)) {
1834 d_printf("failed\n");
1839 result
= rpccli_samr_DeleteDomainGroup(pipe_hnd
, mem_ctx
,
1843 /* removing a local group is easier... */
1844 case SID_NAME_ALIAS
:
1845 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
1847 MAXIMUM_ALLOWED_ACCESS
,
1851 if (!NT_STATUS_IS_OK(result
)) {
1852 d_fprintf(stderr
, "Request open_alias failed\n");
1856 result
= rpccli_samr_DeleteDomAlias(pipe_hnd
, mem_ctx
,
1860 d_fprintf(stderr
, "%s is of type %s. This command is only for deleting local or global groups\n",
1861 argv
[0],sid_type_lookup(name_types
.ids
[0]));
1862 result
= NT_STATUS_UNSUCCESSFUL
;
1866 if (NT_STATUS_IS_OK(result
)) {
1868 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types
.ids
[0]),argv
[0]);
1870 d_fprintf(stderr
, "Deleting of %s failed: %s\n",argv
[0],
1871 get_friendly_nt_error_msg(result
));
1879 static int rpc_group_delete(struct net_context
*c
, int argc
, const char **argv
)
1881 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
1882 rpc_group_delete_internals
, argc
,argv
);
1885 static int rpc_group_add_internals(struct net_context
*c
, int argc
, const char **argv
)
1887 NET_API_STATUS status
;
1888 struct GROUP_INFO_1 info1
;
1889 uint32_t parm_error
= 0;
1891 if (argc
!= 1 || c
->display_usage
) {
1892 rpc_group_usage(c
, argc
, argv
);
1898 info1
.grpi1_name
= argv
[0];
1899 if (c
->opt_comment
&& strlen(c
->opt_comment
) > 0) {
1900 info1
.grpi1_comment
= c
->opt_comment
;
1903 status
= NetGroupAdd(c
->opt_host
, 1, (uint8_t *)&info1
, &parm_error
);
1906 d_fprintf(stderr
, "Failed to add group '%s' with: %s.\n",
1907 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
1911 d_printf("Added group '%s'.\n", argv
[0]);
1917 static NTSTATUS
rpc_alias_add_internals(struct net_context
*c
,
1918 const DOM_SID
*domain_sid
,
1919 const char *domain_name
,
1920 struct cli_state
*cli
,
1921 struct rpc_pipe_client
*pipe_hnd
,
1922 TALLOC_CTX
*mem_ctx
,
1926 POLICY_HND connect_pol
, domain_pol
, alias_pol
;
1927 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
1928 union samr_AliasInfo alias_info
;
1929 struct lsa_String alias_name
;
1932 if (argc
!= 1 || c
->display_usage
) {
1933 rpc_group_usage(c
, argc
, argv
);
1934 return NT_STATUS_OK
;
1937 init_lsa_String(&alias_name
, argv
[0]);
1939 /* Get sam policy handle */
1941 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
1943 MAXIMUM_ALLOWED_ACCESS
,
1945 if (!NT_STATUS_IS_OK(result
)) goto done
;
1947 /* Get domain policy handle */
1949 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
1951 MAXIMUM_ALLOWED_ACCESS
,
1952 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
1954 if (!NT_STATUS_IS_OK(result
)) goto done
;
1956 /* Create the group */
1958 result
= rpccli_samr_CreateDomAlias(pipe_hnd
, mem_ctx
,
1961 MAXIMUM_ALLOWED_ACCESS
,
1964 if (!NT_STATUS_IS_OK(result
)) goto done
;
1966 if (strlen(c
->opt_comment
) == 0) goto done
;
1968 /* We've got a comment to set */
1970 init_lsa_String(&alias_info
.description
, c
->opt_comment
);
1972 result
= rpccli_samr_SetAliasInfo(pipe_hnd
, mem_ctx
,
1977 if (!NT_STATUS_IS_OK(result
)) goto done
;
1980 if (NT_STATUS_IS_OK(result
))
1981 DEBUG(5, ("add alias succeeded\n"));
1983 d_fprintf(stderr
, "add alias failed: %s\n", nt_errstr(result
));
1988 static int rpc_group_add(struct net_context
*c
, int argc
, const char **argv
)
1990 if (c
->opt_localgroup
)
1991 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
1992 rpc_alias_add_internals
,
1995 return rpc_group_add_internals(c
, argc
, argv
);
1998 static NTSTATUS
get_sid_from_name(struct cli_state
*cli
,
1999 TALLOC_CTX
*mem_ctx
,
2002 enum lsa_SidType
*type
)
2004 DOM_SID
*sids
= NULL
;
2005 enum lsa_SidType
*types
= NULL
;
2006 struct rpc_pipe_client
*pipe_hnd
;
2008 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2010 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
2012 if (!NT_STATUS_IS_OK(result
)) {
2016 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, false,
2017 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
2019 if (!NT_STATUS_IS_OK(result
)) {
2023 result
= rpccli_lsa_lookup_names(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
2024 &name
, NULL
, 1, &sids
, &types
);
2026 if (NT_STATUS_IS_OK(result
)) {
2027 sid_copy(sid
, &sids
[0]);
2031 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
2035 TALLOC_FREE(pipe_hnd
);
2038 if (!NT_STATUS_IS_OK(result
) && (StrnCaseCmp(name
, "S-", 2) == 0)) {
2040 /* Try as S-1-5-whatever */
2044 if (string_to_sid(&tmp_sid
, name
)) {
2045 sid_copy(sid
, &tmp_sid
);
2046 *type
= SID_NAME_UNKNOWN
;
2047 result
= NT_STATUS_OK
;
2054 static NTSTATUS
rpc_add_groupmem(struct rpc_pipe_client
*pipe_hnd
,
2055 TALLOC_CTX
*mem_ctx
,
2056 const DOM_SID
*group_sid
,
2059 POLICY_HND connect_pol
, domain_pol
;
2062 POLICY_HND group_pol
;
2064 struct samr_Ids rids
, rid_types
;
2065 struct lsa_String lsa_acct_name
;
2069 sid_copy(&sid
, group_sid
);
2071 if (!sid_split_rid(&sid
, &group_rid
)) {
2072 return NT_STATUS_UNSUCCESSFUL
;
2075 /* Get sam policy handle */
2076 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2078 MAXIMUM_ALLOWED_ACCESS
,
2080 if (!NT_STATUS_IS_OK(result
)) {
2084 /* Get domain policy handle */
2085 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2087 MAXIMUM_ALLOWED_ACCESS
,
2090 if (!NT_STATUS_IS_OK(result
)) {
2094 init_lsa_String(&lsa_acct_name
, member
);
2096 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2103 if (!NT_STATUS_IS_OK(result
)) {
2104 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2108 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2110 MAXIMUM_ALLOWED_ACCESS
,
2114 if (!NT_STATUS_IS_OK(result
)) {
2118 result
= rpccli_samr_AddGroupMember(pipe_hnd
, mem_ctx
,
2121 0x0005); /* unknown flags */
2124 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2128 static NTSTATUS
rpc_add_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2129 TALLOC_CTX
*mem_ctx
,
2130 const DOM_SID
*alias_sid
,
2133 POLICY_HND connect_pol
, domain_pol
;
2136 POLICY_HND alias_pol
;
2139 enum lsa_SidType member_type
;
2143 sid_copy(&sid
, alias_sid
);
2145 if (!sid_split_rid(&sid
, &alias_rid
)) {
2146 return NT_STATUS_UNSUCCESSFUL
;
2149 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2150 member
, &member_sid
, &member_type
);
2152 if (!NT_STATUS_IS_OK(result
)) {
2153 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2157 /* Get sam policy handle */
2158 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2160 MAXIMUM_ALLOWED_ACCESS
,
2162 if (!NT_STATUS_IS_OK(result
)) {
2166 /* Get domain policy handle */
2167 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2169 MAXIMUM_ALLOWED_ACCESS
,
2172 if (!NT_STATUS_IS_OK(result
)) {
2176 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2178 MAXIMUM_ALLOWED_ACCESS
,
2182 if (!NT_STATUS_IS_OK(result
)) {
2186 result
= rpccli_samr_AddAliasMember(pipe_hnd
, mem_ctx
,
2190 if (!NT_STATUS_IS_OK(result
)) {
2195 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2199 static NTSTATUS
rpc_group_addmem_internals(struct net_context
*c
,
2200 const DOM_SID
*domain_sid
,
2201 const char *domain_name
,
2202 struct cli_state
*cli
,
2203 struct rpc_pipe_client
*pipe_hnd
,
2204 TALLOC_CTX
*mem_ctx
,
2209 enum lsa_SidType group_type
;
2211 if (argc
!= 2 || c
->display_usage
) {
2213 "net rpc group addmem <group> <member>\n"
2214 " Add a member to a group\n"
2215 " group\tGroup to add member to\n"
2216 " member\tMember to add to group\n");
2217 return NT_STATUS_UNSUCCESSFUL
;
2220 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2221 &group_sid
, &group_type
))) {
2222 d_fprintf(stderr
, "Could not lookup group name %s\n", argv
[0]);
2223 return NT_STATUS_UNSUCCESSFUL
;
2226 if (group_type
== SID_NAME_DOM_GRP
) {
2227 NTSTATUS result
= rpc_add_groupmem(pipe_hnd
, mem_ctx
,
2228 &group_sid
, argv
[1]);
2230 if (!NT_STATUS_IS_OK(result
)) {
2231 d_fprintf(stderr
, "Could not add %s to %s: %s\n",
2232 argv
[1], argv
[0], nt_errstr(result
));
2237 if (group_type
== SID_NAME_ALIAS
) {
2238 NTSTATUS result
= rpc_add_aliasmem(pipe_hnd
, mem_ctx
,
2239 &group_sid
, argv
[1]);
2241 if (!NT_STATUS_IS_OK(result
)) {
2242 d_fprintf(stderr
, "Could not add %s to %s: %s\n",
2243 argv
[1], argv
[0], nt_errstr(result
));
2248 d_fprintf(stderr
, "Can only add members to global or local groups "
2249 "which %s is not\n", argv
[0]);
2251 return NT_STATUS_UNSUCCESSFUL
;
2254 static int rpc_group_addmem(struct net_context
*c
, int argc
, const char **argv
)
2256 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2257 rpc_group_addmem_internals
,
2261 static NTSTATUS
rpc_del_groupmem(struct net_context
*c
,
2262 struct rpc_pipe_client
*pipe_hnd
,
2263 TALLOC_CTX
*mem_ctx
,
2264 const DOM_SID
*group_sid
,
2267 POLICY_HND connect_pol
, domain_pol
;
2270 POLICY_HND group_pol
;
2272 struct samr_Ids rids
, rid_types
;
2273 struct lsa_String lsa_acct_name
;
2277 sid_copy(&sid
, group_sid
);
2279 if (!sid_split_rid(&sid
, &group_rid
))
2280 return NT_STATUS_UNSUCCESSFUL
;
2282 /* Get sam policy handle */
2283 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2285 MAXIMUM_ALLOWED_ACCESS
,
2287 if (!NT_STATUS_IS_OK(result
))
2290 /* Get domain policy handle */
2291 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2293 MAXIMUM_ALLOWED_ACCESS
,
2296 if (!NT_STATUS_IS_OK(result
))
2299 init_lsa_String(&lsa_acct_name
, member
);
2301 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2307 if (!NT_STATUS_IS_OK(result
)) {
2308 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2312 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2314 MAXIMUM_ALLOWED_ACCESS
,
2318 if (!NT_STATUS_IS_OK(result
))
2321 result
= rpccli_samr_DeleteGroupMember(pipe_hnd
, mem_ctx
,
2326 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2330 static NTSTATUS
rpc_del_aliasmem(struct rpc_pipe_client
*pipe_hnd
,
2331 TALLOC_CTX
*mem_ctx
,
2332 const DOM_SID
*alias_sid
,
2335 POLICY_HND connect_pol
, domain_pol
;
2338 POLICY_HND alias_pol
;
2341 enum lsa_SidType member_type
;
2345 sid_copy(&sid
, alias_sid
);
2347 if (!sid_split_rid(&sid
, &alias_rid
))
2348 return NT_STATUS_UNSUCCESSFUL
;
2350 result
= get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd
), mem_ctx
,
2351 member
, &member_sid
, &member_type
);
2353 if (!NT_STATUS_IS_OK(result
)) {
2354 d_fprintf(stderr
, "Could not lookup up group member %s\n", member
);
2358 /* Get sam policy handle */
2359 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2361 MAXIMUM_ALLOWED_ACCESS
,
2363 if (!NT_STATUS_IS_OK(result
)) {
2367 /* Get domain policy handle */
2368 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2370 MAXIMUM_ALLOWED_ACCESS
,
2373 if (!NT_STATUS_IS_OK(result
)) {
2377 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2379 MAXIMUM_ALLOWED_ACCESS
,
2383 if (!NT_STATUS_IS_OK(result
))
2386 result
= rpccli_samr_DeleteAliasMember(pipe_hnd
, mem_ctx
,
2390 if (!NT_STATUS_IS_OK(result
))
2394 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
2398 static NTSTATUS
rpc_group_delmem_internals(struct net_context
*c
,
2399 const DOM_SID
*domain_sid
,
2400 const char *domain_name
,
2401 struct cli_state
*cli
,
2402 struct rpc_pipe_client
*pipe_hnd
,
2403 TALLOC_CTX
*mem_ctx
,
2408 enum lsa_SidType group_type
;
2410 if (argc
!= 2 || c
->display_usage
) {
2412 "net rpc group delmem <group> <member>\n"
2413 " Delete a member from a group\n"
2414 " group\tGroup to delete member from\n"
2415 " member\tMember to delete from group\n");
2416 return NT_STATUS_UNSUCCESSFUL
;
2419 if (!NT_STATUS_IS_OK(get_sid_from_name(cli
, mem_ctx
, argv
[0],
2420 &group_sid
, &group_type
))) {
2421 d_fprintf(stderr
, "Could not lookup group name %s\n", argv
[0]);
2422 return NT_STATUS_UNSUCCESSFUL
;
2425 if (group_type
== SID_NAME_DOM_GRP
) {
2426 NTSTATUS result
= rpc_del_groupmem(c
, pipe_hnd
, mem_ctx
,
2427 &group_sid
, argv
[1]);
2429 if (!NT_STATUS_IS_OK(result
)) {
2430 d_fprintf(stderr
, "Could not del %s from %s: %s\n",
2431 argv
[1], argv
[0], nt_errstr(result
));
2436 if (group_type
== SID_NAME_ALIAS
) {
2437 NTSTATUS result
= rpc_del_aliasmem(pipe_hnd
, mem_ctx
,
2438 &group_sid
, argv
[1]);
2440 if (!NT_STATUS_IS_OK(result
)) {
2441 d_fprintf(stderr
, "Could not del %s from %s: %s\n",
2442 argv
[1], argv
[0], nt_errstr(result
));
2447 d_fprintf(stderr
, "Can only delete members from global or local groups "
2448 "which %s is not\n", argv
[0]);
2450 return NT_STATUS_UNSUCCESSFUL
;
2453 static int rpc_group_delmem(struct net_context
*c
, int argc
, const char **argv
)
2455 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2456 rpc_group_delmem_internals
,
2461 * List groups on a remote RPC server.
2463 * All parameters are provided by the run_rpc_command function, except for
2464 * argc, argv which are passes through.
2466 * @param domain_sid The domain sid acquired from the remote server.
2467 * @param cli A cli_state connected to the server.
2468 * @param mem_ctx Talloc context, destroyed on completion of the function.
2469 * @param argc Standard main() style argc.
2470 * @param argv Standard main() style argv. Initial components are already
2473 * @return Normal NTSTATUS return.
2476 static NTSTATUS
rpc_group_list_internals(struct net_context
*c
,
2477 const DOM_SID
*domain_sid
,
2478 const char *domain_name
,
2479 struct cli_state
*cli
,
2480 struct rpc_pipe_client
*pipe_hnd
,
2481 TALLOC_CTX
*mem_ctx
,
2485 POLICY_HND connect_pol
, domain_pol
;
2486 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
2487 uint32 start_idx
=0, max_entries
=250, num_entries
, i
, loop_count
= 0;
2488 struct samr_SamArray
*groups
= NULL
;
2489 bool global
= false;
2491 bool builtin
= false;
2493 if (c
->display_usage
) {
2495 "net rpc group list [global] [local] [builtin]\n"
2496 " List groups on RPC server\n"
2497 " global\tList global groups\n"
2498 " local\tList local groups\n"
2499 " builtin\tList builtin groups\n"
2500 " If none of global, local or builtin is "
2501 "specified, all three options are considered set\n");
2502 return NT_STATUS_OK
;
2511 for (i
=0; i
<argc
; i
++) {
2512 if (strequal(argv
[i
], "global"))
2515 if (strequal(argv
[i
], "local"))
2518 if (strequal(argv
[i
], "builtin"))
2522 /* Get sam policy handle */
2524 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2526 MAXIMUM_ALLOWED_ACCESS
,
2528 if (!NT_STATUS_IS_OK(result
)) {
2532 /* Get domain policy handle */
2534 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2536 MAXIMUM_ALLOWED_ACCESS
,
2537 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2539 if (!NT_STATUS_IS_OK(result
)) {
2543 /* Query domain groups */
2544 if (c
->opt_long_list_entries
)
2545 d_printf("\nGroup name Comment"
2546 "\n-----------------------------\n");
2548 uint32_t max_size
, total_size
, returned_size
;
2549 union samr_DispInfo info
;
2553 get_query_dispinfo_params(
2554 loop_count
, &max_entries
, &max_size
);
2556 result
= rpccli_samr_QueryDisplayInfo(pipe_hnd
, mem_ctx
,
2565 num_entries
= info
.info3
.count
;
2566 start_idx
+= info
.info3
.count
;
2568 if (!NT_STATUS_IS_OK(result
) &&
2569 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2572 for (i
= 0; i
< num_entries
; i
++) {
2574 const char *group
= NULL
;
2575 const char *desc
= NULL
;
2577 group
= info
.info3
.entries
[i
].account_name
.string
;
2578 desc
= info
.info3
.entries
[i
].description
.string
;
2580 if (c
->opt_long_list_entries
)
2581 printf("%-21.21s %-50.50s\n",
2584 printf("%s\n", group
);
2586 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2587 /* query domain aliases */
2592 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
2598 if (!NT_STATUS_IS_OK(result
) &&
2599 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2602 for (i
= 0; i
< num_entries
; i
++) {
2604 const char *description
= NULL
;
2606 if (c
->opt_long_list_entries
) {
2608 POLICY_HND alias_pol
;
2609 union samr_AliasInfo
*info
= NULL
;
2611 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2614 groups
->entries
[i
].idx
,
2616 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd
, mem_ctx
,
2620 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd
, mem_ctx
,
2622 description
= info
->description
.string
;
2626 if (description
!= NULL
) {
2627 printf("%-21.21s %-50.50s\n",
2628 groups
->entries
[i
].name
.string
,
2631 printf("%s\n", groups
->entries
[i
].name
.string
);
2634 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2635 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
2636 /* Get builtin policy handle */
2638 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2640 MAXIMUM_ALLOWED_ACCESS
,
2641 CONST_DISCARD(struct dom_sid2
*, &global_sid_Builtin
),
2643 if (!NT_STATUS_IS_OK(result
)) {
2646 /* query builtin aliases */
2649 if (!builtin
) break;
2651 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
2657 if (!NT_STATUS_IS_OK(result
) &&
2658 !NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
))
2661 for (i
= 0; i
< num_entries
; i
++) {
2663 const char *description
= NULL
;
2665 if (c
->opt_long_list_entries
) {
2667 POLICY_HND alias_pol
;
2668 union samr_AliasInfo
*info
= NULL
;
2670 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2673 groups
->entries
[i
].idx
,
2675 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd
, mem_ctx
,
2679 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd
, mem_ctx
,
2681 description
= info
->description
.string
;
2685 if (description
!= NULL
) {
2686 printf("%-21.21s %-50.50s\n",
2687 groups
->entries
[i
].name
.string
,
2690 printf("%s\n", groups
->entries
[i
].name
.string
);
2693 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
2699 static int rpc_group_list(struct net_context
*c
, int argc
, const char **argv
)
2701 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2702 rpc_group_list_internals
,
2706 static NTSTATUS
rpc_list_group_members(struct net_context
*c
,
2707 struct rpc_pipe_client
*pipe_hnd
,
2708 TALLOC_CTX
*mem_ctx
,
2709 const char *domain_name
,
2710 const DOM_SID
*domain_sid
,
2711 POLICY_HND
*domain_pol
,
2715 POLICY_HND group_pol
;
2716 uint32 num_members
, *group_rids
;
2718 struct samr_RidTypeArray
*rids
= NULL
;
2719 struct lsa_Strings names
;
2720 struct samr_Ids types
;
2723 sid_to_fstring(sid_str
, domain_sid
);
2725 result
= rpccli_samr_OpenGroup(pipe_hnd
, mem_ctx
,
2727 MAXIMUM_ALLOWED_ACCESS
,
2731 if (!NT_STATUS_IS_OK(result
))
2734 result
= rpccli_samr_QueryGroupMember(pipe_hnd
, mem_ctx
,
2738 if (!NT_STATUS_IS_OK(result
))
2741 num_members
= rids
->count
;
2742 group_rids
= rids
->rids
;
2744 while (num_members
> 0) {
2745 int this_time
= 512;
2747 if (num_members
< this_time
)
2748 this_time
= num_members
;
2750 result
= rpccli_samr_LookupRids(pipe_hnd
, mem_ctx
,
2757 if (!NT_STATUS_IS_OK(result
))
2760 /* We only have users as members, but make the output
2761 the same as the output of alias members */
2763 for (i
= 0; i
< this_time
; i
++) {
2765 if (c
->opt_long_list_entries
) {
2766 printf("%s-%d %s\\%s %d\n", sid_str
,
2767 group_rids
[i
], domain_name
,
2768 names
.names
[i
].string
,
2771 printf("%s\\%s\n", domain_name
,
2772 names
.names
[i
].string
);
2776 num_members
-= this_time
;
2780 return NT_STATUS_OK
;
2783 static NTSTATUS
rpc_list_alias_members(struct net_context
*c
,
2784 struct rpc_pipe_client
*pipe_hnd
,
2785 TALLOC_CTX
*mem_ctx
,
2786 POLICY_HND
*domain_pol
,
2790 struct rpc_pipe_client
*lsa_pipe
;
2791 POLICY_HND alias_pol
, lsa_pol
;
2793 DOM_SID
*alias_sids
;
2796 enum lsa_SidType
*types
;
2798 struct lsa_SidArray sid_array
;
2800 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
2802 MAXIMUM_ALLOWED_ACCESS
,
2806 if (!NT_STATUS_IS_OK(result
))
2809 result
= rpccli_samr_GetMembersInAlias(pipe_hnd
, mem_ctx
,
2813 if (!NT_STATUS_IS_OK(result
)) {
2814 d_fprintf(stderr
, "Couldn't list alias members\n");
2818 num_members
= sid_array
.num_sids
;
2820 if (num_members
== 0) {
2821 return NT_STATUS_OK
;
2824 result
= cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd
),
2825 &ndr_table_lsarpc
.syntax_id
,
2827 if (!NT_STATUS_IS_OK(result
)) {
2828 d_fprintf(stderr
, "Couldn't open LSA pipe. Error was %s\n",
2829 nt_errstr(result
) );
2833 result
= rpccli_lsa_open_policy(lsa_pipe
, mem_ctx
, true,
2834 SEC_RIGHTS_MAXIMUM_ALLOWED
, &lsa_pol
);
2836 if (!NT_STATUS_IS_OK(result
)) {
2837 d_fprintf(stderr
, "Couldn't open LSA policy handle\n");
2838 TALLOC_FREE(lsa_pipe
);
2842 alias_sids
= TALLOC_ZERO_ARRAY(mem_ctx
, DOM_SID
, num_members
);
2844 d_fprintf(stderr
, "Out of memory\n");
2845 TALLOC_FREE(lsa_pipe
);
2846 return NT_STATUS_NO_MEMORY
;
2849 for (i
=0; i
<num_members
; i
++) {
2850 sid_copy(&alias_sids
[i
], sid_array
.sids
[i
].sid
);
2853 result
= rpccli_lsa_lookup_sids(lsa_pipe
, mem_ctx
, &lsa_pol
,
2854 num_members
, alias_sids
,
2855 &domains
, &names
, &types
);
2857 if (!NT_STATUS_IS_OK(result
) &&
2858 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
2859 d_fprintf(stderr
, "Couldn't lookup SIDs\n");
2860 TALLOC_FREE(lsa_pipe
);
2864 for (i
= 0; i
< num_members
; i
++) {
2866 sid_to_fstring(sid_str
, &alias_sids
[i
]);
2868 if (c
->opt_long_list_entries
) {
2869 printf("%s %s\\%s %d\n", sid_str
,
2870 domains
[i
] ? domains
[i
] : "*unknown*",
2871 names
[i
] ? names
[i
] : "*unknown*", types
[i
]);
2874 printf("%s\\%s\n", domains
[i
], names
[i
]);
2876 printf("%s\n", sid_str
);
2880 TALLOC_FREE(lsa_pipe
);
2881 return NT_STATUS_OK
;
2884 static NTSTATUS
rpc_group_members_internals(struct net_context
*c
,
2885 const DOM_SID
*domain_sid
,
2886 const char *domain_name
,
2887 struct cli_state
*cli
,
2888 struct rpc_pipe_client
*pipe_hnd
,
2889 TALLOC_CTX
*mem_ctx
,
2894 POLICY_HND connect_pol
, domain_pol
;
2895 struct samr_Ids rids
, rid_types
;
2896 struct lsa_String lsa_acct_name
;
2898 /* Get sam policy handle */
2900 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
2902 MAXIMUM_ALLOWED_ACCESS
,
2905 if (!NT_STATUS_IS_OK(result
))
2908 /* Get domain policy handle */
2910 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2912 MAXIMUM_ALLOWED_ACCESS
,
2913 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
2916 if (!NT_STATUS_IS_OK(result
))
2919 init_lsa_String(&lsa_acct_name
, argv
[0]); /* sure? */
2921 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2928 if (!NT_STATUS_IS_OK(result
)) {
2930 /* Ok, did not find it in the global sam, try with builtin */
2932 DOM_SID sid_Builtin
;
2934 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
2936 sid_copy(&sid_Builtin
, &global_sid_Builtin
);
2938 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
2940 MAXIMUM_ALLOWED_ACCESS
,
2944 if (!NT_STATUS_IS_OK(result
)) {
2945 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2949 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
2956 if (!NT_STATUS_IS_OK(result
)) {
2957 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2962 if (rids
.count
!= 1) {
2963 d_fprintf(stderr
, "Couldn't find group %s\n", argv
[0]);
2967 if (rid_types
.ids
[0] == SID_NAME_DOM_GRP
) {
2968 return rpc_list_group_members(c
, pipe_hnd
, mem_ctx
, domain_name
,
2969 domain_sid
, &domain_pol
,
2973 if (rid_types
.ids
[0] == SID_NAME_ALIAS
) {
2974 return rpc_list_alias_members(c
, pipe_hnd
, mem_ctx
, &domain_pol
,
2978 return NT_STATUS_NO_SUCH_GROUP
;
2981 static int rpc_group_members(struct net_context
*c
, int argc
, const char **argv
)
2983 if (argc
!= 1 || c
->display_usage
) {
2984 return rpc_group_usage(c
, argc
, argv
);
2987 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
2988 rpc_group_members_internals
,
2992 static int rpc_group_rename_internals(struct net_context
*c
, int argc
, const char **argv
)
2994 NET_API_STATUS status
;
2995 struct GROUP_INFO_0 g0
;
2999 d_printf("Usage: 'net rpc group rename group newname'\n");
3003 g0
.grpi0_name
= argv
[1];
3005 status
= NetGroupSetInfo(c
->opt_host
,
3012 d_fprintf(stderr
, "Renaming group %s failed with: %s\n",
3013 argv
[0], libnetapi_get_error_string(c
->netapi_ctx
,
3021 static int rpc_group_rename(struct net_context
*c
, int argc
, const char **argv
)
3023 if (argc
!= 2 || c
->display_usage
) {
3024 return rpc_group_usage(c
, argc
, argv
);
3027 return rpc_group_rename_internals(c
, argc
, argv
);
3031 * 'net rpc group' entrypoint.
3032 * @param argc Standard main() style argc.
3033 * @param argv Standard main() style argv. Initial components are already
3037 int net_rpc_group(struct net_context
*c
, int argc
, const char **argv
)
3039 NET_API_STATUS status
;
3041 struct functable func
[] = {
3046 "Create specified group",
3047 "net rpc group add\n"
3048 " Create specified group"
3054 "Delete specified group",
3055 "net rpc group delete\n"
3056 " Delete specified group"
3062 "Add member to group",
3063 "net rpc group addmem\n"
3064 " Add member to group"
3070 "Remove member from group",
3071 "net rpc group delmem\n"
3072 " Remove member from group"
3079 "net rpc group list\n"
3086 "List group members",
3087 "net rpc group members\n"
3088 " List group members"
3095 "net rpc group rename\n"
3098 {NULL
, NULL
, 0, NULL
, NULL
}
3101 status
= libnetapi_init(&c
->netapi_ctx
);
3105 libnetapi_set_username(c
->netapi_ctx
, c
->opt_user_name
);
3106 libnetapi_set_password(c
->netapi_ctx
, c
->opt_password
);
3109 if (c
->display_usage
) {
3110 d_printf("Usage:\n");
3111 d_printf("net rpc group\n"
3112 " Alias for net rpc group list global local "
3114 net_display_usage_from_functable(func
);
3118 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
3119 rpc_group_list_internals
,
3123 return net_run_function(c
, argc
, argv
, "net rpc group", func
);
3126 /****************************************************************************/
3128 static int rpc_share_usage(struct net_context
*c
, int argc
, const char **argv
)
3130 return net_share_usage(c
, argc
, argv
);
3134 * Add a share on a remote RPC server.
3136 * All parameters are provided by the run_rpc_command function, except for
3137 * argc, argv which are passed through.
3139 * @param domain_sid The domain sid acquired from the remote server.
3140 * @param cli A cli_state connected to the server.
3141 * @param mem_ctx Talloc context, destroyed on completion of the function.
3142 * @param argc Standard main() style argc.
3143 * @param argv Standard main() style argv. Initial components are already
3146 * @return Normal NTSTATUS return.
3148 static NTSTATUS
rpc_share_add_internals(struct net_context
*c
,
3149 const DOM_SID
*domain_sid
,
3150 const char *domain_name
,
3151 struct cli_state
*cli
,
3152 struct rpc_pipe_client
*pipe_hnd
,
3153 TALLOC_CTX
*mem_ctx
,int argc
,
3160 uint32 type
= STYPE_DISKTREE
; /* only allow disk shares to be added */
3161 uint32 num_users
=0, perms
=0;
3162 char *password
=NULL
; /* don't allow a share password */
3164 union srvsvc_NetShareInfo info
;
3165 struct srvsvc_NetShareInfo2 info2
;
3166 uint32_t parm_error
= 0;
3168 if ((sharename
= talloc_strdup(mem_ctx
, argv
[0])) == NULL
) {
3169 return NT_STATUS_NO_MEMORY
;
3172 path
= strchr(sharename
, '=');
3174 return NT_STATUS_UNSUCCESSFUL
;
3177 info2
.name
= sharename
;
3179 info2
.comment
= c
->opt_comment
;
3180 info2
.permissions
= perms
;
3181 info2
.max_users
= c
->opt_maxusers
;
3182 info2
.current_users
= num_users
;
3184 info2
.password
= password
;
3186 info
.info2
= &info2
;
3188 status
= rpccli_srvsvc_NetShareAdd(pipe_hnd
, mem_ctx
,
3197 static int rpc_share_add(struct net_context
*c
, int argc
, const char **argv
)
3199 if ((argc
< 1) || !strchr(argv
[0], '=') || c
->display_usage
) {
3200 return rpc_share_usage(c
, argc
, argv
);
3202 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3203 rpc_share_add_internals
,
3208 * Delete a share on a remote RPC server.
3210 * All parameters are provided by the run_rpc_command function, except for
3211 * argc, argv which are passed through.
3213 * @param domain_sid The domain sid acquired from the remote server.
3214 * @param cli A cli_state connected to the server.
3215 * @param mem_ctx Talloc context, destroyed on completion of the function.
3216 * @param argc Standard main() style argc.
3217 * @param argv Standard main() style argv. Initial components are already
3220 * @return Normal NTSTATUS return.
3222 static NTSTATUS
rpc_share_del_internals(struct net_context
*c
,
3223 const DOM_SID
*domain_sid
,
3224 const char *domain_name
,
3225 struct cli_state
*cli
,
3226 struct rpc_pipe_client
*pipe_hnd
,
3227 TALLOC_CTX
*mem_ctx
,
3233 return rpccli_srvsvc_NetShareDel(pipe_hnd
, mem_ctx
,
3241 * Delete a share on a remote RPC server.
3243 * @param domain_sid The domain sid acquired from the remote server.
3244 * @param argc Standard main() style argc.
3245 * @param argv Standard main() style argv. Initial components are already
3248 * @return A shell status integer (0 for success).
3250 static int rpc_share_delete(struct net_context
*c
, int argc
, const char **argv
)
3252 if (argc
< 1 || c
->display_usage
) {
3253 return rpc_share_usage(c
, argc
, argv
);
3255 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3256 rpc_share_del_internals
,
3261 * Formatted print of share info
3263 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3266 static void display_share_info_1(struct net_context
*c
,
3267 struct srvsvc_NetShareInfo1
*r
)
3269 if (c
->opt_long_list_entries
) {
3270 d_printf("%-12s %-8.8s %-50s\n",
3272 c
->share_type
[r
->type
& ~(STYPE_TEMPORARY
|STYPE_HIDDEN
)],
3275 d_printf("%s\n", r
->name
);
3279 static WERROR
get_share_info(struct net_context
*c
,
3280 struct rpc_pipe_client
*pipe_hnd
,
3281 TALLOC_CTX
*mem_ctx
,
3285 struct srvsvc_NetShareInfoCtr
*info_ctr
)
3289 union srvsvc_NetShareInfo info
;
3291 /* no specific share requested, enumerate all */
3294 uint32_t preferred_len
= 0xffffffff;
3295 uint32_t total_entries
= 0;
3296 uint32_t resume_handle
= 0;
3298 info_ctr
->level
= level
;
3300 status
= rpccli_srvsvc_NetShareEnumAll(pipe_hnd
, mem_ctx
,
3310 /* request just one share */
3311 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
3318 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
3323 ZERO_STRUCTP(info_ctr
);
3325 info_ctr
->level
= level
;
3330 struct srvsvc_NetShareCtr1
*ctr1
;
3332 ctr1
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr1
);
3333 W_ERROR_HAVE_NO_MEMORY(ctr1
);
3336 ctr1
->array
= info
.info1
;
3338 info_ctr
->ctr
.ctr1
= ctr1
;
3342 struct srvsvc_NetShareCtr2
*ctr2
;
3344 ctr2
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr2
);
3345 W_ERROR_HAVE_NO_MEMORY(ctr2
);
3348 ctr2
->array
= info
.info2
;
3350 info_ctr
->ctr
.ctr2
= ctr2
;
3354 struct srvsvc_NetShareCtr502
*ctr502
;
3356 ctr502
= TALLOC_ZERO_P(mem_ctx
, struct srvsvc_NetShareCtr502
);
3357 W_ERROR_HAVE_NO_MEMORY(ctr502
);
3360 ctr502
->array
= info
.info502
;
3362 info_ctr
->ctr
.ctr502
= ctr502
;
3370 * List shares on a remote RPC server.
3372 * All parameters are provided by the run_rpc_command function, except for
3373 * argc, argv which are passed through.
3375 * @param domain_sid The domain sid acquired from the remote server.
3376 * @param cli A cli_state connected to the server.
3377 * @param mem_ctx Talloc context, destroyed on completion of the function.
3378 * @param argc Standard main() style argc.
3379 * @param argv Standard main() style argv. Initial components are already
3382 * @return Normal NTSTATUS return.
3385 static NTSTATUS
rpc_share_list_internals(struct net_context
*c
,
3386 const DOM_SID
*domain_sid
,
3387 const char *domain_name
,
3388 struct cli_state
*cli
,
3389 struct rpc_pipe_client
*pipe_hnd
,
3390 TALLOC_CTX
*mem_ctx
,
3394 struct srvsvc_NetShareInfoCtr info_ctr
;
3395 struct srvsvc_NetShareCtr1 ctr1
;
3397 uint32 i
, level
= 1;
3399 ZERO_STRUCT(info_ctr
);
3403 info_ctr
.ctr
.ctr1
= &ctr1
;
3405 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3407 if (!W_ERROR_IS_OK(result
))
3410 /* Display results */
3412 if (c
->opt_long_list_entries
) {
3414 "\nEnumerating shared resources (exports) on remote server:\n\n"
3415 "\nShare name Type Description\n"
3416 "---------- ---- -----------\n");
3418 for (i
= 0; i
< info_ctr
.ctr
.ctr1
->count
; i
++)
3419 display_share_info_1(c
, &info_ctr
.ctr
.ctr1
->array
[i
]);
3421 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
3425 * 'net rpc share list' entrypoint.
3426 * @param argc Standard main() style argc.
3427 * @param argv Standard main() style argv. Initial components are already
3430 static int rpc_share_list(struct net_context
*c
, int argc
, const char **argv
)
3432 if (c
->display_usage
) {
3434 "net rpc share list\n"
3435 " List shares on remote server\n");
3439 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3440 rpc_share_list_internals
, argc
, argv
);
3443 static bool check_share_availability(struct cli_state
*cli
, const char *netname
)
3445 if (!cli_send_tconX(cli
, netname
, "A:", "", 0)) {
3446 d_printf("skipping [%s]: not a file share.\n", netname
);
3456 static bool check_share_sanity(struct net_context
*c
, struct cli_state
*cli
,
3457 const char *netname
, uint32 type
)
3459 /* only support disk shares */
3460 if (! ( type
== STYPE_DISKTREE
|| type
== (STYPE_DISKTREE
| STYPE_HIDDEN
)) ) {
3461 printf("share [%s] is not a diskshare (type: %x)\n", netname
, type
);
3465 /* skip builtin shares */
3466 /* FIXME: should print$ be added too ? */
3467 if (strequal(netname
,"IPC$") || strequal(netname
,"ADMIN$") ||
3468 strequal(netname
,"global"))
3471 if (c
->opt_exclude
&& in_list(netname
, c
->opt_exclude
, false)) {
3472 printf("excluding [%s]\n", netname
);
3476 return check_share_availability(cli
, netname
);
3480 * Migrate shares from a remote RPC server to the local RPC server.
3482 * All parameters are provided by the run_rpc_command function, except for
3483 * argc, argv which are passed through.
3485 * @param domain_sid The domain sid acquired from the remote server.
3486 * @param cli A cli_state connected to the server.
3487 * @param mem_ctx Talloc context, destroyed on completion of the function.
3488 * @param argc Standard main() style argc.
3489 * @param argv Standard main() style argv. Initial components are already
3492 * @return Normal NTSTATUS return.
3495 static NTSTATUS
rpc_share_migrate_shares_internals(struct net_context
*c
,
3496 const DOM_SID
*domain_sid
,
3497 const char *domain_name
,
3498 struct cli_state
*cli
,
3499 struct rpc_pipe_client
*pipe_hnd
,
3500 TALLOC_CTX
*mem_ctx
,
3505 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3506 struct srvsvc_NetShareInfoCtr ctr_src
;
3508 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3509 struct cli_state
*cli_dst
= NULL
;
3510 uint32 level
= 502; /* includes secdesc */
3511 uint32_t parm_error
= 0;
3513 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3515 if (!W_ERROR_IS_OK(result
))
3518 /* connect destination PI_SRVSVC */
3519 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3520 &ndr_table_srvsvc
.syntax_id
);
3521 if (!NT_STATUS_IS_OK(nt_status
))
3525 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3527 union srvsvc_NetShareInfo info
;
3528 struct srvsvc_NetShareInfo502 info502
=
3529 ctr_src
.ctr
.ctr502
->array
[i
];
3531 /* reset error-code */
3532 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3534 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3537 /* finally add the share on the dst server */
3539 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3540 info502
.name
, info502
.path
, info502
.comment
);
3542 info
.info502
= &info502
;
3544 nt_status
= rpccli_srvsvc_NetShareAdd(srvsvc_pipe
, mem_ctx
,
3545 srvsvc_pipe
->desthost
,
3551 if (W_ERROR_V(result
) == W_ERROR_V(WERR_ALREADY_EXISTS
)) {
3552 printf(" [%s] does already exist\n",
3557 if (!NT_STATUS_IS_OK(nt_status
) || !W_ERROR_IS_OK(result
)) {
3558 printf("cannot add share: %s\n", dos_errstr(result
));
3564 nt_status
= NT_STATUS_OK
;
3568 cli_shutdown(cli_dst
);
3576 * Migrate shares from a RPC server to another.
3578 * @param argc Standard main() style argc.
3579 * @param argv Standard main() style argv. Initial components are already
3582 * @return A shell status integer (0 for success).
3584 static int rpc_share_migrate_shares(struct net_context
*c
, int argc
,
3587 if (c
->display_usage
) {
3589 "net rpc share migrate shares\n"
3590 " Migrate shares to local server\n");
3595 printf("no server to migrate\n");
3599 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3600 rpc_share_migrate_shares_internals
,
3607 * @param f file_info
3608 * @param mask current search mask
3609 * @param state arg-pointer
3612 static void copy_fn(const char *mnt
, file_info
*f
,
3613 const char *mask
, void *state
)
3615 static NTSTATUS nt_status
;
3616 static struct copy_clistate
*local_state
;
3617 static fstring filename
, new_mask
;
3620 struct net_context
*c
;
3622 local_state
= (struct copy_clistate
*)state
;
3623 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3627 if (strequal(f
->name
, ".") || strequal(f
->name
, ".."))
3630 DEBUG(3,("got mask: %s, name: %s\n", mask
, f
->name
));
3633 if (f
->mode
& aDIR
) {
3635 DEBUG(3,("got dir: %s\n", f
->name
));
3637 fstrcpy(dir
, local_state
->cwd
);
3639 fstrcat(dir
, f
->name
);
3641 switch (net_mode_share
)
3643 case NET_MODE_SHARE_MIGRATE
:
3644 /* create that directory */
3645 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3646 local_state
->cli_share_src
,
3647 local_state
->cli_share_dst
,
3649 c
->opt_acls
? true : false,
3650 c
->opt_attrs
? true : false,
3651 c
->opt_timestamps
? true:false,
3655 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3659 if (!NT_STATUS_IS_OK(nt_status
))
3660 printf("could not handle dir %s: %s\n",
3661 dir
, nt_errstr(nt_status
));
3663 /* search below that directory */
3664 fstrcpy(new_mask
, dir
);
3665 fstrcat(new_mask
, "\\*");
3667 old_dir
= local_state
->cwd
;
3668 local_state
->cwd
= dir
;
3669 if (!sync_files(local_state
, new_mask
))
3670 printf("could not handle files\n");
3671 local_state
->cwd
= old_dir
;
3678 fstrcpy(filename
, local_state
->cwd
);
3679 fstrcat(filename
, "\\");
3680 fstrcat(filename
, f
->name
);
3682 DEBUG(3,("got file: %s\n", filename
));
3684 switch (net_mode_share
)
3686 case NET_MODE_SHARE_MIGRATE
:
3687 nt_status
= net_copy_file(c
, local_state
->mem_ctx
,
3688 local_state
->cli_share_src
,
3689 local_state
->cli_share_dst
,
3691 c
->opt_acls
? true : false,
3692 c
->opt_attrs
? true : false,
3693 c
->opt_timestamps
? true: false,
3697 d_fprintf(stderr
, "Unsupported file mode %d\n", net_mode_share
);
3701 if (!NT_STATUS_IS_OK(nt_status
))
3702 printf("could not handle file %s: %s\n",
3703 filename
, nt_errstr(nt_status
));
3708 * sync files, can be called recursivly to list files
3709 * and then call copy_fn for each file
3711 * @param cp_clistate pointer to the copy_clistate we work with
3712 * @param mask the current search mask
3714 * @return Boolean result
3716 static bool sync_files(struct copy_clistate
*cp_clistate
, const char *mask
)
3718 struct cli_state
*targetcli
;
3719 char *targetpath
= NULL
;
3721 DEBUG(3,("calling cli_list with mask: %s\n", mask
));
3723 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate
->cli_share_src
,
3724 mask
, &targetcli
, &targetpath
) ) {
3725 d_fprintf(stderr
, "cli_resolve_path %s failed with error: %s\n",
3726 mask
, cli_errstr(cp_clistate
->cli_share_src
));
3730 if (cli_list(targetcli
, targetpath
, cp_clistate
->attribute
, copy_fn
, cp_clistate
) == -1) {
3731 d_fprintf(stderr
, "listing %s failed with error: %s\n",
3732 mask
, cli_errstr(targetcli
));
3741 * Set the top level directory permissions before we do any further copies.
3742 * Should set up ACL inheritance.
3745 bool copy_top_level_perms(struct net_context
*c
,
3746 struct copy_clistate
*cp_clistate
,
3747 const char *sharename
)
3749 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3751 switch (net_mode_share
) {
3752 case NET_MODE_SHARE_MIGRATE
:
3753 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename
));
3754 nt_status
= net_copy_fileattr(c
,
3755 cp_clistate
->mem_ctx
,
3756 cp_clistate
->cli_share_src
,
3757 cp_clistate
->cli_share_dst
,
3759 c
->opt_acls
? true : false,
3760 c
->opt_attrs
? true : false,
3761 c
->opt_timestamps
? true: false,
3765 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3769 if (!NT_STATUS_IS_OK(nt_status
)) {
3770 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3771 sharename
, nt_errstr(nt_status
));
3779 * Sync all files inside a remote share to another share (over smb).
3781 * All parameters are provided by the run_rpc_command function, except for
3782 * argc, argv which are passed through.
3784 * @param domain_sid The domain sid acquired from the remote server.
3785 * @param cli A cli_state connected to the server.
3786 * @param mem_ctx Talloc context, destroyed on completion of the function.
3787 * @param argc Standard main() style argc.
3788 * @param argv Standard main() style argv. Initial components are already
3791 * @return Normal NTSTATUS return.
3794 static NTSTATUS
rpc_share_migrate_files_internals(struct net_context
*c
,
3795 const DOM_SID
*domain_sid
,
3796 const char *domain_name
,
3797 struct cli_state
*cli
,
3798 struct rpc_pipe_client
*pipe_hnd
,
3799 TALLOC_CTX
*mem_ctx
,
3804 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3805 struct srvsvc_NetShareInfoCtr ctr_src
;
3808 struct copy_clistate cp_clistate
;
3809 bool got_src_share
= false;
3810 bool got_dst_share
= false;
3811 const char *mask
= "\\*";
3814 dst
= SMB_STRDUP(c
->opt_destination
?c
->opt_destination
:"127.0.0.1");
3816 nt_status
= NT_STATUS_NO_MEMORY
;
3820 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3823 if (!W_ERROR_IS_OK(result
))
3826 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3828 struct srvsvc_NetShareInfo502 info502
=
3829 ctr_src
.ctr
.ctr502
->array
[i
];
3831 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3834 /* one might not want to mirror whole discs :) */
3835 if (strequal(info502
.name
, "print$") || info502
.name
[1] == '$') {
3836 d_printf("skipping [%s]: builtin/hidden share\n", info502
.name
);
3840 switch (net_mode_share
)
3842 case NET_MODE_SHARE_MIGRATE
:
3846 d_fprintf(stderr
, "Unsupported mode %d\n", net_mode_share
);
3849 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3851 c
->opt_acls
? "including" : "without",
3852 c
->opt_attrs
? "including" : "without",
3853 c
->opt_timestamps
? "(preserving timestamps)" : "");
3855 cp_clistate
.mem_ctx
= mem_ctx
;
3856 cp_clistate
.cli_share_src
= NULL
;
3857 cp_clistate
.cli_share_dst
= NULL
;
3858 cp_clistate
.cwd
= NULL
;
3859 cp_clistate
.attribute
= aSYSTEM
| aHIDDEN
| aDIR
;
3862 /* open share source */
3863 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_src
,
3864 &cli
->dest_ss
, cli
->desthost
,
3865 info502
.name
, "A:");
3866 if (!NT_STATUS_IS_OK(nt_status
))
3869 got_src_share
= true;
3871 if (net_mode_share
== NET_MODE_SHARE_MIGRATE
) {
3872 /* open share destination */
3873 nt_status
= connect_to_service(c
, &cp_clistate
.cli_share_dst
,
3874 NULL
, dst
, info502
.name
, "A:");
3875 if (!NT_STATUS_IS_OK(nt_status
))
3878 got_dst_share
= true;
3881 if (!copy_top_level_perms(c
, &cp_clistate
, info502
.name
)) {
3882 d_fprintf(stderr
, "Could not handle the top level directory permissions for the share: %s\n", info502
.name
);
3883 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3887 if (!sync_files(&cp_clistate
, mask
)) {
3888 d_fprintf(stderr
, "could not handle files for share: %s\n", info502
.name
);
3889 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3894 nt_status
= NT_STATUS_OK
;
3899 cli_shutdown(cp_clistate
.cli_share_src
);
3902 cli_shutdown(cp_clistate
.cli_share_dst
);
3909 static int rpc_share_migrate_files(struct net_context
*c
, int argc
, const char **argv
)
3911 if (c
->display_usage
) {
3913 "net share migrate files\n"
3914 " Migrate files to local server\n");
3919 d_printf("no server to migrate\n");
3923 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
3924 rpc_share_migrate_files_internals
,
3929 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3931 * All parameters are provided by the run_rpc_command function, except for
3932 * argc, argv which are passed through.
3934 * @param domain_sid The domain sid acquired from the remote server.
3935 * @param cli A cli_state connected to the server.
3936 * @param mem_ctx Talloc context, destroyed on completion of the function.
3937 * @param argc Standard main() style argc.
3938 * @param argv Standard main() style argv. Initial components are already
3941 * @return Normal NTSTATUS return.
3944 static NTSTATUS
rpc_share_migrate_security_internals(struct net_context
*c
,
3945 const DOM_SID
*domain_sid
,
3946 const char *domain_name
,
3947 struct cli_state
*cli
,
3948 struct rpc_pipe_client
*pipe_hnd
,
3949 TALLOC_CTX
*mem_ctx
,
3954 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
3955 struct srvsvc_NetShareInfoCtr ctr_src
;
3956 union srvsvc_NetShareInfo info
;
3958 struct rpc_pipe_client
*srvsvc_pipe
= NULL
;
3959 struct cli_state
*cli_dst
= NULL
;
3960 uint32 level
= 502; /* includes secdesc */
3961 uint32_t parm_error
= 0;
3963 result
= get_share_info(c
, pipe_hnd
, mem_ctx
, level
, argc
, argv
,
3966 if (!W_ERROR_IS_OK(result
))
3969 /* connect destination PI_SRVSVC */
3970 nt_status
= connect_dst_pipe(c
, &cli_dst
, &srvsvc_pipe
,
3971 &ndr_table_srvsvc
.syntax_id
);
3972 if (!NT_STATUS_IS_OK(nt_status
))
3976 for (i
= 0; i
< ctr_src
.ctr
.ctr502
->count
; i
++) {
3978 struct srvsvc_NetShareInfo502 info502
=
3979 ctr_src
.ctr
.ctr502
->array
[i
];
3981 /* reset error-code */
3982 nt_status
= NT_STATUS_UNSUCCESSFUL
;
3984 if (!check_share_sanity(c
, cli
, info502
.name
, info502
.type
))
3987 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3988 info502
.name
, info502
.path
, info502
.comment
);
3991 display_sec_desc(info502
.sd_buf
.sd
);
3993 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3994 info
.info502
= &info502
;
3996 /* finally modify the share on the dst server */
3997 nt_status
= rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe
, mem_ctx
,
3998 srvsvc_pipe
->desthost
,
4004 if (!NT_STATUS_IS_OK(nt_status
) || !W_ERROR_IS_OK(result
)) {
4005 printf("cannot set share-acl: %s\n", dos_errstr(result
));
4011 nt_status
= NT_STATUS_OK
;
4015 cli_shutdown(cli_dst
);
4023 * Migrate share-acls from a RPC server to another.
4025 * @param argc Standard main() style argc.
4026 * @param argv Standard main() style argv. Initial components are already
4029 * @return A shell status integer (0 for success).
4031 static int rpc_share_migrate_security(struct net_context
*c
, int argc
,
4034 if (c
->display_usage
) {
4036 "net rpc share migrate security\n"
4037 " Migrate share-acls to local server\n");
4042 d_printf("no server to migrate\n");
4046 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4047 rpc_share_migrate_security_internals
,
4052 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4053 * from one server to another.
4055 * @param argc Standard main() style argc.
4056 * @param argv Standard main() style argv. Initial components are already
4059 * @return A shell status integer (0 for success).
4062 static int rpc_share_migrate_all(struct net_context
*c
, int argc
,
4067 if (c
->display_usage
) {
4069 "net rpc share migrate all\n"
4070 " Migrates shares including all share settings\n");
4075 d_printf("no server to migrate\n");
4079 /* order is important. we don't want to be locked out by the share-acl
4080 * before copying files - gd */
4082 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4083 rpc_share_migrate_shares_internals
, argc
, argv
);
4087 ret
= run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4088 rpc_share_migrate_files_internals
, argc
, argv
);
4092 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4093 rpc_share_migrate_security_internals
, argc
,
4099 * 'net rpc share migrate' entrypoint.
4100 * @param argc Standard main() style argc.
4101 * @param argv Standard main() style argv. Initial components are already
4104 static int rpc_share_migrate(struct net_context
*c
, int argc
, const char **argv
)
4107 struct functable func
[] = {
4110 rpc_share_migrate_all
,
4112 "Migrate shares from remote to local server",
4113 "net rpc share migrate all\n"
4114 " Migrate shares from remote to local server"
4118 rpc_share_migrate_files
,
4120 "Migrate files from remote to local server",
4121 "net rpc share migrate files\n"
4122 " Migrate files from remote to local server"
4126 rpc_share_migrate_security
,
4128 "Migrate share-ACLs from remote to local server",
4129 "net rpc share migrate security\n"
4130 " Migrate share-ACLs from remote to local server"
4134 rpc_share_migrate_shares
,
4136 "Migrate shares from remote to local server",
4137 "net rpc share migrate shares\n"
4138 " Migrate shares from remote to local server"
4140 {NULL
, NULL
, 0, NULL
, NULL
}
4143 net_mode_share
= NET_MODE_SHARE_MIGRATE
;
4145 return net_run_function(c
, argc
, argv
, "net rpc share migrate", func
);
4154 static int num_server_aliases
;
4155 static struct full_alias
*server_aliases
;
4158 * Add an alias to the static list.
4160 static void push_alias(TALLOC_CTX
*mem_ctx
, struct full_alias
*alias
)
4162 if (server_aliases
== NULL
)
4163 server_aliases
= SMB_MALLOC_ARRAY(struct full_alias
, 100);
4165 server_aliases
[num_server_aliases
] = *alias
;
4166 num_server_aliases
+= 1;
4170 * For a specific domain on the server, fetch all the aliases
4171 * and their members. Add all of them to the server_aliases.
4174 static NTSTATUS
rpc_fetch_domain_aliases(struct rpc_pipe_client
*pipe_hnd
,
4175 TALLOC_CTX
*mem_ctx
,
4176 POLICY_HND
*connect_pol
,
4177 const DOM_SID
*domain_sid
)
4179 uint32 start_idx
, max_entries
, num_entries
, i
;
4180 struct samr_SamArray
*groups
= NULL
;
4182 POLICY_HND domain_pol
;
4184 /* Get domain policy handle */
4186 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
4188 MAXIMUM_ALLOWED_ACCESS
,
4189 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
4191 if (!NT_STATUS_IS_OK(result
))
4198 result
= rpccli_samr_EnumDomainAliases(pipe_hnd
, mem_ctx
,
4204 for (i
= 0; i
< num_entries
; i
++) {
4206 POLICY_HND alias_pol
;
4207 struct full_alias alias
;
4208 struct lsa_SidArray sid_array
;
4211 result
= rpccli_samr_OpenAlias(pipe_hnd
, mem_ctx
,
4213 MAXIMUM_ALLOWED_ACCESS
,
4214 groups
->entries
[i
].idx
,
4216 if (!NT_STATUS_IS_OK(result
))
4219 result
= rpccli_samr_GetMembersInAlias(pipe_hnd
, mem_ctx
,
4222 if (!NT_STATUS_IS_OK(result
))
4225 alias
.num_members
= sid_array
.num_sids
;
4227 result
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &alias_pol
);
4228 if (!NT_STATUS_IS_OK(result
))
4231 alias
.members
= NULL
;
4233 if (alias
.num_members
> 0) {
4234 alias
.members
= SMB_MALLOC_ARRAY(DOM_SID
, alias
.num_members
);
4236 for (j
= 0; j
< alias
.num_members
; j
++)
4237 sid_copy(&alias
.members
[j
],
4238 sid_array
.sids
[j
].sid
);
4241 sid_copy(&alias
.sid
, domain_sid
);
4242 sid_append_rid(&alias
.sid
, groups
->entries
[i
].idx
);
4244 push_alias(mem_ctx
, &alias
);
4246 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
4248 result
= NT_STATUS_OK
;
4251 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_pol
);
4257 * Dump server_aliases as names for debugging purposes.
4260 static NTSTATUS
rpc_aliaslist_dump(struct net_context
*c
,
4261 const DOM_SID
*domain_sid
,
4262 const char *domain_name
,
4263 struct cli_state
*cli
,
4264 struct rpc_pipe_client
*pipe_hnd
,
4265 TALLOC_CTX
*mem_ctx
,
4273 result
= rpccli_lsa_open_policy(pipe_hnd
, mem_ctx
, true,
4274 SEC_RIGHTS_MAXIMUM_ALLOWED
,
4276 if (!NT_STATUS_IS_OK(result
))
4279 for (i
=0; i
<num_server_aliases
; i
++) {
4282 enum lsa_SidType
*types
;
4285 struct full_alias
*alias
= &server_aliases
[i
];
4287 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
, 1,
4289 &domains
, &names
, &types
);
4290 if (!NT_STATUS_IS_OK(result
))
4293 DEBUG(1, ("%s\\%s %d: ", domains
[0], names
[0], types
[0]));
4295 if (alias
->num_members
== 0) {
4300 result
= rpccli_lsa_lookup_sids(pipe_hnd
, mem_ctx
, &lsa_pol
,
4303 &domains
, &names
, &types
);
4305 if (!NT_STATUS_IS_OK(result
) &&
4306 !NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
))
4309 for (j
=0; j
<alias
->num_members
; j
++)
4310 DEBUG(1, ("%s\\%s (%d); ",
4311 domains
[j
] ? domains
[j
] : "*unknown*",
4312 names
[j
] ? names
[j
] : "*unknown*",types
[j
]));
4316 rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &lsa_pol
);
4318 return NT_STATUS_OK
;
4322 * Fetch a list of all server aliases and their members into
4326 static NTSTATUS
rpc_aliaslist_internals(struct net_context
*c
,
4327 const DOM_SID
*domain_sid
,
4328 const char *domain_name
,
4329 struct cli_state
*cli
,
4330 struct rpc_pipe_client
*pipe_hnd
,
4331 TALLOC_CTX
*mem_ctx
,
4336 POLICY_HND connect_pol
;
4338 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
4340 MAXIMUM_ALLOWED_ACCESS
,
4343 if (!NT_STATUS_IS_OK(result
))
4346 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4347 &global_sid_Builtin
);
4349 if (!NT_STATUS_IS_OK(result
))
4352 result
= rpc_fetch_domain_aliases(pipe_hnd
, mem_ctx
, &connect_pol
,
4355 rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_pol
);
4360 static void init_user_token(NT_USER_TOKEN
*token
, DOM_SID
*user_sid
)
4362 token
->num_sids
= 4;
4364 if (!(token
->user_sids
= SMB_MALLOC_ARRAY(DOM_SID
, 4))) {
4365 d_fprintf(stderr
, "malloc failed\n");
4366 token
->num_sids
= 0;
4370 token
->user_sids
[0] = *user_sid
;
4371 sid_copy(&token
->user_sids
[1], &global_sid_World
);
4372 sid_copy(&token
->user_sids
[2], &global_sid_Network
);
4373 sid_copy(&token
->user_sids
[3], &global_sid_Authenticated_Users
);
4376 static void free_user_token(NT_USER_TOKEN
*token
)
4378 SAFE_FREE(token
->user_sids
);
4381 static bool is_sid_in_token(NT_USER_TOKEN
*token
, DOM_SID
*sid
)
4385 for (i
=0; i
<token
->num_sids
; i
++) {
4386 if (sid_compare(sid
, &token
->user_sids
[i
]) == 0)
4392 static void add_sid_to_token(NT_USER_TOKEN
*token
, DOM_SID
*sid
)
4394 if (is_sid_in_token(token
, sid
))
4397 token
->user_sids
= SMB_REALLOC_ARRAY(token
->user_sids
, DOM_SID
, token
->num_sids
+1);
4398 if (!token
->user_sids
) {
4402 sid_copy(&token
->user_sids
[token
->num_sids
], sid
);
4404 token
->num_sids
+= 1;
4409 NT_USER_TOKEN token
;
4412 static void dump_user_token(struct user_token
*token
)
4416 d_printf("%s\n", token
->name
);
4418 for (i
=0; i
<token
->token
.num_sids
; i
++) {
4419 d_printf(" %s\n", sid_string_tos(&token
->token
.user_sids
[i
]));
4423 static bool is_alias_member(DOM_SID
*sid
, struct full_alias
*alias
)
4427 for (i
=0; i
<alias
->num_members
; i
++) {
4428 if (sid_compare(sid
, &alias
->members
[i
]) == 0)
4435 static void collect_sid_memberships(NT_USER_TOKEN
*token
, DOM_SID sid
)
4439 for (i
=0; i
<num_server_aliases
; i
++) {
4440 if (is_alias_member(&sid
, &server_aliases
[i
]))
4441 add_sid_to_token(token
, &server_aliases
[i
].sid
);
4446 * We got a user token with all the SIDs we can know about without asking the
4447 * server directly. These are the user and domain group sids. All of these can
4448 * be members of aliases. So scan the list of aliases for each of the SIDs and
4449 * add them to the token.
4452 static void collect_alias_memberships(NT_USER_TOKEN
*token
)
4454 int num_global_sids
= token
->num_sids
;
4457 for (i
=0; i
<num_global_sids
; i
++) {
4458 collect_sid_memberships(token
, token
->user_sids
[i
]);
4462 static bool get_user_sids(const char *domain
, const char *user
, NT_USER_TOKEN
*token
)
4464 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4465 enum wbcSidType type
;
4467 struct wbcDomainSid wsid
;
4468 char *sid_str
= NULL
;
4470 uint32_t num_groups
;
4471 gid_t
*groups
= NULL
;
4474 fstr_sprintf(full_name
, "%s%c%s",
4475 domain
, *lp_winbind_separator(), user
);
4477 /* First let's find out the user sid */
4479 wbc_status
= wbcLookupName(domain
, user
, &wsid
, &type
);
4481 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4482 DEBUG(1, ("winbind could not find %s: %s\n",
4483 full_name
, wbcErrorString(wbc_status
)));
4487 wbc_status
= wbcSidToString(&wsid
, &sid_str
);
4488 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4492 if (type
!= SID_NAME_USER
) {
4493 wbcFreeMemory(sid_str
);
4494 DEBUG(1, ("%s is not a user\n", full_name
));
4498 string_to_sid(&user_sid
, sid_str
);
4499 wbcFreeMemory(sid_str
);
4502 init_user_token(token
, &user_sid
);
4504 /* And now the groups winbind knows about */
4506 wbc_status
= wbcGetGroups(full_name
, &num_groups
, &groups
);
4507 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4508 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4509 full_name
, wbcErrorString(wbc_status
)));
4513 for (i
= 0; i
< num_groups
; i
++) {
4514 gid_t gid
= groups
[i
];
4517 wbc_status
= wbcGidToSid(gid
, &wsid
);
4518 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4519 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4520 gid
, wbcErrorString(wbc_status
)));
4521 wbcFreeMemory(groups
);
4525 wbc_status
= wbcSidToString(&wsid
, &sid_str
);
4526 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4527 wbcFreeMemory(groups
);
4531 DEBUG(3, (" %s\n", sid_str
));
4533 string_to_sid(&sid
, sid_str
);
4534 wbcFreeMemory(sid_str
);
4537 add_sid_to_token(token
, &sid
);
4539 wbcFreeMemory(groups
);
4545 * Get a list of all user tokens we want to look at
4548 static bool get_user_tokens(struct net_context
*c
, int *num_tokens
,
4549 struct user_token
**user_tokens
)
4551 wbcErr wbc_status
= WBC_ERR_UNKNOWN_FAILURE
;
4552 uint32_t i
, num_users
;
4554 struct user_token
*result
;
4555 TALLOC_CTX
*frame
= NULL
;
4557 if (lp_winbind_use_default_domain() &&
4558 (c
->opt_target_workgroup
== NULL
)) {
4559 d_fprintf(stderr
, "winbind use default domain = yes set, "
4560 "please specify a workgroup\n");
4564 /* Send request to winbind daemon */
4566 wbc_status
= wbcListUsers(NULL
, &num_users
, &users
);
4567 if (!WBC_ERROR_IS_OK(wbc_status
)) {
4568 DEBUG(1, ("winbind could not list users: %s\n",
4569 wbcErrorString(wbc_status
)));
4573 result
= SMB_MALLOC_ARRAY(struct user_token
, num_users
);
4575 if (result
== NULL
) {
4576 DEBUG(1, ("Could not malloc sid array\n"));
4577 wbcFreeMemory(users
);
4581 frame
= talloc_stackframe();
4582 for (i
=0; i
< num_users
; i
++) {
4583 fstring domain
, user
;
4586 fstrcpy(result
[i
].name
, users
[i
]);
4588 p
= strchr(users
[i
], *lp_winbind_separator());
4590 DEBUG(3, ("%s\n", users
[i
]));
4593 fstrcpy(domain
, c
->opt_target_workgroup
);
4594 fstrcpy(user
, users
[i
]);
4597 fstrcpy(domain
, users
[i
]);
4602 get_user_sids(domain
, user
, &(result
[i
].token
));
4606 wbcFreeMemory(users
);
4608 *num_tokens
= num_users
;
4609 *user_tokens
= result
;
4614 static bool get_user_tokens_from_file(FILE *f
,
4616 struct user_token
**tokens
)
4618 struct user_token
*token
= NULL
;
4623 if (fgets(line
, sizeof(line
)-1, f
) == NULL
) {
4627 if (line
[strlen(line
)-1] == '\n')
4628 line
[strlen(line
)-1] = '\0';
4630 if (line
[0] == ' ') {
4634 string_to_sid(&sid
, &line
[1]);
4636 if (token
== NULL
) {
4637 DEBUG(0, ("File does not begin with username"));
4641 add_sid_to_token(&token
->token
, &sid
);
4645 /* And a new user... */
4648 *tokens
= SMB_REALLOC_ARRAY(*tokens
, struct user_token
, *num_tokens
);
4649 if (*tokens
== NULL
) {
4650 DEBUG(0, ("Could not realloc tokens\n"));
4654 token
= &((*tokens
)[*num_tokens
-1]);
4656 fstrcpy(token
->name
, line
);
4657 token
->token
.num_sids
= 0;
4658 token
->token
.user_sids
= NULL
;
4667 * Show the list of all users that have access to a share
4670 static void show_userlist(struct rpc_pipe_client
*pipe_hnd
,
4671 TALLOC_CTX
*mem_ctx
,
4672 const char *netname
,
4674 struct user_token
*tokens
)
4677 SEC_DESC
*share_sd
= NULL
;
4678 SEC_DESC
*root_sd
= NULL
;
4679 struct cli_state
*cli
= rpc_pipe_np_smb_conn(pipe_hnd
);
4681 union srvsvc_NetShareInfo info
;
4686 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
4693 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
4694 DEBUG(1, ("Coult not query secdesc for share %s\n",
4699 share_sd
= info
.info502
->sd_buf
.sd
;
4700 if (share_sd
== NULL
) {
4701 DEBUG(1, ("Got no secdesc for share %s\n",
4707 if (!cli_send_tconX(cli
, netname
, "A:", "", 0)) {
4711 fnum
= cli_nt_create(cli
, "\\", READ_CONTROL_ACCESS
);
4714 root_sd
= cli_query_secdesc(cli
, fnum
, mem_ctx
);
4717 for (i
=0; i
<num_tokens
; i
++) {
4720 if (share_sd
!= NULL
) {
4721 if (!se_access_check(share_sd
, &tokens
[i
].token
,
4722 1, &acc_granted
, &status
)) {
4723 DEBUG(1, ("Could not check share_sd for "
4729 if (!NT_STATUS_IS_OK(status
))
4733 if (root_sd
== NULL
) {
4734 d_printf(" %s\n", tokens
[i
].name
);
4738 if (!se_access_check(root_sd
, &tokens
[i
].token
,
4739 1, &acc_granted
, &status
)) {
4740 DEBUG(1, ("Could not check root_sd for user %s\n",
4745 if (!NT_STATUS_IS_OK(status
))
4748 d_printf(" %s\n", tokens
[i
].name
);
4752 cli_close(cli
, fnum
);
4764 static void collect_share(const char *name
, uint32 m
,
4765 const char *comment
, void *state
)
4767 struct share_list
*share_list
= (struct share_list
*)state
;
4769 if (m
!= STYPE_DISKTREE
)
4772 share_list
->num_shares
+= 1;
4773 share_list
->shares
= SMB_REALLOC_ARRAY(share_list
->shares
, char *, share_list
->num_shares
);
4774 if (!share_list
->shares
) {
4775 share_list
->num_shares
= 0;
4778 share_list
->shares
[share_list
->num_shares
-1] = SMB_STRDUP(name
);
4782 * List shares on a remote RPC server, including the security descriptors.
4784 * All parameters are provided by the run_rpc_command function, except for
4785 * argc, argv which are passed through.
4787 * @param domain_sid The domain sid acquired from the remote server.
4788 * @param cli A cli_state connected to the server.
4789 * @param mem_ctx Talloc context, destroyed on completion of the function.
4790 * @param argc Standard main() style argc.
4791 * @param argv Standard main() style argv. Initial components are already
4794 * @return Normal NTSTATUS return.
4797 static NTSTATUS
rpc_share_allowedusers_internals(struct net_context
*c
,
4798 const DOM_SID
*domain_sid
,
4799 const char *domain_name
,
4800 struct cli_state
*cli
,
4801 struct rpc_pipe_client
*pipe_hnd
,
4802 TALLOC_CTX
*mem_ctx
,
4812 struct user_token
*tokens
= NULL
;
4815 struct share_list share_list
;
4820 f
= fopen(argv
[0], "r");
4824 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno
)));
4825 return NT_STATUS_UNSUCCESSFUL
;
4828 r
= get_user_tokens_from_file(f
, &num_tokens
, &tokens
);
4834 DEBUG(0, ("Could not read users from file\n"));
4835 return NT_STATUS_UNSUCCESSFUL
;
4838 for (i
=0; i
<num_tokens
; i
++)
4839 collect_alias_memberships(&tokens
[i
].token
);
4841 init_enum_hnd(&hnd
, 0);
4843 share_list
.num_shares
= 0;
4844 share_list
.shares
= NULL
;
4846 ret
= cli_RNetShareEnum(cli
, collect_share
, &share_list
);
4849 DEBUG(0, ("Error returning browse list: %s\n",
4854 for (i
= 0; i
< share_list
.num_shares
; i
++) {
4855 char *netname
= share_list
.shares
[i
];
4857 if (netname
[strlen(netname
)-1] == '$')
4860 d_printf("%s\n", netname
);
4862 show_userlist(pipe_hnd
, mem_ctx
, netname
,
4863 num_tokens
, tokens
);
4866 for (i
=0; i
<num_tokens
; i
++) {
4867 free_user_token(&tokens
[i
].token
);
4870 SAFE_FREE(share_list
.shares
);
4872 return NT_STATUS_OK
;
4875 static int rpc_share_allowedusers(struct net_context
*c
, int argc
,
4880 if (c
->display_usage
) {
4882 "net rpc share allowedusers\n"
4883 " List allowed users\n");
4887 result
= run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
4888 rpc_aliaslist_internals
,
4893 result
= run_rpc_command(c
, NULL
, &ndr_table_lsarpc
.syntax_id
, 0,
4899 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
4900 rpc_share_allowedusers_internals
,
4904 int net_usersidlist(struct net_context
*c
, int argc
, const char **argv
)
4907 struct user_token
*tokens
= NULL
;
4911 net_usersidlist_usage(c
, argc
, argv
);
4915 if (!get_user_tokens(c
, &num_tokens
, &tokens
)) {
4916 DEBUG(0, ("Could not get the user/sid list\n"));
4920 for (i
=0; i
<num_tokens
; i
++) {
4921 dump_user_token(&tokens
[i
]);
4922 free_user_token(&tokens
[i
].token
);
4929 int net_usersidlist_usage(struct net_context
*c
, int argc
, const char **argv
)
4931 d_printf("net usersidlist\n"
4932 "\tprints out a list of all users the running winbind knows\n"
4933 "\tabout, together with all their SIDs. This is used as\n"
4934 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4936 net_common_flags_usage(c
, argc
, argv
);
4941 * 'net rpc share' entrypoint.
4942 * @param argc Standard main() style argc.
4943 * @param argv Standard main() style argv. Initial components are already
4947 int net_rpc_share(struct net_context
*c
, int argc
, const char **argv
)
4949 struct functable func
[] = {
4955 "net rpc share add\n"
4963 "net rpc share delete\n"
4968 rpc_share_allowedusers
,
4970 "Modify allowed users",
4971 "net rpc share allowedusers\n"
4972 " Modify allowed users"
4978 "Migrate share to local server",
4979 "net rpc share migrate\n"
4980 " Migrate share to local server"
4987 "net rpc share list\n"
4990 {NULL
, NULL
, 0, NULL
, NULL
}
4995 if (c
->display_usage
) {
4999 " Alias for net rpc share list\n");
5000 net_display_usage_from_functable(func
);
5004 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
5005 rpc_share_list_internals
,
5009 return net_run_function(c
, argc
, argv
, "net rpc share", func
);
5012 static NTSTATUS
rpc_sh_share_list(struct net_context
*c
,
5013 TALLOC_CTX
*mem_ctx
,
5014 struct rpc_sh_ctx
*ctx
,
5015 struct rpc_pipe_client
*pipe_hnd
,
5016 int argc
, const char **argv
)
5018 return rpc_share_list_internals(c
, ctx
->domain_sid
, ctx
->domain_name
,
5019 ctx
->cli
, pipe_hnd
, mem_ctx
,
5023 static NTSTATUS
rpc_sh_share_add(struct net_context
*c
,
5024 TALLOC_CTX
*mem_ctx
,
5025 struct rpc_sh_ctx
*ctx
,
5026 struct rpc_pipe_client
*pipe_hnd
,
5027 int argc
, const char **argv
)
5031 uint32_t parm_err
= 0;
5032 union srvsvc_NetShareInfo info
;
5033 struct srvsvc_NetShareInfo2 info2
;
5035 if ((argc
< 2) || (argc
> 3)) {
5036 d_fprintf(stderr
, "usage: %s <share> <path> [comment]\n",
5038 return NT_STATUS_INVALID_PARAMETER
;
5041 info2
.name
= argv
[0];
5042 info2
.type
= STYPE_DISKTREE
;
5043 info2
.comment
= (argc
== 3) ? argv
[2] : "";
5044 info2
.permissions
= 0;
5045 info2
.max_users
= 0;
5046 info2
.current_users
= 0;
5047 info2
.path
= argv
[1];
5048 info2
.password
= NULL
;
5050 info
.info2
= &info2
;
5052 status
= rpccli_srvsvc_NetShareAdd(pipe_hnd
, mem_ctx
,
5062 static NTSTATUS
rpc_sh_share_delete(struct net_context
*c
,
5063 TALLOC_CTX
*mem_ctx
,
5064 struct rpc_sh_ctx
*ctx
,
5065 struct rpc_pipe_client
*pipe_hnd
,
5066 int argc
, const char **argv
)
5072 d_fprintf(stderr
, "usage: %s <share>\n", ctx
->whoami
);
5073 return NT_STATUS_INVALID_PARAMETER
;
5076 status
= rpccli_srvsvc_NetShareDel(pipe_hnd
, mem_ctx
,
5085 static NTSTATUS
rpc_sh_share_info(struct net_context
*c
,
5086 TALLOC_CTX
*mem_ctx
,
5087 struct rpc_sh_ctx
*ctx
,
5088 struct rpc_pipe_client
*pipe_hnd
,
5089 int argc
, const char **argv
)
5091 union srvsvc_NetShareInfo info
;
5096 d_fprintf(stderr
, "usage: %s <share>\n", ctx
->whoami
);
5097 return NT_STATUS_INVALID_PARAMETER
;
5100 status
= rpccli_srvsvc_NetShareGetInfo(pipe_hnd
, mem_ctx
,
5106 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
)) {
5110 d_printf("Name: %s\n", info
.info2
->name
);
5111 d_printf("Comment: %s\n", info
.info2
->comment
);
5112 d_printf("Path: %s\n", info
.info2
->path
);
5113 d_printf("Password: %s\n", info
.info2
->password
);
5116 return werror_to_ntstatus(result
);
5119 struct rpc_sh_cmd
*net_rpc_share_cmds(struct net_context
*c
, TALLOC_CTX
*mem_ctx
,
5120 struct rpc_sh_ctx
*ctx
)
5122 static struct rpc_sh_cmd cmds
[] = {
5124 { "list", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_list
,
5125 "List available shares" },
5127 { "add", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_add
,
5130 { "delete", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_delete
,
5133 { "info", NULL
, &ndr_table_srvsvc
.syntax_id
, rpc_sh_share_info
,
5134 "Get information about a share" },
5136 { NULL
, NULL
, 0, NULL
, NULL
}
5142 /****************************************************************************/
5144 static int rpc_file_usage(struct net_context
*c
, int argc
, const char **argv
)
5146 return net_file_usage(c
, argc
, argv
);
5150 * Close a file on a remote RPC server.
5152 * All parameters are provided by the run_rpc_command function, except for
5153 * argc, argv which are passed through.
5155 * @param c A net_context structure.
5156 * @param domain_sid The domain sid acquired from the remote server.
5157 * @param cli A cli_state connected to the server.
5158 * @param mem_ctx Talloc context, destroyed on completion of the function.
5159 * @param argc Standard main() style argc.
5160 * @param argv Standard main() style argv. Initial components are already
5163 * @return Normal NTSTATUS return.
5165 static NTSTATUS
rpc_file_close_internals(struct net_context
*c
,
5166 const DOM_SID
*domain_sid
,
5167 const char *domain_name
,
5168 struct cli_state
*cli
,
5169 struct rpc_pipe_client
*pipe_hnd
,
5170 TALLOC_CTX
*mem_ctx
,
5174 return rpccli_srvsvc_NetFileClose(pipe_hnd
, mem_ctx
,
5176 atoi(argv
[0]), NULL
);
5180 * Close a file on a remote RPC server.
5182 * @param argc Standard main() style argc.
5183 * @param argv Standard main() style argv. Initial components are already
5186 * @return A shell status integer (0 for success).
5188 static int rpc_file_close(struct net_context
*c
, int argc
, const char **argv
)
5190 if (argc
< 1 || c
->display_usage
) {
5191 return rpc_file_usage(c
, argc
, argv
);
5194 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
5195 rpc_file_close_internals
,
5200 * Formatted print of open file info
5202 * @param r struct srvsvc_NetFileInfo3 contents
5205 static void display_file_info_3(struct srvsvc_NetFileInfo3
*r
)
5207 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5208 r
->fid
, r
->user
, r
->permissions
, r
->num_locks
, r
->path
);
5212 * List open files on a remote RPC server.
5214 * All parameters are provided by the run_rpc_command function, except for
5215 * argc, argv which are passed through.
5217 * @param c A net_context structure.
5218 * @param domain_sid The domain sid acquired from the remote server.
5219 * @param cli A cli_state connected to the server.
5220 * @param mem_ctx Talloc context, destroyed on completion of the function.
5221 * @param argc Standard main() style argc.
5222 * @param argv Standard main() style argv. Initial components are already
5225 * @return Normal NTSTATUS return.
5228 static NTSTATUS
rpc_file_list_internals(struct net_context
*c
,
5229 const DOM_SID
*domain_sid
,
5230 const char *domain_name
,
5231 struct cli_state
*cli
,
5232 struct rpc_pipe_client
*pipe_hnd
,
5233 TALLOC_CTX
*mem_ctx
,
5237 struct srvsvc_NetFileInfoCtr info_ctr
;
5238 struct srvsvc_NetFileCtr3 ctr3
;
5241 uint32 preferred_len
= 0xffffffff, i
;
5242 const char *username
=NULL
;
5243 uint32_t total_entries
= 0;
5244 uint32_t resume_handle
= 0;
5246 /* if argc > 0, must be user command */
5248 username
= smb_xstrdup(argv
[0]);
5250 ZERO_STRUCT(info_ctr
);
5254 info_ctr
.ctr
.ctr3
= &ctr3
;
5256 status
= rpccli_srvsvc_NetFileEnum(pipe_hnd
, mem_ctx
,
5266 if (!NT_STATUS_IS_OK(status
) || !W_ERROR_IS_OK(result
))
5269 /* Display results */
5272 "\nEnumerating open files on remote server:\n\n"
5273 "\nFileId Opened by Perms Locks Path"
5274 "\n------ --------- ----- ----- ---- \n");
5275 for (i
= 0; i
< total_entries
; i
++)
5276 display_file_info_3(&info_ctr
.ctr
.ctr3
->array
[i
]);
5278 return W_ERROR_IS_OK(result
) ? NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
5282 * List files for a user on a remote RPC server.
5284 * @param argc Standard main() style argc.
5285 * @param argv Standard main() style argv. Initial components are already
5288 * @return A shell status integer (0 for success)..
5291 static int rpc_file_user(struct net_context
*c
, int argc
, const char **argv
)
5293 if (argc
< 1 || c
->display_usage
) {
5294 return rpc_file_usage(c
, argc
, argv
);
5297 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
5298 rpc_file_list_internals
,
5303 * 'net rpc file' entrypoint.
5304 * @param argc Standard main() style argc.
5305 * @param argv Standard main() style argv. Initial components are already
5309 int net_rpc_file(struct net_context
*c
, int argc
, const char **argv
)
5311 struct functable func
[] = {
5316 "Close opened file",
5317 "net rpc file close\n"
5318 " Close opened file"
5324 "List files opened by user",
5325 "net rpc file user\n"
5326 " List files opened by user"
5333 "Display information about opened file",
5334 "net rpc file info\n"
5335 " Display information about opened file"
5338 {NULL
, NULL
, 0, NULL
, NULL
}
5342 if (c
->display_usage
) {
5343 d_printf("Usage:\n");
5344 d_printf("net rpc file\n"
5345 " List opened files\n");
5346 net_display_usage_from_functable(func
);
5350 return run_rpc_command(c
, NULL
, &ndr_table_srvsvc
.syntax_id
, 0,
5351 rpc_file_list_internals
,
5355 return net_run_function(c
, argc
, argv
, "net rpc file", func
);
5359 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5361 * All parameters are provided by the run_rpc_command function, except for
5362 * argc, argv which are passed through.
5364 * @param c A net_context structure.
5365 * @param domain_sid The domain sid acquired from the remote server.
5366 * @param cli A cli_state connected to the server.
5367 * @param mem_ctx Talloc context, destroyed on completion of the function.
5368 * @param argc Standard main() style argc.
5369 * @param argv Standard main() style argv. Initial components are already
5372 * @return Normal NTSTATUS return.
5375 static NTSTATUS
rpc_shutdown_abort_internals(struct net_context
*c
,
5376 const DOM_SID
*domain_sid
,
5377 const char *domain_name
,
5378 struct cli_state
*cli
,
5379 struct rpc_pipe_client
*pipe_hnd
,
5380 TALLOC_CTX
*mem_ctx
,
5384 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5386 result
= rpccli_initshutdown_Abort(pipe_hnd
, mem_ctx
, NULL
, NULL
);
5388 if (NT_STATUS_IS_OK(result
)) {
5389 d_printf("\nShutdown successfully aborted\n");
5390 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5392 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5398 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5400 * All parameters are provided by the run_rpc_command function, except for
5401 * argc, argv which are passed through.
5403 * @param c A net_context structure.
5404 * @param domain_sid The domain sid acquired from the remote server.
5405 * @param cli A cli_state connected to the server.
5406 * @param mem_ctx Talloc context, destroyed on completion of the function.
5407 * @param argc Standard main() style argc.
5408 * @param argv Standard main() style argv. Initial components are already
5411 * @return Normal NTSTATUS return.
5414 static NTSTATUS
rpc_reg_shutdown_abort_internals(struct net_context
*c
,
5415 const DOM_SID
*domain_sid
,
5416 const char *domain_name
,
5417 struct cli_state
*cli
,
5418 struct rpc_pipe_client
*pipe_hnd
,
5419 TALLOC_CTX
*mem_ctx
,
5423 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5425 result
= rpccli_winreg_AbortSystemShutdown(pipe_hnd
, mem_ctx
, NULL
, NULL
);
5427 if (NT_STATUS_IS_OK(result
)) {
5428 d_printf("\nShutdown successfully aborted\n");
5429 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5431 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5437 * ABORT the shutdown of a remote RPC server.
5439 * @param argc Standard main() style argc.
5440 * @param argv Standard main() style argv. Initial components are already
5443 * @return A shell status integer (0 for success).
5446 static int rpc_shutdown_abort(struct net_context
*c
, int argc
,
5451 if (c
->display_usage
) {
5453 "net rpc abortshutdown\n"
5454 " Abort a scheduled shutdown\n");
5458 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
5459 rpc_shutdown_abort_internals
, argc
, argv
);
5464 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5466 return run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
5467 rpc_reg_shutdown_abort_internals
,
5472 * Shut down a remote RPC Server via initshutdown pipe.
5474 * All parameters are provided by the run_rpc_command function, except for
5475 * argc, argv which are passed through.
5477 * @param c A net_context structure.
5478 * @param domain_sid The domain sid acquired from the remote server.
5479 * @param cli A cli_state connected to the server.
5480 * @param mem_ctx Talloc context, destroyed on completion of the function.
5481 * @param argc Standard main() style argc.
5482 * @param argv Standard main() style argv. Initial components are already
5485 * @return Normal NTSTATUS return.
5488 NTSTATUS
rpc_init_shutdown_internals(struct net_context
*c
,
5489 const DOM_SID
*domain_sid
,
5490 const char *domain_name
,
5491 struct cli_state
*cli
,
5492 struct rpc_pipe_client
*pipe_hnd
,
5493 TALLOC_CTX
*mem_ctx
,
5497 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5498 const char *msg
= "This machine will be shutdown shortly";
5499 uint32 timeout
= 20;
5500 struct initshutdown_String msg_string
;
5501 struct initshutdown_String_sub s
;
5503 if (c
->opt_comment
) {
5504 msg
= c
->opt_comment
;
5506 if (c
->opt_timeout
) {
5507 timeout
= c
->opt_timeout
;
5511 msg_string
.name
= &s
;
5513 /* create an entry */
5514 result
= rpccli_initshutdown_Init(pipe_hnd
, mem_ctx
, NULL
,
5515 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5518 if (NT_STATUS_IS_OK(result
)) {
5519 d_printf("\nShutdown of remote machine succeeded\n");
5520 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5522 DEBUG(1,("Shutdown of remote machine failed!\n"));
5528 * Shut down a remote RPC Server via winreg pipe.
5530 * All parameters are provided by the run_rpc_command function, except for
5531 * argc, argv which are passed through.
5533 * @param c A net_context structure.
5534 * @param domain_sid The domain sid acquired from the remote server.
5535 * @param cli A cli_state connected to the server.
5536 * @param mem_ctx Talloc context, destroyed on completion of the function.
5537 * @param argc Standard main() style argc.
5538 * @param argv Standard main() style argv. Initial components are already
5541 * @return Normal NTSTATUS return.
5544 NTSTATUS
rpc_reg_shutdown_internals(struct net_context
*c
,
5545 const DOM_SID
*domain_sid
,
5546 const char *domain_name
,
5547 struct cli_state
*cli
,
5548 struct rpc_pipe_client
*pipe_hnd
,
5549 TALLOC_CTX
*mem_ctx
,
5553 const char *msg
= "This machine will be shutdown shortly";
5554 uint32 timeout
= 20;
5555 struct initshutdown_String msg_string
;
5556 struct initshutdown_String_sub s
;
5560 if (c
->opt_comment
) {
5561 msg
= c
->opt_comment
;
5564 msg_string
.name
= &s
;
5566 if (c
->opt_timeout
) {
5567 timeout
= c
->opt_timeout
;
5570 /* create an entry */
5571 result
= rpccli_winreg_InitiateSystemShutdown(pipe_hnd
, mem_ctx
, NULL
,
5572 &msg_string
, timeout
, c
->opt_force
, c
->opt_reboot
,
5575 if (NT_STATUS_IS_OK(result
)) {
5576 d_printf("\nShutdown of remote machine succeeded\n");
5578 d_fprintf(stderr
, "\nShutdown of remote machine failed\n");
5579 if ( W_ERROR_EQUAL(werr
, WERR_MACHINE_LOCKED
) )
5580 d_fprintf(stderr
, "\nMachine locked, use -f switch to force\n");
5582 d_fprintf(stderr
, "\nresult was: %s\n", dos_errstr(werr
));
5589 * Shut down a remote RPC server.
5591 * @param argc Standard main() style argc.
5592 * @param argv Standard main() style argv. Initial components are already
5595 * @return A shell status integer (0 for success).
5598 static int rpc_shutdown(struct net_context
*c
, int argc
, const char **argv
)
5602 if (c
->display_usage
) {
5604 "net rpc shutdown\n"
5605 " Shut down a remote RPC server\n");
5609 rc
= run_rpc_command(c
, NULL
, &ndr_table_initshutdown
.syntax_id
, 0,
5610 rpc_init_shutdown_internals
, argc
, argv
);
5613 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5614 rc
= run_rpc_command(c
, NULL
, &ndr_table_winreg
.syntax_id
, 0,
5615 rpc_reg_shutdown_internals
, argc
, argv
);
5621 /***************************************************************************
5622 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5623 ***************************************************************************/
5626 * Add interdomain trust account to the RPC server.
5627 * All parameters (except for argc and argv) are passed by run_rpc_command
5630 * @param c A net_context structure.
5631 * @param domain_sid The domain sid acquired from the server.
5632 * @param cli A cli_state connected to the server.
5633 * @param mem_ctx Talloc context, destroyed on completion of the function.
5634 * @param argc Standard main() style argc.
5635 * @param argv Standard main() style argv. Initial components are already
5638 * @return normal NTSTATUS return code.
5641 static NTSTATUS
rpc_trustdom_add_internals(struct net_context
*c
,
5642 const DOM_SID
*domain_sid
,
5643 const char *domain_name
,
5644 struct cli_state
*cli
,
5645 struct rpc_pipe_client
*pipe_hnd
,
5646 TALLOC_CTX
*mem_ctx
,
5650 POLICY_HND connect_pol
, domain_pol
, user_pol
;
5651 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5653 struct lsa_String lsa_acct_name
;
5655 uint32 acct_flags
=0;
5657 uint32_t access_granted
= 0;
5658 union samr_UserInfo info
;
5659 unsigned int orig_timeout
;
5662 d_printf("Usage: net rpc trustdom add <domain_name> "
5663 "<trust password>\n");
5664 return NT_STATUS_INVALID_PARAMETER
;
5668 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5671 if (asprintf(&acct_name
, "%s$", argv
[0]) < 0) {
5672 return NT_STATUS_NO_MEMORY
;
5675 strupper_m(acct_name
);
5677 init_lsa_String(&lsa_acct_name
, acct_name
);
5679 /* Get samr policy handle */
5680 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
5682 MAXIMUM_ALLOWED_ACCESS
,
5684 if (!NT_STATUS_IS_OK(result
)) {
5688 /* Get domain policy handle */
5689 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
5691 MAXIMUM_ALLOWED_ACCESS
,
5692 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5694 if (!NT_STATUS_IS_OK(result
)) {
5698 /* This call can take a long time - allow the server to time out.
5699 * 35 seconds should do it. */
5701 orig_timeout
= rpccli_set_timeout(pipe_hnd
, 35000);
5703 /* Create trusting domain's account */
5704 acb_info
= ACB_NORMAL
;
5705 acct_flags
= SEC_GENERIC_READ
| SEC_GENERIC_WRITE
| SEC_GENERIC_EXECUTE
|
5706 SEC_STD_WRITE_DAC
| SEC_STD_DELETE
|
5707 SAMR_USER_ACCESS_SET_PASSWORD
|
5708 SAMR_USER_ACCESS_GET_ATTRIBUTES
|
5709 SAMR_USER_ACCESS_SET_ATTRIBUTES
;
5711 result
= rpccli_samr_CreateUser2(pipe_hnd
, mem_ctx
,
5720 /* And restore our original timeout. */
5721 rpccli_set_timeout(pipe_hnd
, orig_timeout
);
5723 if (!NT_STATUS_IS_OK(result
)) {
5724 d_printf("net rpc trustdom add: create user %s failed %s\n",
5725 acct_name
, nt_errstr(result
));
5731 struct samr_LogonHours hours
;
5732 struct lsa_BinaryString parameters
;
5733 const int units_per_week
= 168;
5736 encode_pw_buffer(pwbuf
, argv
[1], STR_UNICODE
);
5738 ZERO_STRUCT(notime
);
5740 ZERO_STRUCT(parameters
);
5742 hours
.bits
= talloc_array(mem_ctx
, uint8_t, units_per_week
);
5744 result
= NT_STATUS_NO_MEMORY
;
5747 hours
.units_per_week
= units_per_week
;
5748 memset(hours
.bits
, 0xFF, units_per_week
);
5750 init_samr_user_info23(&info
.info23
,
5751 notime
, notime
, notime
,
5752 notime
, notime
, notime
,
5753 NULL
, NULL
, NULL
, NULL
, NULL
,
5754 NULL
, NULL
, NULL
, NULL
, ¶meters
,
5755 0, 0, ACB_DOMTRUST
, SAMR_FIELD_ACCT_FLAGS
,
5757 0, 0, 0, 0, 0, 0, 0,
5760 SamOEMhashBlob(info
.info23
.password
.data
, 516,
5761 &cli
->user_session_key
);
5763 result
= rpccli_samr_SetUserInfo2(pipe_hnd
, mem_ctx
,
5768 if (!NT_STATUS_IS_OK(result
)) {
5769 DEBUG(0,("Could not set trust account password: %s\n",
5770 nt_errstr(result
)));
5776 SAFE_FREE(acct_name
);
5781 * Create interdomain trust account for a remote domain.
5783 * @param argc Standard argc.
5784 * @param argv Standard argv without initial components.
5786 * @return Integer status (0 means success).
5789 static int rpc_trustdom_add(struct net_context
*c
, int argc
, const char **argv
)
5791 if (argc
> 0 && !c
->display_usage
) {
5792 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5793 rpc_trustdom_add_internals
, argc
, argv
);
5796 "net rpc trustdom add <domain_name> <trust password>\n");
5803 * Remove interdomain trust account from the RPC server.
5804 * All parameters (except for argc and argv) are passed by run_rpc_command
5807 * @param c A net_context structure.
5808 * @param domain_sid The domain sid acquired from the server.
5809 * @param cli A cli_state connected to the server.
5810 * @param mem_ctx Talloc context, destroyed on completion of the function.
5811 * @param argc Standard main() style argc.
5812 * @param argv Standard main() style argv. Initial components are already
5815 * @return normal NTSTATUS return code.
5818 static NTSTATUS
rpc_trustdom_del_internals(struct net_context
*c
,
5819 const DOM_SID
*domain_sid
,
5820 const char *domain_name
,
5821 struct cli_state
*cli
,
5822 struct rpc_pipe_client
*pipe_hnd
,
5823 TALLOC_CTX
*mem_ctx
,
5827 POLICY_HND connect_pol
, domain_pol
, user_pol
;
5828 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
5830 DOM_SID trust_acct_sid
;
5831 struct samr_Ids user_rids
, name_types
;
5832 struct lsa_String lsa_acct_name
;
5835 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5836 return NT_STATUS_INVALID_PARAMETER
;
5840 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5842 acct_name
= talloc_asprintf(mem_ctx
, "%s$", argv
[0]);
5844 if (acct_name
== NULL
)
5845 return NT_STATUS_NO_MEMORY
;
5847 strupper_m(acct_name
);
5849 /* Get samr policy handle */
5850 result
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
5852 MAXIMUM_ALLOWED_ACCESS
,
5854 if (!NT_STATUS_IS_OK(result
)) {
5858 /* Get domain policy handle */
5859 result
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
5861 MAXIMUM_ALLOWED_ACCESS
,
5862 CONST_DISCARD(struct dom_sid2
*, domain_sid
),
5864 if (!NT_STATUS_IS_OK(result
)) {
5868 init_lsa_String(&lsa_acct_name
, acct_name
);
5870 result
= rpccli_samr_LookupNames(pipe_hnd
, mem_ctx
,
5877 if (!NT_STATUS_IS_OK(result
)) {
5878 d_printf("net rpc trustdom del: LookupNames on user %s failed %s\n",
5879 acct_name
, nt_errstr(result
) );
5883 result
= rpccli_samr_OpenUser(pipe_hnd
, mem_ctx
,
5885 MAXIMUM_ALLOWED_ACCESS
,
5889 if (!NT_STATUS_IS_OK(result
)) {
5890 d_printf("net rpc trustdom del: OpenUser on user %s failed %s\n",
5891 acct_name
, nt_errstr(result
) );
5895 /* append the rid to the domain sid */
5896 sid_copy(&trust_acct_sid
, domain_sid
);
5897 if (!sid_append_rid(&trust_acct_sid
, user_rids
.ids
[0])) {
5901 /* remove the sid */
5903 result
= rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd
, mem_ctx
,
5906 if (!NT_STATUS_IS_OK(result
)) {
5907 d_printf("net rpc trustdom del: RemoveMemberFromForeignDomain on user %s failed %s\n",
5908 acct_name
, nt_errstr(result
) );
5914 result
= rpccli_samr_DeleteUser(pipe_hnd
, mem_ctx
,
5917 if (!NT_STATUS_IS_OK(result
)) {
5918 d_printf("net rpc trustdom del: DeleteUser on user %s failed %s\n",
5919 acct_name
, nt_errstr(result
) );
5923 if (!NT_STATUS_IS_OK(result
)) {
5924 d_printf("Could not set trust account password: %s\n",
5934 * Delete interdomain trust account for a remote domain.
5936 * @param argc Standard argc.
5937 * @param argv Standard argv without initial components.
5939 * @return Integer status (0 means success).
5942 static int rpc_trustdom_del(struct net_context
*c
, int argc
, const char **argv
)
5944 if (argc
> 0 && !c
->display_usage
) {
5945 return run_rpc_command(c
, NULL
, &ndr_table_samr
.syntax_id
, 0,
5946 rpc_trustdom_del_internals
, argc
, argv
);
5949 "net rpc trustdom del <domain>\n");
5954 static NTSTATUS
rpc_trustdom_get_pdc(struct net_context
*c
,
5955 struct cli_state
*cli
,
5956 TALLOC_CTX
*mem_ctx
,
5957 const char *domain_name
)
5959 char *dc_name
= NULL
;
5960 const char *buffer
= NULL
;
5961 struct rpc_pipe_client
*netr
;
5964 /* Use NetServerEnum2 */
5966 if (cli_get_pdc_name(cli
, domain_name
, &dc_name
)) {
5968 return NT_STATUS_OK
;
5971 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5972 for domain %s\n", domain_name
));
5974 /* Try netr_GetDcName */
5976 status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_netlogon
.syntax_id
,
5978 if (!NT_STATUS_IS_OK(status
)) {
5982 status
= rpccli_netr_GetDcName(netr
, mem_ctx
,
5989 if (NT_STATUS_IS_OK(status
)) {
5993 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5994 for domain %s\n", domain_name
));
6000 * Establish trust relationship to a trusting domain.
6001 * Interdomain account must already be created on remote PDC.
6003 * @param c A net_context structure.
6004 * @param argc Standard argc.
6005 * @param argv Standard argv without initial components.
6007 * @return Integer status (0 means success).
6010 static int rpc_trustdom_establish(struct net_context
*c
, int argc
,
6013 struct cli_state
*cli
= NULL
;
6014 struct sockaddr_storage server_ss
;
6015 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6016 POLICY_HND connect_hnd
;
6017 TALLOC_CTX
*mem_ctx
;
6019 DOM_SID
*domain_sid
;
6024 union lsa_PolicyInformation
*info
= NULL
;
6027 * Connect to \\server\ipc$ as 'our domain' account with password
6030 if (argc
!= 1 || c
->display_usage
) {
6032 "net rpc trustdom establish <domain_name>\n");
6036 domain_name
= smb_xstrdup(argv
[0]);
6037 strupper_m(domain_name
);
6039 /* account name used at first is our domain's name with '$' */
6040 asprintf(&acct_name
, "%s$", lp_workgroup());
6041 strupper_m(acct_name
);
6044 * opt_workgroup will be used by connection functions further,
6045 * hence it should be set to remote domain name instead of ours
6047 if (c
->opt_workgroup
) {
6048 c
->opt_workgroup
= smb_xstrdup(domain_name
);
6051 c
->opt_user_name
= acct_name
;
6053 /* find the domain controller */
6054 if (!net_find_pdc(&server_ss
, pdc_name
, domain_name
)) {
6055 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name
));
6059 /* connect to ipc$ as username/password */
6060 nt_status
= connect_to_ipc(c
, &cli
, &server_ss
, pdc_name
);
6061 if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
)) {
6063 /* Is it trusting domain account for sure ? */
6064 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6065 nt_errstr(nt_status
)));
6069 /* store who we connected to */
6071 saf_store( domain_name
, pdc_name
);
6074 * Connect to \\server\ipc$ again (this time anonymously)
6077 nt_status
= connect_to_ipc_anonymous(c
, &cli
, &server_ss
,
6080 if (NT_STATUS_IS_ERR(nt_status
)) {
6081 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6082 domain_name
, nt_errstr(nt_status
)));
6086 if (!(mem_ctx
= talloc_init("establishing trust relationship to "
6087 "domain %s", domain_name
))) {
6088 DEBUG(0, ("talloc_init() failed\n"));
6093 /* Make sure we're talking to a proper server */
6095 nt_status
= rpc_trustdom_get_pdc(c
, cli
, mem_ctx
, domain_name
);
6096 if (!NT_STATUS_IS_OK(nt_status
)) {
6098 talloc_destroy(mem_ctx
);
6103 * Call LsaOpenPolicy and LsaQueryInfo
6106 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6108 if (!NT_STATUS_IS_OK(nt_status
)) {
6109 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status
) ));
6111 talloc_destroy(mem_ctx
);
6115 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, true, SEC_RIGHTS_QUERY_VALUE
,
6117 if (NT_STATUS_IS_ERR(nt_status
)) {
6118 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6119 nt_errstr(nt_status
)));
6121 talloc_destroy(mem_ctx
);
6125 /* Querying info level 5 */
6127 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
6129 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6131 if (NT_STATUS_IS_ERR(nt_status
)) {
6132 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6133 nt_errstr(nt_status
)));
6135 talloc_destroy(mem_ctx
);
6139 domain_sid
= info
->account_domain
.sid
;
6141 /* There should be actually query info level 3 (following nt serv behaviour),
6142 but I still don't know if it's _really_ necessary */
6145 * Store the password in secrets db
6148 if (!pdb_set_trusteddom_pw(domain_name
, c
->opt_password
, domain_sid
)) {
6149 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6151 talloc_destroy(mem_ctx
);
6156 * Close the pipes and clean up
6159 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6160 if (NT_STATUS_IS_ERR(nt_status
)) {
6161 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6162 nt_errstr(nt_status
)));
6164 talloc_destroy(mem_ctx
);
6170 talloc_destroy(mem_ctx
);
6172 d_printf("Trust to domain %s established\n", domain_name
);
6177 * Revoke trust relationship to the remote domain.
6179 * @param c A net_context structure.
6180 * @param argc Standard argc.
6181 * @param argv Standard argv without initial components.
6183 * @return Integer status (0 means success).
6186 static int rpc_trustdom_revoke(struct net_context
*c
, int argc
,
6192 if (argc
< 1 || c
->display_usage
) {
6194 "net rpc trustdom revoke <domain_name>\n"
6195 " Revoke trust relationship\n"
6196 " domain_name\tName of domain to revoke trust\n");
6200 /* generate upper cased domain name */
6201 domain_name
= smb_xstrdup(argv
[0]);
6202 strupper_m(domain_name
);
6204 /* delete password of the trust */
6205 if (!pdb_del_trusteddom_pw(domain_name
)) {
6206 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6213 SAFE_FREE(domain_name
);
6217 static NTSTATUS
rpc_query_domain_sid(struct net_context
*c
,
6218 const DOM_SID
*domain_sid
,
6219 const char *domain_name
,
6220 struct cli_state
*cli
,
6221 struct rpc_pipe_client
*pipe_hnd
,
6222 TALLOC_CTX
*mem_ctx
,
6227 sid_to_fstring(str_sid
, domain_sid
);
6228 d_printf("%s\n", str_sid
);
6229 return NT_STATUS_OK
;
6232 static void print_trusted_domain(DOM_SID
*dom_sid
, const char *trusted_dom_name
)
6234 fstring ascii_sid
, padding
;
6235 int pad_len
, col_len
= 20;
6237 /* convert sid into ascii string */
6238 sid_to_fstring(ascii_sid
, dom_sid
);
6240 /* calculate padding space for d_printf to look nicer */
6241 pad_len
= col_len
- strlen(trusted_dom_name
);
6242 padding
[pad_len
] = 0;
6243 do padding
[--pad_len
] = ' '; while (pad_len
);
6245 d_printf("%s%s%s\n", trusted_dom_name
, padding
, ascii_sid
);
6248 static NTSTATUS
vampire_trusted_domain(struct rpc_pipe_client
*pipe_hnd
,
6249 TALLOC_CTX
*mem_ctx
,
6252 const char *trusted_dom_name
)
6255 union lsa_TrustedDomainInfo
*info
= NULL
;
6256 char *cleartextpwd
= NULL
;
6257 uint8_t nt_hash
[16];
6260 nt_status
= rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd
, mem_ctx
,
6263 LSA_TRUSTED_DOMAIN_INFO_PASSWORD
,
6265 if (NT_STATUS_IS_ERR(nt_status
)) {
6266 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6267 nt_errstr(nt_status
)));
6271 data
= data_blob(info
->password
.password
->data
,
6272 info
->password
.password
->length
);
6274 if (!rpccli_get_pwd_hash(pipe_hnd
, nt_hash
)) {
6275 DEBUG(0, ("Could not retrieve password hash\n"));
6279 cleartextpwd
= decrypt_trustdom_secret(nt_hash
, &data
);
6281 if (cleartextpwd
== NULL
) {
6282 DEBUG(0,("retrieved NULL password\n"));
6283 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6287 if (!pdb_set_trusteddom_pw(trusted_dom_name
, cleartextpwd
, &dom_sid
)) {
6288 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6289 nt_status
= NT_STATUS_UNSUCCESSFUL
;
6293 #ifdef DEBUG_PASSWORD
6294 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6295 "password: [%s]\n", trusted_dom_name
,
6296 sid_string_dbg(&dom_sid
), cleartextpwd
));
6300 SAFE_FREE(cleartextpwd
);
6301 data_blob_free(&data
);
6306 static int rpc_trustdom_vampire(struct net_context
*c
, int argc
,
6309 /* common variables */
6310 TALLOC_CTX
* mem_ctx
;
6311 struct cli_state
*cli
= NULL
;
6312 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6314 const char *domain_name
= NULL
;
6315 DOM_SID
*queried_dom_sid
;
6316 POLICY_HND connect_hnd
;
6317 union lsa_PolicyInformation
*info
= NULL
;
6319 /* trusted domains listing variables */
6320 unsigned int enum_ctx
= 0;
6322 struct lsa_DomainList dom_list
;
6325 if (c
->display_usage
) {
6327 "net rpc trustdom vampire\n"
6328 " Vampire trust relationship from remote server\n");
6333 * Listing trusted domains (stored in secrets.tdb, if local)
6336 mem_ctx
= talloc_init("trust relationships vampire");
6339 * set domain and pdc name to local samba server (default)
6340 * or to remote one given in command line
6343 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
6344 domain_name
= c
->opt_workgroup
;
6345 c
->opt_target_workgroup
= c
->opt_workgroup
;
6347 fstrcpy(pdc_name
, global_myname());
6348 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6349 c
->opt_target_workgroup
= domain_name
;
6352 /* open \PIPE\lsarpc and open policy handle */
6353 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6354 if (!NT_STATUS_IS_OK(nt_status
)) {
6355 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6356 nt_errstr(nt_status
)));
6357 talloc_destroy(mem_ctx
);
6361 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6363 if (!NT_STATUS_IS_OK(nt_status
)) {
6364 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6365 nt_errstr(nt_status
) ));
6367 talloc_destroy(mem_ctx
);
6371 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, SEC_RIGHTS_QUERY_VALUE
,
6373 if (NT_STATUS_IS_ERR(nt_status
)) {
6374 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6375 nt_errstr(nt_status
)));
6377 talloc_destroy(mem_ctx
);
6381 /* query info level 5 to obtain sid of a domain being queried */
6382 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
6384 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6387 if (NT_STATUS_IS_ERR(nt_status
)) {
6388 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6389 nt_errstr(nt_status
)));
6391 talloc_destroy(mem_ctx
);
6395 queried_dom_sid
= info
->account_domain
.sid
;
6398 * Keep calling LsaEnumTrustdom over opened pipe until
6399 * the end of enumeration is reached
6402 d_printf("Vampire trusted domains:\n\n");
6405 nt_status
= rpccli_lsa_EnumTrustDom(pipe_hnd
, mem_ctx
,
6410 if (NT_STATUS_IS_ERR(nt_status
)) {
6411 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6412 nt_errstr(nt_status
)));
6414 talloc_destroy(mem_ctx
);
6418 for (i
= 0; i
< dom_list
.count
; i
++) {
6420 print_trusted_domain(dom_list
.domains
[i
].sid
,
6421 dom_list
.domains
[i
].name
.string
);
6423 nt_status
= vampire_trusted_domain(pipe_hnd
, mem_ctx
, &connect_hnd
,
6424 *dom_list
.domains
[i
].sid
,
6425 dom_list
.domains
[i
].name
.string
);
6426 if (!NT_STATUS_IS_OK(nt_status
)) {
6428 talloc_destroy(mem_ctx
);
6434 * in case of no trusted domains say something rather
6435 * than just display blank line
6437 if (!dom_list
.count
) d_printf("none\n");
6439 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6441 /* close this connection before doing next one */
6442 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6443 if (NT_STATUS_IS_ERR(nt_status
)) {
6444 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6445 nt_errstr(nt_status
)));
6447 talloc_destroy(mem_ctx
);
6451 /* close lsarpc pipe and connection to IPC$ */
6454 talloc_destroy(mem_ctx
);
6458 static int rpc_trustdom_list(struct net_context
*c
, int argc
, const char **argv
)
6460 /* common variables */
6461 TALLOC_CTX
* mem_ctx
;
6462 struct cli_state
*cli
= NULL
, *remote_cli
= NULL
;
6463 struct rpc_pipe_client
*pipe_hnd
= NULL
;
6465 const char *domain_name
= NULL
;
6466 DOM_SID
*queried_dom_sid
;
6468 int ascii_dom_name_len
;
6469 POLICY_HND connect_hnd
;
6470 union lsa_PolicyInformation
*info
= NULL
;
6472 /* trusted domains listing variables */
6473 unsigned int num_domains
, enum_ctx
= 0;
6474 int i
, pad_len
, col_len
= 20;
6475 struct lsa_DomainList dom_list
;
6478 /* trusting domains listing variables */
6479 POLICY_HND domain_hnd
;
6480 struct samr_SamArray
*trusts
= NULL
;
6482 if (c
->display_usage
) {
6484 "net rpc trustdom list\n"
6485 " List trust relationships\n");
6490 * Listing trusted domains (stored in secrets.tdb, if local)
6493 mem_ctx
= talloc_init("trust relationships listing");
6496 * set domain and pdc name to local samba server (default)
6497 * or to remote one given in command line
6500 if (StrCaseCmp(c
->opt_workgroup
, lp_workgroup())) {
6501 domain_name
= c
->opt_workgroup
;
6502 c
->opt_target_workgroup
= c
->opt_workgroup
;
6504 fstrcpy(pdc_name
, global_myname());
6505 domain_name
= talloc_strdup(mem_ctx
, lp_workgroup());
6506 c
->opt_target_workgroup
= domain_name
;
6509 /* open \PIPE\lsarpc and open policy handle */
6510 nt_status
= net_make_ipc_connection(c
, NET_FLAGS_PDC
, &cli
);
6511 if (!NT_STATUS_IS_OK(nt_status
)) {
6512 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6513 nt_errstr(nt_status
)));
6514 talloc_destroy(mem_ctx
);
6518 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
6520 if (!NT_STATUS_IS_OK(nt_status
)) {
6521 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6522 nt_errstr(nt_status
) ));
6524 talloc_destroy(mem_ctx
);
6528 nt_status
= rpccli_lsa_open_policy2(pipe_hnd
, mem_ctx
, false, SEC_RIGHTS_QUERY_VALUE
,
6530 if (NT_STATUS_IS_ERR(nt_status
)) {
6531 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6532 nt_errstr(nt_status
)));
6534 talloc_destroy(mem_ctx
);
6538 /* query info level 5 to obtain sid of a domain being queried */
6539 nt_status
= rpccli_lsa_QueryInfoPolicy(pipe_hnd
, mem_ctx
,
6541 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
6544 if (NT_STATUS_IS_ERR(nt_status
)) {
6545 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6546 nt_errstr(nt_status
)));
6548 talloc_destroy(mem_ctx
);
6552 queried_dom_sid
= info
->account_domain
.sid
;
6555 * Keep calling LsaEnumTrustdom over opened pipe until
6556 * the end of enumeration is reached
6559 d_printf("Trusted domains list:\n\n");
6562 nt_status
= rpccli_lsa_EnumTrustDom(pipe_hnd
, mem_ctx
,
6567 if (NT_STATUS_IS_ERR(nt_status
)) {
6568 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6569 nt_errstr(nt_status
)));
6571 talloc_destroy(mem_ctx
);
6575 for (i
= 0; i
< dom_list
.count
; i
++) {
6576 print_trusted_domain(dom_list
.domains
[i
].sid
,
6577 dom_list
.domains
[i
].name
.string
);
6581 * in case of no trusted domains say something rather
6582 * than just display blank line
6584 if (!dom_list
.count
) d_printf("none\n");
6586 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6588 /* close this connection before doing next one */
6589 nt_status
= rpccli_lsa_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6590 if (NT_STATUS_IS_ERR(nt_status
)) {
6591 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6592 nt_errstr(nt_status
)));
6594 talloc_destroy(mem_ctx
);
6598 TALLOC_FREE(pipe_hnd
);
6601 * Listing trusting domains (stored in passdb backend, if local)
6604 d_printf("\nTrusting domains list:\n\n");
6607 * Open \PIPE\samr and get needed policy handles
6609 nt_status
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_samr
.syntax_id
,
6611 if (!NT_STATUS_IS_OK(nt_status
)) {
6612 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status
)));
6614 talloc_destroy(mem_ctx
);
6619 nt_status
= rpccli_samr_Connect2(pipe_hnd
, mem_ctx
,
6621 SA_RIGHT_SAM_OPEN_DOMAIN
,
6623 if (!NT_STATUS_IS_OK(nt_status
)) {
6624 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6625 nt_errstr(nt_status
)));
6627 talloc_destroy(mem_ctx
);
6631 /* SamrOpenDomain - we have to open domain policy handle in order to be
6632 able to enumerate accounts*/
6633 nt_status
= rpccli_samr_OpenDomain(pipe_hnd
, mem_ctx
,
6635 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS
,
6638 if (!NT_STATUS_IS_OK(nt_status
)) {
6639 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6640 nt_errstr(nt_status
)));
6642 talloc_destroy(mem_ctx
);
6647 * perform actual enumeration
6650 enum_ctx
= 0; /* reset enumeration context from last enumeration */
6653 nt_status
= rpccli_samr_EnumDomainUsers(pipe_hnd
, mem_ctx
,
6660 if (NT_STATUS_IS_ERR(nt_status
)) {
6661 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6662 nt_errstr(nt_status
)));
6664 talloc_destroy(mem_ctx
);
6668 for (i
= 0; i
< num_domains
; i
++) {
6670 char *str
= CONST_DISCARD(char *, trusts
->entries
[i
].name
.string
);
6673 * get each single domain's sid (do we _really_ need this ?):
6674 * 1) connect to domain's pdc
6675 * 2) query the pdc for domain's sid
6678 /* get rid of '$' tail */
6679 ascii_dom_name_len
= strlen(str
);
6680 if (ascii_dom_name_len
&& ascii_dom_name_len
< FSTRING_LEN
)
6681 str
[ascii_dom_name_len
- 1] = '\0';
6683 /* calculate padding space for d_printf to look nicer */
6684 pad_len
= col_len
- strlen(str
);
6685 padding
[pad_len
] = 0;
6686 do padding
[--pad_len
] = ' '; while (pad_len
);
6688 /* set opt_* variables to remote domain */
6690 c
->opt_workgroup
= talloc_strdup(mem_ctx
, str
);
6691 c
->opt_target_workgroup
= c
->opt_workgroup
;
6693 d_printf("%s%s", str
, padding
);
6695 /* connect to remote domain controller */
6696 nt_status
= net_make_ipc_connection(c
,
6697 NET_FLAGS_PDC
| NET_FLAGS_ANONYMOUS
,
6699 if (NT_STATUS_IS_OK(nt_status
)) {
6700 /* query for domain's sid */
6701 if (run_rpc_command(
6703 &ndr_table_lsarpc
.syntax_id
, 0,
6704 rpc_query_domain_sid
, argc
,
6706 d_fprintf(stderr
, "couldn't get domain's sid\n");
6708 cli_shutdown(remote_cli
);
6711 d_fprintf(stderr
, "domain controller is not "
6713 nt_errstr(nt_status
));
6717 if (!num_domains
) d_printf("none\n");
6719 } while (NT_STATUS_EQUAL(nt_status
, STATUS_MORE_ENTRIES
));
6721 /* close opened samr and domain policy handles */
6722 nt_status
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &domain_hnd
);
6723 if (!NT_STATUS_IS_OK(nt_status
)) {
6724 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name
));
6727 nt_status
= rpccli_samr_Close(pipe_hnd
, mem_ctx
, &connect_hnd
);
6728 if (!NT_STATUS_IS_OK(nt_status
)) {
6729 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name
));
6732 /* close samr pipe and connection to IPC$ */
6735 talloc_destroy(mem_ctx
);
6740 * Entrypoint for 'net rpc trustdom' code.
6742 * @param argc Standard argc.
6743 * @param argv Standard argv without initial components.
6745 * @return Integer status (0 means success).
6748 static int rpc_trustdom(struct net_context
*c
, int argc
, const char **argv
)
6750 struct functable func
[] = {
6755 "Add trusted domain's account",
6756 "net rpc trustdom add\n"
6757 " Add trusted domain's account"
6763 "Remove trusted domain's account",
6764 "net rpc trustdom del\n"
6765 " Remove trusted domain's account"
6769 rpc_trustdom_establish
,
6771 "Establish trust relationship",
6772 "net rpc trustdom establish\n"
6773 " Establish trust relationship"
6777 rpc_trustdom_revoke
,
6779 "Revoke trust relationship",
6780 "net rpc trustdom revoke\n"
6781 " Revoke trust relationship"
6787 "List domain trusts",
6788 "net rpc trustdom list\n"
6789 " List domain trusts"
6793 rpc_trustdom_vampire
,
6795 "Vampire trusts from remote server",
6796 "net rpc trustdom vampire\n"
6797 " Vampire trusts from remote server"
6799 {NULL
, NULL
, 0, NULL
, NULL
}
6802 return net_run_function(c
, argc
, argv
, "net rpc trustdom", func
);
6806 * Check if a server will take rpc commands
6807 * @param flags Type of server to connect to (PDC, DMB, localhost)
6808 * if the host is not explicitly specified
6809 * @return bool (true means rpc supported)
6811 bool net_rpc_check(struct net_context
*c
, unsigned flags
)
6813 struct cli_state
*cli
;
6815 struct sockaddr_storage server_ss
;
6816 char *server_name
= NULL
;
6819 /* flags (i.e. server type) may depend on command */
6820 if (!net_find_server(c
, NULL
, flags
, &server_ss
, &server_name
))
6823 if ((cli
= cli_initialise()) == NULL
) {
6827 status
= cli_connect(cli
, server_name
, &server_ss
);
6828 if (!NT_STATUS_IS_OK(status
))
6830 if (!attempt_netbios_session_request(&cli
, global_myname(),
6831 server_name
, &server_ss
))
6833 if (!cli_negprot(cli
))
6835 if (cli
->protocol
< PROTOCOL_NT1
)
6844 /* dump sam database via samsync rpc calls */
6845 static int rpc_samdump(struct net_context
*c
, int argc
, const char **argv
) {
6846 if (c
->display_usage
) {
6849 " Dump remote SAM database\n");
6853 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
6854 NET_FLAGS_ANONYMOUS
,
6855 rpc_samdump_internals
, argc
, argv
);
6858 /* syncronise sam database via samsync rpc calls */
6859 static int rpc_vampire(struct net_context
*c
, int argc
, const char **argv
)
6861 struct functable func
[] = {
6866 "Dump remote SAM database to ldif",
6867 "net rpc vampire ldif\n"
6868 " Dump remote SAM database to LDIF file or stdout"
6874 "Dump remote SAM database to Kerberos Keytab",
6875 "net rpc vampire keytab\n"
6876 " Dump remote SAM database to Kerberos keytab file"
6879 {NULL
, NULL
, 0, NULL
, NULL
}
6883 if (c
->display_usage
) {
6886 " Vampire remote SAM database\n");
6890 return run_rpc_command(c
, NULL
, &ndr_table_netlogon
.syntax_id
,
6891 NET_FLAGS_ANONYMOUS
,
6892 rpc_vampire_internals
,
6896 return net_run_function(c
, argc
, argv
, "net rpc vampire", func
);
6900 * Migrate everything from a print server.
6902 * @param c A net_context structure.
6903 * @param argc Standard main() style argc.
6904 * @param argv Standard main() style argv. Initial components are already
6907 * @return A shell status integer (0 for success).
6909 * The order is important !
6910 * To successfully add drivers the print queues have to exist !
6911 * Applying ACLs should be the last step, because you're easily locked out.
6914 static int rpc_printer_migrate_all(struct net_context
*c
, int argc
,
6919 if (c
->display_usage
) {
6921 "net rpc printer migrate all\n"
6922 " Migrate everything from a print server\n");
6927 d_printf("no server to migrate\n");
6931 ret
= run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6932 rpc_printer_migrate_printers_internals
, argc
,
6937 ret
= run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6938 rpc_printer_migrate_drivers_internals
, argc
,
6943 ret
= run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6944 rpc_printer_migrate_forms_internals
, argc
, argv
);
6948 ret
= run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6949 rpc_printer_migrate_settings_internals
, argc
,
6954 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6955 rpc_printer_migrate_security_internals
, argc
,
6961 * Migrate print drivers from a print server.
6963 * @param c A net_context structure.
6964 * @param argc Standard main() style argc.
6965 * @param argv Standard main() style argv. Initial components are already
6968 * @return A shell status integer (0 for success).
6970 static int rpc_printer_migrate_drivers(struct net_context
*c
, int argc
,
6973 if (c
->display_usage
) {
6975 "net rpc printer migrate drivers\n"
6976 " Migrate print-drivers from a print-server\n");
6981 d_printf("no server to migrate\n");
6985 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
6986 rpc_printer_migrate_drivers_internals
,
6991 * Migrate print-forms from a print-server.
6993 * @param c A net_context structure.
6994 * @param argc Standard main() style argc.
6995 * @param argv Standard main() style argv. Initial components are already
6998 * @return A shell status integer (0 for success).
7000 static int rpc_printer_migrate_forms(struct net_context
*c
, int argc
,
7003 if (c
->display_usage
) {
7005 "net rpc printer migrate forms\n"
7006 " Migrate print-forms from a print-server\n");
7011 d_printf("no server to migrate\n");
7015 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
7016 rpc_printer_migrate_forms_internals
,
7021 * Migrate printers from a print-server.
7023 * @param c A net_context structure.
7024 * @param argc Standard main() style argc.
7025 * @param argv Standard main() style argv. Initial components are already
7028 * @return A shell status integer (0 for success).
7030 static int rpc_printer_migrate_printers(struct net_context
*c
, int argc
,
7033 if (c
->display_usage
) {
7035 "net rpc printer migrate printers\n"
7036 " Migrate printers from a print-server\n");
7041 d_printf("no server to migrate\n");
7045 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
7046 rpc_printer_migrate_printers_internals
,
7051 * Migrate printer-ACLs from a print-server
7053 * @param c A net_context structure.
7054 * @param argc Standard main() style argc.
7055 * @param argv Standard main() style argv. Initial components are already
7058 * @return A shell status integer (0 for success).
7060 static int rpc_printer_migrate_security(struct net_context
*c
, int argc
,
7063 if (c
->display_usage
) {
7065 "net rpc printer migrate security\n"
7066 " Migrate printer-ACLs from a print-server\n");
7071 d_printf("no server to migrate\n");
7075 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
7076 rpc_printer_migrate_security_internals
,
7081 * Migrate printer-settings from a print-server.
7083 * @param c A net_context structure.
7084 * @param argc Standard main() style argc.
7085 * @param argv Standard main() style argv. Initial components are already
7088 * @return A shell status integer (0 for success).
7090 static int rpc_printer_migrate_settings(struct net_context
*c
, int argc
,
7093 if (c
->display_usage
) {
7095 "net rpc printer migrate settings\n"
7096 " Migrate printer-settings from a print-server\n");
7101 d_printf("no server to migrate\n");
7105 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
7106 rpc_printer_migrate_settings_internals
,
7111 * 'net rpc printer' entrypoint.
7113 * @param c A net_context structure.
7114 * @param argc Standard main() style argc.
7115 * @param argv Standard main() style argv. Initial components are already
7119 int rpc_printer_migrate(struct net_context
*c
, int argc
, const char **argv
)
7122 /* ouch: when addriver and setdriver are called from within
7123 rpc_printer_migrate_drivers_internals, the printer-queue already
7126 struct functable func
[] = {
7129 rpc_printer_migrate_all
,
7131 "Migrate all from remote to local print server",
7132 "net rpc printer migrate all\n"
7133 " Migrate all from remote to local print server"
7137 rpc_printer_migrate_drivers
,
7139 "Migrate drivers to local server",
7140 "net rpc printer migrate drivers\n"
7141 " Migrate drivers to local server"
7145 rpc_printer_migrate_forms
,
7147 "Migrate froms to local server",
7148 "net rpc printer migrate forms\n"
7149 " Migrate froms to local server"
7153 rpc_printer_migrate_printers
,
7155 "Migrate printers to local server",
7156 "net rpc printer migrate printers\n"
7157 " Migrate printers to local server"
7161 rpc_printer_migrate_security
,
7163 "Mirgate printer ACLs to local server",
7164 "net rpc printer migrate security\n"
7165 " Mirgate printer ACLs to local server"
7169 rpc_printer_migrate_settings
,
7171 "Migrate printer settings to local server",
7172 "net rpc printer migrate settings\n"
7173 " Migrate printer settings to local server"
7175 {NULL
, NULL
, 0, NULL
, NULL
}
7178 return net_run_function(c
, argc
, argv
, "net rpc printer migrate",func
);
7183 * List printers on a remote RPC server.
7185 * @param c A net_context structure.
7186 * @param argc Standard main() style argc.
7187 * @param argv Standard main() style argv. Initial components are already
7190 * @return A shell status integer (0 for success).
7192 static int rpc_printer_list(struct net_context
*c
, int argc
, const char **argv
)
7194 if (c
->display_usage
) {
7196 "net rpc printer list\n"
7197 " List printers on a remote RPC server\n");
7201 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
7202 rpc_printer_list_internals
,
7207 * List printer-drivers on a remote RPC server.
7209 * @param c A net_context structure.
7210 * @param argc Standard main() style argc.
7211 * @param argv Standard main() style argv. Initial components are already
7214 * @return A shell status integer (0 for success).
7216 static int rpc_printer_driver_list(struct net_context
*c
, int argc
,
7219 if (c
->display_usage
) {
7221 "net rpc printer driver\n"
7222 " List printer-drivers on a remote RPC server\n");
7226 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
7227 rpc_printer_driver_list_internals
,
7232 * Publish printer in ADS via MSRPC.
7234 * @param c A net_context structure.
7235 * @param argc Standard main() style argc.
7236 * @param argv Standard main() style argv. Initial components are already
7239 * @return A shell status integer (0 for success).
7241 static int rpc_printer_publish_publish(struct net_context
*c
, int argc
,
7244 if (c
->display_usage
) {
7246 "net rpc printer publish publish\n"
7247 " Publish printer in ADS via MSRPC\n");
7251 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
7252 rpc_printer_publish_publish_internals
,
7257 * Update printer in ADS via MSRPC.
7259 * @param c A net_context structure.
7260 * @param argc Standard main() style argc.
7261 * @param argv Standard main() style argv. Initial components are already
7264 * @return A shell status integer (0 for success).
7266 static int rpc_printer_publish_update(struct net_context
*c
, int argc
, const char **argv
)
7268 if (c
->display_usage
) {
7270 "net rpc printer publish update\n"
7271 " Update printer in ADS via MSRPC\n");
7275 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
7276 rpc_printer_publish_update_internals
,
7281 * UnPublish printer in ADS via MSRPC.
7283 * @param c A net_context structure.
7284 * @param argc Standard main() style argc.
7285 * @param argv Standard main() style argv. Initial components are already
7288 * @return A shell status integer (0 for success).
7290 static int rpc_printer_publish_unpublish(struct net_context
*c
, int argc
,
7293 if (c
->display_usage
) {
7295 "net rpc printer publish unpublish\n"
7296 " UnPublish printer in ADS via MSRPC\n");
7300 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
7301 rpc_printer_publish_unpublish_internals
,
7306 * List published printers via MSRPC.
7308 * @param c A net_context structure.
7309 * @param argc Standard main() style argc.
7310 * @param argv Standard main() style argv. Initial components are already
7313 * @return A shell status integer (0 for success).
7315 static int rpc_printer_publish_list(struct net_context
*c
, int argc
,
7318 if (c
->display_usage
) {
7320 "net rpc printer publish list\n"
7321 " List published printers via MSRPC\n");
7325 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
7326 rpc_printer_publish_list_internals
,
7332 * Publish printer in ADS.
7334 * @param c A net_context structure.
7335 * @param argc Standard main() style argc.
7336 * @param argv Standard main() style argv. Initial components are already
7339 * @return A shell status integer (0 for success).
7341 static int rpc_printer_publish(struct net_context
*c
, int argc
,
7345 struct functable func
[] = {
7348 rpc_printer_publish_publish
,
7350 "Publish printer in AD",
7351 "net rpc printer publish publish\n"
7352 " Publish printer in AD"
7356 rpc_printer_publish_update
,
7358 "Update printer in AD",
7359 "net rpc printer publish update\n"
7360 " Update printer in AD"
7364 rpc_printer_publish_unpublish
,
7366 "Unpublish printer",
7367 "net rpc printer publish unpublish\n"
7368 " Unpublish printer"
7372 rpc_printer_publish_list
,
7374 "List published printers",
7375 "net rpc printer publish list\n"
7376 " List published printers"
7378 {NULL
, NULL
, 0, NULL
, NULL
}
7382 if (c
->display_usage
) {
7383 d_printf("Usage:\n");
7384 d_printf("net rpc printer publish\n"
7385 " List published printers\n"
7386 " Alias of net rpc printer publish list\n");
7387 net_display_usage_from_functable(func
);
7390 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
7391 rpc_printer_publish_list_internals
,
7395 return net_run_function(c
, argc
, argv
, "net rpc printer publish",func
);
7401 * Display rpc printer help page.
7403 * @param c A net_context structure.
7404 * @param argc Standard main() style argc.
7405 * @param argv Standard main() style argv. Initial components are already
7408 int rpc_printer_usage(struct net_context
*c
, int argc
, const char **argv
)
7410 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"
7411 "\tlists all printers on print-server\n\n");
7412 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7413 "\tlists all printer-drivers on print-server\n\n");
7414 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7415 "\tpublishes printer settings in Active Directory\n"
7416 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
7417 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7418 "\n\tmigrates printers from remote to local server\n\n");
7419 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7420 "\n\tmigrates printer-settings from remote to local server\n\n");
7421 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7422 "\n\tmigrates printer-drivers from remote to local server\n\n");
7423 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7424 "\n\tmigrates printer-forms from remote to local server\n\n");
7425 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7426 "\n\tmigrates printer-ACLs from remote to local server\n\n");
7427 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7428 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7429 "\tremote to local print-server\n\n");
7430 net_common_methods_usage(c
, argc
, argv
);
7431 net_common_flags_usage(c
, argc
, argv
);
7433 "\t-v or --verbose\t\t\tgive verbose output\n"
7434 "\t --destination\t\tmigration target server (default: localhost)\n");
7440 * 'net rpc printer' entrypoint.
7442 * @param c A net_context structure.
7443 * @param argc Standard main() style argc.
7444 * @param argv Standard main() style argv. Initial components are already
7447 int net_rpc_printer(struct net_context
*c
, int argc
, const char **argv
)
7449 struct functable func
[] = {
7454 "List all printers on print server",
7455 "net rpc printer list\n"
7456 " List all printers on print server"
7460 rpc_printer_migrate
,
7462 "Migrate printer to local server",
7463 "net rpc printer migrate\n"
7464 " Migrate printer to local server"
7468 rpc_printer_driver_list
,
7470 "List printer drivers",
7471 "net rpc printer driver\n"
7472 " List printer drivers"
7476 rpc_printer_publish
,
7478 "Publish printer in AD",
7479 "net rpc printer publish\n"
7480 " Publish printer in AD"
7482 {NULL
, NULL
, 0, NULL
, NULL
}
7486 if (c
->display_usage
) {
7487 d_printf("Usage:\n");
7488 d_printf("net rpc printer\n"
7489 " List printers\n");
7490 net_display_usage_from_functable(func
);
7493 return run_rpc_command(c
, NULL
, &syntax_spoolss
, 0,
7494 rpc_printer_list_internals
,
7498 return net_run_function(c
, argc
, argv
, "net rpc printer", func
);
7502 * 'net rpc' entrypoint.
7504 * @param c A net_context structure.
7505 * @param argc Standard main() style argc.
7506 * @param argv Standard main() style argv. Initial components are already
7510 int net_rpc(struct net_context
*c
, int argc
, const char **argv
)
7512 struct functable func
[] = {
7517 "Modify global audit settings",
7519 " Modify global audit settings"
7525 "Show basic info about a domain",
7527 " Show basic info about a domain"
7541 "Join a domain created in server manager",
7543 " Join a domain created in server manager"
7549 "Test that a join is valid",
7550 "net rpc testjoin\n"
7551 " Test that a join is valid"
7557 "List/modify users",
7559 " List/modify users"
7565 "Change a user password",
7566 "net rpc password\n"
7567 " Change a user password\n"
7568 " Alias for net rpc user password"
7574 "List/modify groups",
7576 " List/modify groups"
7582 "List/modify shares",
7584 " List/modify shares"
7598 "List/modify printers",
7600 " List/modify printers"
7604 net_rpc_changetrustpw
,
7606 "Change trust account password",
7607 "net rpc changetrustpw\n"
7608 " Change trust account password"
7614 "Modify domain trusts",
7615 "net rpc trustdom\n"
7616 " Modify domain trusts"
7622 "Abort a remote shutdown",
7623 "net rpc abortshutdown\n"
7624 " Abort a remote shutdown"
7630 "Shutdown a remote server",
7631 "net rpc shutdown\n"
7632 " Shutdown a remote server"
7638 "Dump SAM data of remote NT PDC",
7640 " Dump SAM data of remote NT PDC"
7646 "Sync a remote NT PDC's data into local passdb",
7648 " Sync a remote NT PDC's data into local passdb"
7654 "Fetch the domain sid into local secrets.tdb",
7656 " Fetch the domain sid into local secrets.tdb"
7662 "Manage privileges assigned to SID",
7664 " Manage privileges assigned to SID"
7670 "Start/stop/query remote services",
7672 " Start/stop/query remote services"
7678 "Manage registry hives",
7679 "net rpc registry\n"
7680 " Manage registry hives"
7686 "Open interactive shell on remote server",
7688 " Open interactive shell on remote server"
7690 {NULL
, NULL
, 0, NULL
, NULL
}
7692 return net_run_function(c
, argc
, argv
, "net rpc", func
);