Another typo, sorry for samba-cvs spam :-)
[Samba/gebeck_regimport.git] / source / utils / net_rpc.c
blob46835d080d2216c57750b97c9a61879ed5d9dba3
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 NTSTATUS
1102 rpc_alias_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1103 struct cli_state *cli,
1104 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1106 POLICY_HND connect_pol, domain_pol, alias_pol;
1107 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1108 ALIAS_INFO_CTR alias_info;
1110 if (argc != 1) {
1111 d_printf("Group name must be specified\n");
1112 rpc_group_usage(argc, argv);
1113 return NT_STATUS_OK;
1116 /* Get sam policy handle */
1118 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1119 &connect_pol);
1120 if (!NT_STATUS_IS_OK(result)) goto done;
1122 /* Get domain policy handle */
1124 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1125 MAXIMUM_ALLOWED_ACCESS,
1126 domain_sid, &domain_pol);
1127 if (!NT_STATUS_IS_OK(result)) goto done;
1129 /* Create the group */
1131 result = cli_samr_create_dom_alias(cli, mem_ctx, &domain_pol,
1132 argv[0], &alias_pol);
1133 if (!NT_STATUS_IS_OK(result)) goto done;
1135 if (strlen(opt_comment) == 0) goto done;
1137 /* We've got a comment to set */
1139 alias_info.switch_value1 = 3;
1140 alias_info.switch_value2 = 3;
1141 init_samr_alias_info3(&alias_info.alias.info3, opt_comment);
1143 result = cli_samr_set_aliasinfo(cli, mem_ctx, &alias_pol, &alias_info);
1144 if (!NT_STATUS_IS_OK(result)) goto done;
1146 done:
1147 if (NT_STATUS_IS_OK(result))
1148 DEBUG(5, ("add group succeeded\n"));
1149 else
1150 d_printf("add group failed: %s\n", nt_errstr(result));
1152 return result;
1155 static int rpc_group_add(int argc, const char **argv)
1157 if (opt_localgroup)
1158 return run_rpc_command(NULL, PI_SAMR, 0,
1159 rpc_alias_add_internals,
1160 argc, argv);
1162 return run_rpc_command(NULL, PI_SAMR, 0,
1163 rpc_group_add_internals,
1164 argc, argv);
1167 static NTSTATUS
1168 get_sid_from_name(struct cli_state *cli, TALLOC_CTX *mem_ctx, const char *name,
1169 DOM_SID *sid, enum SID_NAME_USE *type)
1171 int current_pipe = cli->pipe_idx;
1173 DOM_SID *sids = NULL;
1174 uint32 *types = NULL;
1175 POLICY_HND lsa_pol;
1176 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1178 if (current_pipe != PI_LSARPC) {
1180 if (current_pipe != -1)
1181 cli_nt_session_close(cli);
1183 if (!cli_nt_session_open(cli, PI_LSARPC))
1184 goto done;
1187 result = cli_lsa_open_policy(cli, mem_ctx, False,
1188 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1190 if (!NT_STATUS_IS_OK(result))
1191 goto done;
1193 result = cli_lsa_lookup_names(cli, mem_ctx, &lsa_pol, 1,
1194 &name, &sids, &types);
1196 if (NT_STATUS_IS_OK(result)) {
1197 sid_copy(sid, &sids[0]);
1198 *type = types[0];
1201 cli_lsa_close(cli, mem_ctx, &lsa_pol);
1203 done:
1204 if (current_pipe != PI_LSARPC) {
1205 cli_nt_session_close(cli);
1206 if (current_pipe != -1)
1207 cli_nt_session_open(cli, current_pipe);
1210 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1212 /* Try as S-1-5-whatever */
1214 DOM_SID tmp_sid;
1216 if (string_to_sid(&tmp_sid, name)) {
1217 sid_copy(sid, &tmp_sid);
1218 *type = SID_NAME_UNKNOWN;
1219 result = NT_STATUS_OK;
1223 return result;
1226 static NTSTATUS
1227 rpc_add_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1228 const DOM_SID *group_sid, const char *member)
1230 POLICY_HND connect_pol, domain_pol;
1231 NTSTATUS result;
1232 uint32 group_rid;
1233 POLICY_HND group_pol;
1235 uint32 num_rids;
1236 uint32 *rids = NULL;
1237 uint32 *rid_types = NULL;
1239 DOM_SID sid;
1241 sid_copy(&sid, group_sid);
1243 if (!sid_split_rid(&sid, &group_rid))
1244 return NT_STATUS_UNSUCCESSFUL;
1246 /* Get sam policy handle */
1247 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1248 &connect_pol);
1249 if (!NT_STATUS_IS_OK(result))
1250 return result;
1252 /* Get domain policy handle */
1253 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1254 MAXIMUM_ALLOWED_ACCESS,
1255 &sid, &domain_pol);
1256 if (!NT_STATUS_IS_OK(result))
1257 return result;
1259 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1260 1, &member,
1261 &num_rids, &rids, &rid_types);
1263 if (!NT_STATUS_IS_OK(result)) {
1264 d_printf("Could not lookup up group member %s\n", member);
1265 goto done;
1268 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1269 MAXIMUM_ALLOWED_ACCESS,
1270 group_rid, &group_pol);
1272 if (!NT_STATUS_IS_OK(result))
1273 goto done;
1275 result = cli_samr_add_groupmem(cli, mem_ctx, &group_pol, rids[0]);
1277 done:
1278 cli_samr_close(cli, mem_ctx, &connect_pol);
1279 return result;
1282 static NTSTATUS
1283 rpc_add_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1284 const DOM_SID *alias_sid, const char *member)
1286 POLICY_HND connect_pol, domain_pol;
1287 NTSTATUS result;
1288 uint32 alias_rid;
1289 POLICY_HND alias_pol;
1291 DOM_SID member_sid;
1292 enum SID_NAME_USE member_type;
1294 DOM_SID sid;
1296 sid_copy(&sid, alias_sid);
1298 if (!sid_split_rid(&sid, &alias_rid))
1299 return NT_STATUS_UNSUCCESSFUL;
1301 result = get_sid_from_name(cli, mem_ctx, member,
1302 &member_sid, &member_type);
1304 if (!NT_STATUS_IS_OK(result)) {
1305 d_printf("Could not lookup up group member %s\n", member);
1306 return result;
1309 /* Get sam policy handle */
1310 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1311 &connect_pol);
1312 if (!NT_STATUS_IS_OK(result)) {
1313 goto done;
1316 /* Get domain policy handle */
1317 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1318 MAXIMUM_ALLOWED_ACCESS,
1319 &sid, &domain_pol);
1320 if (!NT_STATUS_IS_OK(result)) {
1321 goto done;
1324 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1325 MAXIMUM_ALLOWED_ACCESS,
1326 alias_rid, &alias_pol);
1328 if (!NT_STATUS_IS_OK(result))
1329 return result;
1331 result = cli_samr_add_aliasmem(cli, mem_ctx, &alias_pol, &member_sid);
1333 if (!NT_STATUS_IS_OK(result))
1334 return result;
1336 done:
1337 cli_samr_close(cli, mem_ctx, &connect_pol);
1338 return result;
1341 static NTSTATUS
1342 rpc_group_addmem_internals(const DOM_SID *domain_sid, const char *domain_name,
1343 struct cli_state *cli,
1344 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1346 DOM_SID group_sid;
1347 enum SID_NAME_USE group_type;
1349 if (argc != 2) {
1350 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
1351 return NT_STATUS_UNSUCCESSFUL;
1354 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1355 &group_sid, &group_type))) {
1356 d_printf("Could not lookup group name %s\n", argv[0]);
1357 return NT_STATUS_UNSUCCESSFUL;
1360 if (group_type == SID_NAME_DOM_GRP) {
1361 NTSTATUS result = rpc_add_groupmem(cli, mem_ctx,
1362 &group_sid, argv[1]);
1364 if (!NT_STATUS_IS_OK(result)) {
1365 d_printf("Could not add %s to %s: %s\n",
1366 argv[1], argv[0], nt_errstr(result));
1368 return result;
1371 if (group_type == SID_NAME_ALIAS) {
1372 NTSTATUS result = rpc_add_aliasmem(cli, mem_ctx,
1373 &group_sid, argv[1]);
1375 if (!NT_STATUS_IS_OK(result)) {
1376 d_printf("Could not add %s to %s: %s\n",
1377 argv[1], argv[0], nt_errstr(result));
1379 return result;
1382 d_printf("Can only add members to global or local groups which "
1383 "%s is not\n", argv[0]);
1385 return NT_STATUS_UNSUCCESSFUL;
1388 static int rpc_group_addmem(int argc, const char **argv)
1390 return run_rpc_command(NULL, PI_SAMR, 0,
1391 rpc_group_addmem_internals,
1392 argc, argv);
1395 static NTSTATUS
1396 rpc_del_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1397 const DOM_SID *group_sid, const char *member)
1399 POLICY_HND connect_pol, domain_pol;
1400 NTSTATUS result;
1401 uint32 group_rid;
1402 POLICY_HND group_pol;
1404 uint32 num_rids;
1405 uint32 *rids = NULL;
1406 uint32 *rid_types = NULL;
1408 DOM_SID sid;
1410 sid_copy(&sid, group_sid);
1412 if (!sid_split_rid(&sid, &group_rid))
1413 return NT_STATUS_UNSUCCESSFUL;
1415 /* Get sam policy handle */
1416 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1417 &connect_pol);
1418 if (!NT_STATUS_IS_OK(result))
1419 return result;
1421 /* Get domain policy handle */
1422 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1423 MAXIMUM_ALLOWED_ACCESS,
1424 &sid, &domain_pol);
1425 if (!NT_STATUS_IS_OK(result))
1426 return result;
1428 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1429 1, &member,
1430 &num_rids, &rids, &rid_types);
1432 if (!NT_STATUS_IS_OK(result)) {
1433 d_printf("Could not lookup up group member %s\n", member);
1434 goto done;
1437 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1438 MAXIMUM_ALLOWED_ACCESS,
1439 group_rid, &group_pol);
1441 if (!NT_STATUS_IS_OK(result))
1442 goto done;
1444 result = cli_samr_del_groupmem(cli, mem_ctx, &group_pol, rids[0]);
1446 done:
1447 cli_samr_close(cli, mem_ctx, &connect_pol);
1448 return result;
1451 static NTSTATUS
1452 rpc_del_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1453 const DOM_SID *alias_sid, const char *member)
1455 POLICY_HND connect_pol, domain_pol;
1456 NTSTATUS result;
1457 uint32 alias_rid;
1458 POLICY_HND alias_pol;
1460 DOM_SID member_sid;
1461 enum SID_NAME_USE member_type;
1463 DOM_SID sid;
1465 sid_copy(&sid, alias_sid);
1467 if (!sid_split_rid(&sid, &alias_rid))
1468 return NT_STATUS_UNSUCCESSFUL;
1470 result = get_sid_from_name(cli, mem_ctx, member,
1471 &member_sid, &member_type);
1473 if (!NT_STATUS_IS_OK(result)) {
1474 d_printf("Could not lookup up group member %s\n", member);
1475 return result;
1478 /* Get sam policy handle */
1479 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1480 &connect_pol);
1481 if (!NT_STATUS_IS_OK(result)) {
1482 goto done;
1485 /* Get domain policy handle */
1486 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1487 MAXIMUM_ALLOWED_ACCESS,
1488 &sid, &domain_pol);
1489 if (!NT_STATUS_IS_OK(result)) {
1490 goto done;
1493 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1494 MAXIMUM_ALLOWED_ACCESS,
1495 alias_rid, &alias_pol);
1497 if (!NT_STATUS_IS_OK(result))
1498 return result;
1500 result = cli_samr_del_aliasmem(cli, mem_ctx, &alias_pol, &member_sid);
1502 if (!NT_STATUS_IS_OK(result))
1503 return result;
1505 done:
1506 cli_samr_close(cli, mem_ctx, &connect_pol);
1507 return result;
1510 static NTSTATUS
1511 rpc_group_delmem_internals(const DOM_SID *domain_sid, const char *domain_name,
1512 struct cli_state *cli,
1513 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1515 DOM_SID group_sid;
1516 enum SID_NAME_USE group_type;
1518 if (argc != 2) {
1519 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
1520 return NT_STATUS_UNSUCCESSFUL;
1523 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1524 &group_sid, &group_type))) {
1525 d_printf("Could not lookup group name %s\n", argv[0]);
1526 return NT_STATUS_UNSUCCESSFUL;
1529 if (group_type == SID_NAME_DOM_GRP) {
1530 NTSTATUS result = rpc_del_groupmem(cli, mem_ctx,
1531 &group_sid, argv[1]);
1533 if (!NT_STATUS_IS_OK(result)) {
1534 d_printf("Could not del %s from %s: %s\n",
1535 argv[1], argv[0], nt_errstr(result));
1537 return result;
1540 if (group_type == SID_NAME_ALIAS) {
1541 NTSTATUS result = rpc_del_aliasmem(cli, mem_ctx,
1542 &group_sid, argv[1]);
1544 if (!NT_STATUS_IS_OK(result)) {
1545 d_printf("Could not del %s from %s: %s\n",
1546 argv[1], argv[0], nt_errstr(result));
1548 return result;
1551 d_printf("Can only delete members from global or local groups which "
1552 "%s is not\n", argv[0]);
1554 return NT_STATUS_UNSUCCESSFUL;
1557 static int rpc_group_delmem(int argc, const char **argv)
1559 return run_rpc_command(NULL, PI_SAMR, 0,
1560 rpc_group_delmem_internals,
1561 argc, argv);
1564 /**
1565 * List groups on a remote RPC server
1567 * All parameters are provided by the run_rpc_command function, except for
1568 * argc, argv which are passes through.
1570 * @param domain_sid The domain sid acquired from the remote server
1571 * @param cli A cli_state connected to the server.
1572 * @param mem_ctx Talloc context, destoyed on completion of the function.
1573 * @param argc Standard main() style argc
1574 * @param argv Standard main() style argv. Initial components are already
1575 * stripped
1577 * @return Normal NTSTATUS return.
1580 static NTSTATUS
1581 rpc_group_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1582 struct cli_state *cli,
1583 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1585 POLICY_HND connect_pol, domain_pol;
1586 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1587 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
1588 struct acct_info *groups;
1589 DOM_SID global_sid_Builtin;
1590 BOOL global = False;
1591 BOOL local = False;
1592 BOOL builtin = False;
1594 if (argc == 0) {
1595 global = True;
1596 local = True;
1597 builtin = True;
1600 for (i=0; i<argc; i++) {
1601 if (strequal(argv[i], "global"))
1602 global = True;
1604 if (strequal(argv[i], "local"))
1605 local = True;
1607 if (strequal(argv[i], "builtin"))
1608 builtin = True;
1611 string_to_sid(&global_sid_Builtin, "S-1-5-32");
1613 /* Get sam policy handle */
1615 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1616 &connect_pol);
1617 if (!NT_STATUS_IS_OK(result)) {
1618 goto done;
1621 /* Get domain policy handle */
1623 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1624 MAXIMUM_ALLOWED_ACCESS,
1625 domain_sid, &domain_pol);
1626 if (!NT_STATUS_IS_OK(result)) {
1627 goto done;
1630 /* Query domain groups */
1631 if (opt_long_list_entries)
1632 d_printf("\nGroup name Comment"\
1633 "\n-----------------------------\n");
1634 do {
1635 SAM_DISPINFO_CTR ctr;
1636 SAM_DISPINFO_3 info3;
1637 uint32 max_size;
1639 ZERO_STRUCT(ctr);
1640 ZERO_STRUCT(info3);
1641 ctr.sam.info3 = &info3;
1643 if (!global) break;
1645 get_query_dispinfo_params(
1646 loop_count, &max_entries, &max_size);
1648 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1649 &start_idx, 3, &num_entries,
1650 max_entries, max_size, &ctr);
1652 if (!NT_STATUS_IS_OK(result) &&
1653 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1654 break;
1656 for (i = 0; i < num_entries; i++) {
1658 fstring group, desc;
1660 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
1661 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
1663 if (opt_long_list_entries)
1664 printf("%-21.21s %-50.50s\n",
1665 group, desc);
1666 else
1667 printf("%s\n", group);
1669 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1670 /* query domain aliases */
1671 start_idx = 0;
1672 do {
1673 if (!local) break;
1675 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1676 &start_idx, max_entries,
1677 &groups, &num_entries);
1679 if (!NT_STATUS_IS_OK(result) &&
1680 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1681 break;
1683 for (i = 0; i < num_entries; i++) {
1685 char *description = NULL;
1687 if (opt_long_list_entries) {
1689 POLICY_HND alias_pol;
1690 ALIAS_INFO_CTR ctr;
1692 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1693 &domain_pol,
1694 0x8,
1695 groups[i].rid,
1696 &alias_pol))) &&
1697 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1698 &alias_pol, 3,
1699 &ctr))) &&
1700 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1701 &alias_pol)))) {
1702 description = unistr2_tdup(mem_ctx,
1703 &ctr.alias.info3.uni_acct_desc);
1707 if (description != NULL) {
1708 printf("%-21.21s %-50.50s\n",
1709 groups[i].acct_name,
1710 description);
1711 } else {
1712 printf("%s\n", groups[i].acct_name);
1715 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1716 cli_samr_close(cli, mem_ctx, &domain_pol);
1717 /* Get builtin policy handle */
1719 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1720 MAXIMUM_ALLOWED_ACCESS,
1721 &global_sid_Builtin, &domain_pol);
1722 if (!NT_STATUS_IS_OK(result)) {
1723 goto done;
1725 /* query builtin aliases */
1726 start_idx = 0;
1727 do {
1728 if (!builtin) break;
1730 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
1731 &start_idx, max_entries,
1732 &groups, &num_entries);
1734 if (!NT_STATUS_IS_OK(result) &&
1735 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1736 break;
1738 for (i = 0; i < num_entries; i++) {
1740 char *description = NULL;
1742 if (opt_long_list_entries) {
1744 POLICY_HND alias_pol;
1745 ALIAS_INFO_CTR ctr;
1747 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
1748 &domain_pol,
1749 0x8,
1750 groups[i].rid,
1751 &alias_pol))) &&
1752 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
1753 &alias_pol, 3,
1754 &ctr))) &&
1755 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
1756 &alias_pol)))) {
1757 description = unistr2_tdup(mem_ctx,
1758 &ctr.alias.info3.uni_acct_desc);
1762 if (description != NULL) {
1763 printf("%-21.21s %-50.50s\n",
1764 groups[i].acct_name,
1765 description);
1766 } else {
1767 printf("%s\n", groups[i].acct_name);
1770 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1772 done:
1773 return result;
1776 static int rpc_group_list(int argc, const char **argv)
1778 return run_rpc_command(NULL, PI_SAMR, 0,
1779 rpc_group_list_internals,
1780 argc, argv);
1783 static NTSTATUS
1784 rpc_list_group_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1785 const char *domain_name, const DOM_SID *domain_sid,
1786 POLICY_HND *domain_pol, uint32 rid)
1788 NTSTATUS result;
1789 POLICY_HND group_pol;
1790 uint32 num_members, *group_rids, *group_attrs;
1791 uint32 num_names;
1792 char **names;
1793 uint32 *name_types;
1794 int i;
1796 fstring sid_str;
1797 sid_to_string(sid_str, domain_sid);
1799 result = cli_samr_open_group(cli, mem_ctx, domain_pol,
1800 MAXIMUM_ALLOWED_ACCESS,
1801 rid, &group_pol);
1803 if (!NT_STATUS_IS_OK(result))
1804 return result;
1806 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
1807 &num_members, &group_rids,
1808 &group_attrs);
1810 if (!NT_STATUS_IS_OK(result))
1811 return result;
1813 while (num_members > 0) {
1814 int this_time = 512;
1816 if (num_members < this_time)
1817 this_time = num_members;
1819 result = cli_samr_lookup_rids(cli, mem_ctx, domain_pol, 1000,
1820 this_time, group_rids,
1821 &num_names, &names, &name_types);
1823 if (!NT_STATUS_IS_OK(result))
1824 return result;
1826 /* We only have users as members, but make the output
1827 the same as the output of alias members */
1829 for (i = 0; i < this_time; i++) {
1831 if (opt_long_list_entries) {
1832 printf("%s-%d %s\\%s %d\n", sid_str,
1833 group_rids[i], domain_name, names[i],
1834 SID_NAME_USER);
1835 } else {
1836 printf("%s\\%s\n", domain_name, names[i]);
1840 num_members -= this_time;
1841 group_rids += 512;
1844 return NT_STATUS_OK;
1847 static NTSTATUS
1848 rpc_list_alias_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1849 POLICY_HND *domain_pol, uint32 rid)
1851 NTSTATUS result;
1852 POLICY_HND alias_pol, lsa_pol;
1853 uint32 num_members;
1854 DOM_SID *alias_sids;
1855 char **domains;
1856 char **names;
1857 uint32 *types;
1858 int i;
1860 result = cli_samr_open_alias(cli, mem_ctx, domain_pol,
1861 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
1863 if (!NT_STATUS_IS_OK(result))
1864 return result;
1866 result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
1867 &num_members, &alias_sids);
1869 if (!NT_STATUS_IS_OK(result)) {
1870 d_printf("Couldn't list alias members\n");
1871 return result;
1874 if (num_members == 0) {
1875 return NT_STATUS_OK;
1878 cli_nt_session_close(cli);
1880 if (!cli_nt_session_open(cli, PI_LSARPC)) {
1881 d_printf("Couldn't open LSA pipe\n");
1882 return result;
1885 result = cli_lsa_open_policy(cli, mem_ctx, True,
1886 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1888 if (!NT_STATUS_IS_OK(result)) {
1889 d_printf("Couldn't open LSA policy handle\n");
1890 return result;
1893 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, num_members,
1894 alias_sids,
1895 &domains, &names, &types);
1897 if (!NT_STATUS_IS_OK(result) &&
1898 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
1899 d_printf("Couldn't lookup SIDs\n");
1900 return result;
1903 for (i = 0; i < num_members; i++) {
1904 fstring sid_str;
1905 sid_to_string(sid_str, &alias_sids[i]);
1907 if (opt_long_list_entries) {
1908 printf("%s %s\\%s %d\n", sid_str,
1909 domains[i] ? domains[i] : "*unknown*",
1910 names[i] ? names[i] : "*unknown*", types[i]);
1911 } else {
1912 if (domains[i])
1913 printf("%s\\%s\n", domains[i], names[i]);
1914 else
1915 printf("%s\n", sid_str);
1919 return NT_STATUS_OK;
1922 static NTSTATUS
1923 rpc_group_members_internals(const DOM_SID *domain_sid,
1924 const char *domain_name,
1925 struct cli_state *cli,
1926 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1928 NTSTATUS result;
1929 POLICY_HND connect_pol, domain_pol;
1930 uint32 num_rids, *rids, *rid_types;
1932 /* Get sam policy handle */
1934 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1935 &connect_pol);
1937 if (!NT_STATUS_IS_OK(result))
1938 return result;
1940 /* Get domain policy handle */
1942 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1943 MAXIMUM_ALLOWED_ACCESS,
1944 domain_sid, &domain_pol);
1946 if (!NT_STATUS_IS_OK(result))
1947 return result;
1949 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1950 1, argv, &num_rids, &rids, &rid_types);
1952 if (!NT_STATUS_IS_OK(result)) {
1954 /* Ok, did not find it in the global sam, try with builtin */
1956 DOM_SID sid_Builtin;
1958 cli_samr_close(cli, mem_ctx, &domain_pol);
1960 string_to_sid(&sid_Builtin, "S-1-5-32");
1962 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1963 MAXIMUM_ALLOWED_ACCESS,
1964 &sid_Builtin, &domain_pol);
1966 if (!NT_STATUS_IS_OK(result)) {
1967 d_printf("Couldn't find group %s\n", argv[0]);
1968 return result;
1971 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1972 1, argv, &num_rids,
1973 &rids, &rid_types);
1975 if (!NT_STATUS_IS_OK(result)) {
1976 d_printf("Couldn't find group %s\n", argv[0]);
1977 return result;
1981 if (num_rids != 1) {
1982 d_printf("Couldn't find group %s\n", argv[0]);
1983 return result;
1986 if (rid_types[0] == SID_NAME_DOM_GRP) {
1987 return rpc_list_group_members(cli, mem_ctx, domain_name,
1988 domain_sid, &domain_pol,
1989 rids[0]);
1992 if (rid_types[0] == SID_NAME_ALIAS) {
1993 return rpc_list_alias_members(cli, mem_ctx, &domain_pol,
1994 rids[0]);
1997 return NT_STATUS_NO_SUCH_GROUP;
2000 static int rpc_group_members(int argc, const char **argv)
2002 if (argc != 1) {
2003 return rpc_group_usage(argc, argv);
2006 return run_rpc_command(NULL, PI_SAMR, 0,
2007 rpc_group_members_internals,
2008 argc, argv);
2011 /**
2012 * 'net rpc group' entrypoint.
2013 * @param argc Standard main() style argc
2014 * @param argc Standard main() style argv. Initial components are already
2015 * stripped
2018 int net_rpc_group(int argc, const char **argv)
2020 struct functable func[] = {
2021 {"add", rpc_group_add},
2022 {"addmem", rpc_group_addmem},
2023 {"delmem", rpc_group_delmem},
2024 #if 0
2025 {"delete", rpc_group_delete},
2026 #endif
2027 {"list", rpc_group_list},
2028 {"members", rpc_group_members},
2029 {NULL, NULL}
2032 if (argc == 0) {
2033 if (opt_long_list_entries) {
2034 } else {
2036 return run_rpc_command(NULL, PI_SAMR, 0,
2037 rpc_group_list_internals,
2038 argc, argv);
2041 return net_run_function(argc, argv, func, rpc_group_usage);
2044 /****************************************************************************/
2046 static int rpc_share_usage(int argc, const char **argv)
2048 return net_help_share(argc, argv);
2051 /**
2052 * Add a share on a remote RPC server
2054 * All parameters are provided by the run_rpc_command function, except for
2055 * argc, argv which are passes through.
2057 * @param domain_sid The domain sid acquired from the remote server
2058 * @param cli A cli_state connected to the server.
2059 * @param mem_ctx Talloc context, destoyed on completion of the function.
2060 * @param argc Standard main() style argc
2061 * @param argv Standard main() style argv. Initial components are already
2062 * stripped
2064 * @return Normal NTSTATUS return.
2066 static NTSTATUS
2067 rpc_share_add_internals(const DOM_SID *domain_sid, const char *domain_name,
2068 struct cli_state *cli,
2069 TALLOC_CTX *mem_ctx,int argc, const char **argv)
2071 WERROR result;
2072 char *sharename=talloc_strdup(mem_ctx, argv[0]);
2073 char *path;
2074 uint32 type=0; /* only allow disk shares to be added */
2075 uint32 num_users=0, perms=0;
2076 char *password=NULL; /* don't allow a share password */
2078 path = strchr(sharename, '=');
2079 if (!path)
2080 return NT_STATUS_UNSUCCESSFUL;
2081 *path++ = '\0';
2083 result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
2084 opt_comment, perms, opt_maxusers,
2085 num_users, path, password);
2086 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2089 static int rpc_share_add(int argc, const char **argv)
2091 if ((argc < 1) || !strchr(argv[0], '=')) {
2092 DEBUG(1,("Sharename or path not specified on add\n"));
2093 return rpc_share_usage(argc, argv);
2095 return run_rpc_command(NULL, PI_SRVSVC, 0,
2096 rpc_share_add_internals,
2097 argc, argv);
2100 /**
2101 * Delete a share on a remote RPC server
2103 * All parameters are provided by the run_rpc_command function, except for
2104 * argc, argv which are passes through.
2106 * @param domain_sid The domain sid acquired from the remote server
2107 * @param cli A cli_state connected to the server.
2108 * @param mem_ctx Talloc context, destoyed on completion of the function.
2109 * @param argc Standard main() style argc
2110 * @param argv Standard main() style argv. Initial components are already
2111 * stripped
2113 * @return Normal NTSTATUS return.
2115 static NTSTATUS
2116 rpc_share_del_internals(const DOM_SID *domain_sid, const char *domain_name,
2117 struct cli_state *cli,
2118 TALLOC_CTX *mem_ctx,int argc, const char **argv)
2120 WERROR result;
2122 result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
2123 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2126 /**
2127 * Delete a share on a remote RPC server
2129 * @param domain_sid The domain sid acquired from the remote server
2130 * @param argc Standard main() style argc
2131 * @param argv Standard main() style argv. Initial components are already
2132 * stripped
2134 * @return A shell status integer (0 for success)
2136 static int rpc_share_delete(int argc, const char **argv)
2138 if (argc < 1) {
2139 DEBUG(1,("Sharename not specified on delete\n"));
2140 return rpc_share_usage(argc, argv);
2142 return run_rpc_command(NULL, PI_SRVSVC, 0,
2143 rpc_share_del_internals,
2144 argc, argv);
2148 * Formatted print of share info
2150 * @param info1 pointer to SRV_SHARE_INFO_1 to format
2153 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
2155 fstring netname = "", remark = "";
2157 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
2158 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
2160 if (opt_long_list_entries) {
2161 d_printf("%-12s %-8.8s %-50s\n",
2162 netname, share_type[info1->info_1.type], remark);
2163 } else {
2164 d_printf("%s\n", netname);
2169 /**
2170 * List shares on a remote RPC server
2172 * All parameters are provided by the run_rpc_command function, except for
2173 * argc, argv which are passes through.
2175 * @param domain_sid The domain sid acquired from the remote server
2176 * @param cli A cli_state connected to the server.
2177 * @param mem_ctx Talloc context, destoyed on completion of the function.
2178 * @param argc Standard main() style argc
2179 * @param argv Standard main() style argv. Initial components are already
2180 * stripped
2182 * @return Normal NTSTATUS return.
2185 static NTSTATUS
2186 rpc_share_list_internals(const DOM_SID *domain_sid, const char *domain_name,
2187 struct cli_state *cli,
2188 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2190 SRV_SHARE_INFO_CTR ctr;
2191 WERROR result;
2192 ENUM_HND hnd;
2193 uint32 preferred_len = 0xffffffff, i;
2195 init_enum_hnd(&hnd, 0);
2197 result = cli_srvsvc_net_share_enum(
2198 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
2200 if (!W_ERROR_IS_OK(result))
2201 goto done;
2203 /* Display results */
2205 if (opt_long_list_entries) {
2206 d_printf(
2207 "\nEnumerating shared resources (exports) on remote server:\n\n"\
2208 "\nShare name Type Description\n"\
2209 "---------- ---- -----------\n");
2211 for (i = 0; i < ctr.num_entries; i++)
2212 display_share_info_1(&ctr.share.info1[i]);
2213 done:
2214 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2217 /**
2218 * 'net rpc share' entrypoint.
2219 * @param argc Standard main() style argc
2220 * @param argv Standard main() style argv. Initial components are already
2221 * stripped
2224 int net_rpc_share(int argc, const char **argv)
2226 struct functable func[] = {
2227 {"add", rpc_share_add},
2228 {"delete", rpc_share_delete},
2229 {NULL, NULL}
2232 if (argc == 0)
2233 return run_rpc_command(NULL, PI_SRVSVC, 0,
2234 rpc_share_list_internals,
2235 argc, argv);
2237 return net_run_function(argc, argv, func, rpc_share_usage);
2240 /****************************************************************************/
2242 static int rpc_file_usage(int argc, const char **argv)
2244 return net_help_file(argc, argv);
2247 /**
2248 * Close a file on a remote RPC server
2250 * All parameters are provided by the run_rpc_command function, except for
2251 * argc, argv which are passes through.
2253 * @param domain_sid The domain sid acquired from the remote server
2254 * @param cli A cli_state connected to the server.
2255 * @param mem_ctx Talloc context, destoyed on completion of the function.
2256 * @param argc Standard main() style argc
2257 * @param argv Standard main() style argv. Initial components are already
2258 * stripped
2260 * @return Normal NTSTATUS return.
2262 static NTSTATUS
2263 rpc_file_close_internals(const DOM_SID *domain_sid, const char *domain_name,
2264 struct cli_state *cli,
2265 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2267 WERROR result;
2268 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
2269 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2272 /**
2273 * Close a file on a remote RPC server
2275 * @param argc Standard main() style argc
2276 * @param argv Standard main() style argv. Initial components are already
2277 * stripped
2279 * @return A shell status integer (0 for success)
2281 static int rpc_file_close(int argc, const char **argv)
2283 if (argc < 1) {
2284 DEBUG(1, ("No fileid given on close\n"));
2285 return(rpc_file_usage(argc, argv));
2288 return run_rpc_command(NULL, PI_SRVSVC, 0,
2289 rpc_file_close_internals,
2290 argc, argv);
2293 /**
2294 * Formatted print of open file info
2296 * @param info3 FILE_INFO_3 contents
2297 * @param str3 strings for FILE_INFO_3
2300 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
2302 fstring user = "", path = "";
2304 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
2305 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
2307 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
2308 info3->id, user, info3->perms, info3->num_locks, path);
2311 /**
2312 * List open files on a remote RPC server
2314 * All parameters are provided by the run_rpc_command function, except for
2315 * argc, argv which are passes through.
2317 * @param domain_sid The domain sid acquired from the remote server
2318 * @param cli A cli_state connected to the server.
2319 * @param mem_ctx Talloc context, destoyed on completion of the function.
2320 * @param argc Standard main() style argc
2321 * @param argv Standard main() style argv. Initial components are already
2322 * stripped
2324 * @return Normal NTSTATUS return.
2327 static NTSTATUS
2328 rpc_file_list_internals(const DOM_SID *domain_sid, const char *domain_name,
2329 struct cli_state *cli,
2330 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2332 SRV_FILE_INFO_CTR ctr;
2333 WERROR result;
2334 ENUM_HND hnd;
2335 uint32 preferred_len = 0xffffffff, i;
2336 const char *username=NULL;
2338 init_enum_hnd(&hnd, 0);
2340 /* if argc > 0, must be user command */
2341 if (argc > 0)
2342 username = smb_xstrdup(argv[0]);
2344 result = cli_srvsvc_net_file_enum(
2345 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
2347 if (!W_ERROR_IS_OK(result))
2348 goto done;
2350 /* Display results */
2352 d_printf(
2353 "\nEnumerating open files on remote server:\n\n"\
2354 "\nFileId Opened by Perms Locks Path"\
2355 "\n------ --------- ----- ----- ---- \n");
2356 for (i = 0; i < ctr.num_entries; i++)
2357 display_file_info_3(&ctr.file.info3[i].info_3,
2358 &ctr.file.info3[i].info_3_str);
2359 done:
2360 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2364 /**
2365 * List files for a user on a remote RPC server
2367 * @param argc Standard main() style argc
2368 * @param argv Standard main() style argv. Initial components are already
2369 * stripped
2371 * @return A shell status integer (0 for success)
2373 static int rpc_file_user(int argc, const char **argv)
2375 if (argc < 1) {
2376 DEBUG(1, ("No username given\n"));
2377 return(rpc_file_usage(argc, argv));
2380 return run_rpc_command(NULL, PI_SRVSVC, 0,
2381 rpc_file_list_internals,
2382 argc, argv);
2386 /**
2387 * 'net rpc file' entrypoint.
2388 * @param argc Standard main() style argc
2389 * @param argv Standard main() style argv. Initial components are already
2390 * stripped
2393 int net_rpc_file(int argc, const char **argv)
2395 struct functable func[] = {
2396 {"close", rpc_file_close},
2397 {"user", rpc_file_user},
2398 #if 0
2399 {"info", rpc_file_info},
2400 #endif
2401 {NULL, NULL}
2404 if (argc == 0)
2405 return run_rpc_command(NULL, PI_SRVSVC, 0,
2406 rpc_file_list_internals,
2407 argc, argv);
2409 return net_run_function(argc, argv, func, rpc_file_usage);
2412 /****************************************************************************/
2416 /**
2417 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
2419 * All parameters are provided by the run_rpc_command function, except for
2420 * argc, argv which are passed through.
2422 * @param domain_sid The domain sid aquired from the remote server
2423 * @param cli A cli_state connected to the server.
2424 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2425 * @param argc Standard main() style argc
2426 * @param argv Standard main() style argv. Initial components are already
2427 * stripped
2429 * @return Normal NTSTATUS return.
2432 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
2433 const char *domain_name,
2434 struct cli_state *cli,
2435 TALLOC_CTX *mem_ctx,
2436 int argc, const char **argv)
2438 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2440 result = cli_shutdown_abort(cli, mem_ctx);
2442 if (NT_STATUS_IS_OK(result))
2443 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
2444 else
2445 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
2447 return result;
2451 /**
2452 * ABORT the shutdown of a remote RPC Server, over winreg pipe
2454 * All parameters are provided by the run_rpc_command function, except for
2455 * argc, argv which are passed through.
2457 * @param domain_sid The domain sid aquired from the remote server
2458 * @param cli A cli_state connected to the server.
2459 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2460 * @param argc Standard main() style argc
2461 * @param argv Standard main() style argv. Initial components are already
2462 * stripped
2464 * @return Normal NTSTATUS return.
2467 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
2468 const char *domain_name,
2469 struct cli_state *cli,
2470 TALLOC_CTX *mem_ctx,
2471 int argc, const char **argv)
2473 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2475 result = cli_reg_abort_shutdown(cli, mem_ctx);
2477 if (NT_STATUS_IS_OK(result))
2478 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
2479 else
2480 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
2482 return result;
2485 /**
2486 * ABORT the Shut down of a remote RPC server
2488 * @param argc Standard main() style argc
2489 * @param argv Standard main() style argv. Initial components are already
2490 * stripped
2492 * @return A shell status integer (0 for success)
2495 static int rpc_shutdown_abort(int argc, const char **argv)
2497 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
2498 rpc_shutdown_abort_internals,
2499 argc, argv);
2501 if (rc == 0)
2502 return rc;
2504 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
2506 return run_rpc_command(NULL, PI_WINREG, 0,
2507 rpc_reg_shutdown_abort_internals,
2508 argc, argv);
2511 /**
2512 * Shut down a remote RPC Server
2514 * All parameters are provided by the run_rpc_command function, except for
2515 * argc, argv which are passes through.
2517 * @param domain_sid The domain sid aquired from the remote server
2518 * @param cli A cli_state connected to the server.
2519 * @param mem_ctx Talloc context, destoyed on compleation of the function.
2520 * @param argc Standard main() style argc
2521 * @param argc Standard main() style argv. Initial components are already
2522 * stripped
2524 * @return Normal NTSTATUS return.
2527 static NTSTATUS rpc_shutdown_internals(const DOM_SID *domain_sid,
2528 const char *domain_name,
2529 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2530 int argc, const char **argv)
2532 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2533 const char *msg = "This machine will be shutdown shortly";
2534 uint32 timeout = 20;
2535 #if 0
2536 poptContext pc;
2537 int rc;
2539 struct poptOption long_options[] = {
2540 {"message", 'm', POPT_ARG_STRING, &msg},
2541 {"timeout", 't', POPT_ARG_INT, &timeout},
2542 {"reboot", 'r', POPT_ARG_NONE, &reboot},
2543 {"force", 'f', POPT_ARG_NONE, &force},
2544 { 0, 0, 0, 0}
2547 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
2548 POPT_CONTEXT_KEEP_FIRST);
2550 rc = poptGetNextOpt(pc);
2552 if (rc < -1) {
2553 /* an error occurred during option processing */
2554 DEBUG(0, ("%s: %s\n",
2555 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
2556 poptStrerror(rc)));
2557 return NT_STATUS_INVALID_PARAMETER;
2559 #endif
2560 if (opt_comment) {
2561 msg = opt_comment;
2563 if (opt_timeout) {
2564 timeout = opt_timeout;
2567 /* create an entry */
2568 result = cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force);
2570 if (NT_STATUS_IS_OK(result))
2571 DEBUG(5,("Shutdown of remote machine succeeded\n"));
2572 else
2573 DEBUG(0,("Shutdown of remote machine failed!\n"));
2575 return result;
2578 /**
2579 * Shut down a remote RPC server
2581 * @param argc Standard main() style argc
2582 * @param argc Standard main() style argv. Initial components are already
2583 * stripped
2585 * @return A shell status integer (0 for success)
2588 static int rpc_shutdown(int argc, const char **argv)
2590 return run_rpc_command(NULL, PI_WINREG, 0, rpc_shutdown_internals,
2591 argc, argv);
2594 /***************************************************************************
2595 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
2597 ***************************************************************************/
2600 * Add interdomain trust account to the RPC server.
2601 * All parameters (except for argc and argv) are passed by run_rpc_command
2602 * function.
2604 * @param domain_sid The domain sid acquired from the server
2605 * @param cli A cli_state connected to the server.
2606 * @param mem_ctx Talloc context, destoyed on completion of the function.
2607 * @param argc Standard main() style argc
2608 * @param argc Standard main() style argv. Initial components are already
2609 * stripped
2611 * @return normal NTSTATUS return code
2614 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
2615 const char *domain_name,
2616 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2617 int argc, const char **argv) {
2619 POLICY_HND connect_pol, domain_pol, user_pol;
2620 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2621 char *acct_name;
2622 uint16 acb_info;
2623 uint32 unknown, user_rid;
2625 if (argc != 2) {
2626 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
2627 return NT_STATUS_INVALID_PARAMETER;
2631 * Make valid trusting domain account (ie. uppercased and with '$' appended)
2634 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
2635 return NT_STATUS_NO_MEMORY;
2638 strupper_m(acct_name);
2640 /* Get samr policy handle */
2641 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2642 &connect_pol);
2643 if (!NT_STATUS_IS_OK(result)) {
2644 goto done;
2647 /* Get domain policy handle */
2648 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2649 MAXIMUM_ALLOWED_ACCESS,
2650 domain_sid, &domain_pol);
2651 if (!NT_STATUS_IS_OK(result)) {
2652 goto done;
2655 /* Create trusting domain's account */
2656 acb_info = ACB_DOMTRUST;
2657 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
2658 mimir: yes, most probably it is */
2660 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
2661 acct_name, acb_info, unknown,
2662 &user_pol, &user_rid);
2663 if (!NT_STATUS_IS_OK(result)) {
2664 goto done;
2668 SAM_USERINFO_CTR ctr;
2669 SAM_USER_INFO_24 p24;
2670 uchar pwbuf[516];
2672 encode_pw_buffer((char *)pwbuf, argv[1], STR_UNICODE);
2674 ZERO_STRUCT(ctr);
2675 ZERO_STRUCT(p24);
2677 init_sam_user_info24(&p24, (char *)pwbuf, 24);
2679 ctr.switch_value = 24;
2680 ctr.info.id24 = &p24;
2682 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
2683 &cli->user_session_key, &ctr);
2685 if (!NT_STATUS_IS_OK(result)) {
2686 DEBUG(0,("Could not set trust account password: %s\n",
2687 nt_errstr(result)));
2688 goto done;
2692 done:
2693 SAFE_FREE(acct_name);
2694 return result;
2698 * Create interdomain trust account for a remote domain.
2700 * @param argc standard argc
2701 * @param argv standard argv without initial components
2703 * @return Integer status (0 means success)
2706 static int rpc_trustdom_add(int argc, const char **argv)
2708 if (argc > 0) {
2709 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
2710 argc, argv);
2711 } else {
2712 d_printf("Usage: net rpc trustdom add <domain>\n");
2713 return -1;
2719 * Delete interdomain trust account for a remote domain.
2721 * @param argc standard argc
2722 * @param argv standard argv without initial components
2724 * @return Integer status (0 means success)
2727 static int rpc_trustdom_del(int argc, const char **argv)
2729 d_printf("Sorry, not yet implemented.\n");
2730 d_printf("Use 'smbpasswd -x -i' instead.\n");
2731 return -1;
2736 * Establish trust relationship to a trusting domain.
2737 * Interdomain account must already be created on remote PDC.
2739 * @param argc standard argc
2740 * @param argv standard argv without initial components
2742 * @return Integer status (0 means success)
2745 static int rpc_trustdom_establish(int argc, const char **argv)
2747 struct cli_state *cli;
2748 struct in_addr server_ip;
2749 POLICY_HND connect_hnd;
2750 TALLOC_CTX *mem_ctx;
2751 NTSTATUS nt_status;
2752 DOM_SID *domain_sid;
2753 WKS_INFO_100 wks_info;
2755 char* domain_name;
2756 char* domain_name_pol;
2757 char* acct_name;
2758 fstring pdc_name;
2761 * Connect to \\server\ipc$ as 'our domain' account with password
2764 if (argc != 1) {
2765 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
2766 return -1;
2769 domain_name = smb_xstrdup(argv[0]);
2770 strupper_m(domain_name);
2772 /* account name used at first is our domain's name with '$' */
2773 asprintf(&acct_name, "%s$", lp_workgroup());
2774 strupper_m(acct_name);
2777 * opt_workgroup will be used by connection functions further,
2778 * hence it should be set to remote domain name instead of ours
2780 if (opt_workgroup) {
2781 opt_workgroup = smb_xstrdup(domain_name);
2784 opt_user_name = acct_name;
2786 /* find the domain controller */
2787 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
2788 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
2789 return -1;
2792 /* connect to ipc$ as username/password */
2793 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
2794 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
2796 /* Is it trusting domain account for sure ? */
2797 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
2798 nt_errstr(nt_status)));
2799 return -1;
2803 * Connect to \\server\ipc$ again (this time anonymously)
2806 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
2808 if (NT_STATUS_IS_ERR(nt_status)) {
2809 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
2810 domain_name, nt_errstr(nt_status)));
2814 * Use NetServerEnum2 to make sure we're talking to a proper server
2817 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
2818 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
2819 for domain %s\n", domain_name));
2823 * Call WksQueryInfo to check remote server's capabilities
2824 * note: It is now used only to get unicode domain name
2827 if (!cli_nt_session_open(cli, PI_WKSSVC)) {
2828 DEBUG(0, ("Couldn't not initialise wkssvc pipe\n"));
2829 return -1;
2832 if (!(mem_ctx = talloc_init("establishing trust relationship to domain %s",
2833 domain_name))) {
2834 DEBUG(0, ("talloc_init() failed\n"));
2835 cli_shutdown(cli);
2836 return -1;
2839 nt_status = cli_wks_query_info(cli, mem_ctx, &wks_info);
2841 if (NT_STATUS_IS_ERR(nt_status)) {
2842 DEBUG(0, ("WksQueryInfo call failed.\n"));
2843 return -1;
2846 if (cli->nt_pipe_fnum)
2847 cli_nt_session_close(cli);
2851 * Call LsaOpenPolicy and LsaQueryInfo
2854 if (!(mem_ctx = talloc_init("rpc_trustdom_establish"))) {
2855 DEBUG(0, ("talloc_init() failed\n"));
2856 cli_shutdown(cli);
2857 return -1;
2860 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2861 DEBUG(0, ("Could not initialise lsa pipe\n"));
2862 cli_shutdown(cli);
2863 return -1;
2866 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
2867 &connect_hnd);
2868 if (NT_STATUS_IS_ERR(nt_status)) {
2869 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
2870 nt_errstr(nt_status)));
2871 return -1;
2874 /* Querying info level 5 */
2876 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
2877 5 /* info level */, &domain_name_pol,
2878 &domain_sid);
2879 if (NT_STATUS_IS_ERR(nt_status)) {
2880 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
2881 nt_errstr(nt_status)));
2882 return -1;
2888 /* There should be actually query info level 3 (following nt serv behaviour),
2889 but I still don't know if it's _really_ necessary */
2892 * Store the password in secrets db
2895 if (!secrets_store_trusted_domain_password(domain_name, wks_info.uni_lan_grp.buffer,
2896 wks_info.uni_lan_grp.uni_str_len, opt_password,
2897 *domain_sid)) {
2898 DEBUG(0, ("Storing password for trusted domain failed.\n"));
2899 return -1;
2903 * Close the pipes and clean up
2906 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
2907 if (NT_STATUS_IS_ERR(nt_status)) {
2908 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
2909 nt_errstr(nt_status)));
2910 return -1;
2913 if (cli->nt_pipe_fnum)
2914 cli_nt_session_close(cli);
2916 talloc_destroy(mem_ctx);
2918 DEBUG(0, ("Success!\n"));
2919 return 0;
2923 * Revoke trust relationship to the remote domain
2925 * @param argc standard argc
2926 * @param argv standard argv without initial components
2928 * @return Integer status (0 means success)
2931 static int rpc_trustdom_revoke(int argc, const char **argv)
2933 char* domain_name;
2935 if (argc < 1) return -1;
2937 /* generate upper cased domain name */
2938 domain_name = smb_xstrdup(argv[0]);
2939 strupper_m(domain_name);
2941 /* delete password of the trust */
2942 if (!trusted_domain_password_delete(domain_name)) {
2943 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
2944 domain_name));
2945 return -1;
2948 return 0;
2952 * Usage for 'net rpc trustdom' command
2954 * @param argc standard argc
2955 * @param argv standard argv without inital components
2957 * @return Integer status returned to shell
2960 static int rpc_trustdom_usage(int argc, const char **argv)
2962 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
2963 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
2964 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
2965 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
2966 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
2967 return -1;
2971 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
2972 const char *domain_name,
2973 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2974 int argc, const char **argv)
2976 fstring str_sid;
2977 sid_to_string(str_sid, domain_sid);
2978 d_printf("%s\n", str_sid);
2979 return NT_STATUS_OK;
2983 static int rpc_trustdom_list(int argc, const char **argv)
2985 /* common variables */
2986 TALLOC_CTX* mem_ctx;
2987 struct cli_state *cli, *remote_cli;
2988 NTSTATUS nt_status;
2989 const char *domain_name = NULL;
2990 DOM_SID *queried_dom_sid;
2991 fstring ascii_sid, padding;
2992 int ascii_dom_name_len;
2993 POLICY_HND connect_hnd;
2995 /* trusted domains listing variables */
2996 unsigned int num_domains, enum_ctx = 0;
2997 int i, pad_len, col_len = 20;
2998 DOM_SID *domain_sids;
2999 char **trusted_dom_names;
3000 fstring pdc_name;
3001 char *dummy;
3003 /* trusting domains listing variables */
3004 POLICY_HND domain_hnd;
3005 char **trusting_dom_names;
3006 uint32 *trusting_dom_rids;
3009 * Listing trusted domains (stored in secrets.tdb, if local)
3012 mem_ctx = talloc_init("trust relationships listing");
3015 * set domain and pdc name to local samba server (default)
3016 * or to remote one given in command line
3019 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
3020 domain_name = opt_workgroup;
3021 opt_target_workgroup = opt_workgroup;
3022 } else {
3023 fstrcpy(pdc_name, global_myname());
3024 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
3025 opt_target_workgroup = domain_name;
3028 /* open \PIPE\lsarpc and open policy handle */
3029 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
3030 DEBUG(0, ("Couldn't connect to domain controller\n"));
3031 return -1;
3034 if (!cli_nt_session_open(cli, PI_LSARPC)) {
3035 DEBUG(0, ("Could not initialise lsa pipe\n"));
3036 return -1;
3039 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
3040 &connect_hnd);
3041 if (NT_STATUS_IS_ERR(nt_status)) {
3042 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
3043 nt_errstr(nt_status)));
3044 return -1;
3047 /* query info level 5 to obtain sid of a domain being queried */
3048 nt_status = cli_lsa_query_info_policy(
3049 cli, mem_ctx, &connect_hnd, 5 /* info level */,
3050 &dummy, &queried_dom_sid);
3052 if (NT_STATUS_IS_ERR(nt_status)) {
3053 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
3054 nt_errstr(nt_status)));
3055 return -1;
3059 * Keep calling LsaEnumTrustdom over opened pipe until
3060 * the end of enumeration is reached
3063 d_printf("Trusted domains list:\n\n");
3065 do {
3066 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
3067 &num_domains,
3068 &trusted_dom_names, &domain_sids);
3070 if (NT_STATUS_IS_ERR(nt_status)) {
3071 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
3072 nt_errstr(nt_status)));
3073 return -1;
3076 for (i = 0; i < num_domains; i++) {
3077 /* convert sid into ascii string */
3078 sid_to_string(ascii_sid, &(domain_sids[i]));
3080 /* calculate padding space for d_printf to look nicer */
3081 pad_len = col_len - strlen(trusted_dom_names[i]);
3082 padding[pad_len] = 0;
3083 do padding[--pad_len] = ' '; while (pad_len);
3085 d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
3089 * in case of no trusted domains say something rather
3090 * than just display blank line
3092 if (!num_domains) d_printf("none\n");
3094 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
3096 /* close this connection before doing next one */
3097 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
3098 if (NT_STATUS_IS_ERR(nt_status)) {
3099 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
3100 nt_errstr(nt_status)));
3101 return -1;
3104 cli_nt_session_close(cli);
3107 * Listing trusting domains (stored in passdb backend, if local)
3110 d_printf("\nTrusting domains list:\n\n");
3113 * Open \PIPE\samr and get needed policy handles
3115 if (!cli_nt_session_open(cli, PI_SAMR)) {
3116 DEBUG(0, ("Could not initialise samr pipe\n"));
3117 return -1;
3120 /* SamrConnect */
3121 nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
3122 &connect_hnd);
3123 if (!NT_STATUS_IS_OK(nt_status)) {
3124 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
3125 nt_errstr(nt_status)));
3126 return -1;
3129 /* SamrOpenDomain - we have to open domain policy handle in order to be
3130 able to enumerate accounts*/
3131 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
3132 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
3133 queried_dom_sid, &domain_hnd);
3134 if (!NT_STATUS_IS_OK(nt_status)) {
3135 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
3136 nt_errstr(nt_status)));
3137 return -1;
3141 * perform actual enumeration
3144 enum_ctx = 0; /* reset enumeration context from last enumeration */
3145 do {
3147 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
3148 &enum_ctx, ACB_DOMTRUST, 0xffff,
3149 &trusting_dom_names, &trusting_dom_rids,
3150 &num_domains);
3151 if (NT_STATUS_IS_ERR(nt_status)) {
3152 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
3153 nt_errstr(nt_status)));
3154 return -1;
3157 for (i = 0; i < num_domains; i++) {
3160 * get each single domain's sid (do we _really_ need this ?):
3161 * 1) connect to domain's pdc
3162 * 2) query the pdc for domain's sid
3165 /* get rid of '$' tail */
3166 ascii_dom_name_len = strlen(trusting_dom_names[i]);
3167 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
3168 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
3170 /* calculate padding space for d_printf to look nicer */
3171 pad_len = col_len - strlen(trusting_dom_names[i]);
3172 padding[pad_len] = 0;
3173 do padding[--pad_len] = ' '; while (pad_len);
3175 /* set opt_* variables to remote domain */
3176 strupper_m(trusting_dom_names[i]);
3177 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
3178 opt_target_workgroup = opt_workgroup;
3180 d_printf("%s%s", trusting_dom_names[i], padding);
3182 /* connect to remote domain controller */
3183 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
3184 if (remote_cli) {
3185 /* query for domain's sid */
3186 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
3187 d_printf("couldn't get domain's sid\n");
3189 cli_shutdown(remote_cli);
3191 } else {
3192 d_printf("domain controller is not responding\n");
3196 if (!num_domains) d_printf("none\n");
3198 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
3200 /* close opened samr and domain policy handles */
3201 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
3202 if (!NT_STATUS_IS_OK(nt_status)) {
3203 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
3206 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
3207 if (!NT_STATUS_IS_OK(nt_status)) {
3208 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
3211 /* close samr pipe and connection to IPC$ */
3212 cli_nt_session_close(cli);
3213 cli_shutdown(cli);
3215 talloc_destroy(mem_ctx);
3216 return 0;
3220 * Entrypoint for 'net rpc trustdom' code
3222 * @param argc standard argc
3223 * @param argv standard argv without initial components
3225 * @return Integer status (0 means success)
3228 static int rpc_trustdom(int argc, const char **argv)
3230 struct functable func[] = {
3231 {"add", rpc_trustdom_add},
3232 {"del", rpc_trustdom_del},
3233 {"establish", rpc_trustdom_establish},
3234 {"revoke", rpc_trustdom_revoke},
3235 {"help", rpc_trustdom_usage},
3236 {"list", rpc_trustdom_list},
3237 {NULL, NULL}
3240 if (argc == 0) {
3241 rpc_trustdom_usage(argc, argv);
3242 return -1;
3245 return (net_run_function(argc, argv, func, rpc_user_usage));
3249 * Check if a server will take rpc commands
3250 * @param flags Type of server to connect to (PDC, DMB, localhost)
3251 * if the host is not explicitly specified
3252 * @return BOOL (true means rpc supported)
3254 BOOL net_rpc_check(unsigned flags)
3256 struct cli_state cli;
3257 BOOL ret = False;
3258 struct in_addr server_ip;
3259 char *server_name = NULL;
3261 /* flags (i.e. server type) may depend on command */
3262 if (!net_find_server(flags, &server_ip, &server_name))
3263 return False;
3265 ZERO_STRUCT(cli);
3266 if (cli_initialise(&cli) == False)
3267 return False;
3269 if (!cli_connect(&cli, server_name, &server_ip))
3270 goto done;
3271 if (!attempt_netbios_session_request(&cli, global_myname(),
3272 server_name, &server_ip))
3273 goto done;
3274 if (!cli_negprot(&cli))
3275 goto done;
3276 if (cli.protocol < PROTOCOL_NT1)
3277 goto done;
3279 ret = True;
3280 done:
3281 cli_shutdown(&cli);
3282 return ret;
3285 /* dump sam database via samsync rpc calls */
3286 static int rpc_samdump(int argc, const char **argv) {
3287 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
3288 argc, argv);
3291 /* syncronise sam database via samsync rpc calls */
3292 static int rpc_vampire(int argc, const char **argv) {
3293 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
3294 argc, argv);
3296 /****************************************************************************/
3299 /**
3300 * Basic usage function for 'net rpc'
3301 * @param argc Standard main() style argc
3302 * @param argv Standard main() style argv. Initial components are already
3303 * stripped
3306 int net_rpc_usage(int argc, const char **argv)
3308 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
3309 d_printf(" net rpc join \t\t\tto join a domain \n");
3310 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n\n\n");
3311 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
3312 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
3313 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass");
3314 d_printf(" net rpc group \t\tto list groups\n");
3315 d_printf(" net rpc share \t\tto add, delete, and list shares\n");
3316 d_printf(" net rpc file \t\t\tto list open files\n");
3317 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
3318 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
3319 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
3320 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
3321 d_printf(" net rpc trustdom \t\tto create trusting domain's account\n"
3322 "\t\t\t\t\tor establish trust\n");
3323 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
3324 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
3325 d_printf("\n");
3326 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
3327 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
3328 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
3329 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
3330 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
3331 return -1;
3336 * Help function for 'net rpc'. Calls command specific help if requested
3337 * or displays usage of net rpc
3338 * @param argc Standard main() style argc
3339 * @param argv Standard main() style argv. Initial components are already
3340 * stripped
3343 int net_rpc_help(int argc, const char **argv)
3345 struct functable func[] = {
3346 {"join", rpc_join_usage},
3347 {"user", rpc_user_usage},
3348 {"group", rpc_group_usage},
3349 {"share", rpc_share_usage},
3350 /*{"changetrustpw", rpc_changetrustpw_usage}, */
3351 {"trustdom", rpc_trustdom_usage},
3352 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
3353 /*{"shutdown", rpc_shutdown_usage}, */
3354 {NULL, NULL}
3357 if (argc == 0) {
3358 net_rpc_usage(argc, argv);
3359 return -1;
3362 return (net_run_function(argc, argv, func, rpc_user_usage));
3366 /**
3367 * 'net rpc' entrypoint.
3368 * @param argc Standard main() style argc
3369 * @param argv Standard main() style argv. Initial components are already
3370 * stripped
3373 int net_rpc(int argc, const char **argv)
3375 struct functable func[] = {
3376 {"info", net_rpc_info},
3377 {"join", net_rpc_join},
3378 {"oldjoin", net_rpc_oldjoin},
3379 {"testjoin", net_rpc_testjoin},
3380 {"user", net_rpc_user},
3381 {"password", rpc_user_password},
3382 {"group", net_rpc_group},
3383 {"share", net_rpc_share},
3384 {"file", net_rpc_file},
3385 {"changetrustpw", net_rpc_changetrustpw},
3386 {"trustdom", rpc_trustdom},
3387 {"abortshutdown", rpc_shutdown_abort},
3388 {"shutdown", rpc_shutdown},
3389 {"samdump", rpc_samdump},
3390 {"vampire", rpc_vampire},
3391 {"getsid", net_rpc_getsid},
3392 {"help", net_rpc_help},
3393 {NULL, NULL}
3395 return net_run_function(argc, argv, func, net_rpc_usage);