(merge from 3.0)
[Samba/gebeck_regimport.git] / source / utils / net_rpc.c
blobeef3adbcb81b35a8582ba5e9f8c43f3212042611
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)
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include "includes.h"
22 #include "../utils/net.h"
24 /**
25 * @file net_rpc.c
27 * @brief RPC based subcommands for the 'net' utility.
29 * This file should contain much of the functionality that used to
30 * be found in rpcclient, execpt that the commands should change
31 * less often, and the fucntionality should be sane (the user is not
32 * expected to know a rid/sid before they conduct an operation etc.)
34 * @todo Perhaps eventually these should be split out into a number
35 * of files, as this could get quite big.
36 **/
39 /* A function of this type is passed to the 'run_rpc_command' wrapper */
40 typedef NTSTATUS (*rpc_command_fn)(const DOM_SID *, struct cli_state *, TALLOC_CTX *, int, const char **);
42 /**
43 * Many of the RPC functions need the domain sid. This function gets
44 * it at the start of every run
46 * @param cli A cli_state already connected to the remote machine
48 * @return The Domain SID of the remote machine.
49 **/
51 static DOM_SID *net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx)
53 DOM_SID *domain_sid;
54 POLICY_HND pol;
55 NTSTATUS result = NT_STATUS_OK;
56 uint32 info_class = 5;
57 char *domain_name;
59 if (!cli_nt_session_open (cli, PI_LSARPC)) {
60 fprintf(stderr, "could not initialise lsa pipe\n");
61 goto error;
64 result = cli_lsa_open_policy(cli, mem_ctx, False,
65 SEC_RIGHTS_MAXIMUM_ALLOWED,
66 &pol);
67 if (!NT_STATUS_IS_OK(result)) {
68 goto error;
71 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
72 &domain_name, &domain_sid);
73 if (!NT_STATUS_IS_OK(result)) {
74 error:
75 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
77 if (!NT_STATUS_IS_OK(result)) {
78 fprintf(stderr, "error: %s\n", nt_errstr(result));
81 exit(1);
84 cli_lsa_close(cli, mem_ctx, &pol);
85 cli_nt_session_close(cli);
87 return domain_sid;
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 static int run_rpc_command(struct cli_state *cli_arg, const int pipe_idx, int conn_flags,
103 rpc_command_fn fn,
104 int argc, const char **argv)
106 struct cli_state *cli = NULL;
107 TALLOC_CTX *mem_ctx;
108 NTSTATUS nt_status;
109 DOM_SID *domain_sid;
111 /* make use of cli_state handed over as an argument, if possible */
112 if (!cli_arg)
113 cli = net_make_ipc_connection(conn_flags);
114 else
115 cli = cli_arg;
117 if (!cli) {
118 return -1;
121 /* Create mem_ctx */
123 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
124 DEBUG(0, ("talloc_init() failed\n"));
125 cli_shutdown(cli);
126 return -1;
129 domain_sid = net_get_remote_domain_sid(cli, mem_ctx);
131 if (!cli_nt_session_open(cli, pipe_idx)) {
132 DEBUG(0, ("Could not initialise pipe\n"));
135 nt_status = fn(domain_sid, cli, mem_ctx, argc, argv);
137 if (!NT_STATUS_IS_OK(nt_status)) {
138 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
139 } else {
140 DEBUG(5, ("rpc command function succedded\n"));
144 if (cli->nt_pipe_fnum)
145 cli_nt_session_close(cli);
147 /* close the connection only if it was opened here */
148 if (!cli_arg)
149 cli_shutdown(cli);
151 talloc_destroy(mem_ctx);
153 return (!NT_STATUS_IS_OK(nt_status));
157 /****************************************************************************/
160 /**
161 * Force a change of the trust acccount password.
163 * All parameters are provided by the run_rpc_command function, except for
164 * argc, argv which are passes through.
166 * @param domain_sid The domain sid aquired from the remote server
167 * @param cli A cli_state connected to the server.
168 * @param mem_ctx Talloc context, destoyed on compleation of the function.
169 * @param argc Standard main() style argc
170 * @param argc Standard main() style argv. Initial components are already
171 * stripped
173 * @return Normal NTSTATUS return.
176 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
177 int argc, const char **argv) {
179 return trust_pw_find_change_and_store_it(cli, mem_ctx, opt_target_workgroup);
182 /**
183 * Force a change of the trust acccount password.
185 * @param argc Standard main() style argc
186 * @param argc Standard main() style argv. Initial components are already
187 * stripped
189 * @return A shell status integer (0 for success)
192 int net_rpc_changetrustpw(int argc, const char **argv)
194 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, rpc_changetrustpw_internals,
195 argc, argv);
199 /****************************************************************************/
202 /**
203 * Join a domain, the old way.
205 * This uses 'machinename' as the inital password, and changes it.
207 * The password should be created with 'server manager' or equiv first.
209 * All parameters are provided by the run_rpc_command function, except for
210 * argc, argv which are passes through.
212 * @param domain_sid The domain sid aquired from the remote server
213 * @param cli A cli_state connected to the server.
214 * @param mem_ctx Talloc context, destoyed on compleation of the function.
215 * @param argc Standard main() style argc
216 * @param argc Standard main() style argv. Initial components are already
217 * stripped
219 * @return Normal NTSTATUS return.
222 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid, struct cli_state *cli,
223 TALLOC_CTX *mem_ctx,
224 int argc, const char **argv) {
226 fstring trust_passwd;
227 unsigned char orig_trust_passwd_hash[16];
228 NTSTATUS result;
229 uint32 sec_channel_type;
232 check what type of join - if the user want's to join as
233 a BDC, the server must agree that we are a BDC.
235 if (argc >= 0) {
236 sec_channel_type = get_sec_channel_type(argv[0]);
237 } else {
238 sec_channel_type = get_sec_channel_type(NULL);
241 fstrcpy(trust_passwd, global_myname());
242 strlower_m(trust_passwd);
245 * Machine names can be 15 characters, but the max length on
246 * a password is 14. --jerry
249 trust_passwd[14] = '\0';
251 E_md4hash(trust_passwd, orig_trust_passwd_hash);
253 result = trust_pw_change_and_store_it(cli, mem_ctx, opt_target_workgroup,
254 orig_trust_passwd_hash,
255 sec_channel_type);
257 if (NT_STATUS_IS_OK(result))
258 printf("Joined domain %s.\n",opt_target_workgroup);
261 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
262 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
263 result = NT_STATUS_UNSUCCESSFUL;
266 return result;
269 /**
270 * Join a domain, the old way.
272 * @param argc Standard main() style argc
273 * @param argc Standard main() style argv. Initial components are already
274 * stripped
276 * @return A shell status integer (0 for success)
279 static int net_rpc_perform_oldjoin(int argc, const char **argv)
281 return run_rpc_command(NULL, PI_NETLOGON,
282 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
283 rpc_oldjoin_internals,
284 argc, argv);
287 /**
288 * Join a domain, the old way. This function exists to allow
289 * the message to be displayed when oldjoin was explicitly
290 * requested, but not when it was implied by "net rpc join"
292 * @param argc Standard main() style argc
293 * @param argc Standard main() style argv. Initial components are already
294 * stripped
296 * @return A shell status integer (0 for success)
299 static int net_rpc_oldjoin(int argc, const char **argv)
301 int rc = net_rpc_perform_oldjoin(argc, argv);
303 if (rc) {
304 d_printf("Failed to join domain\n");
307 return rc;
310 /**
311 * Basic usage function for 'net rpc join'
312 * @param argc Standard main() style argc
313 * @param argc Standard main() style argv. Initial components are already
314 * stripped
317 static int rpc_join_usage(int argc, const char **argv)
319 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
320 "\t to join a domain with admin username & password\n"\
321 "\t\t password will be prompted if needed and none is specified\n"\
322 "\t <type> can be (default MEMBER)\n"\
323 "\t\t BDC - Join as a BDC\n"\
324 "\t\t PDC - Join as a PDC\n"\
325 "\t\t MEMBER - Join as a MEMBER server\n");
327 net_common_flags_usage(argc, argv);
328 return -1;
331 /**
332 * 'net rpc join' entrypoint.
333 * @param argc Standard main() style argc
334 * @param argc Standard main() style argv. Initial components are already
335 * stripped
337 * Main 'net_rpc_join()' (where the admain username/password is used) is
338 * in net_rpc_join.c
339 * Try to just change the password, but if that doesn't work, use/prompt
340 * for a username/password.
343 int net_rpc_join(int argc, const char **argv)
345 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
346 return 0;
348 return net_rpc_join_newstyle(argc, argv);
353 /**
354 * display info about a rpc domain
356 * All parameters are provided by the run_rpc_command function, except for
357 * argc, argv which are passed through.
359 * @param domain_sid The domain sid acquired from the remote server
360 * @param cli A cli_state connected to the server.
361 * @param mem_ctx Talloc context, destoyed on completion of the function.
362 * @param argc Standard main() style argc
363 * @param argv Standard main() style argv. Initial components are already
364 * stripped
366 * @return Normal NTSTATUS return.
369 static NTSTATUS
370 rpc_info_internals(const DOM_SID *domain_sid, struct cli_state *cli,
371 TALLOC_CTX *mem_ctx, int argc, const char **argv)
373 POLICY_HND connect_pol, domain_pol;
374 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
375 SAM_UNK_CTR ctr;
376 fstring sid_str;
378 sid_to_string(sid_str, domain_sid);
380 /* Get sam policy handle */
381 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
382 &connect_pol);
383 if (!NT_STATUS_IS_OK(result)) {
384 goto done;
387 /* Get domain policy handle */
388 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
389 MAXIMUM_ALLOWED_ACCESS,
390 domain_sid, &domain_pol);
391 if (!NT_STATUS_IS_OK(result)) {
392 goto done;
395 ZERO_STRUCT(ctr);
396 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
397 2, &ctr);
398 if (NT_STATUS_IS_OK(result)) {
399 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
400 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
401 d_printf("Domain SID: %s\n", sid_str);
402 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num);
403 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
404 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
405 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
406 talloc_destroy(ctx);
409 done:
410 return result;
414 /**
415 * 'net rpc info' entrypoint.
416 * @param argc Standard main() style argc
417 * @param argc Standard main() style argv. Initial components are already
418 * stripped
420 int net_rpc_info(int argc, const char **argv)
422 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
423 rpc_info_internals,
424 argc, argv);
428 /**
429 * Fetch domain SID into the local secrets.tdb
431 * All parameters are provided by the run_rpc_command function, except for
432 * argc, argv which are passes through.
434 * @param domain_sid The domain sid acquired from the remote server
435 * @param cli A cli_state connected to the server.
436 * @param mem_ctx Talloc context, destoyed on completion of the function.
437 * @param argc Standard main() style argc
438 * @param argv Standard main() style argv. Initial components are already
439 * stripped
441 * @return Normal NTSTATUS return.
444 static NTSTATUS
445 rpc_getsid_internals(const DOM_SID *domain_sid, struct cli_state *cli,
446 TALLOC_CTX *mem_ctx, int argc, const char **argv)
448 fstring sid_str;
450 sid_to_string(sid_str, domain_sid);
451 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
452 sid_str, lp_workgroup());
454 if (!secrets_store_domain_sid(global_myname(), domain_sid)) {
455 DEBUG(0,("Can't store domain SID\n"));
456 return NT_STATUS_UNSUCCESSFUL;
459 return NT_STATUS_OK;
463 /**
464 * 'net rpc getsid' entrypoint.
465 * @param argc Standard main() style argc
466 * @param argc Standard main() style argv. Initial components are already
467 * stripped
469 int net_rpc_getsid(int argc, const char **argv)
471 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
472 rpc_getsid_internals,
473 argc, argv);
477 /****************************************************************************/
480 * Basic usage function for 'net rpc user'
481 * @param argc Standard main() style argc.
482 * @param argv Standard main() style argv. Initial components are already
483 * stripped.
486 static int rpc_user_usage(int argc, const char **argv)
488 return net_help_user(argc, argv);
491 /**
492 * Add a new user to a remote RPC server
494 * All parameters are provided by the run_rpc_command function, except for
495 * argc, argv which are passes through.
497 * @param domain_sid The domain sid acquired from the remote server
498 * @param cli A cli_state connected to the server.
499 * @param mem_ctx Talloc context, destoyed on completion of the function.
500 * @param argc Standard main() style argc
501 * @param argv Standard main() style argv. Initial components are already
502 * stripped
504 * @return Normal NTSTATUS return.
507 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
508 int argc, const char **argv) {
510 POLICY_HND connect_pol, domain_pol, user_pol;
511 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
512 const char *acct_name;
513 uint16 acb_info;
514 uint32 unknown, user_rid;
516 if (argc != 1) {
517 d_printf("User must be specified\n");
518 rpc_user_usage(argc, argv);
519 return NT_STATUS_OK;
522 acct_name = argv[0];
524 /* Get sam policy handle */
526 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
527 &connect_pol);
528 if (!NT_STATUS_IS_OK(result)) {
529 goto done;
532 /* Get domain policy handle */
534 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
535 MAXIMUM_ALLOWED_ACCESS,
536 domain_sid, &domain_pol);
537 if (!NT_STATUS_IS_OK(result)) {
538 goto done;
541 /* Create domain user */
543 acb_info = ACB_NORMAL;
544 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
546 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
547 acct_name, acb_info, unknown,
548 &user_pol, &user_rid);
549 if (!NT_STATUS_IS_OK(result)) {
550 goto done;
553 done:
554 if (!NT_STATUS_IS_OK(result)) {
555 d_printf("Failed to add user %s - %s\n", acct_name,
556 nt_errstr(result));
557 } else {
558 d_printf("Added user %s\n", acct_name);
560 return result;
563 /**
564 * Add a new user to a remote RPC server
566 * @param argc Standard main() style argc
567 * @param argv Standard main() style argv. Initial components are already
568 * stripped
570 * @return A shell status integer (0 for success)
573 static int rpc_user_add(int argc, const char **argv)
575 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
576 argc, argv);
579 /**
580 * Delete a user from a remote RPC server
582 * All parameters are provided by the run_rpc_command function, except for
583 * argc, argv which are passes through.
585 * @param domain_sid The domain sid acquired from the remote server
586 * @param cli A cli_state connected to the server.
587 * @param mem_ctx Talloc context, destoyed on completion of the function.
588 * @param argc Standard main() style argc
589 * @param argv Standard main() style argv. Initial components are already
590 * stripped
592 * @return Normal NTSTATUS return.
595 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
596 struct cli_state *cli,
597 TALLOC_CTX *mem_ctx,
598 int argc, const char **argv)
600 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
601 POLICY_HND connect_pol, domain_pol, user_pol;
603 if (argc < 1) {
604 d_printf("User must be specified\n");
605 rpc_user_usage(argc, argv);
606 return NT_STATUS_OK;
608 /* Get sam policy and domain handles */
610 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
611 &connect_pol);
613 if (!NT_STATUS_IS_OK(result)) {
614 goto done;
617 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
618 MAXIMUM_ALLOWED_ACCESS,
619 domain_sid, &domain_pol);
621 if (!NT_STATUS_IS_OK(result)) {
622 goto done;
625 /* Get handle on user */
628 uint32 *user_rids, num_rids, *name_types;
629 uint32 flags = 0x000003e8; /* Unknown */
631 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
632 flags, 1, &argv[0],
633 &num_rids, &user_rids,
634 &name_types);
636 if (!NT_STATUS_IS_OK(result)) {
637 goto done;
640 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
641 MAXIMUM_ALLOWED_ACCESS,
642 user_rids[0], &user_pol);
644 if (!NT_STATUS_IS_OK(result)) {
645 goto done;
649 /* Delete user */
651 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
653 if (!NT_STATUS_IS_OK(result)) {
654 goto done;
657 /* Display results */
659 done:
660 return result;
664 /**
665 * Delete a user from a remote RPC server
667 * @param argc Standard main() style argc
668 * @param argv Standard main() style argv. Initial components are already
669 * stripped
671 * @return A shell status integer (0 for success)
674 static int rpc_user_delete(int argc, const char **argv)
676 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
677 argc, argv);
680 /**
681 * Set a password for a user on a remote RPC server
683 * All parameters are provided by the run_rpc_command function, except for
684 * argc, argv which are passes through.
686 * @param domain_sid The domain sid acquired from the remote server
687 * @param cli A cli_state connected to the server.
688 * @param mem_ctx Talloc context, destoyed on completion of the function.
689 * @param argc Standard main() style argc
690 * @param argv Standard main() style argv. Initial components are already
691 * stripped
693 * @return Normal NTSTATUS return.
696 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
697 struct cli_state *cli,
698 TALLOC_CTX *mem_ctx,
699 int argc, const char **argv)
701 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
702 POLICY_HND connect_pol, domain_pol, user_pol;
703 SAM_USERINFO_CTR ctr;
704 SAM_USER_INFO_24 p24;
705 uchar pwbuf[516];
706 const char *user;
707 const char *new_password;
708 char *prompt = NULL;
710 if (argc < 1) {
711 d_printf("User must be specified\n");
712 rpc_user_usage(argc, argv);
713 return NT_STATUS_OK;
716 user = argv[0];
718 if (argv[1]) {
719 new_password = argv[1];
720 } else {
721 asprintf(&prompt, "Enter new password for %s:", user);
722 new_password = getpass(prompt);
723 SAFE_FREE(prompt);
726 /* Get sam policy and domain handles */
728 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
729 &connect_pol);
731 if (!NT_STATUS_IS_OK(result)) {
732 goto done;
735 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
736 MAXIMUM_ALLOWED_ACCESS,
737 domain_sid, &domain_pol);
739 if (!NT_STATUS_IS_OK(result)) {
740 goto done;
743 /* Get handle on user */
746 uint32 *user_rids, num_rids, *name_types;
747 uint32 flags = 0x000003e8; /* Unknown */
749 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
750 flags, 1, &user,
751 &num_rids, &user_rids,
752 &name_types);
754 if (!NT_STATUS_IS_OK(result)) {
755 goto done;
758 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
759 MAXIMUM_ALLOWED_ACCESS,
760 user_rids[0], &user_pol);
762 if (!NT_STATUS_IS_OK(result)) {
763 goto done;
767 /* Set password on account */
769 ZERO_STRUCT(ctr);
770 ZERO_STRUCT(p24);
772 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
774 init_sam_user_info24(&p24, (char *)pwbuf,24);
776 ctr.switch_value = 24;
777 ctr.info.id24 = &p24;
779 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
780 &cli->user_session_key, &ctr);
782 if (!NT_STATUS_IS_OK(result)) {
783 goto done;
786 /* Display results */
788 done:
789 return result;
793 /**
794 * Set a user's password on a remote RPC server
796 * @param argc Standard main() style argc
797 * @param argv Standard main() style argv. Initial components are already
798 * stripped
800 * @return A shell status integer (0 for success)
803 static int rpc_user_password(int argc, const char **argv)
805 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
806 argc, argv);
809 /**
810 * List user's groups on a remote RPC server
812 * All parameters are provided by the run_rpc_command function, except for
813 * argc, argv which are passes through.
815 * @param domain_sid The domain sid acquired from the remote server
816 * @param cli A cli_state connected to the server.
817 * @param mem_ctx Talloc context, destoyed on completion of the function.
818 * @param argc Standard main() style argc
819 * @param argv Standard main() style argv. Initial components are already
820 * stripped
822 * @return Normal NTSTATUS return.
825 static NTSTATUS
826 rpc_user_info_internals(const DOM_SID *domain_sid, struct cli_state *cli,
827 TALLOC_CTX *mem_ctx, int argc, const char **argv)
829 POLICY_HND connect_pol, domain_pol, user_pol;
830 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
831 uint32 *rids, num_rids, *name_types, num_names;
832 uint32 flags = 0x000003e8; /* Unknown */
833 int i;
834 char **names;
835 DOM_GID *user_gids;
837 if (argc < 1) {
838 d_printf("User must be specified\n");
839 rpc_user_usage(argc, argv);
840 return NT_STATUS_OK;
842 /* Get sam policy handle */
844 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
845 &connect_pol);
846 if (!NT_STATUS_IS_OK(result)) goto done;
848 /* Get domain policy handle */
850 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
851 MAXIMUM_ALLOWED_ACCESS,
852 domain_sid, &domain_pol);
853 if (!NT_STATUS_IS_OK(result)) goto done;
855 /* Get handle on user */
857 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
858 flags, 1, &argv[0],
859 &num_rids, &rids, &name_types);
861 if (!NT_STATUS_IS_OK(result)) goto done;
863 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
864 MAXIMUM_ALLOWED_ACCESS,
865 rids[0], &user_pol);
866 if (!NT_STATUS_IS_OK(result)) goto done;
868 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
869 &num_rids, &user_gids);
871 /* Look up rids */
873 rids = (uint32 *)talloc(mem_ctx, sizeof(uint32) * num_rids);
875 for (i = 0; i < num_rids; i++)
876 rids[i] = user_gids[i].g_rid;
878 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
879 flags, num_rids, rids,
880 &num_names, &names, &name_types);
882 if (!NT_STATUS_IS_OK(result)) {
883 goto done;
886 /* Display results */
888 for (i = 0; i < num_names; i++)
889 printf("%s\n", names[i]);
891 done:
892 return result;
895 /**
896 * List a user's groups from a remote RPC server
898 * @param argc Standard main() style argc
899 * @param argv Standard main() style argv. Initial components are already
900 * stripped
902 * @return A shell status integer (0 for success)
905 static int rpc_user_info(int argc, const char **argv)
907 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
908 argc, argv);
911 /**
912 * List users on a remote RPC server
914 * All parameters are provided by the run_rpc_command function, except for
915 * argc, argv which are passes through.
917 * @param domain_sid The domain sid acquired from the remote server
918 * @param cli A cli_state connected to the server.
919 * @param mem_ctx Talloc context, destoyed on completion of the function.
920 * @param argc Standard main() style argc
921 * @param argv Standard main() style argv. Initial components are already
922 * stripped
924 * @return Normal NTSTATUS return.
927 static NTSTATUS
928 rpc_user_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
929 TALLOC_CTX *mem_ctx, int argc, const char **argv)
931 POLICY_HND connect_pol, domain_pol;
932 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
933 uint32 start_idx=0, num_entries, i, loop_count = 0;
934 SAM_DISPINFO_CTR ctr;
935 SAM_DISPINFO_1 info1;
937 /* Get sam policy handle */
939 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
940 &connect_pol);
941 if (!NT_STATUS_IS_OK(result)) {
942 goto done;
945 /* Get domain policy handle */
947 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
948 MAXIMUM_ALLOWED_ACCESS,
949 domain_sid, &domain_pol);
950 if (!NT_STATUS_IS_OK(result)) {
951 goto done;
954 /* Query domain users */
955 ZERO_STRUCT(ctr);
956 ZERO_STRUCT(info1);
957 ctr.sam.info1 = &info1;
958 if (opt_long_list_entries)
959 d_printf("\nUser name Comment"\
960 "\n-----------------------------\n");
961 do {
962 fstring user, desc;
963 uint32 max_entries, max_size;
965 get_query_dispinfo_params(
966 loop_count, &max_entries, &max_size);
968 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
969 &start_idx, 1, &num_entries,
970 max_entries, max_size, &ctr);
971 loop_count++;
973 for (i = 0; i < num_entries; i++) {
974 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
975 if (opt_long_list_entries)
976 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
978 if (opt_long_list_entries)
979 printf("%-21.21s %s\n", user, desc);
980 else
981 printf("%s\n", user);
983 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
985 done:
986 return result;
989 /**
990 * 'net rpc user' entrypoint.
991 * @param argc Standard main() style argc
992 * @param argc Standard main() style argv. Initial components are already
993 * stripped
996 int net_rpc_user(int argc, const char **argv)
998 struct functable func[] = {
999 {"add", rpc_user_add},
1000 {"info", rpc_user_info},
1001 {"delete", rpc_user_delete},
1002 {"password", rpc_user_password},
1003 {NULL, NULL}
1006 if (argc == 0) {
1007 if (opt_long_list_entries) {
1008 } else {
1010 return run_rpc_command(NULL,PI_SAMR, 0,
1011 rpc_user_list_internals,
1012 argc, argv);
1015 return net_run_function(argc, argv, func, rpc_user_usage);
1019 /****************************************************************************/
1022 * Basic usage function for 'net rpc group'
1023 * @param argc Standard main() style argc.
1024 * @param argv Standard main() style argv. Initial components are already
1025 * stripped.
1028 static int rpc_group_usage(int argc, const char **argv)
1030 return net_help_group(argc, argv);
1033 /**
1034 * List groups on a remote RPC server
1036 * All parameters are provided by the run_rpc_command function, except for
1037 * argc, argv which are passes through.
1039 * @param domain_sid The domain sid acquired from the remote server
1040 * @param cli A cli_state connected to the server.
1041 * @param mem_ctx Talloc context, destoyed on completion of the function.
1042 * @param argc Standard main() style argc
1043 * @param argv Standard main() style argv. Initial components are already
1044 * stripped
1046 * @return Normal NTSTATUS return.
1049 static NTSTATUS
1050 rpc_group_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1051 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1053 POLICY_HND connect_pol, domain_pol;
1054 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1055 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
1056 struct acct_info *groups;
1057 DOM_SID global_sid_Builtin;
1058 BOOL global = False;
1059 BOOL local = False;
1060 BOOL builtin = False;
1062 if (argc == 0) {
1063 global = True;
1064 local = True;
1065 builtin = True;
1068 for (i=0; i<argc; i++) {
1069 if (strequal(argv[i], "global"))
1070 global = True;
1072 if (strequal(argv[i], "local"))
1073 local = True;
1075 if (strequal(argv[i], "builtin"))
1076 builtin = True;
1079 string_to_sid(&global_sid_Builtin, "S-1-5-32");
1081 /* Get sam policy handle */
1083 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1084 &connect_pol);
1085 if (!NT_STATUS_IS_OK(result)) {
1086 goto done;
1089 /* Get domain policy handle */
1091 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1092 MAXIMUM_ALLOWED_ACCESS,
1093 domain_sid, &domain_pol);
1094 if (!NT_STATUS_IS_OK(result)) {
1095 goto done;
1098 /* Query domain groups */
1099 if (opt_long_list_entries)
1100 d_printf("\nGroup name Comment"\
1101 "\n-----------------------------\n");
1102 do {
1103 SAM_DISPINFO_CTR ctr;
1104 SAM_DISPINFO_3 info3;
1105 uint32 max_size;
1107 ZERO_STRUCT(ctr);
1108 ZERO_STRUCT(info3);
1109 ctr.sam.info3 = &info3;
1111 if (!global) break;
1113 get_query_dispinfo_params(
1114 loop_count, &max_entries, &max_size);
1116 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1117 &start_idx, 3, &num_entries,
1118 max_entries, max_size, &ctr);
1120 for (i = 0; i < num_entries; i++) {
1122 fstring group, desc;
1124 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
1125 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
1127 if (opt_long_list_entries)
1128 printf("%-21.21s %-50.50s\n",
1129 group, desc);
1130 else
1131 printf("%s\n", group);
1133 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1134 /* query domain aliases */
1135 start_idx = 0;
1136 do {
1137 if (!local) break;
1139 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1140 &start_idx, max_entries,
1141 &groups, &num_entries);
1143 for (i = 0; i < num_entries; i++) {
1145 char *description = NULL;
1147 if (opt_long_list_entries) {
1149 POLICY_HND alias_pol;
1150 ALIAS_INFO_CTR ctr;
1152 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1153 &domain_pol,
1154 0x8,
1155 groups[i].rid,
1156 &alias_pol))) &&
1157 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1158 &alias_pol, 3,
1159 &ctr))) &&
1160 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1161 &alias_pol)))) {
1162 description = unistr2_tdup(mem_ctx,
1163 &ctr.alias.info3.uni_acct_desc);
1167 if (description != NULL) {
1168 printf("%-21.21s %-50.50s\n",
1169 groups[i].acct_name,
1170 description);
1171 } else {
1172 printf("%s\n", groups[i].acct_name);
1175 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1176 cli_samr_close(cli, mem_ctx, &domain_pol);
1177 /* Get builtin policy handle */
1179 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1180 MAXIMUM_ALLOWED_ACCESS,
1181 &global_sid_Builtin, &domain_pol);
1182 if (!NT_STATUS_IS_OK(result)) {
1183 goto done;
1185 /* query builtin aliases */
1186 start_idx = 0;
1187 do {
1188 if (!builtin) break;
1190 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1191 &start_idx, max_entries,
1192 &groups, &num_entries);
1194 for (i = 0; i < num_entries; i++) {
1196 char *description = NULL;
1198 if (opt_long_list_entries) {
1200 POLICY_HND alias_pol;
1201 ALIAS_INFO_CTR ctr;
1203 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1204 &domain_pol,
1205 0x8,
1206 groups[i].rid,
1207 &alias_pol))) &&
1208 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1209 &alias_pol, 3,
1210 &ctr))) &&
1211 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1212 &alias_pol)))) {
1213 description = unistr2_tdup(mem_ctx,
1214 &ctr.alias.info3.uni_acct_desc);
1218 if (description != NULL) {
1219 printf("%-21.21s %-50.50s\n",
1220 groups[i].acct_name,
1221 description);
1222 } else {
1223 printf("%s\n", groups[i].acct_name);
1226 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1228 done:
1229 return result;
1232 static int rpc_group_list(int argc, const char **argv)
1234 return run_rpc_command(NULL, PI_SAMR, 0,
1235 rpc_group_list_internals,
1236 argc, argv);
1239 static NTSTATUS
1240 rpc_group_members_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1241 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1243 NTSTATUS result;
1244 POLICY_HND connect_pol, domain_pol, group_pol;
1245 uint32 num_rids, *rids, *rid_types;
1246 uint32 num_members, *group_rids, *group_attrs;
1247 uint32 num_names;
1248 char **names;
1249 uint32 *name_types;
1250 int i;
1252 /* Get sam policy handle */
1254 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1255 &connect_pol);
1256 if (!NT_STATUS_IS_OK(result)) {
1257 goto done;
1260 /* Get domain policy handle */
1262 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1263 MAXIMUM_ALLOWED_ACCESS,
1264 domain_sid, &domain_pol);
1265 if (!NT_STATUS_IS_OK(result)) {
1266 goto done;
1269 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1270 1, argv, &num_rids, &rids, &rid_types);
1272 if (!NT_STATUS_IS_OK(result)) {
1273 goto done;
1276 if (num_rids != 1) {
1277 d_printf("Could not find group %s\n", argv[0]);
1278 goto done;
1281 if (rid_types[0] != SID_NAME_DOM_GRP) {
1282 d_printf("%s is not a domain group\n", argv[0]);
1283 goto done;
1286 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1287 MAXIMUM_ALLOWED_ACCESS,
1288 rids[0], &group_pol);
1290 if (!NT_STATUS_IS_OK(result))
1291 goto done;
1293 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
1294 &num_members, &group_rids,
1295 &group_attrs);
1297 if (!NT_STATUS_IS_OK(result))
1298 goto done;
1300 while (num_members > 0) {
1301 int this_time = 512;
1303 if (num_members < this_time)
1304 this_time = num_members;
1306 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol, 1000,
1307 this_time, group_rids,
1308 &num_names, &names, &name_types);
1310 if (!NT_STATUS_IS_OK(result))
1311 goto done;
1313 for (i = 0; i < this_time; i++) {
1314 printf("%s\n", names[i]);
1317 num_members -= this_time;
1318 group_rids += 512;
1321 done:
1322 return result;
1325 static int rpc_group_members(int argc, const char **argv)
1327 if (argc != 1) {
1328 return rpc_group_usage(argc, argv);
1331 return run_rpc_command(NULL, PI_SAMR, 0,
1332 rpc_group_members_internals,
1333 argc, argv);
1336 /**
1337 * 'net rpc group' entrypoint.
1338 * @param argc Standard main() style argc
1339 * @param argc Standard main() style argv. Initial components are already
1340 * stripped
1343 int net_rpc_group(int argc, const char **argv)
1345 struct functable func[] = {
1346 #if 0
1347 {"add", rpc_group_add},
1348 {"delete", rpc_group_delete},
1349 #endif
1350 {"list", rpc_group_list},
1351 {"members", rpc_group_members},
1352 {NULL, NULL}
1355 if (argc == 0) {
1356 if (opt_long_list_entries) {
1357 } else {
1359 return run_rpc_command(NULL, PI_SAMR, 0,
1360 rpc_group_list_internals,
1361 argc, argv);
1364 return net_run_function(argc, argv, func, rpc_group_usage);
1367 /****************************************************************************/
1369 static int rpc_share_usage(int argc, const char **argv)
1371 return net_help_share(argc, argv);
1374 /**
1375 * Add a share on a remote RPC server
1377 * All parameters are provided by the run_rpc_command function, except for
1378 * argc, argv which are passes through.
1380 * @param domain_sid The domain sid acquired from the remote server
1381 * @param cli A cli_state connected to the server.
1382 * @param mem_ctx Talloc context, destoyed on completion of the function.
1383 * @param argc Standard main() style argc
1384 * @param argv Standard main() style argv. Initial components are already
1385 * stripped
1387 * @return Normal NTSTATUS return.
1389 static NTSTATUS
1390 rpc_share_add_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1391 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1393 WERROR result;
1394 char *sharename=talloc_strdup(mem_ctx, argv[0]);
1395 char *path;
1396 uint32 type=0; /* only allow disk shares to be added */
1397 uint32 num_users=0, perms=0;
1398 char *password=NULL; /* don't allow a share password */
1400 path = strchr(sharename, '=');
1401 if (!path)
1402 return NT_STATUS_UNSUCCESSFUL;
1403 *path++ = '\0';
1405 result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
1406 opt_comment, perms, opt_maxusers,
1407 num_users, path, password);
1408 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1411 static int rpc_share_add(int argc, const char **argv)
1413 if ((argc < 1) || !strchr(argv[0], '=')) {
1414 DEBUG(1,("Sharename or path not specified on add\n"));
1415 return rpc_share_usage(argc, argv);
1417 return run_rpc_command(NULL, PI_SRVSVC, 0,
1418 rpc_share_add_internals,
1419 argc, argv);
1422 /**
1423 * Delete a share on a remote RPC server
1425 * All parameters are provided by the run_rpc_command function, except for
1426 * argc, argv which are passes through.
1428 * @param domain_sid The domain sid acquired from the remote server
1429 * @param cli A cli_state connected to the server.
1430 * @param mem_ctx Talloc context, destoyed on completion of the function.
1431 * @param argc Standard main() style argc
1432 * @param argv Standard main() style argv. Initial components are already
1433 * stripped
1435 * @return Normal NTSTATUS return.
1437 static NTSTATUS
1438 rpc_share_del_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1439 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1441 WERROR result;
1443 result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
1444 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1447 /**
1448 * Delete a share on a remote RPC server
1450 * @param domain_sid The domain sid acquired from the remote server
1451 * @param argc Standard main() style argc
1452 * @param argv Standard main() style argv. Initial components are already
1453 * stripped
1455 * @return A shell status integer (0 for success)
1457 static int rpc_share_delete(int argc, const char **argv)
1459 if (argc < 1) {
1460 DEBUG(1,("Sharename not specified on delete\n"));
1461 return rpc_share_usage(argc, argv);
1463 return run_rpc_command(NULL, PI_SRVSVC, 0,
1464 rpc_share_del_internals,
1465 argc, argv);
1469 * Formatted print of share info
1471 * @param info1 pointer to SRV_SHARE_INFO_1 to format
1474 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
1476 fstring netname = "", remark = "";
1478 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
1479 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
1481 if (opt_long_list_entries) {
1482 d_printf("%-12.12s %-8.8s %-50.50s\n",
1483 netname, share_type[info1->info_1.type], remark);
1484 } else {
1485 d_printf("%-12.12s\n", netname);
1490 /**
1491 * List shares on a remote RPC server
1493 * All parameters are provided by the run_rpc_command function, except for
1494 * argc, argv which are passes through.
1496 * @param domain_sid The domain sid acquired from the remote server
1497 * @param cli A cli_state connected to the server.
1498 * @param mem_ctx Talloc context, destoyed on completion of the function.
1499 * @param argc Standard main() style argc
1500 * @param argv Standard main() style argv. Initial components are already
1501 * stripped
1503 * @return Normal NTSTATUS return.
1506 static NTSTATUS
1507 rpc_share_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1508 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1510 SRV_SHARE_INFO_CTR ctr;
1511 WERROR result;
1512 ENUM_HND hnd;
1513 uint32 preferred_len = 0xffffffff, i;
1515 init_enum_hnd(&hnd, 0);
1517 result = cli_srvsvc_net_share_enum(
1518 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
1520 if (!W_ERROR_IS_OK(result))
1521 goto done;
1523 /* Display results */
1525 if (opt_long_list_entries) {
1526 d_printf(
1527 "\nEnumerating shared resources (exports) on remote server:\n\n"\
1528 "\nShare name Type Description\n"\
1529 "---------- ---- -----------\n");
1531 for (i = 0; i < ctr.num_entries; i++)
1532 display_share_info_1(&ctr.share.info1[i]);
1533 done:
1534 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1537 /**
1538 * 'net rpc share' entrypoint.
1539 * @param argc Standard main() style argc
1540 * @param argv Standard main() style argv. Initial components are already
1541 * stripped
1544 int net_rpc_share(int argc, const char **argv)
1546 struct functable func[] = {
1547 {"add", rpc_share_add},
1548 {"delete", rpc_share_delete},
1549 {NULL, NULL}
1552 if (argc == 0)
1553 return run_rpc_command(NULL, PI_SRVSVC, 0,
1554 rpc_share_list_internals,
1555 argc, argv);
1557 return net_run_function(argc, argv, func, rpc_share_usage);
1560 /****************************************************************************/
1562 static int rpc_file_usage(int argc, const char **argv)
1564 return net_help_file(argc, argv);
1567 /**
1568 * Close a file on a remote RPC server
1570 * All parameters are provided by the run_rpc_command function, except for
1571 * argc, argv which are passes through.
1573 * @param domain_sid The domain sid acquired from the remote server
1574 * @param cli A cli_state connected to the server.
1575 * @param mem_ctx Talloc context, destoyed on completion of the function.
1576 * @param argc Standard main() style argc
1577 * @param argv Standard main() style argv. Initial components are already
1578 * stripped
1580 * @return Normal NTSTATUS return.
1582 static NTSTATUS
1583 rpc_file_close_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1584 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1586 WERROR result;
1587 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
1588 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1591 /**
1592 * Close a file on a remote RPC server
1594 * @param argc Standard main() style argc
1595 * @param argv Standard main() style argv. Initial components are already
1596 * stripped
1598 * @return A shell status integer (0 for success)
1600 static int rpc_file_close(int argc, const char **argv)
1602 if (argc < 1) {
1603 DEBUG(1, ("No fileid given on close\n"));
1604 return(rpc_file_usage(argc, argv));
1607 return run_rpc_command(NULL, PI_SRVSVC, 0,
1608 rpc_file_close_internals,
1609 argc, argv);
1612 /**
1613 * Formatted print of open file info
1615 * @param info3 FILE_INFO_3 contents
1616 * @param str3 strings for FILE_INFO_3
1619 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
1621 fstring user = "", path = "";
1623 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
1624 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
1626 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
1627 info3->id, user, info3->perms, info3->num_locks, path);
1630 /**
1631 * List open files on a remote RPC server
1633 * All parameters are provided by the run_rpc_command function, except for
1634 * argc, argv which are passes through.
1636 * @param domain_sid The domain sid acquired from the remote server
1637 * @param cli A cli_state connected to the server.
1638 * @param mem_ctx Talloc context, destoyed on completion of the function.
1639 * @param argc Standard main() style argc
1640 * @param argv Standard main() style argv. Initial components are already
1641 * stripped
1643 * @return Normal NTSTATUS return.
1646 static NTSTATUS
1647 rpc_file_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1648 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1650 SRV_FILE_INFO_CTR ctr;
1651 WERROR result;
1652 ENUM_HND hnd;
1653 uint32 preferred_len = 0xffffffff, i;
1654 const char *username=NULL;
1656 init_enum_hnd(&hnd, 0);
1658 /* if argc > 0, must be user command */
1659 if (argc > 0)
1660 username = smb_xstrdup(argv[0]);
1662 result = cli_srvsvc_net_file_enum(
1663 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
1665 if (!W_ERROR_IS_OK(result))
1666 goto done;
1668 /* Display results */
1670 d_printf(
1671 "\nEnumerating open files on remote server:\n\n"\
1672 "\nFileId Opened by Perms Locks Path"\
1673 "\n------ --------- ----- ----- ---- \n");
1674 for (i = 0; i < ctr.num_entries; i++)
1675 display_file_info_3(&ctr.file.info3[i].info_3,
1676 &ctr.file.info3[i].info_3_str);
1677 done:
1678 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1682 /**
1683 * List files for a user on a remote RPC server
1685 * @param argc Standard main() style argc
1686 * @param argv Standard main() style argv. Initial components are already
1687 * stripped
1689 * @return A shell status integer (0 for success)
1691 static int rpc_file_user(int argc, const char **argv)
1693 if (argc < 1) {
1694 DEBUG(1, ("No username given\n"));
1695 return(rpc_file_usage(argc, argv));
1698 return run_rpc_command(NULL, PI_SRVSVC, 0,
1699 rpc_file_list_internals,
1700 argc, argv);
1704 /**
1705 * 'net rpc file' entrypoint.
1706 * @param argc Standard main() style argc
1707 * @param argv Standard main() style argv. Initial components are already
1708 * stripped
1711 int net_rpc_file(int argc, const char **argv)
1713 struct functable func[] = {
1714 {"close", rpc_file_close},
1715 {"user", rpc_file_user},
1716 #if 0
1717 {"info", rpc_file_info},
1718 #endif
1719 {NULL, NULL}
1722 if (argc == 0)
1723 return run_rpc_command(NULL, PI_SRVSVC, 0,
1724 rpc_file_list_internals,
1725 argc, argv);
1727 return net_run_function(argc, argv, func, rpc_file_usage);
1730 /****************************************************************************/
1734 /**
1735 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
1737 * All parameters are provided by the run_rpc_command function, except for
1738 * argc, argv which are passed through.
1740 * @param domain_sid The domain sid aquired from the remote server
1741 * @param cli A cli_state connected to the server.
1742 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1743 * @param argc Standard main() style argc
1744 * @param argv Standard main() style argv. Initial components are already
1745 * stripped
1747 * @return Normal NTSTATUS return.
1750 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
1751 struct cli_state *cli,
1752 TALLOC_CTX *mem_ctx,
1753 int argc, const char **argv)
1755 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1757 result = cli_shutdown_abort(cli, mem_ctx);
1759 if (NT_STATUS_IS_OK(result))
1760 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
1761 else
1762 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
1764 return result;
1768 /**
1769 * ABORT the shutdown of a remote RPC Server, over winreg pipe
1771 * All parameters are provided by the run_rpc_command function, except for
1772 * argc, argv which are passed through.
1774 * @param domain_sid The domain sid aquired from the remote server
1775 * @param cli A cli_state connected to the server.
1776 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1777 * @param argc Standard main() style argc
1778 * @param argv Standard main() style argv. Initial components are already
1779 * stripped
1781 * @return Normal NTSTATUS return.
1784 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
1785 struct cli_state *cli,
1786 TALLOC_CTX *mem_ctx,
1787 int argc, const char **argv)
1789 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1791 result = cli_reg_abort_shutdown(cli, mem_ctx);
1793 if (NT_STATUS_IS_OK(result))
1794 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
1795 else
1796 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
1798 return result;
1801 /**
1802 * ABORT the Shut down of a remote RPC server
1804 * @param argc Standard main() style argc
1805 * @param argv Standard main() style argv. Initial components are already
1806 * stripped
1808 * @return A shell status integer (0 for success)
1811 static int rpc_shutdown_abort(int argc, const char **argv)
1813 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
1814 rpc_shutdown_abort_internals,
1815 argc, argv);
1817 if (rc == 0)
1818 return rc;
1820 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
1822 return run_rpc_command(NULL, PI_WINREG, 0,
1823 rpc_reg_shutdown_abort_internals,
1824 argc, argv);
1827 /**
1828 * Shut down a remote RPC Server
1830 * All parameters are provided by the run_rpc_command function, except for
1831 * argc, argv which are passes through.
1833 * @param domain_sid The domain sid aquired from the remote server
1834 * @param cli A cli_state connected to the server.
1835 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1836 * @param argc Standard main() style argc
1837 * @param argc Standard main() style argv. Initial components are already
1838 * stripped
1840 * @return Normal NTSTATUS return.
1843 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1844 int argc, const char **argv)
1846 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1847 const char *msg = "This machine will be shutdown shortly";
1848 uint32 timeout = 20;
1849 #if 0
1850 poptContext pc;
1851 int rc;
1853 struct poptOption long_options[] = {
1854 {"message", 'm', POPT_ARG_STRING, &msg},
1855 {"timeout", 't', POPT_ARG_INT, &timeout},
1856 {"reboot", 'r', POPT_ARG_NONE, &reboot},
1857 {"force", 'f', POPT_ARG_NONE, &force},
1858 { 0, 0, 0, 0}
1861 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
1862 POPT_CONTEXT_KEEP_FIRST);
1864 rc = poptGetNextOpt(pc);
1866 if (rc < -1) {
1867 /* an error occurred during option processing */
1868 DEBUG(0, ("%s: %s\n",
1869 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1870 poptStrerror(rc)));
1871 return NT_STATUS_INVALID_PARAMETER;
1873 #endif
1874 if (opt_comment) {
1875 msg = opt_comment;
1877 if (opt_timeout) {
1878 timeout = opt_timeout;
1881 /* create an entry */
1882 result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force);
1884 if (NT_STATUS_IS_OK(result))
1885 DEBUG(5,("Shutdown of remote machine succeeded\n"));
1886 else
1887 DEBUG(0,("Shutdown of remote machine failed!\n"));
1889 return result;
1892 /**
1893 * Shut down a remote RPC server
1895 * @param argc Standard main() style argc
1896 * @param argc Standard main() style argv. Initial components are already
1897 * stripped
1899 * @return A shell status integer (0 for success)
1902 static int rpc_shutdown(int argc, const char **argv)
1904 return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_internals,
1905 argc, argv);
1908 /***************************************************************************
1909 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
1911 ***************************************************************************/
1914 * Add interdomain trust account to the RPC server.
1915 * All parameters (except for argc and argv) are passed by run_rpc_command
1916 * function.
1918 * @param domain_sid The domain sid acquired from the server
1919 * @param cli A cli_state connected to the server.
1920 * @param mem_ctx Talloc context, destoyed on completion of the function.
1921 * @param argc Standard main() style argc
1922 * @param argc Standard main() style argv. Initial components are already
1923 * stripped
1925 * @return normal NTSTATUS return code
1928 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1929 int argc, const char **argv) {
1931 POLICY_HND connect_pol, domain_pol, user_pol;
1932 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1933 char *acct_name;
1934 uint16 acb_info;
1935 uint32 unknown, user_rid;
1937 if (argc != 2) {
1938 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
1939 return NT_STATUS_INVALID_PARAMETER;
1943 * Make valid trusting domain account (ie. uppercased and with '$' appended)
1946 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
1947 return NT_STATUS_NO_MEMORY;
1950 strupper_m(acct_name);
1952 /* Get samr policy handle */
1953 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1954 &connect_pol);
1955 if (!NT_STATUS_IS_OK(result)) {
1956 goto done;
1959 /* Get domain policy handle */
1960 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1961 MAXIMUM_ALLOWED_ACCESS,
1962 domain_sid, &domain_pol);
1963 if (!NT_STATUS_IS_OK(result)) {
1964 goto done;
1967 /* Create trusting domain's account */
1968 acb_info = ACB_DOMTRUST;
1969 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
1970 mimir: yes, most probably it is */
1972 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
1973 acct_name, acb_info, unknown,
1974 &user_pol, &user_rid);
1975 if (!NT_STATUS_IS_OK(result)) {
1976 goto done;
1980 SAM_USERINFO_CTR ctr;
1981 SAM_USER_INFO_24 p24;
1982 uchar pwbuf[516];
1984 encode_pw_buffer((char *)pwbuf, argv[1], STR_UNICODE);
1986 ZERO_STRUCT(ctr);
1987 ZERO_STRUCT(p24);
1989 init_sam_user_info24(&p24, (char *)pwbuf, 24);
1991 ctr.switch_value = 24;
1992 ctr.info.id24 = &p24;
1994 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
1995 &cli->user_session_key, &ctr);
1997 if (!NT_STATUS_IS_OK(result)) {
1998 DEBUG(0,("Could not set trust account password: %s\n",
1999 nt_errstr(result)));
2000 goto done;
2004 done:
2005 SAFE_FREE(acct_name);
2006 return result;
2010 * Create interdomain trust account for a remote domain.
2012 * @param argc standard argc
2013 * @param argv standard argv without initial components
2015 * @return Integer status (0 means success)
2018 static int rpc_trustdom_add(int argc, const char **argv)
2020 if (argc > 0) {
2021 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
2022 argc, argv);
2023 } else {
2024 d_printf("Usage: net rpc trustdom add <domain>\n");
2025 return -1;
2031 * Delete interdomain trust account for a remote domain.
2033 * @param argc standard argc
2034 * @param argv standard argv without initial components
2036 * @return Integer status (0 means success)
2039 static int rpc_trustdom_del(int argc, const char **argv)
2041 d_printf("Sorry, not yet implemented.\n");
2042 d_printf("Use 'smbpasswd -x -i' instead.\n");
2043 return -1;
2048 * Establish trust relationship to a trusting domain.
2049 * Interdomain account must already be created on remote PDC.
2051 * @param argc standard argc
2052 * @param argv standard argv without initial components
2054 * @return Integer status (0 means success)
2057 static int rpc_trustdom_establish(int argc, const char **argv)
2059 struct cli_state *cli;
2060 struct in_addr server_ip;
2061 POLICY_HND connect_hnd;
2062 TALLOC_CTX *mem_ctx;
2063 NTSTATUS nt_status;
2064 DOM_SID *domain_sid;
2065 WKS_INFO_100 wks_info;
2067 char* domain_name;
2068 char* domain_name_pol;
2069 char* acct_name;
2070 fstring pdc_name;
2073 * Connect to \\server\ipc$ as 'our domain' account with password
2076 if (argc != 1) {
2077 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
2078 return -1;
2081 domain_name = smb_xstrdup(argv[0]);
2082 strupper_m(domain_name);
2084 /* account name used at first is our domain's name with '$' */
2085 asprintf(&acct_name, "%s$", lp_workgroup());
2086 strupper_m(acct_name);
2089 * opt_workgroup will be used by connection functions further,
2090 * hence it should be set to remote domain name instead of ours
2092 if (opt_workgroup) {
2093 opt_workgroup = smb_xstrdup(domain_name);
2096 opt_user_name = acct_name;
2098 /* find the domain controller */
2099 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
2100 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
2101 return -1;
2104 /* connect to ipc$ as username/password */
2105 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
2106 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
2108 /* Is it trusting domain account for sure ? */
2109 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
2110 nt_errstr(nt_status)));
2111 return -1;
2115 * Connect to \\server\ipc$ again (this time anonymously)
2118 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
2120 if (NT_STATUS_IS_ERR(nt_status)) {
2121 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
2122 domain_name, nt_errstr(nt_status)));
2126 * Use NetServerEnum2 to make sure we're talking to a proper server
2129 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
2130 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
2131 for domain %s\n", domain_name));
2135 * Call WksQueryInfo to check remote server's capabilities
2136 * note: It is now used only to get unicode domain name
2139 if (!cli_nt_session_open(cli, PI_WKSSVC)) {
2140 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
2141 return -1;
2144 if (!(mem_ctx = talloc_init("establishing trust relationship to domain %s",
2145 domain_name))) {
2146 DEBUG(0, ("talloc_init() failed\n"));
2147 cli_shutdown(cli);
2148 return -1;
2151 nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
2153 if (NT_STATUS_IS_ERR(nt_status)) {
2154 DEBUG(0, ("WksQueryInfo call failed.\n"));
2155 return -1;
2158 if (cli->nt_pipe_fnum)
2159 cli_nt_session_close(cli);
2163 * Call LsaOpenPolicy and LsaQueryInfo
2166 if (!(mem_ctx = talloc_init("rpc_trustdom_establish"))) {
2167 DEBUG(0, ("talloc_init() failed\n"));
2168 cli_shutdown(cli);
2169 return -1;
2172 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2173 DEBUG(0, ("Could not initialise lsa pipe\n"));
2174 cli_shutdown(cli);
2175 return -1;
2178 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
2179 &connect_hnd);
2180 if (NT_STATUS_IS_ERR(nt_status)) {
2181 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2182 nt_errstr(nt_status)));
2183 return -1;
2186 /* Querying info level 5 */
2188 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
2189 5 /* info level */, &domain_name_pol,
2190 &domain_sid);
2191 if (NT_STATUS_IS_ERR(nt_status)) {
2192 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2193 nt_errstr(nt_status)));
2194 return -1;
2200 /* There should be actually query info level 3 (following nt serv behaviour),
2201 but I still don't know if it's _really_ necessary */
2204 * Store the password in secrets db
2207 if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
2208 wks_info.uni_lan_grp.uni_str_len, opt_password,
2209 *domain_sid)) {
2210 DEBUG(0, ("Storing password for trusted domain failed.\n"));
2211 return -1;
2215 * Close the pipes and clean up
2218 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2219 if (NT_STATUS_IS_ERR(nt_status)) {
2220 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
2221 nt_errstr(nt_status)));
2222 return -1;
2225 if (cli->nt_pipe_fnum)
2226 cli_nt_session_close(cli);
2228 talloc_destroy(mem_ctx);
2230 DEBUG(0, ("Success!\n"));
2231 return 0;
2235 * Revoke trust relationship to the remote domain
2237 * @param argc standard argc
2238 * @param argv standard argv without initial components
2240 * @return Integer status (0 means success)
2243 static int rpc_trustdom_revoke(int argc, const char **argv)
2245 char* domain_name;
2247 if (argc < 1) return -1;
2249 /* generate upper cased domain name */
2250 domain_name = smb_xstrdup(argv[0]);
2251 strupper_m(domain_name);
2253 /* delete password of the trust */
2254 if (!trusted_domain_password_delete(domain_name)) {
2255 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
2256 domain_name));
2257 return -1;
2260 return 0;
2264 * Usage for 'net rpc trustdom' command
2266 * @param argc standard argc
2267 * @param argv standard argv without inital components
2269 * @return Integer status returned to shell
2272 static int rpc_trustdom_usage(int argc, const char **argv)
2274 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
2275 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
2276 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
2277 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
2278 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
2279 return -1;
2283 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
2284 int argc, const char **argv)
2286 fstring str_sid;
2287 sid_to_string(str_sid, domain_sid);
2288 d_printf("%s\n", str_sid);
2289 return NT_STATUS_OK;
2293 static int rpc_trustdom_list(int argc, const char **argv)
2295 /* common variables */
2296 TALLOC_CTX* mem_ctx;
2297 struct cli_state *cli, *remote_cli;
2298 NTSTATUS nt_status;
2299 const char *domain_name = NULL;
2300 DOM_SID *queried_dom_sid;
2301 fstring ascii_sid, padding;
2302 int ascii_dom_name_len;
2303 POLICY_HND connect_hnd;
2305 /* trusted domains listing variables */
2306 unsigned int num_domains, enum_ctx = 0;
2307 int i, pad_len, col_len = 20;
2308 DOM_SID *domain_sids;
2309 char **trusted_dom_names;
2310 fstring pdc_name;
2311 char *dummy;
2313 /* trusting domains listing variables */
2314 POLICY_HND domain_hnd;
2315 char **trusting_dom_names;
2316 uint32 *trusting_dom_rids;
2319 * Listing trusted domains (stored in secrets.tdb, if local)
2322 mem_ctx = talloc_init("trust relationships listing");
2325 * set domain and pdc name to local samba server (default)
2326 * or to remote one given in command line
2329 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
2330 domain_name = opt_workgroup;
2331 opt_target_workgroup = opt_workgroup;
2332 } else {
2333 fstrcpy(pdc_name, global_myname());
2334 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
2335 opt_target_workgroup = domain_name;
2338 /* open \PIPE\lsarpc and open policy handle */
2339 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
2340 DEBUG(0, ("Couldn't connect to domain controller\n"));
2341 return -1;
2344 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2345 DEBUG(0, ("Could not initialise lsa pipe\n"));
2346 return -1;
2349 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
2350 &connect_hnd);
2351 if (NT_STATUS_IS_ERR(nt_status)) {
2352 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2353 nt_errstr(nt_status)));
2354 return -1;
2357 /* query info level 5 to obtain sid of a domain being queried */
2358 nt_status = cli_lsa_query_info_policy(
2359 cli, mem_ctx, &connect_hnd, 5 /* info level */,
2360 &dummy, &queried_dom_sid);
2362 if (NT_STATUS_IS_ERR(nt_status)) {
2363 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2364 nt_errstr(nt_status)));
2365 return -1;
2369 * Keep calling LsaEnumTrustdom over opened pipe until
2370 * the end of enumeration is reached
2373 d_printf("Trusted domains list:\n\n");
2375 do {
2376 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
2377 &num_domains,
2378 &trusted_dom_names, &domain_sids);
2380 if (NT_STATUS_IS_ERR(nt_status)) {
2381 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
2382 nt_errstr(nt_status)));
2383 return -1;
2386 for (i = 0; i < num_domains; i++) {
2387 /* convert sid into ascii string */
2388 sid_to_string(ascii_sid, &(domain_sids[i]));
2390 /* calculate padding space for d_printf to look nicer */
2391 pad_len = col_len - strlen(trusted_dom_names[i]);
2392 padding[pad_len] = 0;
2393 do padding[--pad_len] = ' '; while (pad_len);
2395 d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
2399 * in case of no trusted domains say something rather
2400 * than just display blank line
2402 if (!num_domains) d_printf("none\n");
2404 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2406 /* close this connection before doing next one */
2407 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2408 if (NT_STATUS_IS_ERR(nt_status)) {
2409 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
2410 nt_errstr(nt_status)));
2411 return -1;
2414 cli_nt_session_close(cli);
2417 * Listing trusting domains (stored in passdb backend, if local)
2420 d_printf("\nTrusting domains list:\n\n");
2423 * Open \PIPE\samr and get needed policy handles
2425 if (!cli_nt_session_open(cli, PI_SAMR)) {
2426 DEBUG(0, ("Could not initialise samr pipe\n"));
2427 return -1;
2430 /* SamrConnect */
2431 nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
2432 &connect_hnd);
2433 if (!NT_STATUS_IS_OK(nt_status)) {
2434 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
2435 nt_errstr(nt_status)));
2436 return -1;
2439 /* SamrOpenDomain - we have to open domain policy handle in order to be
2440 able to enumerate accounts*/
2441 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
2442 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
2443 queried_dom_sid, &domain_hnd);
2444 if (!NT_STATUS_IS_OK(nt_status)) {
2445 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
2446 nt_errstr(nt_status)));
2447 return -1;
2451 * perform actual enumeration
2454 enum_ctx = 0; /* reset enumeration context from last enumeration */
2455 do {
2457 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
2458 &enum_ctx, ACB_DOMTRUST, 0xffff,
2459 &trusting_dom_names, &trusting_dom_rids,
2460 &num_domains);
2461 if (NT_STATUS_IS_ERR(nt_status)) {
2462 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
2463 nt_errstr(nt_status)));
2464 return -1;
2467 for (i = 0; i < num_domains; i++) {
2470 * get each single domain's sid (do we _really_ need this ?):
2471 * 1) connect to domain's pdc
2472 * 2) query the pdc for domain's sid
2475 /* get rid of '$' tail */
2476 ascii_dom_name_len = strlen(trusting_dom_names[i]);
2477 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
2478 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
2480 /* calculate padding space for d_printf to look nicer */
2481 pad_len = col_len - strlen(trusting_dom_names[i]);
2482 padding[pad_len] = 0;
2483 do padding[--pad_len] = ' '; while (pad_len);
2485 /* set opt_* variables to remote domain */
2486 strupper_m(trusting_dom_names[i]);
2487 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
2488 opt_target_workgroup = opt_workgroup;
2490 d_printf("%s%s", trusting_dom_names[i], padding);
2492 /* connect to remote domain controller */
2493 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
2494 if (remote_cli) {
2495 /* query for domain's sid */
2496 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
2497 d_printf("couldn't get domain's sid\n");
2499 cli_shutdown(remote_cli);
2501 } else {
2502 d_printf("domain controller is not responding\n");
2506 if (!num_domains) d_printf("none\n");
2508 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2510 /* close opened samr and domain policy handles */
2511 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
2512 if (!NT_STATUS_IS_OK(nt_status)) {
2513 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
2516 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
2517 if (!NT_STATUS_IS_OK(nt_status)) {
2518 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
2521 /* close samr pipe and connection to IPC$ */
2522 cli_nt_session_close(cli);
2523 cli_shutdown(cli);
2525 talloc_destroy(mem_ctx);
2526 return 0;
2530 * Entrypoint for 'net rpc trustdom' code
2532 * @param argc standard argc
2533 * @param argv standard argv without initial components
2535 * @return Integer status (0 means success)
2538 static int rpc_trustdom(int argc, const char **argv)
2540 struct functable func[] = {
2541 {"add", rpc_trustdom_add},
2542 {"del", rpc_trustdom_del},
2543 {"establish", rpc_trustdom_establish},
2544 {"revoke", rpc_trustdom_revoke},
2545 {"help", rpc_trustdom_usage},
2546 {"list", rpc_trustdom_list},
2547 {NULL, NULL}
2550 if (argc == 0) {
2551 rpc_trustdom_usage(argc, argv);
2552 return -1;
2555 return (net_run_function(argc, argv, func, rpc_user_usage));
2559 * Check if a server will take rpc commands
2560 * @param flags Type of server to connect to (PDC, DMB, localhost)
2561 * if the host is not explicitly specified
2562 * @return BOOL (true means rpc supported)
2564 BOOL net_rpc_check(unsigned flags)
2566 struct cli_state cli;
2567 BOOL ret = False;
2568 struct in_addr server_ip;
2569 char *server_name = NULL;
2571 /* flags (i.e. server type) may depend on command */
2572 if (!net_find_server(flags, &server_ip, &server_name))
2573 return False;
2575 ZERO_STRUCT(cli);
2576 if (cli_initialise(&cli) == False)
2577 return False;
2579 if (!cli_connect(&cli, server_name, &server_ip))
2580 goto done;
2581 if (!attempt_netbios_session_request(&cli, global_myname(),
2582 server_name, &server_ip))
2583 goto done;
2584 if (!cli_negprot(&cli))
2585 goto done;
2586 if (cli.protocol < PROTOCOL_NT1)
2587 goto done;
2589 ret = True;
2590 done:
2591 cli_shutdown(&cli);
2592 return ret;
2596 /****************************************************************************/
2599 /**
2600 * Basic usage function for 'net rpc'
2601 * @param argc Standard main() style argc
2602 * @param argv Standard main() style argv. Initial components are already
2603 * stripped
2606 int net_rpc_usage(int argc, const char **argv)
2608 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
2609 d_printf(" net rpc join \t\t\tto join a domain \n");
2610 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n\n\n");
2611 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
2612 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
2613 d_printf(" net rpc group \t\tto list groups\n");
2614 d_printf(" net rpc share \t\tto add, delete, and list shares\n");
2615 d_printf(" net rpc file \t\t\tto list open files\n");
2616 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
2617 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
2618 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
2619 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
2620 d_printf(" net rpc trustdom \t\tto create trusting domain's account\n"
2621 "\t\t\t\t\tor establish trust\n");
2622 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
2623 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
2624 d_printf("\n");
2625 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
2626 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
2627 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
2628 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
2629 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
2630 return -1;
2635 * Help function for 'net rpc'. Calls command specific help if requested
2636 * or displays usage of net rpc
2637 * @param argc Standard main() style argc
2638 * @param argv Standard main() style argv. Initial components are already
2639 * stripped
2642 int net_rpc_help(int argc, const char **argv)
2644 struct functable func[] = {
2645 {"join", rpc_join_usage},
2646 {"user", rpc_user_usage},
2647 {"group", rpc_group_usage},
2648 {"share", rpc_share_usage},
2649 /*{"changetrustpw", rpc_changetrustpw_usage}, */
2650 {"trustdom", rpc_trustdom_usage},
2651 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
2652 /*{"shutdown", rpc_shutdown_usage}, */
2653 {NULL, NULL}
2656 if (argc == 0) {
2657 net_rpc_usage(argc, argv);
2658 return -1;
2661 return (net_run_function(argc, argv, func, rpc_user_usage));
2665 /**
2666 * 'net rpc' entrypoint.
2667 * @param argc Standard main() style argc
2668 * @param argv Standard main() style argv. Initial components are already
2669 * stripped
2672 int net_rpc(int argc, const char **argv)
2674 struct functable func[] = {
2675 {"info", net_rpc_info},
2676 {"join", net_rpc_join},
2677 {"oldjoin", net_rpc_oldjoin},
2678 {"testjoin", net_rpc_testjoin},
2679 {"user", net_rpc_user},
2680 {"password", rpc_user_password},
2681 {"group", net_rpc_group},
2682 {"share", net_rpc_share},
2683 {"file", net_rpc_file},
2684 {"changetrustpw", net_rpc_changetrustpw},
2685 {"trustdom", rpc_trustdom},
2686 {"abortshutdown", rpc_shutdown_abort},
2687 {"shutdown", rpc_shutdown},
2688 {"samdump", rpc_samdump},
2689 {"vampire", rpc_vampire},
2690 {"getsid", net_rpc_getsid},
2691 {"help", net_rpc_help},
2692 {NULL, NULL}
2694 return net_run_function(argc, argv, func, net_rpc_usage);