r14101: Fix a segfault in trustdom establish, cli is NULL here.
[Samba/bb.git] / source / utils / net_rpc.c
blob1ba6794272843b1de3f04a0a64769e2f93343196
1 /*
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)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "includes.h"
24 #include "utils/net.h"
26 static int net_mode_share;
28 /**
29 * @file net_rpc.c
31 * @brief RPC based subcommands for the 'net' utility.
33 * This file should contain much of the functionality that used to
34 * be found in rpcclient, execpt that the commands should change
35 * less often, and the fucntionality should be sane (the user is not
36 * expected to know a rid/sid before they conduct an operation etc.)
38 * @todo Perhaps eventually these should be split out into a number
39 * of files, as this could get quite big.
40 **/
43 /**
44 * Many of the RPC functions need the domain sid. This function gets
45 * it at the start of every run
47 * @param cli A cli_state already connected to the remote machine
49 * @return The Domain SID of the remote machine.
50 **/
52 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
53 DOM_SID **domain_sid, char **domain_name)
55 struct rpc_pipe_client *lsa_pipe;
56 POLICY_HND pol;
57 NTSTATUS result = NT_STATUS_OK;
58 uint32 info_class = 5;
60 lsa_pipe = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
61 if (!lsa_pipe) {
62 d_fprintf(stderr, "Could not initialise lsa pipe\n");
63 return result;
66 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, False,
67 SEC_RIGHTS_MAXIMUM_ALLOWED,
68 &pol);
69 if (!NT_STATUS_IS_OK(result)) {
70 d_fprintf(stderr, "open_policy failed: %s\n",
71 nt_errstr(result));
72 return result;
75 result = rpccli_lsa_query_info_policy(lsa_pipe, mem_ctx, &pol,
76 info_class, domain_name,
77 domain_sid);
78 if (!NT_STATUS_IS_OK(result)) {
79 d_fprintf(stderr, "lsaquery failed: %s\n",
80 nt_errstr(result));
81 return result;
84 rpccli_lsa_close(lsa_pipe, mem_ctx, &pol);
85 cli_rpc_pipe_close(lsa_pipe);
87 return NT_STATUS_OK;
90 /**
91 * Run a single RPC command, from start to finish.
93 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
94 * @param conn_flag a NET_FLAG_ combination. Passed to
95 * net_make_ipc_connection.
96 * @param argc Standard main() style argc
97 * @param argc Standard main() style argv. Initial components are already
98 * stripped
99 * @return A shell status integer (0 for success)
102 int run_rpc_command(struct cli_state *cli_arg,
103 const int pipe_idx,
104 int conn_flags,
105 rpc_command_fn fn,
106 int argc,
107 const char **argv)
109 struct cli_state *cli = NULL;
110 struct rpc_pipe_client *pipe_hnd = NULL;
111 TALLOC_CTX *mem_ctx;
112 NTSTATUS nt_status;
113 DOM_SID *domain_sid;
114 char *domain_name;
116 /* make use of cli_state handed over as an argument, if possible */
117 if (!cli_arg) {
118 cli = net_make_ipc_connection(conn_flags);
119 } else {
120 cli = cli_arg;
123 if (!cli) {
124 return -1;
127 /* Create mem_ctx */
129 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
130 DEBUG(0, ("talloc_init() failed\n"));
131 cli_shutdown(cli);
132 return -1;
135 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
136 &domain_name);
137 if (!NT_STATUS_IS_OK(nt_status)) {
138 cli_shutdown(cli);
139 return -1;
142 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
143 if (lp_client_schannel() && (pipe_idx == PI_NETLOGON)) {
144 /* Always try and create an schannel netlogon pipe. */
145 pipe_hnd = cli_rpc_pipe_open_schannel(cli, pipe_idx,
146 PIPE_AUTH_LEVEL_PRIVACY,
147 domain_name,
148 &nt_status);
149 if (!pipe_hnd) {
150 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
151 nt_errstr(nt_status) ));
152 cli_shutdown(cli);
153 return -1;
155 } else {
156 pipe_hnd = cli_rpc_pipe_open_noauth(cli, pipe_idx, &nt_status);
157 if (!pipe_hnd) {
158 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
159 cli_get_pipe_name(pipe_idx),
160 nt_errstr(nt_status) ));
161 cli_shutdown(cli);
162 return -1;
167 nt_status = fn(domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
169 if (!NT_STATUS_IS_OK(nt_status)) {
170 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
171 } else {
172 DEBUG(5, ("rpc command function succedded\n"));
175 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
176 if (pipe_hnd) {
177 cli_rpc_pipe_close(pipe_hnd);
181 /* close the connection only if it was opened here */
182 if (!cli_arg) {
183 cli_shutdown(cli);
186 talloc_destroy(mem_ctx);
187 return (!NT_STATUS_IS_OK(nt_status));
190 /**
191 * Force a change of the trust acccount password.
193 * All parameters are provided by the run_rpc_command function, except for
194 * argc, argv which are passes through.
196 * @param domain_sid The domain sid aquired from the remote server
197 * @param cli A cli_state connected to the server.
198 * @param mem_ctx Talloc context, destoyed on compleation of the function.
199 * @param argc Standard main() style argc
200 * @param argc Standard main() style argv. Initial components are already
201 * stripped
203 * @return Normal NTSTATUS return.
206 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid,
207 const char *domain_name,
208 struct cli_state *cli,
209 struct rpc_pipe_client *pipe_hnd,
210 TALLOC_CTX *mem_ctx,
211 int argc,
212 const char **argv)
215 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup);
218 /**
219 * Force a change of the trust acccount password.
221 * @param argc Standard main() style argc
222 * @param argc Standard main() style argv. Initial components are already
223 * stripped
225 * @return A shell status integer (0 for success)
228 int net_rpc_changetrustpw(int argc, const char **argv)
230 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
231 rpc_changetrustpw_internals,
232 argc, argv);
235 /**
236 * Join a domain, the old way.
238 * This uses 'machinename' as the inital password, and changes it.
240 * The password should be created with 'server manager' or equiv first.
242 * All parameters are provided by the run_rpc_command function, except for
243 * argc, argv which are passes through.
245 * @param domain_sid The domain sid aquired from the remote server
246 * @param cli A cli_state connected to the server.
247 * @param mem_ctx Talloc context, destoyed on compleation of the function.
248 * @param argc Standard main() style argc
249 * @param argc Standard main() style argv. Initial components are already
250 * stripped
252 * @return Normal NTSTATUS return.
255 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid,
256 const char *domain_name,
257 struct cli_state *cli,
258 struct rpc_pipe_client *pipe_hnd,
259 TALLOC_CTX *mem_ctx,
260 int argc,
261 const char **argv)
264 fstring trust_passwd;
265 unsigned char orig_trust_passwd_hash[16];
266 NTSTATUS result;
267 uint32 sec_channel_type;
269 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &result);
270 if (!pipe_hnd) {
271 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
272 "error was %s\n",
273 cli->desthost,
274 nt_errstr(result) ));
275 return result;
279 check what type of join - if the user want's to join as
280 a BDC, the server must agree that we are a BDC.
282 if (argc >= 0) {
283 sec_channel_type = get_sec_channel_type(argv[0]);
284 } else {
285 sec_channel_type = get_sec_channel_type(NULL);
288 fstrcpy(trust_passwd, global_myname());
289 strlower_m(trust_passwd);
292 * Machine names can be 15 characters, but the max length on
293 * a password is 14. --jerry
296 trust_passwd[14] = '\0';
298 E_md4hash(trust_passwd, orig_trust_passwd_hash);
300 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, opt_target_workgroup,
301 orig_trust_passwd_hash,
302 sec_channel_type);
304 if (NT_STATUS_IS_OK(result))
305 printf("Joined domain %s.\n",opt_target_workgroup);
308 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
309 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
310 result = NT_STATUS_UNSUCCESSFUL;
313 return result;
316 /**
317 * Join a domain, the old way.
319 * @param argc Standard main() style argc
320 * @param argc Standard main() style argv. Initial components are already
321 * stripped
323 * @return A shell status integer (0 for success)
326 static int net_rpc_perform_oldjoin(int argc, const char **argv)
328 return run_rpc_command(NULL, PI_NETLOGON,
329 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
330 rpc_oldjoin_internals,
331 argc, argv);
334 /**
335 * Join a domain, the old way. This function exists to allow
336 * the message to be displayed when oldjoin was explicitly
337 * requested, but not when it was implied by "net rpc join"
339 * @param argc Standard main() style argc
340 * @param argc Standard main() style argv. Initial components are already
341 * stripped
343 * @return A shell status integer (0 for success)
346 static int net_rpc_oldjoin(int argc, const char **argv)
348 int rc = net_rpc_perform_oldjoin(argc, argv);
350 if (rc) {
351 d_fprintf(stderr, "Failed to join domain\n");
354 return rc;
357 /**
358 * Basic usage function for 'net rpc join'
359 * @param argc Standard main() style argc
360 * @param argc Standard main() style argv. Initial components are already
361 * stripped
364 static int rpc_join_usage(int argc, const char **argv)
366 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
367 "\t to join a domain with admin username & password\n"\
368 "\t\t password will be prompted if needed and none is specified\n"\
369 "\t <type> can be (default MEMBER)\n"\
370 "\t\t BDC - Join as a BDC\n"\
371 "\t\t PDC - Join as a PDC\n"\
372 "\t\t MEMBER - Join as a MEMBER server\n");
374 net_common_flags_usage(argc, argv);
375 return -1;
378 /**
379 * 'net rpc join' entrypoint.
380 * @param argc Standard main() style argc
381 * @param argc Standard main() style argv. Initial components are already
382 * stripped
384 * Main 'net_rpc_join()' (where the admain username/password is used) is
385 * in net_rpc_join.c
386 * Try to just change the password, but if that doesn't work, use/prompt
387 * for a username/password.
390 int net_rpc_join(int argc, const char **argv)
392 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
393 return 0;
395 return net_rpc_join_newstyle(argc, argv);
398 /**
399 * display info about a rpc domain
401 * All parameters are provided by the run_rpc_command function, except for
402 * argc, argv which are passed through.
404 * @param domain_sid The domain sid acquired from the remote server
405 * @param cli A cli_state connected to the server.
406 * @param mem_ctx Talloc context, destoyed on completion of the function.
407 * @param argc Standard main() style argc
408 * @param argv Standard main() style argv. Initial components are already
409 * stripped
411 * @return Normal NTSTATUS return.
414 NTSTATUS rpc_info_internals(const DOM_SID *domain_sid,
415 const char *domain_name,
416 struct cli_state *cli,
417 struct rpc_pipe_client *pipe_hnd,
418 TALLOC_CTX *mem_ctx,
419 int argc,
420 const char **argv)
422 POLICY_HND connect_pol, domain_pol;
423 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
424 SAM_UNK_CTR ctr;
425 fstring sid_str;
427 sid_to_string(sid_str, domain_sid);
429 /* Get sam policy handle */
430 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
431 &connect_pol);
432 if (!NT_STATUS_IS_OK(result)) {
433 goto done;
436 /* Get domain policy handle */
437 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
438 MAXIMUM_ALLOWED_ACCESS,
439 domain_sid, &domain_pol);
440 if (!NT_STATUS_IS_OK(result)) {
441 goto done;
444 ZERO_STRUCT(ctr);
445 result = rpccli_samr_query_dom_info(pipe_hnd, mem_ctx, &domain_pol,
446 2, &ctr);
447 if (NT_STATUS_IS_OK(result)) {
448 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
449 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
450 d_printf("Domain SID: %s\n", sid_str);
451 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num.low);
452 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
453 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
454 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
455 talloc_destroy(ctx);
458 done:
459 return result;
462 /**
463 * 'net rpc info' entrypoint.
464 * @param argc Standard main() style argc
465 * @param argc Standard main() style argv. Initial components are already
466 * stripped
469 int net_rpc_info(int argc, const char **argv)
471 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
472 rpc_info_internals,
473 argc, argv);
476 /**
477 * Fetch domain SID into the local secrets.tdb
479 * All parameters are provided by the run_rpc_command function, except for
480 * argc, argv which are passes through.
482 * @param domain_sid The domain sid acquired from the remote server
483 * @param cli A cli_state connected to the server.
484 * @param mem_ctx Talloc context, destoyed on completion of the function.
485 * @param argc Standard main() style argc
486 * @param argv Standard main() style argv. Initial components are already
487 * stripped
489 * @return Normal NTSTATUS return.
492 static NTSTATUS rpc_getsid_internals(const DOM_SID *domain_sid,
493 const char *domain_name,
494 struct cli_state *cli,
495 struct rpc_pipe_client *pipe_hnd,
496 TALLOC_CTX *mem_ctx,
497 int argc,
498 const char **argv)
500 fstring sid_str;
502 sid_to_string(sid_str, domain_sid);
503 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
504 sid_str, domain_name);
506 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
507 DEBUG(0,("Can't store domain SID\n"));
508 return NT_STATUS_UNSUCCESSFUL;
511 return NT_STATUS_OK;
514 /**
515 * 'net rpc getsid' entrypoint.
516 * @param argc Standard main() style argc
517 * @param argc Standard main() style argv. Initial components are already
518 * stripped
521 int net_rpc_getsid(int argc, const char **argv)
523 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
524 rpc_getsid_internals,
525 argc, argv);
528 /****************************************************************************/
531 * Basic usage function for 'net rpc user'
532 * @param argc Standard main() style argc.
533 * @param argv Standard main() style argv. Initial components are already
534 * stripped.
537 static int rpc_user_usage(int argc, const char **argv)
539 return net_help_user(argc, argv);
542 /**
543 * Add a new user to a remote RPC server
545 * All parameters are provided by the run_rpc_command function, except for
546 * argc, argv which are passes through.
548 * @param domain_sid The domain sid acquired from the remote server
549 * @param cli A cli_state connected to the server.
550 * @param mem_ctx Talloc context, destoyed on completion of the function.
551 * @param argc Standard main() style argc
552 * @param argv Standard main() style argv. Initial components are already
553 * stripped
555 * @return Normal NTSTATUS return.
558 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid,
559 const char *domain_name,
560 struct cli_state *cli,
561 struct rpc_pipe_client *pipe_hnd,
562 TALLOC_CTX *mem_ctx,
563 int argc, const char **argv)
566 POLICY_HND connect_pol, domain_pol, user_pol;
567 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
568 const char *acct_name;
569 uint32 acb_info;
570 uint32 unknown, user_rid;
572 if (argc != 1) {
573 d_printf("User must be specified\n");
574 rpc_user_usage(argc, argv);
575 return NT_STATUS_OK;
578 acct_name = argv[0];
580 /* Get sam policy handle */
582 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
583 &connect_pol);
584 if (!NT_STATUS_IS_OK(result)) {
585 goto done;
588 /* Get domain policy handle */
590 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
591 MAXIMUM_ALLOWED_ACCESS,
592 domain_sid, &domain_pol);
593 if (!NT_STATUS_IS_OK(result)) {
594 goto done;
597 /* Create domain user */
599 acb_info = ACB_NORMAL;
600 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
602 result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
603 acct_name, acb_info, unknown,
604 &user_pol, &user_rid);
605 if (!NT_STATUS_IS_OK(result)) {
606 goto done;
609 done:
610 if (!NT_STATUS_IS_OK(result)) {
611 d_fprintf(stderr, "Failed to add user %s - %s\n", acct_name,
612 nt_errstr(result));
613 } else {
614 d_printf("Added user %s\n", acct_name);
616 return result;
619 /**
620 * Add a new user to a remote RPC server
622 * @param argc Standard main() style argc
623 * @param argv Standard main() style argv. Initial components are already
624 * stripped
626 * @return A shell status integer (0 for success)
629 static int rpc_user_add(int argc, const char **argv)
631 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
632 argc, argv);
635 /**
636 * Delete a user from a remote RPC server
638 * All parameters are provided by the run_rpc_command function, except for
639 * argc, argv which are passes through.
641 * @param domain_sid The domain sid acquired from the remote server
642 * @param cli A cli_state connected to the server.
643 * @param mem_ctx Talloc context, destoyed on completion of the function.
644 * @param argc Standard main() style argc
645 * @param argv Standard main() style argv. Initial components are already
646 * stripped
648 * @return Normal NTSTATUS return.
651 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
652 const char *domain_name,
653 struct cli_state *cli,
654 struct rpc_pipe_client *pipe_hnd,
655 TALLOC_CTX *mem_ctx,
656 int argc,
657 const char **argv)
659 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
660 POLICY_HND connect_pol, domain_pol, user_pol;
662 if (argc < 1) {
663 d_printf("User must be specified\n");
664 rpc_user_usage(argc, argv);
665 return NT_STATUS_OK;
667 /* Get sam policy and domain handles */
669 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
670 &connect_pol);
672 if (!NT_STATUS_IS_OK(result)) {
673 goto done;
676 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
677 MAXIMUM_ALLOWED_ACCESS,
678 domain_sid, &domain_pol);
680 if (!NT_STATUS_IS_OK(result)) {
681 goto done;
684 /* Get handle on user */
687 uint32 *user_rids, num_rids, *name_types;
688 uint32 flags = 0x000003e8; /* Unknown */
690 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
691 flags, 1, &argv[0],
692 &num_rids, &user_rids,
693 &name_types);
695 if (!NT_STATUS_IS_OK(result)) {
696 goto done;
699 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
700 MAXIMUM_ALLOWED_ACCESS,
701 user_rids[0], &user_pol);
703 if (!NT_STATUS_IS_OK(result)) {
704 goto done;
708 /* Delete user */
710 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
712 if (!NT_STATUS_IS_OK(result)) {
713 goto done;
716 /* Display results */
717 if (!NT_STATUS_IS_OK(result)) {
718 d_fprintf(stderr, "Failed to delete user account - %s\n", nt_errstr(result));
719 } else {
720 d_printf("Deleted user account\n");
723 done:
724 return result;
727 /**
728 * Rename a user on a remote RPC server
730 * All parameters are provided by the run_rpc_command function, except for
731 * argc, argv which are passes through.
733 * @param domain_sid The domain sid acquired from the remote server
734 * @param cli A cli_state connected to the server.
735 * @param mem_ctx Talloc context, destoyed on completion of the function.
736 * @param argc Standard main() style argc
737 * @param argv Standard main() style argv. Initial components are already
738 * stripped
740 * @return Normal NTSTATUS return.
743 static NTSTATUS rpc_user_rename_internals(const DOM_SID *domain_sid,
744 const char *domain_name,
745 struct cli_state *cli,
746 struct rpc_pipe_client *pipe_hnd,
747 TALLOC_CTX *mem_ctx,
748 int argc,
749 const char **argv)
751 POLICY_HND connect_pol, domain_pol, user_pol;
752 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
753 uint32 info_level = 7;
754 const char *old_name, *new_name;
755 uint32 *user_rid;
756 uint32 flags = 0x000003e8; /* Unknown */
757 uint32 num_rids, *name_types;
758 uint32 num_names = 1;
759 const char **names;
760 SAM_USERINFO_CTR *user_ctr;
761 SAM_USERINFO_CTR ctr;
762 SAM_USER_INFO_7 info7;
764 if (argc != 2) {
765 d_printf("Old and new username must be specified\n");
766 rpc_user_usage(argc, argv);
767 return NT_STATUS_OK;
770 old_name = argv[0];
771 new_name = argv[1];
773 ZERO_STRUCT(ctr);
774 ZERO_STRUCT(user_ctr);
776 /* Get sam policy handle */
778 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
779 &connect_pol);
780 if (!NT_STATUS_IS_OK(result)) {
781 goto done;
784 /* Get domain policy handle */
786 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
787 MAXIMUM_ALLOWED_ACCESS,
788 domain_sid, &domain_pol);
789 if (!NT_STATUS_IS_OK(result)) {
790 goto done;
793 names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
794 names[0] = old_name;
795 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
796 flags, num_names, names,
797 &num_rids, &user_rid, &name_types);
798 if (!NT_STATUS_IS_OK(result)) {
799 goto done;
802 /* Open domain user */
803 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
804 MAXIMUM_ALLOWED_ACCESS, user_rid[0], &user_pol);
806 if (!NT_STATUS_IS_OK(result)) {
807 goto done;
810 /* Query user info */
811 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
812 info_level, &user_ctr);
814 if (!NT_STATUS_IS_OK(result)) {
815 goto done;
818 ctr.switch_value = info_level;
819 ctr.info.id7 = &info7;
821 init_sam_user_info7(&info7, new_name);
823 /* Set new name */
824 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol,
825 info_level, &cli->user_session_key, &ctr);
827 if (!NT_STATUS_IS_OK(result)) {
828 goto done;
831 done:
832 if (!NT_STATUS_IS_OK(result)) {
833 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
834 nt_errstr(result));
835 } else {
836 d_printf("Renamed user from %s to %s\n", old_name, new_name);
838 return result;
841 /**
842 * Rename a user on a remote RPC server
844 * @param argc Standard main() style argc
845 * @param argv Standard main() style argv. Initial components are already
846 * stripped
848 * @return A shell status integer (0 for success)
851 static int rpc_user_rename(int argc, const char **argv)
853 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
854 argc, argv);
857 /**
858 * Delete a user from a remote RPC server
860 * @param argc Standard main() style argc
861 * @param argv Standard main() style argv. Initial components are already
862 * stripped
864 * @return A shell status integer (0 for success)
867 static int rpc_user_delete(int argc, const char **argv)
869 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
870 argc, argv);
873 /**
874 * Set a password for a user on a remote RPC server
876 * All parameters are provided by the run_rpc_command function, except for
877 * argc, argv which are passes through.
879 * @param domain_sid The domain sid acquired from the remote server
880 * @param cli A cli_state connected to the server.
881 * @param mem_ctx Talloc context, destoyed on completion of the function.
882 * @param argc Standard main() style argc
883 * @param argv Standard main() style argv. Initial components are already
884 * stripped
886 * @return Normal NTSTATUS return.
889 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
890 const char *domain_name,
891 struct cli_state *cli,
892 struct rpc_pipe_client *pipe_hnd,
893 TALLOC_CTX *mem_ctx,
894 int argc,
895 const char **argv)
897 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
898 POLICY_HND connect_pol, domain_pol, user_pol;
899 SAM_USERINFO_CTR ctr;
900 SAM_USER_INFO_24 p24;
901 uchar pwbuf[516];
902 const char *user;
903 const char *new_password;
904 char *prompt = NULL;
906 if (argc < 1) {
907 d_printf("User must be specified\n");
908 rpc_user_usage(argc, argv);
909 return NT_STATUS_OK;
912 user = argv[0];
914 if (argv[1]) {
915 new_password = argv[1];
916 } else {
917 asprintf(&prompt, "Enter new password for %s:", user);
918 new_password = getpass(prompt);
919 SAFE_FREE(prompt);
922 /* Get sam policy and domain handles */
924 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
925 &connect_pol);
927 if (!NT_STATUS_IS_OK(result)) {
928 goto done;
931 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
932 MAXIMUM_ALLOWED_ACCESS,
933 domain_sid, &domain_pol);
935 if (!NT_STATUS_IS_OK(result)) {
936 goto done;
939 /* Get handle on user */
942 uint32 *user_rids, num_rids, *name_types;
943 uint32 flags = 0x000003e8; /* Unknown */
945 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
946 flags, 1, &user,
947 &num_rids, &user_rids,
948 &name_types);
950 if (!NT_STATUS_IS_OK(result)) {
951 goto done;
954 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
955 MAXIMUM_ALLOWED_ACCESS,
956 user_rids[0], &user_pol);
958 if (!NT_STATUS_IS_OK(result)) {
959 goto done;
963 /* Set password on account */
965 ZERO_STRUCT(ctr);
966 ZERO_STRUCT(p24);
968 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
970 init_sam_user_info24(&p24, (char *)pwbuf,24);
972 ctr.switch_value = 24;
973 ctr.info.id24 = &p24;
975 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 24,
976 &cli->user_session_key, &ctr);
978 if (!NT_STATUS_IS_OK(result)) {
979 goto done;
982 /* Display results */
984 done:
985 return result;
989 /**
990 * Set a user's password on a remote RPC server
992 * @param argc Standard main() style argc
993 * @param argv Standard main() style argv. Initial components are already
994 * stripped
996 * @return A shell status integer (0 for success)
999 static int rpc_user_password(int argc, const char **argv)
1001 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
1002 argc, argv);
1005 /**
1006 * List user's groups on a remote RPC server
1008 * All parameters are provided by the run_rpc_command function, except for
1009 * argc, argv which are passes through.
1011 * @param domain_sid The domain sid acquired from the remote server
1012 * @param cli A cli_state connected to the server.
1013 * @param mem_ctx Talloc context, destoyed on completion of the function.
1014 * @param argc Standard main() style argc
1015 * @param argv Standard main() style argv. Initial components are already
1016 * stripped
1018 * @return Normal NTSTATUS return.
1021 static NTSTATUS rpc_user_info_internals(const DOM_SID *domain_sid,
1022 const char *domain_name,
1023 struct cli_state *cli,
1024 struct rpc_pipe_client *pipe_hnd,
1025 TALLOC_CTX *mem_ctx,
1026 int argc,
1027 const char **argv)
1029 POLICY_HND connect_pol, domain_pol, user_pol;
1030 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1031 uint32 *rids, num_rids, *name_types, num_names;
1032 uint32 flags = 0x000003e8; /* Unknown */
1033 int i;
1034 char **names;
1035 DOM_GID *user_gids;
1037 if (argc < 1) {
1038 d_printf("User must be specified\n");
1039 rpc_user_usage(argc, argv);
1040 return NT_STATUS_OK;
1042 /* Get sam policy handle */
1044 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1045 &connect_pol);
1046 if (!NT_STATUS_IS_OK(result)) goto done;
1048 /* Get domain policy handle */
1050 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1051 MAXIMUM_ALLOWED_ACCESS,
1052 domain_sid, &domain_pol);
1053 if (!NT_STATUS_IS_OK(result)) goto done;
1055 /* Get handle on user */
1057 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1058 flags, 1, &argv[0],
1059 &num_rids, &rids, &name_types);
1061 if (!NT_STATUS_IS_OK(result)) goto done;
1063 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1064 MAXIMUM_ALLOWED_ACCESS,
1065 rids[0], &user_pol);
1066 if (!NT_STATUS_IS_OK(result)) goto done;
1068 result = rpccli_samr_query_usergroups(pipe_hnd, mem_ctx, &user_pol,
1069 &num_rids, &user_gids);
1071 if (!NT_STATUS_IS_OK(result)) goto done;
1073 /* Look up rids */
1075 if (num_rids) {
1076 rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids);
1078 for (i = 0; i < num_rids; i++)
1079 rids[i] = user_gids[i].g_rid;
1081 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, &domain_pol,
1082 num_rids, rids,
1083 &num_names, &names, &name_types);
1085 if (!NT_STATUS_IS_OK(result)) {
1086 goto done;
1089 /* Display results */
1091 for (i = 0; i < num_names; i++)
1092 printf("%s\n", names[i]);
1094 done:
1095 return result;
1098 /**
1099 * List a user's groups from a remote RPC server
1101 * @param argc Standard main() style argc
1102 * @param argv Standard main() style argv. Initial components are already
1103 * stripped
1105 * @return A shell status integer (0 for success)
1108 static int rpc_user_info(int argc, const char **argv)
1110 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1111 argc, argv);
1114 /**
1115 * List users on a remote RPC server
1117 * All parameters are provided by the run_rpc_command function, except for
1118 * argc, argv which are passes through.
1120 * @param domain_sid The domain sid acquired from the remote server
1121 * @param cli A cli_state connected to the server.
1122 * @param mem_ctx Talloc context, destoyed on completion of the function.
1123 * @param argc Standard main() style argc
1124 * @param argv Standard main() style argv. Initial components are already
1125 * stripped
1127 * @return Normal NTSTATUS return.
1130 static NTSTATUS rpc_user_list_internals(const DOM_SID *domain_sid,
1131 const char *domain_name,
1132 struct cli_state *cli,
1133 struct rpc_pipe_client *pipe_hnd,
1134 TALLOC_CTX *mem_ctx,
1135 int argc,
1136 const char **argv)
1138 POLICY_HND connect_pol, domain_pol;
1139 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1140 uint32 start_idx=0, num_entries, i, loop_count = 0;
1141 SAM_DISPINFO_CTR ctr;
1142 SAM_DISPINFO_1 info1;
1144 /* Get sam policy handle */
1146 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1147 &connect_pol);
1148 if (!NT_STATUS_IS_OK(result)) {
1149 goto done;
1152 /* Get domain policy handle */
1154 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1155 MAXIMUM_ALLOWED_ACCESS,
1156 domain_sid, &domain_pol);
1157 if (!NT_STATUS_IS_OK(result)) {
1158 goto done;
1161 /* Query domain users */
1162 ZERO_STRUCT(ctr);
1163 ZERO_STRUCT(info1);
1164 ctr.sam.info1 = &info1;
1165 if (opt_long_list_entries)
1166 d_printf("\nUser name Comment"\
1167 "\n-----------------------------\n");
1168 do {
1169 fstring user, desc;
1170 uint32 max_entries, max_size;
1172 get_query_dispinfo_params(
1173 loop_count, &max_entries, &max_size);
1175 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
1176 &start_idx, 1, &num_entries,
1177 max_entries, max_size, &ctr);
1178 loop_count++;
1180 for (i = 0; i < num_entries; i++) {
1181 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
1182 if (opt_long_list_entries)
1183 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
1185 if (opt_long_list_entries)
1186 printf("%-21.21s %s\n", user, desc);
1187 else
1188 printf("%s\n", user);
1190 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1192 done:
1193 return result;
1196 /**
1197 * 'net rpc user' entrypoint.
1198 * @param argc Standard main() style argc
1199 * @param argc Standard main() style argv. Initial components are already
1200 * stripped
1203 int net_rpc_user(int argc, const char **argv)
1205 struct functable func[] = {
1206 {"add", rpc_user_add},
1207 {"info", rpc_user_info},
1208 {"delete", rpc_user_delete},
1209 {"password", rpc_user_password},
1210 {"rename", rpc_user_rename},
1211 {NULL, NULL}
1214 if (argc == 0) {
1215 return run_rpc_command(NULL,PI_SAMR, 0,
1216 rpc_user_list_internals,
1217 argc, argv);
1220 return net_run_function(argc, argv, func, rpc_user_usage);
1223 static NTSTATUS rpc_sh_user_list(TALLOC_CTX *mem_ctx,
1224 struct rpc_sh_ctx *ctx,
1225 struct rpc_pipe_client *pipe_hnd,
1226 int argc, const char **argv)
1228 return rpc_user_list_internals(ctx->domain_sid, ctx->domain_name,
1229 ctx->cli, pipe_hnd, mem_ctx,
1230 argc, argv);
1233 static NTSTATUS rpc_sh_user_info(TALLOC_CTX *mem_ctx,
1234 struct rpc_sh_ctx *ctx,
1235 struct rpc_pipe_client *pipe_hnd,
1236 int argc, const char **argv)
1238 return rpc_user_info_internals(ctx->domain_sid, ctx->domain_name,
1239 ctx->cli, pipe_hnd, mem_ctx,
1240 argc, argv);
1243 static NTSTATUS rpc_sh_handle_user(TALLOC_CTX *mem_ctx,
1244 struct rpc_sh_ctx *ctx,
1245 struct rpc_pipe_client *pipe_hnd,
1246 int argc, const char **argv,
1247 NTSTATUS (*fn)(
1248 TALLOC_CTX *mem_ctx,
1249 struct rpc_sh_ctx *ctx,
1250 struct rpc_pipe_client *pipe_hnd,
1251 const POLICY_HND *user_hnd,
1252 int argc, const char **argv))
1255 POLICY_HND connect_pol, domain_pol, user_pol;
1256 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1257 DOM_SID sid;
1258 uint32 rid;
1259 enum SID_NAME_USE type;
1261 if (argc == 0) {
1262 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1263 return NT_STATUS_INVALID_PARAMETER;
1266 ZERO_STRUCT(connect_pol);
1267 ZERO_STRUCT(domain_pol);
1268 ZERO_STRUCT(user_pol);
1270 result = net_rpc_lookup_name(mem_ctx, pipe_hnd->cli, argv[0],
1271 NULL, NULL, &sid, &type);
1272 if (!NT_STATUS_IS_OK(result)) {
1273 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1274 nt_errstr(result));
1275 goto done;
1278 if (type != SID_NAME_USER) {
1279 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1280 sid_type_lookup(type));
1281 result = NT_STATUS_NO_SUCH_USER;
1282 goto done;
1285 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1286 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1287 result = NT_STATUS_NO_SUCH_USER;
1288 goto done;
1291 result = rpccli_samr_connect(pipe_hnd, mem_ctx,
1292 MAXIMUM_ALLOWED_ACCESS, &connect_pol);
1293 if (!NT_STATUS_IS_OK(result)) {
1294 goto done;
1297 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1298 MAXIMUM_ALLOWED_ACCESS,
1299 ctx->domain_sid, &domain_pol);
1300 if (!NT_STATUS_IS_OK(result)) {
1301 goto done;
1304 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1305 MAXIMUM_ALLOWED_ACCESS,
1306 rid, &user_pol);
1307 if (!NT_STATUS_IS_OK(result)) {
1308 goto done;
1311 result = fn(mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1313 done:
1314 if (is_valid_policy_hnd(&user_pol)) {
1315 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1317 if (is_valid_policy_hnd(&domain_pol)) {
1318 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
1320 if (is_valid_policy_hnd(&connect_pol)) {
1321 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
1323 return result;
1326 static NTSTATUS rpc_sh_user_show_internals(TALLOC_CTX *mem_ctx,
1327 struct rpc_sh_ctx *ctx,
1328 struct rpc_pipe_client *pipe_hnd,
1329 const POLICY_HND *user_hnd,
1330 int argc, const char **argv)
1332 NTSTATUS result;
1333 SAM_USERINFO_CTR *ctr;
1334 SAM_USER_INFO_21 *info;
1336 if (argc != 0) {
1337 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1338 return NT_STATUS_INVALID_PARAMETER;
1341 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1342 21, &ctr);
1343 if (!NT_STATUS_IS_OK(result)) {
1344 return result;
1347 info = ctr->info.id21;
1349 d_printf("user rid: %d, group rid: %d\n", info->user_rid,
1350 info->group_rid);
1352 return result;
1355 static NTSTATUS rpc_sh_user_show(TALLOC_CTX *mem_ctx,
1356 struct rpc_sh_ctx *ctx,
1357 struct rpc_pipe_client *pipe_hnd,
1358 int argc, const char **argv)
1360 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1361 rpc_sh_user_show_internals);
1364 #define FETCHSTR(name, rec) \
1365 do { if (strequal(ctx->thiscmd, name)) { \
1366 oldval = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_##rec); } \
1367 } while (0);
1369 #define SETSTR(name, rec, flag) \
1370 do { if (strequal(ctx->thiscmd, name)) { \
1371 init_unistr2(&usr->uni_##rec, argv[0], STR_TERMINATE); \
1372 init_uni_hdr(&usr->hdr_##rec, &usr->uni_##rec); \
1373 usr->fields_present |= ACCT_##flag; } \
1374 } while (0);
1376 static NTSTATUS rpc_sh_user_str_edit_internals(TALLOC_CTX *mem_ctx,
1377 struct rpc_sh_ctx *ctx,
1378 struct rpc_pipe_client *pipe_hnd,
1379 const POLICY_HND *user_hnd,
1380 int argc, const char **argv)
1382 NTSTATUS result;
1383 SAM_USERINFO_CTR *ctr;
1384 SAM_USER_INFO_21 *usr;
1385 const char *username;
1386 const char *oldval = "";
1388 if (argc > 1) {
1389 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1390 ctx->whoami);
1391 return NT_STATUS_INVALID_PARAMETER;
1394 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1395 21, &ctr);
1396 if (!NT_STATUS_IS_OK(result)) {
1397 return result;
1400 usr = ctr->info.id21;
1402 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1404 FETCHSTR("fullname", full_name);
1405 FETCHSTR("homedir", home_dir);
1406 FETCHSTR("homedrive", dir_drive);
1407 FETCHSTR("logonscript", logon_script);
1408 FETCHSTR("profilepath", profile_path);
1409 FETCHSTR("description", acct_desc);
1411 if (argc == 0) {
1412 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1413 goto done;
1416 ZERO_STRUCTP(usr);
1418 if (strcmp(argv[0], "NULL") == 0) {
1419 argv[0] = "";
1422 SETSTR("fullname", full_name, FULL_NAME);
1423 SETSTR("homedir", home_dir, HOME_DIR);
1424 SETSTR("homedrive", dir_drive, HOME_DRIVE);
1425 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1426 SETSTR("profilepath", profile_path, PROFILE);
1427 SETSTR("description", acct_desc, DESCRIPTION);
1429 result = rpccli_samr_set_userinfo2(
1430 pipe_hnd, mem_ctx, user_hnd, 21,
1431 &pipe_hnd->cli->user_session_key, ctr);
1433 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1434 ctx->thiscmd, oldval, argv[0]);
1436 done:
1438 return result;
1441 #define HANDLEFLG(name, rec) \
1442 do { if (strequal(ctx->thiscmd, name)) { \
1443 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1444 if (newval) { \
1445 newflags = oldflags | ACB_##rec; \
1446 } else { \
1447 newflags = oldflags & ~ACB_##rec; \
1448 } } } while (0);
1450 static NTSTATUS rpc_sh_user_str_edit(TALLOC_CTX *mem_ctx,
1451 struct rpc_sh_ctx *ctx,
1452 struct rpc_pipe_client *pipe_hnd,
1453 int argc, const char **argv)
1455 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1456 rpc_sh_user_str_edit_internals);
1459 static NTSTATUS rpc_sh_user_flag_edit_internals(TALLOC_CTX *mem_ctx,
1460 struct rpc_sh_ctx *ctx,
1461 struct rpc_pipe_client *pipe_hnd,
1462 const POLICY_HND *user_hnd,
1463 int argc, const char **argv)
1465 NTSTATUS result;
1466 SAM_USERINFO_CTR *ctr;
1467 SAM_USER_INFO_21 *usr;
1468 const char *username;
1469 const char *oldval = "unknown";
1470 uint32 oldflags, newflags;
1471 BOOL newval;
1473 if ((argc > 1) ||
1474 ((argc == 1) && !strequal(argv[0], "yes") &&
1475 !strequal(argv[0], "no"))) {
1476 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1477 ctx->whoami);
1478 return NT_STATUS_INVALID_PARAMETER;
1481 newval = strequal(argv[0], "yes");
1483 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, user_hnd,
1484 21, &ctr);
1485 if (!NT_STATUS_IS_OK(result)) {
1486 return result;
1489 usr = ctr->info.id21;
1491 username = rpcstr_pull_unistr2_talloc(mem_ctx, &usr->uni_user_name);
1492 oldflags = usr->acb_info;
1493 newflags = usr->acb_info;
1495 HANDLEFLG("disabled", DISABLED);
1496 HANDLEFLG("pwnotreq", PWNOTREQ);
1497 HANDLEFLG("autolock", AUTOLOCK);
1498 HANDLEFLG("pwnoexp", PWNOEXP);
1500 if (argc == 0) {
1501 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1502 goto done;
1505 ZERO_STRUCTP(usr);
1507 usr->acb_info = newflags;
1508 usr->fields_present = ACCT_FLAGS;
1510 result = rpccli_samr_set_userinfo2(
1511 pipe_hnd, mem_ctx, user_hnd, 21,
1512 &pipe_hnd->cli->user_session_key, ctr);
1514 if (NT_STATUS_IS_OK(result)) {
1515 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1516 ctx->thiscmd, oldval, argv[0]);
1519 done:
1521 return result;
1524 static NTSTATUS rpc_sh_user_flag_edit(TALLOC_CTX *mem_ctx,
1525 struct rpc_sh_ctx *ctx,
1526 struct rpc_pipe_client *pipe_hnd,
1527 int argc, const char **argv)
1529 return rpc_sh_handle_user(mem_ctx, ctx, pipe_hnd, argc, argv,
1530 rpc_sh_user_flag_edit_internals);
1533 struct rpc_sh_cmd *net_rpc_user_edit_cmds(TALLOC_CTX *mem_ctx,
1534 struct rpc_sh_ctx *ctx)
1536 static struct rpc_sh_cmd cmds[] = {
1538 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1539 "Show/Set a user's full name" },
1541 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1542 "Show/Set a user's home directory" },
1544 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1545 "Show/Set a user's home drive" },
1547 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1548 "Show/Set a user's logon script" },
1550 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1551 "Show/Set a user's profile path" },
1553 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1554 "Show/Set a user's description" },
1556 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1557 "Show/Set whether a user is disabled" },
1559 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1560 "Show/Set whether a user locked out" },
1562 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1563 "Show/Set whether a user does not need a password" },
1565 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1566 "Show/Set whether a user's password does not expire" },
1568 { NULL, NULL, 0, NULL, NULL }
1571 return cmds;
1574 struct rpc_sh_cmd *net_rpc_user_cmds(TALLOC_CTX *mem_ctx,
1575 struct rpc_sh_ctx *ctx)
1577 static struct rpc_sh_cmd cmds[] = {
1579 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1580 "List available users" },
1582 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1583 "List the domain groups a user is member of" },
1585 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1586 "Show info about a user" },
1588 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1589 "Show/Modify a user's fields" },
1591 { NULL, NULL, 0, NULL, NULL }
1594 return cmds;
1597 /****************************************************************************/
1600 * Basic usage function for 'net rpc group'
1601 * @param argc Standard main() style argc.
1602 * @param argv Standard main() style argv. Initial components are already
1603 * stripped.
1606 static int rpc_group_usage(int argc, const char **argv)
1608 return net_help_group(argc, argv);
1612 * Delete group on a remote RPC server
1614 * All parameters are provided by the run_rpc_command function, except for
1615 * argc, argv which are passes through.
1617 * @param domain_sid The domain sid acquired from the remote server
1618 * @param cli A cli_state connected to the server.
1619 * @param mem_ctx Talloc context, destoyed on completion of the function.
1620 * @param argc Standard main() style argc
1621 * @param argv Standard main() style argv. Initial components are already
1622 * stripped
1624 * @return Normal NTSTATUS return.
1627 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1628 const char *domain_name,
1629 struct cli_state *cli,
1630 struct rpc_pipe_client *pipe_hnd,
1631 TALLOC_CTX *mem_ctx,
1632 int argc,
1633 const char **argv)
1635 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1636 BOOL group_is_primary = False;
1637 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1639 uint32 *group_rids, num_rids, *name_types, num_members,
1640 *group_attrs, group_rid;
1641 uint32 flags = 0x000003e8; /* Unknown */
1642 /* char **names; */
1643 int i;
1644 /* DOM_GID *user_gids; */
1645 SAM_USERINFO_CTR *user_ctr;
1646 fstring temp;
1648 if (argc < 1) {
1649 d_printf("specify group\n");
1650 rpc_group_usage(argc,argv);
1651 return NT_STATUS_OK; /* ok? */
1654 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1655 &connect_pol);
1657 if (!NT_STATUS_IS_OK(result)) {
1658 d_fprintf(stderr, "Request samr_connect failed\n");
1659 goto done;
1662 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1663 MAXIMUM_ALLOWED_ACCESS,
1664 domain_sid, &domain_pol);
1666 if (!NT_STATUS_IS_OK(result)) {
1667 d_fprintf(stderr, "Request open_domain failed\n");
1668 goto done;
1671 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol,
1672 flags, 1, &argv[0],
1673 &num_rids, &group_rids,
1674 &name_types);
1676 if (!NT_STATUS_IS_OK(result)) {
1677 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1678 goto done;
1681 switch (name_types[0])
1683 case SID_NAME_DOM_GRP:
1684 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
1685 MAXIMUM_ALLOWED_ACCESS,
1686 group_rids[0], &group_pol);
1687 if (!NT_STATUS_IS_OK(result)) {
1688 d_fprintf(stderr, "Request open_group failed");
1689 goto done;
1692 group_rid = group_rids[0];
1694 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
1695 &num_members, &group_rids,
1696 &group_attrs);
1698 if (!NT_STATUS_IS_OK(result)) {
1699 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1700 goto done;
1703 if (opt_verbose) {
1704 d_printf("Domain Group %s (rid: %d) has %d members\n",
1705 argv[0],group_rid,num_members);
1708 /* Check if group is anyone's primary group */
1709 for (i = 0; i < num_members; i++)
1711 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
1712 MAXIMUM_ALLOWED_ACCESS,
1713 group_rids[i], &user_pol);
1715 if (!NT_STATUS_IS_OK(result)) {
1716 d_fprintf(stderr, "Unable to open group member %d\n",group_rids[i]);
1717 goto done;
1720 ZERO_STRUCT(user_ctr);
1722 result = rpccli_samr_query_userinfo(pipe_hnd, mem_ctx, &user_pol,
1723 21, &user_ctr);
1725 if (!NT_STATUS_IS_OK(result)) {
1726 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",group_rids[i]);
1727 goto done;
1730 if (user_ctr->info.id21->group_rid == group_rid) {
1731 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1732 sizeof(temp)-1);
1733 if (opt_verbose)
1734 d_printf("Group is primary group of %s\n",temp);
1735 group_is_primary = True;
1738 rpccli_samr_close(pipe_hnd, mem_ctx, &user_pol);
1741 if (group_is_primary) {
1742 d_fprintf(stderr, "Unable to delete group because some "
1743 "of it's members have it as primary group\n");
1744 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1745 goto done;
1748 /* remove all group members */
1749 for (i = 0; i < num_members; i++)
1751 if (opt_verbose)
1752 d_printf("Remove group member %d...",group_rids[i]);
1753 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, group_rids[i]);
1755 if (NT_STATUS_IS_OK(result)) {
1756 if (opt_verbose)
1757 d_printf("ok\n");
1758 } else {
1759 if (opt_verbose)
1760 d_printf("failed\n");
1761 goto done;
1765 result = rpccli_samr_delete_dom_group(pipe_hnd, mem_ctx, &group_pol);
1767 break;
1768 /* removing a local group is easier... */
1769 case SID_NAME_ALIAS:
1770 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
1771 MAXIMUM_ALLOWED_ACCESS,
1772 group_rids[0], &group_pol);
1774 if (!NT_STATUS_IS_OK(result)) {
1775 d_fprintf(stderr, "Request open_alias failed\n");
1776 goto done;
1779 result = rpccli_samr_delete_dom_alias(pipe_hnd, mem_ctx, &group_pol);
1780 break;
1781 default:
1782 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1783 argv[0],sid_type_lookup(name_types[0]));
1784 result = NT_STATUS_UNSUCCESSFUL;
1785 goto done;
1789 if (NT_STATUS_IS_OK(result)) {
1790 if (opt_verbose)
1791 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1792 } else {
1793 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1794 get_friendly_nt_error_msg(result));
1797 done:
1798 return result;
1802 static int rpc_group_delete(int argc, const char **argv)
1804 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1805 argc,argv);
1808 static NTSTATUS rpc_group_add_internals(const DOM_SID *domain_sid,
1809 const char *domain_name,
1810 struct cli_state *cli,
1811 struct rpc_pipe_client *pipe_hnd,
1812 TALLOC_CTX *mem_ctx,
1813 int argc,
1814 const char **argv)
1816 POLICY_HND connect_pol, domain_pol, group_pol;
1817 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1818 GROUP_INFO_CTR group_info;
1820 if (argc != 1) {
1821 d_printf("Group name must be specified\n");
1822 rpc_group_usage(argc, argv);
1823 return NT_STATUS_OK;
1826 /* Get sam policy handle */
1828 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1829 &connect_pol);
1830 if (!NT_STATUS_IS_OK(result)) goto done;
1832 /* Get domain policy handle */
1834 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1835 MAXIMUM_ALLOWED_ACCESS,
1836 domain_sid, &domain_pol);
1837 if (!NT_STATUS_IS_OK(result)) goto done;
1839 /* Create the group */
1841 result = rpccli_samr_create_dom_group(pipe_hnd, mem_ctx, &domain_pol,
1842 argv[0], MAXIMUM_ALLOWED_ACCESS,
1843 &group_pol);
1844 if (!NT_STATUS_IS_OK(result)) goto done;
1846 if (strlen(opt_comment) == 0) goto done;
1848 /* We've got a comment to set */
1850 group_info.switch_value1 = 4;
1851 init_samr_group_info4(&group_info.group.info4, opt_comment);
1853 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &group_info);
1854 if (!NT_STATUS_IS_OK(result)) goto done;
1856 done:
1857 if (NT_STATUS_IS_OK(result))
1858 DEBUG(5, ("add group succeeded\n"));
1859 else
1860 d_fprintf(stderr, "add group failed: %s\n", nt_errstr(result));
1862 return result;
1865 static NTSTATUS rpc_alias_add_internals(const DOM_SID *domain_sid,
1866 const char *domain_name,
1867 struct cli_state *cli,
1868 struct rpc_pipe_client *pipe_hnd,
1869 TALLOC_CTX *mem_ctx,
1870 int argc,
1871 const char **argv)
1873 POLICY_HND connect_pol, domain_pol, alias_pol;
1874 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1875 ALIAS_INFO_CTR alias_info;
1877 if (argc != 1) {
1878 d_printf("Alias name must be specified\n");
1879 rpc_group_usage(argc, argv);
1880 return NT_STATUS_OK;
1883 /* Get sam policy handle */
1885 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1886 &connect_pol);
1887 if (!NT_STATUS_IS_OK(result)) goto done;
1889 /* Get domain policy handle */
1891 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
1892 MAXIMUM_ALLOWED_ACCESS,
1893 domain_sid, &domain_pol);
1894 if (!NT_STATUS_IS_OK(result)) goto done;
1896 /* Create the group */
1898 result = rpccli_samr_create_dom_alias(pipe_hnd, mem_ctx, &domain_pol,
1899 argv[0], &alias_pol);
1900 if (!NT_STATUS_IS_OK(result)) goto done;
1902 if (strlen(opt_comment) == 0) goto done;
1904 /* We've got a comment to set */
1906 alias_info.level = 3;
1907 init_samr_alias_info3(&alias_info.alias.info3, opt_comment);
1909 result = rpccli_samr_set_aliasinfo(pipe_hnd, mem_ctx, &alias_pol, &alias_info);
1910 if (!NT_STATUS_IS_OK(result)) goto done;
1912 done:
1913 if (NT_STATUS_IS_OK(result))
1914 DEBUG(5, ("add alias succeeded\n"));
1915 else
1916 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1918 return result;
1921 static int rpc_group_add(int argc, const char **argv)
1923 if (opt_localgroup)
1924 return run_rpc_command(NULL, PI_SAMR, 0,
1925 rpc_alias_add_internals,
1926 argc, argv);
1928 return run_rpc_command(NULL, PI_SAMR, 0,
1929 rpc_group_add_internals,
1930 argc, argv);
1933 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1934 TALLOC_CTX *mem_ctx,
1935 const char *name,
1936 DOM_SID *sid,
1937 enum SID_NAME_USE *type)
1939 DOM_SID *sids = NULL;
1940 uint32 *types = NULL;
1941 struct rpc_pipe_client *pipe_hnd;
1942 POLICY_HND lsa_pol;
1943 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1945 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
1946 if (!pipe_hnd) {
1947 goto done;
1950 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, False,
1951 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1953 if (!NT_STATUS_IS_OK(result)) {
1954 goto done;
1957 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1958 &name, NULL, &sids, &types);
1960 if (NT_STATUS_IS_OK(result)) {
1961 sid_copy(sid, &sids[0]);
1962 *type = types[0];
1965 rpccli_lsa_close(pipe_hnd, mem_ctx, &lsa_pol);
1967 done:
1968 if (pipe_hnd) {
1969 cli_rpc_pipe_close(pipe_hnd);
1972 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1974 /* Try as S-1-5-whatever */
1976 DOM_SID tmp_sid;
1978 if (string_to_sid(&tmp_sid, name)) {
1979 sid_copy(sid, &tmp_sid);
1980 *type = SID_NAME_UNKNOWN;
1981 result = NT_STATUS_OK;
1985 return result;
1988 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1989 TALLOC_CTX *mem_ctx,
1990 const DOM_SID *group_sid,
1991 const char *member)
1993 POLICY_HND connect_pol, domain_pol;
1994 NTSTATUS result;
1995 uint32 group_rid;
1996 POLICY_HND group_pol;
1998 uint32 num_rids;
1999 uint32 *rids = NULL;
2000 uint32 *rid_types = NULL;
2002 DOM_SID sid;
2004 sid_copy(&sid, group_sid);
2006 if (!sid_split_rid(&sid, &group_rid)) {
2007 return NT_STATUS_UNSUCCESSFUL;
2010 /* Get sam policy handle */
2011 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2012 &connect_pol);
2013 if (!NT_STATUS_IS_OK(result)) {
2014 return result;
2017 /* Get domain policy handle */
2018 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2019 MAXIMUM_ALLOWED_ACCESS,
2020 &sid, &domain_pol);
2021 if (!NT_STATUS_IS_OK(result)) {
2022 return result;
2025 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2026 1, &member,
2027 &num_rids, &rids, &rid_types);
2029 if (!NT_STATUS_IS_OK(result)) {
2030 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2031 goto done;
2034 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2035 MAXIMUM_ALLOWED_ACCESS,
2036 group_rid, &group_pol);
2038 if (!NT_STATUS_IS_OK(result)) {
2039 goto done;
2042 result = rpccli_samr_add_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2044 done:
2045 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2046 return result;
2049 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2050 TALLOC_CTX *mem_ctx,
2051 const DOM_SID *alias_sid,
2052 const char *member)
2054 POLICY_HND connect_pol, domain_pol;
2055 NTSTATUS result;
2056 uint32 alias_rid;
2057 POLICY_HND alias_pol;
2059 DOM_SID member_sid;
2060 enum SID_NAME_USE member_type;
2062 DOM_SID sid;
2064 sid_copy(&sid, alias_sid);
2066 if (!sid_split_rid(&sid, &alias_rid)) {
2067 return NT_STATUS_UNSUCCESSFUL;
2070 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2071 &member_sid, &member_type);
2073 if (!NT_STATUS_IS_OK(result)) {
2074 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2075 return result;
2078 /* Get sam policy handle */
2079 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2080 &connect_pol);
2081 if (!NT_STATUS_IS_OK(result)) {
2082 goto done;
2085 /* Get domain policy handle */
2086 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2087 MAXIMUM_ALLOWED_ACCESS,
2088 &sid, &domain_pol);
2089 if (!NT_STATUS_IS_OK(result)) {
2090 goto done;
2093 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2094 MAXIMUM_ALLOWED_ACCESS,
2095 alias_rid, &alias_pol);
2097 if (!NT_STATUS_IS_OK(result)) {
2098 return result;
2101 result = rpccli_samr_add_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2103 if (!NT_STATUS_IS_OK(result)) {
2104 return result;
2107 done:
2108 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2109 return result;
2112 static NTSTATUS rpc_group_addmem_internals(const DOM_SID *domain_sid,
2113 const char *domain_name,
2114 struct cli_state *cli,
2115 struct rpc_pipe_client *pipe_hnd,
2116 TALLOC_CTX *mem_ctx,
2117 int argc,
2118 const char **argv)
2120 DOM_SID group_sid;
2121 enum SID_NAME_USE group_type;
2123 if (argc != 2) {
2124 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2125 return NT_STATUS_UNSUCCESSFUL;
2128 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2129 &group_sid, &group_type))) {
2130 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2131 return NT_STATUS_UNSUCCESSFUL;
2134 if (group_type == SID_NAME_DOM_GRP) {
2135 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2136 &group_sid, argv[1]);
2138 if (!NT_STATUS_IS_OK(result)) {
2139 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2140 argv[1], argv[0], nt_errstr(result));
2142 return result;
2145 if (group_type == SID_NAME_ALIAS) {
2146 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2147 &group_sid, argv[1]);
2149 if (!NT_STATUS_IS_OK(result)) {
2150 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2151 argv[1], argv[0], nt_errstr(result));
2153 return result;
2156 d_fprintf(stderr, "Can only add members to global or local groups "
2157 "which %s is not\n", argv[0]);
2159 return NT_STATUS_UNSUCCESSFUL;
2162 static int rpc_group_addmem(int argc, const char **argv)
2164 return run_rpc_command(NULL, PI_SAMR, 0,
2165 rpc_group_addmem_internals,
2166 argc, argv);
2169 static NTSTATUS rpc_del_groupmem(struct rpc_pipe_client *pipe_hnd,
2170 TALLOC_CTX *mem_ctx,
2171 const DOM_SID *group_sid,
2172 const char *member)
2174 POLICY_HND connect_pol, domain_pol;
2175 NTSTATUS result;
2176 uint32 group_rid;
2177 POLICY_HND group_pol;
2179 uint32 num_rids;
2180 uint32 *rids = NULL;
2181 uint32 *rid_types = NULL;
2183 DOM_SID sid;
2185 sid_copy(&sid, group_sid);
2187 if (!sid_split_rid(&sid, &group_rid))
2188 return NT_STATUS_UNSUCCESSFUL;
2190 /* Get sam policy handle */
2191 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2192 &connect_pol);
2193 if (!NT_STATUS_IS_OK(result))
2194 return result;
2196 /* Get domain policy handle */
2197 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2198 MAXIMUM_ALLOWED_ACCESS,
2199 &sid, &domain_pol);
2200 if (!NT_STATUS_IS_OK(result))
2201 return result;
2203 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2204 1, &member,
2205 &num_rids, &rids, &rid_types);
2207 if (!NT_STATUS_IS_OK(result)) {
2208 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2209 goto done;
2212 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2213 MAXIMUM_ALLOWED_ACCESS,
2214 group_rid, &group_pol);
2216 if (!NT_STATUS_IS_OK(result))
2217 goto done;
2219 result = rpccli_samr_del_groupmem(pipe_hnd, mem_ctx, &group_pol, rids[0]);
2221 done:
2222 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2223 return result;
2226 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2227 TALLOC_CTX *mem_ctx,
2228 const DOM_SID *alias_sid,
2229 const char *member)
2231 POLICY_HND connect_pol, domain_pol;
2232 NTSTATUS result;
2233 uint32 alias_rid;
2234 POLICY_HND alias_pol;
2236 DOM_SID member_sid;
2237 enum SID_NAME_USE member_type;
2239 DOM_SID sid;
2241 sid_copy(&sid, alias_sid);
2243 if (!sid_split_rid(&sid, &alias_rid))
2244 return NT_STATUS_UNSUCCESSFUL;
2246 result = get_sid_from_name(pipe_hnd->cli, mem_ctx, member,
2247 &member_sid, &member_type);
2249 if (!NT_STATUS_IS_OK(result)) {
2250 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2251 return result;
2254 /* Get sam policy handle */
2255 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2256 &connect_pol);
2257 if (!NT_STATUS_IS_OK(result)) {
2258 goto done;
2261 /* Get domain policy handle */
2262 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2263 MAXIMUM_ALLOWED_ACCESS,
2264 &sid, &domain_pol);
2265 if (!NT_STATUS_IS_OK(result)) {
2266 goto done;
2269 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
2270 MAXIMUM_ALLOWED_ACCESS,
2271 alias_rid, &alias_pol);
2273 if (!NT_STATUS_IS_OK(result))
2274 return result;
2276 result = rpccli_samr_del_aliasmem(pipe_hnd, mem_ctx, &alias_pol, &member_sid);
2278 if (!NT_STATUS_IS_OK(result))
2279 return result;
2281 done:
2282 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
2283 return result;
2286 static NTSTATUS rpc_group_delmem_internals(const DOM_SID *domain_sid,
2287 const char *domain_name,
2288 struct cli_state *cli,
2289 struct rpc_pipe_client *pipe_hnd,
2290 TALLOC_CTX *mem_ctx,
2291 int argc,
2292 const char **argv)
2294 DOM_SID group_sid;
2295 enum SID_NAME_USE group_type;
2297 if (argc != 2) {
2298 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2299 return NT_STATUS_UNSUCCESSFUL;
2302 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2303 &group_sid, &group_type))) {
2304 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2305 return NT_STATUS_UNSUCCESSFUL;
2308 if (group_type == SID_NAME_DOM_GRP) {
2309 NTSTATUS result = rpc_del_groupmem(pipe_hnd, mem_ctx,
2310 &group_sid, argv[1]);
2312 if (!NT_STATUS_IS_OK(result)) {
2313 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2314 argv[1], argv[0], nt_errstr(result));
2316 return result;
2319 if (group_type == SID_NAME_ALIAS) {
2320 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2321 &group_sid, argv[1]);
2323 if (!NT_STATUS_IS_OK(result)) {
2324 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2325 argv[1], argv[0], nt_errstr(result));
2327 return result;
2330 d_fprintf(stderr, "Can only delete members from global or local groups "
2331 "which %s is not\n", argv[0]);
2333 return NT_STATUS_UNSUCCESSFUL;
2336 static int rpc_group_delmem(int argc, const char **argv)
2338 return run_rpc_command(NULL, PI_SAMR, 0,
2339 rpc_group_delmem_internals,
2340 argc, argv);
2343 /**
2344 * List groups on a remote RPC server
2346 * All parameters are provided by the run_rpc_command function, except for
2347 * argc, argv which are passes through.
2349 * @param domain_sid The domain sid acquired from the remote server
2350 * @param cli A cli_state connected to the server.
2351 * @param mem_ctx Talloc context, destoyed on completion of the function.
2352 * @param argc Standard main() style argc
2353 * @param argv Standard main() style argv. Initial components are already
2354 * stripped
2356 * @return Normal NTSTATUS return.
2359 static NTSTATUS rpc_group_list_internals(const DOM_SID *domain_sid,
2360 const char *domain_name,
2361 struct cli_state *cli,
2362 struct rpc_pipe_client *pipe_hnd,
2363 TALLOC_CTX *mem_ctx,
2364 int argc,
2365 const char **argv)
2367 POLICY_HND connect_pol, domain_pol;
2368 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2369 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2370 struct acct_info *groups;
2371 BOOL global = False;
2372 BOOL local = False;
2373 BOOL builtin = False;
2375 if (argc == 0) {
2376 global = True;
2377 local = True;
2378 builtin = True;
2381 for (i=0; i<argc; i++) {
2382 if (strequal(argv[i], "global"))
2383 global = True;
2385 if (strequal(argv[i], "local"))
2386 local = True;
2388 if (strequal(argv[i], "builtin"))
2389 builtin = True;
2392 /* Get sam policy handle */
2394 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2395 &connect_pol);
2396 if (!NT_STATUS_IS_OK(result)) {
2397 goto done;
2400 /* Get domain policy handle */
2402 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2403 MAXIMUM_ALLOWED_ACCESS,
2404 domain_sid, &domain_pol);
2405 if (!NT_STATUS_IS_OK(result)) {
2406 goto done;
2409 /* Query domain groups */
2410 if (opt_long_list_entries)
2411 d_printf("\nGroup name Comment"\
2412 "\n-----------------------------\n");
2413 do {
2414 SAM_DISPINFO_CTR ctr;
2415 SAM_DISPINFO_3 info3;
2416 uint32 max_size;
2418 ZERO_STRUCT(ctr);
2419 ZERO_STRUCT(info3);
2420 ctr.sam.info3 = &info3;
2422 if (!global) break;
2424 get_query_dispinfo_params(
2425 loop_count, &max_entries, &max_size);
2427 result = rpccli_samr_query_dispinfo(pipe_hnd, mem_ctx, &domain_pol,
2428 &start_idx, 3, &num_entries,
2429 max_entries, max_size, &ctr);
2431 if (!NT_STATUS_IS_OK(result) &&
2432 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2433 break;
2435 for (i = 0; i < num_entries; i++) {
2437 fstring group, desc;
2439 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
2440 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
2442 if (opt_long_list_entries)
2443 printf("%-21.21s %-50.50s\n",
2444 group, desc);
2445 else
2446 printf("%s\n", group);
2448 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2449 /* query domain aliases */
2450 start_idx = 0;
2451 do {
2452 if (!local) break;
2454 /* The max_size field in cli_samr_enum_als_groups is more like
2455 * an account_control field with indiviual bits what to
2456 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
2457 * everything. I'm too lazy (sorry) to get this through to
2458 * rpc_parse/ etc. Volker */
2460 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2461 &start_idx, 0xffff,
2462 &groups, &num_entries);
2464 if (!NT_STATUS_IS_OK(result) &&
2465 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2466 break;
2468 for (i = 0; i < num_entries; i++) {
2470 char *description = NULL;
2472 if (opt_long_list_entries) {
2474 POLICY_HND alias_pol;
2475 ALIAS_INFO_CTR ctr;
2477 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2478 &domain_pol,
2479 0x8,
2480 groups[i].rid,
2481 &alias_pol))) &&
2482 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2483 &alias_pol, 3,
2484 &ctr))) &&
2485 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2486 &alias_pol)))) {
2487 description = unistr2_tdup(mem_ctx,
2488 ctr.alias.info3.description.string);
2492 if (description != NULL) {
2493 printf("%-21.21s %-50.50s\n",
2494 groups[i].acct_name,
2495 description);
2496 } else {
2497 printf("%s\n", groups[i].acct_name);
2500 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2501 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2502 /* Get builtin policy handle */
2504 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2505 MAXIMUM_ALLOWED_ACCESS,
2506 &global_sid_Builtin, &domain_pol);
2507 if (!NT_STATUS_IS_OK(result)) {
2508 goto done;
2510 /* query builtin aliases */
2511 start_idx = 0;
2512 do {
2513 if (!builtin) break;
2515 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
2516 &start_idx, max_entries,
2517 &groups, &num_entries);
2519 if (!NT_STATUS_IS_OK(result) &&
2520 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2521 break;
2523 for (i = 0; i < num_entries; i++) {
2525 char *description = NULL;
2527 if (opt_long_list_entries) {
2529 POLICY_HND alias_pol;
2530 ALIAS_INFO_CTR ctr;
2532 if ((NT_STATUS_IS_OK(rpccli_samr_open_alias(pipe_hnd, mem_ctx,
2533 &domain_pol,
2534 0x8,
2535 groups[i].rid,
2536 &alias_pol))) &&
2537 (NT_STATUS_IS_OK(rpccli_samr_query_alias_info(pipe_hnd, mem_ctx,
2538 &alias_pol, 3,
2539 &ctr))) &&
2540 (NT_STATUS_IS_OK(rpccli_samr_close(pipe_hnd, mem_ctx,
2541 &alias_pol)))) {
2542 description = unistr2_tdup(mem_ctx,
2543 ctr.alias.info3.description.string);
2547 if (description != NULL) {
2548 printf("%-21.21s %-50.50s\n",
2549 groups[i].acct_name,
2550 description);
2551 } else {
2552 printf("%s\n", groups[i].acct_name);
2555 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2557 done:
2558 return result;
2561 static int rpc_group_list(int argc, const char **argv)
2563 return run_rpc_command(NULL, PI_SAMR, 0,
2564 rpc_group_list_internals,
2565 argc, argv);
2568 static NTSTATUS rpc_list_group_members(struct rpc_pipe_client *pipe_hnd,
2569 TALLOC_CTX *mem_ctx,
2570 const char *domain_name,
2571 const DOM_SID *domain_sid,
2572 POLICY_HND *domain_pol,
2573 uint32 rid)
2575 NTSTATUS result;
2576 POLICY_HND group_pol;
2577 uint32 num_members, *group_rids, *group_attrs;
2578 uint32 num_names;
2579 char **names;
2580 uint32 *name_types;
2581 int i;
2583 fstring sid_str;
2584 sid_to_string(sid_str, domain_sid);
2586 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, domain_pol,
2587 MAXIMUM_ALLOWED_ACCESS,
2588 rid, &group_pol);
2590 if (!NT_STATUS_IS_OK(result))
2591 return result;
2593 result = rpccli_samr_query_groupmem(pipe_hnd, mem_ctx, &group_pol,
2594 &num_members, &group_rids,
2595 &group_attrs);
2597 if (!NT_STATUS_IS_OK(result))
2598 return result;
2600 while (num_members > 0) {
2601 int this_time = 512;
2603 if (num_members < this_time)
2604 this_time = num_members;
2606 result = rpccli_samr_lookup_rids(pipe_hnd, mem_ctx, domain_pol,
2607 this_time, group_rids,
2608 &num_names, &names, &name_types);
2610 if (!NT_STATUS_IS_OK(result))
2611 return result;
2613 /* We only have users as members, but make the output
2614 the same as the output of alias members */
2616 for (i = 0; i < this_time; i++) {
2618 if (opt_long_list_entries) {
2619 printf("%s-%d %s\\%s %d\n", sid_str,
2620 group_rids[i], domain_name, names[i],
2621 SID_NAME_USER);
2622 } else {
2623 printf("%s\\%s\n", domain_name, names[i]);
2627 num_members -= this_time;
2628 group_rids += 512;
2631 return NT_STATUS_OK;
2634 static NTSTATUS rpc_list_alias_members(struct rpc_pipe_client *pipe_hnd,
2635 TALLOC_CTX *mem_ctx,
2636 POLICY_HND *domain_pol,
2637 uint32 rid)
2639 NTSTATUS result;
2640 struct rpc_pipe_client *lsa_pipe;
2641 POLICY_HND alias_pol, lsa_pol;
2642 uint32 num_members;
2643 DOM_SID *alias_sids;
2644 char **domains;
2645 char **names;
2646 uint32 *types;
2647 int i;
2649 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, domain_pol,
2650 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
2652 if (!NT_STATUS_IS_OK(result))
2653 return result;
2655 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx, &alias_pol,
2656 &num_members, &alias_sids);
2658 if (!NT_STATUS_IS_OK(result)) {
2659 d_fprintf(stderr, "Couldn't list alias members\n");
2660 return result;
2663 if (num_members == 0) {
2664 return NT_STATUS_OK;
2667 lsa_pipe = cli_rpc_pipe_open_noauth(pipe_hnd->cli, PI_LSARPC, &result);
2668 if (!lsa_pipe) {
2669 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2670 nt_errstr(result) );
2671 return result;
2674 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, True,
2675 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2677 if (!NT_STATUS_IS_OK(result)) {
2678 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2679 cli_rpc_pipe_close(lsa_pipe);
2680 return result;
2683 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol, num_members,
2684 alias_sids,
2685 &domains, &names, &types);
2687 if (!NT_STATUS_IS_OK(result) &&
2688 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2689 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2690 cli_rpc_pipe_close(lsa_pipe);
2691 return result;
2694 for (i = 0; i < num_members; i++) {
2695 fstring sid_str;
2696 sid_to_string(sid_str, &alias_sids[i]);
2698 if (opt_long_list_entries) {
2699 printf("%s %s\\%s %d\n", sid_str,
2700 domains[i] ? domains[i] : "*unknown*",
2701 names[i] ? names[i] : "*unknown*", types[i]);
2702 } else {
2703 if (domains[i])
2704 printf("%s\\%s\n", domains[i], names[i]);
2705 else
2706 printf("%s\n", sid_str);
2710 cli_rpc_pipe_close(lsa_pipe);
2711 return NT_STATUS_OK;
2714 static NTSTATUS rpc_group_members_internals(const DOM_SID *domain_sid,
2715 const char *domain_name,
2716 struct cli_state *cli,
2717 struct rpc_pipe_client *pipe_hnd,
2718 TALLOC_CTX *mem_ctx,
2719 int argc,
2720 const char **argv)
2722 NTSTATUS result;
2723 POLICY_HND connect_pol, domain_pol;
2724 uint32 num_rids, *rids, *rid_types;
2726 /* Get sam policy handle */
2728 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2729 &connect_pol);
2731 if (!NT_STATUS_IS_OK(result))
2732 return result;
2734 /* Get domain policy handle */
2736 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2737 MAXIMUM_ALLOWED_ACCESS,
2738 domain_sid, &domain_pol);
2740 if (!NT_STATUS_IS_OK(result))
2741 return result;
2743 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2744 1, argv, &num_rids, &rids, &rid_types);
2746 if (!NT_STATUS_IS_OK(result)) {
2748 /* Ok, did not find it in the global sam, try with builtin */
2750 DOM_SID sid_Builtin;
2752 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
2754 string_to_sid(&sid_Builtin, "S-1-5-32");
2756 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2757 MAXIMUM_ALLOWED_ACCESS,
2758 &sid_Builtin, &domain_pol);
2760 if (!NT_STATUS_IS_OK(result)) {
2761 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2762 return result;
2765 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2766 1, argv, &num_rids,
2767 &rids, &rid_types);
2769 if (!NT_STATUS_IS_OK(result)) {
2770 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2771 return result;
2775 if (num_rids != 1) {
2776 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2777 return result;
2780 if (rid_types[0] == SID_NAME_DOM_GRP) {
2781 return rpc_list_group_members(pipe_hnd, mem_ctx, domain_name,
2782 domain_sid, &domain_pol,
2783 rids[0]);
2786 if (rid_types[0] == SID_NAME_ALIAS) {
2787 return rpc_list_alias_members(pipe_hnd, mem_ctx, &domain_pol,
2788 rids[0]);
2791 return NT_STATUS_NO_SUCH_GROUP;
2794 static int rpc_group_members(int argc, const char **argv)
2796 if (argc != 1) {
2797 return rpc_group_usage(argc, argv);
2800 return run_rpc_command(NULL, PI_SAMR, 0,
2801 rpc_group_members_internals,
2802 argc, argv);
2805 static NTSTATUS rpc_group_rename_internals(const DOM_SID *domain_sid,
2806 const char *domain_name,
2807 struct cli_state *cli,
2808 struct rpc_pipe_client *pipe_hnd,
2809 TALLOC_CTX *mem_ctx,
2810 int argc,
2811 const char **argv)
2813 NTSTATUS result;
2814 POLICY_HND connect_pol, domain_pol, group_pol;
2815 uint32 num_rids, *rids, *rid_types;
2816 GROUP_INFO_CTR ctr;
2818 if (argc != 2) {
2819 d_printf("Usage: 'net rpc group rename group newname'\n");
2820 return NT_STATUS_UNSUCCESSFUL;
2823 /* Get sam policy handle */
2825 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2826 &connect_pol);
2828 if (!NT_STATUS_IS_OK(result))
2829 return result;
2831 /* Get domain policy handle */
2833 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
2834 MAXIMUM_ALLOWED_ACCESS,
2835 domain_sid, &domain_pol);
2837 if (!NT_STATUS_IS_OK(result))
2838 return result;
2840 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, 1000,
2841 1, argv, &num_rids, &rids, &rid_types);
2843 if (num_rids != 1) {
2844 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2845 return result;
2848 if (rid_types[0] != SID_NAME_DOM_GRP) {
2849 d_fprintf(stderr, "Can only rename domain groups\n");
2850 return NT_STATUS_UNSUCCESSFUL;
2853 result = rpccli_samr_open_group(pipe_hnd, mem_ctx, &domain_pol,
2854 MAXIMUM_ALLOWED_ACCESS,
2855 rids[0], &group_pol);
2857 if (!NT_STATUS_IS_OK(result))
2858 return result;
2860 ZERO_STRUCT(ctr);
2862 ctr.switch_value1 = 2;
2863 init_samr_group_info2(&ctr.group.info2, argv[1]);
2865 result = rpccli_samr_set_groupinfo(pipe_hnd, mem_ctx, &group_pol, &ctr);
2867 if (!NT_STATUS_IS_OK(result))
2868 return result;
2870 return NT_STATUS_NO_SUCH_GROUP;
2873 static int rpc_group_rename(int argc, const char **argv)
2875 if (argc != 2) {
2876 return rpc_group_usage(argc, argv);
2879 return run_rpc_command(NULL, PI_SAMR, 0,
2880 rpc_group_rename_internals,
2881 argc, argv);
2884 /**
2885 * 'net rpc group' entrypoint.
2886 * @param argc Standard main() style argc
2887 * @param argc Standard main() style argv. Initial components are already
2888 * stripped
2891 int net_rpc_group(int argc, const char **argv)
2893 struct functable func[] = {
2894 {"add", rpc_group_add},
2895 {"delete", rpc_group_delete},
2896 {"addmem", rpc_group_addmem},
2897 {"delmem", rpc_group_delmem},
2898 {"list", rpc_group_list},
2899 {"members", rpc_group_members},
2900 {"rename", rpc_group_rename},
2901 {NULL, NULL}
2904 if (argc == 0) {
2905 return run_rpc_command(NULL, PI_SAMR, 0,
2906 rpc_group_list_internals,
2907 argc, argv);
2910 return net_run_function(argc, argv, func, rpc_group_usage);
2913 /****************************************************************************/
2915 static int rpc_share_usage(int argc, const char **argv)
2917 return net_help_share(argc, argv);
2920 /**
2921 * Add a share on a remote RPC server
2923 * All parameters are provided by the run_rpc_command function, except for
2924 * argc, argv which are passes through.
2926 * @param domain_sid The domain sid acquired from the remote server
2927 * @param cli A cli_state connected to the server.
2928 * @param mem_ctx Talloc context, destoyed on completion of the function.
2929 * @param argc Standard main() style argc
2930 * @param argv Standard main() style argv. Initial components are already
2931 * stripped
2933 * @return Normal NTSTATUS return.
2935 static NTSTATUS rpc_share_add_internals(const DOM_SID *domain_sid,
2936 const char *domain_name,
2937 struct cli_state *cli,
2938 struct rpc_pipe_client *pipe_hnd,
2939 TALLOC_CTX *mem_ctx,int argc,
2940 const char **argv)
2942 WERROR result;
2943 char *sharename=talloc_strdup(mem_ctx, argv[0]);
2944 char *path;
2945 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2946 uint32 num_users=0, perms=0;
2947 char *password=NULL; /* don't allow a share password */
2948 uint32 level = 2;
2950 path = strchr(sharename, '=');
2951 if (!path)
2952 return NT_STATUS_UNSUCCESSFUL;
2953 *path++ = '\0';
2955 result = rpccli_srvsvc_net_share_add(pipe_hnd, mem_ctx, sharename, type,
2956 opt_comment, perms, opt_maxusers,
2957 num_users, path, password,
2958 level, NULL);
2959 return werror_to_ntstatus(result);
2962 static int rpc_share_add(int argc, const char **argv)
2964 if ((argc < 1) || !strchr(argv[0], '=')) {
2965 DEBUG(1,("Sharename or path not specified on add\n"));
2966 return rpc_share_usage(argc, argv);
2968 return run_rpc_command(NULL, PI_SRVSVC, 0,
2969 rpc_share_add_internals,
2970 argc, argv);
2973 /**
2974 * Delete a share on a remote RPC server
2976 * All parameters are provided by the run_rpc_command function, except for
2977 * argc, argv which are passes through.
2979 * @param domain_sid The domain sid acquired from the remote server
2980 * @param cli A cli_state connected to the server.
2981 * @param mem_ctx Talloc context, destoyed on completion of the function.
2982 * @param argc Standard main() style argc
2983 * @param argv Standard main() style argv. Initial components are already
2984 * stripped
2986 * @return Normal NTSTATUS return.
2988 static NTSTATUS rpc_share_del_internals(const DOM_SID *domain_sid,
2989 const char *domain_name,
2990 struct cli_state *cli,
2991 struct rpc_pipe_client *pipe_hnd,
2992 TALLOC_CTX *mem_ctx,
2993 int argc,
2994 const char **argv)
2996 WERROR result;
2998 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
2999 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3002 /**
3003 * Delete a share on a remote RPC server
3005 * @param domain_sid The domain sid acquired from the remote server
3006 * @param argc Standard main() style argc
3007 * @param argv Standard main() style argv. Initial components are already
3008 * stripped
3010 * @return A shell status integer (0 for success)
3012 static int rpc_share_delete(int argc, const char **argv)
3014 if (argc < 1) {
3015 DEBUG(1,("Sharename not specified on delete\n"));
3016 return rpc_share_usage(argc, argv);
3018 return run_rpc_command(NULL, PI_SRVSVC, 0,
3019 rpc_share_del_internals,
3020 argc, argv);
3024 * Formatted print of share info
3026 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3029 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
3031 fstring netname = "", remark = "";
3033 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
3034 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
3036 if (opt_long_list_entries) {
3037 d_printf("%-12s %-8.8s %-50s\n",
3038 netname, share_type[info1->info_1.type], remark);
3039 } else {
3040 d_printf("%s\n", netname);
3045 static WERROR get_share_info(struct rpc_pipe_client *pipe_hnd,
3046 TALLOC_CTX *mem_ctx,
3047 uint32 level,
3048 int argc,
3049 const char **argv,
3050 SRV_SHARE_INFO_CTR *ctr)
3052 WERROR result;
3053 SRV_SHARE_INFO info;
3055 /* no specific share requested, enumerate all */
3056 if (argc == 0) {
3058 ENUM_HND hnd;
3059 uint32 preferred_len = 0xffffffff;
3061 init_enum_hnd(&hnd, 0);
3063 return rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, level, ctr,
3064 preferred_len, &hnd);
3067 /* request just one share */
3068 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, argv[0], level, &info);
3070 if (!W_ERROR_IS_OK(result))
3071 goto done;
3073 /* construct ctr */
3074 ZERO_STRUCTP(ctr);
3076 ctr->info_level = ctr->switch_value = level;
3077 ctr->ptr_share_info = ctr->ptr_entries = 1;
3078 ctr->num_entries = ctr->num_entries2 = 1;
3080 switch (level) {
3081 case 1:
3083 char *s;
3084 SRV_SHARE_INFO_1 *info1;
3086 ctr->share.info1 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_1, 1);
3087 info1 = ctr->share.info1;
3089 memset(ctr->share.info1, 0, sizeof(SRV_SHARE_INFO_1));
3091 /* Copy pointer crap */
3093 memcpy(&info1->info_1, &info.share.info1.info_1, sizeof(SH_INFO_1));
3095 /* Duplicate strings */
3097 s = unistr2_tdup(mem_ctx, &info.share.info1.info_1_str.uni_netname);
3098 if (s)
3099 init_unistr2(&info1->info_1_str.uni_netname, s, UNI_STR_TERMINATE);
3101 s = unistr2_tdup(mem_ctx, &info.share.info1.info_1_str.uni_remark);
3102 if (s)
3103 init_unistr2(&info1->info_1_str.uni_remark, s, UNI_STR_TERMINATE);
3105 case 2:
3107 char *s;
3108 SRV_SHARE_INFO_2 *info2;
3110 ctr->share.info2 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_2, 1);
3111 info2 = ctr->share.info2;
3113 memset(ctr->share.info2, 0, sizeof(SRV_SHARE_INFO_2));
3115 /* Copy pointer crap */
3117 memcpy(&info2->info_2, &info.share.info2.info_2, sizeof(SH_INFO_2));
3119 /* Duplicate strings */
3121 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_netname);
3122 if (s)
3123 init_unistr2(&info2->info_2_str.uni_netname, s, UNI_STR_TERMINATE);
3125 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_remark);
3126 if (s)
3127 init_unistr2(&info2->info_2_str.uni_remark, s, UNI_STR_TERMINATE);
3129 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_path);
3130 if (s)
3131 init_unistr2(&info2->info_2_str.uni_path, s, UNI_STR_TERMINATE);
3133 s = unistr2_tdup(mem_ctx, &info.share.info2.info_2_str.uni_passwd);
3134 if (s)
3135 init_unistr2(&info2->info_2_str.uni_passwd, s, UNI_STR_TERMINATE);
3137 case 502:
3139 char *s;
3140 SRV_SHARE_INFO_502 *info502;
3142 ctr->share.info502 = TALLOC_ARRAY(mem_ctx, SRV_SHARE_INFO_502, 1);
3143 info502 = ctr->share.info502;
3145 memset(ctr->share.info502, 0, sizeof(SRV_SHARE_INFO_502));
3147 /* Copy pointer crap */
3149 memcpy(&info502->info_502, &info.share.info502.info_502, sizeof(SH_INFO_502));
3151 /* Duplicate strings */
3153 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_netname);
3154 if (s)
3155 init_unistr2(&info502->info_502_str.uni_netname, s, UNI_STR_TERMINATE);
3157 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_remark);
3158 if (s)
3159 init_unistr2(&info502->info_502_str.uni_remark, s, UNI_STR_TERMINATE);
3161 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_path);
3162 if (s)
3163 init_unistr2(&info502->info_502_str.uni_path, s, UNI_STR_TERMINATE);
3165 s = unistr2_tdup(mem_ctx, &info.share.info502.info_502_str.uni_passwd);
3166 if (s)
3167 init_unistr2(&info502->info_502_str.uni_passwd, s, UNI_STR_TERMINATE);
3169 info502->info_502_str.sd = dup_sec_desc(mem_ctx, info.share.info502.info_502_str.sd);
3173 } /* switch */
3175 done:
3176 return result;
3179 /**
3180 * List shares on a remote RPC server
3182 * All parameters are provided by the run_rpc_command function, except for
3183 * argc, argv which are passes through.
3185 * @param domain_sid The domain sid acquired from the remote server
3186 * @param cli A cli_state connected to the server.
3187 * @param mem_ctx Talloc context, destoyed on completion of the function.
3188 * @param argc Standard main() style argc
3189 * @param argv Standard main() style argv. Initial components are already
3190 * stripped
3192 * @return Normal NTSTATUS return.
3195 static NTSTATUS rpc_share_list_internals(const DOM_SID *domain_sid,
3196 const char *domain_name,
3197 struct cli_state *cli,
3198 struct rpc_pipe_client *pipe_hnd,
3199 TALLOC_CTX *mem_ctx,
3200 int argc,
3201 const char **argv)
3203 SRV_SHARE_INFO_CTR ctr;
3204 WERROR result;
3205 uint32 i, level = 1;
3207 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr);
3208 if (!W_ERROR_IS_OK(result))
3209 goto done;
3211 /* Display results */
3213 if (opt_long_list_entries) {
3214 d_printf(
3215 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3216 "\nShare name Type Description\n"\
3217 "---------- ---- -----------\n");
3219 for (i = 0; i < ctr.num_entries; i++)
3220 display_share_info_1(&ctr.share.info1[i]);
3221 done:
3222 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3225 /***
3226 * 'net rpc share list' entrypoint.
3227 * @param argc Standard main() style argc
3228 * @param argv Standard main() style argv. Initial components are already
3229 * stripped
3231 static int rpc_share_list(int argc, const char **argv)
3233 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_list_internals, argc, argv);
3236 static BOOL check_share_availability(struct cli_state *cli, const char *netname)
3238 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3239 d_printf("skipping [%s]: not a file share.\n", netname);
3240 return False;
3243 if (!cli_tdis(cli))
3244 return False;
3246 return True;
3249 static BOOL check_share_sanity(struct cli_state *cli, fstring netname, uint32 type)
3251 /* only support disk shares */
3252 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3253 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3254 return False;
3257 /* skip builtin shares */
3258 /* FIXME: should print$ be added too ? */
3259 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3260 strequal(netname,"global"))
3261 return False;
3263 if (opt_exclude && in_list(netname, opt_exclude, False)) {
3264 printf("excluding [%s]\n", netname);
3265 return False;
3268 return check_share_availability(cli, netname);
3271 /**
3272 * Migrate shares from a remote RPC server to the local RPC srever
3274 * All parameters are provided by the run_rpc_command function, except for
3275 * argc, argv which are passes through.
3277 * @param domain_sid The domain sid acquired from the remote server
3278 * @param cli A cli_state connected to the server.
3279 * @param mem_ctx Talloc context, destoyed on completion of the function.
3280 * @param argc Standard main() style argc
3281 * @param argv Standard main() style argv. Initial components are already
3282 * stripped
3284 * @return Normal NTSTATUS return.
3287 static NTSTATUS rpc_share_migrate_shares_internals(const DOM_SID *domain_sid,
3288 const char *domain_name,
3289 struct cli_state *cli,
3290 struct rpc_pipe_client *pipe_hnd,
3291 TALLOC_CTX *mem_ctx,
3292 int argc,
3293 const char **argv)
3295 WERROR result;
3296 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3297 SRV_SHARE_INFO_CTR ctr_src;
3298 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3299 char *password = NULL; /* don't allow a share password */
3300 uint32 i;
3301 struct rpc_pipe_client *srvsvc_pipe = NULL;
3302 struct cli_state *cli_dst = NULL;
3303 uint32 level = 502; /* includes secdesc */
3305 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3306 if (!W_ERROR_IS_OK(result))
3307 goto done;
3309 /* connect destination PI_SRVSVC */
3310 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3311 if (!NT_STATUS_IS_OK(nt_status))
3312 return nt_status;
3315 for (i = 0; i < ctr_src.num_entries; i++) {
3317 fstring netname = "", remark = "", path = "";
3318 /* reset error-code */
3319 nt_status = NT_STATUS_UNSUCCESSFUL;
3321 rpcstr_pull_unistr2_fstring(
3322 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3323 rpcstr_pull_unistr2_fstring(
3324 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3325 rpcstr_pull_unistr2_fstring(
3326 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3328 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3329 continue;
3331 /* finally add the share on the dst server */
3333 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3334 netname, path, remark);
3336 result = rpccli_srvsvc_net_share_add(srvsvc_pipe, mem_ctx, netname, type, remark,
3337 ctr_src.share.info502[i].info_502.perms,
3338 ctr_src.share.info502[i].info_502.max_uses,
3339 ctr_src.share.info502[i].info_502.num_uses,
3340 path, password, level,
3341 NULL);
3343 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3344 printf(" [%s] does already exist\n", netname);
3345 continue;
3348 if (!W_ERROR_IS_OK(result)) {
3349 printf("cannot add share: %s\n", dos_errstr(result));
3350 goto done;
3355 nt_status = NT_STATUS_OK;
3357 done:
3358 if (cli_dst) {
3359 cli_shutdown(cli_dst);
3362 return nt_status;
3366 /**
3367 * Migrate shares from a rpc-server to another
3369 * @param argc Standard main() style argc
3370 * @param argv Standard main() style argv. Initial components are already
3371 * stripped
3373 * @return A shell status integer (0 for success)
3375 static int rpc_share_migrate_shares(int argc, const char **argv)
3378 if (!opt_host) {
3379 printf("no server to migrate\n");
3380 return -1;
3383 return run_rpc_command(NULL, PI_SRVSVC, 0,
3384 rpc_share_migrate_shares_internals,
3385 argc, argv);
3389 * Copy a file/dir
3391 * @param f file_info
3392 * @param mask current search mask
3393 * @param state arg-pointer
3396 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
3398 static NTSTATUS nt_status;
3399 static struct copy_clistate *local_state;
3400 static fstring filename, new_mask;
3401 fstring dir;
3402 char *old_dir;
3404 local_state = (struct copy_clistate *)state;
3405 nt_status = NT_STATUS_UNSUCCESSFUL;
3407 if (strequal(f->name, ".") || strequal(f->name, ".."))
3408 return;
3410 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3412 /* DIRECTORY */
3413 if (f->mode & aDIR) {
3415 DEBUG(3,("got dir: %s\n", f->name));
3417 fstrcpy(dir, local_state->cwd);
3418 fstrcat(dir, "\\");
3419 fstrcat(dir, f->name);
3421 switch (net_mode_share)
3423 case NET_MODE_SHARE_MIGRATE:
3424 /* create that directory */
3425 nt_status = net_copy_file(local_state->mem_ctx,
3426 local_state->cli_share_src,
3427 local_state->cli_share_dst,
3428 dir, dir,
3429 opt_acls? True : False,
3430 opt_attrs? True : False,
3431 opt_timestamps? True : False,
3432 False);
3433 break;
3434 default:
3435 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3436 return;
3439 if (!NT_STATUS_IS_OK(nt_status))
3440 printf("could not handle dir %s: %s\n",
3441 dir, nt_errstr(nt_status));
3443 /* search below that directory */
3444 fstrcpy(new_mask, dir);
3445 fstrcat(new_mask, "\\*");
3447 old_dir = local_state->cwd;
3448 local_state->cwd = dir;
3449 if (!sync_files(local_state, new_mask))
3450 printf("could not handle files\n");
3451 local_state->cwd = old_dir;
3453 return;
3457 /* FILE */
3458 fstrcpy(filename, local_state->cwd);
3459 fstrcat(filename, "\\");
3460 fstrcat(filename, f->name);
3462 DEBUG(3,("got file: %s\n", filename));
3464 switch (net_mode_share)
3466 case NET_MODE_SHARE_MIGRATE:
3467 nt_status = net_copy_file(local_state->mem_ctx,
3468 local_state->cli_share_src,
3469 local_state->cli_share_dst,
3470 filename, filename,
3471 opt_acls? True : False,
3472 opt_attrs? True : False,
3473 opt_timestamps? True: False,
3474 True);
3475 break;
3476 default:
3477 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3478 return;
3481 if (!NT_STATUS_IS_OK(nt_status))
3482 printf("could not handle file %s: %s\n",
3483 filename, nt_errstr(nt_status));
3488 * sync files, can be called recursivly to list files
3489 * and then call copy_fn for each file
3491 * @param cp_clistate pointer to the copy_clistate we work with
3492 * @param mask the current search mask
3494 * @return Boolean result
3496 BOOL sync_files(struct copy_clistate *cp_clistate, pstring mask)
3499 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3501 if (cli_list(cp_clistate->cli_share_src, mask, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3502 d_fprintf(stderr, "listing %s failed with error: %s\n",
3503 mask, cli_errstr(cp_clistate->cli_share_src));
3504 return False;
3507 return True;
3512 * Set the top level directory permissions before we do any further copies.
3513 * Should set up ACL inheritance.
3516 BOOL copy_top_level_perms(struct copy_clistate *cp_clistate,
3517 const char *sharename)
3519 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3521 switch (net_mode_share) {
3522 case NET_MODE_SHARE_MIGRATE:
3523 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3524 nt_status = net_copy_fileattr(cp_clistate->mem_ctx,
3525 cp_clistate->cli_share_src,
3526 cp_clistate->cli_share_dst,
3527 "\\", "\\",
3528 opt_acls? True : False,
3529 opt_attrs? True : False,
3530 opt_timestamps? True: False,
3531 False);
3532 break;
3533 default:
3534 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3535 break;
3538 if (!NT_STATUS_IS_OK(nt_status)) {
3539 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3540 sharename, nt_errstr(nt_status));
3541 return False;
3544 return True;
3547 /**
3548 * Sync all files inside a remote share to another share (over smb)
3550 * All parameters are provided by the run_rpc_command function, except for
3551 * argc, argv which are passes through.
3553 * @param domain_sid The domain sid acquired from the remote server
3554 * @param cli A cli_state connected to the server.
3555 * @param mem_ctx Talloc context, destoyed on completion of the function.
3556 * @param argc Standard main() style argc
3557 * @param argv Standard main() style argv. Initial components are already
3558 * stripped
3560 * @return Normal NTSTATUS return.
3563 static NTSTATUS rpc_share_migrate_files_internals(const DOM_SID *domain_sid,
3564 const char *domain_name,
3565 struct cli_state *cli,
3566 struct rpc_pipe_client *pipe_hnd,
3567 TALLOC_CTX *mem_ctx,
3568 int argc,
3569 const char **argv)
3571 WERROR result;
3572 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3573 SRV_SHARE_INFO_CTR ctr_src;
3574 uint32 i;
3575 uint32 level = 502;
3576 struct copy_clistate cp_clistate;
3577 BOOL got_src_share = False;
3578 BOOL got_dst_share = False;
3579 pstring mask = "\\*";
3580 char *dst = NULL;
3582 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
3584 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3586 if (!W_ERROR_IS_OK(result))
3587 goto done;
3589 for (i = 0; i < ctr_src.num_entries; i++) {
3591 fstring netname = "";
3593 rpcstr_pull_unistr2_fstring(
3594 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3596 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3597 continue;
3599 /* one might not want to mirror whole discs :) */
3600 if (strequal(netname, "print$") || netname[1] == '$') {
3601 d_printf("skipping [%s]: builtin/hidden share\n", netname);
3602 continue;
3605 switch (net_mode_share)
3607 case NET_MODE_SHARE_MIGRATE:
3608 printf("syncing");
3609 break;
3610 default:
3611 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3612 break;
3614 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3615 netname,
3616 opt_acls ? "including" : "without",
3617 opt_attrs ? "including" : "without",
3618 opt_timestamps ? "(preserving timestamps)" : "");
3620 cp_clistate.mem_ctx = mem_ctx;
3621 cp_clistate.cli_share_src = NULL;
3622 cp_clistate.cli_share_dst = NULL;
3623 cp_clistate.cwd = NULL;
3624 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3626 /* open share source */
3627 nt_status = connect_to_service(&cp_clistate.cli_share_src,
3628 &cli->dest_ip, cli->desthost,
3629 netname, "A:");
3630 if (!NT_STATUS_IS_OK(nt_status))
3631 goto done;
3633 got_src_share = True;
3635 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3636 /* open share destination */
3637 nt_status = connect_to_service(&cp_clistate.cli_share_dst,
3638 NULL, dst, netname, "A:");
3639 if (!NT_STATUS_IS_OK(nt_status))
3640 goto done;
3642 got_dst_share = True;
3645 if (!copy_top_level_perms(&cp_clistate, netname)) {
3646 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", netname);
3647 nt_status = NT_STATUS_UNSUCCESSFUL;
3648 goto done;
3651 if (!sync_files(&cp_clistate, mask)) {
3652 d_fprintf(stderr, "could not handle files for share: %s\n", netname);
3653 nt_status = NT_STATUS_UNSUCCESSFUL;
3654 goto done;
3658 nt_status = NT_STATUS_OK;
3660 done:
3662 if (got_src_share)
3663 cli_shutdown(cp_clistate.cli_share_src);
3665 if (got_dst_share)
3666 cli_shutdown(cp_clistate.cli_share_dst);
3668 return nt_status;
3672 static int rpc_share_migrate_files(int argc, const char **argv)
3675 if (!opt_host) {
3676 printf("no server to migrate\n");
3677 return -1;
3680 return run_rpc_command(NULL, PI_SRVSVC, 0,
3681 rpc_share_migrate_files_internals,
3682 argc, argv);
3685 /**
3686 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3688 * All parameters are provided by the run_rpc_command function, except for
3689 * argc, argv which are passes through.
3691 * @param domain_sid The domain sid acquired from the remote server
3692 * @param cli A cli_state connected to the server.
3693 * @param mem_ctx Talloc context, destoyed on completion of the function.
3694 * @param argc Standard main() style argc
3695 * @param argv Standard main() style argv. Initial components are already
3696 * stripped
3698 * @return Normal NTSTATUS return.
3701 static NTSTATUS rpc_share_migrate_security_internals(const DOM_SID *domain_sid,
3702 const char *domain_name,
3703 struct cli_state *cli,
3704 struct rpc_pipe_client *pipe_hnd,
3705 TALLOC_CTX *mem_ctx,
3706 int argc,
3707 const char **argv)
3709 WERROR result;
3710 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3711 SRV_SHARE_INFO_CTR ctr_src;
3712 SRV_SHARE_INFO info;
3713 uint32 i;
3714 struct rpc_pipe_client *srvsvc_pipe = NULL;
3715 struct cli_state *cli_dst = NULL;
3716 uint32 level = 502; /* includes secdesc */
3718 result = get_share_info(pipe_hnd, mem_ctx, level, argc, argv, &ctr_src);
3720 if (!W_ERROR_IS_OK(result))
3721 goto done;
3723 /* connect destination PI_SRVSVC */
3724 nt_status = connect_dst_pipe(&cli_dst, &srvsvc_pipe, PI_SRVSVC);
3725 if (!NT_STATUS_IS_OK(nt_status))
3726 return nt_status;
3729 for (i = 0; i < ctr_src.num_entries; i++) {
3731 fstring netname = "", remark = "", path = "";
3732 /* reset error-code */
3733 nt_status = NT_STATUS_UNSUCCESSFUL;
3735 rpcstr_pull_unistr2_fstring(
3736 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
3737 rpcstr_pull_unistr2_fstring(
3738 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
3739 rpcstr_pull_unistr2_fstring(
3740 path, &ctr_src.share.info502[i].info_502_str.uni_path);
3742 if (!check_share_sanity(cli, netname, ctr_src.share.info502[i].info_502.type))
3743 continue;
3745 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3746 netname, path, remark);
3748 if (opt_verbose)
3749 display_sec_desc(ctr_src.share.info502[i].info_502_str.sd);
3751 /* init info */
3752 ZERO_STRUCT(info);
3754 info.switch_value = level;
3755 info.ptr_share_ctr = 1;
3757 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3758 info.share.info502 = ctr_src.share.info502[i];
3760 /* finally modify the share on the dst server */
3761 result = rpccli_srvsvc_net_share_set_info(srvsvc_pipe, mem_ctx, netname, level, &info);
3763 if (!W_ERROR_IS_OK(result)) {
3764 printf("cannot set share-acl: %s\n", dos_errstr(result));
3765 goto done;
3770 nt_status = NT_STATUS_OK;
3772 done:
3773 if (cli_dst) {
3774 cli_shutdown(cli_dst);
3777 return nt_status;
3781 /**
3782 * Migrate share-acls from a rpc-server to another
3784 * @param argc Standard main() style argc
3785 * @param argv Standard main() style argv. Initial components are already
3786 * stripped
3788 * @return A shell status integer (0 for success)
3790 static int rpc_share_migrate_security(int argc, const char **argv)
3793 if (!opt_host) {
3794 printf("no server to migrate\n");
3795 return -1;
3798 return run_rpc_command(NULL, PI_SRVSVC, 0,
3799 rpc_share_migrate_security_internals,
3800 argc, argv);
3803 /**
3804 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3805 * from one server to another
3807 * @param argc Standard main() style argc
3808 * @param argv Standard main() style argv. Initial components are already
3809 * stripped
3811 * @return A shell status integer (0 for success)
3814 static int rpc_share_migrate_all(int argc, const char **argv)
3816 int ret;
3818 if (!opt_host) {
3819 printf("no server to migrate\n");
3820 return -1;
3823 /* order is important. we don't want to be locked out by the share-acl
3824 * before copying files - gd */
3826 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
3827 if (ret)
3828 return ret;
3830 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
3831 if (ret)
3832 return ret;
3834 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_security_internals, argc, argv);
3838 /**
3839 * 'net rpc share migrate' entrypoint.
3840 * @param argc Standard main() style argc
3841 * @param argv Standard main() style argv. Initial components are already
3842 * stripped
3844 static int rpc_share_migrate(int argc, const char **argv)
3847 struct functable func[] = {
3848 {"all", rpc_share_migrate_all},
3849 {"files", rpc_share_migrate_files},
3850 {"help", rpc_share_usage},
3851 {"security", rpc_share_migrate_security},
3852 {"shares", rpc_share_migrate_shares},
3853 {NULL, NULL}
3856 net_mode_share = NET_MODE_SHARE_MIGRATE;
3858 return net_run_function(argc, argv, func, rpc_share_usage);
3861 struct full_alias {
3862 DOM_SID sid;
3863 uint32 num_members;
3864 DOM_SID *members;
3867 static int num_server_aliases;
3868 static struct full_alias *server_aliases;
3871 * Add an alias to the static list.
3873 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3875 if (server_aliases == NULL)
3876 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3878 server_aliases[num_server_aliases] = *alias;
3879 num_server_aliases += 1;
3883 * For a specific domain on the server, fetch all the aliases
3884 * and their members. Add all of them to the server_aliases.
3887 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3888 TALLOC_CTX *mem_ctx,
3889 POLICY_HND *connect_pol,
3890 const DOM_SID *domain_sid)
3892 uint32 start_idx, max_entries, num_entries, i;
3893 struct acct_info *groups;
3894 NTSTATUS result;
3895 POLICY_HND domain_pol;
3897 /* Get domain policy handle */
3899 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, connect_pol,
3900 MAXIMUM_ALLOWED_ACCESS,
3901 domain_sid, &domain_pol);
3902 if (!NT_STATUS_IS_OK(result))
3903 return result;
3905 start_idx = 0;
3906 max_entries = 250;
3908 do {
3909 result = rpccli_samr_enum_als_groups(pipe_hnd, mem_ctx, &domain_pol,
3910 &start_idx, max_entries,
3911 &groups, &num_entries);
3913 for (i = 0; i < num_entries; i++) {
3915 POLICY_HND alias_pol;
3916 struct full_alias alias;
3917 DOM_SID *members;
3918 int j;
3920 result = rpccli_samr_open_alias(pipe_hnd, mem_ctx, &domain_pol,
3921 MAXIMUM_ALLOWED_ACCESS,
3922 groups[i].rid,
3923 &alias_pol);
3924 if (!NT_STATUS_IS_OK(result))
3925 goto done;
3927 result = rpccli_samr_query_aliasmem(pipe_hnd, mem_ctx,
3928 &alias_pol,
3929 &alias.num_members,
3930 &members);
3931 if (!NT_STATUS_IS_OK(result))
3932 goto done;
3934 result = rpccli_samr_close(pipe_hnd, mem_ctx, &alias_pol);
3935 if (!NT_STATUS_IS_OK(result))
3936 goto done;
3938 alias.members = NULL;
3940 if (alias.num_members > 0) {
3941 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3943 for (j = 0; j < alias.num_members; j++)
3944 sid_copy(&alias.members[j],
3945 &members[j]);
3948 sid_copy(&alias.sid, domain_sid);
3949 sid_append_rid(&alias.sid, groups[i].rid);
3951 push_alias(mem_ctx, &alias);
3953 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3955 result = NT_STATUS_OK;
3957 done:
3958 rpccli_samr_close(pipe_hnd, mem_ctx, &domain_pol);
3960 return result;
3964 * Dump server_aliases as names for debugging purposes.
3967 static NTSTATUS rpc_aliaslist_dump(const DOM_SID *domain_sid,
3968 const char *domain_name,
3969 struct cli_state *cli,
3970 struct rpc_pipe_client *pipe_hnd,
3971 TALLOC_CTX *mem_ctx,
3972 int argc,
3973 const char **argv)
3975 int i;
3976 NTSTATUS result;
3977 POLICY_HND lsa_pol;
3979 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, True,
3980 SEC_RIGHTS_MAXIMUM_ALLOWED,
3981 &lsa_pol);
3982 if (!NT_STATUS_IS_OK(result))
3983 return result;
3985 for (i=0; i<num_server_aliases; i++) {
3986 char **names;
3987 char **domains;
3988 uint32 *types;
3989 int j;
3991 struct full_alias *alias = &server_aliases[i];
3993 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3994 &alias->sid,
3995 &domains, &names, &types);
3996 if (!NT_STATUS_IS_OK(result))
3997 continue;
3999 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4001 if (alias->num_members == 0) {
4002 DEBUG(1, ("\n"));
4003 continue;
4006 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4007 alias->num_members,
4008 alias->members,
4009 &domains, &names, &types);
4011 if (!NT_STATUS_IS_OK(result) &&
4012 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4013 continue;
4015 for (j=0; j<alias->num_members; j++)
4016 DEBUG(1, ("%s\\%s (%d); ",
4017 domains[j] ? domains[j] : "*unknown*",
4018 names[j] ? names[j] : "*unknown*",types[j]));
4019 DEBUG(1, ("\n"));
4022 rpccli_lsa_close(pipe_hnd, mem_ctx, &lsa_pol);
4024 return NT_STATUS_OK;
4028 * Fetch a list of all server aliases and their members into
4029 * server_aliases.
4032 static NTSTATUS rpc_aliaslist_internals(const DOM_SID *domain_sid,
4033 const char *domain_name,
4034 struct cli_state *cli,
4035 struct rpc_pipe_client *pipe_hnd,
4036 TALLOC_CTX *mem_ctx,
4037 int argc,
4038 const char **argv)
4040 NTSTATUS result;
4041 POLICY_HND connect_pol;
4043 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4044 &connect_pol);
4046 if (!NT_STATUS_IS_OK(result))
4047 goto done;
4049 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4050 &global_sid_Builtin);
4052 if (!NT_STATUS_IS_OK(result))
4053 goto done;
4055 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4056 domain_sid);
4058 rpccli_samr_close(pipe_hnd, mem_ctx, &connect_pol);
4059 done:
4060 return result;
4063 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4065 token->num_sids = 4;
4067 token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4);
4069 token->user_sids[0] = *user_sid;
4070 sid_copy(&token->user_sids[1], &global_sid_World);
4071 sid_copy(&token->user_sids[2], &global_sid_Network);
4072 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4075 static void free_user_token(NT_USER_TOKEN *token)
4077 SAFE_FREE(token->user_sids);
4080 static BOOL is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4082 int i;
4084 for (i=0; i<token->num_sids; i++) {
4085 if (sid_compare(sid, &token->user_sids[i]) == 0)
4086 return True;
4088 return False;
4091 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4093 if (is_sid_in_token(token, sid))
4094 return;
4096 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4097 if (!token->user_sids) {
4098 return;
4101 sid_copy(&token->user_sids[token->num_sids], sid);
4103 token->num_sids += 1;
4106 struct user_token {
4107 fstring name;
4108 NT_USER_TOKEN token;
4111 static void dump_user_token(struct user_token *token)
4113 int i;
4115 d_printf("%s\n", token->name);
4117 for (i=0; i<token->token.num_sids; i++) {
4118 d_printf(" %s\n", sid_string_static(&token->token.user_sids[i]));
4122 static BOOL is_alias_member(DOM_SID *sid, struct full_alias *alias)
4124 int i;
4126 for (i=0; i<alias->num_members; i++) {
4127 if (sid_compare(sid, &alias->members[i]) == 0)
4128 return True;
4131 return False;
4134 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4136 int i;
4138 for (i=0; i<num_server_aliases; i++) {
4139 if (is_alias_member(&sid, &server_aliases[i]))
4140 add_sid_to_token(token, &server_aliases[i].sid);
4145 * We got a user token with all the SIDs we can know about without asking the
4146 * server directly. These are the user and domain group sids. All of these can
4147 * be members of aliases. So scan the list of aliases for each of the SIDs and
4148 * add them to the token.
4151 static void collect_alias_memberships(NT_USER_TOKEN *token)
4153 int num_global_sids = token->num_sids;
4154 int i;
4156 for (i=0; i<num_global_sids; i++) {
4157 collect_sid_memberships(token, token->user_sids[i]);
4161 static BOOL get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4163 struct winbindd_request request;
4164 struct winbindd_response response;
4165 fstring full_name;
4166 NSS_STATUS result;
4168 DOM_SID user_sid;
4170 int i;
4172 fstr_sprintf(full_name, "%s%c%s",
4173 domain, *lp_winbind_separator(), user);
4175 /* First let's find out the user sid */
4177 ZERO_STRUCT(request);
4178 ZERO_STRUCT(response);
4180 fstrcpy(request.data.name.dom_name, domain);
4181 fstrcpy(request.data.name.name, user);
4183 result = winbindd_request_response(WINBINDD_LOOKUPNAME, &request, &response);
4185 if (result != NSS_STATUS_SUCCESS) {
4186 DEBUG(1, ("winbind could not find %s\n", full_name));
4187 return False;
4190 if (response.data.sid.type != SID_NAME_USER) {
4191 DEBUG(1, ("%s is not a user\n", full_name));
4192 return False;
4195 string_to_sid(&user_sid, response.data.sid.sid);
4197 init_user_token(token, &user_sid);
4199 /* And now the groups winbind knows about */
4201 ZERO_STRUCT(response);
4203 fstrcpy(request.data.username, full_name);
4205 result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response);
4207 if (result != NSS_STATUS_SUCCESS) {
4208 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
4209 return False;
4212 for (i = 0; i < response.data.num_entries; i++) {
4213 gid_t gid = ((gid_t *)response.extra_data)[i];
4214 DOM_SID sid;
4216 struct winbindd_request sidrequest;
4217 struct winbindd_response sidresponse;
4219 ZERO_STRUCT(sidrequest);
4220 ZERO_STRUCT(sidresponse);
4222 sidrequest.data.gid = gid;
4224 result = winbindd_request_response(WINBINDD_GID_TO_SID,
4225 &sidrequest, &sidresponse);
4227 if (result != NSS_STATUS_SUCCESS) {
4228 DEBUG(1, ("winbind could not find SID of gid %d\n",
4229 gid));
4230 return False;
4233 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
4235 string_to_sid(&sid, sidresponse.data.sid.sid);
4236 add_sid_to_token(token, &sid);
4239 SAFE_FREE(response.extra_data);
4241 return True;
4245 * Get a list of all user tokens we want to look at
4248 static BOOL get_user_tokens(int *num_tokens, struct user_token **user_tokens)
4250 struct winbindd_request request;
4251 struct winbindd_response response;
4252 const char *extra_data;
4253 fstring name;
4254 int i;
4255 struct user_token *result;
4257 if (lp_winbind_use_default_domain() &&
4258 (opt_target_workgroup == NULL)) {
4259 d_fprintf(stderr, "winbind use default domain = yes set, "
4260 "please specify a workgroup\n");
4261 return False;
4264 /* Send request to winbind daemon */
4266 ZERO_STRUCT(request);
4267 ZERO_STRUCT(response);
4269 if (winbindd_request_response(WINBINDD_LIST_USERS, &request, &response) !=
4270 NSS_STATUS_SUCCESS)
4271 return False;
4273 /* Look through extra data */
4275 if (!response.extra_data)
4276 return False;
4278 extra_data = (const char *)response.extra_data;
4279 *num_tokens = 0;
4281 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4282 *num_tokens += 1;
4285 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
4287 if (result == NULL) {
4288 DEBUG(1, ("Could not malloc sid array\n"));
4289 return False;
4292 extra_data = (const char *)response.extra_data;
4293 i=0;
4295 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
4297 fstring domain, user;
4298 char *p;
4300 fstrcpy(result[i].name, name);
4302 p = strchr(name, *lp_winbind_separator());
4304 DEBUG(3, ("%s\n", name));
4306 if (p == NULL) {
4307 fstrcpy(domain, opt_target_workgroup);
4308 fstrcpy(user, name);
4309 } else {
4310 *p++ = '\0';
4311 fstrcpy(domain, name);
4312 strupper_m(domain);
4313 fstrcpy(user, p);
4316 get_user_sids(domain, user, &(result[i].token));
4317 i+=1;
4320 SAFE_FREE(response.extra_data);
4322 *user_tokens = result;
4324 return True;
4327 static BOOL get_user_tokens_from_file(FILE *f,
4328 int *num_tokens,
4329 struct user_token **tokens)
4331 struct user_token *token = NULL;
4333 while (!feof(f)) {
4334 fstring line;
4336 if (fgets(line, sizeof(line)-1, f) == NULL) {
4337 return True;
4340 if (line[strlen(line)-1] == '\n')
4341 line[strlen(line)-1] = '\0';
4343 if (line[0] == ' ') {
4344 /* We have a SID */
4346 DOM_SID sid;
4347 string_to_sid(&sid, &line[1]);
4349 if (token == NULL) {
4350 DEBUG(0, ("File does not begin with username"));
4351 return False;
4354 add_sid_to_token(&token->token, &sid);
4355 continue;
4358 /* And a new user... */
4360 *num_tokens += 1;
4361 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4362 if (*tokens == NULL) {
4363 DEBUG(0, ("Could not realloc tokens\n"));
4364 return False;
4367 token = &((*tokens)[*num_tokens-1]);
4369 fstrcpy(token->name, line);
4370 token->token.num_sids = 0;
4371 token->token.user_sids = NULL;
4372 continue;
4375 return False;
4380 * Show the list of all users that have access to a share
4383 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4384 TALLOC_CTX *mem_ctx,
4385 const char *netname,
4386 int num_tokens,
4387 struct user_token *tokens)
4389 int fnum;
4390 SEC_DESC *share_sd = NULL;
4391 SEC_DESC *root_sd = NULL;
4392 struct cli_state *cli = pipe_hnd->cli;
4393 int i;
4394 SRV_SHARE_INFO info;
4395 WERROR result;
4396 uint16 cnum;
4398 result = rpccli_srvsvc_net_share_get_info(pipe_hnd, mem_ctx, netname,
4399 502, &info);
4401 if (!W_ERROR_IS_OK(result)) {
4402 DEBUG(1, ("Coult not query secdesc for share %s\n",
4403 netname));
4404 return;
4407 share_sd = info.share.info502.info_502_str.sd;
4408 if (share_sd == NULL) {
4409 DEBUG(1, ("Got no secdesc for share %s\n",
4410 netname));
4413 cnum = cli->cnum;
4415 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4416 return;
4419 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4421 if (fnum != -1) {
4422 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4425 for (i=0; i<num_tokens; i++) {
4426 uint32 acc_granted;
4427 NTSTATUS status;
4429 if (share_sd != NULL) {
4430 if (!se_access_check(share_sd, &tokens[i].token,
4431 1, &acc_granted, &status)) {
4432 DEBUG(1, ("Could not check share_sd for "
4433 "user %s\n",
4434 tokens[i].name));
4435 continue;
4438 if (!NT_STATUS_IS_OK(status))
4439 continue;
4442 if (root_sd == NULL) {
4443 d_printf(" %s\n", tokens[i].name);
4444 continue;
4447 if (!se_access_check(root_sd, &tokens[i].token,
4448 1, &acc_granted, &status)) {
4449 DEBUG(1, ("Could not check root_sd for user %s\n",
4450 tokens[i].name));
4451 continue;
4454 if (!NT_STATUS_IS_OK(status))
4455 continue;
4457 d_printf(" %s\n", tokens[i].name);
4460 if (fnum != -1)
4461 cli_close(cli, fnum);
4462 cli_tdis(cli);
4463 cli->cnum = cnum;
4465 return;
4468 struct share_list {
4469 int num_shares;
4470 char **shares;
4473 static void collect_share(const char *name, uint32 m,
4474 const char *comment, void *state)
4476 struct share_list *share_list = (struct share_list *)state;
4478 if (m != STYPE_DISKTREE)
4479 return;
4481 share_list->num_shares += 1;
4482 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4483 if (!share_list->shares) {
4484 share_list->num_shares = 0;
4485 return;
4487 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4490 static void rpc_share_userlist_usage(void)
4492 return;
4495 /**
4496 * List shares on a remote RPC server, including the security descriptors
4498 * All parameters are provided by the run_rpc_command function, except for
4499 * argc, argv which are passes through.
4501 * @param domain_sid The domain sid acquired from the remote server
4502 * @param cli A cli_state connected to the server.
4503 * @param mem_ctx Talloc context, destoyed on completion of the function.
4504 * @param argc Standard main() style argc
4505 * @param argv Standard main() style argv. Initial components are already
4506 * stripped
4508 * @return Normal NTSTATUS return.
4511 static NTSTATUS rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
4512 const char *domain_name,
4513 struct cli_state *cli,
4514 struct rpc_pipe_client *pipe_hnd,
4515 TALLOC_CTX *mem_ctx,
4516 int argc,
4517 const char **argv)
4519 int ret;
4520 BOOL r;
4521 ENUM_HND hnd;
4522 uint32 i;
4523 FILE *f;
4525 struct user_token *tokens = NULL;
4526 int num_tokens = 0;
4528 struct share_list share_list;
4530 if (argc > 1) {
4531 rpc_share_userlist_usage();
4532 return NT_STATUS_UNSUCCESSFUL;
4535 if (argc == 0) {
4536 f = stdin;
4537 } else {
4538 f = fopen(argv[0], "r");
4541 if (f == NULL) {
4542 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4543 return NT_STATUS_UNSUCCESSFUL;
4546 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4548 if (f != stdin)
4549 fclose(f);
4551 if (!r) {
4552 DEBUG(0, ("Could not read users from file\n"));
4553 return NT_STATUS_UNSUCCESSFUL;
4556 for (i=0; i<num_tokens; i++)
4557 collect_alias_memberships(&tokens[i].token);
4559 init_enum_hnd(&hnd, 0);
4561 share_list.num_shares = 0;
4562 share_list.shares = NULL;
4564 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4566 if (ret == -1) {
4567 DEBUG(0, ("Error returning browse list: %s\n",
4568 cli_errstr(cli)));
4569 goto done;
4572 for (i = 0; i < share_list.num_shares; i++) {
4573 char *netname = share_list.shares[i];
4575 if (netname[strlen(netname)-1] == '$')
4576 continue;
4578 d_printf("%s\n", netname);
4580 show_userlist(pipe_hnd, mem_ctx, netname,
4581 num_tokens, tokens);
4583 done:
4584 for (i=0; i<num_tokens; i++) {
4585 free_user_token(&tokens[i].token);
4587 SAFE_FREE(tokens);
4588 SAFE_FREE(share_list.shares);
4590 return NT_STATUS_OK;
4593 static int rpc_share_allowedusers(int argc, const char **argv)
4595 int result;
4597 result = run_rpc_command(NULL, PI_SAMR, 0,
4598 rpc_aliaslist_internals,
4599 argc, argv);
4600 if (result != 0)
4601 return result;
4603 result = run_rpc_command(NULL, PI_LSARPC, 0,
4604 rpc_aliaslist_dump,
4605 argc, argv);
4606 if (result != 0)
4607 return result;
4609 return run_rpc_command(NULL, PI_SRVSVC, 0,
4610 rpc_share_allowedusers_internals,
4611 argc, argv);
4614 int net_usersidlist(int argc, const char **argv)
4616 int num_tokens = 0;
4617 struct user_token *tokens = NULL;
4618 int i;
4620 if (argc != 0) {
4621 net_usersidlist_usage(argc, argv);
4622 return 0;
4625 if (!get_user_tokens(&num_tokens, &tokens)) {
4626 DEBUG(0, ("Could not get the user/sid list\n"));
4627 return 0;
4630 for (i=0; i<num_tokens; i++) {
4631 dump_user_token(&tokens[i]);
4632 free_user_token(&tokens[i].token);
4635 SAFE_FREE(tokens);
4636 return 1;
4639 int net_usersidlist_usage(int argc, const char **argv)
4641 d_printf("net usersidlist\n"
4642 "\tprints out a list of all users the running winbind knows\n"
4643 "\tabout, together with all their SIDs. This is used as\n"
4644 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4646 net_common_flags_usage(argc, argv);
4647 return -1;
4650 /**
4651 * 'net rpc share' entrypoint.
4652 * @param argc Standard main() style argc
4653 * @param argv Standard main() style argv. Initial components are already
4654 * stripped
4657 int net_rpc_share(int argc, const char **argv)
4659 struct functable func[] = {
4660 {"add", rpc_share_add},
4661 {"delete", rpc_share_delete},
4662 {"allowedusers", rpc_share_allowedusers},
4663 {"migrate", rpc_share_migrate},
4664 {"list", rpc_share_list},
4665 {NULL, NULL}
4668 if (argc == 0)
4669 return run_rpc_command(NULL, PI_SRVSVC, 0,
4670 rpc_share_list_internals,
4671 argc, argv);
4673 return net_run_function(argc, argv, func, rpc_share_usage);
4676 static NTSTATUS rpc_sh_share_list(TALLOC_CTX *mem_ctx,
4677 struct rpc_sh_ctx *ctx,
4678 struct rpc_pipe_client *pipe_hnd,
4679 int argc, const char **argv)
4681 return rpc_share_list_internals(ctx->domain_sid, ctx->domain_name,
4682 ctx->cli, pipe_hnd, mem_ctx,
4683 argc, argv);
4686 static NTSTATUS rpc_sh_share_add(TALLOC_CTX *mem_ctx,
4687 struct rpc_sh_ctx *ctx,
4688 struct rpc_pipe_client *pipe_hnd,
4689 int argc, const char **argv)
4691 WERROR result;
4693 if ((argc < 2) || (argc > 3)) {
4694 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4695 ctx->whoami);
4696 return NT_STATUS_INVALID_PARAMETER;
4699 result = rpccli_srvsvc_net_share_add(
4700 pipe_hnd, mem_ctx, argv[0], STYPE_DISKTREE,
4701 (argc == 3) ? argv[2] : "",
4702 0, 0, 0, argv[1], NULL, 2, NULL);
4704 return werror_to_ntstatus(result);
4707 static NTSTATUS rpc_sh_share_delete(TALLOC_CTX *mem_ctx,
4708 struct rpc_sh_ctx *ctx,
4709 struct rpc_pipe_client *pipe_hnd,
4710 int argc, const char **argv)
4712 WERROR result;
4714 if (argc != 1) {
4715 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4716 return NT_STATUS_INVALID_PARAMETER;
4719 result = rpccli_srvsvc_net_share_del(pipe_hnd, mem_ctx, argv[0]);
4720 return werror_to_ntstatus(result);
4723 static NTSTATUS rpc_sh_share_info(TALLOC_CTX *mem_ctx,
4724 struct rpc_sh_ctx *ctx,
4725 struct rpc_pipe_client *pipe_hnd,
4726 int argc, const char **argv)
4728 SRV_SHARE_INFO info;
4729 SRV_SHARE_INFO_2 *info2 = &info.share.info2;
4730 WERROR result;
4732 if (argc != 1) {
4733 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4734 return NT_STATUS_INVALID_PARAMETER;
4737 result = rpccli_srvsvc_net_share_get_info(
4738 pipe_hnd, mem_ctx, argv[0], 2, &info);
4739 if (!W_ERROR_IS_OK(result)) {
4740 goto done;
4743 d_printf("Name: %s\n",
4744 rpcstr_pull_unistr2_talloc(mem_ctx,
4745 &info2->info_2_str.uni_netname));
4746 d_printf("Comment: %s\n",
4747 rpcstr_pull_unistr2_talloc(mem_ctx,
4748 &info2->info_2_str.uni_remark));
4750 d_printf("Path: %s\n",
4751 rpcstr_pull_unistr2_talloc(mem_ctx,
4752 &info2->info_2_str.uni_path));
4753 d_printf("Password: %s\n",
4754 rpcstr_pull_unistr2_talloc(mem_ctx,
4755 &info2->info_2_str.uni_passwd));
4757 done:
4758 return werror_to_ntstatus(result);
4761 struct rpc_sh_cmd *net_rpc_share_cmds(TALLOC_CTX *mem_ctx,
4762 struct rpc_sh_ctx *ctx)
4764 static struct rpc_sh_cmd cmds[] = {
4766 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
4767 "List available shares" },
4769 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
4770 "Add a share" },
4772 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
4773 "Delete a share" },
4775 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
4776 "Get information about a share" },
4778 { NULL, NULL, 0, NULL, NULL }
4781 return cmds;
4784 /****************************************************************************/
4786 static int rpc_file_usage(int argc, const char **argv)
4788 return net_help_file(argc, argv);
4791 /**
4792 * Close a file on a remote RPC server
4794 * All parameters are provided by the run_rpc_command function, except for
4795 * argc, argv which are passes through.
4797 * @param domain_sid The domain sid acquired from the remote server
4798 * @param cli A cli_state connected to the server.
4799 * @param mem_ctx Talloc context, destoyed on completion of the function.
4800 * @param argc Standard main() style argc
4801 * @param argv Standard main() style argv. Initial components are already
4802 * stripped
4804 * @return Normal NTSTATUS return.
4806 static NTSTATUS rpc_file_close_internals(const DOM_SID *domain_sid,
4807 const char *domain_name,
4808 struct cli_state *cli,
4809 struct rpc_pipe_client *pipe_hnd,
4810 TALLOC_CTX *mem_ctx,
4811 int argc,
4812 const char **argv)
4814 WERROR result;
4815 result = rpccli_srvsvc_net_file_close(pipe_hnd, mem_ctx, atoi(argv[0]));
4816 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4819 /**
4820 * Close a file on a remote RPC server
4822 * @param argc Standard main() style argc
4823 * @param argv Standard main() style argv. Initial components are already
4824 * stripped
4826 * @return A shell status integer (0 for success)
4828 static int rpc_file_close(int argc, const char **argv)
4830 if (argc < 1) {
4831 DEBUG(1, ("No fileid given on close\n"));
4832 return(rpc_file_usage(argc, argv));
4835 return run_rpc_command(NULL, PI_SRVSVC, 0,
4836 rpc_file_close_internals,
4837 argc, argv);
4840 /**
4841 * Formatted print of open file info
4843 * @param info3 FILE_INFO_3 contents
4844 * @param str3 strings for FILE_INFO_3
4847 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
4849 fstring user = "", path = "";
4851 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
4852 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
4854 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4855 info3->id, user, info3->perms, info3->num_locks, path);
4858 /**
4859 * List open files on a remote RPC server
4861 * All parameters are provided by the run_rpc_command function, except for
4862 * argc, argv which are passes through.
4864 * @param domain_sid The domain sid acquired from the remote server
4865 * @param cli A cli_state connected to the server.
4866 * @param mem_ctx Talloc context, destoyed on completion of the function.
4867 * @param argc Standard main() style argc
4868 * @param argv Standard main() style argv. Initial components are already
4869 * stripped
4871 * @return Normal NTSTATUS return.
4874 static NTSTATUS rpc_file_list_internals(const DOM_SID *domain_sid,
4875 const char *domain_name,
4876 struct cli_state *cli,
4877 struct rpc_pipe_client *pipe_hnd,
4878 TALLOC_CTX *mem_ctx,
4879 int argc,
4880 const char **argv)
4882 SRV_FILE_INFO_CTR ctr;
4883 WERROR result;
4884 ENUM_HND hnd;
4885 uint32 preferred_len = 0xffffffff, i;
4886 const char *username=NULL;
4888 init_enum_hnd(&hnd, 0);
4890 /* if argc > 0, must be user command */
4891 if (argc > 0)
4892 username = smb_xstrdup(argv[0]);
4894 result = rpccli_srvsvc_net_file_enum(pipe_hnd,
4895 mem_ctx, 3, username, &ctr, preferred_len, &hnd);
4897 if (!W_ERROR_IS_OK(result))
4898 goto done;
4900 /* Display results */
4902 d_printf(
4903 "\nEnumerating open files on remote server:\n\n"\
4904 "\nFileId Opened by Perms Locks Path"\
4905 "\n------ --------- ----- ----- ---- \n");
4906 for (i = 0; i < ctr.num_entries; i++)
4907 display_file_info_3(&ctr.file.info3[i].info_3,
4908 &ctr.file.info3[i].info_3_str);
4909 done:
4910 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4913 /**
4914 * List files for a user on a remote RPC server
4916 * @param argc Standard main() style argc
4917 * @param argv Standard main() style argv. Initial components are already
4918 * stripped
4920 * @return A shell status integer (0 for success)
4923 static int rpc_file_user(int argc, const char **argv)
4925 if (argc < 1) {
4926 DEBUG(1, ("No username given\n"));
4927 return(rpc_file_usage(argc, argv));
4930 return run_rpc_command(NULL, PI_SRVSVC, 0,
4931 rpc_file_list_internals,
4932 argc, argv);
4935 /**
4936 * 'net rpc file' entrypoint.
4937 * @param argc Standard main() style argc
4938 * @param argv Standard main() style argv. Initial components are already
4939 * stripped
4942 int net_rpc_file(int argc, const char **argv)
4944 struct functable func[] = {
4945 {"close", rpc_file_close},
4946 {"user", rpc_file_user},
4947 #if 0
4948 {"info", rpc_file_info},
4949 #endif
4950 {NULL, NULL}
4953 if (argc == 0)
4954 return run_rpc_command(NULL, PI_SRVSVC, 0,
4955 rpc_file_list_internals,
4956 argc, argv);
4958 return net_run_function(argc, argv, func, rpc_file_usage);
4961 /**
4962 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
4964 * All parameters are provided by the run_rpc_command function, except for
4965 * argc, argv which are passed through.
4967 * @param domain_sid The domain sid aquired from the remote server
4968 * @param cli A cli_state connected to the server.
4969 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4970 * @param argc Standard main() style argc
4971 * @param argv Standard main() style argv. Initial components are already
4972 * stripped
4974 * @return Normal NTSTATUS return.
4977 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
4978 const char *domain_name,
4979 struct cli_state *cli,
4980 struct rpc_pipe_client *pipe_hnd,
4981 TALLOC_CTX *mem_ctx,
4982 int argc,
4983 const char **argv)
4985 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4987 result = rpccli_shutdown_abort(pipe_hnd, mem_ctx);
4989 if (NT_STATUS_IS_OK(result)) {
4990 d_printf("\nShutdown successfully aborted\n");
4991 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4992 } else
4993 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4995 return result;
4998 /**
4999 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5001 * All parameters are provided by the run_rpc_command function, except for
5002 * argc, argv which are passed through.
5004 * @param domain_sid The domain sid aquired from the remote server
5005 * @param cli A cli_state connected to the server.
5006 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5007 * @param argc Standard main() style argc
5008 * @param argv Standard main() style argv. Initial components are already
5009 * stripped
5011 * @return Normal NTSTATUS return.
5014 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
5015 const char *domain_name,
5016 struct cli_state *cli,
5017 struct rpc_pipe_client *pipe_hnd,
5018 TALLOC_CTX *mem_ctx,
5019 int argc,
5020 const char **argv)
5022 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5024 result = werror_to_ntstatus(rpccli_reg_abort_shutdown(pipe_hnd, mem_ctx));
5026 if (NT_STATUS_IS_OK(result)) {
5027 d_printf("\nShutdown successfully aborted\n");
5028 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5029 } else
5030 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5032 return result;
5035 /**
5036 * ABORT the Shut down of a remote RPC server
5038 * @param argc Standard main() style argc
5039 * @param argv Standard main() style argv. Initial components are already
5040 * stripped
5042 * @return A shell status integer (0 for success)
5045 static int rpc_shutdown_abort(int argc, const char **argv)
5047 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
5048 rpc_shutdown_abort_internals,
5049 argc, argv);
5051 if (rc == 0)
5052 return rc;
5054 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5056 return run_rpc_command(NULL, PI_WINREG, 0,
5057 rpc_reg_shutdown_abort_internals,
5058 argc, argv);
5061 /**
5062 * Shut down a remote RPC Server via initshutdown pipe
5064 * All parameters are provided by the run_rpc_command function, except for
5065 * argc, argv which are passes through.
5067 * @param domain_sid The domain sid aquired from the remote server
5068 * @param cli A cli_state connected to the server.
5069 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5070 * @param argc Standard main() style argc
5071 * @param argc Standard main() style argv. Initial components are already
5072 * stripped
5074 * @return Normal NTSTATUS return.
5077 static NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
5078 const char *domain_name,
5079 struct cli_state *cli,
5080 struct rpc_pipe_client *pipe_hnd,
5081 TALLOC_CTX *mem_ctx,
5082 int argc,
5083 const char **argv)
5085 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5086 const char *msg = "This machine will be shutdown shortly";
5087 uint32 timeout = 20;
5089 if (opt_comment) {
5090 msg = opt_comment;
5092 if (opt_timeout) {
5093 timeout = opt_timeout;
5096 /* create an entry */
5097 result = rpccli_shutdown_init(pipe_hnd, mem_ctx, msg, timeout, opt_reboot,
5098 opt_force);
5100 if (NT_STATUS_IS_OK(result)) {
5101 d_printf("\nShutdown of remote machine succeeded\n");
5102 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5103 } else {
5104 DEBUG(1,("Shutdown of remote machine failed!\n"));
5106 return result;
5109 /**
5110 * Shut down a remote RPC Server via winreg pipe
5112 * All parameters are provided by the run_rpc_command function, except for
5113 * argc, argv which are passes through.
5115 * @param domain_sid The domain sid aquired from the remote server
5116 * @param cli A cli_state connected to the server.
5117 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5118 * @param argc Standard main() style argc
5119 * @param argc Standard main() style argv. Initial components are already
5120 * stripped
5122 * @return Normal NTSTATUS return.
5125 static NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
5126 const char *domain_name,
5127 struct cli_state *cli,
5128 struct rpc_pipe_client *pipe_hnd,
5129 TALLOC_CTX *mem_ctx,
5130 int argc,
5131 const char **argv)
5133 WERROR result;
5134 const char *msg = "This machine will be shutdown shortly";
5135 uint32 timeout = 20;
5136 #if 0
5137 poptContext pc;
5138 int rc;
5140 struct poptOption long_options[] = {
5141 {"message", 'm', POPT_ARG_STRING, &msg},
5142 {"timeout", 't', POPT_ARG_INT, &timeout},
5143 {"reboot", 'r', POPT_ARG_NONE, &reboot},
5144 {"force", 'f', POPT_ARG_NONE, &force},
5145 { 0, 0, 0, 0}
5148 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
5149 POPT_CONTEXT_KEEP_FIRST);
5151 rc = poptGetNextOpt(pc);
5153 if (rc < -1) {
5154 /* an error occurred during option processing */
5155 DEBUG(0, ("%s: %s\n",
5156 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
5157 poptStrerror(rc)));
5158 return NT_STATUS_INVALID_PARAMETER;
5160 #endif
5161 if (opt_comment) {
5162 msg = opt_comment;
5164 if (opt_timeout) {
5165 timeout = opt_timeout;
5168 /* create an entry */
5169 result = rpccli_reg_shutdown(pipe_hnd, mem_ctx, msg, timeout, opt_reboot, opt_force);
5171 if (W_ERROR_IS_OK(result)) {
5172 d_printf("\nShutdown of remote machine succeeded\n");
5173 } else {
5174 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5175 if (W_ERROR_EQUAL(result,WERR_MACHINE_LOCKED))
5176 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5177 else
5178 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(result));
5181 return werror_to_ntstatus(result);
5184 /**
5185 * Shut down a remote RPC server
5187 * @param argc Standard main() style argc
5188 * @param argc Standard main() style argv. Initial components are already
5189 * stripped
5191 * @return A shell status integer (0 for success)
5194 static int rpc_shutdown(int argc, const char **argv)
5196 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
5197 rpc_init_shutdown_internals,
5198 argc, argv);
5200 if (rc) {
5201 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5202 rc = run_rpc_command(NULL, PI_WINREG, 0,
5203 rpc_reg_shutdown_internals, argc, argv);
5206 return rc;
5209 /***************************************************************************
5210 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5212 ***************************************************************************/
5215 * Add interdomain trust account to the RPC server.
5216 * All parameters (except for argc and argv) are passed by run_rpc_command
5217 * function.
5219 * @param domain_sid The domain sid acquired from the server
5220 * @param cli A cli_state connected to the server.
5221 * @param mem_ctx Talloc context, destoyed on completion of the function.
5222 * @param argc Standard main() style argc
5223 * @param argc Standard main() style argv. Initial components are already
5224 * stripped
5226 * @return normal NTSTATUS return code
5229 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
5230 const char *domain_name,
5231 struct cli_state *cli,
5232 struct rpc_pipe_client *pipe_hnd,
5233 TALLOC_CTX *mem_ctx,
5234 int argc,
5235 const char **argv)
5237 POLICY_HND connect_pol, domain_pol, user_pol;
5238 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5239 char *acct_name;
5240 uint32 acb_info;
5241 uint32 unknown, user_rid;
5243 if (argc != 2) {
5244 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5245 return NT_STATUS_INVALID_PARAMETER;
5249 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5252 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5253 return NT_STATUS_NO_MEMORY;
5256 strupper_m(acct_name);
5258 /* Get samr policy handle */
5259 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5260 &connect_pol);
5261 if (!NT_STATUS_IS_OK(result)) {
5262 goto done;
5265 /* Get domain policy handle */
5266 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5267 MAXIMUM_ALLOWED_ACCESS,
5268 domain_sid, &domain_pol);
5269 if (!NT_STATUS_IS_OK(result)) {
5270 goto done;
5273 /* Create trusting domain's account */
5274 acb_info = ACB_NORMAL;
5275 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
5276 mimir: yes, most probably it is */
5278 result = rpccli_samr_create_dom_user(pipe_hnd, mem_ctx, &domain_pol,
5279 acct_name, acb_info, unknown,
5280 &user_pol, &user_rid);
5281 if (!NT_STATUS_IS_OK(result)) {
5282 goto done;
5286 SAM_USERINFO_CTR ctr;
5287 SAM_USER_INFO_23 p23;
5288 NTTIME notime;
5289 char nostr[] = "";
5290 LOGON_HRS hrs;
5291 uchar pwbuf[516];
5293 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5295 ZERO_STRUCT(ctr);
5296 ZERO_STRUCT(p23);
5297 ZERO_STRUCT(notime);
5298 hrs.max_len = 1260;
5299 hrs.offset = 0;
5300 hrs.len = 21;
5301 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
5302 acb_info = ACB_DOMTRUST;
5304 init_sam_user_info23A(&p23, &notime, &notime, &notime,
5305 &notime, &notime, &notime,
5306 nostr, nostr, nostr, nostr, nostr,
5307 nostr, nostr, nostr, nostr, nostr,
5308 0, 0, acb_info, ACCT_FLAGS, 168, &hrs,
5309 0, 0, (char *)pwbuf);
5310 ctr.switch_value = 23;
5311 ctr.info.id23 = &p23;
5312 p23.passmustchange = 0;
5314 result = rpccli_samr_set_userinfo(pipe_hnd, mem_ctx, &user_pol, 23,
5315 &cli->user_session_key, &ctr);
5317 if (!NT_STATUS_IS_OK(result)) {
5318 DEBUG(0,("Could not set trust account password: %s\n",
5319 nt_errstr(result)));
5320 goto done;
5324 done:
5325 SAFE_FREE(acct_name);
5326 return result;
5330 * Create interdomain trust account for a remote domain.
5332 * @param argc standard argc
5333 * @param argv standard argv without initial components
5335 * @return Integer status (0 means success)
5338 static int rpc_trustdom_add(int argc, const char **argv)
5340 if (argc > 0) {
5341 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
5342 argc, argv);
5343 } else {
5344 d_printf("Usage: net rpc trustdom add <domain>\n");
5345 return -1;
5351 * Remove interdomain trust account from the RPC server.
5352 * All parameters (except for argc and argv) are passed by run_rpc_command
5353 * function.
5355 * @param domain_sid The domain sid acquired from the server
5356 * @param cli A cli_state connected to the server.
5357 * @param mem_ctx Talloc context, destoyed on completion of the function.
5358 * @param argc Standard main() style argc
5359 * @param argc Standard main() style argv. Initial components are already
5360 * stripped
5362 * @return normal NTSTATUS return code
5365 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
5366 const char *domain_name,
5367 struct cli_state *cli,
5368 struct rpc_pipe_client *pipe_hnd,
5369 TALLOC_CTX *mem_ctx,
5370 int argc,
5371 const char **argv)
5373 POLICY_HND connect_pol, domain_pol, user_pol;
5374 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5375 char *acct_name;
5376 const char **names;
5377 DOM_SID trust_acct_sid;
5378 uint32 *user_rids, num_rids, *name_types;
5379 uint32 flags = 0x000003e8; /* Unknown */
5381 if (argc != 1) {
5382 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5383 return NT_STATUS_INVALID_PARAMETER;
5387 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5389 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5391 if (acct_name == NULL)
5392 return NT_STATUS_NO_MEMORY;
5394 strupper_m(acct_name);
5396 names = TALLOC_ARRAY(mem_ctx, const char *, 1);
5397 names[0] = acct_name;
5400 /* Get samr policy handle */
5401 result = rpccli_samr_connect(pipe_hnd, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
5402 &connect_pol);
5403 if (!NT_STATUS_IS_OK(result)) {
5404 goto done;
5407 /* Get domain policy handle */
5408 result = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_pol,
5409 MAXIMUM_ALLOWED_ACCESS,
5410 domain_sid, &domain_pol);
5411 if (!NT_STATUS_IS_OK(result)) {
5412 goto done;
5415 result = rpccli_samr_lookup_names(pipe_hnd, mem_ctx, &domain_pol, flags, 1,
5416 names, &num_rids,
5417 &user_rids, &name_types);
5419 if (!NT_STATUS_IS_OK(result)) {
5420 goto done;
5423 result = rpccli_samr_open_user(pipe_hnd, mem_ctx, &domain_pol,
5424 MAXIMUM_ALLOWED_ACCESS,
5425 user_rids[0], &user_pol);
5427 if (!NT_STATUS_IS_OK(result)) {
5428 goto done;
5431 /* append the rid to the domain sid */
5432 sid_copy(&trust_acct_sid, domain_sid);
5433 if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
5434 goto done;
5437 /* remove the sid */
5439 result = rpccli_samr_remove_sid_foreign_domain(pipe_hnd, mem_ctx, &user_pol,
5440 &trust_acct_sid);
5442 if (!NT_STATUS_IS_OK(result)) {
5443 goto done;
5446 /* Delete user */
5448 result = rpccli_samr_delete_dom_user(pipe_hnd, mem_ctx, &user_pol);
5450 if (!NT_STATUS_IS_OK(result)) {
5451 goto done;
5454 if (!NT_STATUS_IS_OK(result)) {
5455 DEBUG(0,("Could not set trust account password: %s\n",
5456 nt_errstr(result)));
5457 goto done;
5460 done:
5461 return result;
5465 * Delete interdomain trust account for a remote domain.
5467 * @param argc standard argc
5468 * @param argv standard argv without initial components
5470 * @return Integer status (0 means success)
5473 static int rpc_trustdom_del(int argc, const char **argv)
5475 if (argc > 0) {
5476 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
5477 argc, argv);
5478 } else {
5479 d_printf("Usage: net rpc trustdom del <domain>\n");
5480 return -1;
5486 * Establish trust relationship to a trusting domain.
5487 * Interdomain account must already be created on remote PDC.
5489 * @param argc standard argc
5490 * @param argv standard argv without initial components
5492 * @return Integer status (0 means success)
5495 static int rpc_trustdom_establish(int argc, const char **argv)
5497 struct cli_state *cli = NULL;
5498 struct in_addr server_ip;
5499 struct rpc_pipe_client *pipe_hnd = NULL;
5500 POLICY_HND connect_hnd;
5501 TALLOC_CTX *mem_ctx;
5502 NTSTATUS nt_status;
5503 DOM_SID *domain_sid;
5505 char* domain_name;
5506 char* domain_name_pol;
5507 char* acct_name;
5508 fstring pdc_name;
5511 * Connect to \\server\ipc$ as 'our domain' account with password
5514 if (argc != 1) {
5515 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5516 return -1;
5519 domain_name = smb_xstrdup(argv[0]);
5520 strupper_m(domain_name);
5522 /* account name used at first is our domain's name with '$' */
5523 asprintf(&acct_name, "%s$", lp_workgroup());
5524 strupper_m(acct_name);
5527 * opt_workgroup will be used by connection functions further,
5528 * hence it should be set to remote domain name instead of ours
5530 if (opt_workgroup) {
5531 opt_workgroup = smb_xstrdup(domain_name);
5534 opt_user_name = acct_name;
5536 /* find the domain controller */
5537 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
5538 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5539 return -1;
5542 /* connect to ipc$ as username/password */
5543 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
5544 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5546 /* Is it trusting domain account for sure ? */
5547 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5548 nt_errstr(nt_status)));
5549 return -1;
5552 /* store who we connected to */
5554 saf_store( domain_name, pdc_name );
5557 * Connect to \\server\ipc$ again (this time anonymously)
5560 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
5562 if (NT_STATUS_IS_ERR(nt_status)) {
5563 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5564 domain_name, nt_errstr(nt_status)));
5568 * Use NetServerEnum2 to make sure we're talking to a proper server
5571 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
5572 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
5573 for domain %s\n", domain_name));
5576 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5577 "domain %s", domain_name))) {
5578 DEBUG(0, ("talloc_init() failed\n"));
5579 cli_shutdown(cli);
5580 return -1;
5584 * Call LsaOpenPolicy and LsaQueryInfo
5587 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5588 if (!pipe_hnd) {
5589 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5590 cli_shutdown(cli);
5591 return -1;
5594 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
5595 &connect_hnd);
5596 if (NT_STATUS_IS_ERR(nt_status)) {
5597 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5598 nt_errstr(nt_status)));
5599 cli_shutdown(cli);
5600 return -1;
5603 /* Querying info level 5 */
5605 nt_status = rpccli_lsa_query_info_policy(pipe_hnd, mem_ctx, &connect_hnd,
5606 5 /* info level */,
5607 &domain_name_pol, &domain_sid);
5608 if (NT_STATUS_IS_ERR(nt_status)) {
5609 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5610 nt_errstr(nt_status)));
5611 cli_shutdown(cli);
5612 return -1;
5615 /* There should be actually query info level 3 (following nt serv behaviour),
5616 but I still don't know if it's _really_ necessary */
5619 * Store the password in secrets db
5622 if (!secrets_store_trusted_domain_password(domain_name,
5623 opt_password,
5624 domain_sid)) {
5625 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5626 cli_shutdown(cli);
5627 return -1;
5631 * Close the pipes and clean up
5634 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
5635 if (NT_STATUS_IS_ERR(nt_status)) {
5636 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5637 nt_errstr(nt_status)));
5638 cli_shutdown(cli);
5639 return -1;
5642 cli_shutdown(cli);
5644 talloc_destroy(mem_ctx);
5646 d_printf("Trust to domain %s established\n", domain_name);
5647 return 0;
5651 * Revoke trust relationship to the remote domain
5653 * @param argc standard argc
5654 * @param argv standard argv without initial components
5656 * @return Integer status (0 means success)
5659 static int rpc_trustdom_revoke(int argc, const char **argv)
5661 char* domain_name;
5663 if (argc < 1) return -1;
5665 /* generate upper cased domain name */
5666 domain_name = smb_xstrdup(argv[0]);
5667 strupper_m(domain_name);
5669 /* delete password of the trust */
5670 if (!trusted_domain_password_delete(domain_name)) {
5671 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5672 domain_name));
5673 return -1;
5676 return 0;
5680 * Usage for 'net rpc trustdom' command
5682 * @param argc standard argc
5683 * @param argv standard argv without inital components
5685 * @return Integer status returned to shell
5688 static int rpc_trustdom_usage(int argc, const char **argv)
5690 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
5691 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
5692 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
5693 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5694 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
5695 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5696 return -1;
5700 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
5701 const char *domain_name,
5702 struct cli_state *cli,
5703 struct rpc_pipe_client *pipe_hnd,
5704 TALLOC_CTX *mem_ctx,
5705 int argc,
5706 const char **argv)
5708 fstring str_sid;
5709 sid_to_string(str_sid, domain_sid);
5710 d_printf("%s\n", str_sid);
5711 return NT_STATUS_OK;
5714 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5716 fstring ascii_sid, padding;
5717 int pad_len, col_len = 20;
5719 /* convert sid into ascii string */
5720 sid_to_string(ascii_sid, dom_sid);
5722 /* calculate padding space for d_printf to look nicer */
5723 pad_len = col_len - strlen(trusted_dom_name);
5724 padding[pad_len] = 0;
5725 do padding[--pad_len] = ' '; while (pad_len);
5727 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5730 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5731 TALLOC_CTX *mem_ctx,
5732 POLICY_HND *pol,
5733 DOM_SID dom_sid,
5734 const char *trusted_dom_name)
5736 NTSTATUS nt_status;
5737 LSA_TRUSTED_DOMAIN_INFO *info;
5738 char *cleartextpwd = NULL;
5739 DATA_BLOB data;
5741 nt_status = rpccli_lsa_query_trusted_domain_info_by_sid(pipe_hnd, mem_ctx, pol, 4, &dom_sid, &info);
5743 if (NT_STATUS_IS_ERR(nt_status)) {
5744 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5745 nt_errstr(nt_status)));
5746 goto done;
5749 data = data_blob(NULL, info->password.password.length);
5751 memcpy(data.data, info->password.password.data, info->password.password.length);
5752 data.length = info->password.password.length;
5754 cleartextpwd = decrypt_trustdom_secret(pipe_hnd->cli->pwd.password, &data);
5756 if (cleartextpwd == NULL) {
5757 DEBUG(0,("retrieved NULL password\n"));
5758 nt_status = NT_STATUS_UNSUCCESSFUL;
5759 goto done;
5762 if (!secrets_store_trusted_domain_password(trusted_dom_name,
5763 cleartextpwd,
5764 &dom_sid)) {
5765 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5766 nt_status = NT_STATUS_UNSUCCESSFUL;
5767 goto done;
5770 #ifdef DEBUG_PASSWORD
5771 DEBUG(100,("sucessfully vampired trusted domain [%s], sid: [%s], password: [%s]\n",
5772 trusted_dom_name, sid_string_static(&dom_sid), cleartextpwd));
5773 #endif
5775 done:
5776 SAFE_FREE(cleartextpwd);
5777 data_blob_free(&data);
5779 return nt_status;
5782 static int rpc_trustdom_vampire(int argc, const char **argv)
5784 /* common variables */
5785 TALLOC_CTX* mem_ctx;
5786 struct cli_state *cli = NULL;
5787 struct rpc_pipe_client *pipe_hnd = NULL;
5788 NTSTATUS nt_status;
5789 const char *domain_name = NULL;
5790 DOM_SID *queried_dom_sid;
5791 POLICY_HND connect_hnd;
5793 /* trusted domains listing variables */
5794 unsigned int num_domains, enum_ctx = 0;
5795 int i;
5796 DOM_SID *domain_sids;
5797 char **trusted_dom_names;
5798 fstring pdc_name;
5799 char *dummy;
5802 * Listing trusted domains (stored in secrets.tdb, if local)
5805 mem_ctx = talloc_init("trust relationships vampire");
5808 * set domain and pdc name to local samba server (default)
5809 * or to remote one given in command line
5812 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5813 domain_name = opt_workgroup;
5814 opt_target_workgroup = opt_workgroup;
5815 } else {
5816 fstrcpy(pdc_name, global_myname());
5817 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5818 opt_target_workgroup = domain_name;
5821 /* open \PIPE\lsarpc and open policy handle */
5822 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5823 DEBUG(0, ("Couldn't connect to domain controller\n"));
5824 return -1;
5827 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5828 if (!pipe_hnd) {
5829 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5830 nt_errstr(nt_status) ));
5831 cli_shutdown(cli);
5832 return -1;
5835 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5836 &connect_hnd);
5837 if (NT_STATUS_IS_ERR(nt_status)) {
5838 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5839 nt_errstr(nt_status)));
5840 cli_shutdown(cli);
5841 return -1;
5844 /* query info level 5 to obtain sid of a domain being queried */
5845 nt_status = rpccli_lsa_query_info_policy(
5846 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
5847 &dummy, &queried_dom_sid);
5849 if (NT_STATUS_IS_ERR(nt_status)) {
5850 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5851 nt_errstr(nt_status)));
5852 cli_shutdown(cli);
5853 return -1;
5857 * Keep calling LsaEnumTrustdom over opened pipe until
5858 * the end of enumeration is reached
5861 d_printf("Vampire trusted domains:\n\n");
5863 do {
5864 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
5865 &num_domains,
5866 &trusted_dom_names, &domain_sids);
5868 if (NT_STATUS_IS_ERR(nt_status)) {
5869 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5870 nt_errstr(nt_status)));
5871 cli_shutdown(cli);
5872 return -1;
5875 for (i = 0; i < num_domains; i++) {
5877 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
5879 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5880 domain_sids[i], trusted_dom_names[i]);
5881 if (!NT_STATUS_IS_OK(nt_status)) {
5882 cli_shutdown(cli);
5883 return -1;
5888 * in case of no trusted domains say something rather
5889 * than just display blank line
5891 if (!num_domains) d_printf("none\n");
5893 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5895 /* close this connection before doing next one */
5896 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
5897 if (NT_STATUS_IS_ERR(nt_status)) {
5898 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5899 nt_errstr(nt_status)));
5900 cli_shutdown(cli);
5901 return -1;
5904 /* close lsarpc pipe and connection to IPC$ */
5905 cli_shutdown(cli);
5907 talloc_destroy(mem_ctx);
5908 return 0;
5911 static int rpc_trustdom_list(int argc, const char **argv)
5913 /* common variables */
5914 TALLOC_CTX* mem_ctx;
5915 struct cli_state *cli = NULL, *remote_cli = NULL;
5916 struct rpc_pipe_client *pipe_hnd = NULL;
5917 NTSTATUS nt_status;
5918 const char *domain_name = NULL;
5919 DOM_SID *queried_dom_sid;
5920 fstring padding;
5921 int ascii_dom_name_len;
5922 POLICY_HND connect_hnd;
5924 /* trusted domains listing variables */
5925 unsigned int num_domains, enum_ctx = 0;
5926 int i, pad_len, col_len = 20;
5927 DOM_SID *domain_sids;
5928 char **trusted_dom_names;
5929 fstring pdc_name;
5930 char *dummy;
5932 /* trusting domains listing variables */
5933 POLICY_HND domain_hnd;
5934 char **trusting_dom_names;
5935 uint32 *trusting_dom_rids;
5938 * Listing trusted domains (stored in secrets.tdb, if local)
5941 mem_ctx = talloc_init("trust relationships listing");
5944 * set domain and pdc name to local samba server (default)
5945 * or to remote one given in command line
5948 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
5949 domain_name = opt_workgroup;
5950 opt_target_workgroup = opt_workgroup;
5951 } else {
5952 fstrcpy(pdc_name, global_myname());
5953 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5954 opt_target_workgroup = domain_name;
5957 /* open \PIPE\lsarpc and open policy handle */
5958 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
5959 DEBUG(0, ("Couldn't connect to domain controller\n"));
5960 return -1;
5963 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5964 if (!pipe_hnd) {
5965 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5966 nt_errstr(nt_status) ));
5967 return -1;
5970 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
5971 &connect_hnd);
5972 if (NT_STATUS_IS_ERR(nt_status)) {
5973 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5974 nt_errstr(nt_status)));
5975 return -1;
5978 /* query info level 5 to obtain sid of a domain being queried */
5979 nt_status = rpccli_lsa_query_info_policy(
5980 pipe_hnd, mem_ctx, &connect_hnd, 5 /* info level */,
5981 &dummy, &queried_dom_sid);
5983 if (NT_STATUS_IS_ERR(nt_status)) {
5984 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5985 nt_errstr(nt_status)));
5986 return -1;
5990 * Keep calling LsaEnumTrustdom over opened pipe until
5991 * the end of enumeration is reached
5994 d_printf("Trusted domains list:\n\n");
5996 do {
5997 nt_status = rpccli_lsa_enum_trust_dom(pipe_hnd, mem_ctx, &connect_hnd, &enum_ctx,
5998 &num_domains,
5999 &trusted_dom_names, &domain_sids);
6001 if (NT_STATUS_IS_ERR(nt_status)) {
6002 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6003 nt_errstr(nt_status)));
6004 return -1;
6007 for (i = 0; i < num_domains; i++) {
6008 print_trusted_domain(&(domain_sids[i]), trusted_dom_names[i]);
6012 * in case of no trusted domains say something rather
6013 * than just display blank line
6015 if (!num_domains) d_printf("none\n");
6017 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6019 /* close this connection before doing next one */
6020 nt_status = rpccli_lsa_close(pipe_hnd, mem_ctx, &connect_hnd);
6021 if (NT_STATUS_IS_ERR(nt_status)) {
6022 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6023 nt_errstr(nt_status)));
6024 return -1;
6027 cli_rpc_pipe_close(pipe_hnd);
6030 * Listing trusting domains (stored in passdb backend, if local)
6033 d_printf("\nTrusting domains list:\n\n");
6036 * Open \PIPE\samr and get needed policy handles
6038 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6039 if (!pipe_hnd) {
6040 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6041 return -1;
6044 /* SamrConnect */
6045 nt_status = rpccli_samr_connect(pipe_hnd, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
6046 &connect_hnd);
6047 if (!NT_STATUS_IS_OK(nt_status)) {
6048 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6049 nt_errstr(nt_status)));
6050 return -1;
6053 /* SamrOpenDomain - we have to open domain policy handle in order to be
6054 able to enumerate accounts*/
6055 nt_status = rpccli_samr_open_domain(pipe_hnd, mem_ctx, &connect_hnd,
6056 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6057 queried_dom_sid, &domain_hnd);
6058 if (!NT_STATUS_IS_OK(nt_status)) {
6059 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6060 nt_errstr(nt_status)));
6061 return -1;
6065 * perform actual enumeration
6068 enum_ctx = 0; /* reset enumeration context from last enumeration */
6069 do {
6071 nt_status = rpccli_samr_enum_dom_users(pipe_hnd, mem_ctx, &domain_hnd,
6072 &enum_ctx, ACB_DOMTRUST, 0xffff,
6073 &trusting_dom_names, &trusting_dom_rids,
6074 &num_domains);
6075 if (NT_STATUS_IS_ERR(nt_status)) {
6076 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6077 nt_errstr(nt_status)));
6078 return -1;
6081 for (i = 0; i < num_domains; i++) {
6084 * get each single domain's sid (do we _really_ need this ?):
6085 * 1) connect to domain's pdc
6086 * 2) query the pdc for domain's sid
6089 /* get rid of '$' tail */
6090 ascii_dom_name_len = strlen(trusting_dom_names[i]);
6091 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6092 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
6094 /* calculate padding space for d_printf to look nicer */
6095 pad_len = col_len - strlen(trusting_dom_names[i]);
6096 padding[pad_len] = 0;
6097 do padding[--pad_len] = ' '; while (pad_len);
6099 /* set opt_* variables to remote domain */
6100 strupper_m(trusting_dom_names[i]);
6101 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
6102 opt_target_workgroup = opt_workgroup;
6104 d_printf("%s%s", trusting_dom_names[i], padding);
6106 /* connect to remote domain controller */
6107 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
6108 if (remote_cli) {
6109 /* query for domain's sid */
6110 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
6111 d_fprintf(stderr, "couldn't get domain's sid\n");
6113 cli_shutdown(remote_cli);
6115 } else {
6116 d_fprintf(stderr, "domain controller is not responding\n");
6120 if (!num_domains) d_printf("none\n");
6122 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6124 /* close opened samr and domain policy handles */
6125 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &domain_hnd);
6126 if (!NT_STATUS_IS_OK(nt_status)) {
6127 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6130 nt_status = rpccli_samr_close(pipe_hnd, mem_ctx, &connect_hnd);
6131 if (!NT_STATUS_IS_OK(nt_status)) {
6132 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6135 /* close samr pipe and connection to IPC$ */
6136 cli_shutdown(cli);
6138 talloc_destroy(mem_ctx);
6139 return 0;
6143 * Entrypoint for 'net rpc trustdom' code
6145 * @param argc standard argc
6146 * @param argv standard argv without initial components
6148 * @return Integer status (0 means success)
6151 static int rpc_trustdom(int argc, const char **argv)
6153 struct functable func[] = {
6154 {"add", rpc_trustdom_add},
6155 {"del", rpc_trustdom_del},
6156 {"establish", rpc_trustdom_establish},
6157 {"revoke", rpc_trustdom_revoke},
6158 {"help", rpc_trustdom_usage},
6159 {"list", rpc_trustdom_list},
6160 {"vampire", rpc_trustdom_vampire},
6161 {NULL, NULL}
6164 if (argc == 0) {
6165 rpc_trustdom_usage(argc, argv);
6166 return -1;
6169 return (net_run_function(argc, argv, func, rpc_user_usage));
6173 * Check if a server will take rpc commands
6174 * @param flags Type of server to connect to (PDC, DMB, localhost)
6175 * if the host is not explicitly specified
6176 * @return BOOL (true means rpc supported)
6178 BOOL net_rpc_check(unsigned flags)
6180 struct cli_state cli;
6181 BOOL ret = False;
6182 struct in_addr server_ip;
6183 char *server_name = NULL;
6185 /* flags (i.e. server type) may depend on command */
6186 if (!net_find_server(flags, &server_ip, &server_name))
6187 return False;
6189 ZERO_STRUCT(cli);
6190 if (cli_initialise(&cli) == False)
6191 return False;
6193 if (!cli_connect(&cli, server_name, &server_ip))
6194 goto done;
6195 if (!attempt_netbios_session_request(&cli, global_myname(),
6196 server_name, &server_ip))
6197 goto done;
6198 if (!cli_negprot(&cli))
6199 goto done;
6200 if (cli.protocol < PROTOCOL_NT1)
6201 goto done;
6203 ret = True;
6204 done:
6205 cli_shutdown(&cli);
6206 return ret;
6209 /* dump sam database via samsync rpc calls */
6210 static int rpc_samdump(int argc, const char **argv) {
6211 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
6212 argc, argv);
6215 /* syncronise sam database via samsync rpc calls */
6216 static int rpc_vampire(int argc, const char **argv) {
6217 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
6218 argc, argv);
6221 /**
6222 * Migrate everything from a print-server
6224 * @param argc Standard main() style argc
6225 * @param argv Standard main() style argv. Initial components are already
6226 * stripped
6228 * @return A shell status integer (0 for success)
6230 * The order is important !
6231 * To successfully add drivers the print-queues have to exist !
6232 * Applying ACLs should be the last step, because you're easily locked out
6235 static int rpc_printer_migrate_all(int argc, const char **argv)
6237 int ret;
6239 if (!opt_host) {
6240 printf("no server to migrate\n");
6241 return -1;
6244 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
6245 if (ret)
6246 return ret;
6248 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
6249 if (ret)
6250 return ret;
6252 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
6253 if (ret)
6254 return ret;
6256 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
6257 if (ret)
6258 return ret;
6260 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
6264 /**
6265 * Migrate print-drivers from a print-server
6267 * @param argc Standard main() style argc
6268 * @param argv Standard main() style argv. Initial components are already
6269 * stripped
6271 * @return A shell status integer (0 for success)
6273 static int rpc_printer_migrate_drivers(int argc, const char **argv)
6275 if (!opt_host) {
6276 printf("no server to migrate\n");
6277 return -1;
6280 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6281 rpc_printer_migrate_drivers_internals,
6282 argc, argv);
6285 /**
6286 * Migrate print-forms from a print-server
6288 * @param argc Standard main() style argc
6289 * @param argv Standard main() style argv. Initial components are already
6290 * stripped
6292 * @return A shell status integer (0 for success)
6294 static int rpc_printer_migrate_forms(int argc, const char **argv)
6296 if (!opt_host) {
6297 printf("no server to migrate\n");
6298 return -1;
6301 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6302 rpc_printer_migrate_forms_internals,
6303 argc, argv);
6306 /**
6307 * Migrate printers from a print-server
6309 * @param argc Standard main() style argc
6310 * @param argv Standard main() style argv. Initial components are already
6311 * stripped
6313 * @return A shell status integer (0 for success)
6315 static int rpc_printer_migrate_printers(int argc, const char **argv)
6317 if (!opt_host) {
6318 printf("no server to migrate\n");
6319 return -1;
6322 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6323 rpc_printer_migrate_printers_internals,
6324 argc, argv);
6327 /**
6328 * Migrate printer-ACLs from a print-server
6330 * @param argc Standard main() style argc
6331 * @param argv Standard main() style argv. Initial components are already
6332 * stripped
6334 * @return A shell status integer (0 for success)
6336 static int rpc_printer_migrate_security(int argc, const char **argv)
6338 if (!opt_host) {
6339 printf("no server to migrate\n");
6340 return -1;
6343 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6344 rpc_printer_migrate_security_internals,
6345 argc, argv);
6348 /**
6349 * Migrate printer-settings from a print-server
6351 * @param argc Standard main() style argc
6352 * @param argv Standard main() style argv. Initial components are already
6353 * stripped
6355 * @return A shell status integer (0 for success)
6357 static int rpc_printer_migrate_settings(int argc, const char **argv)
6359 if (!opt_host) {
6360 printf("no server to migrate\n");
6361 return -1;
6364 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6365 rpc_printer_migrate_settings_internals,
6366 argc, argv);
6369 /**
6370 * 'net rpc printer' entrypoint.
6371 * @param argc Standard main() style argc
6372 * @param argv Standard main() style argv. Initial components are already
6373 * stripped
6376 int rpc_printer_migrate(int argc, const char **argv)
6379 /* ouch: when addriver and setdriver are called from within
6380 rpc_printer_migrate_drivers_internals, the printer-queue already
6381 *has* to exist */
6383 struct functable func[] = {
6384 {"all", rpc_printer_migrate_all},
6385 {"drivers", rpc_printer_migrate_drivers},
6386 {"forms", rpc_printer_migrate_forms},
6387 {"help", rpc_printer_usage},
6388 {"printers", rpc_printer_migrate_printers},
6389 {"security", rpc_printer_migrate_security},
6390 {"settings", rpc_printer_migrate_settings},
6391 {NULL, NULL}
6394 return net_run_function(argc, argv, func, rpc_printer_usage);
6398 /**
6399 * List printers on a remote RPC server
6401 * @param argc Standard main() style argc
6402 * @param argv Standard main() style argv. Initial components are already
6403 * stripped
6405 * @return A shell status integer (0 for success)
6407 static int rpc_printer_list(int argc, const char **argv)
6410 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6411 rpc_printer_list_internals,
6412 argc, argv);
6415 /**
6416 * List printer-drivers on a remote RPC server
6418 * @param argc Standard main() style argc
6419 * @param argv Standard main() style argv. Initial components are already
6420 * stripped
6422 * @return A shell status integer (0 for success)
6424 static int rpc_printer_driver_list(int argc, const char **argv)
6427 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6428 rpc_printer_driver_list_internals,
6429 argc, argv);
6432 /**
6433 * Publish printer in ADS via MSRPC
6435 * @param argc Standard main() style argc
6436 * @param argv Standard main() style argv. Initial components are already
6437 * stripped
6439 * @return A shell status integer (0 for success)
6441 static int rpc_printer_publish_publish(int argc, const char **argv)
6444 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6445 rpc_printer_publish_publish_internals,
6446 argc, argv);
6449 /**
6450 * Update printer in ADS via MSRPC
6452 * @param argc Standard main() style argc
6453 * @param argv Standard main() style argv. Initial components are already
6454 * stripped
6456 * @return A shell status integer (0 for success)
6458 static int rpc_printer_publish_update(int argc, const char **argv)
6461 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6462 rpc_printer_publish_update_internals,
6463 argc, argv);
6466 /**
6467 * UnPublish printer in ADS via MSRPC
6469 * @param argc Standard main() style argc
6470 * @param argv Standard main() style argv. Initial components are already
6471 * stripped
6473 * @return A shell status integer (0 for success)
6475 static int rpc_printer_publish_unpublish(int argc, const char **argv)
6478 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6479 rpc_printer_publish_unpublish_internals,
6480 argc, argv);
6483 /**
6484 * List published printers via MSRPC
6486 * @param argc Standard main() style argc
6487 * @param argv Standard main() style argv. Initial components are already
6488 * stripped
6490 * @return A shell status integer (0 for success)
6492 static int rpc_printer_publish_list(int argc, const char **argv)
6495 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6496 rpc_printer_publish_list_internals,
6497 argc, argv);
6501 /**
6502 * Publish printer in ADS
6504 * @param argc Standard main() style argc
6505 * @param argv Standard main() style argv. Initial components are already
6506 * stripped
6508 * @return A shell status integer (0 for success)
6510 static int rpc_printer_publish(int argc, const char **argv)
6513 struct functable func[] = {
6514 {"publish", rpc_printer_publish_publish},
6515 {"update", rpc_printer_publish_update},
6516 {"unpublish", rpc_printer_publish_unpublish},
6517 {"list", rpc_printer_publish_list},
6518 {"help", rpc_printer_usage},
6519 {NULL, NULL}
6522 if (argc == 0)
6523 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6524 rpc_printer_publish_list_internals,
6525 argc, argv);
6527 return net_run_function(argc, argv, func, rpc_printer_usage);
6532 /**
6533 * Display rpc printer help page.
6534 * @param argc Standard main() style argc
6535 * @param argv Standard main() style argv. Initial components are already
6536 * stripped
6538 int rpc_printer_usage(int argc, const char **argv)
6540 return net_help_printer(argc, argv);
6543 /**
6544 * 'net rpc printer' entrypoint.
6545 * @param argc Standard main() style argc
6546 * @param argv Standard main() style argv. Initial components are already
6547 * stripped
6549 int net_rpc_printer(int argc, const char **argv)
6551 struct functable func[] = {
6552 {"list", rpc_printer_list},
6553 {"migrate", rpc_printer_migrate},
6554 {"driver", rpc_printer_driver_list},
6555 {"publish", rpc_printer_publish},
6556 {NULL, NULL}
6559 if (argc == 0)
6560 return run_rpc_command(NULL, PI_SPOOLSS, 0,
6561 rpc_printer_list_internals,
6562 argc, argv);
6564 return net_run_function(argc, argv, func, rpc_printer_usage);
6567 /****************************************************************************/
6570 /**
6571 * Basic usage function for 'net rpc'
6572 * @param argc Standard main() style argc
6573 * @param argv Standard main() style argv. Initial components are already
6574 * stripped
6577 int net_rpc_usage(int argc, const char **argv)
6579 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
6580 d_printf(" net rpc join \t\t\tto join a domain \n");
6581 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n");
6582 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
6583 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
6584 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
6585 d_printf(" net rpc group \t\tto list groups\n");
6586 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
6587 d_printf(" net rpc printer \t\tto list and migrate printers\n");
6588 d_printf(" net rpc file \t\t\tto list open files\n");
6589 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
6590 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
6591 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
6592 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
6593 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
6594 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
6595 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
6596 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
6597 d_printf(" net rpc registry\t\tto manage registry hives\n");
6598 d_printf(" net rpc service\t\tto start, stop and query services\n");
6599 d_printf("\n");
6600 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
6601 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
6602 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
6603 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
6604 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
6605 return -1;
6610 * Help function for 'net rpc'. Calls command specific help if requested
6611 * or displays usage of net rpc
6612 * @param argc Standard main() style argc
6613 * @param argv Standard main() style argv. Initial components are already
6614 * stripped
6617 int net_rpc_help(int argc, const char **argv)
6619 struct functable func[] = {
6620 {"join", rpc_join_usage},
6621 {"user", rpc_user_usage},
6622 {"group", rpc_group_usage},
6623 {"share", rpc_share_usage},
6624 /*{"changetrustpw", rpc_changetrustpw_usage}, */
6625 {"trustdom", rpc_trustdom_usage},
6626 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
6627 /*{"shutdown", rpc_shutdown_usage}, */
6628 {"vampire", rpc_vampire_usage},
6629 {NULL, NULL}
6632 if (argc == 0) {
6633 net_rpc_usage(argc, argv);
6634 return -1;
6637 return (net_run_function(argc, argv, func, rpc_user_usage));
6640 /**
6641 * 'net rpc' entrypoint.
6642 * @param argc Standard main() style argc
6643 * @param argv Standard main() style argv. Initial components are already
6644 * stripped
6647 int net_rpc(int argc, const char **argv)
6649 struct functable func[] = {
6650 {"info", net_rpc_info},
6651 {"join", net_rpc_join},
6652 {"oldjoin", net_rpc_oldjoin},
6653 {"testjoin", net_rpc_testjoin},
6654 {"user", net_rpc_user},
6655 {"password", rpc_user_password},
6656 {"group", net_rpc_group},
6657 {"share", net_rpc_share},
6658 {"file", net_rpc_file},
6659 {"printer", net_rpc_printer},
6660 {"changetrustpw", net_rpc_changetrustpw},
6661 {"trustdom", rpc_trustdom},
6662 {"abortshutdown", rpc_shutdown_abort},
6663 {"shutdown", rpc_shutdown},
6664 {"samdump", rpc_samdump},
6665 {"vampire", rpc_vampire},
6666 {"getsid", net_rpc_getsid},
6667 {"rights", net_rpc_rights},
6668 {"service", net_rpc_service},
6669 {"registry", net_rpc_registry},
6670 {"shell", net_rpc_shell},
6671 {"help", net_rpc_help},
6672 {NULL, NULL}
6674 return net_run_function(argc, argv, func, net_rpc_usage);