If there are no alias members, don't ask for their sids.
[Samba/gebeck_regimport.git] / source / utils / net_rpc.c
blob80f02f2ae9363393d31aa85d29a27f6aaba49846
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 *, const char *,
41 struct cli_state *, TALLOC_CTX *, int, const char **);
43 /**
44 * Many of the RPC functions need the domain sid. This function gets
45 * it at the start of every run
47 * @param cli A cli_state already connected to the remote machine
49 * @return The Domain SID of the remote machine.
50 **/
52 static DOM_SID *net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx, char **domain_name)
54 DOM_SID *domain_sid;
55 POLICY_HND pol;
56 NTSTATUS result = NT_STATUS_OK;
57 uint32 info_class = 5;
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;
110 char *domain_name;
112 /* make use of cli_state handed over as an argument, if possible */
113 if (!cli_arg)
114 cli = net_make_ipc_connection(conn_flags);
115 else
116 cli = cli_arg;
118 if (!cli) {
119 return -1;
122 /* Create mem_ctx */
124 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
125 DEBUG(0, ("talloc_init() failed\n"));
126 cli_shutdown(cli);
127 return -1;
130 domain_sid = net_get_remote_domain_sid(cli, mem_ctx, &domain_name);
132 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
133 if (!cli_nt_session_open(cli, pipe_idx)) {
134 DEBUG(0, ("Could not initialise pipe\n"));
138 nt_status = fn(domain_sid, domain_name, cli, mem_ctx, argc, argv);
140 if (!NT_STATUS_IS_OK(nt_status)) {
141 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
142 } else {
143 DEBUG(5, ("rpc command function succedded\n"));
146 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
147 if (cli->nt_pipe_fnum)
148 cli_nt_session_close(cli);
151 /* close the connection only if it was opened here */
152 if (!cli_arg)
153 cli_shutdown(cli);
155 talloc_destroy(mem_ctx);
157 return (!NT_STATUS_IS_OK(nt_status));
161 /****************************************************************************/
164 /**
165 * Force a change of the trust acccount password.
167 * All parameters are provided by the run_rpc_command function, except for
168 * argc, argv which are passes through.
170 * @param domain_sid The domain sid aquired from the remote server
171 * @param cli A cli_state connected to the server.
172 * @param mem_ctx Talloc context, destoyed on compleation of the function.
173 * @param argc Standard main() style argc
174 * @param argc Standard main() style argv. Initial components are already
175 * stripped
177 * @return Normal NTSTATUS return.
180 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid, const char *domain_name,
181 struct cli_state *cli, TALLOC_CTX *mem_ctx,
182 int argc, const char **argv) {
184 return trust_pw_find_change_and_store_it(cli, mem_ctx, opt_target_workgroup);
187 /**
188 * Force a change of the trust acccount password.
190 * @param argc Standard main() style argc
191 * @param argc Standard main() style argv. Initial components are already
192 * stripped
194 * @return A shell status integer (0 for success)
197 int net_rpc_changetrustpw(int argc, const char **argv)
199 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
200 rpc_changetrustpw_internals,
201 argc, argv);
205 /****************************************************************************/
208 /**
209 * Join a domain, the old way.
211 * This uses 'machinename' as the inital password, and changes it.
213 * The password should be created with 'server manager' or equiv first.
215 * All parameters are provided by the run_rpc_command function, except for
216 * argc, argv which are passes through.
218 * @param domain_sid The domain sid aquired from the remote server
219 * @param cli A cli_state connected to the server.
220 * @param mem_ctx Talloc context, destoyed on compleation of the function.
221 * @param argc Standard main() style argc
222 * @param argc Standard main() style argv. Initial components are already
223 * stripped
225 * @return Normal NTSTATUS return.
228 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid, const char *domain_name,
229 struct cli_state *cli,
230 TALLOC_CTX *mem_ctx,
231 int argc, const char **argv) {
233 fstring trust_passwd;
234 unsigned char orig_trust_passwd_hash[16];
235 NTSTATUS result;
236 uint32 sec_channel_type;
239 check what type of join - if the user want's to join as
240 a BDC, the server must agree that we are a BDC.
242 if (argc >= 0) {
243 sec_channel_type = get_sec_channel_type(argv[0]);
244 } else {
245 sec_channel_type = get_sec_channel_type(NULL);
248 fstrcpy(trust_passwd, global_myname());
249 strlower_m(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, opt_target_workgroup,
261 orig_trust_passwd_hash,
262 sec_channel_type);
264 if (NT_STATUS_IS_OK(result))
265 printf("Joined domain %s.\n",opt_target_workgroup);
268 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
269 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
270 result = NT_STATUS_UNSUCCESSFUL;
273 return result;
276 /**
277 * Join a domain, the old way.
279 * @param argc Standard main() style argc
280 * @param argc Standard main() style argv. Initial components are already
281 * stripped
283 * @return A shell status integer (0 for success)
286 static int net_rpc_perform_oldjoin(int argc, const char **argv)
288 return run_rpc_command(NULL, PI_NETLOGON,
289 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
290 rpc_oldjoin_internals,
291 argc, argv);
294 /**
295 * Join a domain, the old way. This function exists to allow
296 * the message to be displayed when oldjoin was explicitly
297 * requested, but not when it was implied by "net rpc join"
299 * @param argc Standard main() style argc
300 * @param argc Standard main() style argv. Initial components are already
301 * stripped
303 * @return A shell status integer (0 for success)
306 static int net_rpc_oldjoin(int argc, const char **argv)
308 int rc = net_rpc_perform_oldjoin(argc, argv);
310 if (rc) {
311 d_printf("Failed to join domain\n");
314 return rc;
317 /**
318 * Basic usage function for 'net rpc join'
319 * @param argc Standard main() style argc
320 * @param argc Standard main() style argv. Initial components are already
321 * stripped
324 static int rpc_join_usage(int argc, const char **argv)
326 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
327 "\t to join a domain with admin username & password\n"\
328 "\t\t password will be prompted if needed and none is specified\n"\
329 "\t <type> can be (default MEMBER)\n"\
330 "\t\t BDC - Join as a BDC\n"\
331 "\t\t PDC - Join as a PDC\n"\
332 "\t\t MEMBER - Join as a MEMBER server\n");
334 net_common_flags_usage(argc, argv);
335 return -1;
338 /**
339 * 'net rpc join' entrypoint.
340 * @param argc Standard main() style argc
341 * @param argc Standard main() style argv. Initial components are already
342 * stripped
344 * Main 'net_rpc_join()' (where the admain username/password is used) is
345 * in net_rpc_join.c
346 * Try to just change the password, but if that doesn't work, use/prompt
347 * for a username/password.
350 int net_rpc_join(int argc, const char **argv)
352 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
353 return 0;
355 return net_rpc_join_newstyle(argc, argv);
360 /**
361 * display info about a rpc domain
363 * All parameters are provided by the run_rpc_command function, except for
364 * argc, argv which are passed through.
366 * @param domain_sid The domain sid acquired from the remote server
367 * @param cli A cli_state connected to the server.
368 * @param mem_ctx Talloc context, destoyed on completion of the function.
369 * @param argc Standard main() style argc
370 * @param argv Standard main() style argv. Initial components are already
371 * stripped
373 * @return Normal NTSTATUS return.
376 static NTSTATUS
377 rpc_info_internals(const DOM_SID *domain_sid, const char *domain_name,
378 struct cli_state *cli,
379 TALLOC_CTX *mem_ctx, int argc, const char **argv)
381 POLICY_HND connect_pol, domain_pol;
382 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
383 SAM_UNK_CTR ctr;
384 fstring sid_str;
386 sid_to_string(sid_str, domain_sid);
388 /* Get sam policy handle */
389 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
390 &connect_pol);
391 if (!NT_STATUS_IS_OK(result)) {
392 goto done;
395 /* Get domain policy handle */
396 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
397 MAXIMUM_ALLOWED_ACCESS,
398 domain_sid, &domain_pol);
399 if (!NT_STATUS_IS_OK(result)) {
400 goto done;
403 ZERO_STRUCT(ctr);
404 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
405 2, &ctr);
406 if (NT_STATUS_IS_OK(result)) {
407 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
408 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
409 d_printf("Domain SID: %s\n", sid_str);
410 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num);
411 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
412 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
413 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
414 talloc_destroy(ctx);
417 done:
418 return result;
422 /**
423 * 'net rpc info' entrypoint.
424 * @param argc Standard main() style argc
425 * @param argc Standard main() style argv. Initial components are already
426 * stripped
428 int net_rpc_info(int argc, const char **argv)
430 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
431 rpc_info_internals,
432 argc, argv);
436 /**
437 * Fetch domain SID into the local secrets.tdb
439 * All parameters are provided by the run_rpc_command function, except for
440 * argc, argv which are passes through.
442 * @param domain_sid The domain sid acquired from the remote server
443 * @param cli A cli_state connected to the server.
444 * @param mem_ctx Talloc context, destoyed on completion of the function.
445 * @param argc Standard main() style argc
446 * @param argv Standard main() style argv. Initial components are already
447 * stripped
449 * @return Normal NTSTATUS return.
452 static NTSTATUS
453 rpc_getsid_internals(const DOM_SID *domain_sid, const char *domain_name,
454 struct cli_state *cli,
455 TALLOC_CTX *mem_ctx, int argc, const char **argv)
457 fstring sid_str;
459 sid_to_string(sid_str, domain_sid);
460 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
461 sid_str, domain_name);
463 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
464 DEBUG(0,("Can't store domain SID\n"));
465 return NT_STATUS_UNSUCCESSFUL;
468 return NT_STATUS_OK;
472 /**
473 * 'net rpc getsid' entrypoint.
474 * @param argc Standard main() style argc
475 * @param argc Standard main() style argv. Initial components are already
476 * stripped
478 int net_rpc_getsid(int argc, const char **argv)
480 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
481 rpc_getsid_internals,
482 argc, argv);
486 /****************************************************************************/
489 * Basic usage function for 'net rpc user'
490 * @param argc Standard main() style argc.
491 * @param argv Standard main() style argv. Initial components are already
492 * stripped.
495 static int rpc_user_usage(int argc, const char **argv)
497 return net_help_user(argc, argv);
500 /**
501 * Add a new user to a remote RPC server
503 * All parameters are provided by the run_rpc_command function, except for
504 * argc, argv which are passes through.
506 * @param domain_sid The domain sid acquired from the remote server
507 * @param cli A cli_state connected to the server.
508 * @param mem_ctx Talloc context, destoyed on completion of the function.
509 * @param argc Standard main() style argc
510 * @param argv Standard main() style argv. Initial components are already
511 * stripped
513 * @return Normal NTSTATUS return.
516 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid, const char *domain_name,
517 struct cli_state *cli, TALLOC_CTX *mem_ctx,
518 int argc, const char **argv) {
520 POLICY_HND connect_pol, domain_pol, user_pol;
521 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
522 const char *acct_name;
523 uint16 acb_info;
524 uint32 unknown, user_rid;
526 if (argc != 1) {
527 d_printf("User must be specified\n");
528 rpc_user_usage(argc, argv);
529 return NT_STATUS_OK;
532 acct_name = argv[0];
534 /* Get sam policy handle */
536 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
537 &connect_pol);
538 if (!NT_STATUS_IS_OK(result)) {
539 goto done;
542 /* Get domain policy handle */
544 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
545 MAXIMUM_ALLOWED_ACCESS,
546 domain_sid, &domain_pol);
547 if (!NT_STATUS_IS_OK(result)) {
548 goto done;
551 /* Create domain user */
553 acb_info = ACB_NORMAL;
554 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
556 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
557 acct_name, acb_info, unknown,
558 &user_pol, &user_rid);
559 if (!NT_STATUS_IS_OK(result)) {
560 goto done;
563 done:
564 if (!NT_STATUS_IS_OK(result)) {
565 d_printf("Failed to add user %s - %s\n", acct_name,
566 nt_errstr(result));
567 } else {
568 d_printf("Added user %s\n", acct_name);
570 return result;
573 /**
574 * Add a new user to a remote RPC server
576 * @param argc Standard main() style argc
577 * @param argv Standard main() style argv. Initial components are already
578 * stripped
580 * @return A shell status integer (0 for success)
583 static int rpc_user_add(int argc, const char **argv)
585 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
586 argc, argv);
589 /**
590 * Delete a user from a remote RPC server
592 * All parameters are provided by the run_rpc_command function, except for
593 * argc, argv which are passes through.
595 * @param domain_sid The domain sid acquired from the remote server
596 * @param cli A cli_state connected to the server.
597 * @param mem_ctx Talloc context, destoyed on completion of the function.
598 * @param argc Standard main() style argc
599 * @param argv Standard main() style argv. Initial components are already
600 * stripped
602 * @return Normal NTSTATUS return.
605 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
606 const char *domain_name,
607 struct cli_state *cli,
608 TALLOC_CTX *mem_ctx,
609 int argc, const char **argv)
611 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
612 POLICY_HND connect_pol, domain_pol, user_pol;
614 if (argc < 1) {
615 d_printf("User must be specified\n");
616 rpc_user_usage(argc, argv);
617 return NT_STATUS_OK;
619 /* Get sam policy and domain handles */
621 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
622 &connect_pol);
624 if (!NT_STATUS_IS_OK(result)) {
625 goto done;
628 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
629 MAXIMUM_ALLOWED_ACCESS,
630 domain_sid, &domain_pol);
632 if (!NT_STATUS_IS_OK(result)) {
633 goto done;
636 /* Get handle on user */
639 uint32 *user_rids, num_rids, *name_types;
640 uint32 flags = 0x000003e8; /* Unknown */
642 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
643 flags, 1, &argv[0],
644 &num_rids, &user_rids,
645 &name_types);
647 if (!NT_STATUS_IS_OK(result)) {
648 goto done;
651 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
652 MAXIMUM_ALLOWED_ACCESS,
653 user_rids[0], &user_pol);
655 if (!NT_STATUS_IS_OK(result)) {
656 goto done;
660 /* Delete user */
662 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
664 if (!NT_STATUS_IS_OK(result)) {
665 goto done;
668 /* Display results */
670 done:
671 return result;
675 /**
676 * Delete a user from a remote RPC server
678 * @param argc Standard main() style argc
679 * @param argv Standard main() style argv. Initial components are already
680 * stripped
682 * @return A shell status integer (0 for success)
685 static int rpc_user_delete(int argc, const char **argv)
687 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
688 argc, argv);
691 /**
692 * Set a password for a user on a remote RPC server
694 * All parameters are provided by the run_rpc_command function, except for
695 * argc, argv which are passes through.
697 * @param domain_sid The domain sid acquired from the remote server
698 * @param cli A cli_state connected to the server.
699 * @param mem_ctx Talloc context, destoyed on completion of the function.
700 * @param argc Standard main() style argc
701 * @param argv Standard main() style argv. Initial components are already
702 * stripped
704 * @return Normal NTSTATUS return.
707 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
708 const char *domain_name,
709 struct cli_state *cli,
710 TALLOC_CTX *mem_ctx,
711 int argc, const char **argv)
713 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
714 POLICY_HND connect_pol, domain_pol, user_pol;
715 SAM_USERINFO_CTR ctr;
716 SAM_USER_INFO_24 p24;
717 uchar pwbuf[516];
718 const char *user;
719 const char *new_password;
720 char *prompt = NULL;
722 if (argc < 1) {
723 d_printf("User must be specified\n");
724 rpc_user_usage(argc, argv);
725 return NT_STATUS_OK;
728 user = argv[0];
730 if (argv[1]) {
731 new_password = argv[1];
732 } else {
733 asprintf(&prompt, "Enter new password for %s:", user);
734 new_password = getpass(prompt);
735 SAFE_FREE(prompt);
738 /* Get sam policy and domain handles */
740 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
741 &connect_pol);
743 if (!NT_STATUS_IS_OK(result)) {
744 goto done;
747 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
748 MAXIMUM_ALLOWED_ACCESS,
749 domain_sid, &domain_pol);
751 if (!NT_STATUS_IS_OK(result)) {
752 goto done;
755 /* Get handle on user */
758 uint32 *user_rids, num_rids, *name_types;
759 uint32 flags = 0x000003e8; /* Unknown */
761 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
762 flags, 1, &user,
763 &num_rids, &user_rids,
764 &name_types);
766 if (!NT_STATUS_IS_OK(result)) {
767 goto done;
770 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
771 MAXIMUM_ALLOWED_ACCESS,
772 user_rids[0], &user_pol);
774 if (!NT_STATUS_IS_OK(result)) {
775 goto done;
779 /* Set password on account */
781 ZERO_STRUCT(ctr);
782 ZERO_STRUCT(p24);
784 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
786 init_sam_user_info24(&p24, (char *)pwbuf,24);
788 ctr.switch_value = 24;
789 ctr.info.id24 = &p24;
791 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
792 &cli->user_session_key, &ctr);
794 if (!NT_STATUS_IS_OK(result)) {
795 goto done;
798 /* Display results */
800 done:
801 return result;
805 /**
806 * Set a user's password on a remote RPC server
808 * @param argc Standard main() style argc
809 * @param argv Standard main() style argv. Initial components are already
810 * stripped
812 * @return A shell status integer (0 for success)
815 static int rpc_user_password(int argc, const char **argv)
817 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
818 argc, argv);
821 /**
822 * List user's groups on a remote RPC server
824 * All parameters are provided by the run_rpc_command function, except for
825 * argc, argv which are passes through.
827 * @param domain_sid The domain sid acquired from the remote server
828 * @param cli A cli_state connected to the server.
829 * @param mem_ctx Talloc context, destoyed on completion of the function.
830 * @param argc Standard main() style argc
831 * @param argv Standard main() style argv. Initial components are already
832 * stripped
834 * @return Normal NTSTATUS return.
837 static NTSTATUS
838 rpc_user_info_internals(const DOM_SID *domain_sid, const char *domain_name,
839 struct cli_state *cli,
840 TALLOC_CTX *mem_ctx, int argc, const char **argv)
842 POLICY_HND connect_pol, domain_pol, user_pol;
843 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
844 uint32 *rids, num_rids, *name_types, num_names;
845 uint32 flags = 0x000003e8; /* Unknown */
846 int i;
847 char **names;
848 DOM_GID *user_gids;
850 if (argc < 1) {
851 d_printf("User must be specified\n");
852 rpc_user_usage(argc, argv);
853 return NT_STATUS_OK;
855 /* Get sam policy handle */
857 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
858 &connect_pol);
859 if (!NT_STATUS_IS_OK(result)) goto done;
861 /* Get domain policy handle */
863 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
864 MAXIMUM_ALLOWED_ACCESS,
865 domain_sid, &domain_pol);
866 if (!NT_STATUS_IS_OK(result)) goto done;
868 /* Get handle on user */
870 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
871 flags, 1, &argv[0],
872 &num_rids, &rids, &name_types);
874 if (!NT_STATUS_IS_OK(result)) goto done;
876 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
877 MAXIMUM_ALLOWED_ACCESS,
878 rids[0], &user_pol);
879 if (!NT_STATUS_IS_OK(result)) goto done;
881 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
882 &num_rids, &user_gids);
884 /* Look up rids */
886 rids = (uint32 *)talloc(mem_ctx, sizeof(uint32) * num_rids);
888 for (i = 0; i < num_rids; i++)
889 rids[i] = user_gids[i].g_rid;
891 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
892 flags, num_rids, rids,
893 &num_names, &names, &name_types);
895 if (!NT_STATUS_IS_OK(result)) {
896 goto done;
899 /* Display results */
901 for (i = 0; i < num_names; i++)
902 printf("%s\n", names[i]);
904 done:
905 return result;
908 /**
909 * List a user's groups from a remote RPC server
911 * @param argc Standard main() style argc
912 * @param argv Standard main() style argv. Initial components are already
913 * stripped
915 * @return A shell status integer (0 for success)
918 static int rpc_user_info(int argc, const char **argv)
920 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
921 argc, argv);
924 /**
925 * List users on a remote RPC server
927 * All parameters are provided by the run_rpc_command function, except for
928 * argc, argv which are passes through.
930 * @param domain_sid The domain sid acquired from the remote server
931 * @param cli A cli_state connected to the server.
932 * @param mem_ctx Talloc context, destoyed on completion of the function.
933 * @param argc Standard main() style argc
934 * @param argv Standard main() style argv. Initial components are already
935 * stripped
937 * @return Normal NTSTATUS return.
940 static NTSTATUS
941 rpc_user_list_internals(const DOM_SID *domain_sid, const char *domain_name,
942 struct cli_state *cli,
943 TALLOC_CTX *mem_ctx, int argc, const char **argv)
945 POLICY_HND connect_pol, domain_pol;
946 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
947 uint32 start_idx=0, num_entries, i, loop_count = 0;
948 SAM_DISPINFO_CTR ctr;
949 SAM_DISPINFO_1 info1;
951 /* Get sam policy handle */
953 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
954 &connect_pol);
955 if (!NT_STATUS_IS_OK(result)) {
956 goto done;
959 /* Get domain policy handle */
961 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
962 MAXIMUM_ALLOWED_ACCESS,
963 domain_sid, &domain_pol);
964 if (!NT_STATUS_IS_OK(result)) {
965 goto done;
968 /* Query domain users */
969 ZERO_STRUCT(ctr);
970 ZERO_STRUCT(info1);
971 ctr.sam.info1 = &info1;
972 if (opt_long_list_entries)
973 d_printf("\nUser name Comment"\
974 "\n-----------------------------\n");
975 do {
976 fstring user, desc;
977 uint32 max_entries, max_size;
979 get_query_dispinfo_params(
980 loop_count, &max_entries, &max_size);
982 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
983 &start_idx, 1, &num_entries,
984 max_entries, max_size, &ctr);
985 loop_count++;
987 for (i = 0; i < num_entries; i++) {
988 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
989 if (opt_long_list_entries)
990 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
992 if (opt_long_list_entries)
993 printf("%-21.21s %s\n", user, desc);
994 else
995 printf("%s\n", user);
997 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
999 done:
1000 return result;
1003 /**
1004 * 'net rpc user' entrypoint.
1005 * @param argc Standard main() style argc
1006 * @param argc Standard main() style argv. Initial components are already
1007 * stripped
1010 int net_rpc_user(int argc, const char **argv)
1012 struct functable func[] = {
1013 {"add", rpc_user_add},
1014 {"info", rpc_user_info},
1015 {"delete", rpc_user_delete},
1016 {"password", rpc_user_password},
1017 {NULL, NULL}
1020 if (argc == 0) {
1021 if (opt_long_list_entries) {
1022 } else {
1024 return run_rpc_command(NULL,PI_SAMR, 0,
1025 rpc_user_list_internals,
1026 argc, argv);
1029 return net_run_function(argc, argv, func, rpc_user_usage);
1033 /****************************************************************************/
1036 * Basic usage function for 'net rpc group'
1037 * @param argc Standard main() style argc.
1038 * @param argv Standard main() style argv. Initial components are already
1039 * stripped.
1042 static int rpc_group_usage(int argc, const char **argv)
1044 return net_help_group(argc, argv);
1047 /**
1048 * List groups on a remote RPC server
1050 * All parameters are provided by the run_rpc_command function, except for
1051 * argc, argv which are passes through.
1053 * @param domain_sid The domain sid acquired from the remote server
1054 * @param cli A cli_state connected to the server.
1055 * @param mem_ctx Talloc context, destoyed on completion of the function.
1056 * @param argc Standard main() style argc
1057 * @param argv Standard main() style argv. Initial components are already
1058 * stripped
1060 * @return Normal NTSTATUS return.
1063 static NTSTATUS
1064 rpc_group_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1065 struct cli_state *cli,
1066 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1068 POLICY_HND connect_pol, domain_pol;
1069 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1070 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
1071 struct acct_info *groups;
1072 DOM_SID global_sid_Builtin;
1073 BOOL global = False;
1074 BOOL local = False;
1075 BOOL builtin = False;
1077 if (argc == 0) {
1078 global = True;
1079 local = True;
1080 builtin = True;
1083 for (i=0; i<argc; i++) {
1084 if (strequal(argv[i], "global"))
1085 global = True;
1087 if (strequal(argv[i], "local"))
1088 local = True;
1090 if (strequal(argv[i], "builtin"))
1091 builtin = True;
1094 string_to_sid(&global_sid_Builtin, "S-1-5-32");
1096 /* Get sam policy handle */
1098 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1099 &connect_pol);
1100 if (!NT_STATUS_IS_OK(result)) {
1101 goto done;
1104 /* Get domain policy handle */
1106 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1107 MAXIMUM_ALLOWED_ACCESS,
1108 domain_sid, &domain_pol);
1109 if (!NT_STATUS_IS_OK(result)) {
1110 goto done;
1113 /* Query domain groups */
1114 if (opt_long_list_entries)
1115 d_printf("\nGroup name Comment"\
1116 "\n-----------------------------\n");
1117 do {
1118 SAM_DISPINFO_CTR ctr;
1119 SAM_DISPINFO_3 info3;
1120 uint32 max_size;
1122 ZERO_STRUCT(ctr);
1123 ZERO_STRUCT(info3);
1124 ctr.sam.info3 = &info3;
1126 if (!global) break;
1128 get_query_dispinfo_params(
1129 loop_count, &max_entries, &max_size);
1131 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1132 &start_idx, 3, &num_entries,
1133 max_entries, max_size, &ctr);
1135 for (i = 0; i < num_entries; i++) {
1137 fstring group, desc;
1139 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
1140 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
1142 if (opt_long_list_entries)
1143 printf("%-21.21s %-50.50s\n",
1144 group, desc);
1145 else
1146 printf("%s\n", group);
1148 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1149 /* query domain aliases */
1150 start_idx = 0;
1151 do {
1152 if (!local) break;
1154 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1155 &start_idx, max_entries,
1156 &groups, &num_entries);
1158 for (i = 0; i < num_entries; i++) {
1160 char *description = NULL;
1162 if (opt_long_list_entries) {
1164 POLICY_HND alias_pol;
1165 ALIAS_INFO_CTR ctr;
1167 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1168 &domain_pol,
1169 0x8,
1170 groups[i].rid,
1171 &alias_pol))) &&
1172 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1173 &alias_pol, 3,
1174 &ctr))) &&
1175 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1176 &alias_pol)))) {
1177 description = unistr2_tdup(mem_ctx,
1178 &ctr.alias.info3.uni_acct_desc);
1182 if (description != NULL) {
1183 printf("%-21.21s %-50.50s\n",
1184 groups[i].acct_name,
1185 description);
1186 } else {
1187 printf("%s\n", groups[i].acct_name);
1190 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1191 cli_samr_close(cli, mem_ctx, &domain_pol);
1192 /* Get builtin policy handle */
1194 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1195 MAXIMUM_ALLOWED_ACCESS,
1196 &global_sid_Builtin, &domain_pol);
1197 if (!NT_STATUS_IS_OK(result)) {
1198 goto done;
1200 /* query builtin aliases */
1201 start_idx = 0;
1202 do {
1203 if (!builtin) break;
1205 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1206 &start_idx, max_entries,
1207 &groups, &num_entries);
1209 for (i = 0; i < num_entries; i++) {
1211 char *description = NULL;
1213 if (opt_long_list_entries) {
1215 POLICY_HND alias_pol;
1216 ALIAS_INFO_CTR ctr;
1218 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1219 &domain_pol,
1220 0x8,
1221 groups[i].rid,
1222 &alias_pol))) &&
1223 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1224 &alias_pol, 3,
1225 &ctr))) &&
1226 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1227 &alias_pol)))) {
1228 description = unistr2_tdup(mem_ctx,
1229 &ctr.alias.info3.uni_acct_desc);
1233 if (description != NULL) {
1234 printf("%-21.21s %-50.50s\n",
1235 groups[i].acct_name,
1236 description);
1237 } else {
1238 printf("%s\n", groups[i].acct_name);
1241 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1243 done:
1244 return result;
1247 static int rpc_group_list(int argc, const char **argv)
1249 return run_rpc_command(NULL, PI_SAMR, 0,
1250 rpc_group_list_internals,
1251 argc, argv);
1254 static NTSTATUS
1255 rpc_list_group_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1256 const char *domain_name, const DOM_SID *domain_sid,
1257 POLICY_HND *domain_pol, uint32 rid)
1259 NTSTATUS result;
1260 POLICY_HND group_pol;
1261 uint32 num_members, *group_rids, *group_attrs;
1262 uint32 num_names;
1263 char **names;
1264 uint32 *name_types;
1265 int i;
1267 fstring sid_str;
1268 sid_to_string(sid_str, domain_sid);
1270 result = cli_samr_open_group(cli, mem_ctx, domain_pol,
1271 MAXIMUM_ALLOWED_ACCESS,
1272 rid, &group_pol);
1274 if (!NT_STATUS_IS_OK(result))
1275 return result;
1277 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
1278 &num_members, &group_rids,
1279 &group_attrs);
1281 if (!NT_STATUS_IS_OK(result))
1282 return result;
1284 while (num_members > 0) {
1285 int this_time = 512;
1287 if (num_members < this_time)
1288 this_time = num_members;
1290 result = cli_samr_lookup_rids(cli, mem_ctx, domain_pol, 1000,
1291 this_time, group_rids,
1292 &num_names, &names, &name_types);
1294 if (!NT_STATUS_IS_OK(result))
1295 return result;
1297 /* We only have users as members, but make the output
1298 the same as the output of alias members */
1300 for (i = 0; i < this_time; i++) {
1302 if (opt_long_list_entries) {
1303 printf("%s-%d %s\\%s %d\n", sid_str,
1304 group_rids[i], domain_name, names[i],
1305 SID_NAME_USER);
1306 } else {
1307 printf("%s\\%s\n", domain_name, names[i]);
1311 num_members -= this_time;
1312 group_rids += 512;
1315 return NT_STATUS_OK;
1318 static NTSTATUS
1319 rpc_list_alias_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1320 POLICY_HND *domain_pol, uint32 rid)
1322 NTSTATUS result;
1323 POLICY_HND alias_pol, lsa_pol;
1324 uint32 num_members;
1325 DOM_SID *alias_sids;
1326 char **domains;
1327 char **names;
1328 uint32 *types;
1329 int i;
1331 result = cli_samr_open_alias(cli, mem_ctx, domain_pol,
1332 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
1334 if (!NT_STATUS_IS_OK(result))
1335 return result;
1337 result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
1338 &num_members, &alias_sids);
1340 if (!NT_STATUS_IS_OK(result)) {
1341 d_printf("Couldn't list alias members\n");
1342 return result;
1345 if (num_members == 0) {
1346 return NT_STATUS_OK;
1349 cli_nt_session_close(cli);
1351 if (!cli_nt_session_open(cli, PI_LSARPC)) {
1352 d_printf("Couldn't open LSA pipe\n");
1353 return result;
1356 result = cli_lsa_open_policy(cli, mem_ctx, True,
1357 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1359 if (!NT_STATUS_IS_OK(result)) {
1360 d_printf("Couldn't open LSA policy handle\n");
1361 return result;
1364 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, num_members,
1365 alias_sids,
1366 &domains, &names, &types);
1368 if (!NT_STATUS_IS_OK(result) &&
1369 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
1370 d_printf("Couldn't lookup SIDs\n");
1371 return result;
1374 for (i = 0; i < num_members; i++) {
1375 fstring sid_str;
1376 sid_to_string(sid_str, &alias_sids[i]);
1378 if (opt_long_list_entries) {
1379 printf("%s %s\\%s %d\n", sid_str,
1380 domains[i] ? domains[i] : "*unknown*",
1381 names[i] ? names[i] : "*unknown*", types[i]);
1382 } else {
1383 if (domains[i])
1384 printf("%s\\%s\n", domains[i], names[i]);
1385 else
1386 printf("%s\n", sid_str);
1390 return NT_STATUS_OK;
1393 static NTSTATUS
1394 rpc_group_members_internals(const DOM_SID *domain_sid,
1395 const char *domain_name,
1396 struct cli_state *cli,
1397 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1399 NTSTATUS result;
1400 POLICY_HND connect_pol, domain_pol;
1401 uint32 num_rids, *rids, *rid_types;
1403 /* Get sam policy handle */
1405 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1406 &connect_pol);
1408 if (!NT_STATUS_IS_OK(result))
1409 return result;
1411 /* Get domain policy handle */
1413 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1414 MAXIMUM_ALLOWED_ACCESS,
1415 domain_sid, &domain_pol);
1417 if (!NT_STATUS_IS_OK(result))
1418 return result;
1420 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1421 1, argv, &num_rids, &rids, &rid_types);
1423 if (!NT_STATUS_IS_OK(result)) {
1425 /* Ok, did not find it in the global sam, try with builtin */
1427 DOM_SID sid_Builtin;
1429 cli_samr_close(cli, mem_ctx, &domain_pol);
1431 string_to_sid(&sid_Builtin, "S-1-5-32");
1433 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1434 MAXIMUM_ALLOWED_ACCESS,
1435 &sid_Builtin, &domain_pol);
1437 if (!NT_STATUS_IS_OK(result)) {
1438 d_printf("Couldn't find group %s\n", argv[0]);
1439 return result;
1442 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1443 1, argv, &num_rids,
1444 &rids, &rid_types);
1446 if (!NT_STATUS_IS_OK(result)) {
1447 d_printf("Couldn't find group %s\n", argv[0]);
1448 return result;
1452 if (num_rids != 1) {
1453 d_printf("Couldn't find group %s\n", argv[0]);
1454 return result;
1457 if (rid_types[0] == SID_NAME_DOM_GRP) {
1458 return rpc_list_group_members(cli, mem_ctx, domain_name,
1459 domain_sid, &domain_pol,
1460 rids[0]);
1463 if (rid_types[0] == SID_NAME_ALIAS) {
1464 return rpc_list_alias_members(cli, mem_ctx, &domain_pol,
1465 rids[0]);
1468 return NT_STATUS_NO_SUCH_GROUP;
1471 static int rpc_group_members(int argc, const char **argv)
1473 if (argc != 1) {
1474 return rpc_group_usage(argc, argv);
1477 return run_rpc_command(NULL, PI_SAMR, 0,
1478 rpc_group_members_internals,
1479 argc, argv);
1482 /**
1483 * 'net rpc group' entrypoint.
1484 * @param argc Standard main() style argc
1485 * @param argc Standard main() style argv. Initial components are already
1486 * stripped
1489 int net_rpc_group(int argc, const char **argv)
1491 struct functable func[] = {
1492 #if 0
1493 {"add", rpc_group_add},
1494 {"delete", rpc_group_delete},
1495 #endif
1496 {"list", rpc_group_list},
1497 {"members", rpc_group_members},
1498 {NULL, NULL}
1501 if (argc == 0) {
1502 if (opt_long_list_entries) {
1503 } else {
1505 return run_rpc_command(NULL, PI_SAMR, 0,
1506 rpc_group_list_internals,
1507 argc, argv);
1510 return net_run_function(argc, argv, func, rpc_group_usage);
1513 /****************************************************************************/
1515 static int rpc_share_usage(int argc, const char **argv)
1517 return net_help_share(argc, argv);
1520 /**
1521 * Add a share on a remote RPC server
1523 * All parameters are provided by the run_rpc_command function, except for
1524 * argc, argv which are passes through.
1526 * @param domain_sid The domain sid acquired from the remote server
1527 * @param cli A cli_state connected to the server.
1528 * @param mem_ctx Talloc context, destoyed on completion of the function.
1529 * @param argc Standard main() style argc
1530 * @param argv Standard main() style argv. Initial components are already
1531 * stripped
1533 * @return Normal NTSTATUS return.
1535 static NTSTATUS
1536 rpc_share_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1537 struct cli_state *cli,
1538 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1540 WERROR result;
1541 char *sharename=talloc_strdup(mem_ctx, argv[0]);
1542 char *path;
1543 uint32 type=0; /* only allow disk shares to be added */
1544 uint32 num_users=0, perms=0;
1545 char *password=NULL; /* don't allow a share password */
1547 path = strchr(sharename, '=');
1548 if (!path)
1549 return NT_STATUS_UNSUCCESSFUL;
1550 *path++ = '\0';
1552 result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
1553 opt_comment, perms, opt_maxusers,
1554 num_users, path, password);
1555 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1558 static int rpc_share_add(int argc, const char **argv)
1560 if ((argc < 1) || !strchr(argv[0], '=')) {
1561 DEBUG(1,("Sharename or path not specified on add\n"));
1562 return rpc_share_usage(argc, argv);
1564 return run_rpc_command(NULL, PI_SRVSVC, 0,
1565 rpc_share_add_internals,
1566 argc, argv);
1569 /**
1570 * Delete a share on a remote RPC server
1572 * All parameters are provided by the run_rpc_command function, except for
1573 * argc, argv which are passes through.
1575 * @param domain_sid The domain sid acquired from the remote server
1576 * @param cli A cli_state connected to the server.
1577 * @param mem_ctx Talloc context, destoyed on completion of the function.
1578 * @param argc Standard main() style argc
1579 * @param argv Standard main() style argv. Initial components are already
1580 * stripped
1582 * @return Normal NTSTATUS return.
1584 static NTSTATUS
1585 rpc_share_del_internals(const DOM_SID *domain_sid, const char *domain_name,
1586 struct cli_state *cli,
1587 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1589 WERROR result;
1591 result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
1592 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1595 /**
1596 * Delete a share on a remote RPC server
1598 * @param domain_sid The domain sid acquired from the remote server
1599 * @param argc Standard main() style argc
1600 * @param argv Standard main() style argv. Initial components are already
1601 * stripped
1603 * @return A shell status integer (0 for success)
1605 static int rpc_share_delete(int argc, const char **argv)
1607 if (argc < 1) {
1608 DEBUG(1,("Sharename not specified on delete\n"));
1609 return rpc_share_usage(argc, argv);
1611 return run_rpc_command(NULL, PI_SRVSVC, 0,
1612 rpc_share_del_internals,
1613 argc, argv);
1617 * Formatted print of share info
1619 * @param info1 pointer to SRV_SHARE_INFO_1 to format
1622 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
1624 fstring netname = "", remark = "";
1626 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
1627 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
1629 if (opt_long_list_entries) {
1630 d_printf("%-12s %-8.8s %-50s\n",
1631 netname, share_type[info1->info_1.type], remark);
1632 } else {
1633 d_printf("%s\n", netname);
1638 /**
1639 * List shares on a remote RPC server
1641 * All parameters are provided by the run_rpc_command function, except for
1642 * argc, argv which are passes through.
1644 * @param domain_sid The domain sid acquired from the remote server
1645 * @param cli A cli_state connected to the server.
1646 * @param mem_ctx Talloc context, destoyed on completion of the function.
1647 * @param argc Standard main() style argc
1648 * @param argv Standard main() style argv. Initial components are already
1649 * stripped
1651 * @return Normal NTSTATUS return.
1654 static NTSTATUS
1655 rpc_share_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1656 struct cli_state *cli,
1657 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1659 SRV_SHARE_INFO_CTR ctr;
1660 WERROR result;
1661 ENUM_HND hnd;
1662 uint32 preferred_len = 0xffffffff, i;
1664 init_enum_hnd(&hnd, 0);
1666 result = cli_srvsvc_net_share_enum(
1667 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
1669 if (!W_ERROR_IS_OK(result))
1670 goto done;
1672 /* Display results */
1674 if (opt_long_list_entries) {
1675 d_printf(
1676 "\nEnumerating shared resources (exports) on remote server:\n\n"\
1677 "\nShare name Type Description\n"\
1678 "---------- ---- -----------\n");
1680 for (i = 0; i < ctr.num_entries; i++)
1681 display_share_info_1(&ctr.share.info1[i]);
1682 done:
1683 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1686 /**
1687 * 'net rpc share' entrypoint.
1688 * @param argc Standard main() style argc
1689 * @param argv Standard main() style argv. Initial components are already
1690 * stripped
1693 int net_rpc_share(int argc, const char **argv)
1695 struct functable func[] = {
1696 {"add", rpc_share_add},
1697 {"delete", rpc_share_delete},
1698 {NULL, NULL}
1701 if (argc == 0)
1702 return run_rpc_command(NULL, PI_SRVSVC, 0,
1703 rpc_share_list_internals,
1704 argc, argv);
1706 return net_run_function(argc, argv, func, rpc_share_usage);
1709 /****************************************************************************/
1711 static int rpc_file_usage(int argc, const char **argv)
1713 return net_help_file(argc, argv);
1716 /**
1717 * Close a file on a remote RPC server
1719 * All parameters are provided by the run_rpc_command function, except for
1720 * argc, argv which are passes through.
1722 * @param domain_sid The domain sid acquired from the remote server
1723 * @param cli A cli_state connected to the server.
1724 * @param mem_ctx Talloc context, destoyed on completion of the function.
1725 * @param argc Standard main() style argc
1726 * @param argv Standard main() style argv. Initial components are already
1727 * stripped
1729 * @return Normal NTSTATUS return.
1731 static NTSTATUS
1732 rpc_file_close_internals(const DOM_SID *domain_sid, const char *domain_name,
1733 struct cli_state *cli,
1734 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1736 WERROR result;
1737 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
1738 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1741 /**
1742 * Close a file on a remote RPC server
1744 * @param argc Standard main() style argc
1745 * @param argv Standard main() style argv. Initial components are already
1746 * stripped
1748 * @return A shell status integer (0 for success)
1750 static int rpc_file_close(int argc, const char **argv)
1752 if (argc < 1) {
1753 DEBUG(1, ("No fileid given on close\n"));
1754 return(rpc_file_usage(argc, argv));
1757 return run_rpc_command(NULL, PI_SRVSVC, 0,
1758 rpc_file_close_internals,
1759 argc, argv);
1762 /**
1763 * Formatted print of open file info
1765 * @param info3 FILE_INFO_3 contents
1766 * @param str3 strings for FILE_INFO_3
1769 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
1771 fstring user = "", path = "";
1773 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
1774 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
1776 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
1777 info3->id, user, info3->perms, info3->num_locks, path);
1780 /**
1781 * List open files on a remote RPC server
1783 * All parameters are provided by the run_rpc_command function, except for
1784 * argc, argv which are passes through.
1786 * @param domain_sid The domain sid acquired from the remote server
1787 * @param cli A cli_state connected to the server.
1788 * @param mem_ctx Talloc context, destoyed on completion of the function.
1789 * @param argc Standard main() style argc
1790 * @param argv Standard main() style argv. Initial components are already
1791 * stripped
1793 * @return Normal NTSTATUS return.
1796 static NTSTATUS
1797 rpc_file_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1798 struct cli_state *cli,
1799 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1801 SRV_FILE_INFO_CTR ctr;
1802 WERROR result;
1803 ENUM_HND hnd;
1804 uint32 preferred_len = 0xffffffff, i;
1805 const char *username=NULL;
1807 init_enum_hnd(&hnd, 0);
1809 /* if argc > 0, must be user command */
1810 if (argc > 0)
1811 username = smb_xstrdup(argv[0]);
1813 result = cli_srvsvc_net_file_enum(
1814 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
1816 if (!W_ERROR_IS_OK(result))
1817 goto done;
1819 /* Display results */
1821 d_printf(
1822 "\nEnumerating open files on remote server:\n\n"\
1823 "\nFileId Opened by Perms Locks Path"\
1824 "\n------ --------- ----- ----- ---- \n");
1825 for (i = 0; i < ctr.num_entries; i++)
1826 display_file_info_3(&ctr.file.info3[i].info_3,
1827 &ctr.file.info3[i].info_3_str);
1828 done:
1829 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1833 /**
1834 * List files for a user on a remote RPC server
1836 * @param argc Standard main() style argc
1837 * @param argv Standard main() style argv. Initial components are already
1838 * stripped
1840 * @return A shell status integer (0 for success)
1842 static int rpc_file_user(int argc, const char **argv)
1844 if (argc < 1) {
1845 DEBUG(1, ("No username given\n"));
1846 return(rpc_file_usage(argc, argv));
1849 return run_rpc_command(NULL, PI_SRVSVC, 0,
1850 rpc_file_list_internals,
1851 argc, argv);
1855 /**
1856 * 'net rpc file' entrypoint.
1857 * @param argc Standard main() style argc
1858 * @param argv Standard main() style argv. Initial components are already
1859 * stripped
1862 int net_rpc_file(int argc, const char **argv)
1864 struct functable func[] = {
1865 {"close", rpc_file_close},
1866 {"user", rpc_file_user},
1867 #if 0
1868 {"info", rpc_file_info},
1869 #endif
1870 {NULL, NULL}
1873 if (argc == 0)
1874 return run_rpc_command(NULL, PI_SRVSVC, 0,
1875 rpc_file_list_internals,
1876 argc, argv);
1878 return net_run_function(argc, argv, func, rpc_file_usage);
1881 /****************************************************************************/
1885 /**
1886 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
1888 * All parameters are provided by the run_rpc_command function, except for
1889 * argc, argv which are passed through.
1891 * @param domain_sid The domain sid aquired from the remote server
1892 * @param cli A cli_state connected to the server.
1893 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1894 * @param argc Standard main() style argc
1895 * @param argv Standard main() style argv. Initial components are already
1896 * stripped
1898 * @return Normal NTSTATUS return.
1901 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
1902 const char *domain_name,
1903 struct cli_state *cli,
1904 TALLOC_CTX *mem_ctx,
1905 int argc, const char **argv)
1907 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1909 result = cli_shutdown_abort(cli, mem_ctx);
1911 if (NT_STATUS_IS_OK(result))
1912 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
1913 else
1914 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
1916 return result;
1920 /**
1921 * ABORT the shutdown of a remote RPC Server, over winreg pipe
1923 * All parameters are provided by the run_rpc_command function, except for
1924 * argc, argv which are passed through.
1926 * @param domain_sid The domain sid aquired from the remote server
1927 * @param cli A cli_state connected to the server.
1928 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1929 * @param argc Standard main() style argc
1930 * @param argv Standard main() style argv. Initial components are already
1931 * stripped
1933 * @return Normal NTSTATUS return.
1936 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
1937 const char *domain_name,
1938 struct cli_state *cli,
1939 TALLOC_CTX *mem_ctx,
1940 int argc, const char **argv)
1942 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1944 result = cli_reg_abort_shutdown(cli, mem_ctx);
1946 if (NT_STATUS_IS_OK(result))
1947 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
1948 else
1949 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
1951 return result;
1954 /**
1955 * ABORT the Shut down of a remote RPC server
1957 * @param argc Standard main() style argc
1958 * @param argv Standard main() style argv. Initial components are already
1959 * stripped
1961 * @return A shell status integer (0 for success)
1964 static int rpc_shutdown_abort(int argc, const char **argv)
1966 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
1967 rpc_shutdown_abort_internals,
1968 argc, argv);
1970 if (rc == 0)
1971 return rc;
1973 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
1975 return run_rpc_command(NULL, PI_WINREG, 0,
1976 rpc_reg_shutdown_abort_internals,
1977 argc, argv);
1980 /**
1981 * Shut down a remote RPC Server
1983 * All parameters are provided by the run_rpc_command function, except for
1984 * argc, argv which are passes through.
1986 * @param domain_sid The domain sid aquired from the remote server
1987 * @param cli A cli_state connected to the server.
1988 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1989 * @param argc Standard main() style argc
1990 * @param argc Standard main() style argv. Initial components are already
1991 * stripped
1993 * @return Normal NTSTATUS return.
1996 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid,
1997 const char *domain_name,
1998 struct cli_state *cli, TALLOC_CTX *mem_ctx,
1999 int argc, const char **argv)
2001 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2002 const char *msg = "This machine will be shutdown shortly";
2003 uint32 timeout = 20;
2004 #if 0
2005 poptContext pc;
2006 int rc;
2008 struct poptOption long_options[] = {
2009 {"message", 'm', POPT_ARG_STRING, &msg},
2010 {"timeout", 't', POPT_ARG_INT, &timeout},
2011 {"reboot", 'r', POPT_ARG_NONE, &reboot},
2012 {"force", 'f', POPT_ARG_NONE, &force},
2013 { 0, 0, 0, 0}
2016 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
2017 POPT_CONTEXT_KEEP_FIRST);
2019 rc = poptGetNextOpt(pc);
2021 if (rc < -1) {
2022 /* an error occurred during option processing */
2023 DEBUG(0, ("%s: %s\n",
2024 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
2025 poptStrerror(rc)));
2026 return NT_STATUS_INVALID_PARAMETER;
2028 #endif
2029 if (opt_comment) {
2030 msg = opt_comment;
2032 if (opt_timeout) {
2033 timeout = opt_timeout;
2036 /* create an entry */
2037 result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force);
2039 if (NT_STATUS_IS_OK(result))
2040 DEBUG(5,("Shutdown of remote machine succeeded\n"));
2041 else
2042 DEBUG(0,("Shutdown of remote machine failed!\n"));
2044 return result;
2047 /**
2048 * Shut down a remote RPC server
2050 * @param argc Standard main() style argc
2051 * @param argc Standard main() style argv. Initial components are already
2052 * stripped
2054 * @return A shell status integer (0 for success)
2057 static int rpc_shutdown(int argc, const char **argv)
2059 return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_internals,
2060 argc, argv);
2063 /***************************************************************************
2064 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
2066 ***************************************************************************/
2069 * Add interdomain trust account to the RPC server.
2070 * All parameters (except for argc and argv) are passed by run_rpc_command
2071 * function.
2073 * @param domain_sid The domain sid acquired from the server
2074 * @param cli A cli_state connected to the server.
2075 * @param mem_ctx Talloc context, destoyed on completion of the function.
2076 * @param argc Standard main() style argc
2077 * @param argc Standard main() style argv. Initial components are already
2078 * stripped
2080 * @return normal NTSTATUS return code
2083 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
2084 const char *domain_name,
2085 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2086 int argc, const char **argv) {
2088 POLICY_HND connect_pol, domain_pol, user_pol;
2089 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2090 char *acct_name;
2091 uint16 acb_info;
2092 uint32 unknown, user_rid;
2094 if (argc != 2) {
2095 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
2096 return NT_STATUS_INVALID_PARAMETER;
2100 * Make valid trusting domain account (ie. uppercased and with '$' appended)
2103 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
2104 return NT_STATUS_NO_MEMORY;
2107 strupper_m(acct_name);
2109 /* Get samr policy handle */
2110 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2111 &connect_pol);
2112 if (!NT_STATUS_IS_OK(result)) {
2113 goto done;
2116 /* Get domain policy handle */
2117 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2118 MAXIMUM_ALLOWED_ACCESS,
2119 domain_sid, &domain_pol);
2120 if (!NT_STATUS_IS_OK(result)) {
2121 goto done;
2124 /* Create trusting domain's account */
2125 acb_info = ACB_DOMTRUST;
2126 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
2127 mimir: yes, most probably it is */
2129 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
2130 acct_name, acb_info, unknown,
2131 &user_pol, &user_rid);
2132 if (!NT_STATUS_IS_OK(result)) {
2133 goto done;
2137 SAM_USERINFO_CTR ctr;
2138 SAM_USER_INFO_24 p24;
2139 uchar pwbuf[516];
2141 encode_pw_buffer((char *)pwbuf, argv[1], STR_UNICODE);
2143 ZERO_STRUCT(ctr);
2144 ZERO_STRUCT(p24);
2146 init_sam_user_info24(&p24, (char *)pwbuf, 24);
2148 ctr.switch_value = 24;
2149 ctr.info.id24 = &p24;
2151 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
2152 &cli->user_session_key, &ctr);
2154 if (!NT_STATUS_IS_OK(result)) {
2155 DEBUG(0,("Could not set trust account password: %s\n",
2156 nt_errstr(result)));
2157 goto done;
2161 done:
2162 SAFE_FREE(acct_name);
2163 return result;
2167 * Create interdomain trust account for a remote domain.
2169 * @param argc standard argc
2170 * @param argv standard argv without initial components
2172 * @return Integer status (0 means success)
2175 static int rpc_trustdom_add(int argc, const char **argv)
2177 if (argc > 0) {
2178 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
2179 argc, argv);
2180 } else {
2181 d_printf("Usage: net rpc trustdom add <domain>\n");
2182 return -1;
2188 * Delete interdomain trust account for a remote domain.
2190 * @param argc standard argc
2191 * @param argv standard argv without initial components
2193 * @return Integer status (0 means success)
2196 static int rpc_trustdom_del(int argc, const char **argv)
2198 d_printf("Sorry, not yet implemented.\n");
2199 d_printf("Use 'smbpasswd -x -i' instead.\n");
2200 return -1;
2205 * Establish trust relationship to a trusting domain.
2206 * Interdomain account must already be created on remote PDC.
2208 * @param argc standard argc
2209 * @param argv standard argv without initial components
2211 * @return Integer status (0 means success)
2214 static int rpc_trustdom_establish(int argc, const char **argv)
2216 struct cli_state *cli;
2217 struct in_addr server_ip;
2218 POLICY_HND connect_hnd;
2219 TALLOC_CTX *mem_ctx;
2220 NTSTATUS nt_status;
2221 DOM_SID *domain_sid;
2222 WKS_INFO_100 wks_info;
2224 char* domain_name;
2225 char* domain_name_pol;
2226 char* acct_name;
2227 fstring pdc_name;
2230 * Connect to \\server\ipc$ as 'our domain' account with password
2233 if (argc != 1) {
2234 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
2235 return -1;
2238 domain_name = smb_xstrdup(argv[0]);
2239 strupper_m(domain_name);
2241 /* account name used at first is our domain's name with '$' */
2242 asprintf(&acct_name, "%s$", lp_workgroup());
2243 strupper_m(acct_name);
2246 * opt_workgroup will be used by connection functions further,
2247 * hence it should be set to remote domain name instead of ours
2249 if (opt_workgroup) {
2250 opt_workgroup = smb_xstrdup(domain_name);
2253 opt_user_name = acct_name;
2255 /* find the domain controller */
2256 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
2257 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
2258 return -1;
2261 /* connect to ipc$ as username/password */
2262 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
2263 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
2265 /* Is it trusting domain account for sure ? */
2266 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
2267 nt_errstr(nt_status)));
2268 return -1;
2272 * Connect to \\server\ipc$ again (this time anonymously)
2275 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
2277 if (NT_STATUS_IS_ERR(nt_status)) {
2278 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
2279 domain_name, nt_errstr(nt_status)));
2283 * Use NetServerEnum2 to make sure we're talking to a proper server
2286 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
2287 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
2288 for domain %s\n", domain_name));
2292 * Call WksQueryInfo to check remote server's capabilities
2293 * note: It is now used only to get unicode domain name
2296 if (!cli_nt_session_open(cli, PI_WKSSVC)) {
2297 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
2298 return -1;
2301 if (!(mem_ctx = talloc_init("establishing trust relationship to domain %s",
2302 domain_name))) {
2303 DEBUG(0, ("talloc_init() failed\n"));
2304 cli_shutdown(cli);
2305 return -1;
2308 nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
2310 if (NT_STATUS_IS_ERR(nt_status)) {
2311 DEBUG(0, ("WksQueryInfo call failed.\n"));
2312 return -1;
2315 if (cli->nt_pipe_fnum)
2316 cli_nt_session_close(cli);
2320 * Call LsaOpenPolicy and LsaQueryInfo
2323 if (!(mem_ctx = talloc_init("rpc_trustdom_establish"))) {
2324 DEBUG(0, ("talloc_init() failed\n"));
2325 cli_shutdown(cli);
2326 return -1;
2329 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2330 DEBUG(0, ("Could not initialise lsa pipe\n"));
2331 cli_shutdown(cli);
2332 return -1;
2335 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
2336 &connect_hnd);
2337 if (NT_STATUS_IS_ERR(nt_status)) {
2338 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2339 nt_errstr(nt_status)));
2340 return -1;
2343 /* Querying info level 5 */
2345 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
2346 5 /* info level */, &domain_name_pol,
2347 &domain_sid);
2348 if (NT_STATUS_IS_ERR(nt_status)) {
2349 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2350 nt_errstr(nt_status)));
2351 return -1;
2357 /* There should be actually query info level 3 (following nt serv behaviour),
2358 but I still don't know if it's _really_ necessary */
2361 * Store the password in secrets db
2364 if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
2365 wks_info.uni_lan_grp.uni_str_len, opt_password,
2366 *domain_sid)) {
2367 DEBUG(0, ("Storing password for trusted domain failed.\n"));
2368 return -1;
2372 * Close the pipes and clean up
2375 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2376 if (NT_STATUS_IS_ERR(nt_status)) {
2377 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
2378 nt_errstr(nt_status)));
2379 return -1;
2382 if (cli->nt_pipe_fnum)
2383 cli_nt_session_close(cli);
2385 talloc_destroy(mem_ctx);
2387 DEBUG(0, ("Success!\n"));
2388 return 0;
2392 * Revoke trust relationship to the remote domain
2394 * @param argc standard argc
2395 * @param argv standard argv without initial components
2397 * @return Integer status (0 means success)
2400 static int rpc_trustdom_revoke(int argc, const char **argv)
2402 char* domain_name;
2404 if (argc < 1) return -1;
2406 /* generate upper cased domain name */
2407 domain_name = smb_xstrdup(argv[0]);
2408 strupper_m(domain_name);
2410 /* delete password of the trust */
2411 if (!trusted_domain_password_delete(domain_name)) {
2412 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
2413 domain_name));
2414 return -1;
2417 return 0;
2421 * Usage for 'net rpc trustdom' command
2423 * @param argc standard argc
2424 * @param argv standard argv without inital components
2426 * @return Integer status returned to shell
2429 static int rpc_trustdom_usage(int argc, const char **argv)
2431 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
2432 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
2433 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
2434 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
2435 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
2436 return -1;
2440 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
2441 const char *domain_name,
2442 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2443 int argc, const char **argv)
2445 fstring str_sid;
2446 sid_to_string(str_sid, domain_sid);
2447 d_printf("%s\n", str_sid);
2448 return NT_STATUS_OK;
2452 static int rpc_trustdom_list(int argc, const char **argv)
2454 /* common variables */
2455 TALLOC_CTX* mem_ctx;
2456 struct cli_state *cli, *remote_cli;
2457 NTSTATUS nt_status;
2458 const char *domain_name = NULL;
2459 DOM_SID *queried_dom_sid;
2460 fstring ascii_sid, padding;
2461 int ascii_dom_name_len;
2462 POLICY_HND connect_hnd;
2464 /* trusted domains listing variables */
2465 unsigned int num_domains, enum_ctx = 0;
2466 int i, pad_len, col_len = 20;
2467 DOM_SID *domain_sids;
2468 char **trusted_dom_names;
2469 fstring pdc_name;
2470 char *dummy;
2472 /* trusting domains listing variables */
2473 POLICY_HND domain_hnd;
2474 char **trusting_dom_names;
2475 uint32 *trusting_dom_rids;
2478 * Listing trusted domains (stored in secrets.tdb, if local)
2481 mem_ctx = talloc_init("trust relationships listing");
2484 * set domain and pdc name to local samba server (default)
2485 * or to remote one given in command line
2488 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
2489 domain_name = opt_workgroup;
2490 opt_target_workgroup = opt_workgroup;
2491 } else {
2492 fstrcpy(pdc_name, global_myname());
2493 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
2494 opt_target_workgroup = domain_name;
2497 /* open \PIPE\lsarpc and open policy handle */
2498 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
2499 DEBUG(0, ("Couldn't connect to domain controller\n"));
2500 return -1;
2503 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2504 DEBUG(0, ("Could not initialise lsa pipe\n"));
2505 return -1;
2508 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
2509 &connect_hnd);
2510 if (NT_STATUS_IS_ERR(nt_status)) {
2511 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2512 nt_errstr(nt_status)));
2513 return -1;
2516 /* query info level 5 to obtain sid of a domain being queried */
2517 nt_status = cli_lsa_query_info_policy(
2518 cli, mem_ctx, &connect_hnd, 5 /* info level */,
2519 &dummy, &queried_dom_sid);
2521 if (NT_STATUS_IS_ERR(nt_status)) {
2522 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2523 nt_errstr(nt_status)));
2524 return -1;
2528 * Keep calling LsaEnumTrustdom over opened pipe until
2529 * the end of enumeration is reached
2532 d_printf("Trusted domains list:\n\n");
2534 do {
2535 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
2536 &num_domains,
2537 &trusted_dom_names, &domain_sids);
2539 if (NT_STATUS_IS_ERR(nt_status)) {
2540 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
2541 nt_errstr(nt_status)));
2542 return -1;
2545 for (i = 0; i < num_domains; i++) {
2546 /* convert sid into ascii string */
2547 sid_to_string(ascii_sid, &(domain_sids[i]));
2549 /* calculate padding space for d_printf to look nicer */
2550 pad_len = col_len - strlen(trusted_dom_names[i]);
2551 padding[pad_len] = 0;
2552 do padding[--pad_len] = ' '; while (pad_len);
2554 d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
2558 * in case of no trusted domains say something rather
2559 * than just display blank line
2561 if (!num_domains) d_printf("none\n");
2563 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2565 /* close this connection before doing next one */
2566 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2567 if (NT_STATUS_IS_ERR(nt_status)) {
2568 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
2569 nt_errstr(nt_status)));
2570 return -1;
2573 cli_nt_session_close(cli);
2576 * Listing trusting domains (stored in passdb backend, if local)
2579 d_printf("\nTrusting domains list:\n\n");
2582 * Open \PIPE\samr and get needed policy handles
2584 if (!cli_nt_session_open(cli, PI_SAMR)) {
2585 DEBUG(0, ("Could not initialise samr pipe\n"));
2586 return -1;
2589 /* SamrConnect */
2590 nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
2591 &connect_hnd);
2592 if (!NT_STATUS_IS_OK(nt_status)) {
2593 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
2594 nt_errstr(nt_status)));
2595 return -1;
2598 /* SamrOpenDomain - we have to open domain policy handle in order to be
2599 able to enumerate accounts*/
2600 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
2601 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
2602 queried_dom_sid, &domain_hnd);
2603 if (!NT_STATUS_IS_OK(nt_status)) {
2604 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
2605 nt_errstr(nt_status)));
2606 return -1;
2610 * perform actual enumeration
2613 enum_ctx = 0; /* reset enumeration context from last enumeration */
2614 do {
2616 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
2617 &enum_ctx, ACB_DOMTRUST, 0xffff,
2618 &trusting_dom_names, &trusting_dom_rids,
2619 &num_domains);
2620 if (NT_STATUS_IS_ERR(nt_status)) {
2621 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
2622 nt_errstr(nt_status)));
2623 return -1;
2626 for (i = 0; i < num_domains; i++) {
2629 * get each single domain's sid (do we _really_ need this ?):
2630 * 1) connect to domain's pdc
2631 * 2) query the pdc for domain's sid
2634 /* get rid of '$' tail */
2635 ascii_dom_name_len = strlen(trusting_dom_names[i]);
2636 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
2637 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
2639 /* calculate padding space for d_printf to look nicer */
2640 pad_len = col_len - strlen(trusting_dom_names[i]);
2641 padding[pad_len] = 0;
2642 do padding[--pad_len] = ' '; while (pad_len);
2644 /* set opt_* variables to remote domain */
2645 strupper_m(trusting_dom_names[i]);
2646 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
2647 opt_target_workgroup = opt_workgroup;
2649 d_printf("%s%s", trusting_dom_names[i], padding);
2651 /* connect to remote domain controller */
2652 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
2653 if (remote_cli) {
2654 /* query for domain's sid */
2655 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
2656 d_printf("couldn't get domain's sid\n");
2658 cli_shutdown(remote_cli);
2660 } else {
2661 d_printf("domain controller is not responding\n");
2665 if (!num_domains) d_printf("none\n");
2667 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2669 /* close opened samr and domain policy handles */
2670 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
2671 if (!NT_STATUS_IS_OK(nt_status)) {
2672 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
2675 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
2676 if (!NT_STATUS_IS_OK(nt_status)) {
2677 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
2680 /* close samr pipe and connection to IPC$ */
2681 cli_nt_session_close(cli);
2682 cli_shutdown(cli);
2684 talloc_destroy(mem_ctx);
2685 return 0;
2689 * Entrypoint for 'net rpc trustdom' code
2691 * @param argc standard argc
2692 * @param argv standard argv without initial components
2694 * @return Integer status (0 means success)
2697 static int rpc_trustdom(int argc, const char **argv)
2699 struct functable func[] = {
2700 {"add", rpc_trustdom_add},
2701 {"del", rpc_trustdom_del},
2702 {"establish", rpc_trustdom_establish},
2703 {"revoke", rpc_trustdom_revoke},
2704 {"help", rpc_trustdom_usage},
2705 {"list", rpc_trustdom_list},
2706 {NULL, NULL}
2709 if (argc == 0) {
2710 rpc_trustdom_usage(argc, argv);
2711 return -1;
2714 return (net_run_function(argc, argv, func, rpc_user_usage));
2718 * Check if a server will take rpc commands
2719 * @param flags Type of server to connect to (PDC, DMB, localhost)
2720 * if the host is not explicitly specified
2721 * @return BOOL (true means rpc supported)
2723 BOOL net_rpc_check(unsigned flags)
2725 struct cli_state cli;
2726 BOOL ret = False;
2727 struct in_addr server_ip;
2728 char *server_name = NULL;
2730 /* flags (i.e. server type) may depend on command */
2731 if (!net_find_server(flags, &server_ip, &server_name))
2732 return False;
2734 ZERO_STRUCT(cli);
2735 if (cli_initialise(&cli) == False)
2736 return False;
2738 if (!cli_connect(&cli, server_name, &server_ip))
2739 goto done;
2740 if (!attempt_netbios_session_request(&cli, global_myname(),
2741 server_name, &server_ip))
2742 goto done;
2743 if (!cli_negprot(&cli))
2744 goto done;
2745 if (cli.protocol < PROTOCOL_NT1)
2746 goto done;
2748 ret = True;
2749 done:
2750 cli_shutdown(&cli);
2751 return ret;
2754 /* dump sam database via samsync rpc calls */
2755 static int rpc_samdump(int argc, const char **argv) {
2756 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
2757 argc, argv);
2760 /* syncronise sam database via samsync rpc calls */
2761 static int rpc_vampire(int argc, const char **argv) {
2762 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
2763 argc, argv);
2765 /****************************************************************************/
2768 /**
2769 * Basic usage function for 'net rpc'
2770 * @param argc Standard main() style argc
2771 * @param argv Standard main() style argv. Initial components are already
2772 * stripped
2775 int net_rpc_usage(int argc, const char **argv)
2777 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
2778 d_printf(" net rpc join \t\t\tto join a domain \n");
2779 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n\n\n");
2780 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
2781 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
2782 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass");
2783 d_printf(" net rpc group \t\tto list groups\n");
2784 d_printf(" net rpc share \t\tto add, delete, and list shares\n");
2785 d_printf(" net rpc file \t\t\tto list open files\n");
2786 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
2787 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
2788 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
2789 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
2790 d_printf(" net rpc trustdom \t\tto create trusting domain's account\n"
2791 "\t\t\t\t\tor establish trust\n");
2792 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
2793 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
2794 d_printf("\n");
2795 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
2796 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
2797 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
2798 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
2799 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
2800 return -1;
2805 * Help function for 'net rpc'. Calls command specific help if requested
2806 * or displays usage of net rpc
2807 * @param argc Standard main() style argc
2808 * @param argv Standard main() style argv. Initial components are already
2809 * stripped
2812 int net_rpc_help(int argc, const char **argv)
2814 struct functable func[] = {
2815 {"join", rpc_join_usage},
2816 {"user", rpc_user_usage},
2817 {"group", rpc_group_usage},
2818 {"share", rpc_share_usage},
2819 /*{"changetrustpw", rpc_changetrustpw_usage}, */
2820 {"trustdom", rpc_trustdom_usage},
2821 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
2822 /*{"shutdown", rpc_shutdown_usage}, */
2823 {NULL, NULL}
2826 if (argc == 0) {
2827 net_rpc_usage(argc, argv);
2828 return -1;
2831 return (net_run_function(argc, argv, func, rpc_user_usage));
2835 /**
2836 * 'net rpc' entrypoint.
2837 * @param argc Standard main() style argc
2838 * @param argv Standard main() style argv. Initial components are already
2839 * stripped
2842 int net_rpc(int argc, const char **argv)
2844 struct functable func[] = {
2845 {"info", net_rpc_info},
2846 {"join", net_rpc_join},
2847 {"oldjoin", net_rpc_oldjoin},
2848 {"testjoin", net_rpc_testjoin},
2849 {"user", net_rpc_user},
2850 {"password", rpc_user_password},
2851 {"group", net_rpc_group},
2852 {"share", net_rpc_share},
2853 {"file", net_rpc_file},
2854 {"changetrustpw", net_rpc_changetrustpw},
2855 {"trustdom", rpc_trustdom},
2856 {"abortshutdown", rpc_shutdown_abort},
2857 {"shutdown", rpc_shutdown},
2858 {"samdump", rpc_samdump},
2859 {"vampire", rpc_vampire},
2860 {"getsid", net_rpc_getsid},
2861 {"help", net_rpc_help},
2862 {NULL, NULL}
2864 return net_run_function(argc, argv, func, net_rpc_usage);