2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2004 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #include "utils/net.h"
27 static int net_mode_share;
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,
54 DOM_SID **domain_sid, char **domain_name)
56 struct rpc_pipe_client *lsa_pipe;
58 NTSTATUS result = NT_STATUS_OK;
59 uint32 info_class = 5;
61 lsa_pipe = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
63 d_fprintf(stderr, "Could not initialise lsa pipe\n");
67 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, False,
68 SEC_RIGHTS_MAXIMUM_ALLOWED,
70 if (!NT_STATUS_IS_OK(result)) {
71 d_fprintf(stderr, "open_policy failed: %s\n",
76 result = rpccli_lsa_query_info_policy(lsa_pipe, mem_ctx, &pol,
77 info_class, domain_name,
79 if (!NT_STATUS_IS_OK(result)) {
80 d_fprintf(stderr, "lsaquery failed: %s\n",
85 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
86 cli_rpc_pipe_close(lsa_pipe);
92 * Run a single RPC command, from start to finish.
94 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
95 * @param conn_flag a NET_FLAG_ combination. Passed to
96 * net_make_ipc_connection.
97 * @param argc Standard main() style argc
98 * @param argc Standard main() style argv. Initial components are already
100 * @return A shell status integer (0 for success)
103 int run_rpc_command(struct cli_state *cli_arg,
110 struct cli_state *cli = NULL;
111 struct rpc_pipe_client *pipe_hnd = NULL;
117 /* make use of cli_state handed over as an argument, if possible */
119 cli = net_make_ipc_connection(conn_flags);
130 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
131 DEBUG(0, ("talloc_init() failed\n"));
136 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
138 if (!NT_STATUS_IS_OK(nt_status)) {
143 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
144 if (lp_client_schannel() && (pipe_idx == PI_NETLOGON)) {
145 /* Always try and create an schannel netlogon pipe. */
146 pipe_hnd = cli_rpc_pipe_open_schannel(cli, pipe_idx,
147 PIPE_AUTH_LEVEL_PRIVACY,
151 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
152 nt_errstr(nt_status) ));
157 pipe_hnd = cli_rpc_pipe_open_noauth(cli, pipe_idx, &nt_status);
159 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
160 cli_get_pipe_name(pipe_idx),
161 nt_errstr(nt_status) ));
168 nt_status = fn(domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
170 if (!NT_STATUS_IS_OK(nt_status)) {
171 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
173 DEBUG(5, ("rpc command function succedded\n"));
176 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
178 cli_rpc_pipe_close(pipe_hnd);
182 /* close the connection only if it was opened here */
187 talloc_destroy(mem_ctx);
188 return (!NT_STATUS_IS_OK(nt_status));
192 * Force a change of the trust acccount password.
194 * All parameters are provided by the run_rpc_command function, except for
195 * argc, argv which are passes through.
197 * @param domain_sid The domain sid aquired from the remote server
198 * @param cli A cli_state connected to the server.
199 * @param mem_ctx Talloc context, destoyed on compleation of the function.
200 * @param argc Standard main() style argc
201 * @param argc Standard main() style argv. Initial components are already
204 * @return Normal NTSTATUS return.
207 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid,
208 const char *domain_name,
209 struct cli_state *cli,
210 struct rpc_pipe_client *pipe_hnd,
216 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup);
220 * Force a change of the trust acccount password.
222 * @param argc Standard main() style argc
223 * @param argc Standard main() style argv. Initial components are already
226 * @return A shell status integer (0 for success)
229 int net_rpc_changetrustpw(int argc, const char **argv)
231 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
232 rpc_changetrustpw_internals,
237 * Join a domain, the old way.
239 * This uses 'machinename' as the inital password, and changes it.
241 * The password should be created with 'server manager' or equiv first.
243 * All parameters are provided by the run_rpc_command function, except for
244 * argc, argv which are passes through.
246 * @param domain_sid The domain sid aquired from the remote server
247 * @param cli A cli_state connected to the server.
248 * @param mem_ctx Talloc context, destoyed on compleation of the function.
249 * @param argc Standard main() style argc
250 * @param argc Standard main() style argv. Initial components are already
253 * @return Normal NTSTATUS return.
256 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid,
257 const char *domain_name,
258 struct cli_state *cli,
259 struct rpc_pipe_client *pipe_hnd,
265 fstring trust_passwd;
266 unsigned char orig_trust_passwd_hash[16];
268 uint32 sec_channel_type;
270 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &result);
272 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
275 nt_errstr(result) ));
280 check what type of join - if the user want's to join as
281 a BDC, the server must agree that we are a BDC.
284 sec_channel_type = get_sec_channel_type(argv[0]);
286 sec_channel_type = get_sec_channel_type(NULL);
289 fstrcpy(trust_passwd, global_myname());
290 strlower_m(trust_passwd);
293 * Machine names can be 15 characters, but the max length on
294 * a password is 14. --jerry
297 trust_passwd[14] = '\0';
299 E_md4hash(trust_passwd, orig_trust_passwd_hash);
301 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup,
302 orig_trust_passwd_hash,
305 if (NT_STATUS_IS_OK(result))
306 printf("Joined domain %s.\n",opt_target_workgroup);
309 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
310 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
311 result = NT_STATUS_UNSUCCESSFUL;
318 * Join a domain, the old way.
320 * @param argc Standard main() style argc
321 * @param argc Standard main() style argv. Initial components are already
324 * @return A shell status integer (0 for success)
327 static int net_rpc_perform_oldjoin(int argc, const char **argv)
329 return run_rpc_command(NULL, PI_NETLOGON,
330 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
331 rpc_oldjoin_internals,
336 * Join a domain, the old way. This function exists to allow
337 * the message to be displayed when oldjoin was explicitly
338 * requested, but not when it was implied by "net rpc join"
340 * @param argc Standard main() style argc
341 * @param argc Standard main() style argv. Initial components are already
344 * @return A shell status integer (0 for success)
347 static int net_rpc_oldjoin(int argc, const char **argv)
349 int rc = net_rpc_perform_oldjoin(argc, argv);
352 d_fprintf(stderr, "Failed to join domain\n");
359 * Basic usage function for 'net rpc join'
360 * @param argc Standard main() style argc
361 * @param argc Standard main() style argv. Initial components are already
365 static int rpc_join_usage(int argc, const char **argv)
367 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
368 "\t to join a domain with admin username & password\n"\
369 "\t\t password will be prompted if needed and none is specified\n"\
370 "\t <type> can be (default MEMBER)\n"\
371 "\t\t BDC - Join as a BDC\n"\
372 "\t\t PDC - Join as a PDC\n"\
373 "\t\t MEMBER - Join as a MEMBER server\n");
375 net_common_flags_usage(argc, argv);
380 * 'net rpc join' entrypoint.
381 * @param argc Standard main() style argc
382 * @param argc Standard main() style argv. Initial components are already
385 * Main 'net_rpc_join()' (where the admain username/password is used) is
387 * Try to just change the password, but if that doesn't work, use/prompt
388 * for a username/password.
391 int net_rpc_join(int argc, const char **argv)
393 if (lp_server_role() == ROLE_STANDALONE) {
394 d_printf("cannot join as standalone machine\n");
398 if (strlen(global_myname()) > 15) {
399 d_printf("Our netbios name can be at most 15 chars long, "
400 "\"%s\" is %u chars long\n",
401 global_myname(), (unsigned int)strlen(global_myname()));
405 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
408 return net_rpc_join_newstyle(argc, argv);
412 * display info about a rpc domain
414 * All parameters are provided by the run_rpc_command function, except for
415 * argc, argv which are passed through.
417 * @param domain_sid The domain sid acquired from the remote server
418 * @param cli A cli_state connected to the server.
419 * @param mem_ctx Talloc context, destoyed on completion of the function.
420 * @param argc Standard main() style argc
421 * @param argv Standard main() style argv. Initial components are already
424 * @return Normal NTSTATUS return.
427 NTSTATUS rpc_info_internals(const DOM_SID *domain_sid,
428 const char *domain_name,
429 struct cli_state *cli,
430 struct rpc_pipe_client *pipe_hnd,
435 POLICY_HND connect_pol, domain_pol;
436 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
440 sid_to_string(sid_str, domain_sid);
442 /* Get sam policy handle */
443 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
445 if (!NT_STATUS_IS_OK(result)) {
446 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
450 /* Get domain policy handle */
451 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
452 MAXIMUM_ALLOWED_ACCESS,
453 domain_sid, &domain_pol);
454 if (!NT_STATUS_IS_OK(result)) {
455 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
460 result = rpccli_samr_query_dom_info(pipe_hnd, mem_ctx, &domain_pol,
462 if (NT_STATUS_IS_OK(result)) {
463 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
464 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
465 d_printf("Domain SID: %s\n", sid_str);
466 d_printf("Sequence number: %llu\n", (unsigned long long)ctr.info.inf2.seq_num);
467 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
468 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
469 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
478 * 'net rpc info' entrypoint.
479 * @param argc Standard main() style argc
480 * @param argc Standard main() style argv. Initial components are already
484 int net_rpc_info(int argc, const char **argv)
486 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_PDC,
492 * Fetch domain SID into the local secrets.tdb
494 * All parameters are provided by the run_rpc_command function, except for
495 * argc, argv which are passes through.
497 * @param domain_sid The domain sid acquired from the remote server
498 * @param cli A cli_state connected to the server.
499 * @param mem_ctx Talloc context, destoyed on completion of the function.
500 * @param argc Standard main() style argc
501 * @param argv Standard main() style argv. Initial components are already
504 * @return Normal NTSTATUS return.
507 static NTSTATUS rpc_getsid_internals(const DOM_SID *domain_sid,
508 const char *domain_name,
509 struct cli_state *cli,
510 struct rpc_pipe_client *pipe_hnd,
517 sid_to_string(sid_str, domain_sid);
518 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
519 sid_str, domain_name);
521 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
522 DEBUG(0,("Can't store domain SID\n"));
523 return NT_STATUS_UNSUCCESSFUL;
530 * 'net rpc getsid' entrypoint.
531 * @param argc Standard main() style argc
532 * @param argc Standard main() style argv. Initial components are already
536 int net_rpc_getsid(int argc, const char **argv)
538 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
539 rpc_getsid_internals,
543 /****************************************************************************/
546 * Basic usage function for 'net rpc user'
547 * @param argc Standard main() style argc.
548 * @param argv Standard main() style argv. Initial components are already
552 static int rpc_user_usage(int argc, const char **argv)
554 return net_help_user(argc, argv);
558 * Add a new user to a remote RPC server
560 * All parameters are provided by the run_rpc_command function, except for
561 * argc, argv which are passes through.
563 * @param domain_sid The domain sid acquired from the remote server
564 * @param cli A cli_state connected to the server.
565 * @param mem_ctx Talloc context, destoyed on completion of the function.
566 * @param argc Standard main() style argc
567 * @param argv Standard main() style argv. Initial components are already
570 * @return Normal NTSTATUS return.
573 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid,
574 const char *domain_name,
575 struct cli_state *cli,
576 struct rpc_pipe_client *pipe_hnd,
578 int argc, const char **argv)
581 POLICY_HND connect_pol, domain_pol, user_pol;
582 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
583 const char *acct_name;
585 uint32 unknown, user_rid;
588 d_printf("User must be specified\n");
589 rpc_user_usage(argc, argv);
595 /* Get sam policy handle */
597 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
599 if (!NT_STATUS_IS_OK(result)) {
603 /* Get domain policy handle */
605 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
606 MAXIMUM_ALLOWED_ACCESS,
607 domain_sid, &domain_pol);
608 if (!NT_STATUS_IS_OK(result)) {
612 /* Create domain user */
614 acb_info = ACB_NORMAL;
615 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
617 result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
618 acct_name, acb_info, unknown,
619 &user_pol, &user_rid);
620 if (!NT_STATUS_IS_OK(result)) {
626 uint32 *user_rids, num_rids, *name_types;
627 uint32 flags = 0x000003e8; /* Unknown */
628 SAM_USERINFO_CTR ctr;
629 SAM_USER_INFO_24 p24;
632 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
633 flags, 1, &acct_name,
634 &num_rids, &user_rids,
637 if (!NT_STATUS_IS_OK(result)) {
641 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
642 MAXIMUM_ALLOWED_ACCESS,
643 user_rids[0], &user_pol);
645 if (!NT_STATUS_IS_OK(result)) {
649 /* Set password on account */
654 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
656 init_sam_user_info24(&p24, (char *)pwbuf,24);
658 ctr.switch_value = 24;
659 ctr.info.id24 = &p24;
661 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
662 &cli->user_session_key, &ctr);
664 if (!NT_STATUS_IS_OK(result)) {
665 d_fprintf(stderr, "Failed to set password for user %s - %s\n",
666 acct_name, nt_errstr(result));
668 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
670 if (!NT_STATUS_IS_OK(result)) {
671 d_fprintf(stderr, "Failed to delete user %s - %s\n",
672 acct_name, nt_errstr(result));
679 if (!NT_STATUS_IS_OK(result)) {
680 d_fprintf(stderr, "Failed to add user %s - %s\n", acct_name,
683 d_printf("Added user %s\n", acct_name);
689 * Add a new user to a remote RPC server
691 * @param argc Standard main() style argc
692 * @param argv Standard main() style argv. Initial components are already
695 * @return A shell status integer (0 for success)
698 static int rpc_user_add(int argc, const char **argv)
700 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
705 * Delete a user from a remote RPC server
707 * All parameters are provided by the run_rpc_command function, except for
708 * argc, argv which are passes through.
710 * @param domain_sid The domain sid acquired from the remote server
711 * @param cli A cli_state connected to the server.
712 * @param mem_ctx Talloc context, destoyed on completion of the function.
713 * @param argc Standard main() style argc
714 * @param argv Standard main() style argv. Initial components are already
717 * @return Normal NTSTATUS return.
720 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
721 const char *domain_name,
722 struct cli_state *cli,
723 struct rpc_pipe_client *pipe_hnd,
728 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
729 POLICY_HND connect_pol, domain_pol, user_pol;
732 d_printf("User must be specified\n");
733 rpc_user_usage(argc, argv);
736 /* Get sam policy and domain handles */
738 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
741 if (!NT_STATUS_IS_OK(result)) {
745 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
746 MAXIMUM_ALLOWED_ACCESS,
747 domain_sid, &domain_pol);
749 if (!NT_STATUS_IS_OK(result)) {
753 /* Get handle on user */
756 uint32 *user_rids, num_rids, *name_types;
757 uint32 flags = 0x000003e8; /* Unknown */
759 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
761 &num_rids, &user_rids,
764 if (!NT_STATUS_IS_OK(result)) {
768 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
769 MAXIMUM_ALLOWED_ACCESS,
770 user_rids[0], &user_pol);
772 if (!NT_STATUS_IS_OK(result)) {
779 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
781 if (!NT_STATUS_IS_OK(result)) {
785 /* Display results */
786 if (!NT_STATUS_IS_OK(result)) {
787 d_fprintf(stderr, "Failed to delete user account - %s\n", nt_errstr(result));
789 d_printf("Deleted user account\n");
797 * Rename a user on a remote RPC server
799 * All parameters are provided by the run_rpc_command function, except for
800 * argc, argv which are passes through.
802 * @param domain_sid The domain sid acquired from the remote server
803 * @param cli A cli_state connected to the server.
804 * @param mem_ctx Talloc context, destoyed on completion of the function.
805 * @param argc Standard main() style argc
806 * @param argv Standard main() style argv. Initial components are already
809 * @return Normal NTSTATUS return.
812 static NTSTATUS rpc_user_rename_internals(const DOM_SID *domain_sid,
813 const char *domain_name,
814 struct cli_state *cli,
815 struct rpc_pipe_client *pipe_hnd,
820 POLICY_HND connect_pol, domain_pol, user_pol;
821 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
822 uint32 info_level = 7;
823 const char *old_name, *new_name;
825 uint32 flags = 0x000003e8; /* Unknown */
826 uint32 num_rids, *name_types;
827 uint32 num_names = 1;
829 SAM_USERINFO_CTR *user_ctr;
830 SAM_USERINFO_CTR ctr;
831 SAM_USER_INFO_7 info7;
834 d_printf("Old and new username must be specified\n");
835 rpc_user_usage(argc, argv);
843 ZERO_STRUCT(user_ctr);
845 /* Get sam policy handle */
847 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
849 if (!NT_STATUS_IS_OK(result)) {
853 /* Get domain policy handle */
855 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
856 MAXIMUM_ALLOWED_ACCESS,
857 domain_sid, &domain_pol);
858 if (!NT_STATUS_IS_OK(result)) {
862 if ((names = TALLOC_ARRAY(mem_ctx, const char *, num_names)) == NULL) {
863 result = NT_STATUS_NO_MEMORY;
867 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
868 flags, num_names, names,
869 &num_rids, &user_rid, &name_types);
870 if (!NT_STATUS_IS_OK(result)) {
874 /* Open domain user */
875 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
876 MAXIMUM_ALLOWED_ACCESS, user_rid[0], &user_pol);
878 if (!NT_STATUS_IS_OK(result)) {
882 /* Query user info */
883 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
884 info_level, &user_ctr);
886 if (!NT_STATUS_IS_OK(result)) {
890 ctr.switch_value = info_level;
891 ctr.info.id7 = &info7;
893 init_sam_user_info7(&info7, new_name);
896 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol,
897 info_level, &cli->user_session_key, &ctr);
899 if (!NT_STATUS_IS_OK(result)) {
904 if (!NT_STATUS_IS_OK(result)) {
905 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
908 d_printf("Renamed user from %s to %s\n", old_name, new_name);
914 * Rename a user on a remote RPC server
916 * @param argc Standard main() style argc
917 * @param argv Standard main() style argv. Initial components are already
920 * @return A shell status integer (0 for success)
923 static int rpc_user_rename(int argc, const char **argv)
925 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
930 * Delete a user from a remote RPC server
932 * @param argc Standard main() style argc
933 * @param argv Standard main() style argv. Initial components are already
936 * @return A shell status integer (0 for success)
939 static int rpc_user_delete(int argc, const char **argv)
941 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
946 * Set a password for a user on a remote RPC server
948 * All parameters are provided by the run_rpc_command function, except for
949 * argc, argv which are passes through.
951 * @param domain_sid The domain sid acquired from the remote server
952 * @param cli A cli_state connected to the server.
953 * @param mem_ctx Talloc context, destoyed on completion of the function.
954 * @param argc Standard main() style argc
955 * @param argv Standard main() style argv. Initial components are already
958 * @return Normal NTSTATUS return.
961 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
962 const char *domain_name,
963 struct cli_state *cli,
964 struct rpc_pipe_client *pipe_hnd,
969 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
970 POLICY_HND connect_pol, domain_pol, user_pol;
971 SAM_USERINFO_CTR ctr;
972 SAM_USER_INFO_24 p24;
975 const char *new_password;
979 d_printf("User must be specified\n");
980 rpc_user_usage(argc, argv);
987 new_password = argv[1];
989 asprintf(&prompt, "Enter new password for %s:", user);
990 new_password = getpass(prompt);
994 /* Get sam policy and domain handles */
996 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
999 if (!NT_STATUS_IS_OK(result)) {
1003 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1004 MAXIMUM_ALLOWED_ACCESS,
1005 domain_sid, &domain_pol);
1007 if (!NT_STATUS_IS_OK(result)) {
1011 /* Get handle on user */
1014 uint32 *user_rids, num_rids, *name_types;
1015 uint32 flags = 0x000003e8; /* Unknown */
1017 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1019 &num_rids, &user_rids,
1022 if (!NT_STATUS_IS_OK(result)) {
1026 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1027 MAXIMUM_ALLOWED_ACCESS,
1028 user_rids[0], &user_pol);
1030 if (!NT_STATUS_IS_OK(result)) {
1035 /* Set password on account */
1040 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
1042 init_sam_user_info24(&p24, (char *)pwbuf,24);
1044 ctr.switch_value = 24;
1045 ctr.info.id24 = &p24;
1047 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
1048 &cli->user_session_key, &ctr);
1050 if (!NT_STATUS_IS_OK(result)) {
1054 /* Display results */
1062 * Set a user's password on a remote RPC server
1064 * @param argc Standard main() style argc
1065 * @param argv Standard main() style argv. Initial components are already
1068 * @return A shell status integer (0 for success)
1071 static int rpc_user_password(int argc, const char **argv)
1073 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
1078 * List user's groups on a remote RPC server
1080 * All parameters are provided by the run_rpc_command function, except for
1081 * argc, argv which are passes through.
1083 * @param domain_sid The domain sid acquired from the remote server
1084 * @param cli A cli_state connected to the server.
1085 * @param mem_ctx Talloc context, destoyed on completion of the function.
1086 * @param argc Standard main() style argc
1087 * @param argv Standard main() style argv. Initial components are already
1090 * @return Normal NTSTATUS return.
1093 static NTSTATUS rpc_user_info_internals(const DOM_SID *domain_sid,
1094 const char *domain_name,
1095 struct cli_state *cli,
1096 struct rpc_pipe_client *pipe_hnd,
1097 TALLOC_CTX *mem_ctx,
1101 POLICY_HND connect_pol, domain_pol, user_pol;
1102 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1103 uint32 *rids, num_rids, *name_types, num_names;
1104 uint32 flags = 0x000003e8; /* Unknown */
1110 d_printf("User must be specified\n");
1111 rpc_user_usage(argc, argv);
1112 return NT_STATUS_OK;
1114 /* Get sam policy handle */
1116 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1118 if (!NT_STATUS_IS_OK(result)) goto done;
1120 /* Get domain policy handle */
1122 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1123 MAXIMUM_ALLOWED_ACCESS,
1124 domain_sid, &domain_pol);
1125 if (!NT_STATUS_IS_OK(result)) goto done;
1127 /* Get handle on user */
1129 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1131 &num_rids, &rids, &name_types);
1133 if (!NT_STATUS_IS_OK(result)) goto done;
1135 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1136 MAXIMUM_ALLOWED_ACCESS,
1137 rids[0], &user_pol);
1138 if (!NT_STATUS_IS_OK(result)) goto done;
1140 result = rpccli_samr_query_usergroups(pipe_hnd, mem_ctx, &user_pol,
1141 &num_rids, &user_gids);
1143 if (!NT_STATUS_IS_OK(result)) goto done;
1148 if ((rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids)) == NULL) {
1149 result = NT_STATUS_NO_MEMORY;
1153 for (i = 0; i < num_rids; i++)
1154 rids[i] = user_gids[i].g_rid;
1156 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, &domain_pol,
1158 &num_names, &names, &name_types);
1160 if (!NT_STATUS_IS_OK(result)) {
1164 /* Display results */
1166 for (i = 0; i < num_names; i++)
1167 printf("%s\n", names[i]);
1174 * List a user's groups from a remote RPC server
1176 * @param argc Standard main() style argc
1177 * @param argv Standard main() style argv. Initial components are already
1180 * @return A shell status integer (0 for success)
1183 static int rpc_user_info(int argc, const char **argv)
1185 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1190 * List users on a remote RPC server
1192 * All parameters are provided by the run_rpc_command function, except for
1193 * argc, argv which are passes through.
1195 * @param domain_sid The domain sid acquired from the remote server
1196 * @param cli A cli_state connected to the server.
1197 * @param mem_ctx Talloc context, destoyed on completion of the function.
1198 * @param argc Standard main() style argc
1199 * @param argv Standard main() style argv. Initial components are already
1202 * @return Normal NTSTATUS return.
1205 static NTSTATUS rpc_user_list_internals(const DOM_SID *domain_sid,
1206 const char *domain_name,
1207 struct cli_state *cli,
1208 struct rpc_pipe_client *pipe_hnd,
1209 TALLOC_CTX *mem_ctx,
1213 POLICY_HND connect_pol, domain_pol;
1214 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1215 uint32 start_idx=0, num_entries, i, loop_count = 0;
1216 SAM_DISPINFO_CTR ctr;
1217 SAM_DISPINFO_1 info1;
1219 /* Get sam policy handle */
1221 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1223 if (!NT_STATUS_IS_OK(result)) {
1227 /* Get domain policy handle */
1229 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1230 MAXIMUM_ALLOWED_ACCESS,
1231 domain_sid, &domain_pol);
1232 if (!NT_STATUS_IS_OK(result)) {
1236 /* Query domain users */
1239 ctr.sam.info1 = &info1;
1240 if (opt_long_list_entries)
1241 d_printf("\nUser name Comment"\
1242 "\n-----------------------------\n");
1245 uint32 max_entries, max_size;
1247 get_query_dispinfo_params(
1248 loop_count, &max_entries, &max_size);
1250 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
1251 &start_idx, 1, &num_entries,
1252 max_entries, max_size, &ctr);
1255 for (i = 0; i < num_entries; i++) {
1256 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
1257 if (opt_long_list_entries)
1258 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
1260 if (opt_long_list_entries)
1261 printf("%-21.21s %s\n", user, desc);
1263 printf("%s\n", user);
1265 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1272 * 'net rpc user' entrypoint.
1273 * @param argc Standard main() style argc
1274 * @param argc Standard main() style argv. Initial components are already
1278 int net_rpc_user(int argc, const char **argv)
1280 struct functable func[] = {
1281 {"add", rpc_user_add},
1282 {"info", rpc_user_info},
1283 {"delete", rpc_user_delete},
1284 {"password", rpc_user_password},
1285 {"rename", rpc_user_rename},
1290 return run_rpc_command(NULL,PI_SAMR, 0,
1291 rpc_user_list_internals,
1295 return net_run_function(argc, argv, func, rpc_user_usage);
1298 static NTSTATUS rpc_sh_user_list(TALLOC_CTX *mem_ctx,
1299 struct rpc_sh_ctx *ctx,
1300 struct rpc_pipe_client *pipe_hnd,
1301 int argc, const char **argv)
1303 return rpc_user_list_internals(ctx->domain_sid, ctx->domain_name,
1304 ctx->cli, pipe_hnd, mem_ctx,
1308 static NTSTATUS rpc_sh_user_info(TALLOC_CTX *mem_ctx,
1309 struct rpc_sh_ctx *ctx,
1310 struct rpc_pipe_client *pipe_hnd,
1311 int argc, const char **argv)
1313 return rpc_user_info_internals(ctx->domain_sid, ctx->domain_name,
1314 ctx->cli, pipe_hnd, mem_ctx,
1318 static NTSTATUS rpc_sh_handle_user(TALLOC_CTX *mem_ctx,
1319 struct rpc_sh_ctx *ctx,
1320 struct rpc_pipe_client *pipe_hnd,
1321 int argc, const char **argv,
1323 TALLOC_CTX *mem_ctx,
1324 struct rpc_sh_ctx *ctx,
1325 struct rpc_pipe_client *pipe_hnd,
1326 const POLICY_HND *user_hnd,
1327 int argc, const char **argv))
1330 POLICY_HND connect_pol, domain_pol, user_pol;
1331 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1334 enum lsa_SidType type;
1337 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1338 return NT_STATUS_INVALID_PARAMETER;
1341 ZERO_STRUCT(connect_pol);
1342 ZERO_STRUCT(domain_pol);
1343 ZERO_STRUCT(user_pol);
1345 result = net_rpc_lookup_name(mem_ctx, pipe_hnd->cli, argv[0],
1346 NULL, NULL, &sid, &type);
1347 if (!NT_STATUS_IS_OK(result)) {
1348 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1353 if (type != SID_NAME_USER) {
1354 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1355 sid_type_lookup(type));
1356 result = NT_STATUS_NO_SUCH_USER;
1360 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1361 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1362 result = NT_STATUS_NO_SUCH_USER;
1366 result = rpccli_samr_connect(pipe_hnd, mem_ctx,
1367 MAXIMUM_ALLOWED_ACCESS, &connect_pol);
1368 if (!NT_STATUS_IS_OK(result)) {
1372 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1373 MAXIMUM_ALLOWED_ACCESS,
1374 ctx->domain_sid, &domain_pol);
1375 if (!NT_STATUS_IS_OK(result)) {
1379 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1380 MAXIMUM_ALLOWED_ACCESS,
1382 if (!NT_STATUS_IS_OK(result)) {
1386 result = fn(mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1389 if (is_valid_policy_hnd(&user_pol)) {
1390 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1392 if (is_valid_policy_hnd(&domain_pol)) {
1393 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
1395 if (is_valid_policy_hnd(&connect_pol)) {
1396 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
1401 static NTSTATUS rpc_sh_user_show_internals(TALLOC_CTX *mem_ctx,
1402 struct rpc_sh_ctx *ctx,
1403 struct rpc_pipe_client *pipe_hnd,
1404 const POLICY_HND *user_hnd,
1405 int argc, const char **argv)
1408 SAM_USERINFO_CTR *ctr;
1409 SAM_USER_INFO_21 *info;
1412 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1413 return NT_STATUS_INVALID_PARAMETER;
1416 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1418 if (!NT_STATUS_IS_OK(result)) {
1422 info = ctr->info.id21;
1424 d_printf("user rid: %d, group rid: %d\n", info->user_rid,
1430 static NTSTATUS rpc_sh_user_show(TALLOC_CTX *mem_ctx,
1431 struct rpc_sh_ctx *ctx,
1432 struct rpc_pipe_client *pipe_hnd,
1433 int argc, const char **argv)
1435 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1436 rpc_sh_user_show_internals);
1439 #define FETCHSTR(name, rec) \
1440 do { if (strequal(ctx->thiscmd, name)) { \
1441 oldval = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_##rec); } \
1444 #define SETSTR(name, rec, flag) \
1445 do { if (strequal(ctx->thiscmd, name)) { \
1446 init_unistr2(&usr->uni_##rec, argv[0], UNI_STR_TERMINATE); \
1447 init_uni_hdr(&usr->hdr_##rec, &usr->uni_##rec); \
1448 usr->fields_present |= ACCT_##flag; } \
1451 static NTSTATUS rpc_sh_user_str_edit_internals(TALLOC_CTX *mem_ctx,
1452 struct rpc_sh_ctx *ctx,
1453 struct rpc_pipe_client *pipe_hnd,
1454 const POLICY_HND *user_hnd,
1455 int argc, const char **argv)
1458 SAM_USERINFO_CTR *ctr;
1459 SAM_USER_INFO_21 *usr;
1460 const char *username;
1461 const char *oldval = "";
1464 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1466 return NT_STATUS_INVALID_PARAMETER;
1469 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1471 if (!NT_STATUS_IS_OK(result)) {
1475 usr = ctr->info.id21;
1477 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1479 FETCHSTR("fullname", full_name);
1480 FETCHSTR("homedir", home_dir);
1481 FETCHSTR("homedrive", dir_drive);
1482 FETCHSTR("logonscript", logon_script);
1483 FETCHSTR("profilepath", profile_path);
1484 FETCHSTR("description", acct_desc);
1487 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1493 if (strcmp(argv[0], "NULL") == 0) {
1497 SETSTR("fullname", full_name, FULL_NAME);
1498 SETSTR("homedir", home_dir, HOME_DIR);
1499 SETSTR("homedrive", dir_drive, HOME_DRIVE);
1500 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1501 SETSTR("profilepath", profile_path, PROFILE);
1502 SETSTR("description", acct_desc, DESCRIPTION);
1504 result = rpccli_samr_set_userinfo2(
1505 pipe_hnd, mem_ctx, user_hnd, 21,
1506 &pipe_hnd->cli->user_session_key, ctr);
1508 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1509 ctx->thiscmd, oldval, argv[0]);
1516 #define HANDLEFLG(name, rec) \
1517 do { if (strequal(ctx->thiscmd, name)) { \
1518 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1520 newflags = oldflags | ACB_##rec; \
1522 newflags = oldflags & ~ACB_##rec; \
1525 static NTSTATUS rpc_sh_user_str_edit(TALLOC_CTX *mem_ctx,
1526 struct rpc_sh_ctx *ctx,
1527 struct rpc_pipe_client *pipe_hnd,
1528 int argc, const char **argv)
1530 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1531 rpc_sh_user_str_edit_internals);
1534 static NTSTATUS rpc_sh_user_flag_edit_internals(TALLOC_CTX *mem_ctx,
1535 struct rpc_sh_ctx *ctx,
1536 struct rpc_pipe_client *pipe_hnd,
1537 const POLICY_HND *user_hnd,
1538 int argc, const char **argv)
1541 SAM_USERINFO_CTR *ctr;
1542 SAM_USER_INFO_21 *usr;
1543 const char *username;
1544 const char *oldval = "unknown";
1545 uint32 oldflags, newflags;
1549 ((argc == 1) && !strequal(argv[0], "yes") &&
1550 !strequal(argv[0], "no"))) {
1551 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1553 return NT_STATUS_INVALID_PARAMETER;
1556 newval = strequal(argv[0], "yes");
1558 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1560 if (!NT_STATUS_IS_OK(result)) {
1564 usr = ctr->info.id21;
1566 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1567 oldflags = usr->acb_info;
1568 newflags = usr->acb_info;
1570 HANDLEFLG("disabled", DISABLED);
1571 HANDLEFLG("pwnotreq", PWNOTREQ);
1572 HANDLEFLG("autolock", AUTOLOCK);
1573 HANDLEFLG("pwnoexp", PWNOEXP);
1576 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1582 usr->acb_info = newflags;
1583 usr->fields_present = ACCT_FLAGS;
1585 result = rpccli_samr_set_userinfo2(
1586 pipe_hnd, mem_ctx, user_hnd, 21,
1587 &pipe_hnd->cli->user_session_key, ctr);
1589 if (NT_STATUS_IS_OK(result)) {
1590 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1591 ctx->thiscmd, oldval, argv[0]);
1599 static NTSTATUS rpc_sh_user_flag_edit(TALLOC_CTX *mem_ctx,
1600 struct rpc_sh_ctx *ctx,
1601 struct rpc_pipe_client *pipe_hnd,
1602 int argc, const char **argv)
1604 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1605 rpc_sh_user_flag_edit_internals);
1608 struct rpc_sh_cmd *net_rpc_user_edit_cmds(TALLOC_CTX *mem_ctx,
1609 struct rpc_sh_ctx *ctx)
1611 static struct rpc_sh_cmd cmds[] = {
1613 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1614 "Show/Set a user's full name" },
1616 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1617 "Show/Set a user's home directory" },
1619 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1620 "Show/Set a user's home drive" },
1622 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1623 "Show/Set a user's logon script" },
1625 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1626 "Show/Set a user's profile path" },
1628 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1629 "Show/Set a user's description" },
1631 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1632 "Show/Set whether a user is disabled" },
1634 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1635 "Show/Set whether a user locked out" },
1637 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1638 "Show/Set whether a user does not need a password" },
1640 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1641 "Show/Set whether a user's password does not expire" },
1643 { NULL, NULL, 0, NULL, NULL }
1649 struct rpc_sh_cmd *net_rpc_user_cmds(TALLOC_CTX *mem_ctx,
1650 struct rpc_sh_ctx *ctx)
1652 static struct rpc_sh_cmd cmds[] = {
1654 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1655 "List available users" },
1657 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1658 "List the domain groups a user is member of" },
1660 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1661 "Show info about a user" },
1663 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1664 "Show/Modify a user's fields" },
1666 { NULL, NULL, 0, NULL, NULL }
1672 /****************************************************************************/
1675 * Basic usage function for 'net rpc group'
1676 * @param argc Standard main() style argc.
1677 * @param argv Standard main() style argv. Initial components are already
1681 static int rpc_group_usage(int argc, const char **argv)
1683 return net_help_group(argc, argv);
1687 * Delete group on a remote RPC server
1689 * All parameters are provided by the run_rpc_command function, except for
1690 * argc, argv which are passes through.
1692 * @param domain_sid The domain sid acquired from the remote server
1693 * @param cli A cli_state connected to the server.
1694 * @param mem_ctx Talloc context, destoyed on completion of the function.
1695 * @param argc Standard main() style argc
1696 * @param argv Standard main() style argv. Initial components are already
1699 * @return Normal NTSTATUS return.
1702 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1703 const char *domain_name,
1704 struct cli_state *cli,
1705 struct rpc_pipe_client *pipe_hnd,
1706 TALLOC_CTX *mem_ctx,
1710 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1711 BOOL group_is_primary = False;
1712 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1714 uint32 *group_rids, num_rids, *name_types, num_members,
1715 *group_attrs, group_rid;
1716 uint32 flags = 0x000003e8; /* Unknown */
1719 /* DOM_GID *user_gids; */
1720 SAM_USERINFO_CTR *user_ctr;
1724 d_printf("specify group\n");
1725 rpc_group_usage(argc,argv);
1726 return NT_STATUS_OK; /* ok? */
1729 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1732 if (!NT_STATUS_IS_OK(result)) {
1733 d_fprintf(stderr, "Request samr_connect failed\n");
1737 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1738 MAXIMUM_ALLOWED_ACCESS,
1739 domain_sid, &domain_pol);
1741 if (!NT_STATUS_IS_OK(result)) {
1742 d_fprintf(stderr, "Request open_domain failed\n");
1746 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1748 &num_rids, &group_rids,
1751 if (!NT_STATUS_IS_OK(result)) {
1752 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1756 switch (name_types[0])
1758 case SID_NAME_DOM_GRP:
1759 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
1760 MAXIMUM_ALLOWED_ACCESS,
1761 group_rids[0], &group_pol);
1762 if (!NT_STATUS_IS_OK(result)) {
1763 d_fprintf(stderr, "Request open_group failed");
1767 group_rid = group_rids[0];
1769 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
1770 &num_members, &group_rids,
1773 if (!NT_STATUS_IS_OK(result)) {
1774 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1779 d_printf("Domain Group %s (rid: %d) has %d members\n",
1780 argv[0],group_rid,num_members);
1783 /* Check if group is anyone's primary group */
1784 for (i = 0; i < num_members; i++)
1786 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1787 MAXIMUM_ALLOWED_ACCESS,
1788 group_rids[i], &user_pol);
1790 if (!NT_STATUS_IS_OK(result)) {
1791 d_fprintf(stderr, "Unable to open group member %d\n",group_rids[i]);
1795 ZERO_STRUCT(user_ctr);
1797 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
1800 if (!NT_STATUS_IS_OK(result)) {
1801 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",group_rids[i]);
1805 if (user_ctr->info.id21->group_rid == group_rid) {
1806 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1809 d_printf("Group is primary group of %s\n",temp);
1810 group_is_primary = True;
1813 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1816 if (group_is_primary) {
1817 d_fprintf(stderr, "Unable to delete group because some "
1818 "of it's members have it as primary group\n");
1819 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1823 /* remove all group members */
1824 for (i = 0; i < num_members; i++)
1827 d_printf("Remove group member %d...",group_rids[i]);
1828 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, group_rids[i]);
1830 if (NT_STATUS_IS_OK(result)) {
1835 d_printf("failed\n");
1840 result = rpccli_samr_delete_dom_group(pipe_hnd, mem_ctx, &group_pol);
1843 /* removing a local group is easier... */
1844 case SID_NAME_ALIAS:
1845 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
1846 MAXIMUM_ALLOWED_ACCESS,
1847 group_rids[0], &group_pol);
1849 if (!NT_STATUS_IS_OK(result)) {
1850 d_fprintf(stderr, "Request open_alias failed\n");
1854 result = rpccli_samr_delete_dom_alias(pipe_hnd, mem_ctx, &group_pol);
1857 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1858 argv[0],sid_type_lookup(name_types[0]));
1859 result = NT_STATUS_UNSUCCESSFUL;
1864 if (NT_STATUS_IS_OK(result)) {
1866 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1868 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1869 get_friendly_nt_error_msg(result));
1877 static int rpc_group_delete(int argc, const char **argv)
1879 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1883 static NTSTATUS rpc_group_add_internals(const DOM_SID *domain_sid,
1884 const char *domain_name,
1885 struct cli_state *cli,
1886 struct rpc_pipe_client *pipe_hnd,
1887 TALLOC_CTX *mem_ctx,
1891 POLICY_HND connect_pol, domain_pol, group_pol;
1892 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1893 GROUP_INFO_CTR group_info;
1896 d_printf("Group name must be specified\n");
1897 rpc_group_usage(argc, argv);
1898 return NT_STATUS_OK;
1901 /* Get sam policy handle */
1903 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1905 if (!NT_STATUS_IS_OK(result)) goto done;
1907 /* Get domain policy handle */
1909 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1910 MAXIMUM_ALLOWED_ACCESS,
1911 domain_sid, &domain_pol);
1912 if (!NT_STATUS_IS_OK(result)) goto done;
1914 /* Create the group */
1916 result = rpccli_samr_create_dom_group(pipe_hnd, mem_ctx, &domain_pol,
1917 argv[0], MAXIMUM_ALLOWED_ACCESS,
1919 if (!NT_STATUS_IS_OK(result)) goto done;
1921 if (strlen(opt_comment) == 0) goto done;
1923 /* We've got a comment to set */
1925 group_info.switch_value1 = 4;
1926 init_samr_group_info4(&group_info.group.info4, opt_comment);
1928 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &group_info);
1929 if (!NT_STATUS_IS_OK(result)) goto done;
1932 if (NT_STATUS_IS_OK(result))
1933 DEBUG(5, ("add group succeeded\n"));
1935 d_fprintf(stderr, "add group failed: %s\n", nt_errstr(result));
1940 static NTSTATUS rpc_alias_add_internals(const DOM_SID *domain_sid,
1941 const char *domain_name,
1942 struct cli_state *cli,
1943 struct rpc_pipe_client *pipe_hnd,
1944 TALLOC_CTX *mem_ctx,
1948 POLICY_HND connect_pol, domain_pol, alias_pol;
1949 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1950 ALIAS_INFO_CTR alias_info;
1953 d_printf("Alias name must be specified\n");
1954 rpc_group_usage(argc, argv);
1955 return NT_STATUS_OK;
1958 /* Get sam policy handle */
1960 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1962 if (!NT_STATUS_IS_OK(result)) goto done;
1964 /* Get domain policy handle */
1966 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1967 MAXIMUM_ALLOWED_ACCESS,
1968 domain_sid, &domain_pol);
1969 if (!NT_STATUS_IS_OK(result)) goto done;
1971 /* Create the group */
1973 result = rpccli_samr_create_dom_alias(pipe_hnd, mem_ctx, &domain_pol,
1974 argv[0], &alias_pol);
1975 if (!NT_STATUS_IS_OK(result)) goto done;
1977 if (strlen(opt_comment) == 0) goto done;
1979 /* We've got a comment to set */
1981 alias_info.level = 3;
1982 init_samr_alias_info3(&alias_info.alias.info3, opt_comment);
1984 result = rpccli_samr_set_aliasinfo(pipe_hnd, mem_ctx, &alias_pol, &alias_info);
1985 if (!NT_STATUS_IS_OK(result)) goto done;
1988 if (NT_STATUS_IS_OK(result))
1989 DEBUG(5, ("add alias succeeded\n"));
1991 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1996 static int rpc_group_add(int argc, const char **argv)
1999 return run_rpc_command(NULL, PI_SAMR, 0,
2000 rpc_alias_add_internals,
2003 return run_rpc_command(NULL, PI_SAMR, 0,
2004 rpc_group_add_internals,
2008 static NTSTATUS get_sid_from_name(struct cli_state *cli,
2009 TALLOC_CTX *mem_ctx,
2012 enum lsa_SidType *type)
2014 DOM_SID *sids = NULL;
2015 enum lsa_SidType *types = NULL;
2016 struct rpc_pipe_client *pipe_hnd;
2018 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2020 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
2025 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, False,
2026 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2028 if (!NT_STATUS_IS_OK(result)) {
2032 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2033 &name, NULL, &sids, &types);
2035 if (NT_STATUS_IS_OK(result)) {
2036 sid_copy(sid, &sids[0]);
2040 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
2044 cli_rpc_pipe_close(pipe_hnd);
2047 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
2049 /* Try as S-1-5-whatever */
2053 if (string_to_sid(&tmp_sid, name)) {
2054 sid_copy(sid, &tmp_sid);
2055 *type = SID_NAME_UNKNOWN;
2056 result = NT_STATUS_OK;
2063 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2064 TALLOC_CTX *mem_ctx,
2065 const DOM_SID *group_sid,
2068 POLICY_HND connect_pol, domain_pol;
2071 POLICY_HND group_pol;
2074 uint32 *rids = NULL;
2075 uint32 *rid_types = NULL;
2079 sid_copy(&sid, group_sid);
2081 if (!sid_split_rid(&sid, &group_rid)) {
2082 return NT_STATUS_UNSUCCESSFUL;
2085 /* Get sam policy handle */
2086 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2088 if (!NT_STATUS_IS_OK(result)) {
2092 /* Get domain policy handle */
2093 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2094 MAXIMUM_ALLOWED_ACCESS,
2096 if (!NT_STATUS_IS_OK(result)) {
2100 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2102 &num_rids, &rids, &rid_types);
2104 if (!NT_STATUS_IS_OK(result)) {
2105 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2109 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2110 MAXIMUM_ALLOWED_ACCESS,
2111 group_rid, &group_pol);
2113 if (!NT_STATUS_IS_OK(result)) {
2117 result = rpccli_samr_add_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2120 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2124 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2125 TALLOC_CTX *mem_ctx,
2126 const DOM_SID *alias_sid,
2129 POLICY_HND connect_pol, domain_pol;
2132 POLICY_HND alias_pol;
2135 enum lsa_SidType member_type;
2139 sid_copy(&sid, alias_sid);
2141 if (!sid_split_rid(&sid, &alias_rid)) {
2142 return NT_STATUS_UNSUCCESSFUL;
2145 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2146 &member_sid, &member_type);
2148 if (!NT_STATUS_IS_OK(result)) {
2149 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2153 /* Get sam policy handle */
2154 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2156 if (!NT_STATUS_IS_OK(result)) {
2160 /* Get domain policy handle */
2161 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2162 MAXIMUM_ALLOWED_ACCESS,
2164 if (!NT_STATUS_IS_OK(result)) {
2168 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2169 MAXIMUM_ALLOWED_ACCESS,
2170 alias_rid, &alias_pol);
2172 if (!NT_STATUS_IS_OK(result)) {
2176 result = rpccli_samr_add_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2178 if (!NT_STATUS_IS_OK(result)) {
2183 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2187 static NTSTATUS rpc_group_addmem_internals(const DOM_SID *domain_sid,
2188 const char *domain_name,
2189 struct cli_state *cli,
2190 struct rpc_pipe_client *pipe_hnd,
2191 TALLOC_CTX *mem_ctx,
2196 enum lsa_SidType group_type;
2199 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2200 return NT_STATUS_UNSUCCESSFUL;
2203 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2204 &group_sid, &group_type))) {
2205 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2206 return NT_STATUS_UNSUCCESSFUL;
2209 if (group_type == SID_NAME_DOM_GRP) {
2210 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2211 &group_sid, argv[1]);
2213 if (!NT_STATUS_IS_OK(result)) {
2214 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2215 argv[1], argv[0], nt_errstr(result));
2220 if (group_type == SID_NAME_ALIAS) {
2221 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2222 &group_sid, argv[1]);
2224 if (!NT_STATUS_IS_OK(result)) {
2225 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2226 argv[1], argv[0], nt_errstr(result));
2231 d_fprintf(stderr, "Can only add members to global or local groups "
2232 "which %s is not\n", argv[0]);
2234 return NT_STATUS_UNSUCCESSFUL;
2237 static int rpc_group_addmem(int argc, const char **argv)
2239 return run_rpc_command(NULL, PI_SAMR, 0,
2240 rpc_group_addmem_internals,
2244 static NTSTATUS rpc_del_groupmem(struct rpc_pipe_client *pipe_hnd,
2245 TALLOC_CTX *mem_ctx,
2246 const DOM_SID *group_sid,
2249 POLICY_HND connect_pol, domain_pol;
2252 POLICY_HND group_pol;
2255 uint32 *rids = NULL;
2256 uint32 *rid_types = NULL;
2260 sid_copy(&sid, group_sid);
2262 if (!sid_split_rid(&sid, &group_rid))
2263 return NT_STATUS_UNSUCCESSFUL;
2265 /* Get sam policy handle */
2266 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2268 if (!NT_STATUS_IS_OK(result))
2271 /* Get domain policy handle */
2272 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2273 MAXIMUM_ALLOWED_ACCESS,
2275 if (!NT_STATUS_IS_OK(result))
2278 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2280 &num_rids, &rids, &rid_types);
2282 if (!NT_STATUS_IS_OK(result)) {
2283 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2287 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2288 MAXIMUM_ALLOWED_ACCESS,
2289 group_rid, &group_pol);
2291 if (!NT_STATUS_IS_OK(result))
2294 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2297 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2301 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2302 TALLOC_CTX *mem_ctx,
2303 const DOM_SID *alias_sid,
2306 POLICY_HND connect_pol, domain_pol;
2309 POLICY_HND alias_pol;
2312 enum lsa_SidType member_type;
2316 sid_copy(&sid, alias_sid);
2318 if (!sid_split_rid(&sid, &alias_rid))
2319 return NT_STATUS_UNSUCCESSFUL;
2321 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2322 &member_sid, &member_type);
2324 if (!NT_STATUS_IS_OK(result)) {
2325 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2329 /* Get sam policy handle */
2330 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2332 if (!NT_STATUS_IS_OK(result)) {
2336 /* Get domain policy handle */
2337 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2338 MAXIMUM_ALLOWED_ACCESS,
2340 if (!NT_STATUS_IS_OK(result)) {
2344 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2345 MAXIMUM_ALLOWED_ACCESS,
2346 alias_rid, &alias_pol);
2348 if (!NT_STATUS_IS_OK(result))
2351 result = rpccli_samr_del_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2353 if (!NT_STATUS_IS_OK(result))
2357 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2361 static NTSTATUS rpc_group_delmem_internals(const DOM_SID *domain_sid,
2362 const char *domain_name,
2363 struct cli_state *cli,
2364 struct rpc_pipe_client *pipe_hnd,
2365 TALLOC_CTX *mem_ctx,
2370 enum lsa_SidType group_type;
2373 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2374 return NT_STATUS_UNSUCCESSFUL;
2377 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2378 &group_sid, &group_type))) {
2379 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2380 return NT_STATUS_UNSUCCESSFUL;
2383 if (group_type == SID_NAME_DOM_GRP) {
2384 NTSTATUS result = rpc_del_groupmem(pipe_hnd, mem_ctx,
2385 &group_sid, argv[1]);
2387 if (!NT_STATUS_IS_OK(result)) {
2388 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2389 argv[1], argv[0], nt_errstr(result));
2394 if (group_type == SID_NAME_ALIAS) {
2395 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2396 &group_sid, argv[1]);
2398 if (!NT_STATUS_IS_OK(result)) {
2399 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2400 argv[1], argv[0], nt_errstr(result));
2405 d_fprintf(stderr, "Can only delete members from global or local groups "
2406 "which %s is not\n", argv[0]);
2408 return NT_STATUS_UNSUCCESSFUL;
2411 static int rpc_group_delmem(int argc, const char **argv)
2413 return run_rpc_command(NULL, PI_SAMR, 0,
2414 rpc_group_delmem_internals,
2419 * List groups on a remote RPC server
2421 * All parameters are provided by the run_rpc_command function, except for
2422 * argc, argv which are passes through.
2424 * @param domain_sid The domain sid acquired from the remote server
2425 * @param cli A cli_state connected to the server.
2426 * @param mem_ctx Talloc context, destoyed on completion of the function.
2427 * @param argc Standard main() style argc
2428 * @param argv Standard main() style argv. Initial components are already
2431 * @return Normal NTSTATUS return.
2434 static NTSTATUS rpc_group_list_internals(const DOM_SID *domain_sid,
2435 const char *domain_name,
2436 struct cli_state *cli,
2437 struct rpc_pipe_client *pipe_hnd,
2438 TALLOC_CTX *mem_ctx,
2442 POLICY_HND connect_pol, domain_pol;
2443 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2444 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2445 struct acct_info *groups;
2446 BOOL global = False;
2448 BOOL builtin = False;
2456 for (i=0; i<argc; i++) {
2457 if (strequal(argv[i], "global"))
2460 if (strequal(argv[i], "local"))
2463 if (strequal(argv[i], "builtin"))
2467 /* Get sam policy handle */
2469 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2471 if (!NT_STATUS_IS_OK(result)) {
2475 /* Get domain policy handle */
2477 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2478 MAXIMUM_ALLOWED_ACCESS,
2479 domain_sid, &domain_pol);
2480 if (!NT_STATUS_IS_OK(result)) {
2484 /* Query domain groups */
2485 if (opt_long_list_entries)
2486 d_printf("\nGroup name Comment"\
2487 "\n-----------------------------\n");
2489 SAM_DISPINFO_CTR ctr;
2490 SAM_DISPINFO_3 info3;
2495 ctr.sam.info3 = &info3;
2499 get_query_dispinfo_params(
2500 loop_count, &max_entries, &max_size);
2502 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
2503 &start_idx, 3, &num_entries,
2504 max_entries, max_size, &ctr);
2506 if (!NT_STATUS_IS_OK(result) &&
2507 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2510 for (i = 0; i < num_entries; i++) {
2512 fstring group, desc;
2514 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
2515 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
2517 if (opt_long_list_entries)
2518 printf("%-21.21s %-50.50s\n",
2521 printf("%s\n", group);
2523 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2524 /* query domain aliases */
2529 /* The max_size field in cli_samr_enum_als_groups is more like
2530 * an account_control field with indiviual bits what to
2531 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2532 * everything. I'm too lazy (sorry) to get this through to
2533 * rpc_parse/ etc. Volker */
2535 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2537 &groups, &num_entries);
2539 if (!NT_STATUS_IS_OK(result) &&
2540 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2543 for (i = 0; i < num_entries; i++) {
2545 char *description = NULL;
2547 if (opt_long_list_entries) {
2549 POLICY_HND alias_pol;
2552 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2557 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2560 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2562 description = unistr2_tdup(mem_ctx,
2563 ctr.alias.info3.description.string);
2567 if (description != NULL) {
2568 printf("%-21.21s %-50.50s\n",
2569 groups[i].acct_name,
2572 printf("%s\n", groups[i].acct_name);
2575 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2576 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2577 /* Get builtin policy handle */
2579 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2580 MAXIMUM_ALLOWED_ACCESS,
2581 &global_sid_Builtin, &domain_pol);
2582 if (!NT_STATUS_IS_OK(result)) {
2585 /* query builtin aliases */
2588 if (!builtin) break;
2590 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2591 &start_idx, max_entries,
2592 &groups, &num_entries);
2594 if (!NT_STATUS_IS_OK(result) &&
2595 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2598 for (i = 0; i < num_entries; i++) {
2600 char *description = NULL;
2602 if (opt_long_list_entries) {
2604 POLICY_HND alias_pol;
2607 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2612 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2615 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2617 description = unistr2_tdup(mem_ctx,
2618 ctr.alias.info3.description.string);
2622 if (description != NULL) {
2623 printf("%-21.21s %-50.50s\n",
2624 groups[i].acct_name,
2627 printf("%s\n", groups[i].acct_name);
2630 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2636 static int rpc_group_list(int argc, const char **argv)
2638 return run_rpc_command(NULL, PI_SAMR, 0,
2639 rpc_group_list_internals,
2643 static NTSTATUS rpc_list_group_members(struct rpc_pipe_client *pipe_hnd,
2644 TALLOC_CTX *mem_ctx,
2645 const char *domain_name,
2646 const DOM_SID *domain_sid,
2647 POLICY_HND *domain_pol,
2651 POLICY_HND group_pol;
2652 uint32 num_members, *group_rids, *group_attrs;
2659 sid_to_string(sid_str, domain_sid);
2661 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, domain_pol,
2662 MAXIMUM_ALLOWED_ACCESS,
2665 if (!NT_STATUS_IS_OK(result))
2668 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
2669 &num_members, &group_rids,
2672 if (!NT_STATUS_IS_OK(result))
2675 while (num_members > 0) {
2676 int this_time = 512;
2678 if (num_members < this_time)
2679 this_time = num_members;
2681 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, domain_pol,
2682 this_time, group_rids,
2683 &num_names, &names, &name_types);
2685 if (!NT_STATUS_IS_OK(result))
2688 /* We only have users as members, but make the output
2689 the same as the output of alias members */
2691 for (i = 0; i < this_time; i++) {
2693 if (opt_long_list_entries) {
2694 printf("%s-%d %s\\%s %d\n", sid_str,
2695 group_rids[i], domain_name, names[i],
2698 printf("%s\\%s\n", domain_name, names[i]);
2702 num_members -= this_time;
2706 return NT_STATUS_OK;
2709 static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
2710 TALLOC_CTX *mem_ctx,
2711 POLICY_HND *domain_pol,
2715 struct rpc_pipe_client *lsa_pipe;
2716 POLICY_HND alias_pol, lsa_pol;
2718 DOM_SID *alias_sids;
2721 enum lsa_SidType *types;
2724 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, domain_pol,
2725 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
2727 if (!NT_STATUS_IS_OK(result))
2730 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx, &alias_pol,
2731 &num_members, &alias_sids);
2733 if (!NT_STATUS_IS_OK(result)) {
2734 d_fprintf(stderr, "Couldn't list alias members\n");
2738 if (num_members == 0) {
2739 return NT_STATUS_OK;
2742 lsa_pipe = cli_rpc_pipe_open_noauth(pipe_hnd->cli, PI_LSARPC, &result);
2744 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2745 nt_errstr(result) );
2749 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
2750 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2752 if (!NT_STATUS_IS_OK(result)) {
2753 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2754 cli_rpc_pipe_close(lsa_pipe);
2758 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol, num_members,
2760 &domains, &names, &types);
2762 if (!NT_STATUS_IS_OK(result) &&
2763 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2764 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2765 cli_rpc_pipe_close(lsa_pipe);
2769 for (i = 0; i < num_members; i++) {
2771 sid_to_string(sid_str, &alias_sids[i]);
2773 if (opt_long_list_entries) {
2774 printf("%s %s\\%s %d\n", sid_str,
2775 domains[i] ? domains[i] : "*unknown*",
2776 names[i] ? names[i] : "*unknown*", types[i]);
2779 printf("%s\\%s\n", domains[i], names[i]);
2781 printf("%s\n", sid_str);
2785 cli_rpc_pipe_close(lsa_pipe);
2786 return NT_STATUS_OK;
2789 static NTSTATUS rpc_group_members_internals(const DOM_SID *domain_sid,
2790 const char *domain_name,
2791 struct cli_state *cli,
2792 struct rpc_pipe_client *pipe_hnd,
2793 TALLOC_CTX *mem_ctx,
2798 POLICY_HND connect_pol, domain_pol;
2799 uint32 num_rids, *rids, *rid_types;
2801 /* Get sam policy handle */
2803 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2806 if (!NT_STATUS_IS_OK(result))
2809 /* Get domain policy handle */
2811 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2812 MAXIMUM_ALLOWED_ACCESS,
2813 domain_sid, &domain_pol);
2815 if (!NT_STATUS_IS_OK(result))
2818 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2819 1, argv, &num_rids, &rids, &rid_types);
2821 if (!NT_STATUS_IS_OK(result)) {
2823 /* Ok, did not find it in the global sam, try with builtin */
2825 DOM_SID sid_Builtin;
2827 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2829 string_to_sid(&sid_Builtin, "S-1-5-32");
2831 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2832 MAXIMUM_ALLOWED_ACCESS,
2833 &sid_Builtin, &domain_pol);
2835 if (!NT_STATUS_IS_OK(result)) {
2836 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2840 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2844 if (!NT_STATUS_IS_OK(result)) {
2845 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2850 if (num_rids != 1) {
2851 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2855 if (rid_types[0] == SID_NAME_DOM_GRP) {
2856 return rpc_list_group_members(pipe_hnd, mem_ctx, domain_name,
2857 domain_sid, &domain_pol,
2861 if (rid_types[0] == SID_NAME_ALIAS) {
2862 return rpc_list_alias_members(pipe_hnd, mem_ctx, &domain_pol,
2866 return NT_STATUS_NO_SUCH_GROUP;
2869 static int rpc_group_members(int argc, const char **argv)
2872 return rpc_group_usage(argc, argv);
2875 return run_rpc_command(NULL, PI_SAMR, 0,
2876 rpc_group_members_internals,
2880 static NTSTATUS rpc_group_rename_internals(const DOM_SID *domain_sid,
2881 const char *domain_name,
2882 struct cli_state *cli,
2883 struct rpc_pipe_client *pipe_hnd,
2884 TALLOC_CTX *mem_ctx,
2889 POLICY_HND connect_pol, domain_pol, group_pol;
2890 uint32 num_rids, *rids, *rid_types;
2894 d_printf("Usage: 'net rpc group rename group newname'\n");
2895 return NT_STATUS_UNSUCCESSFUL;
2898 /* Get sam policy handle */
2900 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2903 if (!NT_STATUS_IS_OK(result))
2906 /* Get domain policy handle */
2908 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2909 MAXIMUM_ALLOWED_ACCESS,
2910 domain_sid, &domain_pol);
2912 if (!NT_STATUS_IS_OK(result))
2915 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2916 1, argv, &num_rids, &rids, &rid_types);
2918 if (num_rids != 1) {
2919 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2923 if (rid_types[0] != SID_NAME_DOM_GRP) {
2924 d_fprintf(stderr, "Can only rename domain groups\n");
2925 return NT_STATUS_UNSUCCESSFUL;
2928 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2929 MAXIMUM_ALLOWED_ACCESS,
2930 rids[0], &group_pol);
2932 if (!NT_STATUS_IS_OK(result))
2937 ctr.switch_value1 = 2;
2938 init_samr_group_info2(&ctr.group.info2, argv[1]);
2940 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &ctr);
2942 if (!NT_STATUS_IS_OK(result))
2945 return NT_STATUS_NO_SUCH_GROUP;
2948 static int rpc_group_rename(int argc, const char **argv)
2951 return rpc_group_usage(argc, argv);
2954 return run_rpc_command(NULL, PI_SAMR, 0,
2955 rpc_group_rename_internals,
2960 * 'net rpc group' entrypoint.
2961 * @param argc Standard main() style argc
2962 * @param argc Standard main() style argv. Initial components are already
2966 int net_rpc_group(int argc, const char **argv)
2968 struct functable func[] = {
2969 {"add", rpc_group_add},
2970 {"delete", rpc_group_delete},
2971 {"addmem", rpc_group_addmem},
2972 {"delmem", rpc_group_delmem},
2973 {"list", rpc_group_list},
2974 {"members", rpc_group_members},
2975 {"rename", rpc_group_rename},
2980 return run_rpc_command(NULL, PI_SAMR, 0,
2981 rpc_group_list_internals,
2985 return net_run_function(argc, argv, func, rpc_group_usage);
2988 /****************************************************************************/
2990 static int rpc_share_usage(int argc, const char **argv)
2992 return net_help_share(argc, argv);
2996 * Add a share on a remote RPC server
2998 * All parameters are provided by the run_rpc_command function, except for
2999 * argc, argv which are passes through.
3001 * @param domain_sid The domain sid acquired from the remote server
3002 * @param cli A cli_state connected to the server.
3003 * @param mem_ctx Talloc context, destoyed on completion of the function.
3004 * @param argc Standard main() style argc
3005 * @param argv Standard main() style argv. Initial components are already
3008 * @return Normal NTSTATUS return.
3010 static NTSTATUS rpc_share_add_internals(const DOM_SID *domain_sid,
3011 const char *domain_name,
3012 struct cli_state *cli,
3013 struct rpc_pipe_client *pipe_hnd,
3014 TALLOC_CTX *mem_ctx,int argc,
3020 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3021 uint32 num_users=0, perms=0;
3022 char *password=NULL; /* don't allow a share password */
3025 union srvsvc_NetShareInfo info;
3026 struct srvsvc_NetShareInfo2 info2;
3028 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3029 return NT_STATUS_NO_MEMORY;
3032 path = strchr(sharename, '=');
3034 return NT_STATUS_UNSUCCESSFUL;
3037 info.info2 = &info2;
3040 info2.comment = opt_comment;
3041 info2.permissions = perms;
3042 info2.max_users = opt_maxusers;
3043 info2.current_users = num_users;
3045 info2.password = password;
3046 info2.name = sharename;
3048 result = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx, NULL, level,
3053 static int rpc_share_add(int argc, const char **argv)
3055 if ((argc < 1) || !strchr(argv[0], '=')) {
3056 DEBUG(1,("Sharename or path not specified on add\n"));
3057 return rpc_share_usage(argc, argv);
3059 return run_rpc_command(NULL, PI_SRVSVC, 0,
3060 rpc_share_add_internals,
3065 * Delete a share on a remote RPC server
3067 * All parameters are provided by the run_rpc_command function, except for
3068 * argc, argv which are passes through.
3070 * @param domain_sid The domain sid acquired from the remote server
3071 * @param cli A cli_state connected to the server.
3072 * @param mem_ctx Talloc context, destoyed on completion of the function.
3073 * @param argc Standard main() style argc
3074 * @param argv Standard main() style argv. Initial components are already
3077 * @return Normal NTSTATUS return.
3079 static NTSTATUS rpc_share_del_internals(const DOM_SID *domain_sid,
3080 const char *domain_name,
3081 struct cli_state *cli,
3082 struct rpc_pipe_client *pipe_hnd,
3083 TALLOC_CTX *mem_ctx,
3087 return rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx, NULL, argv[0], 0);
3091 * Delete a share on a remote RPC server
3093 * @param domain_sid The domain sid acquired from the remote server
3094 * @param argc Standard main() style argc
3095 * @param argv Standard main() style argv. Initial components are already
3098 * @return A shell status integer (0 for success)
3100 static int rpc_share_delete(int argc, const char **argv)
3103 DEBUG(1,("Sharename not specified on delete\n"));
3104 return rpc_share_usage(argc, argv);
3106 return run_rpc_command(NULL, PI_SRVSVC, 0,
3107 rpc_share_del_internals,
3112 * Formatted print of share info
3114 * @param info1 pointer to struct srvsvc_NetShareInfo1 to format
3117 static void display_share_info_1(struct srvsvc_NetShareInfo1 *info1)
3119 if (opt_long_list_entries) {
3120 d_printf("%-12s %-8.8s %-50s\n",
3121 info1->name, share_type[info1->type],
3122 info1->comment ? info1->comment : "");
3124 d_printf("%s\n", info1->name);
3129 static NTSTATUS get_share_info(struct rpc_pipe_client *pipe_hnd,
3130 TALLOC_CTX *mem_ctx,
3134 union srvsvc_NetShareCtr *ctr,
3137 union srvsvc_NetShareInfo info;
3142 if (!(ctr->ctr1 = TALLOC_ZERO_P(
3143 mem_ctx, struct srvsvc_NetShareCtr1))) {
3144 return NT_STATUS_NO_MEMORY;
3148 if (!(ctr->ctr502 = TALLOC_ZERO_P(
3149 mem_ctx, struct srvsvc_NetShareCtr502))) {
3150 return NT_STATUS_NO_MEMORY;
3154 return NT_STATUS_INVALID_LEVEL;
3158 /* no specific share requested, enumerate all */
3162 return rpccli_srvsvc_NetShareEnum(pipe_hnd, mem_ctx, NULL,
3163 &level, ctr, 0xffffffff,
3167 /* request just one share */
3168 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx, NULL,
3169 argv[0], level, &info);
3170 if (!NT_STATUS_IS_OK(status)) {
3178 ctr->ctr1->count = 1;
3179 ctr->ctr1->array = info.info1;
3182 ctr->ctr501->count = 1;
3183 ctr->ctr502->array = info.info502;
3186 return NT_STATUS_INTERNAL_ERROR;
3190 return NT_STATUS_OK;
3194 * List shares on a remote RPC server
3196 * All parameters are provided by the run_rpc_command function, except for
3197 * argc, argv which are passes through.
3199 * @param domain_sid The domain sid acquired from the remote server
3200 * @param cli A cli_state connected to the server.
3201 * @param mem_ctx Talloc context, destoyed on completion of the function.
3202 * @param argc Standard main() style argc
3203 * @param argv Standard main() style argv. Initial components are already
3206 * @return Normal NTSTATUS return.
3209 static NTSTATUS rpc_share_list_internals(const DOM_SID *domain_sid,
3210 const char *domain_name,
3211 struct cli_state *cli,
3212 struct rpc_pipe_client *pipe_hnd,
3213 TALLOC_CTX *mem_ctx,
3217 union srvsvc_NetShareCtr ctr;
3219 uint32 i, level = 1;
3222 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr,
3224 if (!NT_STATUS_IS_OK(result))
3227 /* Display results */
3229 if (opt_long_list_entries) {
3231 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3232 "\nShare name Type Description\n"\
3233 "---------- ---- -----------\n");
3235 for (i = 0; i < numentries; i++)
3236 display_share_info_1(&ctr.ctr1->array[i]);
3238 return NT_STATUS_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3242 * 'net rpc share list' entrypoint.
3243 * @param argc Standard main() style argc
3244 * @param argv Standard main() style argv. Initial components are already
3247 static int rpc_share_list(int argc, const char **argv)
3249 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_list_internals, argc, argv);
3252 static BOOL check_share_availability(struct cli_state *cli, const char *netname)
3254 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3255 d_printf("skipping [%s]: not a file share.\n", netname);
3265 static BOOL check_share_sanity(struct cli_state *cli, const char *netname, uint32 type)
3267 /* only support disk shares */
3268 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3269 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3273 /* skip builtin shares */
3274 /* FIXME: should print$ be added too ? */
3275 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3276 strequal(netname,"global"))
3279 if (opt_exclude && in_list(netname, opt_exclude, False)) {
3280 printf("excluding [%s]\n", netname);
3284 return check_share_availability(cli, netname);
3288 * Migrate shares from a remote RPC server to the local RPC srever
3290 * All parameters are provided by the run_rpc_command function, except for
3291 * argc, argv which are passes through.
3293 * @param domain_sid The domain sid acquired from the remote server
3294 * @param cli A cli_state connected to the server.
3295 * @param mem_ctx Talloc context, destoyed on completion of the function.
3296 * @param argc Standard main() style argc
3297 * @param argv Standard main() style argv. Initial components are already
3300 * @return Normal NTSTATUS return.
3303 static NTSTATUS rpc_share_migrate_shares_internals(const DOM_SID *domain_sid,
3304 const char *domain_name,
3305 struct cli_state *cli,
3306 struct rpc_pipe_client *pipe_hnd,
3307 TALLOC_CTX *mem_ctx,
3312 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3313 union srvsvc_NetShareCtr ctr_src;
3315 struct rpc_pipe_client *srvsvc_pipe = NULL;
3316 struct cli_state *cli_dst = NULL;
3317 uint32 level = 502; /* includes secdesc */
3320 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src,
3322 if (!NT_STATUS_IS_OK(result))
3325 /* connect destination PI_SRVSVC */
3326 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3327 if (!NT_STATUS_IS_OK(nt_status))
3331 for (i = 0; i < numentries; i++) {
3332 uint32 parm_error = 0;
3333 union srvsvc_NetShareInfo info;
3335 /* reset error-code */
3336 nt_status = NT_STATUS_UNSUCCESSFUL;
3338 if (!check_share_sanity(cli, ctr_src.ctr502->array[i].name,
3339 ctr_src.ctr502->array[i].type))
3343 /* finally add the share on the dst server */
3345 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3346 ctr_src.ctr502->array[i].name,
3347 ctr_src.ctr502->array[i].path,
3348 ctr_src.ctr502->array[i].comment);
3350 info.info502 = &ctr_src.ctr502->array[i];
3352 result = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx, NULL,
3353 502, info, &parm_error);
3355 if (NT_STATUS_EQUAL(result, NT_STATUS_OBJECT_NAME_COLLISION)) {
3356 printf(" [%s] does already exist\n", ctr_src.ctr502->array[i].name);
3360 if (!NT_STATUS_IS_OK(result)) {
3361 printf("cannot add share: %s\n", nt_errstr(result));
3367 nt_status = NT_STATUS_OK;
3371 cli_shutdown(cli_dst);
3379 * Migrate shares from a rpc-server to another
3381 * @param argc Standard main() style argc
3382 * @param argv Standard main() style argv. Initial components are already
3385 * @return A shell status integer (0 for success)
3387 static int rpc_share_migrate_shares(int argc, const char **argv)
3391 printf("no server to migrate\n");
3395 return run_rpc_command(NULL, PI_SRVSVC, 0,
3396 rpc_share_migrate_shares_internals,
3403 * @param f file_info
3404 * @param mask current search mask
3405 * @param state arg-pointer
3408 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
3410 static NTSTATUS nt_status;
3411 static struct copy_clistate *local_state;
3412 static fstring filename, new_mask;
3416 local_state = (struct copy_clistate *)state;
3417 nt_status = NT_STATUS_UNSUCCESSFUL;
3419 if (strequal(f->name, ".") || strequal(f->name, ".."))
3422 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3425 if (f->mode & aDIR) {
3427 DEBUG(3,("got dir: %s\n", f->name));
3429 fstrcpy(dir, local_state->cwd);
3431 fstrcat(dir, f->name);
3433 switch (net_mode_share)
3435 case NET_MODE_SHARE_MIGRATE:
3436 /* create that directory */
3437 nt_status = net_copy_file(local_state->mem_ctx,
3438 local_state->cli_share_src,
3439 local_state->cli_share_dst,
3441 opt_acls? True : False,
3442 opt_attrs? True : False,
3443 opt_timestamps? True : False,
3447 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3451 if (!NT_STATUS_IS_OK(nt_status))
3452 printf("could not handle dir %s: %s\n",
3453 dir, nt_errstr(nt_status));
3455 /* search below that directory */
3456 fstrcpy(new_mask, dir);
3457 fstrcat(new_mask, "\\*");
3459 old_dir = local_state->cwd;
3460 local_state->cwd = dir;
3461 if (!sync_files(local_state, new_mask))
3462 printf("could not handle files\n");
3463 local_state->cwd = old_dir;
3470 fstrcpy(filename, local_state->cwd);
3471 fstrcat(filename, "\\");
3472 fstrcat(filename, f->name);
3474 DEBUG(3,("got file: %s\n", filename));
3476 switch (net_mode_share)
3478 case NET_MODE_SHARE_MIGRATE:
3479 nt_status = net_copy_file(local_state->mem_ctx,
3480 local_state->cli_share_src,
3481 local_state->cli_share_dst,
3483 opt_acls? True : False,
3484 opt_attrs? True : False,
3485 opt_timestamps? True: False,
3489 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3493 if (!NT_STATUS_IS_OK(nt_status))
3494 printf("could not handle file %s: %s\n",
3495 filename, nt_errstr(nt_status));
3500 * sync files, can be called recursivly to list files
3501 * and then call copy_fn for each file
3503 * @param cp_clistate pointer to the copy_clistate we work with
3504 * @param mask the current search mask
3506 * @return Boolean result
3508 BOOL sync_files(struct copy_clistate *cp_clistate, pstring mask)
3511 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3513 if (cli_list(cp_clistate->cli_share_src, mask, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3514 d_fprintf(stderr, "listing %s failed with error: %s\n",
3515 mask, cli_errstr(cp_clistate->cli_share_src));
3524 * Set the top level directory permissions before we do any further copies.
3525 * Should set up ACL inheritance.
3528 BOOL copy_top_level_perms(struct copy_clistate *cp_clistate,
3529 const char *sharename)
3531 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3533 switch (net_mode_share) {
3534 case NET_MODE_SHARE_MIGRATE:
3535 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3536 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
3537 cp_clistate->cli_share_src,
3538 cp_clistate->cli_share_dst,
3540 opt_acls? True : False,
3541 opt_attrs? True : False,
3542 opt_timestamps? True: False,
3546 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3550 if (!NT_STATUS_IS_OK(nt_status)) {
3551 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3552 sharename, nt_errstr(nt_status));
3560 * Sync all files inside a remote share to another share (over smb)
3562 * All parameters are provided by the run_rpc_command function, except for
3563 * argc, argv which are passes through.
3565 * @param domain_sid The domain sid acquired from the remote server
3566 * @param cli A cli_state connected to the server.
3567 * @param mem_ctx Talloc context, destoyed on completion of the function.
3568 * @param argc Standard main() style argc
3569 * @param argv Standard main() style argv. Initial components are already
3572 * @return Normal NTSTATUS return.
3575 static NTSTATUS rpc_share_migrate_files_internals(const DOM_SID *domain_sid,
3576 const char *domain_name,
3577 struct cli_state *cli,
3578 struct rpc_pipe_client *pipe_hnd,
3579 TALLOC_CTX *mem_ctx,
3584 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3585 union srvsvc_NetShareCtr ctr_src;
3588 struct copy_clistate cp_clistate;
3589 BOOL got_src_share = False;
3590 BOOL got_dst_share = False;
3591 pstring mask = "\\*";
3595 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
3597 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src,
3600 if (!NT_STATUS_IS_OK(result))
3603 for (i = 0; i < numentries; i++) {
3604 if (!check_share_sanity(cli, ctr_src.ctr502->array[i].name,
3605 ctr_src.ctr502->array[i].type))
3608 /* one might not want to mirror whole discs :) */
3609 if (strequal(ctr_src.ctr502->array[i].name, "print$") || ctr_src.ctr502->array[i].name[1] == '$') {
3610 d_printf("skipping [%s]: builtin/hidden share\n", ctr_src.ctr502->array[i].name);
3614 switch (net_mode_share)
3616 case NET_MODE_SHARE_MIGRATE:
3620 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3623 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3624 ctr_src.ctr502->array[i].name,
3625 opt_acls ? "including" : "without",
3626 opt_attrs ? "including" : "without",
3627 opt_timestamps ? "(preserving timestamps)" : "");
3629 cp_clistate.mem_ctx = mem_ctx;
3630 cp_clistate.cli_share_src = NULL;
3631 cp_clistate.cli_share_dst = NULL;
3632 cp_clistate.cwd = NULL;
3633 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3635 /* open share source */
3636 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3637 &cli->dest_ip, cli->desthost,
3638 ctr_src.ctr502->array[i].name, "A:");
3639 if (!NT_STATUS_IS_OK(nt_status))
3642 got_src_share = True;
3644 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3645 /* open share destination */
3646 nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3647 NULL, dst, ctr_src.ctr502->array[i].name, "A:");
3648 if (!NT_STATUS_IS_OK(nt_status))
3651 got_dst_share = True;
3654 if (!copy_top_level_perms(&cp_clistate, ctr_src.ctr502->array[i].name)) {
3655 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", ctr_src.ctr502->array[i].name);
3656 nt_status = NT_STATUS_UNSUCCESSFUL;
3660 if (!sync_files(&cp_clistate, mask)) {
3661 d_fprintf(stderr, "could not handle files for share: %s\n",
3662 ctr_src.ctr502->array[i].name);
3663 nt_status = NT_STATUS_UNSUCCESSFUL;
3668 nt_status = NT_STATUS_OK;
3673 cli_shutdown(cp_clistate.cli_share_src);
3676 cli_shutdown(cp_clistate.cli_share_dst);
3682 static int rpc_share_migrate_files(int argc, const char **argv)
3686 printf("no server to migrate\n");
3690 return run_rpc_command(NULL, PI_SRVSVC, 0,
3691 rpc_share_migrate_files_internals,
3696 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3698 * All parameters are provided by the run_rpc_command function, except for
3699 * argc, argv which are passes through.
3701 * @param domain_sid The domain sid acquired from the remote server
3702 * @param cli A cli_state connected to the server.
3703 * @param mem_ctx Talloc context, destoyed on completion of the function.
3704 * @param argc Standard main() style argc
3705 * @param argv Standard main() style argv. Initial components are already
3708 * @return Normal NTSTATUS return.
3711 static NTSTATUS rpc_share_migrate_security_internals(const DOM_SID *domain_sid,
3712 const char *domain_name,
3713 struct cli_state *cli,
3714 struct rpc_pipe_client *pipe_hnd,
3715 TALLOC_CTX *mem_ctx,
3720 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3721 union srvsvc_NetShareCtr ctr_src;
3722 union srvsvc_NetShareInfo info;
3724 struct rpc_pipe_client *srvsvc_pipe = NULL;
3725 struct cli_state *cli_dst = NULL;
3726 uint32 level = 502; /* includes secdesc */
3728 uint32 parm_error = 0;
3730 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src,
3733 if (!NT_STATUS_IS_OK(result))
3736 /* connect destination PI_SRVSVC */
3737 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3738 if (!NT_STATUS_IS_OK(nt_status))
3742 for (i = 0; i < numentries; i++) {
3743 /* reset error-code */
3744 nt_status = NT_STATUS_UNSUCCESSFUL;
3746 if (!check_share_sanity(cli, ctr_src.ctr502->array[i].name, ctr_src.ctr502->array[i].type))
3749 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3750 ctr_src.ctr502->array[i].name,
3751 ctr_src.ctr502->array[i].path,
3752 ctr_src.ctr502->array[i].comment);
3755 display_sec_desc(ctr_src.ctr502->array[i].sd);
3760 /* finally modify the share on the dst server */
3761 result = rpccli_srvsvc_NetShareSetInfo(
3762 srvsvc_pipe, mem_ctx, NULL, argv[0], level, info,
3765 if (!NT_STATUS_IS_OK(result)) {
3766 printf("cannot set share-acl: %s\n", nt_errstr(result));
3772 nt_status = NT_STATUS_OK;
3776 cli_shutdown(cli_dst);
3784 * Migrate share-acls from a rpc-server to another
3786 * @param argc Standard main() style argc
3787 * @param argv Standard main() style argv. Initial components are already
3790 * @return A shell status integer (0 for success)
3792 static int rpc_share_migrate_security(int argc, const char **argv)
3796 printf("no server to migrate\n");
3800 return run_rpc_command(NULL, PI_SRVSVC, 0,
3801 rpc_share_migrate_security_internals,
3806 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3807 * from one server to another
3809 * @param argc Standard main() style argc
3810 * @param argv Standard main() style argv. Initial components are already
3813 * @return A shell status integer (0 for success)
3816 static int rpc_share_migrate_all(int argc, const char **argv)
3821 printf("no server to migrate\n");
3825 /* order is important. we don't want to be locked out by the share-acl
3826 * before copying files - gd */
3828 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
3832 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
3836 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_security_internals, argc, argv);
3841 * 'net rpc share migrate' entrypoint.
3842 * @param argc Standard main() style argc
3843 * @param argv Standard main() style argv. Initial components are already
3846 static int rpc_share_migrate(int argc, const char **argv)
3849 struct functable func[] = {
3850 {"all", rpc_share_migrate_all},
3851 {"files", rpc_share_migrate_files},
3852 {"help", rpc_share_usage},
3853 {"security", rpc_share_migrate_security},
3854 {"shares", rpc_share_migrate_shares},
3858 net_mode_share = NET_MODE_SHARE_MIGRATE;
3860 return net_run_function(argc, argv, func, rpc_share_usage);
3869 static int num_server_aliases;
3870 static struct full_alias *server_aliases;
3873 * Add an alias to the static list.
3875 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3877 if (server_aliases == NULL)
3878 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3880 server_aliases[num_server_aliases] = *alias;
3881 num_server_aliases += 1;
3885 * For a specific domain on the server, fetch all the aliases
3886 * and their members. Add all of them to the server_aliases.
3889 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3890 TALLOC_CTX *mem_ctx,
3891 POLICY_HND *connect_pol,
3892 const DOM_SID *domain_sid)
3894 uint32 start_idx, max_entries, num_entries, i;
3895 struct acct_info *groups;
3897 POLICY_HND domain_pol;
3899 /* Get domain policy handle */
3901 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, connect_pol,
3902 MAXIMUM_ALLOWED_ACCESS,
3903 domain_sid, &domain_pol);
3904 if (!NT_STATUS_IS_OK(result))
3911 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
3912 &start_idx, max_entries,
3913 &groups, &num_entries);
3915 for (i = 0; i < num_entries; i++) {
3917 POLICY_HND alias_pol;
3918 struct full_alias alias;
3922 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
3923 MAXIMUM_ALLOWED_ACCESS,
3926 if (!NT_STATUS_IS_OK(result))
3929 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx,
3933 if (!NT_STATUS_IS_OK(result))
3936 result = rpccli_samr_close(pipe_hnd, mem_ctx, &alias_pol);
3937 if (!NT_STATUS_IS_OK(result))
3940 alias.members = NULL;
3942 if (alias.num_members > 0) {
3943 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3945 for (j = 0; j < alias.num_members; j++)
3946 sid_copy(&alias.members[j],
3950 sid_copy(&alias.sid, domain_sid);
3951 sid_append_rid(&alias.sid, groups[i].rid);
3953 push_alias(mem_ctx, &alias);
3955 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3957 result = NT_STATUS_OK;
3960 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
3966 * Dump server_aliases as names for debugging purposes.
3969 static NTSTATUS rpc_aliaslist_dump(const DOM_SID *domain_sid,
3970 const char *domain_name,
3971 struct cli_state *cli,
3972 struct rpc_pipe_client *pipe_hnd,
3973 TALLOC_CTX *mem_ctx,
3981 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
3982 SEC_RIGHTS_MAXIMUM_ALLOWED,
3984 if (!NT_STATUS_IS_OK(result))
3987 for (i=0; i<num_server_aliases; i++) {
3990 enum lsa_SidType *types;
3993 struct full_alias *alias = &server_aliases[i];
3995 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3997 &domains, &names, &types);
3998 if (!NT_STATUS_IS_OK(result))
4001 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4003 if (alias->num_members == 0) {
4008 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4011 &domains, &names, &types);
4013 if (!NT_STATUS_IS_OK(result) &&
4014 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4017 for (j=0; j<alias->num_members; j++)
4018 DEBUG(1, ("%s\\%s (%d); ",
4019 domains[j] ? domains[j] : "*unknown*",
4020 names[j] ? names[j] : "*unknown*",types[j]));
4024 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4026 return NT_STATUS_OK;
4030 * Fetch a list of all server aliases and their members into
4034 static NTSTATUS rpc_aliaslist_internals(const DOM_SID *domain_sid,
4035 const char *domain_name,
4036 struct cli_state *cli,
4037 struct rpc_pipe_client *pipe_hnd,
4038 TALLOC_CTX *mem_ctx,
4043 POLICY_HND connect_pol;
4045 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4048 if (!NT_STATUS_IS_OK(result))
4051 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4052 &global_sid_Builtin);
4054 if (!NT_STATUS_IS_OK(result))
4057 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4060 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
4065 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4067 token->num_sids = 4;
4069 token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4);
4071 token->user_sids[0] = *user_sid;
4072 sid_copy(&token->user_sids[1], &global_sid_World);
4073 sid_copy(&token->user_sids[2], &global_sid_Network);
4074 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4077 static void free_user_token(NT_USER_TOKEN *token)
4079 SAFE_FREE(token->user_sids);
4082 static BOOL is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4086 for (i=0; i<token->num_sids; i++) {
4087 if (sid_compare(sid, &token->user_sids[i]) == 0)
4093 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4095 if (is_sid_in_token(token, sid))
4098 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4099 if (!token->user_sids) {
4103 sid_copy(&token->user_sids[token->num_sids], sid);
4105 token->num_sids += 1;
4110 NT_USER_TOKEN token;
4113 static void dump_user_token(struct user_token *token)
4117 d_printf("%s\n", token->name);
4119 for (i=0; i<token->token.num_sids; i++) {
4120 d_printf(" %s\n", sid_string_static(&token->token.user_sids[i]));
4124 static BOOL is_alias_member(DOM_SID *sid, struct full_alias *alias)
4128 for (i=0; i<alias->num_members; i++) {
4129 if (sid_compare(sid, &alias->members[i]) == 0)
4136 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4140 for (i=0; i<num_server_aliases; i++) {
4141 if (is_alias_member(&sid, &server_aliases[i]))
4142 add_sid_to_token(token, &server_aliases[i].sid);
4147 * We got a user token with all the SIDs we can know about without asking the
4148 * server directly. These are the user and domain group sids. All of these can
4149 * be members of aliases. So scan the list of aliases for each of the SIDs and
4150 * add them to the token.
4153 static void collect_alias_memberships(NT_USER_TOKEN *token)
4155 int num_global_sids = token->num_sids;
4158 for (i=0; i<num_global_sids; i++) {
4159 collect_sid_memberships(token, token->user_sids[i]);
4163 static BOOL get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4165 struct winbindd_request request;
4166 struct winbindd_response response;
4174 fstr_sprintf(full_name, "%s%c%s",
4175 domain, *lp_winbind_separator(), user);
4177 /* First let's find out the user sid */
4179 ZERO_STRUCT(request);
4180 ZERO_STRUCT(response);
4182 fstrcpy(request.data.name.dom_name, domain);
4183 fstrcpy(request.data.name.name, user);
4185 result = winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response);
4187 if (result != NSS_STATUS_SUCCESS) {
4188 DEBUG(1, ("winbind could not find %s\n", full_name));
4192 if (response.data.sid.type != SID_NAME_USER) {
4193 DEBUG(1, ("%s is not a user\n", full_name));
4197 string_to_sid(&user_sid, response.data.sid.sid);
4199 init_user_token(token, &user_sid);
4201 /* And now the groups winbind knows about */
4203 ZERO_STRUCT(response);
4205 fstrcpy(request.data.username, full_name);
4207 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
4209 if (result != NSS_STATUS_SUCCESS) {
4210 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
4214 for (i = 0; i < response.data.num_entries; i++) {
4215 gid_t gid = ((gid_t *)response.extra_data.data)[i];
4218 struct winbindd_request sidrequest;
4219 struct winbindd_response sidresponse;
4221 ZERO_STRUCT(sidrequest);
4222 ZERO_STRUCT(sidresponse);
4224 sidrequest.data.gid = gid;
4226 result = winbindd_request_response(WINBINDD_GID_TO_SID,
4227 &sidrequest, &sidresponse);
4229 if (result != NSS_STATUS_SUCCESS) {
4230 DEBUG(1, ("winbind could not find SID of gid %d\n",
4235 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
4237 string_to_sid(&sid, sidresponse.data.sid.sid);
4238 add_sid_to_token(token, &sid);
4241 SAFE_FREE(response.extra_data.data);
4247 * Get a list of all user tokens we want to look at
4250 static BOOL get_user_tokens(int *num_tokens, struct user_token **user_tokens)
4252 struct winbindd_request request;
4253 struct winbindd_response response;
4254 const char *extra_data;
4257 struct user_token *result;
4259 if (lp_winbind_use_default_domain() &&
4260 (opt_target_workgroup == NULL)) {
4261 d_fprintf(stderr, "winbind use default domain = yes set, "
4262 "please specify a workgroup\n");
4266 /* Send request to winbind daemon */
4268 ZERO_STRUCT(request);
4269 ZERO_STRUCT(response);
4271 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
4275 /* Look through extra data */
4277 if (!response.extra_data.data)
4280 extra_data = (const char *)response.extra_data.data;
4283 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4287 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
4289 if (result == NULL) {
4290 DEBUG(1, ("Could not malloc sid array\n"));
4294 extra_data = (const char *)response.extra_data.data;
4297 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4299 fstring domain, user;
4302 fstrcpy(result[i].name, name);
4304 p = strchr(name, *lp_winbind_separator());
4306 DEBUG(3, ("%s\n", name));
4309 fstrcpy(domain, opt_target_workgroup);
4310 fstrcpy(user, name);
4313 fstrcpy(domain, name);
4318 get_user_sids(domain, user, &(result[i].token));
4322 SAFE_FREE(response.extra_data.data);
4324 *user_tokens = result;
4329 static BOOL get_user_tokens_from_file(FILE *f,
4331 struct user_token **tokens)
4333 struct user_token *token = NULL;
4338 if (fgets(line, sizeof(line)-1, f) == NULL) {
4342 if (line[strlen(line)-1] == '\n')
4343 line[strlen(line)-1] = '\0';
4345 if (line[0] == ' ') {
4349 string_to_sid(&sid, &line[1]);
4351 if (token == NULL) {
4352 DEBUG(0, ("File does not begin with username"));
4356 add_sid_to_token(&token->token, &sid);
4360 /* And a new user... */
4363 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4364 if (*tokens == NULL) {
4365 DEBUG(0, ("Could not realloc tokens\n"));
4369 token = &((*tokens)[*num_tokens-1]);
4371 fstrcpy(token->name, line);
4372 token->token.num_sids = 0;
4373 token->token.user_sids = NULL;
4382 * Show the list of all users that have access to a share
4385 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4386 TALLOC_CTX *mem_ctx,
4387 const char *netname,
4389 struct user_token *tokens)
4392 SEC_DESC *share_sd = NULL;
4393 SEC_DESC *root_sd = NULL;
4394 struct cli_state *cli = pipe_hnd->cli;
4396 union srvsvc_NetShareInfo info;
4400 result = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx, NULL, netname,
4403 if (!NT_STATUS_IS_OK(result)) {
4404 DEBUG(1, ("Coult not query secdesc for share %s\n",
4409 share_sd = info.info502->sd;
4410 if (share_sd == NULL) {
4411 DEBUG(1, ("Got no secdesc for share %s\n",
4417 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4421 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4424 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4427 for (i=0; i<num_tokens; i++) {
4431 if (share_sd != NULL) {
4432 if (!se_access_check(share_sd, &tokens[i].token,
4433 1, &acc_granted, &status)) {
4434 DEBUG(1, ("Could not check share_sd for "
4440 if (!NT_STATUS_IS_OK(status))
4444 if (root_sd == NULL) {
4445 d_printf(" %s\n", tokens[i].name);
4449 if (!se_access_check(root_sd, &tokens[i].token,
4450 1, &acc_granted, &status)) {
4451 DEBUG(1, ("Could not check root_sd for user %s\n",
4456 if (!NT_STATUS_IS_OK(status))
4459 d_printf(" %s\n", tokens[i].name);
4463 cli_close(cli, fnum);
4475 static void collect_share(const char *name, uint32 m,
4476 const char *comment, void *state)
4478 struct share_list *share_list = (struct share_list *)state;
4480 if (m != STYPE_DISKTREE)
4483 share_list->num_shares += 1;
4484 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4485 if (!share_list->shares) {
4486 share_list->num_shares = 0;
4489 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4492 static void rpc_share_userlist_usage(void)
4498 * List shares on a remote RPC server, including the security descriptors
4500 * All parameters are provided by the run_rpc_command function, except for
4501 * argc, argv which are passes through.
4503 * @param domain_sid The domain sid acquired from the remote server
4504 * @param cli A cli_state connected to the server.
4505 * @param mem_ctx Talloc context, destoyed on completion of the function.
4506 * @param argc Standard main() style argc
4507 * @param argv Standard main() style argv. Initial components are already
4510 * @return Normal NTSTATUS return.
4513 static NTSTATUS rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
4514 const char *domain_name,
4515 struct cli_state *cli,
4516 struct rpc_pipe_client *pipe_hnd,
4517 TALLOC_CTX *mem_ctx,
4527 struct user_token *tokens = NULL;
4530 struct share_list share_list;
4533 rpc_share_userlist_usage();
4534 return NT_STATUS_UNSUCCESSFUL;
4540 f = fopen(argv[0], "r");
4544 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4545 return NT_STATUS_UNSUCCESSFUL;
4548 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4554 DEBUG(0, ("Could not read users from file\n"));
4555 return NT_STATUS_UNSUCCESSFUL;
4558 for (i=0; i<num_tokens; i++)
4559 collect_alias_memberships(&tokens[i].token);
4562 share_list.num_shares = 0;
4563 share_list.shares = NULL;
4565 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4568 DEBUG(0, ("Error returning browse list: %s\n",
4573 for (i = 0; i < share_list.num_shares; i++) {
4574 char *netname = share_list.shares[i];
4576 if (netname[strlen(netname)-1] == '$')
4579 d_printf("%s\n", netname);
4581 show_userlist(pipe_hnd, mem_ctx, netname,
4582 num_tokens, tokens);
4585 for (i=0; i<num_tokens; i++) {
4586 free_user_token(&tokens[i].token);
4589 SAFE_FREE(share_list.shares);
4591 return NT_STATUS_OK;
4594 static int rpc_share_allowedusers(int argc, const char **argv)
4598 result = run_rpc_command(NULL, PI_SAMR, 0,
4599 rpc_aliaslist_internals,
4604 result = run_rpc_command(NULL, PI_LSARPC, 0,
4610 return run_rpc_command(NULL, PI_SRVSVC, 0,
4611 rpc_share_allowedusers_internals,
4615 int net_usersidlist(int argc, const char **argv)
4618 struct user_token *tokens = NULL;
4622 net_usersidlist_usage(argc, argv);
4626 if (!get_user_tokens(&num_tokens, &tokens)) {
4627 DEBUG(0, ("Could not get the user/sid list\n"));
4631 for (i=0; i<num_tokens; i++) {
4632 dump_user_token(&tokens[i]);
4633 free_user_token(&tokens[i].token);
4640 int net_usersidlist_usage(int argc, const char **argv)
4642 d_printf("net usersidlist\n"
4643 "\tprints out a list of all users the running winbind knows\n"
4644 "\tabout, together with all their SIDs. This is used as\n"
4645 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4647 net_common_flags_usage(argc, argv);
4652 * 'net rpc share' entrypoint.
4653 * @param argc Standard main() style argc
4654 * @param argv Standard main() style argv. Initial components are already
4658 int net_rpc_share(int argc, const char **argv)
4660 struct functable func[] = {
4661 {"add", rpc_share_add},
4662 {"delete", rpc_share_delete},
4663 {"allowedusers", rpc_share_allowedusers},
4664 {"migrate", rpc_share_migrate},
4665 {"list", rpc_share_list},
4670 return run_rpc_command(NULL, PI_SRVSVC, 0,
4671 rpc_share_list_internals,
4674 return net_run_function(argc, argv, func, rpc_share_usage);
4677 static NTSTATUS rpc_sh_share_list(TALLOC_CTX *mem_ctx,
4678 struct rpc_sh_ctx *ctx,
4679 struct rpc_pipe_client *pipe_hnd,
4680 int argc, const char **argv)
4682 return rpc_share_list_internals(ctx->domain_sid, ctx->domain_name,
4683 ctx->cli, pipe_hnd, mem_ctx,
4687 static NTSTATUS rpc_sh_share_add(TALLOC_CTX *mem_ctx,
4688 struct rpc_sh_ctx *ctx,
4689 struct rpc_pipe_client *pipe_hnd,
4690 int argc, const char **argv)
4692 union srvsvc_NetShareInfo info;
4693 struct srvsvc_NetShareInfo2 info2;
4695 uint32 parm_error = 0;
4697 if ((argc < 2) || (argc > 3)) {
4698 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4700 return NT_STATUS_INVALID_PARAMETER;
4703 info.info2 = &info2;
4704 info2.name = argv[0];
4705 info2.type = STYPE_DISKTREE;
4706 info2.comment = (argc == 3) ? argv[2] : "";
4708 result = rpccli_srvsvc_NetShareAdd(
4709 pipe_hnd, mem_ctx, NULL, 2, info, &parm_error);
4714 static NTSTATUS rpc_sh_share_delete(TALLOC_CTX *mem_ctx,
4715 struct rpc_sh_ctx *ctx,
4716 struct rpc_pipe_client *pipe_hnd,
4717 int argc, const char **argv)
4722 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4723 return NT_STATUS_INVALID_PARAMETER;
4726 result = rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx, NULL, argv[0], 0);
4730 static NTSTATUS rpc_sh_share_info(TALLOC_CTX *mem_ctx,
4731 struct rpc_sh_ctx *ctx,
4732 struct rpc_pipe_client *pipe_hnd,
4733 int argc, const char **argv)
4735 union srvsvc_NetShareInfo info;
4739 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4740 return NT_STATUS_INVALID_PARAMETER;
4743 result = rpccli_srvsvc_NetShareGetInfo(
4744 pipe_hnd, mem_ctx, NULL, argv[0], 2, &info);
4745 if (!NT_STATUS_IS_OK(result)) {
4749 d_printf("Name: %s\n", info.info2->name);
4750 d_printf("Comment: %s\n", info.info2->comment);
4751 d_printf("Path: %s\n", info.info2->path);
4752 d_printf("Password: %s\n", info.info2->password);
4758 struct rpc_sh_cmd *net_rpc_share_cmds(TALLOC_CTX *mem_ctx,
4759 struct rpc_sh_ctx *ctx)
4761 static struct rpc_sh_cmd cmds[] = {
4763 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
4764 "List available shares" },
4766 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
4769 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
4772 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
4773 "Get information about a share" },
4775 { NULL, NULL, 0, NULL, NULL }
4781 /****************************************************************************/
4783 static int rpc_file_usage(int argc, const char **argv)
4785 return net_help_file(argc, argv);
4789 * Close a file on a remote RPC server
4791 * All parameters are provided by the run_rpc_command function, except for
4792 * argc, argv which are passes through.
4794 * @param domain_sid The domain sid acquired from the remote server
4795 * @param cli A cli_state connected to the server.
4796 * @param mem_ctx Talloc context, destoyed on completion of the function.
4797 * @param argc Standard main() style argc
4798 * @param argv Standard main() style argv. Initial components are already
4801 * @return Normal NTSTATUS return.
4803 static NTSTATUS rpc_file_close_internals(const DOM_SID *domain_sid,
4804 const char *domain_name,
4805 struct cli_state *cli,
4806 struct rpc_pipe_client *pipe_hnd,
4807 TALLOC_CTX *mem_ctx,
4812 result = rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx, NULL, atoi(argv[0]));
4813 return NT_STATUS_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4817 * Close a file on a remote RPC server
4819 * @param argc Standard main() style argc
4820 * @param argv Standard main() style argv. Initial components are already
4823 * @return A shell status integer (0 for success)
4825 static int rpc_file_close(int argc, const char **argv)
4828 DEBUG(1, ("No fileid given on close\n"));
4829 return(rpc_file_usage(argc, argv));
4832 return run_rpc_command(NULL, PI_SRVSVC, 0,
4833 rpc_file_close_internals,
4838 * Formatted print of open file info
4840 * @param info3 FILE_INFO_3 contents
4841 * @param str3 strings for FILE_INFO_3
4844 static void display_file_info_3( struct srvsvc_NetFileInfo3 *info3 )
4846 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4847 info3->fid, info3->user, info3->permissions, info3->num_locks, info3->path);
4851 * List open files on a remote RPC server
4853 * All parameters are provided by the run_rpc_command function, except for
4854 * argc, argv which are passes through.
4856 * @param domain_sid The domain sid acquired from the remote server
4857 * @param cli A cli_state connected to the server.
4858 * @param mem_ctx Talloc context, destoyed on completion of the function.
4859 * @param argc Standard main() style argc
4860 * @param argv Standard main() style argv. Initial components are already
4863 * @return Normal NTSTATUS return.
4866 static NTSTATUS rpc_file_list_internals(const DOM_SID *domain_sid,
4867 const char *domain_name,
4868 struct cli_state *cli,
4869 struct rpc_pipe_client *pipe_hnd,
4870 TALLOC_CTX *mem_ctx,
4874 union srvsvc_NetFileCtr ctr;
4877 uint32 preferred_len = 0xffffffff, i;
4878 const char *username=NULL;
4884 /* if argc > 0, must be user command */
4886 username = smb_xstrdup(argv[0]);
4888 result = rpccli_srvsvc_NetFileEnum(pipe_hnd, mem_ctx, NULL, NULL,
4889 username, &level, &ctr,
4890 preferred_len, &numentries, &hnd);
4892 if (!NT_STATUS_IS_OK(result))
4895 /* Display results */
4898 "\nEnumerating open files on remote server:\n\n"\
4899 "\nFileId Opened by Perms Locks Path"\
4900 "\n------ --------- ----- ----- ---- \n");
4901 for (i = 0; i < numentries; i++)
4902 display_file_info_3(&ctr.ctr3->array[i]);
4904 return NT_STATUS_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4908 * List files for a user on a remote RPC server
4910 * @param argc Standard main() style argc
4911 * @param argv Standard main() style argv. Initial components are already
4914 * @return A shell status integer (0 for success)
4917 static int rpc_file_user(int argc, const char **argv)
4920 DEBUG(1, ("No username given\n"));
4921 return(rpc_file_usage(argc, argv));
4924 return run_rpc_command(NULL, PI_SRVSVC, 0,
4925 rpc_file_list_internals,
4930 * 'net rpc file' entrypoint.
4931 * @param argc Standard main() style argc
4932 * @param argv Standard main() style argv. Initial components are already
4936 int net_rpc_file(int argc, const char **argv)
4938 struct functable func[] = {
4939 {"close", rpc_file_close},
4940 {"user", rpc_file_user},
4942 {"info", rpc_file_info},
4948 return run_rpc_command(NULL, PI_SRVSVC, 0,
4949 rpc_file_list_internals,
4952 return net_run_function(argc, argv, func, rpc_file_usage);
4956 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
4958 * All parameters are provided by the run_rpc_command function, except for
4959 * argc, argv which are passed through.
4961 * @param domain_sid The domain sid aquired from the remote server
4962 * @param cli A cli_state connected to the server.
4963 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4964 * @param argc Standard main() style argc
4965 * @param argv Standard main() style argv. Initial components are already
4968 * @return Normal NTSTATUS return.
4971 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
4972 const char *domain_name,
4973 struct cli_state *cli,
4974 struct rpc_pipe_client *pipe_hnd,
4975 TALLOC_CTX *mem_ctx,
4979 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4981 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL);
4983 if (NT_STATUS_IS_OK(result)) {
4984 d_printf("\nShutdown successfully aborted\n");
4985 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4987 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4993 * ABORT the shutdown of a remote RPC Server, over winreg pipe
4995 * All parameters are provided by the run_rpc_command function, except for
4996 * argc, argv which are passed through.
4998 * @param domain_sid The domain sid aquired from the remote server
4999 * @param cli A cli_state connected to the server.
5000 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5001 * @param argc Standard main() style argc
5002 * @param argv Standard main() style argv. Initial components are already
5005 * @return Normal NTSTATUS return.
5008 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
5009 const char *domain_name,
5010 struct cli_state *cli,
5011 struct rpc_pipe_client *pipe_hnd,
5012 TALLOC_CTX *mem_ctx,
5016 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5018 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL);
5020 if (NT_STATUS_IS_OK(result)) {
5021 d_printf("\nShutdown successfully aborted\n");
5022 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5024 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5030 * ABORT the Shut down of a remote RPC server
5032 * @param argc Standard main() style argc
5033 * @param argv Standard main() style argv. Initial components are already
5036 * @return A shell status integer (0 for success)
5039 static int rpc_shutdown_abort(int argc, const char **argv)
5041 int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0,
5042 rpc_shutdown_abort_internals,
5048 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5050 return run_rpc_command(NULL, PI_WINREG, 0,
5051 rpc_reg_shutdown_abort_internals,
5056 * Shut down a remote RPC Server via initshutdown pipe
5058 * All parameters are provided by the run_rpc_command function, except for
5059 * argc, argv which are passes through.
5061 * @param domain_sid The domain sid aquired from the remote server
5062 * @param cli A cli_state connected to the server.
5063 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5064 * @param argc Standard main() style argc
5065 * @param argc Standard main() style argv. Initial components are already
5068 * @return Normal NTSTATUS return.
5071 static NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
5072 const char *domain_name,
5073 struct cli_state *cli,
5074 struct rpc_pipe_client *pipe_hnd,
5075 TALLOC_CTX *mem_ctx,
5079 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5080 const char *msg = "This machine will be shutdown shortly";
5081 uint32 timeout = 20;
5082 struct initshutdown_String msg_string;
5083 struct initshutdown_String_sub s;
5089 timeout = opt_timeout;
5093 msg_string.name = &s;
5095 /* create an entry */
5096 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5097 &msg_string, timeout, opt_force, opt_reboot);
5099 if (NT_STATUS_IS_OK(result)) {
5100 d_printf("\nShutdown of remote machine succeeded\n");
5101 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5103 DEBUG(1,("Shutdown of remote machine failed!\n"));
5109 * Shut down a remote RPC Server via winreg pipe
5111 * All parameters are provided by the run_rpc_command function, except for
5112 * argc, argv which are passes through.
5114 * @param domain_sid The domain sid aquired from the remote server
5115 * @param cli A cli_state connected to the server.
5116 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5117 * @param argc Standard main() style argc
5118 * @param argc Standard main() style argv. Initial components are already
5121 * @return Normal NTSTATUS return.
5124 static NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
5125 const char *domain_name,
5126 struct cli_state *cli,
5127 struct rpc_pipe_client *pipe_hnd,
5128 TALLOC_CTX *mem_ctx,
5132 const char *msg = "This machine will be shutdown shortly";
5133 uint32 timeout = 20;
5134 struct initshutdown_String msg_string;
5135 struct initshutdown_String_sub s;
5142 msg_string.name = &s;
5145 timeout = opt_timeout;
5148 /* create an entry */
5149 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5150 &msg_string, timeout, opt_force, opt_reboot);
5152 if (NT_STATUS_IS_OK(result)) {
5153 d_printf("\nShutdown of remote machine succeeded\n");
5155 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5156 if ( W_ERROR_EQUAL(ntstatus_to_werror(result),WERR_MACHINE_LOCKED) )
5157 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5159 d_fprintf(stderr, "\nresult was: %s\n", nt_errstr(result));
5166 * Shut down a remote RPC server
5168 * @param argc Standard main() style argc
5169 * @param argc Standard main() style argv. Initial components are already
5172 * @return A shell status integer (0 for success)
5175 static int rpc_shutdown(int argc, const char **argv)
5177 int rc = run_rpc_command(NULL, PI_INITSHUTDOWN, 0,
5178 rpc_init_shutdown_internals,
5182 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5183 rc = run_rpc_command(NULL, PI_WINREG, 0,
5184 rpc_reg_shutdown_internals, argc, argv);
5190 /***************************************************************************
5191 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5193 ***************************************************************************/
5196 * Add interdomain trust account to the RPC server.
5197 * All parameters (except for argc and argv) are passed by run_rpc_command
5200 * @param domain_sid The domain sid acquired from the server
5201 * @param cli A cli_state connected to the server.
5202 * @param mem_ctx Talloc context, destoyed on completion of the function.
5203 * @param argc Standard main() style argc
5204 * @param argc Standard main() style argv. Initial components are already
5207 * @return normal NTSTATUS return code
5210 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
5211 const char *domain_name,
5212 struct cli_state *cli,
5213 struct rpc_pipe_client *pipe_hnd,
5214 TALLOC_CTX *mem_ctx,
5218 POLICY_HND connect_pol, domain_pol, user_pol;
5219 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5222 uint32 unknown, user_rid;
5225 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5226 return NT_STATUS_INVALID_PARAMETER;
5230 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5233 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5234 return NT_STATUS_NO_MEMORY;
5237 strupper_m(acct_name);
5239 /* Get samr policy handle */
5240 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5242 if (!NT_STATUS_IS_OK(result)) {
5246 /* Get domain policy handle */
5247 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5248 MAXIMUM_ALLOWED_ACCESS,
5249 domain_sid, &domain_pol);
5250 if (!NT_STATUS_IS_OK(result)) {
5254 /* Create trusting domain's account */
5255 acb_info = ACB_NORMAL;
5256 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
5257 mimir: yes, most probably it is */
5259 result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
5260 acct_name, acb_info, unknown,
5261 &user_pol, &user_rid);
5262 if (!NT_STATUS_IS_OK(result)) {
5267 SAM_USERINFO_CTR ctr;
5268 SAM_USER_INFO_23 p23;
5274 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5278 ZERO_STRUCT(notime);
5282 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
5283 acb_info = ACB_DOMTRUST;
5285 init_sam_user_info23A(&p23, ¬ime, ¬ime, ¬ime,
5286 ¬ime, ¬ime, ¬ime,
5287 nostr, nostr, nostr, nostr, nostr,
5288 nostr, nostr, nostr, nostr, nostr,
5289 0, 0, acb_info, ACCT_FLAGS, 168, &hrs,
5290 0, 0, (char *)pwbuf);
5291 ctr.switch_value = 23;
5292 ctr.info.id23 = &p23;
5293 p23.passmustchange = 0;
5295 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 23,
5296 &cli->user_session_key, &ctr);
5298 if (!NT_STATUS_IS_OK(result)) {
5299 DEBUG(0,("Could not set trust account password: %s\n",
5300 nt_errstr(result)));
5306 SAFE_FREE(acct_name);
5311 * Create interdomain trust account for a remote domain.
5313 * @param argc standard argc
5314 * @param argv standard argv without initial components
5316 * @return Integer status (0 means success)
5319 static int rpc_trustdom_add(int argc, const char **argv)
5322 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
5325 d_printf("Usage: net rpc trustdom add <domain>\n");
5332 * Remove interdomain trust account from the RPC server.
5333 * All parameters (except for argc and argv) are passed by run_rpc_command
5336 * @param domain_sid The domain sid acquired from the server
5337 * @param cli A cli_state connected to the server.
5338 * @param mem_ctx Talloc context, destoyed on completion of the function.
5339 * @param argc Standard main() style argc
5340 * @param argc Standard main() style argv. Initial components are already
5343 * @return normal NTSTATUS return code
5346 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
5347 const char *domain_name,
5348 struct cli_state *cli,
5349 struct rpc_pipe_client *pipe_hnd,
5350 TALLOC_CTX *mem_ctx,
5354 POLICY_HND connect_pol, domain_pol, user_pol;
5355 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5358 DOM_SID trust_acct_sid;
5359 uint32 *user_rids, num_rids, *name_types;
5360 uint32 flags = 0x000003e8; /* Unknown */
5363 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5364 return NT_STATUS_INVALID_PARAMETER;
5368 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5370 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5372 if (acct_name == NULL)
5373 return NT_STATUS_NO_MEMORY;
5375 strupper_m(acct_name);
5377 if ((names = TALLOC_ARRAY(mem_ctx, const char *, 1)) == NULL) {
5378 return NT_STATUS_NO_MEMORY;
5380 names[0] = acct_name;
5383 /* Get samr policy handle */
5384 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5386 if (!NT_STATUS_IS_OK(result)) {
5390 /* Get domain policy handle */
5391 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5392 MAXIMUM_ALLOWED_ACCESS,
5393 domain_sid, &domain_pol);
5394 if (!NT_STATUS_IS_OK(result)) {
5398 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, flags, 1,
5400 &user_rids, &name_types);
5402 if (!NT_STATUS_IS_OK(result)) {
5406 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
5407 MAXIMUM_ALLOWED_ACCESS,
5408 user_rids[0], &user_pol);
5410 if (!NT_STATUS_IS_OK(result)) {
5414 /* append the rid to the domain sid */
5415 sid_copy(&trust_acct_sid, domain_sid);
5416 if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
5420 /* remove the sid */
5422 result = rpccli_samr_remove_sid_foreign_domain(pipe_hnd, mem_ctx, &user_pol,
5425 if (!NT_STATUS_IS_OK(result)) {
5431 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
5433 if (!NT_STATUS_IS_OK(result)) {
5437 if (!NT_STATUS_IS_OK(result)) {
5438 DEBUG(0,("Could not set trust account password: %s\n",
5439 nt_errstr(result)));
5448 * Delete interdomain trust account for a remote domain.
5450 * @param argc standard argc
5451 * @param argv standard argv without initial components
5453 * @return Integer status (0 means success)
5456 static int rpc_trustdom_del(int argc, const char **argv)
5459 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
5462 d_printf("Usage: net rpc trustdom del <domain>\n");
5469 * Establish trust relationship to a trusting domain.
5470 * Interdomain account must already be created on remote PDC.
5472 * @param argc standard argc
5473 * @param argv standard argv without initial components
5475 * @return Integer status (0 means success)
5478 static int rpc_trustdom_establish(int argc, const char **argv)
5480 struct cli_state *cli = NULL;
5481 struct in_addr server_ip;
5482 struct rpc_pipe_client *pipe_hnd = NULL;
5483 POLICY_HND connect_hnd;
5484 TALLOC_CTX *mem_ctx;
5486 DOM_SID *domain_sid;
5489 char* domain_name_pol;
5494 * Connect to \\server\ipc$ as 'our domain' account with password
5498 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5502 domain_name = smb_xstrdup(argv[0]);
5503 strupper_m(domain_name);
5505 /* account name used at first is our domain's name with '$' */
5506 asprintf(&acct_name, "%s$", lp_workgroup());
5507 strupper_m(acct_name);
5510 * opt_workgroup will be used by connection functions further,
5511 * hence it should be set to remote domain name instead of ours
5513 if (opt_workgroup) {
5514 opt_workgroup = smb_xstrdup(domain_name);
5517 opt_user_name = acct_name;
5519 /* find the domain controller */
5520 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
5521 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5525 /* connect to ipc$ as username/password */
5526 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
5527 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5529 /* Is it trusting domain account for sure ? */
5530 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5531 nt_errstr(nt_status)));
5535 /* store who we connected to */
5537 saf_store( domain_name, pdc_name );
5540 * Connect to \\server\ipc$ again (this time anonymously)
5543 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
5545 if (NT_STATUS_IS_ERR(nt_status)) {
5546 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5547 domain_name, nt_errstr(nt_status)));
5552 * Use NetServerEnum2 to make sure we're talking to a proper server
5555 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
5556 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
5557 for domain %s\n", domain_name));
5562 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5563 "domain %s", domain_name))) {
5564 DEBUG(0, ("talloc_init() failed\n"));
5570 * Call LsaOpenPolicy and LsaQueryInfo
5573 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5575 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5580 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
5582 if (NT_STATUS_IS_ERR(nt_status)) {
5583 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5584 nt_errstr(nt_status)));
5589 /* Querying info level 5 */
5591 nt_status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &connect_hnd,
5593 &domain_name_pol, &domain_sid);
5594 if (NT_STATUS_IS_ERR(nt_status)) {
5595 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5596 nt_errstr(nt_status)));
5601 /* There should be actually query info level 3 (following nt serv behaviour),
5602 but I still don't know if it's _really_ necessary */
5605 * Store the password in secrets db
5608 if (!pdb_set_trusteddom_pw(domain_name, opt_password, domain_sid)) {
5609 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5615 * Close the pipes and clean up
5618 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5619 if (NT_STATUS_IS_ERR(nt_status)) {
5620 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5621 nt_errstr(nt_status)));
5628 talloc_destroy(mem_ctx);
5630 d_printf("Trust to domain %s established\n", domain_name);
5635 * Revoke trust relationship to the remote domain
5637 * @param argc standard argc
5638 * @param argv standard argv without initial components
5640 * @return Integer status (0 means success)
5643 static int rpc_trustdom_revoke(int argc, const char **argv)
5648 if (argc < 1) return -1;
5650 /* generate upper cased domain name */
5651 domain_name = smb_xstrdup(argv[0]);
5652 strupper_m(domain_name);
5654 /* delete password of the trust */
5655 if (!pdb_del_trusteddom_pw(domain_name)) {
5656 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5663 SAFE_FREE(domain_name);
5668 * Usage for 'net rpc trustdom' command
5670 * @param argc standard argc
5671 * @param argv standard argv without inital components
5673 * @return Integer status returned to shell
5676 static int rpc_trustdom_usage(int argc, const char **argv)
5678 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
5679 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
5680 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
5681 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5682 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
5683 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5688 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
5689 const char *domain_name,
5690 struct cli_state *cli,
5691 struct rpc_pipe_client *pipe_hnd,
5692 TALLOC_CTX *mem_ctx,
5697 sid_to_string(str_sid, domain_sid);
5698 d_printf("%s\n", str_sid);
5699 return NT_STATUS_OK;
5702 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5704 fstring ascii_sid, padding;
5705 int pad_len, col_len = 20;
5707 /* convert sid into ascii string */
5708 sid_to_string(ascii_sid, dom_sid);
5710 /* calculate padding space for d_printf to look nicer */
5711 pad_len = col_len - strlen(trusted_dom_name);
5712 padding[pad_len] = 0;
5713 do padding[--pad_len] = ' '; while (pad_len);
5715 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5718 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5719 TALLOC_CTX *mem_ctx,
5722 const char *trusted_dom_name)
5725 LSA_TRUSTED_DOMAIN_INFO *info;
5726 char *cleartextpwd = NULL;
5729 nt_status = rpccli_lsa_query_trusted_domain_info_by_sid(pipe_hnd, mem_ctx, pol, 4, &dom_sid, &info);
5731 if (NT_STATUS_IS_ERR(nt_status)) {
5732 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5733 nt_errstr(nt_status)));
5737 data = data_blob(NULL, info->password.password.length);
5739 memcpy(data.data, info->password.password.data, info->password.password.length);
5740 data.length = info->password.password.length;
5742 cleartextpwd = decrypt_trustdom_secret(pipe_hnd->cli->pwd.password, &data);
5744 if (cleartextpwd == NULL) {
5745 DEBUG(0,("retrieved NULL password\n"));
5746 nt_status = NT_STATUS_UNSUCCESSFUL;
5750 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5751 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5752 nt_status = NT_STATUS_UNSUCCESSFUL;
5756 #ifdef DEBUG_PASSWORD
5757 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], password: [%s]\n",
5758 trusted_dom_name, sid_string_static(&dom_sid), cleartextpwd));
5762 SAFE_FREE(cleartextpwd);
5763 data_blob_free(&data);
5768 static int rpc_trustdom_vampire(int argc, const char **argv)
5770 /* common variables */
5771 TALLOC_CTX* mem_ctx;
5772 struct cli_state *cli = NULL;
5773 struct rpc_pipe_client *pipe_hnd = NULL;
5775 const char *domain_name = NULL;
5776 DOM_SID *queried_dom_sid;
5777 POLICY_HND connect_hnd;
5779 /* trusted domains listing variables */
5780 unsigned int num_domains, enum_ctx = 0;
5782 DOM_SID *domain_sids;
5783 char **trusted_dom_names;
5788 * Listing trusted domains (stored in secrets.tdb, if local)
5791 mem_ctx = talloc_init("trust relationships vampire");
5794 * set domain and pdc name to local samba server (default)
5795 * or to remote one given in command line
5798 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5799 domain_name = opt_workgroup;
5800 opt_target_workgroup = opt_workgroup;
5802 fstrcpy(pdc_name, global_myname());
5803 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5804 opt_target_workgroup = domain_name;
5807 /* open \PIPE\lsarpc and open policy handle */
5808 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5809 DEBUG(0, ("Couldn't connect to domain controller\n"));
5813 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5815 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5816 nt_errstr(nt_status) ));
5821 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5823 if (NT_STATUS_IS_ERR(nt_status)) {
5824 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5825 nt_errstr(nt_status)));
5830 /* query info level 5 to obtain sid of a domain being queried */
5831 nt_status = rpccli_lsa_query_info_policy(
5832 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
5833 &dummy, &queried_dom_sid);
5835 if (NT_STATUS_IS_ERR(nt_status)) {
5836 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5837 nt_errstr(nt_status)));
5843 * Keep calling LsaEnumTrustdom over opened pipe until
5844 * the end of enumeration is reached
5847 d_printf("Vampire trusted domains:\n\n");
5850 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
5852 &trusted_dom_names, &domain_sids);
5854 if (NT_STATUS_IS_ERR(nt_status)) {
5855 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5856 nt_errstr(nt_status)));
5861 for (i = 0; i < num_domains; i++) {
5863 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5865 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5866 domain_sids[i], trusted_dom_names[i]);
5867 if (!NT_STATUS_IS_OK(nt_status)) {
5874 * in case of no trusted domains say something rather
5875 * than just display blank line
5877 if (!num_domains) d_printf("none\n");
5879 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5881 /* close this connection before doing next one */
5882 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5883 if (NT_STATUS_IS_ERR(nt_status)) {
5884 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5885 nt_errstr(nt_status)));
5890 /* close lsarpc pipe and connection to IPC$ */
5893 talloc_destroy(mem_ctx);
5897 static int rpc_trustdom_list(int argc, const char **argv)
5899 /* common variables */
5900 TALLOC_CTX* mem_ctx;
5901 struct cli_state *cli = NULL, *remote_cli = NULL;
5902 struct rpc_pipe_client *pipe_hnd = NULL;
5904 const char *domain_name = NULL;
5905 DOM_SID *queried_dom_sid;
5907 int ascii_dom_name_len;
5908 POLICY_HND connect_hnd;
5910 /* trusted domains listing variables */
5911 unsigned int num_domains, enum_ctx = 0;
5912 int i, pad_len, col_len = 20;
5913 DOM_SID *domain_sids;
5914 char **trusted_dom_names;
5918 /* trusting domains listing variables */
5919 POLICY_HND domain_hnd;
5920 char **trusting_dom_names;
5921 uint32 *trusting_dom_rids;
5924 * Listing trusted domains (stored in secrets.tdb, if local)
5927 mem_ctx = talloc_init("trust relationships listing");
5930 * set domain and pdc name to local samba server (default)
5931 * or to remote one given in command line
5934 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5935 domain_name = opt_workgroup;
5936 opt_target_workgroup = opt_workgroup;
5938 fstrcpy(pdc_name, global_myname());
5939 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5940 opt_target_workgroup = domain_name;
5943 /* open \PIPE\lsarpc and open policy handle */
5944 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5945 DEBUG(0, ("Couldn't connect to domain controller\n"));
5949 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5951 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5952 nt_errstr(nt_status) ));
5956 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5958 if (NT_STATUS_IS_ERR(nt_status)) {
5959 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5960 nt_errstr(nt_status)));
5964 /* query info level 5 to obtain sid of a domain being queried */
5965 nt_status = rpccli_lsa_query_info_policy(
5966 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
5967 &dummy, &queried_dom_sid);
5969 if (NT_STATUS_IS_ERR(nt_status)) {
5970 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5971 nt_errstr(nt_status)));
5976 * Keep calling LsaEnumTrustdom over opened pipe until
5977 * the end of enumeration is reached
5980 d_printf("Trusted domains list:\n\n");
5983 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
5985 &trusted_dom_names, &domain_sids);
5987 if (NT_STATUS_IS_ERR(nt_status)) {
5988 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5989 nt_errstr(nt_status)));
5993 for (i = 0; i < num_domains; i++) {
5994 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5998 * in case of no trusted domains say something rather
5999 * than just display blank line
6001 if (!num_domains) d_printf("none\n");
6003 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6005 /* close this connection before doing next one */
6006 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6007 if (NT_STATUS_IS_ERR(nt_status)) {
6008 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6009 nt_errstr(nt_status)));
6013 cli_rpc_pipe_close(pipe_hnd);
6016 * Listing trusting domains (stored in passdb backend, if local)
6019 d_printf("\nTrusting domains list:\n\n");
6022 * Open \PIPE\samr and get needed policy handles
6024 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6026 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6031 nt_status = rpccli_samr_connect(pipe_hnd, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
6033 if (!NT_STATUS_IS_OK(nt_status)) {
6034 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6035 nt_errstr(nt_status)));
6039 /* SamrOpenDomain - we have to open domain policy handle in order to be
6040 able to enumerate accounts*/
6041 nt_status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_hnd,
6042 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6043 queried_dom_sid, &domain_hnd);
6044 if (!NT_STATUS_IS_OK(nt_status)) {
6045 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6046 nt_errstr(nt_status)));
6051 * perform actual enumeration
6054 enum_ctx = 0; /* reset enumeration context from last enumeration */
6057 nt_status = rpccli_samr_enum_dom_users(pipe_hnd, mem_ctx, &domain_hnd,
6058 &enum_ctx, ACB_DOMTRUST, 0xffff,
6059 &trusting_dom_names, &trusting_dom_rids,
6061 if (NT_STATUS_IS_ERR(nt_status)) {
6062 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6063 nt_errstr(nt_status)));
6067 for (i = 0; i < num_domains; i++) {
6070 * get each single domain's sid (do we _really_ need this ?):
6071 * 1) connect to domain's pdc
6072 * 2) query the pdc for domain's sid
6075 /* get rid of '$' tail */
6076 ascii_dom_name_len = strlen(trusting_dom_names[i]);
6077 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6078 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
6080 /* calculate padding space for d_printf to look nicer */
6081 pad_len = col_len - strlen(trusting_dom_names[i]);
6082 padding[pad_len] = 0;
6083 do padding[--pad_len] = ' '; while (pad_len);
6085 /* set opt_* variables to remote domain */
6086 strupper_m(trusting_dom_names[i]);
6087 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
6088 opt_target_workgroup = opt_workgroup;
6090 d_printf("%s%s", trusting_dom_names[i], padding);
6092 /* connect to remote domain controller */
6093 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
6095 /* query for domain's sid */
6096 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
6097 d_fprintf(stderr, "couldn't get domain's sid\n");
6099 cli_shutdown(remote_cli);
6102 d_fprintf(stderr, "domain controller is not responding\n");
6106 if (!num_domains) d_printf("none\n");
6108 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6110 /* close opened samr and domain policy handles */
6111 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &domain_hnd);
6112 if (!NT_STATUS_IS_OK(nt_status)) {
6113 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6116 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &connect_hnd);
6117 if (!NT_STATUS_IS_OK(nt_status)) {
6118 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6121 /* close samr pipe and connection to IPC$ */
6124 talloc_destroy(mem_ctx);
6129 * Entrypoint for 'net rpc trustdom' code
6131 * @param argc standard argc
6132 * @param argv standard argv without initial components
6134 * @return Integer status (0 means success)
6137 static int rpc_trustdom(int argc, const char **argv)
6139 struct functable func[] = {
6140 {"add", rpc_trustdom_add},
6141 {"del", rpc_trustdom_del},
6142 {"establish", rpc_trustdom_establish},
6143 {"revoke", rpc_trustdom_revoke},
6144 {"help", rpc_trustdom_usage},
6145 {"list", rpc_trustdom_list},
6146 {"vampire", rpc_trustdom_vampire},
6151 rpc_trustdom_usage(argc, argv);
6155 return (net_run_function(argc, argv, func, rpc_user_usage));
6159 * Check if a server will take rpc commands
6160 * @param flags Type of server to connect to (PDC, DMB, localhost)
6161 * if the host is not explicitly specified
6162 * @return BOOL (true means rpc supported)
6164 BOOL net_rpc_check(unsigned flags)
6166 struct cli_state *cli;
6168 struct in_addr server_ip;
6169 char *server_name = NULL;
6171 /* flags (i.e. server type) may depend on command */
6172 if (!net_find_server(NULL, flags, &server_ip, &server_name))
6175 if ((cli = cli_initialise()) == NULL) {
6179 if (!cli_connect(cli, server_name, &server_ip))
6181 if (!attempt_netbios_session_request(&cli, global_myname(),
6182 server_name, &server_ip))
6184 if (!cli_negprot(cli))
6186 if (cli->protocol < PROTOCOL_NT1)
6195 /* dump sam database via samsync rpc calls */
6196 static int rpc_samdump(int argc, const char **argv) {
6197 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
6201 /* syncronise sam database via samsync rpc calls */
6202 static int rpc_vampire(int argc, const char **argv) {
6203 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
6208 * Migrate everything from a print-server
6210 * @param argc Standard main() style argc
6211 * @param argv Standard main() style argv. Initial components are already
6214 * @return A shell status integer (0 for success)
6216 * The order is important !
6217 * To successfully add drivers the print-queues have to exist !
6218 * Applying ACLs should be the last step, because you're easily locked out
6221 static int rpc_printer_migrate_all(int argc, const char **argv)
6226 printf("no server to migrate\n");
6230 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
6234 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
6238 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
6242 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
6246 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
6251 * Migrate print-drivers from a print-server
6253 * @param argc Standard main() style argc
6254 * @param argv Standard main() style argv. Initial components are already
6257 * @return A shell status integer (0 for success)
6259 static int rpc_printer_migrate_drivers(int argc, const char **argv)
6262 printf("no server to migrate\n");
6266 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6267 rpc_printer_migrate_drivers_internals,
6272 * Migrate print-forms from a print-server
6274 * @param argc Standard main() style argc
6275 * @param argv Standard main() style argv. Initial components are already
6278 * @return A shell status integer (0 for success)
6280 static int rpc_printer_migrate_forms(int argc, const char **argv)
6283 printf("no server to migrate\n");
6287 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6288 rpc_printer_migrate_forms_internals,
6293 * Migrate printers from a print-server
6295 * @param argc Standard main() style argc
6296 * @param argv Standard main() style argv. Initial components are already
6299 * @return A shell status integer (0 for success)
6301 static int rpc_printer_migrate_printers(int argc, const char **argv)
6304 printf("no server to migrate\n");
6308 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6309 rpc_printer_migrate_printers_internals,
6314 * Migrate printer-ACLs from a print-server
6316 * @param argc Standard main() style argc
6317 * @param argv Standard main() style argv. Initial components are already
6320 * @return A shell status integer (0 for success)
6322 static int rpc_printer_migrate_security(int argc, const char **argv)
6325 printf("no server to migrate\n");
6329 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6330 rpc_printer_migrate_security_internals,
6335 * Migrate printer-settings from a print-server
6337 * @param argc Standard main() style argc
6338 * @param argv Standard main() style argv. Initial components are already
6341 * @return A shell status integer (0 for success)
6343 static int rpc_printer_migrate_settings(int argc, const char **argv)
6346 printf("no server to migrate\n");
6350 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6351 rpc_printer_migrate_settings_internals,
6356 * 'net rpc printer' entrypoint.
6357 * @param argc Standard main() style argc
6358 * @param argv Standard main() style argv. Initial components are already
6362 int rpc_printer_migrate(int argc, const char **argv)
6365 /* ouch: when addriver and setdriver are called from within
6366 rpc_printer_migrate_drivers_internals, the printer-queue already
6369 struct functable func[] = {
6370 {"all", rpc_printer_migrate_all},
6371 {"drivers", rpc_printer_migrate_drivers},
6372 {"forms", rpc_printer_migrate_forms},
6373 {"help", rpc_printer_usage},
6374 {"printers", rpc_printer_migrate_printers},
6375 {"security", rpc_printer_migrate_security},
6376 {"settings", rpc_printer_migrate_settings},
6380 return net_run_function(argc, argv, func, rpc_printer_usage);
6385 * List printers on a remote RPC server
6387 * @param argc Standard main() style argc
6388 * @param argv Standard main() style argv. Initial components are already
6391 * @return A shell status integer (0 for success)
6393 static int rpc_printer_list(int argc, const char **argv)
6396 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6397 rpc_printer_list_internals,
6402 * List printer-drivers on a remote RPC server
6404 * @param argc Standard main() style argc
6405 * @param argv Standard main() style argv. Initial components are already
6408 * @return A shell status integer (0 for success)
6410 static int rpc_printer_driver_list(int argc, const char **argv)
6413 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6414 rpc_printer_driver_list_internals,
6419 * Publish printer in ADS via MSRPC
6421 * @param argc Standard main() style argc
6422 * @param argv Standard main() style argv. Initial components are already
6425 * @return A shell status integer (0 for success)
6427 static int rpc_printer_publish_publish(int argc, const char **argv)
6430 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6431 rpc_printer_publish_publish_internals,
6436 * Update printer in ADS via MSRPC
6438 * @param argc Standard main() style argc
6439 * @param argv Standard main() style argv. Initial components are already
6442 * @return A shell status integer (0 for success)
6444 static int rpc_printer_publish_update(int argc, const char **argv)
6447 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6448 rpc_printer_publish_update_internals,
6453 * UnPublish printer in ADS via MSRPC
6455 * @param argc Standard main() style argc
6456 * @param argv Standard main() style argv. Initial components are already
6459 * @return A shell status integer (0 for success)
6461 static int rpc_printer_publish_unpublish(int argc, const char **argv)
6464 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6465 rpc_printer_publish_unpublish_internals,
6470 * List published printers via MSRPC
6472 * @param argc Standard main() style argc
6473 * @param argv Standard main() style argv. Initial components are already
6476 * @return A shell status integer (0 for success)
6478 static int rpc_printer_publish_list(int argc, const char **argv)
6481 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6482 rpc_printer_publish_list_internals,
6488 * Publish printer in ADS
6490 * @param argc Standard main() style argc
6491 * @param argv Standard main() style argv. Initial components are already
6494 * @return A shell status integer (0 for success)
6496 static int rpc_printer_publish(int argc, const char **argv)
6499 struct functable func[] = {
6500 {"publish", rpc_printer_publish_publish},
6501 {"update", rpc_printer_publish_update},
6502 {"unpublish", rpc_printer_publish_unpublish},
6503 {"list", rpc_printer_publish_list},
6504 {"help", rpc_printer_usage},
6509 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6510 rpc_printer_publish_list_internals,
6513 return net_run_function(argc, argv, func, rpc_printer_usage);
6519 * Display rpc printer help page.
6520 * @param argc Standard main() style argc
6521 * @param argv Standard main() style argv. Initial components are already
6524 int rpc_printer_usage(int argc, const char **argv)
6526 return net_help_printer(argc, argv);
6530 * 'net rpc printer' entrypoint.
6531 * @param argc Standard main() style argc
6532 * @param argv Standard main() style argv. Initial components are already
6535 int net_rpc_printer(int argc, const char **argv)
6537 struct functable func[] = {
6538 {"list", rpc_printer_list},
6539 {"migrate", rpc_printer_migrate},
6540 {"driver", rpc_printer_driver_list},
6541 {"publish", rpc_printer_publish},
6546 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6547 rpc_printer_list_internals,
6550 return net_run_function(argc, argv, func, rpc_printer_usage);
6553 /****************************************************************************/
6557 * Basic usage function for 'net rpc'
6558 * @param argc Standard main() style argc
6559 * @param argv Standard main() style argv. Initial components are already
6563 int net_rpc_usage(int argc, const char **argv)
6565 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
6566 d_printf(" net rpc join \t\t\tto join a domain \n");
6567 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n");
6568 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
6569 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
6570 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
6571 d_printf(" net rpc group \t\tto list groups\n");
6572 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
6573 d_printf(" net rpc printer \t\tto list and migrate printers\n");
6574 d_printf(" net rpc file \t\t\tto list open files\n");
6575 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
6576 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
6577 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
6578 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
6579 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
6580 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
6581 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
6582 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
6583 d_printf(" net rpc registry\t\tto manage registry hives\n");
6584 d_printf(" net rpc service\t\tto start, stop and query services\n");
6585 d_printf(" net rpc audit\t\t\tto modify global auditing settings\n");
6586 d_printf(" net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
6588 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
6589 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
6590 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
6591 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
6592 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
6598 * Help function for 'net rpc'. Calls command specific help if requested
6599 * or displays usage of net rpc
6600 * @param argc Standard main() style argc
6601 * @param argv Standard main() style argv. Initial components are already
6605 int net_rpc_help(int argc, const char **argv)
6607 struct functable func[] = {
6608 {"join", rpc_join_usage},
6609 {"user", rpc_user_usage},
6610 {"group", rpc_group_usage},
6611 {"share", rpc_share_usage},
6612 /*{"changetrustpw", rpc_changetrustpw_usage}, */
6613 {"trustdom", rpc_trustdom_usage},
6614 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
6615 /*{"shutdown", rpc_shutdown_usage}, */
6616 {"vampire", rpc_vampire_usage},
6621 net_rpc_usage(argc, argv);
6625 return (net_run_function(argc, argv, func, rpc_user_usage));
6629 * 'net rpc' entrypoint.
6630 * @param argc Standard main() style argc
6631 * @param argv Standard main() style argv. Initial components are already
6635 int net_rpc(int argc, const char **argv)
6637 struct functable func[] = {
6638 {"audit", net_rpc_audit},
6639 {"info", net_rpc_info},
6640 {"join", net_rpc_join},
6641 {"oldjoin", net_rpc_oldjoin},
6642 {"testjoin", net_rpc_testjoin},
6643 {"user", net_rpc_user},
6644 {"password", rpc_user_password},
6645 {"group", net_rpc_group},
6646 {"share", net_rpc_share},
6647 {"file", net_rpc_file},
6648 {"printer", net_rpc_printer},
6649 {"changetrustpw", net_rpc_changetrustpw},
6650 {"trustdom", rpc_trustdom},
6651 {"abortshutdown", rpc_shutdown_abort},
6652 {"shutdown", rpc_shutdown},
6653 {"samdump", rpc_samdump},
6654 {"vampire", rpc_vampire},
6655 {"getsid", net_rpc_getsid},
6656 {"rights", net_rpc_rights},
6657 {"service", net_rpc_service},
6658 {"registry", net_rpc_registry},
6659 {"shell", net_rpc_shell},
6660 {"help", net_rpc_help},
6663 return net_run_function(argc, argv, func, net_rpc_usage);