merge of new client side support the Win2k LSARPC UUID in rpcbind
[Samba/gbeck.git] / source3 / utils / net_rpc.c
blob06538797e2f0ad59c95db0a8d925a27058cd17e9
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 extern pstring global_myname;
26 /**
27 * @file net_rpc.c
29 * @brief RPC based subcommands for the 'net' utility.
31 * This file should contain much of the functionality that used to
32 * be found in rpcclient, execpt that the commands should change
33 * less often, and the fucntionality should be sane (the user is not
34 * expected to know a rid/sid before they conduct an operation etc.)
36 * @todo Perhaps eventually these should be split out into a number
37 * of files, as this could get quite big.
38 **/
41 /* A function of this type is passed to the 'run_rpc_command' wrapper */
42 typedef NTSTATUS (*rpc_command_fn)(const DOM_SID *, struct cli_state *, TALLOC_CTX *, int, const char **);
44 /**
45 * Many of the RPC functions need the domain sid. This function gets
46 * it at the start of every run
48 * @param cli A cli_state already connected to the remote machine
50 * @return The Domain SID of the remote machine.
51 **/
53 static DOM_SID *net_get_remote_domain_sid(struct cli_state *cli)
55 DOM_SID *domain_sid;
56 POLICY_HND pol;
57 NTSTATUS result = NT_STATUS_OK;
58 uint32 info_class = 5;
59 fstring domain_name;
60 TALLOC_CTX *mem_ctx;
62 if (!(domain_sid = malloc(sizeof(DOM_SID)))){
63 DEBUG(0,("net_get_remote_domain_sid: malloc returned NULL!\n"));
64 goto error;
67 if (!(mem_ctx=talloc_init()))
69 DEBUG(0,("net_get_remote_domain_sid: talloc_init returned NULL!\n"));
70 goto error;
74 if (!cli_nt_session_open (cli, PI_LSARPC)) {
75 fprintf(stderr, "could not initialise lsa pipe\n");
76 goto error;
79 result = cli_lsa_open_policy(cli, mem_ctx, True,
80 SEC_RIGHTS_MAXIMUM_ALLOWED,
81 &pol);
82 if (!NT_STATUS_IS_OK(result)) {
83 goto error;
86 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
87 domain_name, domain_sid);
88 if (!NT_STATUS_IS_OK(result)) {
89 goto error;
92 cli_lsa_close(cli, mem_ctx, &pol);
93 cli_nt_session_close(cli);
94 talloc_destroy(mem_ctx);
96 return domain_sid;
98 error:
99 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
101 if (!NT_STATUS_IS_OK(result)) {
102 fprintf(stderr, "error: %s\n", nt_errstr(result));
105 exit(1);
109 * Run a single RPC command, from start to finish.
111 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
112 * @param conn_flag a NET_FLAG_ combination. Passed to
113 * net_make_ipc_connection.
114 * @param argc Standard main() style argc
115 * @param argc Standard main() style argv. Initial components are already
116 * stripped
117 * @return A shell status integer (0 for success)
120 static int run_rpc_command(struct cli_state *cli_arg, const int pipe_idx, int conn_flags,
121 rpc_command_fn fn,
122 int argc, const char **argv)
124 struct cli_state *cli = NULL;
125 TALLOC_CTX *mem_ctx;
126 NTSTATUS nt_status;
127 DOM_SID *domain_sid;
129 /* make use of cli_state handed over as an argument, if possible */
130 if (!cli_arg)
131 cli = net_make_ipc_connection(conn_flags);
132 else
133 cli = cli_arg;
135 if (!cli) {
136 return -1;
139 domain_sid = net_get_remote_domain_sid(cli);
141 /* Create mem_ctx */
143 if (!(mem_ctx = talloc_init())) {
144 DEBUG(0, ("talloc_init() failed\n"));
145 cli_shutdown(cli);
146 return -1;
149 if (!cli_nt_session_open(cli, pipe_idx)) {
150 DEBUG(0, ("Could not initialise pipe\n"));
153 nt_status = fn(domain_sid, cli, mem_ctx, argc, argv);
155 if (!NT_STATUS_IS_OK(nt_status)) {
156 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
157 } else {
158 DEBUG(5, ("rpc command function succedded\n"));
162 if (cli->nt_pipe_fnum)
163 cli_nt_session_close(cli);
165 /* close the connection only if it was opened here */
166 if (!cli_arg)
167 cli_shutdown(cli);
169 talloc_destroy(mem_ctx);
171 return (!NT_STATUS_IS_OK(nt_status));
175 /****************************************************************************/
178 /**
179 * Force a change of the trust acccount password.
181 * All parameters are provided by the run_rpc_command function, except for
182 * argc, argv which are passes through.
184 * @param domain_sid The domain sid aquired from the remote server
185 * @param cli A cli_state connected to the server.
186 * @param mem_ctx Talloc context, destoyed on compleation of the function.
187 * @param argc Standard main() style argc
188 * @param argc Standard main() style argv. Initial components are already
189 * stripped
191 * @return Normal NTSTATUS return.
194 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
195 int argc, const char **argv) {
197 return trust_pw_find_change_and_store_it(cli, mem_ctx, opt_target_workgroup);
200 /**
201 * Force a change of the trust acccount password.
203 * @param argc Standard main() style argc
204 * @param argc Standard main() style argv. Initial components are already
205 * stripped
207 * @return A shell status integer (0 for success)
210 static int rpc_changetrustpw(int argc, const char **argv)
212 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, rpc_changetrustpw_internals,
213 argc, argv);
217 /****************************************************************************/
220 /**
221 * Join a domain, the old way.
223 * This uses 'machinename' as the inital password, and changes it.
225 * The password should be created with 'server manager' or eqiv first.
227 * All parameters are provided by the run_rpc_command function, except for
228 * argc, argv which are passes through.
230 * @param domain_sid The domain sid aquired from the remote server
231 * @param cli A cli_state connected to the server.
232 * @param mem_ctx Talloc context, destoyed on compleation of the function.
233 * @param argc Standard main() style argc
234 * @param argc Standard main() style argv. Initial components are already
235 * stripped
237 * @return Normal NTSTATUS return.
240 static NTSTATUS rpc_join_oldstyle_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
241 int argc, const char **argv) {
243 extern pstring global_myname;
244 fstring trust_passwd;
245 unsigned char orig_trust_passwd_hash[16];
246 NTSTATUS result;
248 fstrcpy(trust_passwd, global_myname);
249 strlower(trust_passwd);
252 * Machine names can be 15 characters, but the max length on
253 * a password is 14. --jerry
256 trust_passwd[14] = '\0';
258 E_md4hash(trust_passwd, orig_trust_passwd_hash);
260 result = trust_pw_change_and_store_it(cli, mem_ctx, orig_trust_passwd_hash);
262 if (NT_STATUS_IS_OK(result))
263 printf("Joined domain %s.\n",lp_workgroup());
265 return result;
268 /**
269 * Join a domain, the old way.
271 * @param argc Standard main() style argc
272 * @param argc Standard main() style argv. Initial components are already
273 * stripped
275 * @return A shell status integer (0 for success)
278 static int net_rpc_join_oldstyle(int argc, const char **argv)
280 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC, rpc_join_oldstyle_internals,
281 argc, argv);
284 /**
285 * Basic usage function for 'net rpc join'
286 * @param argc Standard main() style argc
287 * @param argc Standard main() style argv. Initial components are already
288 * stripped
291 static int rpc_join_usage(int argc, const char **argv)
293 d_printf("net rpc join -U <username>[%%password] [options]\n"\
294 "\t to join a domain with admin username & password\n"\
295 "\t\t password will be prompted if none is specified\n");
296 d_printf("net rpc join [options except -U]\n"\
297 "\t to join a domain created in server manager\n\n\n");
299 net_common_flags_usage(argc, argv);
300 return -1;
303 /**
304 * 'net rpc join' entrypoint.
305 * @param argc Standard main() style argc
306 * @param argc Standard main() style argv. Initial components are already
307 * stripped
309 * Main 'net_rpc_join()' (where the admain username/password is used) is
310 * in net_rpc_join.c
311 * Assume if a -U is specified, it's the new style, otherwise it's the
312 * old style. If 'oldstyle' is specfied explicity, do it and don't prompt.
315 int net_rpc_join(int argc, const char **argv)
317 struct functable func[] = {
318 {"oldstyle", net_rpc_join_oldstyle},
319 {NULL, NULL}
322 if (argc == 0) {
323 if ((net_rpc_join_oldstyle(argc, argv) == 0))
324 return 0;
326 return net_rpc_join_newstyle(argc, argv);
329 return net_run_function(argc, argv, func, rpc_join_usage);
334 /**
335 * display info about a rpc domain
337 * All parameters are provided by the run_rpc_command function, except for
338 * argc, argv which are passes through.
340 * @param domain_sid The domain sid acquired from the remote server
341 * @param cli A cli_state connected to the server.
342 * @param mem_ctx Talloc context, destoyed on completion of the function.
343 * @param argc Standard main() style argc
344 * @param argv Standard main() style argv. Initial components are already
345 * stripped
347 * @return Normal NTSTATUS return.
350 static NTSTATUS
351 rpc_info_internals(const DOM_SID *domain_sid, struct cli_state *cli,
352 TALLOC_CTX *mem_ctx, int argc, const char **argv)
354 POLICY_HND connect_pol, domain_pol;
355 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
356 SAM_UNK_CTR ctr;
357 fstring sid_str;
359 sid_to_string(sid_str, domain_sid);
361 /* Get sam policy handle */
362 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
363 &connect_pol);
364 if (!NT_STATUS_IS_OK(result)) {
365 goto done;
368 /* Get domain policy handle */
369 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
370 MAXIMUM_ALLOWED_ACCESS,
371 domain_sid, &domain_pol);
372 if (!NT_STATUS_IS_OK(result)) {
373 goto done;
376 ZERO_STRUCT(ctr);
377 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
378 2, &ctr);
379 if (NT_STATUS_IS_OK(result)) {
380 TALLOC_CTX *ctx = talloc_init();
381 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
382 d_printf("Domain SID: %s\n", sid_str);
383 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num);
384 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
385 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
386 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
387 talloc_destroy(ctx);
390 done:
391 return result;
395 /**
396 * 'net rpc info' entrypoint.
397 * @param argc Standard main() style argc
398 * @param argc Standard main() style argv. Initial components are already
399 * stripped
401 int net_rpc_info(int argc, const char **argv)
403 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
404 rpc_info_internals,
405 argc, argv);
409 /**
410 * Fetch domain SID into the local secrets.tdb
412 * All parameters are provided by the run_rpc_command function, except for
413 * argc, argv which are passes through.
415 * @param domain_sid The domain sid acquired from the remote server
416 * @param cli A cli_state connected to the server.
417 * @param mem_ctx Talloc context, destoyed on completion of the function.
418 * @param argc Standard main() style argc
419 * @param argv Standard main() style argv. Initial components are already
420 * stripped
422 * @return Normal NTSTATUS return.
425 static NTSTATUS
426 rpc_getsid_internals(const DOM_SID *domain_sid, struct cli_state *cli,
427 TALLOC_CTX *mem_ctx, int argc, const char **argv)
429 fstring sid_str;
431 sid_to_string(sid_str, domain_sid);
432 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
433 sid_str, lp_workgroup());
435 if (!secrets_store_domain_sid(global_myname, domain_sid)) {
436 DEBUG(0,("Can't store domain SID\n"));
437 return NT_STATUS_UNSUCCESSFUL;
440 return NT_STATUS_OK;
444 /**
445 * 'net rpc getsid' entrypoint.
446 * @param argc Standard main() style argc
447 * @param argc Standard main() style argv. Initial components are already
448 * stripped
450 int net_rpc_getsid(int argc, const char **argv)
452 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
453 rpc_getsid_internals,
454 argc, argv);
458 /****************************************************************************/
461 * Basic usage function for 'net rpc user'
462 * @param argc Standard main() style argc.
463 * @param argv Standard main() style argv. Initial components are already
464 * stripped.
467 static int rpc_user_usage(int argc, const char **argv)
469 return net_help_user(argc, argv);
472 /**
473 * Add a new user to a remote RPC server
475 * All parameters are provided by the run_rpc_command function, except for
476 * argc, argv which are passes through.
478 * @param domain_sid The domain sid acquired from the remote server
479 * @param cli A cli_state connected to the server.
480 * @param mem_ctx Talloc context, destoyed on completion of the function.
481 * @param argc Standard main() style argc
482 * @param argv Standard main() style argv. Initial components are already
483 * stripped
485 * @return Normal NTSTATUS return.
488 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
489 int argc, const char **argv) {
491 POLICY_HND connect_pol, domain_pol, user_pol;
492 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
493 const char *acct_name;
494 uint16 acb_info;
495 uint32 unknown, user_rid;
497 if (argc != 1) {
498 d_printf("User must be specified\n");
499 rpc_user_usage(argc, argv);
500 return NT_STATUS_OK;
503 acct_name = argv[0];
505 /* Get sam policy handle */
507 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
508 &connect_pol);
509 if (!NT_STATUS_IS_OK(result)) {
510 goto done;
513 /* Get domain policy handle */
515 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
516 MAXIMUM_ALLOWED_ACCESS,
517 domain_sid, &domain_pol);
518 if (!NT_STATUS_IS_OK(result)) {
519 goto done;
522 /* Create domain user */
524 acb_info = ACB_NORMAL;
525 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
527 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
528 acct_name, acb_info, unknown,
529 &user_pol, &user_rid);
530 if (!NT_STATUS_IS_OK(result)) {
531 goto done;
534 done:
535 if (!NT_STATUS_IS_OK(result)) {
536 d_printf("Failed to add user %s - %s\n", acct_name,
537 nt_errstr(result));
538 } else {
539 d_printf("Added user %s\n", acct_name);
541 return result;
544 /**
545 * Add a new user to a remote RPC server
547 * @param argc Standard main() style argc
548 * @param argv Standard main() style argv. Initial components are already
549 * stripped
551 * @return A shell status integer (0 for success)
554 static int rpc_user_add(int argc, const char **argv)
556 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
557 argc, argv);
560 /**
561 * Delete a user from a remote RPC server
563 * All parameters are provided by the run_rpc_command function, except for
564 * argc, argv which are passes through.
566 * @param domain_sid The domain sid acquired from the remote server
567 * @param cli A cli_state connected to the server.
568 * @param mem_ctx Talloc context, destoyed on completion of the function.
569 * @param argc Standard main() style argc
570 * @param argv Standard main() style argv. Initial components are already
571 * stripped
573 * @return Normal NTSTATUS return.
576 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
577 struct cli_state *cli,
578 TALLOC_CTX *mem_ctx,
579 int argc, const char **argv)
581 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
582 POLICY_HND connect_pol, domain_pol, user_pol;
584 if (argc < 1) {
585 d_printf("User must be specified\n");
586 rpc_user_usage(argc, argv);
587 return NT_STATUS_OK;
589 /* Get sam policy and domain handles */
591 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
592 &connect_pol);
594 if (!NT_STATUS_IS_OK(result)) {
595 goto done;
598 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
599 MAXIMUM_ALLOWED_ACCESS,
600 domain_sid, &domain_pol);
602 if (!NT_STATUS_IS_OK(result)) {
603 goto done;
606 /* Get handle on user */
609 uint32 *user_rids, num_rids, *name_types;
610 uint32 flags = 0x000003e8; /* Unknown */
612 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
613 flags, 1, &argv[0],
614 &num_rids, &user_rids,
615 &name_types);
617 if (!NT_STATUS_IS_OK(result)) {
618 goto done;
621 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
622 MAXIMUM_ALLOWED_ACCESS,
623 user_rids[0], &user_pol);
625 if (!NT_STATUS_IS_OK(result)) {
626 goto done;
630 /* Delete user */
632 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
634 if (!NT_STATUS_IS_OK(result)) {
635 goto done;
638 /* Display results */
640 done:
641 return result;
645 /**
646 * Delete a user from a remote RPC server
648 * @param argc Standard main() style argc
649 * @param argv Standard main() style argv. Initial components are already
650 * stripped
652 * @return A shell status integer (0 for success)
655 static int rpc_user_delete(int argc, const char **argv)
657 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
658 argc, argv);
661 /**
662 * List user's groups on a remote RPC server
664 * All parameters are provided by the run_rpc_command function, except for
665 * argc, argv which are passes through.
667 * @param domain_sid The domain sid acquired from the remote server
668 * @param cli A cli_state connected to the server.
669 * @param mem_ctx Talloc context, destoyed on completion of the function.
670 * @param argc Standard main() style argc
671 * @param argv Standard main() style argv. Initial components are already
672 * stripped
674 * @return Normal NTSTATUS return.
677 static NTSTATUS
678 rpc_user_info_internals(const DOM_SID *domain_sid, struct cli_state *cli,
679 TALLOC_CTX *mem_ctx, int argc, const char **argv)
681 POLICY_HND connect_pol, domain_pol, user_pol;
682 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
683 uint32 *rids, num_rids, *name_types, num_names;
684 uint32 flags = 0x000003e8; /* Unknown */
685 int i;
686 char **names;
687 DOM_GID *user_gids;
689 if (argc < 1) {
690 d_printf("User must be specified\n");
691 rpc_user_usage(argc, argv);
692 return NT_STATUS_OK;
694 /* Get sam policy handle */
696 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
697 &connect_pol);
698 if (!NT_STATUS_IS_OK(result)) goto done;
700 /* Get domain policy handle */
702 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
703 MAXIMUM_ALLOWED_ACCESS,
704 domain_sid, &domain_pol);
705 if (!NT_STATUS_IS_OK(result)) goto done;
707 /* Get handle on user */
709 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
710 flags, 1, &argv[0],
711 &num_rids, &rids, &name_types);
713 if (!NT_STATUS_IS_OK(result)) goto done;
715 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
716 MAXIMUM_ALLOWED_ACCESS,
717 rids[0], &user_pol);
718 if (!NT_STATUS_IS_OK(result)) goto done;
720 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
721 &num_rids, &user_gids);
723 /* Look up rids */
725 rids = (uint32 *)talloc(mem_ctx, sizeof(uint32) * num_rids);
727 for (i = 0; i < num_rids; i++)
728 rids[i] = user_gids[i].g_rid;
730 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
731 flags, num_rids, rids,
732 &num_names, &names, &name_types);
734 if (!NT_STATUS_IS_OK(result)) {
735 goto done;
738 /* Display results */
740 for (i = 0; i < num_names; i++)
741 printf("%s\n", names[i]);
743 done:
744 return result;
747 /**
748 * List a user's groups from a remote RPC server
750 * @param argc Standard main() style argc
751 * @param argv Standard main() style argv. Initial components are already
752 * stripped
754 * @return A shell status integer (0 for success)
757 static int rpc_user_info(int argc, const char **argv)
759 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
760 argc, argv);
763 /**
764 * List users on a remote RPC server
766 * All parameters are provided by the run_rpc_command function, except for
767 * argc, argv which are passes through.
769 * @param domain_sid The domain sid acquired from the remote server
770 * @param cli A cli_state connected to the server.
771 * @param mem_ctx Talloc context, destoyed on completion of the function.
772 * @param argc Standard main() style argc
773 * @param argv Standard main() style argv. Initial components are already
774 * stripped
776 * @return Normal NTSTATUS return.
779 static NTSTATUS
780 rpc_user_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
781 TALLOC_CTX *mem_ctx, int argc, const char **argv)
783 POLICY_HND connect_pol, domain_pol;
784 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
785 uint32 start_idx=0, max_entries=250, num_entries, i;
786 SAM_DISPINFO_CTR ctr;
787 SAM_DISPINFO_1 info1;
789 /* Get sam policy handle */
791 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
792 &connect_pol);
793 if (!NT_STATUS_IS_OK(result)) {
794 goto done;
797 /* Get domain policy handle */
799 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
800 MAXIMUM_ALLOWED_ACCESS,
801 domain_sid, &domain_pol);
802 if (!NT_STATUS_IS_OK(result)) {
803 goto done;
806 /* Query domain users */
807 ZERO_STRUCT(ctr);
808 ZERO_STRUCT(info1);
809 ctr.sam.info1 = &info1;
810 if (opt_long_list_entries)
811 d_printf("\nUser name Comment"\
812 "\n-----------------------------\n");
813 do {
814 fstring user, desc;
815 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
816 &start_idx, 1, &num_entries,
817 max_entries, &ctr);
818 for (i = 0; i < num_entries; i++) {
819 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
820 if (opt_long_list_entries)
821 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
823 if (opt_long_list_entries)
824 printf("%-21.21s %-50.50s\n", user, desc);
825 else
826 printf("%s\n", user);
828 } while (!NT_STATUS_IS_OK(result));
830 done:
831 return result;
834 /**
835 * 'net rpc user' entrypoint.
836 * @param argc Standard main() style argc
837 * @param argc Standard main() style argv. Initial components are already
838 * stripped
841 int net_rpc_user(int argc, const char **argv)
843 struct functable func[] = {
844 {"add", rpc_user_add},
845 {"info", rpc_user_info},
846 {"delete", rpc_user_delete},
847 {NULL, NULL}
850 if (argc == 0) {
851 if (opt_long_list_entries) {
852 } else {
854 return run_rpc_command(NULL,PI_SAMR, 0,
855 rpc_user_list_internals,
856 argc, argv);
859 return net_run_function(argc, argv, func, rpc_user_usage);
863 /****************************************************************************/
866 * Basic usage function for 'net rpc group'
867 * @param argc Standard main() style argc.
868 * @param argv Standard main() style argv. Initial components are already
869 * stripped.
872 static int rpc_group_usage(int argc, const char **argv)
874 return net_help_group(argc, argv);
877 /**
878 * List groups on a remote RPC server
880 * All parameters are provided by the run_rpc_command function, except for
881 * argc, argv which are passes through.
883 * @param domain_sid The domain sid acquired from the remote server
884 * @param cli A cli_state connected to the server.
885 * @param mem_ctx Talloc context, destoyed on completion of the function.
886 * @param argc Standard main() style argc
887 * @param argv Standard main() style argv. Initial components are already
888 * stripped
890 * @return Normal NTSTATUS return.
893 static NTSTATUS
894 rpc_group_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
895 TALLOC_CTX *mem_ctx, int argc, const char **argv)
897 POLICY_HND connect_pol, domain_pol;
898 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
899 uint32 start_idx=0, max_entries=250, num_entries, i;
900 struct acct_info *groups;
901 DOM_SID global_sid_Builtin;
903 string_to_sid(&global_sid_Builtin, "S-1-5-32");
905 /* Get sam policy handle */
907 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
908 &connect_pol);
909 if (!NT_STATUS_IS_OK(result)) {
910 goto done;
913 /* Get domain policy handle */
915 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
916 MAXIMUM_ALLOWED_ACCESS,
917 domain_sid, &domain_pol);
918 if (!NT_STATUS_IS_OK(result)) {
919 goto done;
922 /* Query domain groups */
923 if (opt_long_list_entries)
924 d_printf("\nGroup name Comment"\
925 "\n-----------------------------\n");
926 do {
927 result = cli_samr_enum_dom_groups(cli, mem_ctx, &domain_pol,
928 &start_idx, max_entries,
929 &groups, &num_entries);
931 for (i = 0; i < num_entries; i++) {
932 if (opt_long_list_entries)
933 printf("%-21.21s %-50.50s\n",
934 groups[i].acct_name,
935 groups[i].acct_desc);
936 else
937 printf("%-21.21s\n", groups[i].acct_name);
939 } while (!NT_STATUS_IS_OK(result));
940 /* query domain aliases */
941 do {
942 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
943 &start_idx, max_entries,
944 &groups, &num_entries);
946 for (i = 0; i < num_entries; i++) {
947 if (opt_long_list_entries)
948 printf("%-21.21s %-50.50s\n",
949 groups[i].acct_name,
950 groups[i].acct_desc);
951 else
952 printf("%-21.21s\n", groups[i].acct_name);
954 } while (!NT_STATUS_IS_OK(result));
955 cli_samr_close(cli, mem_ctx, &domain_pol);
956 /* Get builtin policy handle */
958 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
959 MAXIMUM_ALLOWED_ACCESS,
960 &global_sid_Builtin, &domain_pol);
961 if (!NT_STATUS_IS_OK(result)) {
962 goto done;
964 /* query builtin aliases */
965 do {
966 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
967 &start_idx, max_entries,
968 &groups, &num_entries);
970 for (i = 0; i < num_entries; i++) {
971 if (opt_long_list_entries)
972 printf("%-21.21s %-50.50s\n",
973 groups[i].acct_name,
974 groups[i].acct_desc);
975 else
976 printf("%s\n", groups[i].acct_name);
978 } while (!NT_STATUS_IS_OK(result));
980 done:
981 return result;
984 /**
985 * 'net rpc group' entrypoint.
986 * @param argc Standard main() style argc
987 * @param argc Standard main() style argv. Initial components are already
988 * stripped
991 int net_rpc_group(int argc, const char **argv)
993 struct functable func[] = {
994 #if 0
995 {"add", rpc_group_add},
996 {"delete", rpc_group_delete},
997 #endif
998 {NULL, NULL}
1001 if (argc == 0) {
1002 if (opt_long_list_entries) {
1003 } else {
1005 return run_rpc_command(NULL, PI_SAMR, 0,
1006 rpc_group_list_internals,
1007 argc, argv);
1010 return net_run_function(argc, argv, func, rpc_group_usage);
1013 /****************************************************************************/
1015 static int rpc_share_usage(int argc, const char **argv)
1017 return net_help_share(argc, argv);
1020 /**
1021 * Add a share on a remote RPC server
1023 * All parameters are provided by the run_rpc_command function, except for
1024 * argc, argv which are passes through.
1026 * @param domain_sid The domain sid acquired from the remote server
1027 * @param cli A cli_state connected to the server.
1028 * @param mem_ctx Talloc context, destoyed on completion of the function.
1029 * @param argc Standard main() style argc
1030 * @param argv Standard main() style argv. Initial components are already
1031 * stripped
1033 * @return Normal NTSTATUS return.
1035 static NTSTATUS
1036 rpc_share_add_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1037 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1039 WERROR result;
1040 char *sharename=talloc_strdup(mem_ctx, argv[0]);
1041 char *path;
1042 uint32 type=0; /* only allow disk shares to be added */
1043 uint32 num_users=0, perms=0;
1044 char *password=NULL; /* don't allow a share password */
1046 path = strchr(sharename, '=');
1047 if (!path)
1048 return NT_STATUS_UNSUCCESSFUL;
1049 *path++ = '\0';
1051 result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
1052 opt_comment, perms, opt_maxusers,
1053 num_users, path, password);
1054 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1057 static int rpc_share_add(int argc, const char **argv)
1059 if ((argc < 1) || !strchr(argv[0], '=')) {
1060 DEBUG(1,("Sharename or path not specified on add\n"));
1061 return rpc_share_usage(argc, argv);
1063 return run_rpc_command(NULL, PI_SRVSVC, 0,
1064 rpc_share_add_internals,
1065 argc, argv);
1068 /**
1069 * Delete a share on a remote RPC server
1071 * All parameters are provided by the run_rpc_command function, except for
1072 * argc, argv which are passes through.
1074 * @param domain_sid The domain sid acquired from the remote server
1075 * @param cli A cli_state connected to the server.
1076 * @param mem_ctx Talloc context, destoyed on completion of the function.
1077 * @param argc Standard main() style argc
1078 * @param argv Standard main() style argv. Initial components are already
1079 * stripped
1081 * @return Normal NTSTATUS return.
1083 static NTSTATUS
1084 rpc_share_del_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1085 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1087 WERROR result;
1089 result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
1090 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1093 /**
1094 * Delete a share on a remote RPC server
1096 * @param domain_sid The domain sid acquired from the remote server
1097 * @param argc Standard main() style argc
1098 * @param argv Standard main() style argv. Initial components are already
1099 * stripped
1101 * @return A shell status integer (0 for success)
1103 static int rpc_share_delete(int argc, const char **argv)
1105 if (argc < 1) {
1106 DEBUG(1,("Sharename not specified on delete\n"));
1107 return rpc_share_usage(argc, argv);
1109 return run_rpc_command(NULL, PI_SRVSVC, 0,
1110 rpc_share_del_internals,
1111 argc, argv);
1115 * Formatted print of share info
1117 * @param info1 pointer to SRV_SHARE_INFO_1 to format
1120 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
1122 fstring netname = "", remark = "";
1124 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
1125 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
1127 if (opt_long_list_entries) {
1128 d_printf("%-12.12s %-8.8s %-50.50s\n",
1129 netname, share_type[info1->info_1.type], remark);
1130 } else {
1131 d_printf("%-12.12s\n", netname);
1136 /**
1137 * List shares on a remote RPC server
1139 * All parameters are provided by the run_rpc_command function, except for
1140 * argc, argv which are passes through.
1142 * @param domain_sid The domain sid acquired from the remote server
1143 * @param cli A cli_state connected to the server.
1144 * @param mem_ctx Talloc context, destoyed on completion of the function.
1145 * @param argc Standard main() style argc
1146 * @param argv Standard main() style argv. Initial components are already
1147 * stripped
1149 * @return Normal NTSTATUS return.
1152 static NTSTATUS
1153 rpc_share_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1154 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1156 SRV_SHARE_INFO_CTR ctr;
1157 WERROR result;
1158 ENUM_HND hnd;
1159 uint32 preferred_len = 0xffffffff, i;
1161 init_enum_hnd(&hnd, 0);
1163 result = cli_srvsvc_net_share_enum(
1164 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
1166 if (!W_ERROR_IS_OK(result))
1167 goto done;
1169 /* Display results */
1171 if (opt_long_list_entries) {
1172 d_printf(
1173 "\nEnumerating shared resources (exports) on remote server:\n\n"\
1174 "\nShare name Type Description\n"\
1175 "---------- ---- -----------\n");
1177 for (i = 0; i < ctr.num_entries; i++)
1178 display_share_info_1(&ctr.share.info1[i]);
1179 done:
1180 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1183 /**
1184 * 'net rpc share' entrypoint.
1185 * @param argc Standard main() style argc
1186 * @param argv Standard main() style argv. Initial components are already
1187 * stripped
1190 int net_rpc_share(int argc, const char **argv)
1192 struct functable func[] = {
1193 {"add", rpc_share_add},
1194 {"delete", rpc_share_delete},
1195 {NULL, NULL}
1198 if (argc == 0)
1199 return run_rpc_command(NULL, PI_SRVSVC, 0,
1200 rpc_share_list_internals,
1201 argc, argv);
1203 return net_run_function(argc, argv, func, rpc_share_usage);
1206 /****************************************************************************/
1208 static int rpc_file_usage(int argc, const char **argv)
1210 return net_help_file(argc, argv);
1213 /**
1214 * Close a file on a remote RPC server
1216 * All parameters are provided by the run_rpc_command function, except for
1217 * argc, argv which are passes through.
1219 * @param domain_sid The domain sid acquired from the remote server
1220 * @param cli A cli_state connected to the server.
1221 * @param mem_ctx Talloc context, destoyed on completion of the function.
1222 * @param argc Standard main() style argc
1223 * @param argv Standard main() style argv. Initial components are already
1224 * stripped
1226 * @return Normal NTSTATUS return.
1228 static NTSTATUS
1229 rpc_file_close_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1230 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1232 WERROR result;
1233 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
1234 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1237 /**
1238 * Close a file on a remote RPC server
1240 * @param argc Standard main() style argc
1241 * @param argv Standard main() style argv. Initial components are already
1242 * stripped
1244 * @return A shell status integer (0 for success)
1246 static int rpc_file_close(int argc, const char **argv)
1248 if (argc < 1) {
1249 DEBUG(1, ("No fileid given on close\n"));
1250 return(rpc_file_usage(argc, argv));
1253 return run_rpc_command(NULL, PI_SRVSVC, 0,
1254 rpc_file_close_internals,
1255 argc, argv);
1258 /**
1259 * Formatted print of open file info
1261 * @param info3 FILE_INFO_3 contents
1262 * @param str3 strings for FILE_INFO_3
1265 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
1267 fstring user = "", path = "";
1269 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
1270 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
1272 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
1273 info3->id, user, info3->perms, info3->num_locks, path);
1276 /**
1277 * List open files on a remote RPC server
1279 * All parameters are provided by the run_rpc_command function, except for
1280 * argc, argv which are passes through.
1282 * @param domain_sid The domain sid acquired from the remote server
1283 * @param cli A cli_state connected to the server.
1284 * @param mem_ctx Talloc context, destoyed on completion of the function.
1285 * @param argc Standard main() style argc
1286 * @param argv Standard main() style argv. Initial components are already
1287 * stripped
1289 * @return Normal NTSTATUS return.
1292 static NTSTATUS
1293 rpc_file_list_internals(const DOM_SID *domain_sid, struct cli_state *cli,
1294 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1296 SRV_FILE_INFO_CTR ctr;
1297 WERROR result;
1298 ENUM_HND hnd;
1299 uint32 preferred_len = 0xffffffff, i;
1300 const char *username=NULL;
1302 init_enum_hnd(&hnd, 0);
1304 /* if argc > 0, must be user command */
1305 if (argc > 0)
1306 username = smb_xstrdup(argv[0]);
1308 result = cli_srvsvc_net_file_enum(
1309 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
1311 if (!W_ERROR_IS_OK(result))
1312 goto done;
1314 /* Display results */
1316 d_printf(
1317 "\nEnumerating open files on remote server:\n\n"\
1318 "\nFileId Opened by Perms Locks Path"\
1319 "\n------ --------- ----- ----- ---- \n");
1320 for (i = 0; i < ctr.num_entries; i++)
1321 display_file_info_3(&ctr.file.info3[i].info_3,
1322 &ctr.file.info3[i].info_3_str);
1323 done:
1324 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1328 /**
1329 * List files for a user on a remote RPC server
1331 * @param argc Standard main() style argc
1332 * @param argv Standard main() style argv. Initial components are already
1333 * stripped
1335 * @return A shell status integer (0 for success)
1337 static int rpc_file_user(int argc, const char **argv)
1339 if (argc < 1) {
1340 DEBUG(1, ("No username given\n"));
1341 return(rpc_file_usage(argc, argv));
1344 return run_rpc_command(NULL, PI_SRVSVC, 0,
1345 rpc_file_list_internals,
1346 argc, argv);
1350 /**
1351 * 'net rpc file' entrypoint.
1352 * @param argc Standard main() style argc
1353 * @param argv Standard main() style argv. Initial components are already
1354 * stripped
1357 int net_rpc_file(int argc, const char **argv)
1359 struct functable func[] = {
1360 {"close", rpc_file_close},
1361 {"user", rpc_file_user},
1362 #if 0
1363 {"info", rpc_file_info},
1364 #endif
1365 {NULL, NULL}
1368 if (argc == 0)
1369 return run_rpc_command(NULL, PI_SRVSVC, 0,
1370 rpc_file_list_internals,
1371 argc, argv);
1373 return net_run_function(argc, argv, func, rpc_file_usage);
1376 /****************************************************************************/
1380 /**
1381 * ABORT the shutdown of a remote RPC Server
1383 * All parameters are provided by the run_rpc_command function, except for
1384 * argc, argv which are passed through.
1386 * @param domain_sid The domain sid aquired from the remote server
1387 * @param cli A cli_state connected to the server.
1388 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1389 * @param argc Standard main() style argc
1390 * @param argv Standard main() style argv. Initial components are already
1391 * stripped
1393 * @return Normal NTSTATUS return.
1396 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1397 int argc, const char **argv)
1399 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1401 result = cli_reg_abort_shutdown(cli, mem_ctx);
1403 if (NT_STATUS_IS_OK(result))
1404 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
1405 else
1406 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
1408 return result;
1412 /**
1413 * ABORT the Shut down of a remote RPC server
1415 * @param argc Standard main() style argc
1416 * @param argv Standard main() style argv. Initial components are already
1417 * stripped
1419 * @return A shell status integer (0 for success)
1422 static int rpc_shutdown_abort(int argc, const char **argv)
1424 return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_abort_internals,
1425 argc, argv);
1428 /**
1429 * Shut down a remote RPC Server
1431 * All parameters are provided by the run_rpc_command function, except for
1432 * argc, argv which are passes through.
1434 * @param domain_sid The domain sid aquired from the remote server
1435 * @param cli A cli_state connected to the server.
1436 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1437 * @param argc Standard main() style argc
1438 * @param argc Standard main() style argv. Initial components are already
1439 * stripped
1441 * @return Normal NTSTATUS return.
1444 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1445 int argc, const char **argv)
1447 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1448 char *msg = "This machine will be shutdown shortly";
1449 uint32 timeout = 20;
1450 uint16 flgs = 0;
1451 BOOL reboot = opt_reboot;
1452 BOOL force = opt_force;
1453 #if 0
1454 poptContext pc;
1455 int rc;
1457 struct poptOption long_options[] = {
1458 {"message", 'm', POPT_ARG_STRING, &msg},
1459 {"timeout", 't', POPT_ARG_INT, &timeout},
1460 {"reboot", 'r', POPT_ARG_NONE, &reboot},
1461 {"force", 'f', POPT_ARG_NONE, &force},
1462 { 0, 0, 0, 0}
1465 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
1466 POPT_CONTEXT_KEEP_FIRST);
1468 rc = poptGetNextOpt(pc);
1470 if (rc < -1) {
1471 /* an error occurred during option processing */
1472 DEBUG(0, ("%s: %s\n",
1473 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1474 poptStrerror(rc)));
1475 return NT_STATUS_INVALID_PARAMETER;
1477 #endif
1478 if (reboot) {
1479 flgs |= REG_REBOOT_ON_SHUTDOWN;
1481 if (force) {
1482 flgs |= REG_FORCE_SHUTDOWN;
1484 if (opt_comment) {
1485 msg = opt_comment;
1487 if (opt_timeout) {
1488 timeout = opt_timeout;
1491 /* create an entry */
1492 result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, flgs);
1494 if (NT_STATUS_IS_OK(result))
1495 DEBUG(5,("Shutdown of remote machine succeeded\n"));
1496 else
1497 DEBUG(0,("Shutdown of remote machine failed!\n"));
1499 return result;
1502 /**
1503 * Shut down a remote RPC server
1505 * @param argc Standard main() style argc
1506 * @param argc Standard main() style argv. Initial components are already
1507 * stripped
1509 * @return A shell status integer (0 for success)
1512 static int rpc_shutdown(int argc, const char **argv)
1514 return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_internals,
1515 argc, argv);
1518 /***************************************************************************
1519 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
1521 ***************************************************************************/
1524 * Add interdomain trust account to the RPC server.
1525 * All parameters (except for argc and argv) are passed by run_rpc_command
1526 * function.
1528 * @param domain_sid The domain sid acquired from the server
1529 * @param cli A cli_state connected to the server.
1530 * @param mem_ctx Talloc context, destoyed on completion of the function.
1531 * @param argc Standard main() style argc
1532 * @param argc Standard main() style argv. Initial components are already
1533 * stripped
1535 * @return normal NTSTATUS return code
1538 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1539 int argc, const char **argv) {
1541 POLICY_HND connect_pol, domain_pol, user_pol;
1542 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1543 char *acct_name;
1544 uint16 acb_info;
1545 uint32 unknown, user_rid;
1547 if (argc != 1) {
1548 d_printf("Usage: net rpc trustdom add <domain_name>\n");
1549 return NT_STATUS_INVALID_PARAMETER;
1553 * Make valid trusting domain account (ie. uppercased and with '$' appended)
1556 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
1557 return NT_STATUS_NO_MEMORY;
1560 strupper(acct_name);
1562 /* Get samr policy handle */
1563 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1564 &connect_pol);
1565 if (!NT_STATUS_IS_OK(result)) {
1566 goto done;
1569 /* Get domain policy handle */
1570 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1571 MAXIMUM_ALLOWED_ACCESS,
1572 domain_sid, &domain_pol);
1573 if (!NT_STATUS_IS_OK(result)) {
1574 goto done;
1577 /* Create trusting domain's account */
1578 acb_info = ACB_DOMTRUST;
1579 unknown = 0xe005000b; /* No idea what this is - a permission mask?
1580 mimir: yes, most probably it is */
1582 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
1583 acct_name, acb_info, unknown,
1584 &user_pol, &user_rid);
1585 if (!NT_STATUS_IS_OK(result)) {
1586 goto done;
1589 done:
1590 SAFE_FREE(acct_name);
1591 return result;
1595 * Create interdomain trust account for a remote domain.
1597 * @param argc standard argc
1598 * @param argv standard argv without initial components
1600 * @return Integer status (0 means success)
1603 static int rpc_trustdom_add(int argc, const char **argv)
1605 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
1606 argc, argv);
1611 * Delete interdomain trust account for a remote domain.
1613 * @param argc standard argc
1614 * @param argv standard argv without initial components
1616 * @return Integer status (0 means success)
1619 static int rpc_trustdom_del(int argc, const char **argv)
1621 d_printf("Sorry, not yet implemented.\n");
1622 return -1;
1627 * Establish trust relationship to a trusting domain.
1628 * Interdomain account must already be created on remote PDC.
1630 * @param argc standard argc
1631 * @param argv standard argv without initial components
1633 * @return Integer status (0 means success)
1636 extern char *opt_user_name;
1637 extern char *opt_password;
1638 extern char *opt_workgroup;
1640 static int rpc_trustdom_establish(int argc, const char **argv)
1642 struct cli_state *cli;
1643 struct in_addr server_ip;
1644 POLICY_HND connect_hnd;
1645 TALLOC_CTX *mem_ctx;
1646 NTSTATUS nt_status;
1647 DOM_SID domain_sid;
1648 WKS_INFO_100 wks_info;
1650 char* domain_name;
1651 char* acct_name;
1652 fstring pdc_name;
1655 * Connect to \\server\ipc$ as 'our domain' account with password
1658 if (argc != 1) {
1659 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
1660 return -1;
1663 domain_name = smb_xstrdup(argv[0]);
1664 strupper(domain_name);
1667 * opt_workgroup will be used by connection functions further,
1668 * hence it should be set to remote domain name instead of ours
1670 if (opt_workgroup) {
1671 SAFE_FREE(opt_workgroup);
1672 opt_workgroup = smb_xstrdup(domain_name);
1675 asprintf(&acct_name, "%s$", lp_workgroup());
1676 strupper(acct_name);
1678 opt_user_name = acct_name;
1680 /* find the domain controller */
1681 if (!net_find_dc(&server_ip, pdc_name, domain_name)) {
1682 DEBUG(0, ("Coulnd find domain controller for domain %s\n", domain_name));
1683 return -1;
1686 /* connect to ipc$ as username/password */
1687 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
1688 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
1690 /* Is it trusting domain account for sure ? */
1691 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
1692 nt_errstr(nt_status)));
1693 return -1;
1697 * Connect to \\server\ipc$ again (this time anonymously)
1700 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
1702 if (NT_STATUS_IS_ERR(nt_status)) {
1703 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
1704 domain_name, nt_errstr(nt_status)));
1708 * Use NetServerEnum2 to make sure we're talking to a proper server
1711 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
1712 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
1713 for domain %s\n", domain_name));
1717 * Call WksQueryInfo to check remote server's capabilities
1718 * note: It is now used only to get unicode domain name
1721 if (!cli_nt_session_open(cli, PI_WKSSVC)) {
1722 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
1723 return -1;
1726 if (!(mem_ctx = talloc_init_named("establishing trust relationship to domain %s",
1727 domain_name))) {
1728 DEBUG(0, ("talloc_init() failed\n"));
1729 cli_shutdown(cli);
1730 return -1;
1733 nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
1735 if (NT_STATUS_IS_ERR(nt_status)) {
1736 DEBUG(0, ("WksQueryInfo call failed.\n"));
1737 return -1;
1740 if (cli->nt_pipe_fnum)
1741 cli_nt_session_close(cli);
1745 * Call LsaOpenPolicy and LsaQueryInfo
1748 if (!(mem_ctx = talloc_init())) {
1749 DEBUG(0, ("talloc_init() failed\n"));
1750 cli_shutdown(cli);
1751 return -1;
1754 if (!cli_nt_session_open(cli, PI_LSARPC)) {
1755 DEBUG(0, ("Could not initialise lsa pipe\n"));
1756 cli_shutdown(cli);
1757 return -1;
1760 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
1761 &connect_hnd);
1762 if (NT_STATUS_IS_ERR(nt_status)) {
1763 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
1764 nt_errstr(nt_status)));
1765 return -1;
1768 /* Querying info level 5 */
1770 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
1771 5 /* info level */, domain_name,
1772 &domain_sid);
1773 if (NT_STATUS_IS_ERR(nt_status)) {
1774 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
1775 nt_errstr(nt_status)));
1776 return -1;
1782 /* There should be actually query info level 3 (following nt serv behaviour),
1783 but I still don't know if it's _really_ necessary */
1786 * Store the password in secrets db
1789 if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
1790 wks_info.uni_lan_grp.uni_str_len, opt_password,
1791 domain_sid)) {
1792 DEBUG(0, ("Storing password for trusted domain failed.\n"));
1793 return -1;
1797 * Close the pipes and clean up
1800 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
1801 if (NT_STATUS_IS_ERR(nt_status)) {
1802 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
1803 nt_errstr(nt_status)));
1804 return -1;
1807 if (cli->nt_pipe_fnum)
1808 cli_nt_session_close(cli);
1810 talloc_destroy(mem_ctx);
1812 DEBUG(0, ("Success!\n"));
1813 return 0;
1817 * Revoke trust relationship to the remote domain
1819 * @param argc standard argc
1820 * @param argv standard argv without initial components
1822 * @return Integer status (0 means success)
1825 static int rpc_trustdom_revoke(int argc, const char **argv)
1827 char* domain_name;
1829 if (argc < 1) return -1;
1831 /* generate upper cased domain name */
1832 domain_name = smb_xstrdup(argv[0]);
1833 strupper(domain_name);
1835 /* delete password of the trust */
1836 if (!trusted_domain_password_delete(domain_name)) {
1837 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
1838 domain_name));
1839 return -1;
1842 return 0;
1846 * Usage for 'net rpc trustdom' command
1848 * @param argc standard argc
1849 * @param argv standard argv without inital components
1851 * @return Integer status returned to shell
1854 static int rpc_trustdom_usage(int argc, const char **argv)
1856 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
1857 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
1858 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
1859 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
1860 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
1861 return -1;
1865 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid, struct cli_state *cli, TALLOC_CTX *mem_ctx,
1866 int argc, const char **argv)
1868 fstring str_sid;
1869 sid_to_string(str_sid, domain_sid);
1870 d_printf("%s\n", str_sid);
1871 return NT_STATUS_OK;
1875 extern char* opt_workgroup;
1876 extern char* opt_target_worgroup;
1877 extern char* opt_host;
1878 extern char* opt_password;
1880 static int rpc_trustdom_list(int argc, const char **argv)
1882 /* common variables */
1883 TALLOC_CTX* mem_ctx;
1884 struct cli_state *cli, *remote_cli;
1885 NTSTATUS nt_status;
1886 char *domain_name = NULL;
1887 DOM_SID queried_dom_sid;
1888 fstring ascii_sid, padding;
1889 int ascii_dom_name_len;
1890 POLICY_HND connect_hnd;
1892 /* trusted domains listing variables */
1893 int enum_ctx = 0;
1894 int num_domains, i, pad_len, col_len = 20;
1895 DOM_SID *domain_sids;
1896 char **trusted_dom_names;
1897 fstring pdc_name;
1899 /* trusting domains listing variables */
1900 POLICY_HND domain_hnd;
1901 char **trusting_dom_names;
1902 uint32 *trusting_dom_rids;
1905 * Listing trusted domains (stored in secrets.tdb, if local)
1908 mem_ctx = talloc_init_named("trust relationships listing");
1911 * set domain and pdc name to local samba server (default)
1912 * or to remote one given in command line
1914 strupper(opt_workgroup);
1915 if (strcmp(opt_workgroup, lp_workgroup())) {
1916 domain_name = opt_workgroup;
1917 if (opt_target_workgroup) SAFE_FREE(opt_target_workgroup);
1918 opt_target_workgroup = opt_workgroup;
1919 } else {
1920 safe_strcpy(pdc_name, global_myname, FSTRING_LEN);
1921 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
1922 if (opt_target_workgroup) SAFE_FREE(opt_target_workgroup);
1923 opt_target_workgroup = domain_name;
1926 /* open \PIPE\lsarpc and open policy handle */
1927 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
1928 DEBUG(0, ("Couldn't connect to domain controller\n"));
1929 return -1;
1932 if (!cli_nt_session_open(cli, PI_LSARPC)) {
1933 DEBUG(0, ("Could not initialise lsa pipe\n"));
1934 return -1;
1937 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
1938 &connect_hnd);
1939 if (NT_STATUS_IS_ERR(nt_status)) {
1940 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
1941 nt_errstr(nt_status)));
1942 return -1;
1945 /* query info level 5 to obtain sid of a domain being queried */
1946 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
1947 5 /* info level */, domain_name, &queried_dom_sid);
1948 if (NT_STATUS_IS_ERR(nt_status)) {
1949 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
1950 nt_errstr(nt_status)));
1951 return -1;
1955 * Keep calling LsaEnumTrustdom over opened pipe until
1956 * the end of enumeration is reached
1959 d_printf("Trusted domains list:\n\n");
1961 do {
1962 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
1963 &num_domains,
1964 &trusted_dom_names, &domain_sids);
1966 if (NT_STATUS_IS_ERR(nt_status)) {
1967 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
1968 nt_errstr(nt_status)));
1969 return -1;
1972 for (i = 0; i < num_domains; i++) {
1973 /* convert sid into ascii string */
1974 sid_to_string(ascii_sid, &(domain_sids[i]));
1976 /* calculate padding space for d_printf to look nicer */
1977 pad_len = col_len - strlen(trusted_dom_names[i]);
1978 padding[pad_len] = 0;
1979 do padding[--pad_len] = ' '; while (pad_len);
1981 d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
1985 * in case of no trusted domains say something rather
1986 * than just display blank line
1988 if (!num_domains) d_printf("none\n");
1990 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
1992 /* close this connection before doing next one */
1993 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
1994 if (NT_STATUS_IS_ERR(nt_status)) {
1995 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
1996 nt_errstr(nt_status)));
1997 return -1;
2000 cli_nt_session_close(cli);
2003 * Listing trusting domains (stored in passdb backend, if local)
2006 d_printf("\nTrusting domains list:\n\n");
2009 * Open \PIPE\samr and get needed policy handles
2011 if (!cli_nt_session_open(cli, PI_SAMR)) {
2012 DEBUG(0, ("Could not initialise samr pipe\n"));
2013 return -1;
2016 /* SamrConnect */
2017 nt_status = cli_samr_connect(cli, mem_ctx, SAMR_ACCESS_OPEN_DOMAIN,
2018 &connect_hnd);
2019 if (!NT_STATUS_IS_OK(nt_status)) {
2020 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
2021 nt_errstr(nt_status)));
2022 return -1;
2025 /* SamrOpenDomain - we have to open domain policy handle in order to be
2026 able to enumerate accounts*/
2027 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
2028 DOMAIN_ACCESS_ENUM_ACCOUNTS,
2029 &queried_dom_sid, &domain_hnd);
2030 if (!NT_STATUS_IS_OK(nt_status)) {
2031 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
2032 nt_errstr(nt_status)));
2033 return -1;
2037 * perform actual enumeration
2040 enum_ctx = 0; /* reset enumeration context from last enumeration */
2041 do {
2043 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
2044 &enum_ctx, ACB_DOMTRUST, 0xffff,
2045 &trusting_dom_names, &trusting_dom_rids,
2046 &num_domains);
2047 if (NT_STATUS_IS_ERR(nt_status)) {
2048 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
2049 nt_errstr(nt_status)));
2050 return -1;
2053 for (i = 0; i < num_domains; i++) {
2056 * get each single domain's sid (do we _really_ need this ?):
2057 * 1) connect to domain's pdc
2058 * 2) query the pdc for domain's sid
2061 /* get rid of '$' tail */
2062 ascii_dom_name_len = strlen(trusting_dom_names[i]);
2063 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
2064 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
2066 /* calculate padding space for d_printf to look nicer */
2067 pad_len = col_len - strlen(trusting_dom_names[i]);
2068 padding[pad_len] = 0;
2069 do padding[--pad_len] = ' '; while (pad_len);
2071 /* set opt_* variables to remote domain */
2072 strupper(trusting_dom_names[i]);
2073 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
2074 if (opt_target_workgroup) SAFE_FREE(opt_target_workgroup);
2075 opt_target_workgroup = opt_workgroup;
2077 d_printf("%s%s", trusting_dom_names[i], padding);
2079 /* connect to remote domain controller */
2080 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
2081 if (remote_cli) {
2082 /* query for domain's sid */
2083 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
2084 d_printf("couldn't get domain's sid\n");
2086 cli_shutdown(remote_cli);
2088 } else {
2089 d_printf("domain controller is not responding\n");
2093 if (!num_domains) d_printf("none\n");
2095 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2097 /* close opened samr and domain policy handles */
2098 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
2099 if (!NT_STATUS_IS_OK(nt_status)) {
2100 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
2103 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
2104 if (!NT_STATUS_IS_OK(nt_status)) {
2105 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
2108 /* close samr pipe and connection to IPC$ */
2109 cli_nt_session_close(cli);
2110 cli_shutdown(cli);
2112 talloc_destroy(mem_ctx);
2113 return 0;
2117 * Entrypoint for 'net rpc trustdom' code
2119 * @param argc standard argc
2120 * @param argv standard argv without initial components
2122 * @return Integer status (0 means success)
2125 static int rpc_trustdom(int argc, const char **argv)
2127 struct functable func[] = {
2128 {"add", rpc_trustdom_add},
2129 {"del", rpc_trustdom_del},
2130 {"establish", rpc_trustdom_establish},
2131 {"revoke", rpc_trustdom_revoke},
2132 {"help", rpc_trustdom_usage},
2133 {"list", rpc_trustdom_list},
2134 {NULL, NULL}
2137 if (argc == 0) {
2138 rpc_trustdom_usage(argc, argv);
2139 return -1;
2142 return (net_run_function(argc, argv, func, rpc_user_usage));
2146 * Check if a server will take rpc commands
2147 * @param flags Type of server to connect to (PDC, DMB, localhost)
2148 * if the host is not explicitly specified
2149 * @return BOOL (true means rpc supported)
2151 BOOL net_rpc_check(unsigned flags)
2153 struct cli_state cli;
2154 BOOL ret = False;
2155 struct in_addr server_ip;
2156 char *server_name = NULL;
2158 /* flags (i.e. server type) may depend on command */
2159 if (!net_find_server(flags, &server_ip, &server_name))
2160 return False;
2162 ZERO_STRUCT(cli);
2163 if (cli_initialise(&cli) == False)
2164 return False;
2166 if (!cli_connect(&cli, server_name, &server_ip))
2167 goto done;
2168 if (!attempt_netbios_session_request(&cli, global_myname,
2169 server_name, &server_ip))
2170 goto done;
2171 if (!cli_negprot(&cli))
2172 goto done;
2173 if (cli.protocol < PROTOCOL_NT1)
2174 goto done;
2176 ret = True;
2177 done:
2178 cli_shutdown(&cli);
2179 return ret;
2183 /****************************************************************************/
2186 /**
2187 * Basic usage function for 'net rpc'
2188 * @param argc Standard main() style argc
2189 * @param argv Standard main() style argv. Initial components are already
2190 * stripped
2193 int net_rpc_usage(int argc, const char **argv)
2195 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
2196 d_printf(" net rpc join \t\t\tto join a domain \n");
2197 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
2198 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
2199 d_printf(" net rpc group \t\tto list groups\n");
2200 d_printf(" net rpc share \t\tto add, delete, and list shares\n");
2201 d_printf(" net rpc file \t\t\tto list open files\n");
2202 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
2203 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
2204 d_printf(" net rpc trustdom \t\tto create trusting domain's account\n"
2205 "\t\t\t\t\tor establish trust\n");
2206 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
2207 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
2208 d_printf("\n");
2209 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
2210 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
2211 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
2212 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
2213 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
2214 return -1;
2219 * Help function for 'net rpc'. Calls command specific help if requested
2220 * or displays usage of net rpc
2221 * @param argc Standard main() style argc
2222 * @param argv Standard main() style argv. Initial components are already
2223 * stripped
2226 int net_rpc_help(int argc, const char **argv)
2228 struct functable func[] = {
2229 {"join", rpc_join_usage},
2230 {"user", rpc_user_usage},
2231 {"group", rpc_group_usage},
2232 {"share", rpc_share_usage},
2233 /*{"changetrustpw", rpc_changetrustpw_usage}, */
2234 {"trustdom", rpc_trustdom_usage},
2235 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
2236 /*{"shutdown", rpc_shutdown_usage}, */
2237 {NULL, NULL}
2240 if (argc == 0) {
2241 net_rpc_usage(argc, argv);
2242 return -1;
2245 return (net_run_function(argc, argv, func, rpc_user_usage));
2249 /**
2250 * 'net rpc' entrypoint.
2251 * @param argc Standard main() style argc
2252 * @param argv Standard main() style argv. Initial components are already
2253 * stripped
2256 int net_rpc(int argc, const char **argv)
2258 struct functable func[] = {
2259 {"info", net_rpc_info},
2260 {"join", net_rpc_join},
2261 {"testjoin", net_rpc_testjoin},
2262 {"user", net_rpc_user},
2263 {"group", net_rpc_group},
2264 {"share", net_rpc_share},
2265 {"file", net_rpc_file},
2266 {"changetrustpw", rpc_changetrustpw},
2267 {"trustdom", rpc_trustdom},
2268 {"abortshutdown", rpc_shutdown_abort},
2269 {"shutdown", rpc_shutdown},
2270 {"samdump", rpc_samdump},
2271 {"vampire", rpc_vampire},
2272 {"getsid", net_rpc_getsid},
2273 {"help", net_rpc_help},
2274 {NULL, NULL}
2276 return net_run_function(argc, argv, func, net_rpc_usage);