Add 'net rpc group add'. For this parse_samr.c had to be changed: The
[Samba/gebeck_regimport.git] / source3 / utils / net_rpc.c
blobe46dfe7fd4cbb6801c3e3cb7f9c4dee2c1fdac0e
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 static NTSTATUS
1048 rpc_group_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1049 struct cli_state *cli,
1050 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1052 POLICY_HND connect_pol, domain_pol, group_pol;
1053 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1054 GROUP_INFO_CTR group_info;
1056 if (argc != 1) {
1057 d_printf("Group name must be specified\n");
1058 rpc_group_usage(argc, argv);
1059 return NT_STATUS_OK;
1062 /* Get sam policy handle */
1064 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1065 &connect_pol);
1066 if (!NT_STATUS_IS_OK(result)) goto done;
1068 /* Get domain policy handle */
1070 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1071 MAXIMUM_ALLOWED_ACCESS,
1072 domain_sid, &domain_pol);
1073 if (!NT_STATUS_IS_OK(result)) goto done;
1075 /* Create the group */
1077 result = cli_samr_create_dom_group(cli, mem_ctx, &domain_pol,
1078 argv[0], MAXIMUM_ALLOWED_ACCESS,
1079 &group_pol);
1080 if (!NT_STATUS_IS_OK(result)) goto done;
1082 if (strlen(opt_comment) == 0) goto done;
1084 /* We've got a comment to set */
1086 group_info.switch_value1 = 4;
1087 init_samr_group_info4(&group_info.group.info4, opt_comment);
1089 result = cli_samr_set_groupinfo(cli, mem_ctx, &group_pol, &group_info);
1090 if (!NT_STATUS_IS_OK(result)) goto done;
1092 done:
1093 if (NT_STATUS_IS_OK(result))
1094 DEBUG(5, ("add group succeeded\n"));
1095 else
1096 d_printf("add group failed: %s\n", nt_errstr(result));
1098 return result;
1101 static int rpc_group_add(int argc, const char **argv)
1103 return run_rpc_command(NULL, PI_SAMR, 0,
1104 rpc_group_add_internals,
1105 argc, argv);
1108 /**
1109 * List groups on a remote RPC server
1111 * All parameters are provided by the run_rpc_command function, except for
1112 * argc, argv which are passes through.
1114 * @param domain_sid The domain sid acquired from the remote server
1115 * @param cli A cli_state connected to the server.
1116 * @param mem_ctx Talloc context, destoyed on completion of the function.
1117 * @param argc Standard main() style argc
1118 * @param argv Standard main() style argv. Initial components are already
1119 * stripped
1121 * @return Normal NTSTATUS return.
1124 static NTSTATUS
1125 rpc_group_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1126 struct cli_state *cli,
1127 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1129 POLICY_HND connect_pol, domain_pol;
1130 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1131 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
1132 struct acct_info *groups;
1133 DOM_SID global_sid_Builtin;
1134 BOOL global = False;
1135 BOOL local = False;
1136 BOOL builtin = False;
1138 if (argc == 0) {
1139 global = True;
1140 local = True;
1141 builtin = True;
1144 for (i=0; i<argc; i++) {
1145 if (strequal(argv[i], "global"))
1146 global = True;
1148 if (strequal(argv[i], "local"))
1149 local = True;
1151 if (strequal(argv[i], "builtin"))
1152 builtin = True;
1155 string_to_sid(&global_sid_Builtin, "S-1-5-32");
1157 /* Get sam policy handle */
1159 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1160 &connect_pol);
1161 if (!NT_STATUS_IS_OK(result)) {
1162 goto done;
1165 /* Get domain policy handle */
1167 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1168 MAXIMUM_ALLOWED_ACCESS,
1169 domain_sid, &domain_pol);
1170 if (!NT_STATUS_IS_OK(result)) {
1171 goto done;
1174 /* Query domain groups */
1175 if (opt_long_list_entries)
1176 d_printf("\nGroup name Comment"\
1177 "\n-----------------------------\n");
1178 do {
1179 SAM_DISPINFO_CTR ctr;
1180 SAM_DISPINFO_3 info3;
1181 uint32 max_size;
1183 ZERO_STRUCT(ctr);
1184 ZERO_STRUCT(info3);
1185 ctr.sam.info3 = &info3;
1187 if (!global) break;
1189 get_query_dispinfo_params(
1190 loop_count, &max_entries, &max_size);
1192 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1193 &start_idx, 3, &num_entries,
1194 max_entries, max_size, &ctr);
1196 if (!NT_STATUS_IS_OK(result))
1197 break;
1199 for (i = 0; i < num_entries; i++) {
1201 fstring group, desc;
1203 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
1204 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
1206 if (opt_long_list_entries)
1207 printf("%-21.21s %-50.50s\n",
1208 group, desc);
1209 else
1210 printf("%s\n", group);
1212 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1213 /* query domain aliases */
1214 start_idx = 0;
1215 do {
1216 if (!local) break;
1218 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1219 &start_idx, max_entries,
1220 &groups, &num_entries);
1222 if (!NT_STATUS_IS_OK(result))
1223 break;
1225 for (i = 0; i < num_entries; i++) {
1227 char *description = NULL;
1229 if (opt_long_list_entries) {
1231 POLICY_HND alias_pol;
1232 ALIAS_INFO_CTR ctr;
1234 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1235 &domain_pol,
1236 0x8,
1237 groups[i].rid,
1238 &alias_pol))) &&
1239 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1240 &alias_pol, 3,
1241 &ctr))) &&
1242 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1243 &alias_pol)))) {
1244 description = unistr2_tdup(mem_ctx,
1245 &ctr.alias.info3.uni_acct_desc);
1249 if (description != NULL) {
1250 printf("%-21.21s %-50.50s\n",
1251 groups[i].acct_name,
1252 description);
1253 } else {
1254 printf("%s\n", groups[i].acct_name);
1257 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1258 cli_samr_close(cli, mem_ctx, &domain_pol);
1259 /* Get builtin policy handle */
1261 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1262 MAXIMUM_ALLOWED_ACCESS,
1263 &global_sid_Builtin, &domain_pol);
1264 if (!NT_STATUS_IS_OK(result)) {
1265 goto done;
1267 /* query builtin aliases */
1268 start_idx = 0;
1269 do {
1270 if (!builtin) break;
1272 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1273 &start_idx, max_entries,
1274 &groups, &num_entries);
1276 if (!NT_STATUS_IS_OK(result))
1277 break;
1279 for (i = 0; i < num_entries; i++) {
1281 char *description = NULL;
1283 if (opt_long_list_entries) {
1285 POLICY_HND alias_pol;
1286 ALIAS_INFO_CTR ctr;
1288 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1289 &domain_pol,
1290 0x8,
1291 groups[i].rid,
1292 &alias_pol))) &&
1293 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1294 &alias_pol, 3,
1295 &ctr))) &&
1296 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1297 &alias_pol)))) {
1298 description = unistr2_tdup(mem_ctx,
1299 &ctr.alias.info3.uni_acct_desc);
1303 if (description != NULL) {
1304 printf("%-21.21s %-50.50s\n",
1305 groups[i].acct_name,
1306 description);
1307 } else {
1308 printf("%s\n", groups[i].acct_name);
1311 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1313 done:
1314 return result;
1317 static int rpc_group_list(int argc, const char **argv)
1319 return run_rpc_command(NULL, PI_SAMR, 0,
1320 rpc_group_list_internals,
1321 argc, argv);
1324 static NTSTATUS
1325 rpc_list_group_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1326 const char *domain_name, const DOM_SID *domain_sid,
1327 POLICY_HND *domain_pol, uint32 rid)
1329 NTSTATUS result;
1330 POLICY_HND group_pol;
1331 uint32 num_members, *group_rids, *group_attrs;
1332 uint32 num_names;
1333 char **names;
1334 uint32 *name_types;
1335 int i;
1337 fstring sid_str;
1338 sid_to_string(sid_str, domain_sid);
1340 result = cli_samr_open_group(cli, mem_ctx, domain_pol,
1341 MAXIMUM_ALLOWED_ACCESS,
1342 rid, &group_pol);
1344 if (!NT_STATUS_IS_OK(result))
1345 return result;
1347 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
1348 &num_members, &group_rids,
1349 &group_attrs);
1351 if (!NT_STATUS_IS_OK(result))
1352 return result;
1354 while (num_members > 0) {
1355 int this_time = 512;
1357 if (num_members < this_time)
1358 this_time = num_members;
1360 result = cli_samr_lookup_rids(cli, mem_ctx, domain_pol, 1000,
1361 this_time, group_rids,
1362 &num_names, &names, &name_types);
1364 if (!NT_STATUS_IS_OK(result))
1365 return result;
1367 /* We only have users as members, but make the output
1368 the same as the output of alias members */
1370 for (i = 0; i < this_time; i++) {
1372 if (opt_long_list_entries) {
1373 printf("%s-%d %s\\%s %d\n", sid_str,
1374 group_rids[i], domain_name, names[i],
1375 SID_NAME_USER);
1376 } else {
1377 printf("%s\\%s\n", domain_name, names[i]);
1381 num_members -= this_time;
1382 group_rids += 512;
1385 return NT_STATUS_OK;
1388 static NTSTATUS
1389 rpc_list_alias_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1390 POLICY_HND *domain_pol, uint32 rid)
1392 NTSTATUS result;
1393 POLICY_HND alias_pol, lsa_pol;
1394 uint32 num_members;
1395 DOM_SID *alias_sids;
1396 char **domains;
1397 char **names;
1398 uint32 *types;
1399 int i;
1401 result = cli_samr_open_alias(cli, mem_ctx, domain_pol,
1402 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
1404 if (!NT_STATUS_IS_OK(result))
1405 return result;
1407 result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
1408 &num_members, &alias_sids);
1410 if (!NT_STATUS_IS_OK(result)) {
1411 d_printf("Couldn't list alias members\n");
1412 return result;
1415 if (num_members == 0) {
1416 return NT_STATUS_OK;
1419 cli_nt_session_close(cli);
1421 if (!cli_nt_session_open(cli, PI_LSARPC)) {
1422 d_printf("Couldn't open LSA pipe\n");
1423 return result;
1426 result = cli_lsa_open_policy(cli, mem_ctx, True,
1427 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1429 if (!NT_STATUS_IS_OK(result)) {
1430 d_printf("Couldn't open LSA policy handle\n");
1431 return result;
1434 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, num_members,
1435 alias_sids,
1436 &domains, &names, &types);
1438 if (!NT_STATUS_IS_OK(result) &&
1439 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
1440 d_printf("Couldn't lookup SIDs\n");
1441 return result;
1444 for (i = 0; i < num_members; i++) {
1445 fstring sid_str;
1446 sid_to_string(sid_str, &alias_sids[i]);
1448 if (opt_long_list_entries) {
1449 printf("%s %s\\%s %d\n", sid_str,
1450 domains[i] ? domains[i] : "*unknown*",
1451 names[i] ? names[i] : "*unknown*", types[i]);
1452 } else {
1453 if (domains[i])
1454 printf("%s\\%s\n", domains[i], names[i]);
1455 else
1456 printf("%s\n", sid_str);
1460 return NT_STATUS_OK;
1463 static NTSTATUS
1464 rpc_group_members_internals(const DOM_SID *domain_sid,
1465 const char *domain_name,
1466 struct cli_state *cli,
1467 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1469 NTSTATUS result;
1470 POLICY_HND connect_pol, domain_pol;
1471 uint32 num_rids, *rids, *rid_types;
1473 /* Get sam policy handle */
1475 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1476 &connect_pol);
1478 if (!NT_STATUS_IS_OK(result))
1479 return result;
1481 /* Get domain policy handle */
1483 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1484 MAXIMUM_ALLOWED_ACCESS,
1485 domain_sid, &domain_pol);
1487 if (!NT_STATUS_IS_OK(result))
1488 return result;
1490 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1491 1, argv, &num_rids, &rids, &rid_types);
1493 if (!NT_STATUS_IS_OK(result)) {
1495 /* Ok, did not find it in the global sam, try with builtin */
1497 DOM_SID sid_Builtin;
1499 cli_samr_close(cli, mem_ctx, &domain_pol);
1501 string_to_sid(&sid_Builtin, "S-1-5-32");
1503 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1504 MAXIMUM_ALLOWED_ACCESS,
1505 &sid_Builtin, &domain_pol);
1507 if (!NT_STATUS_IS_OK(result)) {
1508 d_printf("Couldn't find group %s\n", argv[0]);
1509 return result;
1512 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1513 1, argv, &num_rids,
1514 &rids, &rid_types);
1516 if (!NT_STATUS_IS_OK(result)) {
1517 d_printf("Couldn't find group %s\n", argv[0]);
1518 return result;
1522 if (num_rids != 1) {
1523 d_printf("Couldn't find group %s\n", argv[0]);
1524 return result;
1527 if (rid_types[0] == SID_NAME_DOM_GRP) {
1528 return rpc_list_group_members(cli, mem_ctx, domain_name,
1529 domain_sid, &domain_pol,
1530 rids[0]);
1533 if (rid_types[0] == SID_NAME_ALIAS) {
1534 return rpc_list_alias_members(cli, mem_ctx, &domain_pol,
1535 rids[0]);
1538 return NT_STATUS_NO_SUCH_GROUP;
1541 static int rpc_group_members(int argc, const char **argv)
1543 if (argc != 1) {
1544 return rpc_group_usage(argc, argv);
1547 return run_rpc_command(NULL, PI_SAMR, 0,
1548 rpc_group_members_internals,
1549 argc, argv);
1552 /**
1553 * 'net rpc group' entrypoint.
1554 * @param argc Standard main() style argc
1555 * @param argc Standard main() style argv. Initial components are already
1556 * stripped
1559 int net_rpc_group(int argc, const char **argv)
1561 struct functable func[] = {
1562 {"add", rpc_group_add},
1563 #if 0
1564 {"delete", rpc_group_delete},
1565 #endif
1566 {"list", rpc_group_list},
1567 {"members", rpc_group_members},
1568 {NULL, NULL}
1571 if (argc == 0) {
1572 if (opt_long_list_entries) {
1573 } else {
1575 return run_rpc_command(NULL, PI_SAMR, 0,
1576 rpc_group_list_internals,
1577 argc, argv);
1580 return net_run_function(argc, argv, func, rpc_group_usage);
1583 /****************************************************************************/
1585 static int rpc_share_usage(int argc, const char **argv)
1587 return net_help_share(argc, argv);
1590 /**
1591 * Add a share on a remote RPC server
1593 * All parameters are provided by the run_rpc_command function, except for
1594 * argc, argv which are passes through.
1596 * @param domain_sid The domain sid acquired from the remote server
1597 * @param cli A cli_state connected to the server.
1598 * @param mem_ctx Talloc context, destoyed on completion of the function.
1599 * @param argc Standard main() style argc
1600 * @param argv Standard main() style argv. Initial components are already
1601 * stripped
1603 * @return Normal NTSTATUS return.
1605 static NTSTATUS
1606 rpc_share_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1607 struct cli_state *cli,
1608 TALLOC_CTX *mem_ctx,int argc, const char **argv)
1610 WERROR result;
1611 char *sharename=talloc_strdup(mem_ctx, argv[0]);
1612 char *path;
1613 uint32 type=0; /* only allow disk shares to be added */
1614 uint32 num_users=0, perms=0;
1615 char *password=NULL; /* don't allow a share password */
1617 path = strchr(sharename, '=');
1618 if (!path)
1619 return NT_STATUS_UNSUCCESSFUL;
1620 *path++ = '\0';
1622 result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
1623 opt_comment, perms, opt_maxusers,
1624 num_users, path, password);
1625 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1628 static int rpc_share_add(int argc, const char **argv)
1630 if ((argc < 1) || !strchr(argv[0], '=')) {
1631 DEBUG(1,("Sharename or path not specified on add\n"));
1632 return rpc_share_usage(argc, argv);
1634 return run_rpc_command(NULL, PI_SRVSVC, 0,
1635 rpc_share_add_internals,
1636 argc, argv);
1639 /**
1640 * Delete a share on a remote RPC server
1642 * All parameters are provided by the run_rpc_command function, except for
1643 * argc, argv which are passes through.
1645 * @param domain_sid The domain sid acquired from the remote server
1646 * @param cli A cli_state connected to the server.
1647 * @param mem_ctx Talloc context, destoyed on completion of the function.
1648 * @param argc Standard main() style argc
1649 * @param argv Standard main() style argv. Initial components are already
1650 * stripped
1652 * @return Normal NTSTATUS return.
1654 static NTSTATUS
1655 rpc_share_del_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 WERROR result;
1661 result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
1662 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1665 /**
1666 * Delete a share on a remote RPC server
1668 * @param domain_sid The domain sid acquired from the remote server
1669 * @param argc Standard main() style argc
1670 * @param argv Standard main() style argv. Initial components are already
1671 * stripped
1673 * @return A shell status integer (0 for success)
1675 static int rpc_share_delete(int argc, const char **argv)
1677 if (argc < 1) {
1678 DEBUG(1,("Sharename not specified on delete\n"));
1679 return rpc_share_usage(argc, argv);
1681 return run_rpc_command(NULL, PI_SRVSVC, 0,
1682 rpc_share_del_internals,
1683 argc, argv);
1687 * Formatted print of share info
1689 * @param info1 pointer to SRV_SHARE_INFO_1 to format
1692 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
1694 fstring netname = "", remark = "";
1696 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
1697 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
1699 if (opt_long_list_entries) {
1700 d_printf("%-12s %-8.8s %-50s\n",
1701 netname, share_type[info1->info_1.type], remark);
1702 } else {
1703 d_printf("%s\n", netname);
1708 /**
1709 * List shares on a remote RPC server
1711 * All parameters are provided by the run_rpc_command function, except for
1712 * argc, argv which are passes through.
1714 * @param domain_sid The domain sid acquired from the remote server
1715 * @param cli A cli_state connected to the server.
1716 * @param mem_ctx Talloc context, destoyed on completion of the function.
1717 * @param argc Standard main() style argc
1718 * @param argv Standard main() style argv. Initial components are already
1719 * stripped
1721 * @return Normal NTSTATUS return.
1724 static NTSTATUS
1725 rpc_share_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1726 struct cli_state *cli,
1727 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1729 SRV_SHARE_INFO_CTR ctr;
1730 WERROR result;
1731 ENUM_HND hnd;
1732 uint32 preferred_len = 0xffffffff, i;
1734 init_enum_hnd(&hnd, 0);
1736 result = cli_srvsvc_net_share_enum(
1737 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
1739 if (!W_ERROR_IS_OK(result))
1740 goto done;
1742 /* Display results */
1744 if (opt_long_list_entries) {
1745 d_printf(
1746 "\nEnumerating shared resources (exports) on remote server:\n\n"\
1747 "\nShare name Type Description\n"\
1748 "---------- ---- -----------\n");
1750 for (i = 0; i < ctr.num_entries; i++)
1751 display_share_info_1(&ctr.share.info1[i]);
1752 done:
1753 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1756 /**
1757 * 'net rpc share' entrypoint.
1758 * @param argc Standard main() style argc
1759 * @param argv Standard main() style argv. Initial components are already
1760 * stripped
1763 int net_rpc_share(int argc, const char **argv)
1765 struct functable func[] = {
1766 {"add", rpc_share_add},
1767 {"delete", rpc_share_delete},
1768 {NULL, NULL}
1771 if (argc == 0)
1772 return run_rpc_command(NULL, PI_SRVSVC, 0,
1773 rpc_share_list_internals,
1774 argc, argv);
1776 return net_run_function(argc, argv, func, rpc_share_usage);
1779 /****************************************************************************/
1781 static int rpc_file_usage(int argc, const char **argv)
1783 return net_help_file(argc, argv);
1786 /**
1787 * Close a file on a remote RPC server
1789 * All parameters are provided by the run_rpc_command function, except for
1790 * argc, argv which are passes through.
1792 * @param domain_sid The domain sid acquired from the remote server
1793 * @param cli A cli_state connected to the server.
1794 * @param mem_ctx Talloc context, destoyed on completion of the function.
1795 * @param argc Standard main() style argc
1796 * @param argv Standard main() style argv. Initial components are already
1797 * stripped
1799 * @return Normal NTSTATUS return.
1801 static NTSTATUS
1802 rpc_file_close_internals(const DOM_SID *domain_sid, const char *domain_name,
1803 struct cli_state *cli,
1804 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1806 WERROR result;
1807 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
1808 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1811 /**
1812 * Close a file on a remote RPC server
1814 * @param argc Standard main() style argc
1815 * @param argv Standard main() style argv. Initial components are already
1816 * stripped
1818 * @return A shell status integer (0 for success)
1820 static int rpc_file_close(int argc, const char **argv)
1822 if (argc < 1) {
1823 DEBUG(1, ("No fileid given on close\n"));
1824 return(rpc_file_usage(argc, argv));
1827 return run_rpc_command(NULL, PI_SRVSVC, 0,
1828 rpc_file_close_internals,
1829 argc, argv);
1832 /**
1833 * Formatted print of open file info
1835 * @param info3 FILE_INFO_3 contents
1836 * @param str3 strings for FILE_INFO_3
1839 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
1841 fstring user = "", path = "";
1843 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
1844 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
1846 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
1847 info3->id, user, info3->perms, info3->num_locks, path);
1850 /**
1851 * List open files on a remote RPC server
1853 * All parameters are provided by the run_rpc_command function, except for
1854 * argc, argv which are passes through.
1856 * @param domain_sid The domain sid acquired from the remote server
1857 * @param cli A cli_state connected to the server.
1858 * @param mem_ctx Talloc context, destoyed on completion of the function.
1859 * @param argc Standard main() style argc
1860 * @param argv Standard main() style argv. Initial components are already
1861 * stripped
1863 * @return Normal NTSTATUS return.
1866 static NTSTATUS
1867 rpc_file_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1868 struct cli_state *cli,
1869 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1871 SRV_FILE_INFO_CTR ctr;
1872 WERROR result;
1873 ENUM_HND hnd;
1874 uint32 preferred_len = 0xffffffff, i;
1875 const char *username=NULL;
1877 init_enum_hnd(&hnd, 0);
1879 /* if argc > 0, must be user command */
1880 if (argc > 0)
1881 username = smb_xstrdup(argv[0]);
1883 result = cli_srvsvc_net_file_enum(
1884 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
1886 if (!W_ERROR_IS_OK(result))
1887 goto done;
1889 /* Display results */
1891 d_printf(
1892 "\nEnumerating open files on remote server:\n\n"\
1893 "\nFileId Opened by Perms Locks Path"\
1894 "\n------ --------- ----- ----- ---- \n");
1895 for (i = 0; i < ctr.num_entries; i++)
1896 display_file_info_3(&ctr.file.info3[i].info_3,
1897 &ctr.file.info3[i].info_3_str);
1898 done:
1899 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
1903 /**
1904 * List files for a user on a remote RPC server
1906 * @param argc Standard main() style argc
1907 * @param argv Standard main() style argv. Initial components are already
1908 * stripped
1910 * @return A shell status integer (0 for success)
1912 static int rpc_file_user(int argc, const char **argv)
1914 if (argc < 1) {
1915 DEBUG(1, ("No username given\n"));
1916 return(rpc_file_usage(argc, argv));
1919 return run_rpc_command(NULL, PI_SRVSVC, 0,
1920 rpc_file_list_internals,
1921 argc, argv);
1925 /**
1926 * 'net rpc file' entrypoint.
1927 * @param argc Standard main() style argc
1928 * @param argv Standard main() style argv. Initial components are already
1929 * stripped
1932 int net_rpc_file(int argc, const char **argv)
1934 struct functable func[] = {
1935 {"close", rpc_file_close},
1936 {"user", rpc_file_user},
1937 #if 0
1938 {"info", rpc_file_info},
1939 #endif
1940 {NULL, NULL}
1943 if (argc == 0)
1944 return run_rpc_command(NULL, PI_SRVSVC, 0,
1945 rpc_file_list_internals,
1946 argc, argv);
1948 return net_run_function(argc, argv, func, rpc_file_usage);
1951 /****************************************************************************/
1955 /**
1956 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
1958 * All parameters are provided by the run_rpc_command function, except for
1959 * argc, argv which are passed through.
1961 * @param domain_sid The domain sid aquired from the remote server
1962 * @param cli A cli_state connected to the server.
1963 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1964 * @param argc Standard main() style argc
1965 * @param argv Standard main() style argv. Initial components are already
1966 * stripped
1968 * @return Normal NTSTATUS return.
1971 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
1972 const char *domain_name,
1973 struct cli_state *cli,
1974 TALLOC_CTX *mem_ctx,
1975 int argc, const char **argv)
1977 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1979 result = cli_shutdown_abort(cli, mem_ctx);
1981 if (NT_STATUS_IS_OK(result))
1982 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
1983 else
1984 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
1986 return result;
1990 /**
1991 * ABORT the shutdown of a remote RPC Server, over winreg pipe
1993 * All parameters are provided by the run_rpc_command function, except for
1994 * argc, argv which are passed through.
1996 * @param domain_sid The domain sid aquired from the remote server
1997 * @param cli A cli_state connected to the server.
1998 * @param mem_ctx Talloc context, destoyed on compleation of the function.
1999 * @param argc Standard main() style argc
2000 * @param argv Standard main() style argv. Initial components are already
2001 * stripped
2003 * @return Normal NTSTATUS return.
2006 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
2007 const char *domain_name,
2008 struct cli_state *cli,
2009 TALLOC_CTX *mem_ctx,
2010 int argc, const char **argv)
2012 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2014 result = cli_reg_abort_shutdown(cli, mem_ctx);
2016 if (NT_STATUS_IS_OK(result))
2017 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
2018 else
2019 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
2021 return result;
2024 /**
2025 * ABORT the Shut down of a remote RPC server
2027 * @param argc Standard main() style argc
2028 * @param argv Standard main() style argv. Initial components are already
2029 * stripped
2031 * @return A shell status integer (0 for success)
2034 static int rpc_shutdown_abort(int argc, const char **argv)
2036 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
2037 rpc_shutdown_abort_internals,
2038 argc, argv);
2040 if (rc == 0)
2041 return rc;
2043 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
2045 return run_rpc_command(NULL, PI_WINREG, 0,
2046 rpc_reg_shutdown_abort_internals,
2047 argc, argv);
2050 /**
2051 * Shut down a remote RPC Server
2053 * All parameters are provided by the run_rpc_command function, except for
2054 * argc, argv which are passes through.
2056 * @param domain_sid The domain sid aquired from the remote server
2057 * @param cli A cli_state connected to the server.
2058 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2059 * @param argc Standard main() style argc
2060 * @param argc Standard main() style argv. Initial components are already
2061 * stripped
2063 * @return Normal NTSTATUS return.
2066 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid,
2067 const char *domain_name,
2068 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2069 int argc, const char **argv)
2071 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2072 const char *msg = "This machine will be shutdown shortly";
2073 uint32 timeout = 20;
2074 #if 0
2075 poptContext pc;
2076 int rc;
2078 struct poptOption long_options[] = {
2079 {"message", 'm', POPT_ARG_STRING, &msg},
2080 {"timeout", 't', POPT_ARG_INT, &timeout},
2081 {"reboot", 'r', POPT_ARG_NONE, &reboot},
2082 {"force", 'f', POPT_ARG_NONE, &force},
2083 { 0, 0, 0, 0}
2086 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
2087 POPT_CONTEXT_KEEP_FIRST);
2089 rc = poptGetNextOpt(pc);
2091 if (rc < -1) {
2092 /* an error occurred during option processing */
2093 DEBUG(0, ("%s: %s\n",
2094 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
2095 poptStrerror(rc)));
2096 return NT_STATUS_INVALID_PARAMETER;
2098 #endif
2099 if (opt_comment) {
2100 msg = opt_comment;
2102 if (opt_timeout) {
2103 timeout = opt_timeout;
2106 /* create an entry */
2107 result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force);
2109 if (NT_STATUS_IS_OK(result))
2110 DEBUG(5,("Shutdown of remote machine succeeded\n"));
2111 else
2112 DEBUG(0,("Shutdown of remote machine failed!\n"));
2114 return result;
2117 /**
2118 * Shut down a remote RPC server
2120 * @param argc Standard main() style argc
2121 * @param argc Standard main() style argv. Initial components are already
2122 * stripped
2124 * @return A shell status integer (0 for success)
2127 static int rpc_shutdown(int argc, const char **argv)
2129 return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_internals,
2130 argc, argv);
2133 /***************************************************************************
2134 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
2136 ***************************************************************************/
2139 * Add interdomain trust account to the RPC server.
2140 * All parameters (except for argc and argv) are passed by run_rpc_command
2141 * function.
2143 * @param domain_sid The domain sid acquired from the server
2144 * @param cli A cli_state connected to the server.
2145 * @param mem_ctx Talloc context, destoyed on completion of the function.
2146 * @param argc Standard main() style argc
2147 * @param argc Standard main() style argv. Initial components are already
2148 * stripped
2150 * @return normal NTSTATUS return code
2153 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
2154 const char *domain_name,
2155 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2156 int argc, const char **argv) {
2158 POLICY_HND connect_pol, domain_pol, user_pol;
2159 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2160 char *acct_name;
2161 uint16 acb_info;
2162 uint32 unknown, user_rid;
2164 if (argc != 2) {
2165 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
2166 return NT_STATUS_INVALID_PARAMETER;
2170 * Make valid trusting domain account (ie. uppercased and with '$' appended)
2173 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
2174 return NT_STATUS_NO_MEMORY;
2177 strupper_m(acct_name);
2179 /* Get samr policy handle */
2180 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2181 &connect_pol);
2182 if (!NT_STATUS_IS_OK(result)) {
2183 goto done;
2186 /* Get domain policy handle */
2187 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2188 MAXIMUM_ALLOWED_ACCESS,
2189 domain_sid, &domain_pol);
2190 if (!NT_STATUS_IS_OK(result)) {
2191 goto done;
2194 /* Create trusting domain's account */
2195 acb_info = ACB_DOMTRUST;
2196 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
2197 mimir: yes, most probably it is */
2199 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
2200 acct_name, acb_info, unknown,
2201 &user_pol, &user_rid);
2202 if (!NT_STATUS_IS_OK(result)) {
2203 goto done;
2207 SAM_USERINFO_CTR ctr;
2208 SAM_USER_INFO_24 p24;
2209 uchar pwbuf[516];
2211 encode_pw_buffer((char *)pwbuf, argv[1], STR_UNICODE);
2213 ZERO_STRUCT(ctr);
2214 ZERO_STRUCT(p24);
2216 init_sam_user_info24(&p24, (char *)pwbuf, 24);
2218 ctr.switch_value = 24;
2219 ctr.info.id24 = &p24;
2221 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
2222 &cli->user_session_key, &ctr);
2224 if (!NT_STATUS_IS_OK(result)) {
2225 DEBUG(0,("Could not set trust account password: %s\n",
2226 nt_errstr(result)));
2227 goto done;
2231 done:
2232 SAFE_FREE(acct_name);
2233 return result;
2237 * Create interdomain trust account for a remote domain.
2239 * @param argc standard argc
2240 * @param argv standard argv without initial components
2242 * @return Integer status (0 means success)
2245 static int rpc_trustdom_add(int argc, const char **argv)
2247 if (argc > 0) {
2248 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
2249 argc, argv);
2250 } else {
2251 d_printf("Usage: net rpc trustdom add <domain>\n");
2252 return -1;
2258 * Delete interdomain trust account for a remote domain.
2260 * @param argc standard argc
2261 * @param argv standard argv without initial components
2263 * @return Integer status (0 means success)
2266 static int rpc_trustdom_del(int argc, const char **argv)
2268 d_printf("Sorry, not yet implemented.\n");
2269 d_printf("Use 'smbpasswd -x -i' instead.\n");
2270 return -1;
2275 * Establish trust relationship to a trusting domain.
2276 * Interdomain account must already be created on remote PDC.
2278 * @param argc standard argc
2279 * @param argv standard argv without initial components
2281 * @return Integer status (0 means success)
2284 static int rpc_trustdom_establish(int argc, const char **argv)
2286 struct cli_state *cli;
2287 struct in_addr server_ip;
2288 POLICY_HND connect_hnd;
2289 TALLOC_CTX *mem_ctx;
2290 NTSTATUS nt_status;
2291 DOM_SID *domain_sid;
2292 WKS_INFO_100 wks_info;
2294 char* domain_name;
2295 char* domain_name_pol;
2296 char* acct_name;
2297 fstring pdc_name;
2300 * Connect to \\server\ipc$ as 'our domain' account with password
2303 if (argc != 1) {
2304 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
2305 return -1;
2308 domain_name = smb_xstrdup(argv[0]);
2309 strupper_m(domain_name);
2311 /* account name used at first is our domain's name with '$' */
2312 asprintf(&acct_name, "%s$", lp_workgroup());
2313 strupper_m(acct_name);
2316 * opt_workgroup will be used by connection functions further,
2317 * hence it should be set to remote domain name instead of ours
2319 if (opt_workgroup) {
2320 opt_workgroup = smb_xstrdup(domain_name);
2323 opt_user_name = acct_name;
2325 /* find the domain controller */
2326 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
2327 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
2328 return -1;
2331 /* connect to ipc$ as username/password */
2332 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
2333 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
2335 /* Is it trusting domain account for sure ? */
2336 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
2337 nt_errstr(nt_status)));
2338 return -1;
2342 * Connect to \\server\ipc$ again (this time anonymously)
2345 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
2347 if (NT_STATUS_IS_ERR(nt_status)) {
2348 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
2349 domain_name, nt_errstr(nt_status)));
2353 * Use NetServerEnum2 to make sure we're talking to a proper server
2356 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
2357 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
2358 for domain %s\n", domain_name));
2362 * Call WksQueryInfo to check remote server's capabilities
2363 * note: It is now used only to get unicode domain name
2366 if (!cli_nt_session_open(cli, PI_WKSSVC)) {
2367 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
2368 return -1;
2371 if (!(mem_ctx = talloc_init("establishing trust relationship to domain %s",
2372 domain_name))) {
2373 DEBUG(0, ("talloc_init() failed\n"));
2374 cli_shutdown(cli);
2375 return -1;
2378 nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
2380 if (NT_STATUS_IS_ERR(nt_status)) {
2381 DEBUG(0, ("WksQueryInfo call failed.\n"));
2382 return -1;
2385 if (cli->nt_pipe_fnum)
2386 cli_nt_session_close(cli);
2390 * Call LsaOpenPolicy and LsaQueryInfo
2393 if (!(mem_ctx = talloc_init("rpc_trustdom_establish"))) {
2394 DEBUG(0, ("talloc_init() failed\n"));
2395 cli_shutdown(cli);
2396 return -1;
2399 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2400 DEBUG(0, ("Could not initialise lsa pipe\n"));
2401 cli_shutdown(cli);
2402 return -1;
2405 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
2406 &connect_hnd);
2407 if (NT_STATUS_IS_ERR(nt_status)) {
2408 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2409 nt_errstr(nt_status)));
2410 return -1;
2413 /* Querying info level 5 */
2415 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
2416 5 /* info level */, &domain_name_pol,
2417 &domain_sid);
2418 if (NT_STATUS_IS_ERR(nt_status)) {
2419 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2420 nt_errstr(nt_status)));
2421 return -1;
2427 /* There should be actually query info level 3 (following nt serv behaviour),
2428 but I still don't know if it's _really_ necessary */
2431 * Store the password in secrets db
2434 if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
2435 wks_info.uni_lan_grp.uni_str_len, opt_password,
2436 *domain_sid)) {
2437 DEBUG(0, ("Storing password for trusted domain failed.\n"));
2438 return -1;
2442 * Close the pipes and clean up
2445 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2446 if (NT_STATUS_IS_ERR(nt_status)) {
2447 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
2448 nt_errstr(nt_status)));
2449 return -1;
2452 if (cli->nt_pipe_fnum)
2453 cli_nt_session_close(cli);
2455 talloc_destroy(mem_ctx);
2457 DEBUG(0, ("Success!\n"));
2458 return 0;
2462 * Revoke trust relationship to the remote domain
2464 * @param argc standard argc
2465 * @param argv standard argv without initial components
2467 * @return Integer status (0 means success)
2470 static int rpc_trustdom_revoke(int argc, const char **argv)
2472 char* domain_name;
2474 if (argc < 1) return -1;
2476 /* generate upper cased domain name */
2477 domain_name = smb_xstrdup(argv[0]);
2478 strupper_m(domain_name);
2480 /* delete password of the trust */
2481 if (!trusted_domain_password_delete(domain_name)) {
2482 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
2483 domain_name));
2484 return -1;
2487 return 0;
2491 * Usage for 'net rpc trustdom' command
2493 * @param argc standard argc
2494 * @param argv standard argv without inital components
2496 * @return Integer status returned to shell
2499 static int rpc_trustdom_usage(int argc, const char **argv)
2501 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
2502 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
2503 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
2504 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
2505 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
2506 return -1;
2510 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
2511 const char *domain_name,
2512 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2513 int argc, const char **argv)
2515 fstring str_sid;
2516 sid_to_string(str_sid, domain_sid);
2517 d_printf("%s\n", str_sid);
2518 return NT_STATUS_OK;
2522 static int rpc_trustdom_list(int argc, const char **argv)
2524 /* common variables */
2525 TALLOC_CTX* mem_ctx;
2526 struct cli_state *cli, *remote_cli;
2527 NTSTATUS nt_status;
2528 const char *domain_name = NULL;
2529 DOM_SID *queried_dom_sid;
2530 fstring ascii_sid, padding;
2531 int ascii_dom_name_len;
2532 POLICY_HND connect_hnd;
2534 /* trusted domains listing variables */
2535 unsigned int num_domains, enum_ctx = 0;
2536 int i, pad_len, col_len = 20;
2537 DOM_SID *domain_sids;
2538 char **trusted_dom_names;
2539 fstring pdc_name;
2540 char *dummy;
2542 /* trusting domains listing variables */
2543 POLICY_HND domain_hnd;
2544 char **trusting_dom_names;
2545 uint32 *trusting_dom_rids;
2548 * Listing trusted domains (stored in secrets.tdb, if local)
2551 mem_ctx = talloc_init("trust relationships listing");
2554 * set domain and pdc name to local samba server (default)
2555 * or to remote one given in command line
2558 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
2559 domain_name = opt_workgroup;
2560 opt_target_workgroup = opt_workgroup;
2561 } else {
2562 fstrcpy(pdc_name, global_myname());
2563 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
2564 opt_target_workgroup = domain_name;
2567 /* open \PIPE\lsarpc and open policy handle */
2568 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
2569 DEBUG(0, ("Couldn't connect to domain controller\n"));
2570 return -1;
2573 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2574 DEBUG(0, ("Could not initialise lsa pipe\n"));
2575 return -1;
2578 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
2579 &connect_hnd);
2580 if (NT_STATUS_IS_ERR(nt_status)) {
2581 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2582 nt_errstr(nt_status)));
2583 return -1;
2586 /* query info level 5 to obtain sid of a domain being queried */
2587 nt_status = cli_lsa_query_info_policy(
2588 cli, mem_ctx, &connect_hnd, 5 /* info level */,
2589 &dummy, &queried_dom_sid);
2591 if (NT_STATUS_IS_ERR(nt_status)) {
2592 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2593 nt_errstr(nt_status)));
2594 return -1;
2598 * Keep calling LsaEnumTrustdom over opened pipe until
2599 * the end of enumeration is reached
2602 d_printf("Trusted domains list:\n\n");
2604 do {
2605 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
2606 &num_domains,
2607 &trusted_dom_names, &domain_sids);
2609 if (NT_STATUS_IS_ERR(nt_status)) {
2610 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
2611 nt_errstr(nt_status)));
2612 return -1;
2615 for (i = 0; i < num_domains; i++) {
2616 /* convert sid into ascii string */
2617 sid_to_string(ascii_sid, &(domain_sids[i]));
2619 /* calculate padding space for d_printf to look nicer */
2620 pad_len = col_len - strlen(trusted_dom_names[i]);
2621 padding[pad_len] = 0;
2622 do padding[--pad_len] = ' '; while (pad_len);
2624 d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
2628 * in case of no trusted domains say something rather
2629 * than just display blank line
2631 if (!num_domains) d_printf("none\n");
2633 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2635 /* close this connection before doing next one */
2636 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2637 if (NT_STATUS_IS_ERR(nt_status)) {
2638 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
2639 nt_errstr(nt_status)));
2640 return -1;
2643 cli_nt_session_close(cli);
2646 * Listing trusting domains (stored in passdb backend, if local)
2649 d_printf("\nTrusting domains list:\n\n");
2652 * Open \PIPE\samr and get needed policy handles
2654 if (!cli_nt_session_open(cli, PI_SAMR)) {
2655 DEBUG(0, ("Could not initialise samr pipe\n"));
2656 return -1;
2659 /* SamrConnect */
2660 nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
2661 &connect_hnd);
2662 if (!NT_STATUS_IS_OK(nt_status)) {
2663 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
2664 nt_errstr(nt_status)));
2665 return -1;
2668 /* SamrOpenDomain - we have to open domain policy handle in order to be
2669 able to enumerate accounts*/
2670 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
2671 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
2672 queried_dom_sid, &domain_hnd);
2673 if (!NT_STATUS_IS_OK(nt_status)) {
2674 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
2675 nt_errstr(nt_status)));
2676 return -1;
2680 * perform actual enumeration
2683 enum_ctx = 0; /* reset enumeration context from last enumeration */
2684 do {
2686 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
2687 &enum_ctx, ACB_DOMTRUST, 0xffff,
2688 &trusting_dom_names, &trusting_dom_rids,
2689 &num_domains);
2690 if (NT_STATUS_IS_ERR(nt_status)) {
2691 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
2692 nt_errstr(nt_status)));
2693 return -1;
2696 for (i = 0; i < num_domains; i++) {
2699 * get each single domain's sid (do we _really_ need this ?):
2700 * 1) connect to domain's pdc
2701 * 2) query the pdc for domain's sid
2704 /* get rid of '$' tail */
2705 ascii_dom_name_len = strlen(trusting_dom_names[i]);
2706 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
2707 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
2709 /* calculate padding space for d_printf to look nicer */
2710 pad_len = col_len - strlen(trusting_dom_names[i]);
2711 padding[pad_len] = 0;
2712 do padding[--pad_len] = ' '; while (pad_len);
2714 /* set opt_* variables to remote domain */
2715 strupper_m(trusting_dom_names[i]);
2716 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
2717 opt_target_workgroup = opt_workgroup;
2719 d_printf("%s%s", trusting_dom_names[i], padding);
2721 /* connect to remote domain controller */
2722 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
2723 if (remote_cli) {
2724 /* query for domain's sid */
2725 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
2726 d_printf("couldn't get domain's sid\n");
2728 cli_shutdown(remote_cli);
2730 } else {
2731 d_printf("domain controller is not responding\n");
2735 if (!num_domains) d_printf("none\n");
2737 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
2739 /* close opened samr and domain policy handles */
2740 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
2741 if (!NT_STATUS_IS_OK(nt_status)) {
2742 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
2745 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
2746 if (!NT_STATUS_IS_OK(nt_status)) {
2747 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
2750 /* close samr pipe and connection to IPC$ */
2751 cli_nt_session_close(cli);
2752 cli_shutdown(cli);
2754 talloc_destroy(mem_ctx);
2755 return 0;
2759 * Entrypoint for 'net rpc trustdom' code
2761 * @param argc standard argc
2762 * @param argv standard argv without initial components
2764 * @return Integer status (0 means success)
2767 static int rpc_trustdom(int argc, const char **argv)
2769 struct functable func[] = {
2770 {"add", rpc_trustdom_add},
2771 {"del", rpc_trustdom_del},
2772 {"establish", rpc_trustdom_establish},
2773 {"revoke", rpc_trustdom_revoke},
2774 {"help", rpc_trustdom_usage},
2775 {"list", rpc_trustdom_list},
2776 {NULL, NULL}
2779 if (argc == 0) {
2780 rpc_trustdom_usage(argc, argv);
2781 return -1;
2784 return (net_run_function(argc, argv, func, rpc_user_usage));
2788 * Check if a server will take rpc commands
2789 * @param flags Type of server to connect to (PDC, DMB, localhost)
2790 * if the host is not explicitly specified
2791 * @return BOOL (true means rpc supported)
2793 BOOL net_rpc_check(unsigned flags)
2795 struct cli_state cli;
2796 BOOL ret = False;
2797 struct in_addr server_ip;
2798 char *server_name = NULL;
2800 /* flags (i.e. server type) may depend on command */
2801 if (!net_find_server(flags, &server_ip, &server_name))
2802 return False;
2804 ZERO_STRUCT(cli);
2805 if (cli_initialise(&cli) == False)
2806 return False;
2808 if (!cli_connect(&cli, server_name, &server_ip))
2809 goto done;
2810 if (!attempt_netbios_session_request(&cli, global_myname(),
2811 server_name, &server_ip))
2812 goto done;
2813 if (!cli_negprot(&cli))
2814 goto done;
2815 if (cli.protocol < PROTOCOL_NT1)
2816 goto done;
2818 ret = True;
2819 done:
2820 cli_shutdown(&cli);
2821 return ret;
2824 /* dump sam database via samsync rpc calls */
2825 static int rpc_samdump(int argc, const char **argv) {
2826 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
2827 argc, argv);
2830 /* syncronise sam database via samsync rpc calls */
2831 static int rpc_vampire(int argc, const char **argv) {
2832 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
2833 argc, argv);
2835 /****************************************************************************/
2838 /**
2839 * Basic usage function for 'net rpc'
2840 * @param argc Standard main() style argc
2841 * @param argv Standard main() style argv. Initial components are already
2842 * stripped
2845 int net_rpc_usage(int argc, const char **argv)
2847 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
2848 d_printf(" net rpc join \t\t\tto join a domain \n");
2849 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n\n\n");
2850 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
2851 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
2852 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass");
2853 d_printf(" net rpc group \t\tto list groups\n");
2854 d_printf(" net rpc share \t\tto add, delete, and list shares\n");
2855 d_printf(" net rpc file \t\t\tto list open files\n");
2856 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
2857 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
2858 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
2859 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
2860 d_printf(" net rpc trustdom \t\tto create trusting domain's account\n"
2861 "\t\t\t\t\tor establish trust\n");
2862 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
2863 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
2864 d_printf("\n");
2865 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
2866 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
2867 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
2868 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
2869 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
2870 return -1;
2875 * Help function for 'net rpc'. Calls command specific help if requested
2876 * or displays usage of net rpc
2877 * @param argc Standard main() style argc
2878 * @param argv Standard main() style argv. Initial components are already
2879 * stripped
2882 int net_rpc_help(int argc, const char **argv)
2884 struct functable func[] = {
2885 {"join", rpc_join_usage},
2886 {"user", rpc_user_usage},
2887 {"group", rpc_group_usage},
2888 {"share", rpc_share_usage},
2889 /*{"changetrustpw", rpc_changetrustpw_usage}, */
2890 {"trustdom", rpc_trustdom_usage},
2891 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
2892 /*{"shutdown", rpc_shutdown_usage}, */
2893 {NULL, NULL}
2896 if (argc == 0) {
2897 net_rpc_usage(argc, argv);
2898 return -1;
2901 return (net_run_function(argc, argv, func, rpc_user_usage));
2905 /**
2906 * 'net rpc' entrypoint.
2907 * @param argc Standard main() style argc
2908 * @param argv Standard main() style argv. Initial components are already
2909 * stripped
2912 int net_rpc(int argc, const char **argv)
2914 struct functable func[] = {
2915 {"info", net_rpc_info},
2916 {"join", net_rpc_join},
2917 {"oldjoin", net_rpc_oldjoin},
2918 {"testjoin", net_rpc_testjoin},
2919 {"user", net_rpc_user},
2920 {"password", rpc_user_password},
2921 {"group", net_rpc_group},
2922 {"share", net_rpc_share},
2923 {"file", net_rpc_file},
2924 {"changetrustpw", net_rpc_changetrustpw},
2925 {"trustdom", rpc_trustdom},
2926 {"abortshutdown", rpc_shutdown_abort},
2927 {"shutdown", rpc_shutdown},
2928 {"samdump", rpc_samdump},
2929 {"vampire", rpc_vampire},
2930 {"getsid", net_rpc_getsid},
2931 {"help", net_rpc_help},
2932 {NULL, NULL}
2934 return net_run_function(argc, argv, func, net_rpc_usage);