r6303: Setting up for 3.0.15pre1
[Samba.git] / source / utils / net_rpc.c
blob6e884c24dfbd33ce0ecfb96028ad81ab1549d88a
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)
6 Copyright (C) 2004 Guenther Deschner (gd@samba.org)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "includes.h"
23 #include "utils/net.h"
25 /**
26 * @file net_rpc.c
28 * @brief RPC based subcommands for the 'net' utility.
30 * This file should contain much of the functionality that used to
31 * be found in rpcclient, execpt that the commands should change
32 * less often, and the fucntionality should be sane (the user is not
33 * expected to know a rid/sid before they conduct an operation etc.)
35 * @todo Perhaps eventually these should be split out into a number
36 * of files, as this could get quite big.
37 **/
40 /**
41 * Many of the RPC functions need the domain sid. This function gets
42 * it at the start of every run
44 * @param cli A cli_state already connected to the remote machine
46 * @return The Domain SID of the remote machine.
47 **/
49 static DOM_SID *net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx, char **domain_name)
51 DOM_SID *domain_sid;
52 POLICY_HND pol;
53 NTSTATUS result = NT_STATUS_OK;
54 uint32 info_class = 5;
56 if (!cli_nt_session_open (cli, PI_LSARPC)) {
57 fprintf(stderr, "could not initialise lsa pipe\n");
58 goto error;
61 result = cli_lsa_open_policy(cli, mem_ctx, False,
62 SEC_RIGHTS_MAXIMUM_ALLOWED,
63 &pol);
64 if (!NT_STATUS_IS_OK(result)) {
65 goto error;
68 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
69 domain_name, &domain_sid);
70 if (!NT_STATUS_IS_OK(result)) {
71 error:
72 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
74 if (!NT_STATUS_IS_OK(result)) {
75 fprintf(stderr, "error: %s\n", nt_errstr(result));
78 exit(1);
81 cli_lsa_close(cli, mem_ctx, &pol);
82 cli_nt_session_close(cli);
84 return domain_sid;
87 /**
88 * Run a single RPC command, from start to finish.
90 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
91 * @param conn_flag a NET_FLAG_ combination. Passed to
92 * net_make_ipc_connection.
93 * @param argc Standard main() style argc
94 * @param argc Standard main() style argv. Initial components are already
95 * stripped
96 * @return A shell status integer (0 for success)
99 int run_rpc_command(struct cli_state *cli_arg, const int pipe_idx, int conn_flags,
100 rpc_command_fn fn,
101 int argc, const char **argv)
103 struct cli_state *cli = NULL;
104 TALLOC_CTX *mem_ctx;
105 NTSTATUS nt_status;
106 DOM_SID *domain_sid;
107 char *domain_name;
109 /* make use of cli_state handed over as an argument, if possible */
110 if (!cli_arg)
111 cli = net_make_ipc_connection(conn_flags);
112 else
113 cli = cli_arg;
115 if (!cli) {
116 return -1;
119 /* Create mem_ctx */
121 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
122 DEBUG(0, ("talloc_init() failed\n"));
123 cli_shutdown(cli);
124 return -1;
127 domain_sid = net_get_remote_domain_sid(cli, mem_ctx, &domain_name);
129 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
130 if (!cli_nt_session_open(cli, pipe_idx)) {
131 DEBUG(0, ("Could not initialise pipe\n"));
135 nt_status = fn(domain_sid, domain_name, cli, mem_ctx, argc, argv);
137 if (!NT_STATUS_IS_OK(nt_status)) {
138 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
139 } else {
140 DEBUG(5, ("rpc command function succedded\n"));
143 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
144 if (cli->nt_pipe_fnum[cli->pipe_idx])
145 cli_nt_session_close(cli);
148 /* close the connection only if it was opened here */
149 if (!cli_arg)
150 cli_shutdown(cli);
152 talloc_destroy(mem_ctx);
154 return (!NT_STATUS_IS_OK(nt_status));
158 /****************************************************************************/
161 /**
162 * Force a change of the trust acccount password.
164 * All parameters are provided by the run_rpc_command function, except for
165 * argc, argv which are passes through.
167 * @param domain_sid The domain sid aquired from the remote server
168 * @param cli A cli_state connected to the server.
169 * @param mem_ctx Talloc context, destoyed on compleation of the function.
170 * @param argc Standard main() style argc
171 * @param argc Standard main() style argv. Initial components are already
172 * stripped
174 * @return Normal NTSTATUS return.
177 static NTSTATUS rpc_changetrustpw_internals(const DOM_SID *domain_sid, const char *domain_name,
178 struct cli_state *cli, TALLOC_CTX *mem_ctx,
179 int argc, const char **argv) {
181 return trust_pw_find_change_and_store_it(cli, mem_ctx, opt_target_workgroup);
184 /**
185 * Force a change of the trust acccount password.
187 * @param argc Standard main() style argc
188 * @param argc Standard main() style argv. Initial components are already
189 * stripped
191 * @return A shell status integer (0 for success)
194 int net_rpc_changetrustpw(int argc, const char **argv)
196 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
197 rpc_changetrustpw_internals,
198 argc, argv);
202 /****************************************************************************/
205 /**
206 * Join a domain, the old way.
208 * This uses 'machinename' as the inital password, and changes it.
210 * The password should be created with 'server manager' or equiv first.
212 * All parameters are provided by the run_rpc_command function, except for
213 * argc, argv which are passes through.
215 * @param domain_sid The domain sid aquired from the remote server
216 * @param cli A cli_state connected to the server.
217 * @param mem_ctx Talloc context, destoyed on compleation of the function.
218 * @param argc Standard main() style argc
219 * @param argc Standard main() style argv. Initial components are already
220 * stripped
222 * @return Normal NTSTATUS return.
225 static NTSTATUS rpc_oldjoin_internals(const DOM_SID *domain_sid, const char *domain_name,
226 struct cli_state *cli,
227 TALLOC_CTX *mem_ctx,
228 int argc, const char **argv) {
230 fstring trust_passwd;
231 unsigned char orig_trust_passwd_hash[16];
232 NTSTATUS result;
233 uint32 sec_channel_type;
236 check what type of join - if the user want's to join as
237 a BDC, the server must agree that we are a BDC.
239 if (argc >= 0) {
240 sec_channel_type = get_sec_channel_type(argv[0]);
241 } else {
242 sec_channel_type = get_sec_channel_type(NULL);
245 fstrcpy(trust_passwd, global_myname());
246 strlower_m(trust_passwd);
249 * Machine names can be 15 characters, but the max length on
250 * a password is 14. --jerry
253 trust_passwd[14] = '\0';
255 E_md4hash(trust_passwd, orig_trust_passwd_hash);
257 result = trust_pw_change_and_store_it(cli, mem_ctx, opt_target_workgroup,
258 orig_trust_passwd_hash,
259 sec_channel_type);
261 if (NT_STATUS_IS_OK(result))
262 printf("Joined domain %s.\n",opt_target_workgroup);
265 if (!secrets_store_domain_sid(opt_target_workgroup, domain_sid)) {
266 DEBUG(0, ("error storing domain sid for %s\n", opt_target_workgroup));
267 result = NT_STATUS_UNSUCCESSFUL;
270 return result;
273 /**
274 * Join a domain, the old way.
276 * @param argc Standard main() style argc
277 * @param argc Standard main() style argv. Initial components are already
278 * stripped
280 * @return A shell status integer (0 for success)
283 static int net_rpc_perform_oldjoin(int argc, const char **argv)
285 return run_rpc_command(NULL, PI_NETLOGON,
286 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
287 rpc_oldjoin_internals,
288 argc, argv);
291 /**
292 * Join a domain, the old way. This function exists to allow
293 * the message to be displayed when oldjoin was explicitly
294 * requested, but not when it was implied by "net rpc join"
296 * @param argc Standard main() style argc
297 * @param argc Standard main() style argv. Initial components are already
298 * stripped
300 * @return A shell status integer (0 for success)
303 static int net_rpc_oldjoin(int argc, const char **argv)
305 int rc = net_rpc_perform_oldjoin(argc, argv);
307 if (rc) {
308 d_printf("Failed to join domain\n");
311 return rc;
314 /**
315 * Basic usage function for 'net rpc join'
316 * @param argc Standard main() style argc
317 * @param argc Standard main() style argv. Initial components are already
318 * stripped
321 static int rpc_join_usage(int argc, const char **argv)
323 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
324 "\t to join a domain with admin username & password\n"\
325 "\t\t password will be prompted if needed and none is specified\n"\
326 "\t <type> can be (default MEMBER)\n"\
327 "\t\t BDC - Join as a BDC\n"\
328 "\t\t PDC - Join as a PDC\n"\
329 "\t\t MEMBER - Join as a MEMBER server\n");
331 net_common_flags_usage(argc, argv);
332 return -1;
335 /**
336 * 'net rpc join' entrypoint.
337 * @param argc Standard main() style argc
338 * @param argc Standard main() style argv. Initial components are already
339 * stripped
341 * Main 'net_rpc_join()' (where the admain username/password is used) is
342 * in net_rpc_join.c
343 * Try to just change the password, but if that doesn't work, use/prompt
344 * for a username/password.
347 int net_rpc_join(int argc, const char **argv)
349 if ((net_rpc_perform_oldjoin(argc, argv) == 0))
350 return 0;
352 return net_rpc_join_newstyle(argc, argv);
357 /**
358 * display info about a rpc domain
360 * All parameters are provided by the run_rpc_command function, except for
361 * argc, argv which are passed through.
363 * @param domain_sid The domain sid acquired from the remote server
364 * @param cli A cli_state connected to the server.
365 * @param mem_ctx Talloc context, destoyed on completion of the function.
366 * @param argc Standard main() style argc
367 * @param argv Standard main() style argv. Initial components are already
368 * stripped
370 * @return Normal NTSTATUS return.
373 static NTSTATUS
374 rpc_info_internals(const DOM_SID *domain_sid, const char *domain_name,
375 struct cli_state *cli,
376 TALLOC_CTX *mem_ctx, int argc, const char **argv)
378 POLICY_HND connect_pol, domain_pol;
379 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
380 SAM_UNK_CTR ctr;
381 fstring sid_str;
383 sid_to_string(sid_str, domain_sid);
385 /* Get sam policy handle */
386 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
387 &connect_pol);
388 if (!NT_STATUS_IS_OK(result)) {
389 goto done;
392 /* Get domain policy handle */
393 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
394 MAXIMUM_ALLOWED_ACCESS,
395 domain_sid, &domain_pol);
396 if (!NT_STATUS_IS_OK(result)) {
397 goto done;
400 ZERO_STRUCT(ctr);
401 result = cli_samr_query_dom_info(cli, mem_ctx, &domain_pol,
402 2, &ctr);
403 if (NT_STATUS_IS_OK(result)) {
404 TALLOC_CTX *ctx = talloc_init("rpc_info_internals");
405 d_printf("Domain Name: %s\n", unistr2_tdup(ctx, &ctr.info.inf2.uni_domain));
406 d_printf("Domain SID: %s\n", sid_str);
407 d_printf("Sequence number: %u\n", ctr.info.inf2.seq_num.low);
408 d_printf("Num users: %u\n", ctr.info.inf2.num_domain_usrs);
409 d_printf("Num domain groups: %u\n", ctr.info.inf2.num_domain_grps);
410 d_printf("Num local groups: %u\n", ctr.info.inf2.num_local_grps);
411 talloc_destroy(ctx);
414 done:
415 return result;
419 /**
420 * 'net rpc info' entrypoint.
421 * @param argc Standard main() style argc
422 * @param argc Standard main() style argv. Initial components are already
423 * stripped
425 int net_rpc_info(int argc, const char **argv)
427 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
428 rpc_info_internals,
429 argc, argv);
433 /**
434 * Fetch domain SID into the local secrets.tdb
436 * All parameters are provided by the run_rpc_command function, except for
437 * argc, argv which are passes through.
439 * @param domain_sid The domain sid acquired from the remote server
440 * @param cli A cli_state connected to the server.
441 * @param mem_ctx Talloc context, destoyed on completion of the function.
442 * @param argc Standard main() style argc
443 * @param argv Standard main() style argv. Initial components are already
444 * stripped
446 * @return Normal NTSTATUS return.
449 static NTSTATUS
450 rpc_getsid_internals(const DOM_SID *domain_sid, const char *domain_name,
451 struct cli_state *cli,
452 TALLOC_CTX *mem_ctx, int argc, const char **argv)
454 fstring sid_str;
456 sid_to_string(sid_str, domain_sid);
457 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
458 sid_str, domain_name);
460 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
461 DEBUG(0,("Can't store domain SID\n"));
462 return NT_STATUS_UNSUCCESSFUL;
465 return NT_STATUS_OK;
469 /**
470 * 'net rpc getsid' entrypoint.
471 * @param argc Standard main() style argc
472 * @param argc Standard main() style argv. Initial components are already
473 * stripped
475 int net_rpc_getsid(int argc, const char **argv)
477 return run_rpc_command(NULL, PI_SAMR, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
478 rpc_getsid_internals,
479 argc, argv);
483 /****************************************************************************/
486 * Basic usage function for 'net rpc user'
487 * @param argc Standard main() style argc.
488 * @param argv Standard main() style argv. Initial components are already
489 * stripped.
492 static int rpc_user_usage(int argc, const char **argv)
494 return net_help_user(argc, argv);
497 /**
498 * Add a new user to a remote RPC server
500 * All parameters are provided by the run_rpc_command function, except for
501 * argc, argv which are passes through.
503 * @param domain_sid The domain sid acquired from the remote server
504 * @param cli A cli_state connected to the server.
505 * @param mem_ctx Talloc context, destoyed on completion of the function.
506 * @param argc Standard main() style argc
507 * @param argv Standard main() style argv. Initial components are already
508 * stripped
510 * @return Normal NTSTATUS return.
513 static NTSTATUS rpc_user_add_internals(const DOM_SID *domain_sid, const char *domain_name,
514 struct cli_state *cli, TALLOC_CTX *mem_ctx,
515 int argc, const char **argv) {
517 POLICY_HND connect_pol, domain_pol, user_pol;
518 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
519 const char *acct_name;
520 uint16 acb_info;
521 uint32 unknown, user_rid;
523 if (argc != 1) {
524 d_printf("User must be specified\n");
525 rpc_user_usage(argc, argv);
526 return NT_STATUS_OK;
529 acct_name = argv[0];
531 /* Get sam policy handle */
533 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
534 &connect_pol);
535 if (!NT_STATUS_IS_OK(result)) {
536 goto done;
539 /* Get domain policy handle */
541 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
542 MAXIMUM_ALLOWED_ACCESS,
543 domain_sid, &domain_pol);
544 if (!NT_STATUS_IS_OK(result)) {
545 goto done;
548 /* Create domain user */
550 acb_info = ACB_NORMAL;
551 unknown = 0xe005000b; /* No idea what this is - a permission mask? */
553 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
554 acct_name, acb_info, unknown,
555 &user_pol, &user_rid);
556 if (!NT_STATUS_IS_OK(result)) {
557 goto done;
560 done:
561 if (!NT_STATUS_IS_OK(result)) {
562 d_printf("Failed to add user %s - %s\n", acct_name,
563 nt_errstr(result));
564 } else {
565 d_printf("Added user %s\n", acct_name);
567 return result;
570 /**
571 * Add a new user to a remote RPC server
573 * @param argc Standard main() style argc
574 * @param argv Standard main() style argv. Initial components are already
575 * stripped
577 * @return A shell status integer (0 for success)
580 static int rpc_user_add(int argc, const char **argv)
582 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_add_internals,
583 argc, argv);
586 /**
587 * Delete a user from a remote RPC server
589 * All parameters are provided by the run_rpc_command function, except for
590 * argc, argv which are passes through.
592 * @param domain_sid The domain sid acquired from the remote server
593 * @param cli A cli_state connected to the server.
594 * @param mem_ctx Talloc context, destoyed on completion of the function.
595 * @param argc Standard main() style argc
596 * @param argv Standard main() style argv. Initial components are already
597 * stripped
599 * @return Normal NTSTATUS return.
602 static NTSTATUS rpc_user_del_internals(const DOM_SID *domain_sid,
603 const char *domain_name,
604 struct cli_state *cli,
605 TALLOC_CTX *mem_ctx,
606 int argc, const char **argv)
608 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
609 POLICY_HND connect_pol, domain_pol, user_pol;
611 if (argc < 1) {
612 d_printf("User must be specified\n");
613 rpc_user_usage(argc, argv);
614 return NT_STATUS_OK;
616 /* Get sam policy and domain handles */
618 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
619 &connect_pol);
621 if (!NT_STATUS_IS_OK(result)) {
622 goto done;
625 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
626 MAXIMUM_ALLOWED_ACCESS,
627 domain_sid, &domain_pol);
629 if (!NT_STATUS_IS_OK(result)) {
630 goto done;
633 /* Get handle on user */
636 uint32 *user_rids, num_rids, *name_types;
637 uint32 flags = 0x000003e8; /* Unknown */
639 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
640 flags, 1, &argv[0],
641 &num_rids, &user_rids,
642 &name_types);
644 if (!NT_STATUS_IS_OK(result)) {
645 goto done;
648 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
649 MAXIMUM_ALLOWED_ACCESS,
650 user_rids[0], &user_pol);
652 if (!NT_STATUS_IS_OK(result)) {
653 goto done;
657 /* Delete user */
659 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
661 if (!NT_STATUS_IS_OK(result)) {
662 goto done;
665 /* Display results */
667 done:
668 return result;
672 /**
673 * Rename a user on a remote RPC server
675 * All parameters are provided by the run_rpc_command function, except for
676 * argc, argv which are passes through.
678 * @param domain_sid The domain sid acquired from the remote server
679 * @param cli A cli_state connected to the server.
680 * @param mem_ctx Talloc context, destoyed on completion of the function.
681 * @param argc Standard main() style argc
682 * @param argv Standard main() style argv. Initial components are already
683 * stripped
685 * @return Normal NTSTATUS return.
688 static NTSTATUS rpc_user_rename_internals(const DOM_SID *domain_sid, const char *domain_name,
689 struct cli_state *cli, TALLOC_CTX *mem_ctx,
690 int argc, const char **argv) {
692 POLICY_HND connect_pol, domain_pol, user_pol;
693 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
694 uint32 info_level = 7;
695 const char *old_name, *new_name;
696 uint32 *user_rid;
697 uint32 flags = 0x000003e8; /* Unknown */
698 uint32 num_rids, *name_types;
699 uint32 num_names = 1;
700 const char **names;
701 SAM_USERINFO_CTR *user_ctr;
702 SAM_USERINFO_CTR ctr;
703 SAM_USER_INFO_7 info7;
705 if (argc != 2) {
706 d_printf("New and old username must be specified\n");
707 rpc_user_usage(argc, argv);
708 return NT_STATUS_OK;
711 old_name = argv[0];
712 new_name = argv[1];
714 ZERO_STRUCT(ctr);
715 ZERO_STRUCT(user_ctr);
717 /* Get sam policy handle */
719 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
720 &connect_pol);
721 if (!NT_STATUS_IS_OK(result)) {
722 goto done;
725 /* Get domain policy handle */
727 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
728 MAXIMUM_ALLOWED_ACCESS,
729 domain_sid, &domain_pol);
730 if (!NT_STATUS_IS_OK(result)) {
731 goto done;
734 names = TALLOC_ARRAY(mem_ctx, const char *, num_names);
735 names[0] = old_name;
736 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
737 flags, num_names, names,
738 &num_rids, &user_rid, &name_types);
739 if (!NT_STATUS_IS_OK(result)) {
740 goto done;
743 /* Open domain user */
744 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
745 MAXIMUM_ALLOWED_ACCESS, user_rid[0], &user_pol);
747 if (!NT_STATUS_IS_OK(result)) {
748 goto done;
751 /* Query user info */
752 result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol,
753 info_level, &user_ctr);
755 if (!NT_STATUS_IS_OK(result)) {
756 goto done;
759 ctr.switch_value = info_level;
760 ctr.info.id7 = &info7;
762 init_sam_user_info7(&info7, new_name);
764 /* Set new name */
765 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol,
766 info_level, &cli->user_session_key, &ctr);
768 if (!NT_STATUS_IS_OK(result)) {
769 goto done;
772 done:
773 if (!NT_STATUS_IS_OK(result)) {
774 d_printf("Failed to rename user from %s to %s - %s\n", old_name, new_name,
775 nt_errstr(result));
776 } else {
777 d_printf("Renamed user from %s to %s\n", old_name, new_name);
779 return result;
783 /**
784 * Rename a user on a remote RPC server
786 * @param argc Standard main() style argc
787 * @param argv Standard main() style argv. Initial components are already
788 * stripped
790 * @return A shell status integer (0 for success)
793 static int rpc_user_rename(int argc, const char **argv)
795 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_rename_internals,
796 argc, argv);
799 /**
800 * Delete a user from a remote RPC server
802 * @param argc Standard main() style argc
803 * @param argv Standard main() style argv. Initial components are already
804 * stripped
806 * @return A shell status integer (0 for success)
809 static int rpc_user_delete(int argc, const char **argv)
811 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_del_internals,
812 argc, argv);
815 /**
816 * Set a password for a user on a remote RPC server
818 * All parameters are provided by the run_rpc_command function, except for
819 * argc, argv which are passes through.
821 * @param domain_sid The domain sid acquired from the remote server
822 * @param cli A cli_state connected to the server.
823 * @param mem_ctx Talloc context, destoyed on completion of the function.
824 * @param argc Standard main() style argc
825 * @param argv Standard main() style argv. Initial components are already
826 * stripped
828 * @return Normal NTSTATUS return.
831 static NTSTATUS rpc_user_password_internals(const DOM_SID *domain_sid,
832 const char *domain_name,
833 struct cli_state *cli,
834 TALLOC_CTX *mem_ctx,
835 int argc, const char **argv)
837 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
838 POLICY_HND connect_pol, domain_pol, user_pol;
839 SAM_USERINFO_CTR ctr;
840 SAM_USER_INFO_24 p24;
841 uchar pwbuf[516];
842 const char *user;
843 const char *new_password;
844 char *prompt = NULL;
846 if (argc < 1) {
847 d_printf("User must be specified\n");
848 rpc_user_usage(argc, argv);
849 return NT_STATUS_OK;
852 user = argv[0];
854 if (argv[1]) {
855 new_password = argv[1];
856 } else {
857 asprintf(&prompt, "Enter new password for %s:", user);
858 new_password = getpass(prompt);
859 SAFE_FREE(prompt);
862 /* Get sam policy and domain handles */
864 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
865 &connect_pol);
867 if (!NT_STATUS_IS_OK(result)) {
868 goto done;
871 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
872 MAXIMUM_ALLOWED_ACCESS,
873 domain_sid, &domain_pol);
875 if (!NT_STATUS_IS_OK(result)) {
876 goto done;
879 /* Get handle on user */
882 uint32 *user_rids, num_rids, *name_types;
883 uint32 flags = 0x000003e8; /* Unknown */
885 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
886 flags, 1, &user,
887 &num_rids, &user_rids,
888 &name_types);
890 if (!NT_STATUS_IS_OK(result)) {
891 goto done;
894 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
895 MAXIMUM_ALLOWED_ACCESS,
896 user_rids[0], &user_pol);
898 if (!NT_STATUS_IS_OK(result)) {
899 goto done;
903 /* Set password on account */
905 ZERO_STRUCT(ctr);
906 ZERO_STRUCT(p24);
908 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
910 init_sam_user_info24(&p24, (char *)pwbuf,24);
912 ctr.switch_value = 24;
913 ctr.info.id24 = &p24;
915 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 24,
916 &cli->user_session_key, &ctr);
918 if (!NT_STATUS_IS_OK(result)) {
919 goto done;
922 /* Display results */
924 done:
925 return result;
929 /**
930 * Set a user's password on a remote RPC server
932 * @param argc Standard main() style argc
933 * @param argv Standard main() style argv. Initial components are already
934 * stripped
936 * @return A shell status integer (0 for success)
939 static int rpc_user_password(int argc, const char **argv)
941 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_password_internals,
942 argc, argv);
945 /**
946 * List user's groups on a remote RPC server
948 * All parameters are provided by the run_rpc_command function, except for
949 * argc, argv which are passes through.
951 * @param domain_sid The domain sid acquired from the remote server
952 * @param cli A cli_state connected to the server.
953 * @param mem_ctx Talloc context, destoyed on completion of the function.
954 * @param argc Standard main() style argc
955 * @param argv Standard main() style argv. Initial components are already
956 * stripped
958 * @return Normal NTSTATUS return.
961 static NTSTATUS
962 rpc_user_info_internals(const DOM_SID *domain_sid, const char *domain_name,
963 struct cli_state *cli,
964 TALLOC_CTX *mem_ctx, int argc, const char **argv)
966 POLICY_HND connect_pol, domain_pol, user_pol;
967 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
968 uint32 *rids, num_rids, *name_types, num_names;
969 uint32 flags = 0x000003e8; /* Unknown */
970 int i;
971 char **names;
972 DOM_GID *user_gids;
974 if (argc < 1) {
975 d_printf("User must be specified\n");
976 rpc_user_usage(argc, argv);
977 return NT_STATUS_OK;
979 /* Get sam policy handle */
981 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
982 &connect_pol);
983 if (!NT_STATUS_IS_OK(result)) goto done;
985 /* Get domain policy handle */
987 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
988 MAXIMUM_ALLOWED_ACCESS,
989 domain_sid, &domain_pol);
990 if (!NT_STATUS_IS_OK(result)) goto done;
992 /* Get handle on user */
994 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
995 flags, 1, &argv[0],
996 &num_rids, &rids, &name_types);
998 if (!NT_STATUS_IS_OK(result)) goto done;
1000 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1001 MAXIMUM_ALLOWED_ACCESS,
1002 rids[0], &user_pol);
1003 if (!NT_STATUS_IS_OK(result)) goto done;
1005 result = cli_samr_query_usergroups(cli, mem_ctx, &user_pol,
1006 &num_rids, &user_gids);
1008 if (!NT_STATUS_IS_OK(result)) goto done;
1010 /* Look up rids */
1012 if (num_rids) {
1013 rids = TALLOC_ARRAY(mem_ctx, uint32, num_rids);
1015 for (i = 0; i < num_rids; i++)
1016 rids[i] = user_gids[i].g_rid;
1018 result = cli_samr_lookup_rids(cli, mem_ctx, &domain_pol,
1019 num_rids, rids,
1020 &num_names, &names, &name_types);
1022 if (!NT_STATUS_IS_OK(result)) {
1023 goto done;
1026 /* Display results */
1028 for (i = 0; i < num_names; i++)
1029 printf("%s\n", names[i]);
1031 done:
1032 return result;
1035 /**
1036 * List a user's groups from a remote RPC server
1038 * @param argc Standard main() style argc
1039 * @param argv Standard main() style argv. Initial components are already
1040 * stripped
1042 * @return A shell status integer (0 for success)
1045 static int rpc_user_info(int argc, const char **argv)
1047 return run_rpc_command(NULL, PI_SAMR, 0, rpc_user_info_internals,
1048 argc, argv);
1051 /**
1052 * List users on a remote RPC server
1054 * All parameters are provided by the run_rpc_command function, except for
1055 * argc, argv which are passes through.
1057 * @param domain_sid The domain sid acquired from the remote server
1058 * @param cli A cli_state connected to the server.
1059 * @param mem_ctx Talloc context, destoyed on completion of the function.
1060 * @param argc Standard main() style argc
1061 * @param argv Standard main() style argv. Initial components are already
1062 * stripped
1064 * @return Normal NTSTATUS return.
1067 static NTSTATUS
1068 rpc_user_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1069 struct cli_state *cli,
1070 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1072 POLICY_HND connect_pol, domain_pol;
1073 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1074 uint32 start_idx=0, num_entries, i, loop_count = 0;
1075 SAM_DISPINFO_CTR ctr;
1076 SAM_DISPINFO_1 info1;
1078 /* Get sam policy handle */
1080 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1081 &connect_pol);
1082 if (!NT_STATUS_IS_OK(result)) {
1083 goto done;
1086 /* Get domain policy handle */
1088 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1089 MAXIMUM_ALLOWED_ACCESS,
1090 domain_sid, &domain_pol);
1091 if (!NT_STATUS_IS_OK(result)) {
1092 goto done;
1095 /* Query domain users */
1096 ZERO_STRUCT(ctr);
1097 ZERO_STRUCT(info1);
1098 ctr.sam.info1 = &info1;
1099 if (opt_long_list_entries)
1100 d_printf("\nUser name Comment"\
1101 "\n-----------------------------\n");
1102 do {
1103 fstring user, desc;
1104 uint32 max_entries, max_size;
1106 get_query_dispinfo_params(
1107 loop_count, &max_entries, &max_size);
1109 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1110 &start_idx, 1, &num_entries,
1111 max_entries, max_size, &ctr);
1112 loop_count++;
1114 for (i = 0; i < num_entries; i++) {
1115 unistr2_to_ascii(user, &(&ctr.sam.info1->str[i])->uni_acct_name, sizeof(user)-1);
1116 if (opt_long_list_entries)
1117 unistr2_to_ascii(desc, &(&ctr.sam.info1->str[i])->uni_acct_desc, sizeof(desc)-1);
1119 if (opt_long_list_entries)
1120 printf("%-21.21s %s\n", user, desc);
1121 else
1122 printf("%s\n", user);
1124 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1126 done:
1127 return result;
1130 /**
1131 * 'net rpc user' entrypoint.
1132 * @param argc Standard main() style argc
1133 * @param argc Standard main() style argv. Initial components are already
1134 * stripped
1137 int net_rpc_user(int argc, const char **argv)
1139 struct functable func[] = {
1140 {"add", rpc_user_add},
1141 {"info", rpc_user_info},
1142 {"delete", rpc_user_delete},
1143 {"password", rpc_user_password},
1144 {"rename", rpc_user_rename},
1145 {NULL, NULL}
1148 if (argc == 0) {
1149 if (opt_long_list_entries) {
1150 } else {
1152 return run_rpc_command(NULL,PI_SAMR, 0,
1153 rpc_user_list_internals,
1154 argc, argv);
1157 return net_run_function(argc, argv, func, rpc_user_usage);
1161 /****************************************************************************/
1164 * Basic usage function for 'net rpc group'
1165 * @param argc Standard main() style argc.
1166 * @param argv Standard main() style argv. Initial components are already
1167 * stripped.
1170 static int rpc_group_usage(int argc, const char **argv)
1172 return net_help_group(argc, argv);
1176 * Delete group on a remote RPC server
1178 * All parameters are provided by the run_rpc_command function, except for
1179 * argc, argv which are passes through.
1181 * @param domain_sid The domain sid acquired from the remote server
1182 * @param cli A cli_state connected to the server.
1183 * @param mem_ctx Talloc context, destoyed on completion of the function.
1184 * @param argc Standard main() style argc
1185 * @param argv Standard main() style argv. Initial components are already
1186 * stripped
1188 * @return Normal NTSTATUS return.
1191 static NTSTATUS rpc_group_delete_internals(const DOM_SID *domain_sid,
1192 const char *domain_name,
1193 struct cli_state *cli,
1194 TALLOC_CTX *mem_ctx,
1195 int argc, const char **argv)
1197 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1198 BOOL group_is_primary = False;
1199 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1201 uint32 *group_rids, num_rids, *name_types, num_members,
1202 *group_attrs, group_rid;
1203 uint32 flags = 0x000003e8; /* Unknown */
1204 /* char **names; */
1205 int i;
1206 /* DOM_GID *user_gids; */
1207 SAM_USERINFO_CTR *user_ctr;
1208 fstring temp;
1210 if (argc < 1) {
1211 d_printf("specify group\n");
1212 rpc_group_usage(argc,argv);
1213 return NT_STATUS_OK; /* ok? */
1216 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1217 &connect_pol);
1219 if (!NT_STATUS_IS_OK(result)) {
1220 d_printf("Request samr_connect failed\n");
1221 goto done;
1224 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1225 MAXIMUM_ALLOWED_ACCESS,
1226 domain_sid, &domain_pol);
1228 if (!NT_STATUS_IS_OK(result)) {
1229 d_printf("Request open_domain failed\n");
1230 goto done;
1233 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol,
1234 flags, 1, &argv[0],
1235 &num_rids, &group_rids,
1236 &name_types);
1238 if (!NT_STATUS_IS_OK(result)) {
1239 d_printf("Lookup of '%s' failed\n",argv[0]);
1240 goto done;
1243 switch (name_types[0])
1245 case SID_NAME_DOM_GRP:
1246 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1247 MAXIMUM_ALLOWED_ACCESS,
1248 group_rids[0], &group_pol);
1249 if (!NT_STATUS_IS_OK(result)) {
1250 d_printf("Request open_group failed");
1251 goto done;
1254 group_rid = group_rids[0];
1256 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
1257 &num_members, &group_rids,
1258 &group_attrs);
1260 if (!NT_STATUS_IS_OK(result)) {
1261 d_printf("Unable to query group members of %s",argv[0]);
1262 goto done;
1265 if (opt_verbose) {
1266 d_printf("Domain Group %s (rid: %d) has %d members\n",
1267 argv[0],group_rid,num_members);
1270 /* Check if group is anyone's primary group */
1271 for (i = 0; i < num_members; i++)
1273 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
1274 MAXIMUM_ALLOWED_ACCESS,
1275 group_rids[i], &user_pol);
1277 if (!NT_STATUS_IS_OK(result)) {
1278 d_printf("Unable to open group member %d\n",group_rids[i]);
1279 goto done;
1282 ZERO_STRUCT(user_ctr);
1284 result = cli_samr_query_userinfo(cli, mem_ctx, &user_pol,
1285 21, &user_ctr);
1287 if (!NT_STATUS_IS_OK(result)) {
1288 d_printf("Unable to lookup userinfo for group member %d\n",group_rids[i]);
1289 goto done;
1292 if (user_ctr->info.id21->group_rid == group_rid) {
1293 unistr2_to_ascii(temp, &(user_ctr->info.id21)->uni_user_name,
1294 sizeof(temp)-1);
1295 if (opt_verbose)
1296 d_printf("Group is primary group of %s\n",temp);
1297 group_is_primary = True;
1300 cli_samr_close(cli, mem_ctx, &user_pol);
1303 if (group_is_primary) {
1304 d_printf("Unable to delete group because some of it's "
1305 "members have it as primary group\n");
1306 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1307 goto done;
1310 /* remove all group members */
1311 for (i = 0; i < num_members; i++)
1313 if (opt_verbose)
1314 d_printf("Remove group member %d...",group_rids[i]);
1315 result = cli_samr_del_groupmem(cli, mem_ctx, &group_pol, group_rids[i]);
1317 if (NT_STATUS_IS_OK(result)) {
1318 if (opt_verbose)
1319 d_printf("ok\n");
1320 } else {
1321 if (opt_verbose)
1322 d_printf("failed\n");
1323 goto done;
1327 result = cli_samr_delete_dom_group(cli, mem_ctx, &group_pol);
1329 break;
1330 /* removing a local group is easier... */
1331 case SID_NAME_ALIAS:
1332 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1333 MAXIMUM_ALLOWED_ACCESS,
1334 group_rids[0], &group_pol);
1336 if (!NT_STATUS_IS_OK(result)) {
1337 d_printf("Request open_alias failed\n");
1338 goto done;
1341 result = cli_samr_delete_dom_alias(cli, mem_ctx, &group_pol);
1342 break;
1343 default:
1344 d_printf("%s is of type %s. This command is only for deleting local or global groups\n",
1345 argv[0],sid_type_lookup(name_types[0]));
1346 result = NT_STATUS_UNSUCCESSFUL;
1347 goto done;
1351 if (NT_STATUS_IS_OK(result)) {
1352 if (opt_verbose)
1353 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types[0]),argv[0]);
1354 } else {
1355 d_printf("Deleting of %s failed: %s\n",argv[0],
1356 get_friendly_nt_error_msg(result));
1359 done:
1360 return result;
1364 static int rpc_group_delete(int argc, const char **argv)
1366 return run_rpc_command(NULL, PI_SAMR, 0, rpc_group_delete_internals,
1367 argc,argv);
1370 static NTSTATUS
1371 rpc_group_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1372 struct cli_state *cli,
1373 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1375 POLICY_HND connect_pol, domain_pol, group_pol;
1376 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1377 GROUP_INFO_CTR group_info;
1379 if (argc != 1) {
1380 d_printf("Group name must be specified\n");
1381 rpc_group_usage(argc, argv);
1382 return NT_STATUS_OK;
1385 /* Get sam policy handle */
1387 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1388 &connect_pol);
1389 if (!NT_STATUS_IS_OK(result)) goto done;
1391 /* Get domain policy handle */
1393 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1394 MAXIMUM_ALLOWED_ACCESS,
1395 domain_sid, &domain_pol);
1396 if (!NT_STATUS_IS_OK(result)) goto done;
1398 /* Create the group */
1400 result = cli_samr_create_dom_group(cli, mem_ctx, &domain_pol,
1401 argv[0], MAXIMUM_ALLOWED_ACCESS,
1402 &group_pol);
1403 if (!NT_STATUS_IS_OK(result)) goto done;
1405 if (strlen(opt_comment) == 0) goto done;
1407 /* We've got a comment to set */
1409 group_info.switch_value1 = 4;
1410 init_samr_group_info4(&group_info.group.info4, opt_comment);
1412 result = cli_samr_set_groupinfo(cli, mem_ctx, &group_pol, &group_info);
1413 if (!NT_STATUS_IS_OK(result)) goto done;
1415 done:
1416 if (NT_STATUS_IS_OK(result))
1417 DEBUG(5, ("add group succeeded\n"));
1418 else
1419 d_printf("add group failed: %s\n", nt_errstr(result));
1421 return result;
1424 static NTSTATUS
1425 rpc_alias_add_internals(const DOM_SID *domain_sid, const char *domain_name,
1426 struct cli_state *cli,
1427 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1429 POLICY_HND connect_pol, domain_pol, alias_pol;
1430 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1431 ALIAS_INFO_CTR alias_info;
1433 if (argc != 1) {
1434 d_printf("Alias name must be specified\n");
1435 rpc_group_usage(argc, argv);
1436 return NT_STATUS_OK;
1439 /* Get sam policy handle */
1441 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1442 &connect_pol);
1443 if (!NT_STATUS_IS_OK(result)) goto done;
1445 /* Get domain policy handle */
1447 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1448 MAXIMUM_ALLOWED_ACCESS,
1449 domain_sid, &domain_pol);
1450 if (!NT_STATUS_IS_OK(result)) goto done;
1452 /* Create the group */
1454 result = cli_samr_create_dom_alias(cli, mem_ctx, &domain_pol,
1455 argv[0], &alias_pol);
1456 if (!NT_STATUS_IS_OK(result)) goto done;
1458 if (strlen(opt_comment) == 0) goto done;
1460 /* We've got a comment to set */
1462 alias_info.switch_value1 = 3;
1463 alias_info.switch_value2 = 3;
1464 init_samr_alias_info3(&alias_info.alias.info3, opt_comment);
1466 result = cli_samr_set_aliasinfo(cli, mem_ctx, &alias_pol, &alias_info);
1467 if (!NT_STATUS_IS_OK(result)) goto done;
1469 done:
1470 if (NT_STATUS_IS_OK(result))
1471 DEBUG(5, ("add alias succeeded\n"));
1472 else
1473 d_printf("add alias failed: %s\n", nt_errstr(result));
1475 return result;
1478 static int rpc_group_add(int argc, const char **argv)
1480 if (opt_localgroup)
1481 return run_rpc_command(NULL, PI_SAMR, 0,
1482 rpc_alias_add_internals,
1483 argc, argv);
1485 return run_rpc_command(NULL, PI_SAMR, 0,
1486 rpc_group_add_internals,
1487 argc, argv);
1490 static NTSTATUS
1491 get_sid_from_name(struct cli_state *cli, TALLOC_CTX *mem_ctx, const char *name,
1492 DOM_SID *sid, enum SID_NAME_USE *type)
1494 int current_pipe = cli->pipe_idx;
1496 DOM_SID *sids = NULL;
1497 uint32 *types = NULL;
1498 POLICY_HND lsa_pol;
1499 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1501 if (current_pipe != PI_LSARPC) {
1503 if (current_pipe != -1)
1504 cli_nt_session_close(cli);
1506 if (!cli_nt_session_open(cli, PI_LSARPC))
1507 goto done;
1510 result = cli_lsa_open_policy(cli, mem_ctx, False,
1511 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1513 if (!NT_STATUS_IS_OK(result))
1514 goto done;
1516 result = cli_lsa_lookup_names(cli, mem_ctx, &lsa_pol, 1,
1517 &name, &sids, &types);
1519 if (NT_STATUS_IS_OK(result)) {
1520 sid_copy(sid, &sids[0]);
1521 *type = types[0];
1524 cli_lsa_close(cli, mem_ctx, &lsa_pol);
1526 done:
1527 if (current_pipe != PI_LSARPC) {
1528 cli_nt_session_close(cli);
1529 if (current_pipe != -1)
1530 cli_nt_session_open(cli, current_pipe);
1533 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1535 /* Try as S-1-5-whatever */
1537 DOM_SID tmp_sid;
1539 if (string_to_sid(&tmp_sid, name)) {
1540 sid_copy(sid, &tmp_sid);
1541 *type = SID_NAME_UNKNOWN;
1542 result = NT_STATUS_OK;
1546 return result;
1549 static NTSTATUS
1550 rpc_add_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1551 const DOM_SID *group_sid, const char *member)
1553 POLICY_HND connect_pol, domain_pol;
1554 NTSTATUS result;
1555 uint32 group_rid;
1556 POLICY_HND group_pol;
1558 uint32 num_rids;
1559 uint32 *rids = NULL;
1560 uint32 *rid_types = NULL;
1562 DOM_SID sid;
1564 sid_copy(&sid, group_sid);
1566 if (!sid_split_rid(&sid, &group_rid))
1567 return NT_STATUS_UNSUCCESSFUL;
1569 /* Get sam policy handle */
1570 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1571 &connect_pol);
1572 if (!NT_STATUS_IS_OK(result))
1573 return result;
1575 /* Get domain policy handle */
1576 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1577 MAXIMUM_ALLOWED_ACCESS,
1578 &sid, &domain_pol);
1579 if (!NT_STATUS_IS_OK(result))
1580 return result;
1582 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1583 1, &member,
1584 &num_rids, &rids, &rid_types);
1586 if (!NT_STATUS_IS_OK(result)) {
1587 d_printf("Could not lookup up group member %s\n", member);
1588 goto done;
1591 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1592 MAXIMUM_ALLOWED_ACCESS,
1593 group_rid, &group_pol);
1595 if (!NT_STATUS_IS_OK(result))
1596 goto done;
1598 result = cli_samr_add_groupmem(cli, mem_ctx, &group_pol, rids[0]);
1600 done:
1601 cli_samr_close(cli, mem_ctx, &connect_pol);
1602 return result;
1605 static NTSTATUS
1606 rpc_add_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1607 const DOM_SID *alias_sid, const char *member)
1609 POLICY_HND connect_pol, domain_pol;
1610 NTSTATUS result;
1611 uint32 alias_rid;
1612 POLICY_HND alias_pol;
1614 DOM_SID member_sid;
1615 enum SID_NAME_USE member_type;
1617 DOM_SID sid;
1619 sid_copy(&sid, alias_sid);
1621 if (!sid_split_rid(&sid, &alias_rid))
1622 return NT_STATUS_UNSUCCESSFUL;
1624 result = get_sid_from_name(cli, mem_ctx, member,
1625 &member_sid, &member_type);
1627 if (!NT_STATUS_IS_OK(result)) {
1628 d_printf("Could not lookup up group member %s\n", member);
1629 return result;
1632 /* Get sam policy handle */
1633 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1634 &connect_pol);
1635 if (!NT_STATUS_IS_OK(result)) {
1636 goto done;
1639 /* Get domain policy handle */
1640 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1641 MAXIMUM_ALLOWED_ACCESS,
1642 &sid, &domain_pol);
1643 if (!NT_STATUS_IS_OK(result)) {
1644 goto done;
1647 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1648 MAXIMUM_ALLOWED_ACCESS,
1649 alias_rid, &alias_pol);
1651 if (!NT_STATUS_IS_OK(result))
1652 return result;
1654 result = cli_samr_add_aliasmem(cli, mem_ctx, &alias_pol, &member_sid);
1656 if (!NT_STATUS_IS_OK(result))
1657 return result;
1659 done:
1660 cli_samr_close(cli, mem_ctx, &connect_pol);
1661 return result;
1664 static NTSTATUS
1665 rpc_group_addmem_internals(const DOM_SID *domain_sid, const char *domain_name,
1666 struct cli_state *cli,
1667 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1669 DOM_SID group_sid;
1670 enum SID_NAME_USE group_type;
1672 if (argc != 2) {
1673 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
1674 return NT_STATUS_UNSUCCESSFUL;
1677 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1678 &group_sid, &group_type))) {
1679 d_printf("Could not lookup group name %s\n", argv[0]);
1680 return NT_STATUS_UNSUCCESSFUL;
1683 if (group_type == SID_NAME_DOM_GRP) {
1684 NTSTATUS result = rpc_add_groupmem(cli, mem_ctx,
1685 &group_sid, argv[1]);
1687 if (!NT_STATUS_IS_OK(result)) {
1688 d_printf("Could not add %s to %s: %s\n",
1689 argv[1], argv[0], nt_errstr(result));
1691 return result;
1694 if (group_type == SID_NAME_ALIAS) {
1695 NTSTATUS result = rpc_add_aliasmem(cli, mem_ctx,
1696 &group_sid, argv[1]);
1698 if (!NT_STATUS_IS_OK(result)) {
1699 d_printf("Could not add %s to %s: %s\n",
1700 argv[1], argv[0], nt_errstr(result));
1702 return result;
1705 d_printf("Can only add members to global or local groups which "
1706 "%s is not\n", argv[0]);
1708 return NT_STATUS_UNSUCCESSFUL;
1711 static int rpc_group_addmem(int argc, const char **argv)
1713 return run_rpc_command(NULL, PI_SAMR, 0,
1714 rpc_group_addmem_internals,
1715 argc, argv);
1718 static NTSTATUS
1719 rpc_del_groupmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1720 const DOM_SID *group_sid, const char *member)
1722 POLICY_HND connect_pol, domain_pol;
1723 NTSTATUS result;
1724 uint32 group_rid;
1725 POLICY_HND group_pol;
1727 uint32 num_rids;
1728 uint32 *rids = NULL;
1729 uint32 *rid_types = NULL;
1731 DOM_SID sid;
1733 sid_copy(&sid, group_sid);
1735 if (!sid_split_rid(&sid, &group_rid))
1736 return NT_STATUS_UNSUCCESSFUL;
1738 /* Get sam policy handle */
1739 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1740 &connect_pol);
1741 if (!NT_STATUS_IS_OK(result))
1742 return result;
1744 /* Get domain policy handle */
1745 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1746 MAXIMUM_ALLOWED_ACCESS,
1747 &sid, &domain_pol);
1748 if (!NT_STATUS_IS_OK(result))
1749 return result;
1751 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
1752 1, &member,
1753 &num_rids, &rids, &rid_types);
1755 if (!NT_STATUS_IS_OK(result)) {
1756 d_printf("Could not lookup up group member %s\n", member);
1757 goto done;
1760 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
1761 MAXIMUM_ALLOWED_ACCESS,
1762 group_rid, &group_pol);
1764 if (!NT_STATUS_IS_OK(result))
1765 goto done;
1767 result = cli_samr_del_groupmem(cli, mem_ctx, &group_pol, rids[0]);
1769 done:
1770 cli_samr_close(cli, mem_ctx, &connect_pol);
1771 return result;
1774 static NTSTATUS
1775 rpc_del_aliasmem(struct cli_state *cli, TALLOC_CTX *mem_ctx,
1776 const DOM_SID *alias_sid, const char *member)
1778 POLICY_HND connect_pol, domain_pol;
1779 NTSTATUS result;
1780 uint32 alias_rid;
1781 POLICY_HND alias_pol;
1783 DOM_SID member_sid;
1784 enum SID_NAME_USE member_type;
1786 DOM_SID sid;
1788 sid_copy(&sid, alias_sid);
1790 if (!sid_split_rid(&sid, &alias_rid))
1791 return NT_STATUS_UNSUCCESSFUL;
1793 result = get_sid_from_name(cli, mem_ctx, member,
1794 &member_sid, &member_type);
1796 if (!NT_STATUS_IS_OK(result)) {
1797 d_printf("Could not lookup up group member %s\n", member);
1798 return result;
1801 /* Get sam policy handle */
1802 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1803 &connect_pol);
1804 if (!NT_STATUS_IS_OK(result)) {
1805 goto done;
1808 /* Get domain policy handle */
1809 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1810 MAXIMUM_ALLOWED_ACCESS,
1811 &sid, &domain_pol);
1812 if (!NT_STATUS_IS_OK(result)) {
1813 goto done;
1816 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
1817 MAXIMUM_ALLOWED_ACCESS,
1818 alias_rid, &alias_pol);
1820 if (!NT_STATUS_IS_OK(result))
1821 return result;
1823 result = cli_samr_del_aliasmem(cli, mem_ctx, &alias_pol, &member_sid);
1825 if (!NT_STATUS_IS_OK(result))
1826 return result;
1828 done:
1829 cli_samr_close(cli, mem_ctx, &connect_pol);
1830 return result;
1833 static NTSTATUS
1834 rpc_group_delmem_internals(const DOM_SID *domain_sid, const char *domain_name,
1835 struct cli_state *cli,
1836 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1838 DOM_SID group_sid;
1839 enum SID_NAME_USE group_type;
1841 if (argc != 2) {
1842 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
1843 return NT_STATUS_UNSUCCESSFUL;
1846 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1847 &group_sid, &group_type))) {
1848 d_printf("Could not lookup group name %s\n", argv[0]);
1849 return NT_STATUS_UNSUCCESSFUL;
1852 if (group_type == SID_NAME_DOM_GRP) {
1853 NTSTATUS result = rpc_del_groupmem(cli, mem_ctx,
1854 &group_sid, argv[1]);
1856 if (!NT_STATUS_IS_OK(result)) {
1857 d_printf("Could not del %s from %s: %s\n",
1858 argv[1], argv[0], nt_errstr(result));
1860 return result;
1863 if (group_type == SID_NAME_ALIAS) {
1864 NTSTATUS result = rpc_del_aliasmem(cli, mem_ctx,
1865 &group_sid, argv[1]);
1867 if (!NT_STATUS_IS_OK(result)) {
1868 d_printf("Could not del %s from %s: %s\n",
1869 argv[1], argv[0], nt_errstr(result));
1871 return result;
1874 d_printf("Can only delete members from global or local groups which "
1875 "%s is not\n", argv[0]);
1877 return NT_STATUS_UNSUCCESSFUL;
1880 static int rpc_group_delmem(int argc, const char **argv)
1882 return run_rpc_command(NULL, PI_SAMR, 0,
1883 rpc_group_delmem_internals,
1884 argc, argv);
1887 /**
1888 * List groups on a remote RPC server
1890 * All parameters are provided by the run_rpc_command function, except for
1891 * argc, argv which are passes through.
1893 * @param domain_sid The domain sid acquired from the remote server
1894 * @param cli A cli_state connected to the server.
1895 * @param mem_ctx Talloc context, destoyed on completion of the function.
1896 * @param argc Standard main() style argc
1897 * @param argv Standard main() style argv. Initial components are already
1898 * stripped
1900 * @return Normal NTSTATUS return.
1903 static NTSTATUS
1904 rpc_group_list_internals(const DOM_SID *domain_sid, const char *domain_name,
1905 struct cli_state *cli,
1906 TALLOC_CTX *mem_ctx, int argc, const char **argv)
1908 POLICY_HND connect_pol, domain_pol;
1909 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1910 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
1911 struct acct_info *groups;
1912 BOOL global = False;
1913 BOOL local = False;
1914 BOOL builtin = False;
1916 if (argc == 0) {
1917 global = True;
1918 local = True;
1919 builtin = True;
1922 for (i=0; i<argc; i++) {
1923 if (strequal(argv[i], "global"))
1924 global = True;
1926 if (strequal(argv[i], "local"))
1927 local = True;
1929 if (strequal(argv[i], "builtin"))
1930 builtin = True;
1933 /* Get sam policy handle */
1935 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
1936 &connect_pol);
1937 if (!NT_STATUS_IS_OK(result)) {
1938 goto done;
1941 /* Get domain policy handle */
1943 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
1944 MAXIMUM_ALLOWED_ACCESS,
1945 domain_sid, &domain_pol);
1946 if (!NT_STATUS_IS_OK(result)) {
1947 goto done;
1950 /* Query domain groups */
1951 if (opt_long_list_entries)
1952 d_printf("\nGroup name Comment"\
1953 "\n-----------------------------\n");
1954 do {
1955 SAM_DISPINFO_CTR ctr;
1956 SAM_DISPINFO_3 info3;
1957 uint32 max_size;
1959 ZERO_STRUCT(ctr);
1960 ZERO_STRUCT(info3);
1961 ctr.sam.info3 = &info3;
1963 if (!global) break;
1965 get_query_dispinfo_params(
1966 loop_count, &max_entries, &max_size);
1968 result = cli_samr_query_dispinfo(cli, mem_ctx, &domain_pol,
1969 &start_idx, 3, &num_entries,
1970 max_entries, max_size, &ctr);
1972 if (!NT_STATUS_IS_OK(result) &&
1973 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
1974 break;
1976 for (i = 0; i < num_entries; i++) {
1978 fstring group, desc;
1980 unistr2_to_ascii(group, &(&ctr.sam.info3->str[i])->uni_grp_name, sizeof(group)-1);
1981 unistr2_to_ascii(desc, &(&ctr.sam.info3->str[i])->uni_grp_desc, sizeof(desc)-1);
1983 if (opt_long_list_entries)
1984 printf("%-21.21s %-50.50s\n",
1985 group, desc);
1986 else
1987 printf("%s\n", group);
1989 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1990 /* query domain aliases */
1991 start_idx = 0;
1992 do {
1993 if (!local) break;
1995 /* The max_size field in cli_samr_enum_als_groups is more like
1996 * an account_control field with indiviual bits what to
1997 * retrieve. Set this to 0xffff as NT4 usrmgr.exe does to get
1998 * everything. I'm too lazy (sorry) to get this through to
1999 * rpc_parse/ etc. Volker */
2001 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
2002 &start_idx, 0xffff,
2003 &groups, &num_entries);
2005 if (!NT_STATUS_IS_OK(result) &&
2006 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2007 break;
2009 for (i = 0; i < num_entries; i++) {
2011 char *description = NULL;
2013 if (opt_long_list_entries) {
2015 POLICY_HND alias_pol;
2016 ALIAS_INFO_CTR ctr;
2018 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
2019 &domain_pol,
2020 0x8,
2021 groups[i].rid,
2022 &alias_pol))) &&
2023 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
2024 &alias_pol, 3,
2025 &ctr))) &&
2026 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
2027 &alias_pol)))) {
2028 description = unistr2_tdup(mem_ctx,
2029 &ctr.alias.info3.uni_acct_desc);
2033 if (description != NULL) {
2034 printf("%-21.21s %-50.50s\n",
2035 groups[i].acct_name,
2036 description);
2037 } else {
2038 printf("%s\n", groups[i].acct_name);
2041 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2042 cli_samr_close(cli, mem_ctx, &domain_pol);
2043 /* Get builtin policy handle */
2045 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2046 MAXIMUM_ALLOWED_ACCESS,
2047 &global_sid_Builtin, &domain_pol);
2048 if (!NT_STATUS_IS_OK(result)) {
2049 goto done;
2051 /* query builtin aliases */
2052 start_idx = 0;
2053 do {
2054 if (!builtin) break;
2056 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
2057 &start_idx, max_entries,
2058 &groups, &num_entries);
2060 if (!NT_STATUS_IS_OK(result) &&
2061 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2062 break;
2064 for (i = 0; i < num_entries; i++) {
2066 char *description = NULL;
2068 if (opt_long_list_entries) {
2070 POLICY_HND alias_pol;
2071 ALIAS_INFO_CTR ctr;
2073 if ((NT_STATUS_IS_OK(cli_samr_open_alias(cli, mem_ctx,
2074 &domain_pol,
2075 0x8,
2076 groups[i].rid,
2077 &alias_pol))) &&
2078 (NT_STATUS_IS_OK(cli_samr_query_alias_info(cli, mem_ctx,
2079 &alias_pol, 3,
2080 &ctr))) &&
2081 (NT_STATUS_IS_OK(cli_samr_close(cli, mem_ctx,
2082 &alias_pol)))) {
2083 description = unistr2_tdup(mem_ctx,
2084 &ctr.alias.info3.uni_acct_desc);
2088 if (description != NULL) {
2089 printf("%-21.21s %-50.50s\n",
2090 groups[i].acct_name,
2091 description);
2092 } else {
2093 printf("%s\n", groups[i].acct_name);
2096 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2098 done:
2099 return result;
2102 static int rpc_group_list(int argc, const char **argv)
2104 return run_rpc_command(NULL, PI_SAMR, 0,
2105 rpc_group_list_internals,
2106 argc, argv);
2109 static NTSTATUS
2110 rpc_list_group_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2111 const char *domain_name, const DOM_SID *domain_sid,
2112 POLICY_HND *domain_pol, uint32 rid)
2114 NTSTATUS result;
2115 POLICY_HND group_pol;
2116 uint32 num_members, *group_rids, *group_attrs;
2117 uint32 num_names;
2118 char **names;
2119 uint32 *name_types;
2120 int i;
2122 fstring sid_str;
2123 sid_to_string(sid_str, domain_sid);
2125 result = cli_samr_open_group(cli, mem_ctx, domain_pol,
2126 MAXIMUM_ALLOWED_ACCESS,
2127 rid, &group_pol);
2129 if (!NT_STATUS_IS_OK(result))
2130 return result;
2132 result = cli_samr_query_groupmem(cli, mem_ctx, &group_pol,
2133 &num_members, &group_rids,
2134 &group_attrs);
2136 if (!NT_STATUS_IS_OK(result))
2137 return result;
2139 while (num_members > 0) {
2140 int this_time = 512;
2142 if (num_members < this_time)
2143 this_time = num_members;
2145 result = cli_samr_lookup_rids(cli, mem_ctx, domain_pol,
2146 this_time, group_rids,
2147 &num_names, &names, &name_types);
2149 if (!NT_STATUS_IS_OK(result))
2150 return result;
2152 /* We only have users as members, but make the output
2153 the same as the output of alias members */
2155 for (i = 0; i < this_time; i++) {
2157 if (opt_long_list_entries) {
2158 printf("%s-%d %s\\%s %d\n", sid_str,
2159 group_rids[i], domain_name, names[i],
2160 SID_NAME_USER);
2161 } else {
2162 printf("%s\\%s\n", domain_name, names[i]);
2166 num_members -= this_time;
2167 group_rids += 512;
2170 return NT_STATUS_OK;
2173 static NTSTATUS
2174 rpc_list_alias_members(struct cli_state *cli, TALLOC_CTX *mem_ctx,
2175 POLICY_HND *domain_pol, uint32 rid)
2177 NTSTATUS result;
2178 POLICY_HND alias_pol, lsa_pol;
2179 uint32 num_members;
2180 DOM_SID *alias_sids;
2181 char **domains;
2182 char **names;
2183 uint32 *types;
2184 int i;
2186 result = cli_samr_open_alias(cli, mem_ctx, domain_pol,
2187 MAXIMUM_ALLOWED_ACCESS, rid, &alias_pol);
2189 if (!NT_STATUS_IS_OK(result))
2190 return result;
2192 result = cli_samr_query_aliasmem(cli, mem_ctx, &alias_pol,
2193 &num_members, &alias_sids);
2195 if (!NT_STATUS_IS_OK(result)) {
2196 d_printf("Couldn't list alias members\n");
2197 return result;
2200 if (num_members == 0) {
2201 return NT_STATUS_OK;
2204 cli_nt_session_close(cli);
2206 if (!cli_nt_session_open(cli, PI_LSARPC)) {
2207 d_printf("Couldn't open LSA pipe\n");
2208 return result;
2211 result = cli_lsa_open_policy(cli, mem_ctx, True,
2212 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2214 if (!NT_STATUS_IS_OK(result)) {
2215 d_printf("Couldn't open LSA policy handle\n");
2216 return result;
2219 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, num_members,
2220 alias_sids,
2221 &domains, &names, &types);
2223 if (!NT_STATUS_IS_OK(result) &&
2224 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2225 d_printf("Couldn't lookup SIDs\n");
2226 return result;
2229 for (i = 0; i < num_members; i++) {
2230 fstring sid_str;
2231 sid_to_string(sid_str, &alias_sids[i]);
2233 if (opt_long_list_entries) {
2234 printf("%s %s\\%s %d\n", sid_str,
2235 domains[i] ? domains[i] : "*unknown*",
2236 names[i] ? names[i] : "*unknown*", types[i]);
2237 } else {
2238 if (domains[i])
2239 printf("%s\\%s\n", domains[i], names[i]);
2240 else
2241 printf("%s\n", sid_str);
2245 return NT_STATUS_OK;
2248 static NTSTATUS
2249 rpc_group_members_internals(const DOM_SID *domain_sid,
2250 const char *domain_name,
2251 struct cli_state *cli,
2252 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2254 NTSTATUS result;
2255 POLICY_HND connect_pol, domain_pol;
2256 uint32 num_rids, *rids, *rid_types;
2258 /* Get sam policy handle */
2260 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2261 &connect_pol);
2263 if (!NT_STATUS_IS_OK(result))
2264 return result;
2266 /* Get domain policy handle */
2268 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2269 MAXIMUM_ALLOWED_ACCESS,
2270 domain_sid, &domain_pol);
2272 if (!NT_STATUS_IS_OK(result))
2273 return result;
2275 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
2276 1, argv, &num_rids, &rids, &rid_types);
2278 if (!NT_STATUS_IS_OK(result)) {
2280 /* Ok, did not find it in the global sam, try with builtin */
2282 DOM_SID sid_Builtin;
2284 cli_samr_close(cli, mem_ctx, &domain_pol);
2286 string_to_sid(&sid_Builtin, "S-1-5-32");
2288 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2289 MAXIMUM_ALLOWED_ACCESS,
2290 &sid_Builtin, &domain_pol);
2292 if (!NT_STATUS_IS_OK(result)) {
2293 d_printf("Couldn't find group %s\n", argv[0]);
2294 return result;
2297 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
2298 1, argv, &num_rids,
2299 &rids, &rid_types);
2301 if (!NT_STATUS_IS_OK(result)) {
2302 d_printf("Couldn't find group %s\n", argv[0]);
2303 return result;
2307 if (num_rids != 1) {
2308 d_printf("Couldn't find group %s\n", argv[0]);
2309 return result;
2312 if (rid_types[0] == SID_NAME_DOM_GRP) {
2313 return rpc_list_group_members(cli, mem_ctx, domain_name,
2314 domain_sid, &domain_pol,
2315 rids[0]);
2318 if (rid_types[0] == SID_NAME_ALIAS) {
2319 return rpc_list_alias_members(cli, mem_ctx, &domain_pol,
2320 rids[0]);
2323 return NT_STATUS_NO_SUCH_GROUP;
2326 static int rpc_group_members(int argc, const char **argv)
2328 if (argc != 1) {
2329 return rpc_group_usage(argc, argv);
2332 return run_rpc_command(NULL, PI_SAMR, 0,
2333 rpc_group_members_internals,
2334 argc, argv);
2337 static NTSTATUS
2338 rpc_group_rename_internals(const DOM_SID *domain_sid,
2339 const char *domain_name,
2340 struct cli_state *cli,
2341 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2343 NTSTATUS result;
2344 POLICY_HND connect_pol, domain_pol, group_pol;
2345 uint32 num_rids, *rids, *rid_types;
2346 GROUP_INFO_CTR ctr;
2348 if (argc != 2) {
2349 d_printf("Usage: 'net rpc group rename group newname'\n");
2350 return NT_STATUS_UNSUCCESSFUL;
2353 /* Get sam policy handle */
2355 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
2356 &connect_pol);
2358 if (!NT_STATUS_IS_OK(result))
2359 return result;
2361 /* Get domain policy handle */
2363 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
2364 MAXIMUM_ALLOWED_ACCESS,
2365 domain_sid, &domain_pol);
2367 if (!NT_STATUS_IS_OK(result))
2368 return result;
2370 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, 1000,
2371 1, argv, &num_rids, &rids, &rid_types);
2373 if (num_rids != 1) {
2374 d_printf("Couldn't find group %s\n", argv[0]);
2375 return result;
2378 if (rid_types[0] != SID_NAME_DOM_GRP) {
2379 d_printf("Can only rename domain groups\n");
2380 return NT_STATUS_UNSUCCESSFUL;
2383 result = cli_samr_open_group(cli, mem_ctx, &domain_pol,
2384 MAXIMUM_ALLOWED_ACCESS,
2385 rids[0], &group_pol);
2387 if (!NT_STATUS_IS_OK(result))
2388 return result;
2390 ZERO_STRUCT(ctr);
2392 ctr.switch_value1 = 2;
2393 init_samr_group_info2(&ctr.group.info2, argv[1]);
2395 result = cli_samr_set_groupinfo(cli, mem_ctx, &group_pol, &ctr);
2397 if (!NT_STATUS_IS_OK(result))
2398 return result;
2400 return NT_STATUS_NO_SUCH_GROUP;
2403 static int rpc_group_rename(int argc, const char **argv)
2405 if (argc != 2) {
2406 return rpc_group_usage(argc, argv);
2409 return run_rpc_command(NULL, PI_SAMR, 0,
2410 rpc_group_rename_internals,
2411 argc, argv);
2414 /**
2415 * 'net rpc group' entrypoint.
2416 * @param argc Standard main() style argc
2417 * @param argc Standard main() style argv. Initial components are already
2418 * stripped
2421 int net_rpc_group(int argc, const char **argv)
2423 struct functable func[] = {
2424 {"add", rpc_group_add},
2425 {"delete", rpc_group_delete},
2426 {"addmem", rpc_group_addmem},
2427 {"delmem", rpc_group_delmem},
2428 {"list", rpc_group_list},
2429 {"members", rpc_group_members},
2430 {"rename", rpc_group_rename},
2431 {NULL, NULL}
2434 if (argc == 0) {
2435 if (opt_long_list_entries) {
2436 } else {
2438 return run_rpc_command(NULL, PI_SAMR, 0,
2439 rpc_group_list_internals,
2440 argc, argv);
2443 return net_run_function(argc, argv, func, rpc_group_usage);
2446 /****************************************************************************/
2448 static int rpc_share_usage(int argc, const char **argv)
2450 return net_help_share(argc, argv);
2453 /**
2454 * Add a share on a remote RPC server
2456 * All parameters are provided by the run_rpc_command function, except for
2457 * argc, argv which are passes through.
2459 * @param domain_sid The domain sid acquired from the remote server
2460 * @param cli A cli_state connected to the server.
2461 * @param mem_ctx Talloc context, destoyed on completion of the function.
2462 * @param argc Standard main() style argc
2463 * @param argv Standard main() style argv. Initial components are already
2464 * stripped
2466 * @return Normal NTSTATUS return.
2468 static NTSTATUS
2469 rpc_share_add_internals(const DOM_SID *domain_sid, const char *domain_name,
2470 struct cli_state *cli,
2471 TALLOC_CTX *mem_ctx,int argc, const char **argv)
2473 WERROR result;
2474 char *sharename=talloc_strdup(mem_ctx, argv[0]);
2475 char *path;
2476 uint32 type=0; /* only allow disk shares to be added */
2477 uint32 num_users=0, perms=0;
2478 char *password=NULL; /* don't allow a share password */
2479 uint32 level = 2;
2481 path = strchr(sharename, '=');
2482 if (!path)
2483 return NT_STATUS_UNSUCCESSFUL;
2484 *path++ = '\0';
2486 result = cli_srvsvc_net_share_add(cli, mem_ctx, sharename, type,
2487 opt_comment, perms, opt_maxusers,
2488 num_users, path, password,
2489 level, NULL);
2490 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2493 static int rpc_share_add(int argc, const char **argv)
2495 if ((argc < 1) || !strchr(argv[0], '=')) {
2496 DEBUG(1,("Sharename or path not specified on add\n"));
2497 return rpc_share_usage(argc, argv);
2499 return run_rpc_command(NULL, PI_SRVSVC, 0,
2500 rpc_share_add_internals,
2501 argc, argv);
2504 /**
2505 * Delete a share on a remote RPC server
2507 * All parameters are provided by the run_rpc_command function, except for
2508 * argc, argv which are passes through.
2510 * @param domain_sid The domain sid acquired from the remote server
2511 * @param cli A cli_state connected to the server.
2512 * @param mem_ctx Talloc context, destoyed on completion of the function.
2513 * @param argc Standard main() style argc
2514 * @param argv Standard main() style argv. Initial components are already
2515 * stripped
2517 * @return Normal NTSTATUS return.
2519 static NTSTATUS
2520 rpc_share_del_internals(const DOM_SID *domain_sid, const char *domain_name,
2521 struct cli_state *cli,
2522 TALLOC_CTX *mem_ctx,int argc, const char **argv)
2524 WERROR result;
2526 result = cli_srvsvc_net_share_del(cli, mem_ctx, argv[0]);
2527 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2530 /**
2531 * Delete a share on a remote RPC server
2533 * @param domain_sid The domain sid acquired from the remote server
2534 * @param argc Standard main() style argc
2535 * @param argv Standard main() style argv. Initial components are already
2536 * stripped
2538 * @return A shell status integer (0 for success)
2540 static int rpc_share_delete(int argc, const char **argv)
2542 if (argc < 1) {
2543 DEBUG(1,("Sharename not specified on delete\n"));
2544 return rpc_share_usage(argc, argv);
2546 return run_rpc_command(NULL, PI_SRVSVC, 0,
2547 rpc_share_del_internals,
2548 argc, argv);
2552 * Formatted print of share info
2554 * @param info1 pointer to SRV_SHARE_INFO_1 to format
2557 static void display_share_info_1(SRV_SHARE_INFO_1 *info1)
2559 fstring netname = "", remark = "";
2561 rpcstr_pull_unistr2_fstring(netname, &info1->info_1_str.uni_netname);
2562 rpcstr_pull_unistr2_fstring(remark, &info1->info_1_str.uni_remark);
2564 if (opt_long_list_entries) {
2565 d_printf("%-12s %-8.8s %-50s\n",
2566 netname, share_type[info1->info_1.type], remark);
2567 } else {
2568 d_printf("%s\n", netname);
2573 /**
2574 * List shares on a remote RPC server
2576 * All parameters are provided by the run_rpc_command function, except for
2577 * argc, argv which are passes through.
2579 * @param domain_sid The domain sid acquired from the remote server
2580 * @param cli A cli_state connected to the server.
2581 * @param mem_ctx Talloc context, destoyed on completion of the function.
2582 * @param argc Standard main() style argc
2583 * @param argv Standard main() style argv. Initial components are already
2584 * stripped
2586 * @return Normal NTSTATUS return.
2589 static NTSTATUS
2590 rpc_share_list_internals(const DOM_SID *domain_sid, const char *domain_name,
2591 struct cli_state *cli,
2592 TALLOC_CTX *mem_ctx, int argc, const char **argv)
2594 SRV_SHARE_INFO_CTR ctr;
2595 WERROR result;
2596 ENUM_HND hnd;
2597 uint32 preferred_len = 0xffffffff, i;
2599 init_enum_hnd(&hnd, 0);
2601 result = cli_srvsvc_net_share_enum(
2602 cli, mem_ctx, 1, &ctr, preferred_len, &hnd);
2604 if (!W_ERROR_IS_OK(result))
2605 goto done;
2607 /* Display results */
2609 if (opt_long_list_entries) {
2610 d_printf(
2611 "\nEnumerating shared resources (exports) on remote server:\n\n"\
2612 "\nShare name Type Description\n"\
2613 "---------- ---- -----------\n");
2615 for (i = 0; i < ctr.num_entries; i++)
2616 display_share_info_1(&ctr.share.info1[i]);
2617 done:
2618 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
2621 /**
2622 * Migrate shares from a remote RPC server to the local RPC srever
2624 * All parameters are provided by the run_rpc_command function, except for
2625 * argc, argv which are passes through.
2627 * @param domain_sid The domain sid acquired from the remote server
2628 * @param cli A cli_state connected to the server.
2629 * @param mem_ctx Talloc context, destoyed on completion of the function.
2630 * @param argc Standard main() style argc
2631 * @param argv Standard main() style argv. Initial components are already
2632 * stripped
2634 * @return Normal NTSTATUS return.
2636 static NTSTATUS
2637 rpc_share_migrate_shares_internals(const DOM_SID *domain_sid, const char *domain_name,
2638 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2639 int argc, const char **argv)
2641 WERROR result;
2642 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2643 SRV_SHARE_INFO_CTR ctr_src;
2644 ENUM_HND hnd;
2645 uint32 type = 0; /* only allow disk shares to be added */
2646 uint32 num_uses = 0, perms = 0, max_uses = 0;
2647 char *password = NULL; /* don't allow a share password */
2648 uint32 preferred_len = 0xffffffff, i;
2649 BOOL got_dst_srvsvc_pipe = False;
2650 struct cli_state *cli_dst = NULL;
2651 uint32 level = 502; /* includes secdesc */
2652 SEC_DESC *share_sd = NULL;
2654 init_enum_hnd(&hnd, 0);
2656 result = cli_srvsvc_net_share_enum(
2657 cli, mem_ctx, level, &ctr_src, preferred_len, &hnd);
2658 if (!W_ERROR_IS_OK(result))
2659 goto done;
2661 /* connect local PI_SRVSVC */
2662 nt_status = connect_pipe(&cli_dst, PI_SRVSVC, &got_dst_srvsvc_pipe);
2663 if (!NT_STATUS_IS_OK(nt_status))
2664 return nt_status;
2667 for (i = 0; i < ctr_src.num_entries; i++) {
2669 fstring netname = "", remark = "", path = "";
2670 /* reset error-code */
2671 nt_status = NT_STATUS_UNSUCCESSFUL;
2673 rpcstr_pull_unistr2_fstring(
2674 netname, &ctr_src.share.info502[i].info_502_str.uni_netname);
2675 rpcstr_pull_unistr2_fstring(
2676 remark, &ctr_src.share.info502[i].info_502_str.uni_remark);
2677 rpcstr_pull_unistr2_fstring(
2678 path, &ctr_src.share.info502[i].info_502_str.uni_path);
2679 num_uses = ctr_src.share.info502[i].info_502.num_uses;
2680 max_uses = ctr_src.share.info502[i].info_502.max_uses;
2681 perms = ctr_src.share.info502[i].info_502.perms;
2684 if (opt_acls)
2685 share_sd = dup_sec_desc(
2686 mem_ctx, ctr_src.share.info502[i].info_502_str.sd);
2688 /* since we do not have NetShareGetInfo implemented in samba3 we
2689 only can skip inside the enum-ctr_src */
2690 if (argc == 1) {
2691 char *one_share = talloc_strdup(mem_ctx, argv[0]);
2692 if (!strequal(netname, one_share))
2693 continue;
2696 /* skip builtin shares */
2697 /* FIXME: should print$ be added too ? */
2698 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
2699 strequal(netname,"global"))
2700 continue;
2702 if (opt_exclude && in_list(netname, opt_exclude, False)) {
2703 printf("excluding [%s]\n", netname);
2704 continue;
2707 /* only work with file-shares */
2708 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
2709 d_printf("skipping [%s]: not a file share.\n", netname);
2710 continue;
2713 if (!cli_tdis(cli))
2714 goto done;
2717 /* finallly add the share on the dst server
2718 please note that samba currently does not allow to
2719 add a share without existing directory */
2721 printf("migrating: [%s], path: %s, comment: %s, %s share-ACLs\n",
2722 netname, path, remark, opt_acls ? "including" : "without" );
2724 if (opt_verbose && opt_acls)
2725 display_sec_desc(share_sd);
2727 result = cli_srvsvc_net_share_add(cli_dst, mem_ctx, netname, type,
2728 remark, perms, max_uses,
2729 num_uses, path, password,
2730 level, share_sd);
2732 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
2733 printf(" [%s] does already exist\n", netname);
2734 continue;
2737 if (!W_ERROR_IS_OK(result)) {
2738 printf("cannot add share: %s\n", dos_errstr(result));
2739 goto done;
2744 nt_status = NT_STATUS_OK;
2746 done:
2747 if (got_dst_srvsvc_pipe) {
2748 cli_nt_session_close(cli_dst);
2749 cli_shutdown(cli_dst);
2752 return nt_status;
2756 /**
2757 * Migrate shares from a rpc-server to another
2759 * @param argc Standard main() style argc
2760 * @param argv Standard main() style argv. Initial components are already
2761 * stripped
2763 * @return A shell status integer (0 for success)
2765 static int rpc_share_migrate_shares(int argc, const char **argv)
2768 if (!opt_host) {
2769 printf("no server to migrate\n");
2770 return -1;
2773 return run_rpc_command(NULL, PI_SRVSVC, 0,
2774 rpc_share_migrate_shares_internals,
2775 argc, argv);
2778 typedef struct copy_clistate {
2779 TALLOC_CTX *mem_ctx;
2780 struct cli_state *cli_share_src;
2781 struct cli_state *cli_share_dst;
2782 const char *cwd;
2783 } copy_clistate;
2787 * Copy a file/dir
2789 * @param f file_info
2790 * @param mask current search mask
2791 * @param state arg-pointer
2794 static void copy_fn(const char *mnt, file_info *f, const char *mask, void *state)
2796 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2797 struct copy_clistate *local_state = (struct copy_clistate *)state;
2798 fstring filename, new_mask, dir;
2800 if (strequal(f->name, ".") || strequal(f->name, ".."))
2801 return;
2803 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
2805 /* DIRECTORY */
2806 if (f->mode & aDIR) {
2808 DEBUG(3,("got dir: %s\n", f->name));
2810 fstrcpy(dir, local_state->cwd);
2811 fstrcat(dir, "\\");
2812 fstrcat(dir, f->name);
2814 /* create that directory */
2815 nt_status = net_copy_file(local_state->mem_ctx,
2816 local_state->cli_share_src,
2817 local_state->cli_share_dst,
2818 dir, dir,
2819 opt_acls? True : False,
2820 opt_attrs? True : False,
2821 opt_timestamps? True : False,
2822 False);
2824 if (!NT_STATUS_IS_OK(nt_status))
2825 printf("could not copy dir %s: %s\n",
2826 dir, nt_errstr(nt_status));
2828 /* search below that directory */
2829 fstrcpy(new_mask, dir);
2830 fstrcat(new_mask, "\\*");
2832 if (!sync_files(local_state->mem_ctx,
2833 local_state->cli_share_src,
2834 local_state->cli_share_dst,
2835 new_mask, dir))
2837 printf("could not sync files\n");
2839 return;
2843 /* FILE */
2844 fstrcpy(filename, local_state->cwd);
2845 fstrcat(filename, "\\");
2846 fstrcat(filename, f->name);
2848 DEBUG(3,("got file: %s\n", filename));
2850 nt_status = net_copy_file(local_state->mem_ctx,
2851 local_state->cli_share_src,
2852 local_state->cli_share_dst,
2853 filename, filename,
2854 opt_acls? True : False,
2855 opt_attrs? True : False,
2856 opt_timestamps? True: False,
2857 True);
2859 if (!NT_STATUS_IS_OK(nt_status))
2860 printf("could not copy file %s: %s\n",
2861 filename, nt_errstr(nt_status));
2866 * sync files, can be called recursivly to list files
2867 * and then call copy_fn for each file
2869 * @param mem_ctx TALLOC_CTX
2870 * @param cli_share_src a connected share on the originating server
2871 * @param cli_share_dst a connected share on the destination server
2872 * @param mask the current search mask
2873 * @param cwd the current path
2875 * @return Boolean result
2877 BOOL sync_files(TALLOC_CTX *mem_ctx,
2878 struct cli_state *cli_share_src,
2879 struct cli_state *cli_share_dst,
2880 pstring mask, fstring cwd)
2884 uint16 attribute = aSYSTEM | aHIDDEN | aDIR;
2885 struct copy_clistate clistate;
2887 clistate.mem_ctx = mem_ctx;
2888 clistate.cli_share_src = cli_share_src;
2889 clistate.cli_share_dst = cli_share_dst;
2890 clistate.cwd = cwd;
2892 DEBUG(3,("calling cli_list with mask: %s\n", mask));
2894 if (cli_list(cli_share_src, mask, attribute, copy_fn, &clistate) == -1) {
2895 d_printf("listing %s failed with error: %s\n",
2896 mask, cli_errstr(cli_share_src));
2897 return False;
2900 return True;
2904 /**
2905 * Sync all files inside a remote share to another share (over smb)
2907 * All parameters are provided by the run_rpc_command function, except for
2908 * argc, argv which are passes through.
2910 * @param domain_sid The domain sid acquired from the remote server
2911 * @param cli A cli_state connected to the server.
2912 * @param mem_ctx Talloc context, destoyed on completion of the function.
2913 * @param argc Standard main() style argc
2914 * @param argv Standard main() style argv. Initial components are already
2915 * stripped
2917 * @return Normal NTSTATUS return.
2919 static NTSTATUS
2920 rpc_share_migrate_files_internals(const DOM_SID *domain_sid, const char *domain_name,
2921 struct cli_state *cli, TALLOC_CTX *mem_ctx,
2922 int argc, const char **argv)
2924 WERROR result;
2925 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
2926 SRV_SHARE_INFO_CTR ctr_src;
2927 ENUM_HND hnd;
2928 uint32 preferred_len = 0xffffffff, i;
2929 uint32 level = 2;
2930 struct cli_state *cli_share_src = NULL;
2931 struct cli_state *cli_share_dst = NULL;
2932 BOOL got_src_share = False;
2933 BOOL got_dst_share = False;
2934 pstring mask;
2935 char *dst = NULL;
2937 dst = SMB_STRDUP(opt_destination?opt_destination:"127.0.0.1");
2939 init_enum_hnd(&hnd, 0);
2941 result = cli_srvsvc_net_share_enum(
2942 cli, mem_ctx, level, &ctr_src, preferred_len, &hnd);
2944 if (!W_ERROR_IS_OK(result))
2945 goto done;
2947 for (i = 0; i < ctr_src.num_entries; i++) {
2949 fstring netname = "", remark = "", path = "";
2951 rpcstr_pull_unistr2_fstring(
2952 netname, &ctr_src.share.info2[i].info_2_str.uni_netname);
2953 rpcstr_pull_unistr2_fstring(
2954 remark, &ctr_src.share.info2[i].info_2_str.uni_remark);
2955 rpcstr_pull_unistr2_fstring(
2956 path, &ctr_src.share.info2[i].info_2_str.uni_path);
2958 /* since we do not have NetShareGetInfo implemented in samba3 we
2959 only can skip inside the enum-ctr_src */
2960 if (argc == 1) {
2961 char *one_share = talloc_strdup(mem_ctx, argv[0]);
2962 if (!strequal(netname, one_share))
2963 continue;
2966 /* skip builtin and hidden shares
2967 In particular, one might not want to mirror whole discs :) */
2968 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$"))
2969 continue;
2971 if (strequal(netname, "print$") || netname[1] == '$') {
2972 d_printf("skipping [%s]: builtin/hidden share\n", netname);
2973 continue;
2976 if (opt_exclude && in_list(netname, opt_exclude, False)) {
2977 printf("excluding [%s]\n", netname);
2978 continue;
2981 /* only work with file-shares */
2982 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
2983 d_printf("skipping [%s]: not a file share.\n", netname);
2984 continue;
2987 if (!cli_tdis(cli))
2988 return NT_STATUS_UNSUCCESSFUL;
2990 printf("syncing [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
2991 netname,
2992 opt_acls ? "including" : "without",
2993 opt_attrs ? "including" : "without",
2994 opt_timestamps ? "(preserving timestamps)" : "");
2997 /* open share source */
2998 nt_status = connect_to_service(&cli_share_src, &cli->dest_ip,
2999 cli->desthost, netname, "A:");
3000 if (!NT_STATUS_IS_OK(nt_status))
3001 goto done;
3003 got_src_share = True;
3006 /* open share destination */
3007 nt_status = connect_to_service(&cli_share_dst, NULL,
3008 dst, netname, "A:");
3009 if (!NT_STATUS_IS_OK(nt_status))
3010 goto done;
3012 got_dst_share = True;
3015 /* now call the filesync */
3016 pstrcpy(mask, "\\*");
3018 if (!sync_files(mem_ctx, cli_share_src, cli_share_dst, mask, NULL)) {
3019 d_printf("could not sync files for share: %s\n", netname);
3020 nt_status = NT_STATUS_UNSUCCESSFUL;
3021 goto done;
3026 nt_status = NT_STATUS_OK;
3028 done:
3030 if (got_src_share)
3031 cli_shutdown(cli_share_src);
3033 if (got_dst_share)
3034 cli_shutdown(cli_share_dst);
3036 return nt_status;
3040 static int rpc_share_migrate_files(int argc, const char **argv)
3043 if (!opt_host) {
3044 printf("no server to migrate\n");
3045 return -1;
3048 return run_rpc_command(NULL, PI_SRVSVC, 0,
3049 rpc_share_migrate_files_internals,
3050 argc, argv);
3053 /**
3054 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3055 * from one server to another
3057 * @param argc Standard main() style argc
3058 * @param argv Standard main() style argv. Initial components are already
3059 * stripped
3061 * @return A shell status integer (0 for success)
3064 static int rpc_share_migrate_all(int argc, const char **argv)
3066 int ret;
3068 if (!opt_host) {
3069 printf("no server to migrate\n");
3070 return -1;
3073 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_internals, argc, argv);
3074 if (ret)
3075 return ret;
3076 #if 0
3077 ret = run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_shares_security_internals, argc, argv);
3078 if (ret)
3079 return ret;
3080 #endif
3081 return run_rpc_command(NULL, PI_SRVSVC, 0, rpc_share_migrate_files_internals, argc, argv);
3085 /**
3086 * 'net rpc share migrate' entrypoint.
3087 * @param argc Standard main() style argc
3088 * @param argv Standard main() style argv. Initial components are already
3089 * stripped
3091 static int rpc_share_migrate(int argc, const char **argv)
3094 struct functable func[] = {
3095 {"all", rpc_share_migrate_all},
3096 {"files", rpc_share_migrate_files},
3097 {"help", rpc_share_usage},
3098 /* {"security", rpc_share_migrate_security},*/
3099 {"shares", rpc_share_migrate_shares},
3100 {NULL, NULL}
3103 return net_run_function(argc, argv, func, rpc_share_usage);
3106 struct full_alias {
3107 DOM_SID sid;
3108 int num_members;
3109 DOM_SID *members;
3112 static int num_server_aliases;
3113 static struct full_alias *server_aliases;
3116 * Add an alias to the static list.
3118 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3120 if (server_aliases == NULL)
3121 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3123 server_aliases[num_server_aliases] = *alias;
3124 num_server_aliases += 1;
3128 * For a specific domain on the server, fetch all the aliases
3129 * and their members. Add all of them to the server_aliases.
3131 static NTSTATUS
3132 rpc_fetch_domain_aliases(struct cli_state *cli, TALLOC_CTX *mem_ctx,
3133 POLICY_HND *connect_pol,
3134 const DOM_SID *domain_sid)
3136 uint32 start_idx, max_entries, num_entries, i;
3137 struct acct_info *groups;
3138 NTSTATUS result;
3139 POLICY_HND domain_pol;
3141 /* Get domain policy handle */
3143 result = cli_samr_open_domain(cli, mem_ctx, connect_pol,
3144 MAXIMUM_ALLOWED_ACCESS,
3145 domain_sid, &domain_pol);
3146 if (!NT_STATUS_IS_OK(result))
3147 return result;
3149 start_idx = 0;
3150 max_entries = 250;
3152 do {
3153 result = cli_samr_enum_als_groups(cli, mem_ctx, &domain_pol,
3154 &start_idx, max_entries,
3155 &groups, &num_entries);
3157 for (i = 0; i < num_entries; i++) {
3159 POLICY_HND alias_pol;
3160 struct full_alias alias;
3161 DOM_SID *members;
3162 int j;
3164 result = cli_samr_open_alias(cli, mem_ctx, &domain_pol,
3165 MAXIMUM_ALLOWED_ACCESS,
3166 groups[i].rid,
3167 &alias_pol);
3168 if (!NT_STATUS_IS_OK(result))
3169 goto done;
3171 result = cli_samr_query_aliasmem(cli, mem_ctx,
3172 &alias_pol,
3173 &alias.num_members,
3174 &members);
3175 if (!NT_STATUS_IS_OK(result))
3176 goto done;
3178 result = cli_samr_close(cli, mem_ctx, &alias_pol);
3179 if (!NT_STATUS_IS_OK(result))
3180 goto done;
3182 alias.members = NULL;
3184 if (alias.num_members > 0) {
3185 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3187 for (j = 0; j < alias.num_members; j++)
3188 sid_copy(&alias.members[j],
3189 &members[j]);
3192 sid_copy(&alias.sid, domain_sid);
3193 sid_append_rid(&alias.sid, groups[i].rid);
3195 push_alias(mem_ctx, &alias);
3197 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3199 result = NT_STATUS_OK;
3201 done:
3202 cli_samr_close(cli, mem_ctx, &domain_pol);
3204 return result;
3208 * Dump server_aliases as names for debugging purposes.
3210 static NTSTATUS
3211 rpc_aliaslist_dump(const DOM_SID *domain_sid, const char *domain_name,
3212 struct cli_state *cli, TALLOC_CTX *mem_ctx,
3213 int argc, const char **argv)
3215 int i;
3216 NTSTATUS result;
3217 POLICY_HND lsa_pol;
3219 result = cli_lsa_open_policy(cli, mem_ctx, True,
3220 SEC_RIGHTS_MAXIMUM_ALLOWED,
3221 &lsa_pol);
3222 if (!NT_STATUS_IS_OK(result))
3223 return result;
3225 for (i=0; i<num_server_aliases; i++) {
3226 char **names;
3227 char **domains;
3228 uint32 *types;
3229 int j;
3231 struct full_alias *alias = &server_aliases[i];
3233 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol, 1,
3234 &alias->sid,
3235 &domains, &names, &types);
3236 if (!NT_STATUS_IS_OK(result))
3237 continue;
3239 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3241 if (alias->num_members == 0) {
3242 DEBUG(1, ("\n"));
3243 continue;
3246 result = cli_lsa_lookup_sids(cli, mem_ctx, &lsa_pol,
3247 alias->num_members,
3248 alias->members,
3249 &domains, &names, &types);
3251 if (!NT_STATUS_IS_OK(result) &&
3252 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3253 continue;
3255 for (j=0; j<alias->num_members; j++)
3256 DEBUG(1, ("%s\\%s (%d); ",
3257 domains[j] ? domains[j] : "*unknown*",
3258 names[j] ? names[j] : "*unknown*",types[j]));
3259 DEBUG(1, ("\n"));
3262 cli_lsa_close(cli, mem_ctx, &lsa_pol);
3264 return NT_STATUS_OK;
3268 * Fetch a list of all server aliases and their members into
3269 * server_aliases.
3271 static NTSTATUS
3272 rpc_aliaslist_internals(const DOM_SID *domain_sid, const char *domain_name,
3273 struct cli_state *cli, TALLOC_CTX *mem_ctx,
3274 int argc, const char **argv)
3276 NTSTATUS result;
3277 POLICY_HND connect_pol;
3279 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
3280 &connect_pol);
3282 if (!NT_STATUS_IS_OK(result))
3283 goto done;
3285 result = rpc_fetch_domain_aliases(cli, mem_ctx, &connect_pol,
3286 &global_sid_Builtin);
3288 if (!NT_STATUS_IS_OK(result))
3289 goto done;
3291 result = rpc_fetch_domain_aliases(cli, mem_ctx, &connect_pol,
3292 domain_sid);
3294 cli_samr_close(cli, mem_ctx, &connect_pol);
3295 done:
3296 return result;
3299 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
3301 token->num_sids = 4;
3303 token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4);
3305 token->user_sids[0] = *user_sid;
3306 sid_copy(&token->user_sids[1], &global_sid_World);
3307 sid_copy(&token->user_sids[2], &global_sid_Network);
3308 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
3311 static void free_user_token(NT_USER_TOKEN *token)
3313 SAFE_FREE(token->user_sids);
3316 static BOOL is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
3318 int i;
3320 for (i=0; i<token->num_sids; i++) {
3321 if (sid_compare(sid, &token->user_sids[i]) == 0)
3322 return True;
3324 return False;
3327 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
3329 if (is_sid_in_token(token, sid))
3330 return;
3332 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
3334 sid_copy(&token->user_sids[token->num_sids], sid);
3336 token->num_sids += 1;
3339 struct user_token {
3340 fstring name;
3341 NT_USER_TOKEN token;
3344 static void dump_user_token(struct user_token *token)
3346 int i;
3348 d_printf("%s\n", token->name);
3350 for (i=0; i<token->token.num_sids; i++) {
3351 d_printf(" %s\n", sid_string_static(&token->token.user_sids[i]));
3355 static BOOL is_alias_member(DOM_SID *sid, struct full_alias *alias)
3357 int i;
3359 for (i=0; i<alias->num_members; i++) {
3360 if (sid_compare(sid, &alias->members[i]) == 0)
3361 return True;
3364 return False;
3367 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
3369 int i;
3371 for (i=0; i<num_server_aliases; i++) {
3372 if (is_alias_member(&sid, &server_aliases[i]))
3373 add_sid_to_token(token, &server_aliases[i].sid);
3378 * We got a user token with all the SIDs we can know about without asking the
3379 * server directly. These are the user and domain group sids. All of these can
3380 * be members of aliases. So scan the list of aliases for each of the SIDs and
3381 * add them to the token.
3384 static void collect_alias_memberships(NT_USER_TOKEN *token)
3386 int num_global_sids = token->num_sids;
3387 int i;
3389 for (i=0; i<num_global_sids; i++) {
3390 collect_sid_memberships(token, token->user_sids[i]);
3394 static BOOL get_user_sids(const char *domain, const char *user,
3395 NT_USER_TOKEN *token)
3397 struct winbindd_request request;
3398 struct winbindd_response response;
3399 fstring full_name;
3400 NSS_STATUS result;
3402 DOM_SID user_sid;
3404 int i;
3406 fstr_sprintf(full_name, "%s%c%s",
3407 domain, *lp_winbind_separator(), user);
3409 /* First let's find out the user sid */
3411 ZERO_STRUCT(request);
3412 ZERO_STRUCT(response);
3414 fstrcpy(request.data.name.dom_name, domain);
3415 fstrcpy(request.data.name.name, user);
3417 result = winbindd_request(WINBINDD_LOOKUPNAME, &request, &response);
3419 if (result != NSS_STATUS_SUCCESS) {
3420 DEBUG(1, ("winbind could not find %s\n", full_name));
3421 return False;
3424 if (response.data.sid.type != SID_NAME_USER) {
3425 DEBUG(1, ("%s is not a user\n", full_name));
3426 return False;
3429 string_to_sid(&user_sid, response.data.sid.sid);
3431 init_user_token(token, &user_sid);
3433 /* And now the groups winbind knows about */
3435 ZERO_STRUCT(response);
3437 fstrcpy(request.data.username, full_name);
3439 result = winbindd_request(WINBINDD_GETGROUPS, &request, &response);
3441 if (result != NSS_STATUS_SUCCESS) {
3442 DEBUG(1, ("winbind could not get groups of %s\n", full_name));
3443 return False;
3446 for (i = 0; i < response.data.num_entries; i++) {
3447 gid_t gid = ((gid_t *)response.extra_data)[i];
3448 DOM_SID sid;
3450 struct winbindd_request sidrequest;
3451 struct winbindd_response sidresponse;
3453 ZERO_STRUCT(sidrequest);
3454 ZERO_STRUCT(sidresponse);
3456 sidrequest.data.gid = gid;
3458 result = winbindd_request(WINBINDD_GID_TO_SID,
3459 &sidrequest, &sidresponse);
3461 if (result != NSS_STATUS_SUCCESS) {
3462 DEBUG(1, ("winbind could not find SID of gid %d\n",
3463 gid));
3464 return False;
3467 DEBUG(3, (" %s\n", sidresponse.data.sid.sid));
3469 string_to_sid(&sid, sidresponse.data.sid.sid);
3470 add_sid_to_token(token, &sid);
3473 SAFE_FREE(response.extra_data);
3475 return True;
3479 * Get a list of all user tokens we want to look at
3481 static BOOL get_user_tokens(int *num_tokens, struct user_token **user_tokens)
3483 struct winbindd_request request;
3484 struct winbindd_response response;
3485 const char *extra_data;
3486 fstring name;
3487 int i;
3488 struct user_token *result;
3490 /* Send request to winbind daemon */
3492 ZERO_STRUCT(request);
3493 ZERO_STRUCT(response);
3495 if (winbindd_request(WINBINDD_LIST_USERS, &request, &response) !=
3496 NSS_STATUS_SUCCESS)
3497 return False;
3499 /* Look through extra data */
3501 if (!response.extra_data)
3502 return False;
3504 extra_data = (const char *)response.extra_data;
3505 *num_tokens = 0;
3507 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
3508 *num_tokens += 1;
3511 result = SMB_MALLOC_ARRAY(struct user_token, *num_tokens);
3513 if (result == NULL) {
3514 DEBUG(1, ("Could not malloc sid array\n"));
3515 return False;
3518 extra_data = (const char *)response.extra_data;
3519 i=0;
3521 while(next_token(&extra_data, name, ",", sizeof(fstring))) {
3523 fstring domain, user;
3524 char *p;
3526 fstrcpy(result[i].name, name);
3528 p = strchr(name, *lp_winbind_separator());
3530 DEBUG(3, ("%s\n", name));
3532 if (p == NULL)
3533 continue;
3535 *p++ = '\0';
3537 fstrcpy(domain, name);
3538 strupper_m(domain);
3539 fstrcpy(user, p);
3541 get_user_sids(domain, user, &(result[i].token));
3542 i+=1;
3545 SAFE_FREE(response.extra_data);
3547 *user_tokens = result;
3549 return True;
3552 static BOOL get_user_tokens_from_file(FILE *f,
3553 int *num_tokens,
3554 struct user_token **tokens)
3556 struct user_token *token = NULL;
3558 while (!feof(f)) {
3559 fstring line;
3561 if (fgets(line, sizeof(line)-1, f) == NULL) {
3562 return True;
3565 if (line[strlen(line)-1] == '\n')
3566 line[strlen(line)-1] = '\0';
3568 if (line[0] == ' ') {
3569 /* We have a SID */
3571 DOM_SID sid;
3572 string_to_sid(&sid, &line[1]);
3574 if (token == NULL) {
3575 DEBUG(0, ("File does not begin with username"));
3576 return False;
3579 add_sid_to_token(&token->token, &sid);
3580 continue;
3583 /* And a new user... */
3585 *num_tokens += 1;
3586 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
3587 if (*tokens == NULL) {
3588 DEBUG(0, ("Could not realloc tokens\n"));
3589 return False;
3592 token = &((*tokens)[*num_tokens-1]);
3594 fstrcpy(token->name, line);
3595 token->token.num_sids = 0;
3596 token->token.user_sids = NULL;
3597 continue;
3600 return False;
3605 * Show the list of all users that have access to a share
3608 static void show_userlist(struct cli_state *cli,
3609 TALLOC_CTX *mem_ctx, const char *netname,
3610 int num_tokens, struct user_token *tokens)
3612 int fnum;
3613 SEC_DESC *share_sd = NULL;
3614 SEC_DESC *root_sd = NULL;
3615 int i;
3616 SRV_SHARE_INFO info;
3617 WERROR result;
3618 uint16 cnum;
3620 result = cli_srvsvc_net_share_get_info(cli, mem_ctx, netname,
3621 502, &info);
3623 if (!W_ERROR_IS_OK(result)) {
3624 DEBUG(1, ("Coult not query secdesc for share %s\n",
3625 netname));
3626 return;
3629 share_sd = info.share.info502.info_502_str.sd;
3630 if (share_sd == NULL) {
3631 DEBUG(1, ("Got no secdesc for share %s\n",
3632 netname));
3635 cnum = cli->cnum;
3637 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3638 return;
3641 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
3643 if (fnum != -1) {
3644 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
3647 for (i=0; i<num_tokens; i++) {
3648 uint32 acc_granted;
3649 NTSTATUS status;
3651 if (share_sd != NULL) {
3652 if (!se_access_check(share_sd, &tokens[i].token,
3653 1, &acc_granted, &status)) {
3654 DEBUG(1, ("Could not check share_sd for "
3655 "user %s\n",
3656 tokens[i].name));
3657 continue;
3660 if (!NT_STATUS_IS_OK(status))
3661 continue;
3664 if (root_sd == NULL) {
3665 d_printf(" %s\n", tokens[i].name);
3666 continue;
3669 if (!se_access_check(root_sd, &tokens[i].token,
3670 1, &acc_granted, &status)) {
3671 DEBUG(1, ("Could not check root_sd for user %s\n",
3672 tokens[i].name));
3673 continue;
3676 if (!NT_STATUS_IS_OK(status))
3677 continue;
3679 d_printf(" %s\n", tokens[i].name);
3682 if (fnum != -1)
3683 cli_close(cli, fnum);
3684 cli_tdis(cli);
3685 cli->cnum = cnum;
3687 return;
3690 struct share_list {
3691 int num_shares;
3692 char **shares;
3695 static void collect_share(const char *name, uint32 m,
3696 const char *comment, void *state)
3698 struct share_list *share_list = (struct share_list *)state;
3700 if (m != STYPE_DISKTREE)
3701 return;
3703 share_list->num_shares += 1;
3704 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
3705 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
3708 static void rpc_share_userlist_usage(void)
3710 return;
3713 /**
3714 * List shares on a remote RPC server, including the security descriptors
3716 * All parameters are provided by the run_rpc_command function, except for
3717 * argc, argv which are passes through.
3719 * @param domain_sid The domain sid acquired from the remote server
3720 * @param cli A cli_state connected to the server.
3721 * @param mem_ctx Talloc context, destoyed on completion of the function.
3722 * @param argc Standard main() style argc
3723 * @param argv Standard main() style argv. Initial components are already
3724 * stripped
3726 * @return Normal NTSTATUS return.
3729 static NTSTATUS
3730 rpc_share_allowedusers_internals(const DOM_SID *domain_sid,
3731 const char *domain_name,
3732 struct cli_state *cli,
3733 TALLOC_CTX *mem_ctx,
3734 int argc, const char **argv)
3736 int ret;
3737 BOOL r;
3738 ENUM_HND hnd;
3739 uint32 i;
3740 FILE *f;
3742 struct user_token *tokens = NULL;
3743 int num_tokens = 0;
3745 struct share_list share_list;
3747 if (argc > 1) {
3748 rpc_share_userlist_usage();
3749 return NT_STATUS_UNSUCCESSFUL;
3752 if (argc == 0) {
3753 f = stdin;
3754 } else {
3755 f = fopen(argv[0], "r");
3758 if (f == NULL) {
3759 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
3760 return NT_STATUS_UNSUCCESSFUL;
3763 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
3765 if (f != stdin)
3766 fclose(f);
3768 if (!r) {
3769 DEBUG(0, ("Could not read users from file\n"));
3770 return NT_STATUS_UNSUCCESSFUL;
3773 for (i=0; i<num_tokens; i++)
3774 collect_alias_memberships(&tokens[i].token);
3776 init_enum_hnd(&hnd, 0);
3778 share_list.num_shares = 0;
3779 share_list.shares = NULL;
3781 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
3783 if (ret == -1) {
3784 DEBUG(0, ("Error returning browse list: %s\n",
3785 cli_errstr(cli)));
3786 goto done;
3789 for (i = 0; i < share_list.num_shares; i++) {
3790 char *netname = share_list.shares[i];
3792 if (netname[strlen(netname)-1] == '$')
3793 continue;
3795 d_printf("%s\n", netname);
3797 show_userlist(cli, mem_ctx, netname,
3798 num_tokens, tokens);
3800 done:
3801 for (i=0; i<num_tokens; i++) {
3802 free_user_token(&tokens[i].token);
3804 SAFE_FREE(tokens);
3805 SAFE_FREE(share_list.shares);
3807 return NT_STATUS_OK;
3810 static int
3811 rpc_share_allowedusers(int argc, const char **argv)
3813 int result;
3815 result = run_rpc_command(NULL, PI_SAMR, 0,
3816 rpc_aliaslist_internals,
3817 argc, argv);
3818 if (result != 0)
3819 return result;
3821 result = run_rpc_command(NULL, PI_LSARPC, 0,
3822 rpc_aliaslist_dump,
3823 argc, argv);
3824 if (result != 0)
3825 return result;
3827 return run_rpc_command(NULL, PI_SRVSVC, 0,
3828 rpc_share_allowedusers_internals,
3829 argc, argv);
3832 int net_usersidlist(int argc, const char **argv)
3834 int num_tokens = 0;
3835 struct user_token *tokens = NULL;
3836 int i;
3838 if (argc != 0) {
3839 net_usersidlist_usage(argc, argv);
3840 return 0;
3843 if (!get_user_tokens(&num_tokens, &tokens)) {
3844 DEBUG(0, ("Could not get the user/sid list\n"));
3845 return 0;
3848 for (i=0; i<num_tokens; i++) {
3849 dump_user_token(&tokens[i]);
3850 free_user_token(&tokens[i].token);
3853 SAFE_FREE(tokens);
3854 return 1;
3857 int net_usersidlist_usage(int argc, const char **argv)
3859 d_printf("net usersidlist\n"
3860 "\tprints out a list of all users the running winbind knows\n"
3861 "\tabout, together with all their SIDs. This is used as\n"
3862 "\tinput to the 'net rpc share allowedusers' command.\n\n");
3864 net_common_flags_usage(argc, argv);
3865 return -1;
3868 /**
3869 * 'net rpc share' entrypoint.
3870 * @param argc Standard main() style argc
3871 * @param argv Standard main() style argv. Initial components are already
3872 * stripped
3875 int net_rpc_share(int argc, const char **argv)
3877 struct functable func[] = {
3878 {"add", rpc_share_add},
3879 {"delete", rpc_share_delete},
3880 {"allowedusers", rpc_share_allowedusers},
3881 {"migrate", rpc_share_migrate},
3882 {NULL, NULL}
3885 if (argc == 0)
3886 return run_rpc_command(NULL, PI_SRVSVC, 0,
3887 rpc_share_list_internals,
3888 argc, argv);
3890 return net_run_function(argc, argv, func, rpc_share_usage);
3893 /****************************************************************************/
3895 static int rpc_file_usage(int argc, const char **argv)
3897 return net_help_file(argc, argv);
3900 /**
3901 * Close a file on a remote RPC server
3903 * All parameters are provided by the run_rpc_command function, except for
3904 * argc, argv which are passes through.
3906 * @param domain_sid The domain sid acquired from the remote server
3907 * @param cli A cli_state connected to the server.
3908 * @param mem_ctx Talloc context, destoyed on completion of the function.
3909 * @param argc Standard main() style argc
3910 * @param argv Standard main() style argv. Initial components are already
3911 * stripped
3913 * @return Normal NTSTATUS return.
3915 static NTSTATUS
3916 rpc_file_close_internals(const DOM_SID *domain_sid, const char *domain_name,
3917 struct cli_state *cli,
3918 TALLOC_CTX *mem_ctx, int argc, const char **argv)
3920 WERROR result;
3921 result = cli_srvsvc_net_file_close(cli, mem_ctx, atoi(argv[0]));
3922 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3925 /**
3926 * Close a file on a remote RPC server
3928 * @param argc Standard main() style argc
3929 * @param argv Standard main() style argv. Initial components are already
3930 * stripped
3932 * @return A shell status integer (0 for success)
3934 static int rpc_file_close(int argc, const char **argv)
3936 if (argc < 1) {
3937 DEBUG(1, ("No fileid given on close\n"));
3938 return(rpc_file_usage(argc, argv));
3941 return run_rpc_command(NULL, PI_SRVSVC, 0,
3942 rpc_file_close_internals,
3943 argc, argv);
3946 /**
3947 * Formatted print of open file info
3949 * @param info3 FILE_INFO_3 contents
3950 * @param str3 strings for FILE_INFO_3
3953 static void display_file_info_3(FILE_INFO_3 *info3, FILE_INFO_3_STR *str3)
3955 fstring user = "", path = "";
3957 rpcstr_pull_unistr2_fstring(user, &str3->uni_user_name);
3958 rpcstr_pull_unistr2_fstring(path, &str3->uni_path_name);
3960 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
3961 info3->id, user, info3->perms, info3->num_locks, path);
3964 /**
3965 * List open files on a remote RPC server
3967 * All parameters are provided by the run_rpc_command function, except for
3968 * argc, argv which are passes through.
3970 * @param domain_sid The domain sid acquired from the remote server
3971 * @param cli A cli_state connected to the server.
3972 * @param mem_ctx Talloc context, destoyed on completion of the function.
3973 * @param argc Standard main() style argc
3974 * @param argv Standard main() style argv. Initial components are already
3975 * stripped
3977 * @return Normal NTSTATUS return.
3980 static NTSTATUS
3981 rpc_file_list_internals(const DOM_SID *domain_sid, const char *domain_name,
3982 struct cli_state *cli,
3983 TALLOC_CTX *mem_ctx, int argc, const char **argv)
3985 SRV_FILE_INFO_CTR ctr;
3986 WERROR result;
3987 ENUM_HND hnd;
3988 uint32 preferred_len = 0xffffffff, i;
3989 const char *username=NULL;
3991 init_enum_hnd(&hnd, 0);
3993 /* if argc > 0, must be user command */
3994 if (argc > 0)
3995 username = smb_xstrdup(argv[0]);
3997 result = cli_srvsvc_net_file_enum(
3998 cli, mem_ctx, 3, username, &ctr, preferred_len, &hnd);
4000 if (!W_ERROR_IS_OK(result))
4001 goto done;
4003 /* Display results */
4005 d_printf(
4006 "\nEnumerating open files on remote server:\n\n"\
4007 "\nFileId Opened by Perms Locks Path"\
4008 "\n------ --------- ----- ----- ---- \n");
4009 for (i = 0; i < ctr.num_entries; i++)
4010 display_file_info_3(&ctr.file.info3[i].info_3,
4011 &ctr.file.info3[i].info_3_str);
4012 done:
4013 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4017 /**
4018 * List files for a user on a remote RPC server
4020 * @param argc Standard main() style argc
4021 * @param argv Standard main() style argv. Initial components are already
4022 * stripped
4024 * @return A shell status integer (0 for success)
4026 static int rpc_file_user(int argc, const char **argv)
4028 if (argc < 1) {
4029 DEBUG(1, ("No username given\n"));
4030 return(rpc_file_usage(argc, argv));
4033 return run_rpc_command(NULL, PI_SRVSVC, 0,
4034 rpc_file_list_internals,
4035 argc, argv);
4039 /**
4040 * 'net rpc file' entrypoint.
4041 * @param argc Standard main() style argc
4042 * @param argv Standard main() style argv. Initial components are already
4043 * stripped
4046 int net_rpc_file(int argc, const char **argv)
4048 struct functable func[] = {
4049 {"close", rpc_file_close},
4050 {"user", rpc_file_user},
4051 #if 0
4052 {"info", rpc_file_info},
4053 #endif
4054 {NULL, NULL}
4057 if (argc == 0)
4058 return run_rpc_command(NULL, PI_SRVSVC, 0,
4059 rpc_file_list_internals,
4060 argc, argv);
4062 return net_run_function(argc, argv, func, rpc_file_usage);
4065 /****************************************************************************/
4069 /**
4070 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
4072 * All parameters are provided by the run_rpc_command function, except for
4073 * argc, argv which are passed through.
4075 * @param domain_sid The domain sid aquired from the remote server
4076 * @param cli A cli_state connected to the server.
4077 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4078 * @param argc Standard main() style argc
4079 * @param argv Standard main() style argv. Initial components are already
4080 * stripped
4082 * @return Normal NTSTATUS return.
4085 static NTSTATUS rpc_shutdown_abort_internals(const DOM_SID *domain_sid,
4086 const char *domain_name,
4087 struct cli_state *cli,
4088 TALLOC_CTX *mem_ctx,
4089 int argc, const char **argv)
4091 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4093 result = cli_shutdown_abort(cli, mem_ctx);
4095 if (NT_STATUS_IS_OK(result)) {
4096 d_printf("\nShutdown successfully aborted\n");
4097 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4098 } else
4099 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4101 return result;
4105 /**
4106 * ABORT the shutdown of a remote RPC Server, over winreg pipe
4108 * All parameters are provided by the run_rpc_command function, except for
4109 * argc, argv which are passed through.
4111 * @param domain_sid The domain sid aquired from the remote server
4112 * @param cli A cli_state connected to the server.
4113 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4114 * @param argc Standard main() style argc
4115 * @param argv Standard main() style argv. Initial components are already
4116 * stripped
4118 * @return Normal NTSTATUS return.
4121 static NTSTATUS rpc_reg_shutdown_abort_internals(const DOM_SID *domain_sid,
4122 const char *domain_name,
4123 struct cli_state *cli,
4124 TALLOC_CTX *mem_ctx,
4125 int argc, const char **argv)
4127 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4129 result = werror_to_ntstatus(cli_reg_abort_shutdown(cli, mem_ctx));
4131 if (NT_STATUS_IS_OK(result)) {
4132 d_printf("\nShutdown successfully aborted\n");
4133 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
4134 } else
4135 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
4137 return result;
4140 /**
4141 * ABORT the Shut down of a remote RPC server
4143 * @param argc Standard main() style argc
4144 * @param argv Standard main() style argv. Initial components are already
4145 * stripped
4147 * @return A shell status integer (0 for success)
4150 static int rpc_shutdown_abort(int argc, const char **argv)
4152 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
4153 rpc_shutdown_abort_internals,
4154 argc, argv);
4156 if (rc == 0)
4157 return rc;
4159 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4161 return run_rpc_command(NULL, PI_WINREG, 0,
4162 rpc_reg_shutdown_abort_internals,
4163 argc, argv);
4166 /**
4167 * Shut down a remote RPC Server via initshutdown pipe
4169 * All parameters are provided by the run_rpc_command function, except for
4170 * argc, argv which are passes through.
4172 * @param domain_sid The domain sid aquired from the remote server
4173 * @param cli A cli_state connected to the server.
4174 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4175 * @param argc Standard main() style argc
4176 * @param argc Standard main() style argv. Initial components are already
4177 * stripped
4179 * @return Normal NTSTATUS return.
4182 static NTSTATUS rpc_init_shutdown_internals(const DOM_SID *domain_sid,
4183 const char *domain_name,
4184 struct cli_state *cli,
4185 TALLOC_CTX *mem_ctx,
4186 int argc, const char **argv)
4188 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4189 const char *msg = "This machine will be shutdown shortly";
4190 uint32 timeout = 20;
4192 if (opt_comment) {
4193 msg = opt_comment;
4195 if (opt_timeout) {
4196 timeout = opt_timeout;
4199 /* create an entry */
4200 result = cli_shutdown_init(cli, mem_ctx, msg, timeout, opt_reboot,
4201 opt_force);
4203 if (NT_STATUS_IS_OK(result)) {
4204 d_printf("\nShutdown of remote machine succeeded\n");
4205 DEBUG(5,("Shutdown of remote machine succeeded\n"));
4206 } else
4207 DEBUG(0,("Shutdown of remote machine failed!\n"));
4209 return result;
4212 /**
4213 * Shut down a remote RPC Server via winreg pipe
4215 * All parameters are provided by the run_rpc_command function, except for
4216 * argc, argv which are passes through.
4218 * @param domain_sid The domain sid aquired from the remote server
4219 * @param cli A cli_state connected to the server.
4220 * @param mem_ctx Talloc context, destoyed on compleation of the function.
4221 * @param argc Standard main() style argc
4222 * @param argc Standard main() style argv. Initial components are already
4223 * stripped
4225 * @return Normal NTSTATUS return.
4228 static NTSTATUS rpc_reg_shutdown_internals(const DOM_SID *domain_sid,
4229 const char *domain_name,
4230 struct cli_state *cli,
4231 TALLOC_CTX *mem_ctx,
4232 int argc, const char **argv)
4234 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4235 const char *msg = "This machine will be shutdown shortly";
4236 uint32 timeout = 20;
4237 #if 0
4238 poptContext pc;
4239 int rc;
4241 struct poptOption long_options[] = {
4242 {"message", 'm', POPT_ARG_STRING, &msg},
4243 {"timeout", 't', POPT_ARG_INT, &timeout},
4244 {"reboot", 'r', POPT_ARG_NONE, &reboot},
4245 {"force", 'f', POPT_ARG_NONE, &force},
4246 { 0, 0, 0, 0}
4249 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
4250 POPT_CONTEXT_KEEP_FIRST);
4252 rc = poptGetNextOpt(pc);
4254 if (rc < -1) {
4255 /* an error occurred during option processing */
4256 DEBUG(0, ("%s: %s\n",
4257 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
4258 poptStrerror(rc)));
4259 return NT_STATUS_INVALID_PARAMETER;
4261 #endif
4262 if (opt_comment) {
4263 msg = opt_comment;
4265 if (opt_timeout) {
4266 timeout = opt_timeout;
4269 /* create an entry */
4270 result = werror_to_ntstatus(cli_reg_shutdown(cli, mem_ctx, msg, timeout, opt_reboot, opt_force));
4272 if (NT_STATUS_IS_OK(result)) {
4273 d_printf("\nShutdown of remote machine succeeded\n");
4274 DEBUG(5,("Shutdown of remote machine succeeded\n"));
4276 else
4277 DEBUG(0,("Shutdown of remote machine failed!\n"));
4279 return result;
4282 /**
4283 * Shut down a remote RPC server
4285 * @param argc Standard main() style argc
4286 * @param argc Standard main() style argv. Initial components are already
4287 * stripped
4289 * @return A shell status integer (0 for success)
4292 static int rpc_shutdown(int argc, const char **argv)
4294 int rc = run_rpc_command(NULL, PI_SHUTDOWN, 0,
4295 rpc_init_shutdown_internals,
4296 argc, argv);
4297 if (rc == 0)
4298 return rc;
4300 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4302 return run_rpc_command(NULL, PI_WINREG, 0, rpc_reg_shutdown_internals,
4303 argc, argv);
4306 /***************************************************************************
4307 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
4309 ***************************************************************************/
4312 * Add interdomain trust account to the RPC server.
4313 * All parameters (except for argc and argv) are passed by run_rpc_command
4314 * function.
4316 * @param domain_sid The domain sid acquired from the server
4317 * @param cli A cli_state connected to the server.
4318 * @param mem_ctx Talloc context, destoyed on completion of the function.
4319 * @param argc Standard main() style argc
4320 * @param argc Standard main() style argv. Initial components are already
4321 * stripped
4323 * @return normal NTSTATUS return code
4326 static NTSTATUS rpc_trustdom_add_internals(const DOM_SID *domain_sid,
4327 const char *domain_name,
4328 struct cli_state *cli, TALLOC_CTX *mem_ctx,
4329 int argc, const char **argv) {
4331 POLICY_HND connect_pol, domain_pol, user_pol;
4332 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4333 char *acct_name;
4334 uint16 acb_info;
4335 uint32 unknown, user_rid;
4337 if (argc != 2) {
4338 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
4339 return NT_STATUS_INVALID_PARAMETER;
4343 * Make valid trusting domain account (ie. uppercased and with '$' appended)
4346 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
4347 return NT_STATUS_NO_MEMORY;
4350 strupper_m(acct_name);
4352 /* Get samr policy handle */
4353 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4354 &connect_pol);
4355 if (!NT_STATUS_IS_OK(result)) {
4356 goto done;
4359 /* Get domain policy handle */
4360 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
4361 MAXIMUM_ALLOWED_ACCESS,
4362 domain_sid, &domain_pol);
4363 if (!NT_STATUS_IS_OK(result)) {
4364 goto done;
4367 /* Create trusting domain's account */
4368 acb_info = ACB_NORMAL;
4369 unknown = 0xe00500b0; /* No idea what this is - a permission mask?
4370 mimir: yes, most probably it is */
4372 result = cli_samr_create_dom_user(cli, mem_ctx, &domain_pol,
4373 acct_name, acb_info, unknown,
4374 &user_pol, &user_rid);
4375 if (!NT_STATUS_IS_OK(result)) {
4376 goto done;
4380 SAM_USERINFO_CTR ctr;
4381 SAM_USER_INFO_23 p23;
4382 NTTIME notime;
4383 char nostr[] = "";
4384 LOGON_HRS hrs;
4385 uchar pwbuf[516];
4387 encode_pw_buffer((char *)pwbuf, argv[1], STR_UNICODE);
4389 ZERO_STRUCT(ctr);
4390 ZERO_STRUCT(p23);
4391 ZERO_STRUCT(notime);
4392 hrs.max_len = 1260;
4393 hrs.offset = 0;
4394 hrs.len = 21;
4395 memset(hrs.hours, 0xFF, sizeof(hrs.hours));
4396 acb_info = ACB_DOMTRUST;
4398 init_sam_user_info23A(&p23, &notime, &notime, &notime,
4399 &notime, &notime, &notime,
4400 nostr, nostr, nostr, nostr, nostr,
4401 nostr, nostr, nostr, nostr, nostr,
4402 0, 0, acb_info, ACCT_FLAGS, 168, &hrs,
4403 0, 0, (char *)pwbuf);
4404 ctr.switch_value = 23;
4405 ctr.info.id23 = &p23;
4406 p23.passmustchange = 0;
4408 result = cli_samr_set_userinfo(cli, mem_ctx, &user_pol, 23,
4409 &cli->user_session_key, &ctr);
4411 if (!NT_STATUS_IS_OK(result)) {
4412 DEBUG(0,("Could not set trust account password: %s\n",
4413 nt_errstr(result)));
4414 goto done;
4418 done:
4419 SAFE_FREE(acct_name);
4420 return result;
4424 * Create interdomain trust account for a remote domain.
4426 * @param argc standard argc
4427 * @param argv standard argv without initial components
4429 * @return Integer status (0 means success)
4432 static int rpc_trustdom_add(int argc, const char **argv)
4434 if (argc > 0) {
4435 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_add_internals,
4436 argc, argv);
4437 } else {
4438 d_printf("Usage: net rpc trustdom add <domain>\n");
4439 return -1;
4444 * Remove interdomain trust account from the RPC server.
4445 * All parameters (except for argc and argv) are passed by run_rpc_command
4446 * function.
4448 * @param domain_sid The domain sid acquired from the server
4449 * @param cli A cli_state connected to the server.
4450 * @param mem_ctx Talloc context, destoyed on completion of the function.
4451 * @param argc Standard main() style argc
4452 * @param argc Standard main() style argv. Initial components are already
4453 * stripped
4455 * @return normal NTSTATUS return code
4458 static NTSTATUS rpc_trustdom_del_internals(const DOM_SID *domain_sid,
4459 const char *domain_name,
4460 struct cli_state *cli, TALLOC_CTX *mem_ctx,
4461 int argc, const char **argv) {
4463 POLICY_HND connect_pol, domain_pol, user_pol;
4464 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4465 char *acct_name;
4466 const char **names;
4467 DOM_SID trust_acct_sid;
4468 uint32 *user_rids, num_rids, *name_types;
4469 uint32 flags = 0x000003e8; /* Unknown */
4471 if (argc != 1) {
4472 d_printf("Usage: net rpc trustdom del <domain_name>\n");
4473 return NT_STATUS_INVALID_PARAMETER;
4477 * Make valid trusting domain account (ie. uppercased and with '$' appended)
4479 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
4481 if (acct_name == NULL)
4482 return NT_STATUS_NO_MEMORY;
4484 strupper_m(acct_name);
4486 names = TALLOC_ARRAY(mem_ctx, const char *, 1);
4487 names[0] = acct_name;
4490 /* Get samr policy handle */
4491 result = cli_samr_connect(cli, mem_ctx, MAXIMUM_ALLOWED_ACCESS,
4492 &connect_pol);
4493 if (!NT_STATUS_IS_OK(result)) {
4494 goto done;
4497 /* Get domain policy handle */
4498 result = cli_samr_open_domain(cli, mem_ctx, &connect_pol,
4499 MAXIMUM_ALLOWED_ACCESS,
4500 domain_sid, &domain_pol);
4501 if (!NT_STATUS_IS_OK(result)) {
4502 goto done;
4505 result = cli_samr_lookup_names(cli, mem_ctx, &domain_pol, flags, 1,
4506 names, &num_rids,
4507 &user_rids, &name_types);
4509 if (!NT_STATUS_IS_OK(result)) {
4510 goto done;
4513 result = cli_samr_open_user(cli, mem_ctx, &domain_pol,
4514 MAXIMUM_ALLOWED_ACCESS,
4515 user_rids[0], &user_pol);
4517 if (!NT_STATUS_IS_OK(result)) {
4518 goto done;
4521 /* append the rid to the domain sid */
4522 sid_copy(&trust_acct_sid, domain_sid);
4523 if (!sid_append_rid(&trust_acct_sid, user_rids[0])) {
4524 goto done;
4527 /* remove the sid */
4529 result = cli_samr_remove_sid_foreign_domain(cli, mem_ctx, &user_pol,
4530 &trust_acct_sid);
4532 if (!NT_STATUS_IS_OK(result)) {
4533 goto done;
4536 /* Delete user */
4538 result = cli_samr_delete_dom_user(cli, mem_ctx, &user_pol);
4540 if (!NT_STATUS_IS_OK(result)) {
4541 goto done;
4544 if (!NT_STATUS_IS_OK(result)) {
4545 DEBUG(0,("Could not set trust account password: %s\n",
4546 nt_errstr(result)));
4547 goto done;
4550 done:
4551 return result;
4555 * Delete interdomain trust account for a remote domain.
4557 * @param argc standard argc
4558 * @param argv standard argv without initial components
4560 * @return Integer status (0 means success)
4563 static int rpc_trustdom_del(int argc, const char **argv)
4565 if (argc > 0) {
4566 return run_rpc_command(NULL, PI_SAMR, 0, rpc_trustdom_del_internals,
4567 argc, argv);
4568 } else {
4569 d_printf("Usage: net rpc trustdom del <domain>\n");
4570 return -1;
4575 * Establish trust relationship to a trusting domain.
4576 * Interdomain account must already be created on remote PDC.
4578 * @param argc standard argc
4579 * @param argv standard argv without initial components
4581 * @return Integer status (0 means success)
4584 static int rpc_trustdom_establish(int argc, const char **argv)
4586 struct cli_state *cli;
4587 struct in_addr server_ip;
4588 POLICY_HND connect_hnd;
4589 TALLOC_CTX *mem_ctx;
4590 NTSTATUS nt_status;
4591 DOM_SID *domain_sid;
4592 smb_ucs2_t *uni_domain_name;
4594 char* domain_name;
4595 char* domain_name_pol;
4596 char* acct_name;
4597 fstring pdc_name;
4600 * Connect to \\server\ipc$ as 'our domain' account with password
4603 if (argc != 1) {
4604 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
4605 return -1;
4608 domain_name = smb_xstrdup(argv[0]);
4609 strupper_m(domain_name);
4611 /* account name used at first is our domain's name with '$' */
4612 asprintf(&acct_name, "%s$", lp_workgroup());
4613 strupper_m(acct_name);
4616 * opt_workgroup will be used by connection functions further,
4617 * hence it should be set to remote domain name instead of ours
4619 if (opt_workgroup) {
4620 opt_workgroup = smb_xstrdup(domain_name);
4623 opt_user_name = acct_name;
4625 /* find the domain controller */
4626 if (!net_find_pdc(&server_ip, pdc_name, domain_name)) {
4627 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
4628 return -1;
4631 /* connect to ipc$ as username/password */
4632 nt_status = connect_to_ipc(&cli, &server_ip, pdc_name);
4633 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
4635 /* Is it trusting domain account for sure ? */
4636 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
4637 nt_errstr(nt_status)));
4638 return -1;
4642 * Connect to \\server\ipc$ again (this time anonymously)
4645 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, (char*)pdc_name);
4647 if (NT_STATUS_IS_ERR(nt_status)) {
4648 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
4649 domain_name, nt_errstr(nt_status)));
4653 * Use NetServerEnum2 to make sure we're talking to a proper server
4656 if (!cli_get_pdc_name(cli, domain_name, (char*)pdc_name)) {
4657 DEBUG(0, ("NetServerEnum2 error: Couldn't find primary domain controller\
4658 for domain %s\n", domain_name));
4661 if (!(mem_ctx = talloc_init("establishing trust relationship to "
4662 "domain %s", domain_name))) {
4663 DEBUG(0, ("talloc_init() failed\n"));
4664 cli_shutdown(cli);
4665 return -1;
4669 * Call LsaOpenPolicy and LsaQueryInfo
4672 if (!cli_nt_session_open(cli, PI_LSARPC)) {
4673 DEBUG(0, ("Could not initialise lsa pipe\n"));
4674 cli_shutdown(cli);
4675 return -1;
4678 nt_status = cli_lsa_open_policy2(cli, mem_ctx, True, SEC_RIGHTS_QUERY_VALUE,
4679 &connect_hnd);
4680 if (NT_STATUS_IS_ERR(nt_status)) {
4681 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
4682 nt_errstr(nt_status)));
4683 return -1;
4686 /* Querying info level 5 */
4688 nt_status = cli_lsa_query_info_policy(cli, mem_ctx, &connect_hnd,
4689 5 /* info level */,
4690 &domain_name_pol, &domain_sid);
4691 if (NT_STATUS_IS_ERR(nt_status)) {
4692 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
4693 nt_errstr(nt_status)));
4694 return -1;
4697 if (push_ucs2_talloc(mem_ctx, &uni_domain_name, domain_name_pol) < 0) {
4698 DEBUG(0, ("Could not convert domain name %s to unicode\n",
4699 domain_name_pol));
4700 return -1;
4703 /* There should be actually query info level 3 (following nt serv behaviour),
4704 but I still don't know if it's _really_ necessary */
4707 * Store the password in secrets db
4710 if (!secrets_store_trusted_domain_password(domain_name,
4711 uni_domain_name,
4712 strlen_w(uni_domain_name)+1,
4713 opt_password,
4714 *domain_sid)) {
4715 DEBUG(0, ("Storing password for trusted domain failed.\n"));
4716 return -1;
4720 * Close the pipes and clean up
4723 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
4724 if (NT_STATUS_IS_ERR(nt_status)) {
4725 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
4726 nt_errstr(nt_status)));
4727 return -1;
4730 if (cli->nt_pipe_fnum[cli->pipe_idx])
4731 cli_nt_session_close(cli);
4733 cli_shutdown(cli);
4735 talloc_destroy(mem_ctx);
4737 d_printf("Trust to domain %s established\n", domain_name);
4738 return 0;
4742 * Revoke trust relationship to the remote domain
4744 * @param argc standard argc
4745 * @param argv standard argv without initial components
4747 * @return Integer status (0 means success)
4750 static int rpc_trustdom_revoke(int argc, const char **argv)
4752 char* domain_name;
4754 if (argc < 1) return -1;
4756 /* generate upper cased domain name */
4757 domain_name = smb_xstrdup(argv[0]);
4758 strupper_m(domain_name);
4760 /* delete password of the trust */
4761 if (!trusted_domain_password_delete(domain_name)) {
4762 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
4763 domain_name));
4764 return -1;
4767 return 0;
4771 * Usage for 'net rpc trustdom' command
4773 * @param argc standard argc
4774 * @param argv standard argv without inital components
4776 * @return Integer status returned to shell
4779 static int rpc_trustdom_usage(int argc, const char **argv)
4781 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
4782 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
4783 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
4784 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
4785 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
4786 return -1;
4790 static NTSTATUS rpc_query_domain_sid(const DOM_SID *domain_sid,
4791 const char *domain_name,
4792 struct cli_state *cli, TALLOC_CTX *mem_ctx,
4793 int argc, const char **argv)
4795 fstring str_sid;
4796 sid_to_string(str_sid, domain_sid);
4797 d_printf("%s\n", str_sid);
4798 return NT_STATUS_OK;
4802 static int rpc_trustdom_list(int argc, const char **argv)
4804 /* common variables */
4805 TALLOC_CTX* mem_ctx;
4806 struct cli_state *cli, *remote_cli;
4807 NTSTATUS nt_status;
4808 const char *domain_name = NULL;
4809 DOM_SID *queried_dom_sid;
4810 fstring ascii_sid, padding;
4811 int ascii_dom_name_len;
4812 POLICY_HND connect_hnd;
4814 /* trusted domains listing variables */
4815 unsigned int num_domains, enum_ctx = 0;
4816 int i, pad_len, col_len = 20;
4817 DOM_SID *domain_sids;
4818 char **trusted_dom_names;
4819 fstring pdc_name;
4820 char *dummy;
4822 /* trusting domains listing variables */
4823 POLICY_HND domain_hnd;
4824 char **trusting_dom_names;
4825 uint32 *trusting_dom_rids;
4828 * Listing trusted domains (stored in secrets.tdb, if local)
4831 mem_ctx = talloc_init("trust relationships listing");
4834 * set domain and pdc name to local samba server (default)
4835 * or to remote one given in command line
4838 if (StrCaseCmp(opt_workgroup, lp_workgroup())) {
4839 domain_name = opt_workgroup;
4840 opt_target_workgroup = opt_workgroup;
4841 } else {
4842 fstrcpy(pdc_name, global_myname());
4843 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
4844 opt_target_workgroup = domain_name;
4847 /* open \PIPE\lsarpc and open policy handle */
4848 if (!(cli = net_make_ipc_connection(NET_FLAGS_PDC))) {
4849 DEBUG(0, ("Couldn't connect to domain controller\n"));
4850 return -1;
4853 if (!cli_nt_session_open(cli, PI_LSARPC)) {
4854 DEBUG(0, ("Could not initialise lsa pipe\n"));
4855 return -1;
4858 nt_status = cli_lsa_open_policy2(cli, mem_ctx, False, SEC_RIGHTS_QUERY_VALUE,
4859 &connect_hnd);
4860 if (NT_STATUS_IS_ERR(nt_status)) {
4861 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
4862 nt_errstr(nt_status)));
4863 return -1;
4866 /* query info level 5 to obtain sid of a domain being queried */
4867 nt_status = cli_lsa_query_info_policy(
4868 cli, mem_ctx, &connect_hnd, 5 /* info level */,
4869 &dummy, &queried_dom_sid);
4871 if (NT_STATUS_IS_ERR(nt_status)) {
4872 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
4873 nt_errstr(nt_status)));
4874 return -1;
4878 * Keep calling LsaEnumTrustdom over opened pipe until
4879 * the end of enumeration is reached
4882 d_printf("Trusted domains list:\n\n");
4884 do {
4885 nt_status = cli_lsa_enum_trust_dom(cli, mem_ctx, &connect_hnd, &enum_ctx,
4886 &num_domains,
4887 &trusted_dom_names, &domain_sids);
4889 if (NT_STATUS_IS_ERR(nt_status)) {
4890 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
4891 nt_errstr(nt_status)));
4892 return -1;
4895 for (i = 0; i < num_domains; i++) {
4896 /* convert sid into ascii string */
4897 sid_to_string(ascii_sid, &(domain_sids[i]));
4899 /* calculate padding space for d_printf to look nicer */
4900 pad_len = col_len - strlen(trusted_dom_names[i]);
4901 padding[pad_len] = 0;
4902 do padding[--pad_len] = ' '; while (pad_len);
4904 d_printf("%s%s%s\n", trusted_dom_names[i], padding, ascii_sid);
4908 * in case of no trusted domains say something rather
4909 * than just display blank line
4911 if (!num_domains) d_printf("none\n");
4913 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
4915 /* close this connection before doing next one */
4916 nt_status = cli_lsa_close(cli, mem_ctx, &connect_hnd);
4917 if (NT_STATUS_IS_ERR(nt_status)) {
4918 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
4919 nt_errstr(nt_status)));
4920 return -1;
4923 cli_nt_session_close(cli);
4926 * Listing trusting domains (stored in passdb backend, if local)
4929 d_printf("\nTrusting domains list:\n\n");
4932 * Open \PIPE\samr and get needed policy handles
4934 if (!cli_nt_session_open(cli, PI_SAMR)) {
4935 DEBUG(0, ("Could not initialise samr pipe\n"));
4936 return -1;
4939 /* SamrConnect */
4940 nt_status = cli_samr_connect(cli, mem_ctx, SA_RIGHT_SAM_OPEN_DOMAIN,
4941 &connect_hnd);
4942 if (!NT_STATUS_IS_OK(nt_status)) {
4943 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
4944 nt_errstr(nt_status)));
4945 return -1;
4948 /* SamrOpenDomain - we have to open domain policy handle in order to be
4949 able to enumerate accounts*/
4950 nt_status = cli_samr_open_domain(cli, mem_ctx, &connect_hnd,
4951 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
4952 queried_dom_sid, &domain_hnd);
4953 if (!NT_STATUS_IS_OK(nt_status)) {
4954 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
4955 nt_errstr(nt_status)));
4956 return -1;
4960 * perform actual enumeration
4963 enum_ctx = 0; /* reset enumeration context from last enumeration */
4964 do {
4966 nt_status = cli_samr_enum_dom_users(cli, mem_ctx, &domain_hnd,
4967 &enum_ctx, ACB_DOMTRUST, 0xffff,
4968 &trusting_dom_names, &trusting_dom_rids,
4969 &num_domains);
4970 if (NT_STATUS_IS_ERR(nt_status)) {
4971 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
4972 nt_errstr(nt_status)));
4973 return -1;
4976 for (i = 0; i < num_domains; i++) {
4979 * get each single domain's sid (do we _really_ need this ?):
4980 * 1) connect to domain's pdc
4981 * 2) query the pdc for domain's sid
4984 /* get rid of '$' tail */
4985 ascii_dom_name_len = strlen(trusting_dom_names[i]);
4986 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
4987 trusting_dom_names[i][ascii_dom_name_len - 1] = '\0';
4989 /* calculate padding space for d_printf to look nicer */
4990 pad_len = col_len - strlen(trusting_dom_names[i]);
4991 padding[pad_len] = 0;
4992 do padding[--pad_len] = ' '; while (pad_len);
4994 /* set opt_* variables to remote domain */
4995 strupper_m(trusting_dom_names[i]);
4996 opt_workgroup = talloc_strdup(mem_ctx, trusting_dom_names[i]);
4997 opt_target_workgroup = opt_workgroup;
4999 d_printf("%s%s", trusting_dom_names[i], padding);
5001 /* connect to remote domain controller */
5002 remote_cli = net_make_ipc_connection(NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS);
5003 if (remote_cli) {
5004 /* query for domain's sid */
5005 if (run_rpc_command(remote_cli, PI_LSARPC, 0, rpc_query_domain_sid, argc, argv))
5006 d_printf("couldn't get domain's sid\n");
5008 cli_shutdown(remote_cli);
5010 } else {
5011 d_printf("domain controller is not responding\n");
5015 if (!num_domains) d_printf("none\n");
5017 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5019 /* close opened samr and domain policy handles */
5020 nt_status = cli_samr_close(cli, mem_ctx, &domain_hnd);
5021 if (!NT_STATUS_IS_OK(nt_status)) {
5022 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
5025 nt_status = cli_samr_close(cli, mem_ctx, &connect_hnd);
5026 if (!NT_STATUS_IS_OK(nt_status)) {
5027 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
5030 /* close samr pipe and connection to IPC$ */
5031 cli_nt_session_close(cli);
5032 cli_shutdown(cli);
5034 talloc_destroy(mem_ctx);
5035 return 0;
5039 * Entrypoint for 'net rpc trustdom' code
5041 * @param argc standard argc
5042 * @param argv standard argv without initial components
5044 * @return Integer status (0 means success)
5047 static int rpc_trustdom(int argc, const char **argv)
5049 struct functable func[] = {
5050 {"add", rpc_trustdom_add},
5051 {"del", rpc_trustdom_del},
5052 {"establish", rpc_trustdom_establish},
5053 {"revoke", rpc_trustdom_revoke},
5054 {"help", rpc_trustdom_usage},
5055 {"list", rpc_trustdom_list},
5056 {NULL, NULL}
5059 if (argc == 0) {
5060 rpc_trustdom_usage(argc, argv);
5061 return -1;
5064 return (net_run_function(argc, argv, func, rpc_user_usage));
5068 * Check if a server will take rpc commands
5069 * @param flags Type of server to connect to (PDC, DMB, localhost)
5070 * if the host is not explicitly specified
5071 * @return BOOL (true means rpc supported)
5073 BOOL net_rpc_check(unsigned flags)
5075 struct cli_state cli;
5076 BOOL ret = False;
5077 struct in_addr server_ip;
5078 char *server_name = NULL;
5080 /* flags (i.e. server type) may depend on command */
5081 if (!net_find_server(flags, &server_ip, &server_name))
5082 return False;
5084 ZERO_STRUCT(cli);
5085 if (cli_initialise(&cli) == False)
5086 return False;
5088 if (!cli_connect(&cli, server_name, &server_ip))
5089 goto done;
5090 if (!attempt_netbios_session_request(&cli, global_myname(),
5091 server_name, &server_ip))
5092 goto done;
5093 if (!cli_negprot(&cli))
5094 goto done;
5095 if (cli.protocol < PROTOCOL_NT1)
5096 goto done;
5098 ret = True;
5099 done:
5100 cli_shutdown(&cli);
5101 return ret;
5104 /* dump sam database via samsync rpc calls */
5105 static int rpc_samdump(int argc, const char **argv) {
5106 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_samdump_internals,
5107 argc, argv);
5110 /* syncronise sam database via samsync rpc calls */
5111 static int rpc_vampire(int argc, const char **argv) {
5112 return run_rpc_command(NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS, rpc_vampire_internals,
5113 argc, argv);
5116 /**
5117 * Migrate everything from a print-server
5119 * @param argc Standard main() style argc
5120 * @param argv Standard main() style argv. Initial components are already
5121 * stripped
5123 * @return A shell status integer (0 for success)
5125 * The order is important !
5126 * To successfully add drivers the print-queues have to exist !
5127 * Applying ACLs should be the last step, because you're easily locked out
5130 static int rpc_printer_migrate_all(int argc, const char **argv)
5132 int ret;
5134 if (!opt_host) {
5135 printf("no server to migrate\n");
5136 return -1;
5139 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_printers_internals, argc, argv);
5140 if (ret)
5141 return ret;
5143 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_drivers_internals, argc, argv);
5144 if (ret)
5145 return ret;
5147 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_forms_internals, argc, argv);
5148 if (ret)
5149 return ret;
5151 ret = run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_settings_internals, argc, argv);
5152 if (ret)
5153 return ret;
5155 return run_rpc_command(NULL, PI_SPOOLSS, 0, rpc_printer_migrate_security_internals, argc, argv);
5159 /**
5160 * Migrate print-drivers from a print-server
5162 * @param argc Standard main() style argc
5163 * @param argv Standard main() style argv. Initial components are already
5164 * stripped
5166 * @return A shell status integer (0 for success)
5168 static int rpc_printer_migrate_drivers(int argc, const char **argv)
5170 if (!opt_host) {
5171 printf("no server to migrate\n");
5172 return -1;
5175 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5176 rpc_printer_migrate_drivers_internals,
5177 argc, argv);
5180 /**
5181 * Migrate print-forms from a print-server
5183 * @param argc Standard main() style argc
5184 * @param argv Standard main() style argv. Initial components are already
5185 * stripped
5187 * @return A shell status integer (0 for success)
5189 static int rpc_printer_migrate_forms(int argc, const char **argv)
5191 if (!opt_host) {
5192 printf("no server to migrate\n");
5193 return -1;
5196 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5197 rpc_printer_migrate_forms_internals,
5198 argc, argv);
5201 /**
5202 * Migrate printers from a print-server
5204 * @param argc Standard main() style argc
5205 * @param argv Standard main() style argv. Initial components are already
5206 * stripped
5208 * @return A shell status integer (0 for success)
5210 static int rpc_printer_migrate_printers(int argc, const char **argv)
5212 if (!opt_host) {
5213 printf("no server to migrate\n");
5214 return -1;
5217 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5218 rpc_printer_migrate_printers_internals,
5219 argc, argv);
5222 /**
5223 * Migrate printer-ACLs from a print-server
5225 * @param argc Standard main() style argc
5226 * @param argv Standard main() style argv. Initial components are already
5227 * stripped
5229 * @return A shell status integer (0 for success)
5231 static int rpc_printer_migrate_security(int argc, const char **argv)
5233 if (!opt_host) {
5234 printf("no server to migrate\n");
5235 return -1;
5238 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5239 rpc_printer_migrate_security_internals,
5240 argc, argv);
5243 /**
5244 * Migrate printer-settings from a print-server
5246 * @param argc Standard main() style argc
5247 * @param argv Standard main() style argv. Initial components are already
5248 * stripped
5250 * @return A shell status integer (0 for success)
5252 static int rpc_printer_migrate_settings(int argc, const char **argv)
5254 if (!opt_host) {
5255 printf("no server to migrate\n");
5256 return -1;
5259 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5260 rpc_printer_migrate_settings_internals,
5261 argc, argv);
5264 /**
5265 * 'net rpc printer' entrypoint.
5266 * @param argc Standard main() style argc
5267 * @param argv Standard main() style argv. Initial components are already
5268 * stripped
5271 int rpc_printer_migrate(int argc, const char **argv)
5274 /* ouch: when addriver and setdriver are called from within
5275 rpc_printer_migrate_drivers_internals, the printer-queue already
5276 *has* to exist */
5278 struct functable func[] = {
5279 {"all", rpc_printer_migrate_all},
5280 {"drivers", rpc_printer_migrate_drivers},
5281 {"forms", rpc_printer_migrate_forms},
5282 {"help", rpc_printer_usage},
5283 {"printers", rpc_printer_migrate_printers},
5284 {"security", rpc_printer_migrate_security},
5285 {"settings", rpc_printer_migrate_settings},
5286 {NULL, NULL}
5289 return net_run_function(argc, argv, func, rpc_printer_usage);
5293 /**
5294 * List printers on a remote RPC server
5296 * @param argc Standard main() style argc
5297 * @param argv Standard main() style argv. Initial components are already
5298 * stripped
5300 * @return A shell status integer (0 for success)
5302 static int rpc_printer_list(int argc, const char **argv)
5305 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5306 rpc_printer_list_internals,
5307 argc, argv);
5310 /**
5311 * List printer-drivers on a remote RPC server
5313 * @param argc Standard main() style argc
5314 * @param argv Standard main() style argv. Initial components are already
5315 * stripped
5317 * @return A shell status integer (0 for success)
5319 static int rpc_printer_driver_list(int argc, const char **argv)
5322 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5323 rpc_printer_driver_list_internals,
5324 argc, argv);
5327 /**
5328 * Publish printer in ADS via MSRPC
5330 * @param argc Standard main() style argc
5331 * @param argv Standard main() style argv. Initial components are already
5332 * stripped
5334 * @return A shell status integer (0 for success)
5336 static int rpc_printer_publish_publish(int argc, const char **argv)
5339 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5340 rpc_printer_publish_publish_internals,
5341 argc, argv);
5344 /**
5345 * Update printer in ADS via MSRPC
5347 * @param argc Standard main() style argc
5348 * @param argv Standard main() style argv. Initial components are already
5349 * stripped
5351 * @return A shell status integer (0 for success)
5353 static int rpc_printer_publish_update(int argc, const char **argv)
5356 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5357 rpc_printer_publish_update_internals,
5358 argc, argv);
5361 /**
5362 * UnPublish printer in ADS via MSRPC
5364 * @param argc Standard main() style argc
5365 * @param argv Standard main() style argv. Initial components are already
5366 * stripped
5368 * @return A shell status integer (0 for success)
5370 static int rpc_printer_publish_unpublish(int argc, const char **argv)
5373 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5374 rpc_printer_publish_unpublish_internals,
5375 argc, argv);
5378 /**
5379 * List published printers via MSRPC
5381 * @param argc Standard main() style argc
5382 * @param argv Standard main() style argv. Initial components are already
5383 * stripped
5385 * @return A shell status integer (0 for success)
5387 static int rpc_printer_publish_list(int argc, const char **argv)
5390 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5391 rpc_printer_publish_list_internals,
5392 argc, argv);
5396 /**
5397 * Publish printer in ADS
5399 * @param argc Standard main() style argc
5400 * @param argv Standard main() style argv. Initial components are already
5401 * stripped
5403 * @return A shell status integer (0 for success)
5405 static int rpc_printer_publish(int argc, const char **argv)
5408 struct functable func[] = {
5409 {"publish", rpc_printer_publish_publish},
5410 {"update", rpc_printer_publish_update},
5411 {"unpublish", rpc_printer_publish_unpublish},
5412 {"list", rpc_printer_publish_list},
5413 {"help", rpc_printer_usage},
5414 {NULL, NULL}
5417 if (argc == 0)
5418 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5419 rpc_printer_publish_list_internals,
5420 argc, argv);
5422 return net_run_function(argc, argv, func, rpc_printer_usage);
5427 /**
5428 * Display rpc printer help page.
5429 * @param argc Standard main() style argc
5430 * @param argv Standard main() style argv. Initial components are already
5431 * stripped
5433 int rpc_printer_usage(int argc, const char **argv)
5435 return net_help_printer(argc, argv);
5438 /**
5439 * 'net rpc printer' entrypoint.
5440 * @param argc Standard main() style argc
5441 * @param argv Standard main() style argv. Initial components are already
5442 * stripped
5444 int net_rpc_printer(int argc, const char **argv)
5446 struct functable func[] = {
5447 {"list", rpc_printer_list},
5448 {"migrate", rpc_printer_migrate},
5449 {"driver", rpc_printer_driver_list},
5450 {"publish", rpc_printer_publish},
5451 {NULL, NULL}
5454 if (argc == 0)
5455 return run_rpc_command(NULL, PI_SPOOLSS, 0,
5456 rpc_printer_list_internals,
5457 argc, argv);
5459 return net_run_function(argc, argv, func, rpc_printer_usage);
5462 /****************************************************************************/
5465 /**
5466 * Basic usage function for 'net rpc'
5467 * @param argc Standard main() style argc
5468 * @param argv Standard main() style argv. Initial components are already
5469 * stripped
5472 int net_rpc_usage(int argc, const char **argv)
5474 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
5475 d_printf(" net rpc join \t\t\tto join a domain \n");
5476 d_printf(" net rpc oldjoin \t\t\tto join a domain created in server manager\n\n\n");
5477 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
5478 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
5479 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
5480 d_printf(" net rpc group \t\tto list groups\n");
5481 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
5482 d_printf(" net rpc printer \t\tto list and migrate printers\n");
5483 d_printf(" net rpc file \t\t\tto list open files\n");
5484 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
5485 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
5486 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
5487 d_printf(" net rpc samdump \t\tdiplay an NT PDC's users, groups and other data\n");
5488 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
5489 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
5490 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
5491 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
5492 d_printf("\n");
5493 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
5494 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
5495 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
5496 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
5497 d_printf("\t-c or --comment=<message>\ttext message to display on impending shutdown\n");
5498 return -1;
5503 * Help function for 'net rpc'. Calls command specific help if requested
5504 * or displays usage of net rpc
5505 * @param argc Standard main() style argc
5506 * @param argv Standard main() style argv. Initial components are already
5507 * stripped
5510 int net_rpc_help(int argc, const char **argv)
5512 struct functable func[] = {
5513 {"join", rpc_join_usage},
5514 {"user", rpc_user_usage},
5515 {"group", rpc_group_usage},
5516 {"share", rpc_share_usage},
5517 /*{"changetrustpw", rpc_changetrustpw_usage}, */
5518 {"trustdom", rpc_trustdom_usage},
5519 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
5520 /*{"shutdown", rpc_shutdown_usage}, */
5521 {NULL, NULL}
5524 if (argc == 0) {
5525 net_rpc_usage(argc, argv);
5526 return -1;
5529 return (net_run_function(argc, argv, func, rpc_user_usage));
5533 /**
5534 * 'net rpc' entrypoint.
5535 * @param argc Standard main() style argc
5536 * @param argv Standard main() style argv. Initial components are already
5537 * stripped
5540 int net_rpc(int argc, const char **argv)
5542 struct functable func[] = {
5543 {"info", net_rpc_info},
5544 {"join", net_rpc_join},
5545 {"oldjoin", net_rpc_oldjoin},
5546 {"testjoin", net_rpc_testjoin},
5547 {"user", net_rpc_user},
5548 {"password", rpc_user_password},
5549 {"group", net_rpc_group},
5550 {"share", net_rpc_share},
5551 {"file", net_rpc_file},
5552 {"printer", net_rpc_printer},
5553 {"changetrustpw", net_rpc_changetrustpw},
5554 {"trustdom", rpc_trustdom},
5555 {"abortshutdown", rpc_shutdown_abort},
5556 {"shutdown", rpc_shutdown},
5557 {"samdump", rpc_samdump},
5558 {"vampire", rpc_vampire},
5559 {"getsid", net_rpc_getsid},
5560 {"rights", net_rpc_rights},
5561 {"service", net_rpc_service},
5562 {"help", net_rpc_help},
5563 {NULL, NULL}
5565 return net_run_function(argc, argv, func, net_rpc_usage);