A few cleanups from Chere Zhou <chere.zhou@isilon.com>.
[Samba.git] / source / utils / net_rpc.c
blobd6a3e486fb3ec5cf63d2ed509540070809d73392
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,2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "includes.h"
24 #include "utils/net.h"
26 static int net_mode_share;
27 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
29 /**
30 * @file net_rpc.c
32 * @brief RPC based subcommands for the 'net' utility.
34 * This file should contain much of the functionality that used to
35 * be found in rpcclient, execpt that the commands should change
36 * less often, and the fucntionality should be sane (the user is not
37 * expected to know a rid/sid before they conduct an operation etc.)
39 * @todo Perhaps eventually these should be split out into a number
40 * of files, as this could get quite big.
41 **/
44 /**
45 * Many of the RPC functions need the domain sid. This function gets
46 * it at the start of every run
48 * @param cli A cli_state already connected to the remote machine
50 * @return The Domain SID of the remote machine.
51 **/
53 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
54 DOM_SID **domain_sid,
55 const char **domain_name)
57 struct rpc_pipe_client *lsa_pipe;
58 POLICY_HND pol;
59 NTSTATUS result = NT_STATUS_OK;
60 union lsa_PolicyInformation *info = NULL;
62 lsa_pipe = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
63 if (!lsa_pipe) {
64 d_fprintf(stderr, "Could not initialise lsa pipe\n");
65 return result;
68 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
69 SEC_RIGHTS_MAXIMUM_ALLOWED,
70 &pol);
71 if (!NT_STATUS_IS_OK(result)) {
72 d_fprintf(stderr, "open_policy failed: %s\n",
73 nt_errstr(result));
74 return result;
77 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
78 &pol,
79 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
80 &info);
81 if (!NT_STATUS_IS_OK(result)) {
82 d_fprintf(stderr, "lsaquery failed: %s\n",
83 nt_errstr(result));
84 return result;
87 *domain_name = info->account_domain.name.string;
88 *domain_sid = info->account_domain.sid;
90 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
91 TALLOC_FREE(lsa_pipe);
93 return NT_STATUS_OK;
96 /**
97 * Run a single RPC command, from start to finish.
99 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
100 * @param conn_flag a NET_FLAG_ combination. Passed to
101 * net_make_ipc_connection.
102 * @param argc Standard main() style argc
103 * @param argc Standard main() style argv. Initial components are already
104 * stripped
105 * @return A shell status integer (0 for success)
108 int run_rpc_command(struct net_context *c,
109 struct cli_state *cli_arg,
110 const int pipe_idx,
111 int conn_flags,
112 rpc_command_fn fn,
113 int argc,
114 const char **argv)
116 struct cli_state *cli = NULL;
117 struct rpc_pipe_client *pipe_hnd = NULL;
118 TALLOC_CTX *mem_ctx;
119 NTSTATUS nt_status;
120 DOM_SID *domain_sid;
121 const char *domain_name;
123 /* make use of cli_state handed over as an argument, if possible */
124 if (!cli_arg) {
125 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
126 if (!NT_STATUS_IS_OK(nt_status)) {
127 DEBUG(1, ("failed to make ipc connection: %s\n",
128 nt_errstr(nt_status)));
129 return -1;
131 } else {
132 cli = cli_arg;
135 if (!cli) {
136 return -1;
139 /* Create mem_ctx */
141 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
142 DEBUG(0, ("talloc_init() failed\n"));
143 cli_shutdown(cli);
144 return -1;
147 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
148 &domain_name);
149 if (!NT_STATUS_IS_OK(nt_status)) {
150 cli_shutdown(cli);
151 return -1;
154 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
155 if (lp_client_schannel() && (pipe_idx == PI_NETLOGON)) {
156 /* Always try and create an schannel netlogon pipe. */
157 pipe_hnd = cli_rpc_pipe_open_schannel(cli, pipe_idx,
158 PIPE_AUTH_LEVEL_PRIVACY,
159 domain_name,
160 &nt_status);
161 if (!pipe_hnd) {
162 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
163 nt_errstr(nt_status) ));
164 cli_shutdown(cli);
165 return -1;
167 } else {
168 pipe_hnd = cli_rpc_pipe_open_noauth(cli, pipe_idx, &nt_status);
169 if (!pipe_hnd) {
170 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
171 cli_get_pipe_name(pipe_idx),
172 nt_errstr(nt_status) ));
173 cli_shutdown(cli);
174 return -1;
179 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
181 if (!NT_STATUS_IS_OK(nt_status)) {
182 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
183 } else {
184 DEBUG(5, ("rpc command function succedded\n"));
187 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
188 if (pipe_hnd) {
189 TALLOC_FREE(pipe_hnd);
193 /* close the connection only if it was opened here */
194 if (!cli_arg) {
195 cli_shutdown(cli);
198 talloc_destroy(mem_ctx);
199 return (!NT_STATUS_IS_OK(nt_status));
203 * Force a change of the trust acccount password.
205 * All parameters are provided by the run_rpc_command function, except for
206 * argc, argv which are passes through.
208 * @param domain_sid The domain sid aquired from the remote server
209 * @param cli A cli_state connected to the server.
210 * @param mem_ctx Talloc context, destoyed on compleation of the function.
211 * @param argc Standard main() style argc
212 * @param argc Standard main() style argv. Initial components are already
213 * stripped
215 * @return Normal NTSTATUS return.
218 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
219 const DOM_SID *domain_sid,
220 const char *domain_name,
221 struct cli_state *cli,
222 struct rpc_pipe_client *pipe_hnd,
223 TALLOC_CTX *mem_ctx,
224 int argc,
225 const char **argv)
228 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
232 * Force a change of the trust acccount password.
234 * @param argc Standard main() style argc
235 * @param argc Standard main() style argv. Initial components are already
236 * stripped
238 * @return A shell status integer (0 for success)
241 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
243 return run_rpc_command(c, NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
244 rpc_changetrustpw_internals,
245 argc, argv);
249 * Join a domain, the old way.
251 * This uses 'machinename' as the inital password, and changes it.
253 * The password should be created with 'server manager' or equiv first.
255 * All parameters are provided by the run_rpc_command function, except for
256 * argc, argv which are passes through.
258 * @param domain_sid The domain sid aquired from the remote server
259 * @param cli A cli_state connected to the server.
260 * @param mem_ctx Talloc context, destoyed on compleation of the function.
261 * @param argc Standard main() style argc
262 * @param argc Standard main() style argv. Initial components are already
263 * stripped
265 * @return Normal NTSTATUS return.
268 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
269 const DOM_SID *domain_sid,
270 const char *domain_name,
271 struct cli_state *cli,
272 struct rpc_pipe_client *pipe_hnd,
273 TALLOC_CTX *mem_ctx,
274 int argc,
275 const char **argv)
278 fstring trust_passwd;
279 unsigned char orig_trust_passwd_hash[16];
280 NTSTATUS result;
281 uint32 sec_channel_type;
283 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &result);
284 if (!pipe_hnd) {
285 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
286 "error was %s\n",
287 cli->desthost,
288 nt_errstr(result) ));
289 return result;
293 check what type of join - if the user want's to join as
294 a BDC, the server must agree that we are a BDC.
296 if (argc >= 0) {
297 sec_channel_type = get_sec_channel_type(argv[0]);
298 } else {
299 sec_channel_type = get_sec_channel_type(NULL);
302 fstrcpy(trust_passwd, global_myname());
303 strlower_m(trust_passwd);
306 * Machine names can be 15 characters, but the max length on
307 * a password is 14. --jerry
310 trust_passwd[14] = '\0';
312 E_md4hash(trust_passwd, orig_trust_passwd_hash);
314 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
315 orig_trust_passwd_hash,
316 sec_channel_type);
318 if (NT_STATUS_IS_OK(result))
319 printf("Joined domain %s.\n", c->opt_target_workgroup);
322 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
323 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
324 result = NT_STATUS_UNSUCCESSFUL;
327 return result;
331 * Join a domain, the old way.
333 * @param argc Standard main() style argc
334 * @param argc Standard main() style argv. Initial components are already
335 * stripped
337 * @return A shell status integer (0 for success)
340 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
342 return run_rpc_command(c, NULL, PI_NETLOGON,
343 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
344 rpc_oldjoin_internals,
345 argc, argv);
349 * Join a domain, the old way. This function exists to allow
350 * the message to be displayed when oldjoin was explicitly
351 * requested, but not when it was implied by "net rpc join"
353 * @param argc Standard main() style argc
354 * @param argc Standard main() style argv. Initial components are already
355 * stripped
357 * @return A shell status integer (0 for success)
360 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
362 int rc = net_rpc_perform_oldjoin(c, argc, argv);
364 if (rc) {
365 d_fprintf(stderr, "Failed to join domain\n");
368 return rc;
372 * Basic usage function for 'net rpc join'
373 * @param argc Standard main() style argc
374 * @param argc Standard main() style argv. Initial components are already
375 * stripped
378 static int rpc_join_usage(struct net_context *c, int argc, const char **argv)
380 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
381 "\t to join a domain with admin username & password\n"\
382 "\t\t password will be prompted if needed and none is specified\n"\
383 "\t <type> can be (default MEMBER)\n"\
384 "\t\t BDC - Join as a BDC\n"\
385 "\t\t PDC - Join as a PDC\n"\
386 "\t\t MEMBER - Join as a MEMBER server\n");
388 net_common_flags_usage(c, argc, argv);
389 return -1;
393 * 'net rpc join' entrypoint.
394 * @param argc Standard main() style argc
395 * @param argc Standard main() style argv. Initial components are already
396 * stripped
398 * Main 'net_rpc_join()' (where the admin username/password is used) is
399 * in net_rpc_join.c
400 * Try to just change the password, but if that doesn't work, use/prompt
401 * for a username/password.
404 int net_rpc_join(struct net_context *c, int argc, const char **argv)
406 if (lp_server_role() == ROLE_STANDALONE) {
407 d_printf("cannot join as standalone machine\n");
408 return -1;
411 if (strlen(global_myname()) > 15) {
412 d_printf("Our netbios name can be at most 15 chars long, "
413 "\"%s\" is %u chars long\n",
414 global_myname(), (unsigned int)strlen(global_myname()));
415 return -1;
418 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
419 return 0;
421 return net_rpc_join_newstyle(c, argc, argv);
425 * display info about a rpc domain
427 * All parameters are provided by the run_rpc_command function, except for
428 * argc, argv which are passed through.
430 * @param domain_sid The domain sid acquired from the remote server
431 * @param cli A cli_state connected to the server.
432 * @param mem_ctx Talloc context, destoyed on completion of the function.
433 * @param argc Standard main() style argc
434 * @param argv Standard main() style argv. Initial components are already
435 * stripped
437 * @return Normal NTSTATUS return.
440 NTSTATUS rpc_info_internals(struct net_context *c,
441 const DOM_SID *domain_sid,
442 const char *domain_name,
443 struct cli_state *cli,
444 struct rpc_pipe_client *pipe_hnd,
445 TALLOC_CTX *mem_ctx,
446 int argc,
447 const char **argv)
449 POLICY_HND connect_pol, domain_pol;
450 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
451 union samr_DomainInfo *info = NULL;
452 fstring sid_str;
454 sid_to_fstring(sid_str, domain_sid);
456 /* Get sam policy handle */
457 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
458 pipe_hnd->desthost,
459 MAXIMUM_ALLOWED_ACCESS,
460 &connect_pol);
461 if (!NT_STATUS_IS_OK(result)) {
462 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
463 goto done;
466 /* Get domain policy handle */
467 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
468 &connect_pol,
469 MAXIMUM_ALLOWED_ACCESS,
470 CONST_DISCARD(struct dom_sid2 *, domain_sid),
471 &domain_pol);
472 if (!NT_STATUS_IS_OK(result)) {
473 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
474 goto done;
477 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
478 &domain_pol,
480 &info);
481 if (NT_STATUS_IS_OK(result)) {
482 d_printf("Domain Name: %s\n", info->info2.domain_name.string);
483 d_printf("Domain SID: %s\n", sid_str);
484 d_printf("Sequence number: %llu\n",
485 (unsigned long long)info->info2.sequence_num);
486 d_printf("Num users: %u\n", info->info2.num_users);
487 d_printf("Num domain groups: %u\n", info->info2.num_groups);
488 d_printf("Num local groups: %u\n", info->info2.num_aliases);
491 done:
492 return result;
496 * 'net rpc info' entrypoint.
497 * @param argc Standard main() style argc
498 * @param argc Standard main() style argv. Initial components are already
499 * stripped
502 int net_rpc_info(struct net_context *c, int argc, const char **argv)
504 return run_rpc_command(c, NULL, PI_SAMR, NET_FLAGS_PDC,
505 rpc_info_internals,
506 argc, argv);
510 * Fetch domain SID into the local secrets.tdb
512 * All parameters are provided by the run_rpc_command function, except for
513 * argc, argv which are passes through.
515 * @param domain_sid The domain sid acquired from the remote server
516 * @param cli A cli_state connected to the server.
517 * @param mem_ctx Talloc context, destoyed on completion of thea function.
518 * @param argc Standard main() style argc
519 * @param argv Standard main() style argv. Initial components are already
520 * stripped
522 * @return Normal NTSTATUS return.
525 static NTSTATUS rpc_getsid_internals(struct net_context *c,
526 const DOM_SID *domain_sid,
527 const char *domain_name,
528 struct cli_state *cli,
529 struct rpc_pipe_client *pipe_hnd,
530 TALLOC_CTX *mem_ctx,
531 int argc,
532 const char **argv)
534 fstring sid_str;
536 sid_to_fstring(sid_str, domain_sid);
537 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
538 sid_str, domain_name);
540 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
541 DEBUG(0,("Can't store domain SID\n"));
542 return NT_STATUS_UNSUCCESSFUL;
545 return NT_STATUS_OK;
549 * 'net rpc getsid' entrypoint.
550 * @param argc Standard main() style argc
551 * @param argc Standard main() style argv. Initial components are already
552 * stripped
555 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
557 return run_rpc_command(c, NULL, PI_SAMR,
558 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
559 rpc_getsid_internals,
560 argc, argv);
563 /****************************************************************************/
566 * Basic usage function for 'net rpc user'
567 * @param argc Standard main() style argc.
568 * @param argv Standard main() style argv. Initial components are already
569 * stripped.
572 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
574 return net_user_usage(c, argc, argv);
578 * Add a new user to a remote RPC server
580 * @param argc Standard main() style argc
581 * @param argv Standard main() style argv. Initial components are already
582 * stripped
584 * @return A shell status integer (0 for success)
587 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
589 NET_API_STATUS status;
590 struct USER_INFO_1 info1;
591 uint32_t parm_error = 0;
593 if (argc < 1) {
594 d_printf("User must be specified\n");
595 rpc_user_usage(c, argc, argv);
596 return 0;
599 ZERO_STRUCT(info1);
601 info1.usri1_name = argv[0];
602 if (argc == 2) {
603 info1.usri1_password = argv[1];
606 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
608 if (status != 0) {
609 d_fprintf(stderr, "Failed to add user '%s' with: %s.\n",
610 argv[0], libnetapi_get_error_string(c->netapi_ctx,
611 status));
612 return -1;
613 } else {
614 d_printf("Added user '%s'.\n", argv[0]);
617 return 0;
621 * Rename a user on a remote RPC server
623 * All parameters are provided by the run_rpc_command function, except for
624 * argc, argv which are passes through.
626 * @param domain_sid The domain sid acquired from the remote server
627 * @param cli A cli_state connected to the server.
628 * @param mem_ctx Talloc context, destoyed on completion of the function.
629 * @param argc Standard main() style argc
630 * @param argv Standard main() style argv. Initial components are already
631 * stripped
633 * @return Normal NTSTATUS return.
636 static NTSTATUS rpc_user_rename_internals(struct net_context *c,
637 const DOM_SID *domain_sid,
638 const char *domain_name,
639 struct cli_state *cli,
640 struct rpc_pipe_client *pipe_hnd,
641 TALLOC_CTX *mem_ctx,
642 int argc,
643 const char **argv)
645 POLICY_HND connect_pol, domain_pol, user_pol;
646 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
647 uint32 info_level = 7;
648 const char *old_name, *new_name;
649 struct samr_Ids user_rids, name_types;
650 struct lsa_String lsa_acct_name;
651 union samr_UserInfo *info = NULL;
653 if (argc != 2) {
654 d_printf("Old and new username must be specified\n");
655 rpc_user_usage(c, argc, argv);
656 return NT_STATUS_OK;
659 old_name = argv[0];
660 new_name = argv[1];
662 /* Get sam policy handle */
664 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
665 pipe_hnd->desthost,
666 MAXIMUM_ALLOWED_ACCESS,
667 &connect_pol);
669 if (!NT_STATUS_IS_OK(result)) {
670 goto done;
673 /* Get domain policy handle */
675 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
676 &connect_pol,
677 MAXIMUM_ALLOWED_ACCESS,
678 CONST_DISCARD(struct dom_sid2 *, domain_sid),
679 &domain_pol);
680 if (!NT_STATUS_IS_OK(result)) {
681 goto done;
684 init_lsa_String(&lsa_acct_name, old_name);
686 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
687 &domain_pol,
689 &lsa_acct_name,
690 &user_rids,
691 &name_types);
692 if (!NT_STATUS_IS_OK(result)) {
693 goto done;
696 /* Open domain user */
697 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
698 &domain_pol,
699 MAXIMUM_ALLOWED_ACCESS,
700 user_rids.ids[0],
701 &user_pol);
703 if (!NT_STATUS_IS_OK(result)) {
704 goto done;
707 /* Query user info */
708 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
709 &user_pol,
710 info_level,
711 &info);
713 if (!NT_STATUS_IS_OK(result)) {
714 goto done;
717 init_samr_user_info7(&info->info7, new_name);
719 /* Set new name */
720 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
721 &user_pol,
722 info_level,
723 info);
725 if (!NT_STATUS_IS_OK(result)) {
726 goto done;
729 done:
730 if (!NT_STATUS_IS_OK(result)) {
731 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
732 nt_errstr(result));
733 } else {
734 d_printf("Renamed user from %s to %s\n", old_name, new_name);
736 return result;
740 * Rename a user on a remote RPC server
742 * @param argc Standard main() style argc
743 * @param argv Standard main() style argv. Initial components are already
744 * stripped
746 * @return A shell status integer (0 for success)
749 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
751 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_user_rename_internals,
752 argc, argv);
756 * Delete a user from a remote RPC server
758 * @param argc Standard main() style argc
759 * @param argv Standard main() style argv. Initial components are already
760 * stripped
762 * @return A shell status integer (0 for success)
765 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
767 NET_API_STATUS status;
769 if (argc < 1) {
770 d_printf("User must be specified\n");
771 rpc_user_usage(c, argc, argv);
772 return 0;
775 status = NetUserDel(c->opt_host, argv[0]);
777 if (status != 0) {
778 d_fprintf(stderr, "Failed to delete user '%s' with: %s.\n",
779 argv[0],
780 libnetapi_get_error_string(c->netapi_ctx, status));
781 return -1;
782 } else {
783 d_printf("Deleted user '%s'.\n", argv[0]);
786 return 0;
790 * Set a password for a user on a remote RPC server
792 * All parameters are provided by the run_rpc_command function, except for
793 * argc, argv which are passes through.
795 * @param domain_sid The domain sid acquired from the remote server
796 * @param cli A cli_state connected to the server.
797 * @param mem_ctx Talloc context, destoyed on completion of the function.
798 * @param argc Standard main() style argc
799 * @param argv Standard main() style argv. Initial components are already
800 * stripped
802 * @return Normal NTSTATUS return.
805 static NTSTATUS rpc_user_password_internals(struct net_context *c,
806 const DOM_SID *domain_sid,
807 const char *domain_name,
808 struct cli_state *cli,
809 struct rpc_pipe_client *pipe_hnd,
810 TALLOC_CTX *mem_ctx,
811 int argc,
812 const char **argv)
814 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
815 POLICY_HND connect_pol, domain_pol, user_pol;
816 uchar pwbuf[516];
817 const char *user;
818 const char *new_password;
819 char *prompt = NULL;
820 union samr_UserInfo info;
822 if (argc < 1) {
823 d_printf("User must be specified\n");
824 rpc_user_usage(c, argc, argv);
825 return NT_STATUS_OK;
828 user = argv[0];
830 if (argv[1]) {
831 new_password = argv[1];
832 } else {
833 asprintf(&prompt, "Enter new password for %s:", user);
834 new_password = getpass(prompt);
835 SAFE_FREE(prompt);
838 /* Get sam policy and domain handles */
840 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
841 pipe_hnd->desthost,
842 MAXIMUM_ALLOWED_ACCESS,
843 &connect_pol);
845 if (!NT_STATUS_IS_OK(result)) {
846 goto done;
849 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
850 &connect_pol,
851 MAXIMUM_ALLOWED_ACCESS,
852 CONST_DISCARD(struct dom_sid2 *, domain_sid),
853 &domain_pol);
855 if (!NT_STATUS_IS_OK(result)) {
856 goto done;
859 /* Get handle on user */
862 struct samr_Ids user_rids, name_types;
863 struct lsa_String lsa_acct_name;
865 init_lsa_String(&lsa_acct_name, user);
867 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
868 &domain_pol,
870 &lsa_acct_name,
871 &user_rids,
872 &name_types);
873 if (!NT_STATUS_IS_OK(result)) {
874 goto done;
877 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
878 &domain_pol,
879 MAXIMUM_ALLOWED_ACCESS,
880 user_rids.ids[0],
881 &user_pol);
883 if (!NT_STATUS_IS_OK(result)) {
884 goto done;
888 /* Set password on account */
890 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
892 init_samr_user_info24(&info.info24, pwbuf, 24);
894 SamOEMhashBlob(info.info24.password.data, 516,
895 &cli->user_session_key);
897 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
898 &user_pol,
900 &info);
902 if (!NT_STATUS_IS_OK(result)) {
903 goto done;
906 /* Display results */
908 done:
909 return result;
914 * Set a user's password on a remote RPC server
916 * @param argc Standard main() style argc
917 * @param argv Standard main() style argv. Initial components are already
918 * stripped
920 * @return A shell status integer (0 for success)
923 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
925 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_user_password_internals,
926 argc, argv);
930 * List user's groups on a remote RPC server
932 * All parameters are provided by the run_rpc_command function, except for
933 * argc, argv which are passes through.
935 * @param domain_sid The domain sid acquired from the remote server
936 * @param cli A cli_state connected to the server.
937 * @param mem_ctx Talloc context, destoyed on completion of the function.
938 * @param argc Standard main() style argc
939 * @param argv Standard main() style argv. Initial components are already
940 * stripped
942 * @return Normal NTSTATUS return.
945 static NTSTATUS rpc_user_info_internals(struct net_context *c,
946 const DOM_SID *domain_sid,
947 const char *domain_name,
948 struct cli_state *cli,
949 struct rpc_pipe_client *pipe_hnd,
950 TALLOC_CTX *mem_ctx,
951 int argc,
952 const char **argv)
954 POLICY_HND connect_pol, domain_pol, user_pol;
955 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
956 int i;
957 struct samr_RidWithAttributeArray *rid_array = NULL;
958 struct lsa_Strings names;
959 struct samr_Ids types;
960 uint32_t *lrids = NULL;
961 struct samr_Ids rids, name_types;
962 struct lsa_String lsa_acct_name;
965 if (argc < 1) {
966 d_printf("User must be specified\n");
967 rpc_user_usage(c, argc, argv);
968 return NT_STATUS_OK;
970 /* Get sam policy handle */
972 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
973 pipe_hnd->desthost,
974 MAXIMUM_ALLOWED_ACCESS,
975 &connect_pol);
976 if (!NT_STATUS_IS_OK(result)) goto done;
978 /* Get domain policy handle */
980 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
981 &connect_pol,
982 MAXIMUM_ALLOWED_ACCESS,
983 CONST_DISCARD(struct dom_sid2 *, domain_sid),
984 &domain_pol);
985 if (!NT_STATUS_IS_OK(result)) goto done;
987 /* Get handle on user */
989 init_lsa_String(&lsa_acct_name, argv[0]);
991 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
992 &domain_pol,
994 &lsa_acct_name,
995 &rids,
996 &name_types);
998 if (!NT_STATUS_IS_OK(result)) goto done;
1000 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1001 &domain_pol,
1002 MAXIMUM_ALLOWED_ACCESS,
1003 rids.ids[0],
1004 &user_pol);
1005 if (!NT_STATUS_IS_OK(result)) goto done;
1007 result = rpccli_samr_GetGroupsForUser(pipe_hnd, mem_ctx,
1008 &user_pol,
1009 &rid_array);
1011 if (!NT_STATUS_IS_OK(result)) goto done;
1013 /* Look up rids */
1015 if (rid_array->count) {
1016 if ((lrids = TALLOC_ARRAY(mem_ctx, uint32, rid_array->count)) == NULL) {
1017 result = NT_STATUS_NO_MEMORY;
1018 goto done;
1021 for (i = 0; i < rid_array->count; i++)
1022 lrids[i] = rid_array->rids[i].rid;
1024 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
1025 &domain_pol,
1026 rid_array->count,
1027 lrids,
1028 &names,
1029 &types);
1031 if (!NT_STATUS_IS_OK(result)) {
1032 goto done;
1035 /* Display results */
1037 for (i = 0; i < names.count; i++)
1038 printf("%s\n", names.names[i].string);
1040 done:
1041 return result;
1045 * List a user's groups from a remote RPC server
1047 * @param argc Standard main() style argc
1048 * @param argv Standard main() style argv. Initial components are already
1049 * stripped
1051 * @return A shell status integer (0 for success)
1054 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
1056 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_user_info_internals,
1057 argc, argv);
1061 * List users on a remote RPC server
1063 * All parameters are provided by the run_rpc_command function, except for
1064 * argc, argv which are passes through.
1066 * @param domain_sid The domain sid acquired from the remote server
1067 * @param cli A cli_state connected to the server.
1068 * @param mem_ctx Talloc context, destoyed on completion of the function.
1069 * @param argc Standard main() style argc
1070 * @param argv Standard main() style argv. Initial components are already
1071 * stripped
1073 * @return Normal NTSTATUS return.
1076 static NTSTATUS rpc_user_list_internals(struct net_context *c,
1077 const DOM_SID *domain_sid,
1078 const char *domain_name,
1079 struct cli_state *cli,
1080 struct rpc_pipe_client *pipe_hnd,
1081 TALLOC_CTX *mem_ctx,
1082 int argc,
1083 const char **argv)
1085 POLICY_HND connect_pol, domain_pol;
1086 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1087 uint32 start_idx=0, num_entries, i, loop_count = 0;
1089 /* Get sam policy handle */
1091 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1092 pipe_hnd->desthost,
1093 MAXIMUM_ALLOWED_ACCESS,
1094 &connect_pol);
1095 if (!NT_STATUS_IS_OK(result)) {
1096 goto done;
1099 /* Get domain policy handle */
1101 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1102 &connect_pol,
1103 MAXIMUM_ALLOWED_ACCESS,
1104 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1105 &domain_pol);
1106 if (!NT_STATUS_IS_OK(result)) {
1107 goto done;
1110 /* Query domain users */
1111 if (c->opt_long_list_entries)
1112 d_printf("\nUser name Comment"\
1113 "\n-----------------------------\n");
1114 do {
1115 const char *user = NULL;
1116 const char *desc = NULL;
1117 uint32 max_entries, max_size;
1118 uint32_t total_size, returned_size;
1119 union samr_DispInfo info;
1121 get_query_dispinfo_params(
1122 loop_count, &max_entries, &max_size);
1124 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
1125 &domain_pol,
1127 start_idx,
1128 max_entries,
1129 max_size,
1130 &total_size,
1131 &returned_size,
1132 &info);
1133 loop_count++;
1134 start_idx += info.info1.count;
1135 num_entries = info.info1.count;
1137 for (i = 0; i < num_entries; i++) {
1138 user = info.info1.entries[i].account_name.string;
1139 if (c->opt_long_list_entries)
1140 desc = info.info1.entries[i].description.string;
1141 if (c->opt_long_list_entries)
1142 printf("%-21.21s %s\n", user, desc);
1143 else
1144 printf("%s\n", user);
1146 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1148 done:
1149 return result;
1153 * 'net rpc user' entrypoint.
1154 * @param argc Standard main() style argc
1155 * @param argc Standard main() style argv. Initial components are already
1156 * stripped
1159 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1161 NET_API_STATUS status;
1163 struct functable func[] = {
1164 {"add", rpc_user_add},
1165 {"info", rpc_user_info},
1166 {"delete", rpc_user_delete},
1167 {"password", rpc_user_password},
1168 {"rename", rpc_user_rename},
1169 {NULL, NULL}
1172 status = libnetapi_init(&c->netapi_ctx);
1173 if (status != 0) {
1174 return -1;
1176 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1177 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1179 if (argc == 0) {
1180 return run_rpc_command(c, NULL,PI_SAMR, 0,
1181 rpc_user_list_internals,
1182 argc, argv);
1185 return net_run_function(c, argc, argv, func, rpc_user_usage);
1188 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1189 TALLOC_CTX *mem_ctx,
1190 struct rpc_sh_ctx *ctx,
1191 struct rpc_pipe_client *pipe_hnd,
1192 int argc, const char **argv)
1194 return rpc_user_list_internals(c, ctx->domain_sid, ctx->domain_name,
1195 ctx->cli, pipe_hnd, mem_ctx,
1196 argc, argv);
1199 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1200 TALLOC_CTX *mem_ctx,
1201 struct rpc_sh_ctx *ctx,
1202 struct rpc_pipe_client *pipe_hnd,
1203 int argc, const char **argv)
1205 return rpc_user_info_internals(c, ctx->domain_sid, ctx->domain_name,
1206 ctx->cli, pipe_hnd, mem_ctx,
1207 argc, argv);
1210 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1211 TALLOC_CTX *mem_ctx,
1212 struct rpc_sh_ctx *ctx,
1213 struct rpc_pipe_client *pipe_hnd,
1214 int argc, const char **argv,
1215 NTSTATUS (*fn)(
1216 struct net_context *c,
1217 TALLOC_CTX *mem_ctx,
1218 struct rpc_sh_ctx *ctx,
1219 struct rpc_pipe_client *pipe_hnd,
1220 POLICY_HND *user_hnd,
1221 int argc, const char **argv))
1223 POLICY_HND connect_pol, domain_pol, user_pol;
1224 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1225 DOM_SID sid;
1226 uint32 rid;
1227 enum lsa_SidType type;
1229 if (argc == 0) {
1230 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1231 return NT_STATUS_INVALID_PARAMETER;
1234 ZERO_STRUCT(connect_pol);
1235 ZERO_STRUCT(domain_pol);
1236 ZERO_STRUCT(user_pol);
1238 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1239 argv[0], NULL, NULL, &sid, &type);
1240 if (!NT_STATUS_IS_OK(result)) {
1241 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1242 nt_errstr(result));
1243 goto done;
1246 if (type != SID_NAME_USER) {
1247 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1248 sid_type_lookup(type));
1249 result = NT_STATUS_NO_SUCH_USER;
1250 goto done;
1253 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1254 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1255 result = NT_STATUS_NO_SUCH_USER;
1256 goto done;
1259 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1260 pipe_hnd->desthost,
1261 MAXIMUM_ALLOWED_ACCESS,
1262 &connect_pol);
1263 if (!NT_STATUS_IS_OK(result)) {
1264 goto done;
1267 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1268 &connect_pol,
1269 MAXIMUM_ALLOWED_ACCESS,
1270 ctx->domain_sid,
1271 &domain_pol);
1272 if (!NT_STATUS_IS_OK(result)) {
1273 goto done;
1276 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1277 &domain_pol,
1278 MAXIMUM_ALLOWED_ACCESS,
1279 rid,
1280 &user_pol);
1281 if (!NT_STATUS_IS_OK(result)) {
1282 goto done;
1285 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1287 done:
1288 if (is_valid_policy_hnd(&user_pol)) {
1289 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1291 if (is_valid_policy_hnd(&domain_pol)) {
1292 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1294 if (is_valid_policy_hnd(&connect_pol)) {
1295 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1297 return result;
1300 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1301 TALLOC_CTX *mem_ctx,
1302 struct rpc_sh_ctx *ctx,
1303 struct rpc_pipe_client *pipe_hnd,
1304 POLICY_HND *user_hnd,
1305 int argc, const char **argv)
1307 NTSTATUS result;
1308 union samr_UserInfo *info = NULL;
1310 if (argc != 0) {
1311 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1312 return NT_STATUS_INVALID_PARAMETER;
1315 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1316 user_hnd,
1318 &info);
1319 if (!NT_STATUS_IS_OK(result)) {
1320 return result;
1323 d_printf("user rid: %d, group rid: %d\n",
1324 info->info21.rid,
1325 info->info21.primary_gid);
1327 return result;
1330 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1331 TALLOC_CTX *mem_ctx,
1332 struct rpc_sh_ctx *ctx,
1333 struct rpc_pipe_client *pipe_hnd,
1334 int argc, const char **argv)
1336 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1337 rpc_sh_user_show_internals);
1340 #define FETCHSTR(name, rec) \
1341 do { if (strequal(ctx->thiscmd, name)) { \
1342 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1343 } while (0);
1345 #define SETSTR(name, rec, flag) \
1346 do { if (strequal(ctx->thiscmd, name)) { \
1347 init_lsa_String(&(info->info21.rec), argv[0]); \
1348 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1349 } while (0);
1351 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1352 TALLOC_CTX *mem_ctx,
1353 struct rpc_sh_ctx *ctx,
1354 struct rpc_pipe_client *pipe_hnd,
1355 POLICY_HND *user_hnd,
1356 int argc, const char **argv)
1358 NTSTATUS result;
1359 const char *username;
1360 const char *oldval = "";
1361 union samr_UserInfo *info = NULL;
1363 if (argc > 1) {
1364 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1365 ctx->whoami);
1366 return NT_STATUS_INVALID_PARAMETER;
1369 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1370 user_hnd,
1372 &info);
1373 if (!NT_STATUS_IS_OK(result)) {
1374 return result;
1377 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1379 FETCHSTR("fullname", full_name);
1380 FETCHSTR("homedir", home_directory);
1381 FETCHSTR("homedrive", home_drive);
1382 FETCHSTR("logonscript", logon_script);
1383 FETCHSTR("profilepath", profile_path);
1384 FETCHSTR("description", description);
1386 if (argc == 0) {
1387 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1388 goto done;
1391 if (strcmp(argv[0], "NULL") == 0) {
1392 argv[0] = "";
1395 ZERO_STRUCT(info->info21);
1397 SETSTR("fullname", full_name, FULL_NAME);
1398 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1399 SETSTR("homedrive", home_drive, HOME_DRIVE);
1400 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1401 SETSTR("profilepath", profile_path, PROFILE_PATH);
1402 SETSTR("description", description, DESCRIPTION);
1404 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1405 user_hnd,
1407 info);
1409 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1410 ctx->thiscmd, oldval, argv[0]);
1412 done:
1414 return result;
1417 #define HANDLEFLG(name, rec) \
1418 do { if (strequal(ctx->thiscmd, name)) { \
1419 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1420 if (newval) { \
1421 newflags = oldflags | ACB_##rec; \
1422 } else { \
1423 newflags = oldflags & ~ACB_##rec; \
1424 } } } while (0);
1426 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1427 TALLOC_CTX *mem_ctx,
1428 struct rpc_sh_ctx *ctx,
1429 struct rpc_pipe_client *pipe_hnd,
1430 int argc, const char **argv)
1432 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1433 rpc_sh_user_str_edit_internals);
1436 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1437 TALLOC_CTX *mem_ctx,
1438 struct rpc_sh_ctx *ctx,
1439 struct rpc_pipe_client *pipe_hnd,
1440 POLICY_HND *user_hnd,
1441 int argc, const char **argv)
1443 NTSTATUS result;
1444 const char *username;
1445 const char *oldval = "unknown";
1446 uint32 oldflags, newflags;
1447 bool newval;
1448 union samr_UserInfo *info = NULL;
1450 if ((argc > 1) ||
1451 ((argc == 1) && !strequal(argv[0], "yes") &&
1452 !strequal(argv[0], "no"))) {
1453 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1454 ctx->whoami);
1455 return NT_STATUS_INVALID_PARAMETER;
1458 newval = strequal(argv[0], "yes");
1460 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1461 user_hnd,
1463 &info);
1464 if (!NT_STATUS_IS_OK(result)) {
1465 return result;
1468 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1469 oldflags = info->info21.acct_flags;
1470 newflags = info->info21.acct_flags;
1472 HANDLEFLG("disabled", DISABLED);
1473 HANDLEFLG("pwnotreq", PWNOTREQ);
1474 HANDLEFLG("autolock", AUTOLOCK);
1475 HANDLEFLG("pwnoexp", PWNOEXP);
1477 if (argc == 0) {
1478 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1479 goto done;
1482 ZERO_STRUCT(info->info21);
1484 info->info21.acct_flags = newflags;
1485 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1487 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1488 user_hnd,
1490 info);
1492 if (NT_STATUS_IS_OK(result)) {
1493 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1494 ctx->thiscmd, oldval, argv[0]);
1497 done:
1499 return result;
1502 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1503 TALLOC_CTX *mem_ctx,
1504 struct rpc_sh_ctx *ctx,
1505 struct rpc_pipe_client *pipe_hnd,
1506 int argc, const char **argv)
1508 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1509 rpc_sh_user_flag_edit_internals);
1512 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1513 TALLOC_CTX *mem_ctx,
1514 struct rpc_sh_ctx *ctx)
1516 static struct rpc_sh_cmd cmds[] = {
1518 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1519 "Show/Set a user's full name" },
1521 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1522 "Show/Set a user's home directory" },
1524 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1525 "Show/Set a user's home drive" },
1527 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1528 "Show/Set a user's logon script" },
1530 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1531 "Show/Set a user's profile path" },
1533 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1534 "Show/Set a user's description" },
1536 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1537 "Show/Set whether a user is disabled" },
1539 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1540 "Show/Set whether a user locked out" },
1542 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1543 "Show/Set whether a user does not need a password" },
1545 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1546 "Show/Set whether a user's password does not expire" },
1548 { NULL, NULL, 0, NULL, NULL }
1551 return cmds;
1554 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1555 TALLOC_CTX *mem_ctx,
1556 struct rpc_sh_ctx *ctx)
1558 static struct rpc_sh_cmd cmds[] = {
1560 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1561 "List available users" },
1563 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1564 "List the domain groups a user is member of" },
1566 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1567 "Show info about a user" },
1569 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1570 "Show/Modify a user's fields" },
1572 { NULL, NULL, 0, NULL, NULL }
1575 return cmds;
1578 /****************************************************************************/
1581 * Basic usage function for 'net rpc group'
1582 * @param argc Standard main() style argc.
1583 * @param argv Standard main() style argv. Initial components are already
1584 * stripped.
1587 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1589 return net_group_usage(c, argc, argv);
1593 * Delete group on a remote RPC server
1595 * All parameters are provided by the run_rpc_command function, except for
1596 * argc, argv which are passes through.
1598 * @param domain_sid The domain sid acquired from the remote server
1599 * @param cli A cli_state connected to the server.
1600 * @param mem_ctx Talloc context, destoyed on completion of the function.
1601 * @param argc Standard main() style argc
1602 * @param argv Standard main() style argv. Initial components are already
1603 * stripped
1605 * @return Normal NTSTATUS return.
1608 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1609 const DOM_SID *domain_sid,
1610 const char *domain_name,
1611 struct cli_state *cli,
1612 struct rpc_pipe_client *pipe_hnd,
1613 TALLOC_CTX *mem_ctx,
1614 int argc,
1615 const char **argv)
1617 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1618 bool group_is_primary = false;
1619 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1620 uint32_t group_rid;
1621 struct samr_RidTypeArray *rids = NULL;
1622 /* char **names; */
1623 int i;
1624 /* DOM_GID *user_gids; */
1626 struct samr_Ids group_rids, name_types;
1627 struct lsa_String lsa_acct_name;
1628 union samr_UserInfo *info = NULL;
1630 if (argc < 1) {
1631 d_printf("specify group\n");
1632 rpc_group_usage(c, argc,argv);
1633 return NT_STATUS_OK; /* ok? */
1636 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1637 pipe_hnd->desthost,
1638 MAXIMUM_ALLOWED_ACCESS,
1639 &connect_pol);
1641 if (!NT_STATUS_IS_OK(result)) {
1642 d_fprintf(stderr, "Request samr_Connect2 failed\n");
1643 goto done;
1646 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1647 &connect_pol,
1648 MAXIMUM_ALLOWED_ACCESS,
1649 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1650 &domain_pol);
1652 if (!NT_STATUS_IS_OK(result)) {
1653 d_fprintf(stderr, "Request open_domain failed\n");
1654 goto done;
1657 init_lsa_String(&lsa_acct_name, argv[0]);
1659 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1660 &domain_pol,
1662 &lsa_acct_name,
1663 &group_rids,
1664 &name_types);
1665 if (!NT_STATUS_IS_OK(result)) {
1666 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1667 goto done;
1670 switch (name_types.ids[0])
1672 case SID_NAME_DOM_GRP:
1673 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1674 &domain_pol,
1675 MAXIMUM_ALLOWED_ACCESS,
1676 group_rids.ids[0],
1677 &group_pol);
1678 if (!NT_STATUS_IS_OK(result)) {
1679 d_fprintf(stderr, "Request open_group failed");
1680 goto done;
1683 group_rid = group_rids.ids[0];
1685 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1686 &group_pol,
1687 &rids);
1689 if (!NT_STATUS_IS_OK(result)) {
1690 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1691 goto done;
1694 if (c->opt_verbose) {
1695 d_printf("Domain Group %s (rid: %d) has %d members\n",
1696 argv[0],group_rid, rids->count);
1699 /* Check if group is anyone's primary group */
1700 for (i = 0; i < rids->count; i++)
1702 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1703 &domain_pol,
1704 MAXIMUM_ALLOWED_ACCESS,
1705 rids->rids[i],
1706 &user_pol);
1708 if (!NT_STATUS_IS_OK(result)) {
1709 d_fprintf(stderr, "Unable to open group member %d\n",
1710 rids->rids[i]);
1711 goto done;
1714 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1715 &user_pol,
1717 &info);
1719 if (!NT_STATUS_IS_OK(result)) {
1720 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",
1721 rids->rids[i]);
1722 goto done;
1725 if (info->info21.primary_gid == group_rid) {
1726 if (c->opt_verbose) {
1727 d_printf("Group is primary group of %s\n",
1728 info->info21.account_name.string);
1730 group_is_primary = true;
1733 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1736 if (group_is_primary) {
1737 d_fprintf(stderr, "Unable to delete group because some "
1738 "of it's members have it as primary group\n");
1739 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1740 goto done;
1743 /* remove all group members */
1744 for (i = 0; i < rids->count; i++)
1746 if (c->opt_verbose)
1747 d_printf("Remove group member %d...",
1748 rids->rids[i]);
1749 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1750 &group_pol,
1751 rids->rids[i]);
1753 if (NT_STATUS_IS_OK(result)) {
1754 if (c->opt_verbose)
1755 d_printf("ok\n");
1756 } else {
1757 if (c->opt_verbose)
1758 d_printf("failed\n");
1759 goto done;
1763 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1764 &group_pol);
1766 break;
1767 /* removing a local group is easier... */
1768 case SID_NAME_ALIAS:
1769 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1770 &domain_pol,
1771 MAXIMUM_ALLOWED_ACCESS,
1772 group_rids.ids[0],
1773 &group_pol);
1775 if (!NT_STATUS_IS_OK(result)) {
1776 d_fprintf(stderr, "Request open_alias failed\n");
1777 goto done;
1780 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1781 &group_pol);
1782 break;
1783 default:
1784 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1785 argv[0],sid_type_lookup(name_types.ids[0]));
1786 result = NT_STATUS_UNSUCCESSFUL;
1787 goto done;
1790 if (NT_STATUS_IS_OK(result)) {
1791 if (c->opt_verbose)
1792 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types.ids[0]),argv[0]);
1793 } else {
1794 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1795 get_friendly_nt_error_msg(result));
1798 done:
1799 return result;
1803 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1805 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_group_delete_internals,
1806 argc,argv);
1809 static NTSTATUS rpc_group_add_internals(struct net_context *c,
1810 const DOM_SID *domain_sid,
1811 const char *domain_name,
1812 struct cli_state *cli,
1813 struct rpc_pipe_client *pipe_hnd,
1814 TALLOC_CTX *mem_ctx,
1815 int argc,
1816 const char **argv)
1818 POLICY_HND connect_pol, domain_pol, group_pol;
1819 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1820 union samr_GroupInfo group_info;
1821 struct lsa_String grp_name;
1822 uint32_t rid = 0;
1824 if (argc != 1) {
1825 d_printf("Group name must be specified\n");
1826 rpc_group_usage(c, argc, argv);
1827 return NT_STATUS_OK;
1830 init_lsa_String(&grp_name, argv[0]);
1832 /* Get sam policy handle */
1834 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1835 pipe_hnd->desthost,
1836 MAXIMUM_ALLOWED_ACCESS,
1837 &connect_pol);
1838 if (!NT_STATUS_IS_OK(result)) goto done;
1840 /* Get domain policy handle */
1842 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1843 &connect_pol,
1844 MAXIMUM_ALLOWED_ACCESS,
1845 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1846 &domain_pol);
1847 if (!NT_STATUS_IS_OK(result)) goto done;
1849 /* Create the group */
1851 result = rpccli_samr_CreateDomainGroup(pipe_hnd, mem_ctx,
1852 &domain_pol,
1853 &grp_name,
1854 MAXIMUM_ALLOWED_ACCESS,
1855 &group_pol,
1856 &rid);
1857 if (!NT_STATUS_IS_OK(result)) goto done;
1859 if (strlen(c->opt_comment) == 0) goto done;
1861 /* We've got a comment to set */
1863 init_lsa_String(&group_info.description, c->opt_comment);
1865 result = rpccli_samr_SetGroupInfo(pipe_hnd, mem_ctx,
1866 &group_pol,
1868 &group_info);
1869 if (!NT_STATUS_IS_OK(result)) goto done;
1871 done:
1872 if (NT_STATUS_IS_OK(result))
1873 DEBUG(5, ("add group succeeded\n"));
1874 else
1875 d_fprintf(stderr, "add group failed: %s\n", nt_errstr(result));
1877 return result;
1880 static NTSTATUS rpc_alias_add_internals(struct net_context *c,
1881 const DOM_SID *domain_sid,
1882 const char *domain_name,
1883 struct cli_state *cli,
1884 struct rpc_pipe_client *pipe_hnd,
1885 TALLOC_CTX *mem_ctx,
1886 int argc,
1887 const char **argv)
1889 POLICY_HND connect_pol, domain_pol, alias_pol;
1890 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1891 union samr_AliasInfo alias_info;
1892 struct lsa_String alias_name;
1893 uint32_t rid = 0;
1895 if (argc != 1) {
1896 d_printf("Alias name must be specified\n");
1897 rpc_group_usage(c, argc, argv);
1898 return NT_STATUS_OK;
1901 init_lsa_String(&alias_name, argv[0]);
1903 /* Get sam policy handle */
1905 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1906 pipe_hnd->desthost,
1907 MAXIMUM_ALLOWED_ACCESS,
1908 &connect_pol);
1909 if (!NT_STATUS_IS_OK(result)) goto done;
1911 /* Get domain policy handle */
1913 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1914 &connect_pol,
1915 MAXIMUM_ALLOWED_ACCESS,
1916 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1917 &domain_pol);
1918 if (!NT_STATUS_IS_OK(result)) goto done;
1920 /* Create the group */
1922 result = rpccli_samr_CreateDomAlias(pipe_hnd, mem_ctx,
1923 &domain_pol,
1924 &alias_name,
1925 MAXIMUM_ALLOWED_ACCESS,
1926 &alias_pol,
1927 &rid);
1928 if (!NT_STATUS_IS_OK(result)) goto done;
1930 if (strlen(c->opt_comment) == 0) goto done;
1932 /* We've got a comment to set */
1934 init_lsa_String(&alias_info.description, c->opt_comment);
1936 result = rpccli_samr_SetAliasInfo(pipe_hnd, mem_ctx,
1937 &alias_pol,
1939 &alias_info);
1941 if (!NT_STATUS_IS_OK(result)) goto done;
1943 done:
1944 if (NT_STATUS_IS_OK(result))
1945 DEBUG(5, ("add alias succeeded\n"));
1946 else
1947 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1949 return result;
1952 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1954 if (c->opt_localgroup)
1955 return run_rpc_command(c, NULL, PI_SAMR, 0,
1956 rpc_alias_add_internals,
1957 argc, argv);
1959 return run_rpc_command(c, NULL, PI_SAMR, 0,
1960 rpc_group_add_internals,
1961 argc, argv);
1964 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1965 TALLOC_CTX *mem_ctx,
1966 const char *name,
1967 DOM_SID *sid,
1968 enum lsa_SidType *type)
1970 DOM_SID *sids = NULL;
1971 enum lsa_SidType *types = NULL;
1972 struct rpc_pipe_client *pipe_hnd;
1973 POLICY_HND lsa_pol;
1974 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1976 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
1977 if (!pipe_hnd) {
1978 goto done;
1981 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1982 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1984 if (!NT_STATUS_IS_OK(result)) {
1985 goto done;
1988 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1989 &name, NULL, 1, &sids, &types);
1991 if (NT_STATUS_IS_OK(result)) {
1992 sid_copy(sid, &sids[0]);
1993 *type = types[0];
1996 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1998 done:
1999 if (pipe_hnd) {
2000 TALLOC_FREE(pipe_hnd);
2003 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
2005 /* Try as S-1-5-whatever */
2007 DOM_SID tmp_sid;
2009 if (string_to_sid(&tmp_sid, name)) {
2010 sid_copy(sid, &tmp_sid);
2011 *type = SID_NAME_UNKNOWN;
2012 result = NT_STATUS_OK;
2016 return result;
2019 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2020 TALLOC_CTX *mem_ctx,
2021 const DOM_SID *group_sid,
2022 const char *member)
2024 POLICY_HND connect_pol, domain_pol;
2025 NTSTATUS result;
2026 uint32 group_rid;
2027 POLICY_HND group_pol;
2029 struct samr_Ids rids, rid_types;
2030 struct lsa_String lsa_acct_name;
2032 DOM_SID sid;
2034 sid_copy(&sid, group_sid);
2036 if (!sid_split_rid(&sid, &group_rid)) {
2037 return NT_STATUS_UNSUCCESSFUL;
2040 /* Get sam policy handle */
2041 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2042 pipe_hnd->desthost,
2043 MAXIMUM_ALLOWED_ACCESS,
2044 &connect_pol);
2045 if (!NT_STATUS_IS_OK(result)) {
2046 return result;
2049 /* Get domain policy handle */
2050 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2051 &connect_pol,
2052 MAXIMUM_ALLOWED_ACCESS,
2053 &sid,
2054 &domain_pol);
2055 if (!NT_STATUS_IS_OK(result)) {
2056 return result;
2059 init_lsa_String(&lsa_acct_name, member);
2061 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2062 &domain_pol,
2064 &lsa_acct_name,
2065 &rids,
2066 &rid_types);
2068 if (!NT_STATUS_IS_OK(result)) {
2069 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2070 goto done;
2073 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2074 &domain_pol,
2075 MAXIMUM_ALLOWED_ACCESS,
2076 group_rid,
2077 &group_pol);
2079 if (!NT_STATUS_IS_OK(result)) {
2080 goto done;
2083 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
2084 &group_pol,
2085 rids.ids[0],
2086 0x0005); /* unknown flags */
2088 done:
2089 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2090 return result;
2093 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2094 TALLOC_CTX *mem_ctx,
2095 const DOM_SID *alias_sid,
2096 const char *member)
2098 POLICY_HND connect_pol, domain_pol;
2099 NTSTATUS result;
2100 uint32 alias_rid;
2101 POLICY_HND alias_pol;
2103 DOM_SID member_sid;
2104 enum lsa_SidType member_type;
2106 DOM_SID sid;
2108 sid_copy(&sid, alias_sid);
2110 if (!sid_split_rid(&sid, &alias_rid)) {
2111 return NT_STATUS_UNSUCCESSFUL;
2114 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2115 member, &member_sid, &member_type);
2117 if (!NT_STATUS_IS_OK(result)) {
2118 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2119 return result;
2122 /* Get sam policy handle */
2123 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2124 pipe_hnd->desthost,
2125 MAXIMUM_ALLOWED_ACCESS,
2126 &connect_pol);
2127 if (!NT_STATUS_IS_OK(result)) {
2128 goto done;
2131 /* Get domain policy handle */
2132 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2133 &connect_pol,
2134 MAXIMUM_ALLOWED_ACCESS,
2135 &sid,
2136 &domain_pol);
2137 if (!NT_STATUS_IS_OK(result)) {
2138 goto done;
2141 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2142 &domain_pol,
2143 MAXIMUM_ALLOWED_ACCESS,
2144 alias_rid,
2145 &alias_pol);
2147 if (!NT_STATUS_IS_OK(result)) {
2148 return result;
2151 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
2152 &alias_pol,
2153 &member_sid);
2155 if (!NT_STATUS_IS_OK(result)) {
2156 return result;
2159 done:
2160 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2161 return result;
2164 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2165 const DOM_SID *domain_sid,
2166 const char *domain_name,
2167 struct cli_state *cli,
2168 struct rpc_pipe_client *pipe_hnd,
2169 TALLOC_CTX *mem_ctx,
2170 int argc,
2171 const char **argv)
2173 DOM_SID group_sid;
2174 enum lsa_SidType group_type;
2176 if (argc != 2) {
2177 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2178 return NT_STATUS_UNSUCCESSFUL;
2181 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2182 &group_sid, &group_type))) {
2183 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2184 return NT_STATUS_UNSUCCESSFUL;
2187 if (group_type == SID_NAME_DOM_GRP) {
2188 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2189 &group_sid, argv[1]);
2191 if (!NT_STATUS_IS_OK(result)) {
2192 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2193 argv[1], argv[0], nt_errstr(result));
2195 return result;
2198 if (group_type == SID_NAME_ALIAS) {
2199 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2200 &group_sid, argv[1]);
2202 if (!NT_STATUS_IS_OK(result)) {
2203 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2204 argv[1], argv[0], nt_errstr(result));
2206 return result;
2209 d_fprintf(stderr, "Can only add members to global or local groups "
2210 "which %s is not\n", argv[0]);
2212 return NT_STATUS_UNSUCCESSFUL;
2215 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2217 return run_rpc_command(c, NULL, PI_SAMR, 0,
2218 rpc_group_addmem_internals,
2219 argc, argv);
2222 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2223 struct rpc_pipe_client *pipe_hnd,
2224 TALLOC_CTX *mem_ctx,
2225 const DOM_SID *group_sid,
2226 const char *member)
2228 POLICY_HND connect_pol, domain_pol;
2229 NTSTATUS result;
2230 uint32 group_rid;
2231 POLICY_HND group_pol;
2233 struct samr_Ids rids, rid_types;
2234 struct lsa_String lsa_acct_name;
2236 DOM_SID sid;
2238 sid_copy(&sid, group_sid);
2240 if (!sid_split_rid(&sid, &group_rid))
2241 return NT_STATUS_UNSUCCESSFUL;
2243 /* Get sam policy handle */
2244 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2245 pipe_hnd->desthost,
2246 MAXIMUM_ALLOWED_ACCESS,
2247 &connect_pol);
2248 if (!NT_STATUS_IS_OK(result))
2249 return result;
2251 /* Get domain policy handle */
2252 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2253 &connect_pol,
2254 MAXIMUM_ALLOWED_ACCESS,
2255 &sid,
2256 &domain_pol);
2257 if (!NT_STATUS_IS_OK(result))
2258 return result;
2260 init_lsa_String(&lsa_acct_name, member);
2262 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2263 &domain_pol,
2265 &lsa_acct_name,
2266 &rids,
2267 &rid_types);
2268 if (!NT_STATUS_IS_OK(result)) {
2269 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2270 goto done;
2273 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2274 &domain_pol,
2275 MAXIMUM_ALLOWED_ACCESS,
2276 group_rid,
2277 &group_pol);
2279 if (!NT_STATUS_IS_OK(result))
2280 goto done;
2282 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2283 &group_pol,
2284 rids.ids[0]);
2286 done:
2287 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2288 return result;
2291 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2292 TALLOC_CTX *mem_ctx,
2293 const DOM_SID *alias_sid,
2294 const char *member)
2296 POLICY_HND connect_pol, domain_pol;
2297 NTSTATUS result;
2298 uint32 alias_rid;
2299 POLICY_HND alias_pol;
2301 DOM_SID member_sid;
2302 enum lsa_SidType member_type;
2304 DOM_SID sid;
2306 sid_copy(&sid, alias_sid);
2308 if (!sid_split_rid(&sid, &alias_rid))
2309 return NT_STATUS_UNSUCCESSFUL;
2311 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2312 member, &member_sid, &member_type);
2314 if (!NT_STATUS_IS_OK(result)) {
2315 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2316 return result;
2319 /* Get sam policy handle */
2320 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2321 pipe_hnd->desthost,
2322 MAXIMUM_ALLOWED_ACCESS,
2323 &connect_pol);
2324 if (!NT_STATUS_IS_OK(result)) {
2325 goto done;
2328 /* Get domain policy handle */
2329 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2330 &connect_pol,
2331 MAXIMUM_ALLOWED_ACCESS,
2332 &sid,
2333 &domain_pol);
2334 if (!NT_STATUS_IS_OK(result)) {
2335 goto done;
2338 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2339 &domain_pol,
2340 MAXIMUM_ALLOWED_ACCESS,
2341 alias_rid,
2342 &alias_pol);
2344 if (!NT_STATUS_IS_OK(result))
2345 return result;
2347 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2348 &alias_pol,
2349 &member_sid);
2351 if (!NT_STATUS_IS_OK(result))
2352 return result;
2354 done:
2355 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2356 return result;
2359 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2360 const DOM_SID *domain_sid,
2361 const char *domain_name,
2362 struct cli_state *cli,
2363 struct rpc_pipe_client *pipe_hnd,
2364 TALLOC_CTX *mem_ctx,
2365 int argc,
2366 const char **argv)
2368 DOM_SID group_sid;
2369 enum lsa_SidType group_type;
2371 if (argc != 2) {
2372 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2373 return NT_STATUS_UNSUCCESSFUL;
2376 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2377 &group_sid, &group_type))) {
2378 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2379 return NT_STATUS_UNSUCCESSFUL;
2382 if (group_type == SID_NAME_DOM_GRP) {
2383 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2384 &group_sid, argv[1]);
2386 if (!NT_STATUS_IS_OK(result)) {
2387 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2388 argv[1], argv[0], nt_errstr(result));
2390 return result;
2393 if (group_type == SID_NAME_ALIAS) {
2394 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2395 &group_sid, argv[1]);
2397 if (!NT_STATUS_IS_OK(result)) {
2398 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2399 argv[1], argv[0], nt_errstr(result));
2401 return result;
2404 d_fprintf(stderr, "Can only delete members from global or local groups "
2405 "which %s is not\n", argv[0]);
2407 return NT_STATUS_UNSUCCESSFUL;
2410 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2412 return run_rpc_command(c, NULL, PI_SAMR, 0,
2413 rpc_group_delmem_internals,
2414 argc, argv);
2418 * List groups on a remote RPC server
2420 * All parameters are provided by the run_rpc_command function, except for
2421 * argc, argv which are passes through.
2423 * @param domain_sid The domain sid acquired from the remote server
2424 * @param cli A cli_state connected to the server.
2425 * @param mem_ctx Talloc context, destoyed on completion of the function.
2426 * @param argc Standard main() style argc
2427 * @param argv Standard main() style argv. Initial components are already
2428 * stripped
2430 * @return Normal NTSTATUS return.
2433 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2434 const DOM_SID *domain_sid,
2435 const char *domain_name,
2436 struct cli_state *cli,
2437 struct rpc_pipe_client *pipe_hnd,
2438 TALLOC_CTX *mem_ctx,
2439 int argc,
2440 const char **argv)
2442 POLICY_HND connect_pol, domain_pol;
2443 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2444 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2445 struct samr_SamArray *groups = NULL;
2446 bool global = false;
2447 bool local = false;
2448 bool builtin = false;
2450 if (argc == 0) {
2451 global = true;
2452 local = true;
2453 builtin = true;
2456 for (i=0; i<argc; i++) {
2457 if (strequal(argv[i], "global"))
2458 global = true;
2460 if (strequal(argv[i], "local"))
2461 local = true;
2463 if (strequal(argv[i], "builtin"))
2464 builtin = true;
2467 /* Get sam policy handle */
2469 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2470 pipe_hnd->desthost,
2471 MAXIMUM_ALLOWED_ACCESS,
2472 &connect_pol);
2473 if (!NT_STATUS_IS_OK(result)) {
2474 goto done;
2477 /* Get domain policy handle */
2479 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2480 &connect_pol,
2481 MAXIMUM_ALLOWED_ACCESS,
2482 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2483 &domain_pol);
2484 if (!NT_STATUS_IS_OK(result)) {
2485 goto done;
2488 /* Query domain groups */
2489 if (c->opt_long_list_entries)
2490 d_printf("\nGroup name Comment"\
2491 "\n-----------------------------\n");
2492 do {
2493 uint32_t max_size, total_size, returned_size;
2494 union samr_DispInfo info;
2496 if (!global) break;
2498 get_query_dispinfo_params(
2499 loop_count, &max_entries, &max_size);
2501 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2502 &domain_pol,
2504 start_idx,
2505 max_entries,
2506 max_size,
2507 &total_size,
2508 &returned_size,
2509 &info);
2510 num_entries = info.info3.count;
2511 start_idx += info.info3.count;
2513 if (!NT_STATUS_IS_OK(result) &&
2514 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2515 break;
2517 for (i = 0; i < num_entries; i++) {
2519 const char *group = NULL;
2520 const char *desc = NULL;
2522 group = info.info3.entries[i].account_name.string;
2523 desc = info.info3.entries[i].description.string;
2525 if (c->opt_long_list_entries)
2526 printf("%-21.21s %-50.50s\n",
2527 group, desc);
2528 else
2529 printf("%s\n", group);
2531 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2532 /* query domain aliases */
2533 start_idx = 0;
2534 do {
2535 if (!local) break;
2537 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2538 &domain_pol,
2539 &start_idx,
2540 &groups,
2541 0xffff,
2542 &num_entries);
2543 if (!NT_STATUS_IS_OK(result) &&
2544 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2545 break;
2547 for (i = 0; i < num_entries; i++) {
2549 const char *description = NULL;
2551 if (c->opt_long_list_entries) {
2553 POLICY_HND alias_pol;
2554 union samr_AliasInfo *info = NULL;
2556 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2557 &domain_pol,
2558 0x8,
2559 groups->entries[i].idx,
2560 &alias_pol))) &&
2561 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2562 &alias_pol,
2564 &info))) &&
2565 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2566 &alias_pol)))) {
2567 description = info->description.string;
2571 if (description != NULL) {
2572 printf("%-21.21s %-50.50s\n",
2573 groups->entries[i].name.string,
2574 description);
2575 } else {
2576 printf("%s\n", groups->entries[i].name.string);
2579 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2580 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2581 /* Get builtin policy handle */
2583 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2584 &connect_pol,
2585 MAXIMUM_ALLOWED_ACCESS,
2586 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2587 &domain_pol);
2588 if (!NT_STATUS_IS_OK(result)) {
2589 goto done;
2591 /* query builtin aliases */
2592 start_idx = 0;
2593 do {
2594 if (!builtin) break;
2596 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2597 &domain_pol,
2598 &start_idx,
2599 &groups,
2600 max_entries,
2601 &num_entries);
2602 if (!NT_STATUS_IS_OK(result) &&
2603 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2604 break;
2606 for (i = 0; i < num_entries; i++) {
2608 const char *description = NULL;
2610 if (c->opt_long_list_entries) {
2612 POLICY_HND alias_pol;
2613 union samr_AliasInfo *info = NULL;
2615 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2616 &domain_pol,
2617 0x8,
2618 groups->entries[i].idx,
2619 &alias_pol))) &&
2620 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2621 &alias_pol,
2623 &info))) &&
2624 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2625 &alias_pol)))) {
2626 description = info->description.string;
2630 if (description != NULL) {
2631 printf("%-21.21s %-50.50s\n",
2632 groups->entries[i].name.string,
2633 description);
2634 } else {
2635 printf("%s\n", groups->entries[i].name.string);
2638 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2640 done:
2641 return result;
2644 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2646 return run_rpc_command(c, NULL, PI_SAMR, 0,
2647 rpc_group_list_internals,
2648 argc, argv);
2651 static NTSTATUS rpc_list_group_members(struct net_context *c,
2652 struct rpc_pipe_client *pipe_hnd,
2653 TALLOC_CTX *mem_ctx,
2654 const char *domain_name,
2655 const DOM_SID *domain_sid,
2656 POLICY_HND *domain_pol,
2657 uint32 rid)
2659 NTSTATUS result;
2660 POLICY_HND group_pol;
2661 uint32 num_members, *group_rids;
2662 int i;
2663 struct samr_RidTypeArray *rids = NULL;
2664 struct lsa_Strings names;
2665 struct samr_Ids types;
2667 fstring sid_str;
2668 sid_to_fstring(sid_str, domain_sid);
2670 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2671 domain_pol,
2672 MAXIMUM_ALLOWED_ACCESS,
2673 rid,
2674 &group_pol);
2676 if (!NT_STATUS_IS_OK(result))
2677 return result;
2679 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2680 &group_pol,
2681 &rids);
2683 if (!NT_STATUS_IS_OK(result))
2684 return result;
2686 num_members = rids->count;
2687 group_rids = rids->rids;
2689 while (num_members > 0) {
2690 int this_time = 512;
2692 if (num_members < this_time)
2693 this_time = num_members;
2695 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2696 domain_pol,
2697 this_time,
2698 group_rids,
2699 &names,
2700 &types);
2702 if (!NT_STATUS_IS_OK(result))
2703 return result;
2705 /* We only have users as members, but make the output
2706 the same as the output of alias members */
2708 for (i = 0; i < this_time; i++) {
2710 if (c->opt_long_list_entries) {
2711 printf("%s-%d %s\\%s %d\n", sid_str,
2712 group_rids[i], domain_name,
2713 names.names[i].string,
2714 SID_NAME_USER);
2715 } else {
2716 printf("%s\\%s\n", domain_name,
2717 names.names[i].string);
2721 num_members -= this_time;
2722 group_rids += 512;
2725 return NT_STATUS_OK;
2728 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2729 struct rpc_pipe_client *pipe_hnd,
2730 TALLOC_CTX *mem_ctx,
2731 POLICY_HND *domain_pol,
2732 uint32 rid)
2734 NTSTATUS result;
2735 struct rpc_pipe_client *lsa_pipe;
2736 POLICY_HND alias_pol, lsa_pol;
2737 uint32 num_members;
2738 DOM_SID *alias_sids;
2739 char **domains;
2740 char **names;
2741 enum lsa_SidType *types;
2742 int i;
2743 struct lsa_SidArray sid_array;
2745 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2746 domain_pol,
2747 MAXIMUM_ALLOWED_ACCESS,
2748 rid,
2749 &alias_pol);
2751 if (!NT_STATUS_IS_OK(result))
2752 return result;
2754 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2755 &alias_pol,
2756 &sid_array);
2758 if (!NT_STATUS_IS_OK(result)) {
2759 d_fprintf(stderr, "Couldn't list alias members\n");
2760 return result;
2763 num_members = sid_array.num_sids;
2765 if (num_members == 0) {
2766 return NT_STATUS_OK;
2769 lsa_pipe = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2770 PI_LSARPC, &result);
2771 if (!lsa_pipe) {
2772 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2773 nt_errstr(result) );
2774 return result;
2777 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2778 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2780 if (!NT_STATUS_IS_OK(result)) {
2781 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2782 TALLOC_FREE(lsa_pipe);
2783 return result;
2786 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2787 if (!alias_sids) {
2788 d_fprintf(stderr, "Out of memory\n");
2789 TALLOC_FREE(lsa_pipe);
2790 return NT_STATUS_NO_MEMORY;
2793 for (i=0; i<num_members; i++) {
2794 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2797 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2798 num_members, alias_sids,
2799 &domains, &names, &types);
2801 if (!NT_STATUS_IS_OK(result) &&
2802 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2803 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2804 TALLOC_FREE(lsa_pipe);
2805 return result;
2808 for (i = 0; i < num_members; i++) {
2809 fstring sid_str;
2810 sid_to_fstring(sid_str, &alias_sids[i]);
2812 if (c->opt_long_list_entries) {
2813 printf("%s %s\\%s %d\n", sid_str,
2814 domains[i] ? domains[i] : "*unknown*",
2815 names[i] ? names[i] : "*unknown*", types[i]);
2816 } else {
2817 if (domains[i])
2818 printf("%s\\%s\n", domains[i], names[i]);
2819 else
2820 printf("%s\n", sid_str);
2824 TALLOC_FREE(lsa_pipe);
2825 return NT_STATUS_OK;
2828 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2829 const DOM_SID *domain_sid,
2830 const char *domain_name,
2831 struct cli_state *cli,
2832 struct rpc_pipe_client *pipe_hnd,
2833 TALLOC_CTX *mem_ctx,
2834 int argc,
2835 const char **argv)
2837 NTSTATUS result;
2838 POLICY_HND connect_pol, domain_pol;
2839 struct samr_Ids rids, rid_types;
2840 struct lsa_String lsa_acct_name;
2842 /* Get sam policy handle */
2844 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2845 pipe_hnd->desthost,
2846 MAXIMUM_ALLOWED_ACCESS,
2847 &connect_pol);
2849 if (!NT_STATUS_IS_OK(result))
2850 return result;
2852 /* Get domain policy handle */
2854 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2855 &connect_pol,
2856 MAXIMUM_ALLOWED_ACCESS,
2857 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2858 &domain_pol);
2860 if (!NT_STATUS_IS_OK(result))
2861 return result;
2863 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2865 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2866 &domain_pol,
2868 &lsa_acct_name,
2869 &rids,
2870 &rid_types);
2872 if (!NT_STATUS_IS_OK(result)) {
2874 /* Ok, did not find it in the global sam, try with builtin */
2876 DOM_SID sid_Builtin;
2878 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2880 sid_copy(&sid_Builtin, &global_sid_Builtin);
2882 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2883 &connect_pol,
2884 MAXIMUM_ALLOWED_ACCESS,
2885 &sid_Builtin,
2886 &domain_pol);
2888 if (!NT_STATUS_IS_OK(result)) {
2889 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2890 return result;
2893 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2894 &domain_pol,
2896 &lsa_acct_name,
2897 &rids,
2898 &rid_types);
2900 if (!NT_STATUS_IS_OK(result)) {
2901 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2902 return result;
2906 if (rids.count != 1) {
2907 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2908 return result;
2911 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2912 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2913 domain_sid, &domain_pol,
2914 rids.ids[0]);
2917 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2918 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2919 rids.ids[0]);
2922 return NT_STATUS_NO_SUCH_GROUP;
2925 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2927 if (argc != 1) {
2928 return rpc_group_usage(c, argc, argv);
2931 return run_rpc_command(c, NULL, PI_SAMR, 0,
2932 rpc_group_members_internals,
2933 argc, argv);
2936 static NTSTATUS rpc_group_rename_internals(struct net_context *c,
2937 const DOM_SID *domain_sid,
2938 const char *domain_name,
2939 struct cli_state *cli,
2940 struct rpc_pipe_client *pipe_hnd,
2941 TALLOC_CTX *mem_ctx,
2942 int argc,
2943 const char **argv)
2945 NTSTATUS result;
2946 POLICY_HND connect_pol, domain_pol, group_pol;
2947 union samr_GroupInfo group_info;
2948 struct samr_Ids rids, rid_types;
2949 struct lsa_String lsa_acct_name;
2951 if (argc != 2) {
2952 d_printf("Usage: 'net rpc group rename group newname'\n");
2953 return NT_STATUS_UNSUCCESSFUL;
2956 /* Get sam policy handle */
2958 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2959 pipe_hnd->desthost,
2960 MAXIMUM_ALLOWED_ACCESS,
2961 &connect_pol);
2963 if (!NT_STATUS_IS_OK(result))
2964 return result;
2966 /* Get domain policy handle */
2968 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2969 &connect_pol,
2970 MAXIMUM_ALLOWED_ACCESS,
2971 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2972 &domain_pol);
2974 if (!NT_STATUS_IS_OK(result))
2975 return result;
2977 init_lsa_String(&lsa_acct_name, argv[0]);
2979 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2980 &domain_pol,
2982 &lsa_acct_name,
2983 &rids,
2984 &rid_types);
2986 if (rids.count != 1) {
2987 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2988 return result;
2991 if (rid_types.ids[0] != SID_NAME_DOM_GRP) {
2992 d_fprintf(stderr, "Can only rename domain groups\n");
2993 return NT_STATUS_UNSUCCESSFUL;
2996 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2997 &domain_pol,
2998 MAXIMUM_ALLOWED_ACCESS,
2999 rids.ids[0],
3000 &group_pol);
3002 if (!NT_STATUS_IS_OK(result))
3003 return result;
3005 init_lsa_String(&group_info.name, argv[1]);
3007 result = rpccli_samr_SetGroupInfo(pipe_hnd, mem_ctx,
3008 &group_pol,
3010 &group_info);
3012 if (!NT_STATUS_IS_OK(result))
3013 return result;
3015 return NT_STATUS_NO_SUCH_GROUP;
3018 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3020 if (argc != 2) {
3021 return rpc_group_usage(c, argc, argv);
3024 return run_rpc_command(c, NULL, PI_SAMR, 0,
3025 rpc_group_rename_internals,
3026 argc, argv);
3030 * 'net rpc group' entrypoint.
3031 * @param argc Standard main() style argc
3032 * @param argc Standard main() style argv. Initial components are already
3033 * stripped
3036 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3038 struct functable func[] = {
3039 {"add", rpc_group_add},
3040 {"delete", rpc_group_delete},
3041 {"addmem", rpc_group_addmem},
3042 {"delmem", rpc_group_delmem},
3043 {"list", rpc_group_list},
3044 {"members", rpc_group_members},
3045 {"rename", rpc_group_rename},
3046 {NULL, NULL}
3049 if (argc == 0) {
3050 return run_rpc_command(c, NULL, PI_SAMR, 0,
3051 rpc_group_list_internals,
3052 argc, argv);
3055 return net_run_function(c, argc, argv, func, rpc_group_usage);
3058 /****************************************************************************/
3060 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3062 return net_share_usage(c, argc, argv);
3066 * Add a share on a remote RPC server
3068 * All parameters are provided by the run_rpc_command function, except for
3069 * argc, argv which are passes through.
3071 * @param domain_sid The domain sid acquired from the remote server
3072 * @param cli A cli_state connected to the server.
3073 * @param mem_ctx Talloc context, destoyed on completion of the function.
3074 * @param argc Standard main() style argc
3075 * @param argv Standard main() style argv. Initial components are already
3076 * stripped
3078 * @return Normal NTSTATUS return.
3080 static NTSTATUS rpc_share_add_internals(struct net_context *c,
3081 const DOM_SID *domain_sid,
3082 const char *domain_name,
3083 struct cli_state *cli,
3084 struct rpc_pipe_client *pipe_hnd,
3085 TALLOC_CTX *mem_ctx,int argc,
3086 const char **argv)
3088 WERROR result;
3089 NTSTATUS status;
3090 char *sharename;
3091 char *path;
3092 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3093 uint32 num_users=0, perms=0;
3094 char *password=NULL; /* don't allow a share password */
3095 uint32 level = 2;
3096 union srvsvc_NetShareInfo info;
3097 struct srvsvc_NetShareInfo2 info2;
3098 uint32_t parm_error = 0;
3100 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3101 return NT_STATUS_NO_MEMORY;
3104 path = strchr(sharename, '=');
3105 if (!path)
3106 return NT_STATUS_UNSUCCESSFUL;
3107 *path++ = '\0';
3109 info2.name = sharename;
3110 info2.type = type;
3111 info2.comment = c->opt_comment;
3112 info2.permissions = perms;
3113 info2.max_users = c->opt_maxusers;
3114 info2.current_users = num_users;
3115 info2.path = path;
3116 info2.password = password;
3118 info.info2 = &info2;
3120 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
3121 pipe_hnd->desthost,
3122 level,
3123 &info,
3124 &parm_error,
3125 &result);
3126 return status;
3129 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3131 if ((argc < 1) || !strchr(argv[0], '=')) {
3132 DEBUG(1,("Sharename or path not specified on add\n"));
3133 return rpc_share_usage(c, argc, argv);
3135 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3136 rpc_share_add_internals,
3137 argc, argv);
3141 * Delete a share on a remote RPC server
3143 * All parameters are provided by the run_rpc_command function, except for
3144 * argc, argv which are passes through.
3146 * @param domain_sid The domain sid acquired from the remote server
3147 * @param cli A cli_state connected to the server.
3148 * @param mem_ctx Talloc context, destoyed on completion of the function.
3149 * @param argc Standard main() style argc
3150 * @param argv Standard main() style argv. Initial components are already
3151 * stripped
3153 * @return Normal NTSTATUS return.
3155 static NTSTATUS rpc_share_del_internals(struct net_context *c,
3156 const DOM_SID *domain_sid,
3157 const char *domain_name,
3158 struct cli_state *cli,
3159 struct rpc_pipe_client *pipe_hnd,
3160 TALLOC_CTX *mem_ctx,
3161 int argc,
3162 const char **argv)
3164 WERROR result;
3166 return rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
3167 pipe_hnd->desthost,
3168 argv[0],
3170 &result);
3174 * Delete a share on a remote RPC server
3176 * @param domain_sid The domain sid acquired from the remote server
3177 * @param argc Standard main() style argc
3178 * @param argv Standard main() style argv. Initial components are already
3179 * stripped
3181 * @return A shell status integer (0 for success)
3183 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3185 if (argc < 1) {
3186 DEBUG(1,("Sharename not specified on delete\n"));
3187 return rpc_share_usage(c, argc, argv);
3189 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3190 rpc_share_del_internals,
3191 argc, argv);
3195 * Formatted print of share info
3197 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3200 static void display_share_info_1(struct net_context *c,
3201 struct srvsvc_NetShareInfo1 *r)
3203 if (c->opt_long_list_entries) {
3204 d_printf("%-12s %-8.8s %-50s\n",
3205 r->name,
3206 c->share_type[r->type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)],
3207 r->comment);
3208 } else {
3209 d_printf("%s\n", r->name);
3213 static WERROR get_share_info(struct net_context *c,
3214 struct rpc_pipe_client *pipe_hnd,
3215 TALLOC_CTX *mem_ctx,
3216 uint32 level,
3217 int argc,
3218 const char **argv,
3219 struct srvsvc_NetShareInfoCtr *info_ctr)
3221 WERROR result;
3222 NTSTATUS status;
3223 union srvsvc_NetShareInfo info;
3225 /* no specific share requested, enumerate all */
3226 if (argc == 0) {
3228 uint32_t preferred_len = 0xffffffff;
3229 uint32_t total_entries = 0;
3230 uint32_t resume_handle = 0;
3232 info_ctr->level = level;
3234 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
3235 pipe_hnd->desthost,
3236 info_ctr,
3237 preferred_len,
3238 &total_entries,
3239 &resume_handle,
3240 &result);
3241 return result;
3244 /* request just one share */
3245 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
3246 pipe_hnd->desthost,
3247 argv[0],
3248 level,
3249 &info,
3250 &result);
3252 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
3253 goto done;
3256 /* construct ctr */
3257 ZERO_STRUCTP(info_ctr);
3259 info_ctr->level = level;
3261 switch (level) {
3262 case 1:
3264 struct srvsvc_NetShareCtr1 *ctr1;
3266 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3267 W_ERROR_HAVE_NO_MEMORY(ctr1);
3269 ctr1->count = 1;
3270 ctr1->array = info.info1;
3272 info_ctr->ctr.ctr1 = ctr1;
3274 case 2:
3276 struct srvsvc_NetShareCtr2 *ctr2;
3278 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3279 W_ERROR_HAVE_NO_MEMORY(ctr2);
3281 ctr2->count = 1;
3282 ctr2->array = info.info2;
3284 info_ctr->ctr.ctr2 = ctr2;
3286 case 502:
3288 struct srvsvc_NetShareCtr502 *ctr502;
3290 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3291 W_ERROR_HAVE_NO_MEMORY(ctr502);
3293 ctr502->count = 1;
3294 ctr502->array = info.info502;
3296 info_ctr->ctr.ctr502 = ctr502;
3298 } /* switch */
3299 done:
3300 return result;
3304 * List shares on a remote RPC server
3306 * All parameters are provided by the run_rpc_command function, except for
3307 * argc, argv which are passes through.
3309 * @param domain_sid The domain sid acquired from the remote server
3310 * @param cli A cli_state connected to the server.
3311 * @param mem_ctx Talloc context, destoyed on completion of the function.
3312 * @param argc Standard main() style argc
3313 * @param argv Standard main() style argv. Initial components are already
3314 * stripped
3316 * @return Normal NTSTATUS return.
3319 static NTSTATUS rpc_share_list_internals(struct net_context *c,
3320 const DOM_SID *domain_sid,
3321 const char *domain_name,
3322 struct cli_state *cli,
3323 struct rpc_pipe_client *pipe_hnd,
3324 TALLOC_CTX *mem_ctx,
3325 int argc,
3326 const char **argv)
3328 struct srvsvc_NetShareInfoCtr info_ctr;
3329 struct srvsvc_NetShareCtr1 ctr1;
3330 WERROR result;
3331 uint32 i, level = 1;
3333 ZERO_STRUCT(info_ctr);
3334 ZERO_STRUCT(ctr1);
3336 info_ctr.level = 1;
3337 info_ctr.ctr.ctr1 = &ctr1;
3339 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3340 &info_ctr);
3341 if (!W_ERROR_IS_OK(result))
3342 goto done;
3344 /* Display results */
3346 if (c->opt_long_list_entries) {
3347 d_printf(
3348 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3349 "\nShare name Type Description\n"\
3350 "---------- ---- -----------\n");
3352 for (i = 0; i < info_ctr.ctr.ctr1->count; i++)
3353 display_share_info_1(c, &info_ctr.ctr.ctr1->array[i]);
3354 done:
3355 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3358 /***
3359 * 'net rpc share list' entrypoint.
3360 * @param argc Standard main() style argc
3361 * @param argv Standard main() style argv. Initial components are already
3362 * stripped
3364 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3366 return run_rpc_command(c, NULL, PI_SRVSVC, 0, rpc_share_list_internals,
3367 argc, argv);
3370 static bool check_share_availability(struct cli_state *cli, const char *netname)
3372 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3373 d_printf("skipping [%s]: not a file share.\n", netname);
3374 return false;
3377 if (!cli_tdis(cli))
3378 return false;
3380 return true;
3383 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3384 const char *netname, uint32 type)
3386 /* only support disk shares */
3387 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3388 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3389 return false;
3392 /* skip builtin shares */
3393 /* FIXME: should print$ be added too ? */
3394 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3395 strequal(netname,"global"))
3396 return false;
3398 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3399 printf("excluding [%s]\n", netname);
3400 return false;
3403 return check_share_availability(cli, netname);
3407 * Migrate shares from a remote RPC server to the local RPC server
3409 * All parameters are provided by the run_rpc_command function, except for
3410 * argc, argv which are passed through.
3412 * @param domain_sid The domain sid acquired from the remote server
3413 * @param cli A cli_state connected to the server.
3414 * @param mem_ctx Talloc context, destroyed on completion of the function.
3415 * @param argc Standard main() style argc
3416 * @param argv Standard main() style argv. Initial components are already
3417 * stripped
3419 * @return Normal NTSTATUS return.
3422 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3423 const DOM_SID *domain_sid,
3424 const char *domain_name,
3425 struct cli_state *cli,
3426 struct rpc_pipe_client *pipe_hnd,
3427 TALLOC_CTX *mem_ctx,
3428 int argc,
3429 const char **argv)
3431 WERROR result;
3432 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3433 struct srvsvc_NetShareInfoCtr ctr_src;
3434 uint32 i;
3435 struct rpc_pipe_client *srvsvc_pipe = NULL;
3436 struct cli_state *cli_dst = NULL;
3437 uint32 level = 502; /* includes secdesc */
3438 uint32_t parm_error = 0;
3440 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3441 &ctr_src);
3442 if (!W_ERROR_IS_OK(result))
3443 goto done;
3445 /* connect destination PI_SRVSVC */
3446 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe, PI_SRVSVC);
3447 if (!NT_STATUS_IS_OK(nt_status))
3448 return nt_status;
3451 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3453 union srvsvc_NetShareInfo info;
3454 struct srvsvc_NetShareInfo502 info502 =
3455 ctr_src.ctr.ctr502->array[i];
3457 /* reset error-code */
3458 nt_status = NT_STATUS_UNSUCCESSFUL;
3460 if (!check_share_sanity(c, cli, info502.name, info502.type))
3461 continue;
3463 /* finally add the share on the dst server */
3465 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3466 info502.name, info502.path, info502.comment);
3468 info.info502 = &info502;
3470 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3471 srvsvc_pipe->desthost,
3472 502,
3473 &info,
3474 &parm_error,
3475 &result);
3477 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3478 printf(" [%s] does already exist\n",
3479 info502.name);
3480 continue;
3483 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3484 printf("cannot add share: %s\n", dos_errstr(result));
3485 goto done;
3490 nt_status = NT_STATUS_OK;
3492 done:
3493 if (cli_dst) {
3494 cli_shutdown(cli_dst);
3497 return nt_status;
3502 * Migrate shares from a rpc-server to another
3504 * @param argc Standard main() style argc
3505 * @param argv Standard main() style argv. Initial components are already
3506 * stripped
3508 * @return A shell status integer (0 for success)
3510 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3511 const char **argv)
3514 if (!c->opt_host) {
3515 printf("no server to migrate\n");
3516 return -1;
3519 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3520 rpc_share_migrate_shares_internals,
3521 argc, argv);
3525 * Copy a file/dir
3527 * @param f file_info
3528 * @param mask current search mask
3529 * @param state arg-pointer
3532 static void copy_fn(const char *mnt, file_info *f,
3533 const char *mask, void *state)
3535 static NTSTATUS nt_status;
3536 static struct copy_clistate *local_state;
3537 static fstring filename, new_mask;
3538 fstring dir;
3539 char *old_dir;
3540 struct net_context *c;
3542 local_state = (struct copy_clistate *)state;
3543 nt_status = NT_STATUS_UNSUCCESSFUL;
3545 c = local_state->c;
3547 if (strequal(f->name, ".") || strequal(f->name, ".."))
3548 return;
3550 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3552 /* DIRECTORY */
3553 if (f->mode & aDIR) {
3555 DEBUG(3,("got dir: %s\n", f->name));
3557 fstrcpy(dir, local_state->cwd);
3558 fstrcat(dir, "\\");
3559 fstrcat(dir, f->name);
3561 switch (net_mode_share)
3563 case NET_MODE_SHARE_MIGRATE:
3564 /* create that directory */
3565 nt_status = net_copy_file(c, local_state->mem_ctx,
3566 local_state->cli_share_src,
3567 local_state->cli_share_dst,
3568 dir, dir,
3569 c->opt_acls? true : false,
3570 c->opt_attrs? true : false,
3571 c->opt_timestamps? true:false,
3572 false);
3573 break;
3574 default:
3575 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3576 return;
3579 if (!NT_STATUS_IS_OK(nt_status))
3580 printf("could not handle dir %s: %s\n",
3581 dir, nt_errstr(nt_status));
3583 /* search below that directory */
3584 fstrcpy(new_mask, dir);
3585 fstrcat(new_mask, "\\*");
3587 old_dir = local_state->cwd;
3588 local_state->cwd = dir;
3589 if (!sync_files(local_state, new_mask))
3590 printf("could not handle files\n");
3591 local_state->cwd = old_dir;
3593 return;
3597 /* FILE */
3598 fstrcpy(filename, local_state->cwd);
3599 fstrcat(filename, "\\");
3600 fstrcat(filename, f->name);
3602 DEBUG(3,("got file: %s\n", filename));
3604 switch (net_mode_share)
3606 case NET_MODE_SHARE_MIGRATE:
3607 nt_status = net_copy_file(c, local_state->mem_ctx,
3608 local_state->cli_share_src,
3609 local_state->cli_share_dst,
3610 filename, filename,
3611 c->opt_acls? true : false,
3612 c->opt_attrs? true : false,
3613 c->opt_timestamps? true: false,
3614 true);
3615 break;
3616 default:
3617 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3618 return;
3621 if (!NT_STATUS_IS_OK(nt_status))
3622 printf("could not handle file %s: %s\n",
3623 filename, nt_errstr(nt_status));
3628 * sync files, can be called recursivly to list files
3629 * and then call copy_fn for each file
3631 * @param cp_clistate pointer to the copy_clistate we work with
3632 * @param mask the current search mask
3634 * @return Boolean result
3636 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3638 struct cli_state *targetcli;
3639 char *targetpath = NULL;
3641 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3643 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate->cli_share_src,
3644 mask, &targetcli, &targetpath ) ) {
3645 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3646 mask, cli_errstr(cp_clistate->cli_share_src));
3647 return false;
3650 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3651 d_fprintf(stderr, "listing %s failed with error: %s\n",
3652 mask, cli_errstr(targetcli));
3653 return false;
3656 return true;
3661 * Set the top level directory permissions before we do any further copies.
3662 * Should set up ACL inheritance.
3665 bool copy_top_level_perms(struct net_context *c,
3666 struct copy_clistate *cp_clistate,
3667 const char *sharename)
3669 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3671 switch (net_mode_share) {
3672 case NET_MODE_SHARE_MIGRATE:
3673 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3674 nt_status = net_copy_fileattr(c,
3675 cp_clistate->mem_ctx,
3676 cp_clistate->cli_share_src,
3677 cp_clistate->cli_share_dst,
3678 "\\", "\\",
3679 c->opt_acls? true : false,
3680 c->opt_attrs? true : false,
3681 c->opt_timestamps? true: false,
3682 false);
3683 break;
3684 default:
3685 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3686 break;
3689 if (!NT_STATUS_IS_OK(nt_status)) {
3690 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3691 sharename, nt_errstr(nt_status));
3692 return false;
3695 return true;
3699 * Sync all files inside a remote share to another share (over smb)
3701 * All parameters are provided by the run_rpc_command function, except for
3702 * argc, argv which are passes through.
3704 * @param domain_sid The domain sid acquired from the remote server
3705 * @param cli A cli_state connected to the server.
3706 * @param mem_ctx Talloc context, destoyed on completion of the function.
3707 * @param argc Standard main() style argc
3708 * @param argv Standard main() style argv. Initial components are already
3709 * stripped
3711 * @return Normal NTSTATUS return.
3714 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3715 const DOM_SID *domain_sid,
3716 const char *domain_name,
3717 struct cli_state *cli,
3718 struct rpc_pipe_client *pipe_hnd,
3719 TALLOC_CTX *mem_ctx,
3720 int argc,
3721 const char **argv)
3723 WERROR result;
3724 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3725 struct srvsvc_NetShareInfoCtr ctr_src;
3726 uint32 i;
3727 uint32 level = 502;
3728 struct copy_clistate cp_clistate;
3729 bool got_src_share = false;
3730 bool got_dst_share = false;
3731 const char *mask = "\\*";
3732 char *dst = NULL;
3734 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3735 if (dst == NULL) {
3736 nt_status = NT_STATUS_NO_MEMORY;
3737 goto done;
3740 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3741 &ctr_src);
3743 if (!W_ERROR_IS_OK(result))
3744 goto done;
3746 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3748 struct srvsvc_NetShareInfo502 info502 =
3749 ctr_src.ctr.ctr502->array[i];
3751 if (!check_share_sanity(c, cli, info502.name, info502.type))
3752 continue;
3754 /* one might not want to mirror whole discs :) */
3755 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3756 d_printf("skipping [%s]: builtin/hidden share\n", info502.name);
3757 continue;
3760 switch (net_mode_share)
3762 case NET_MODE_SHARE_MIGRATE:
3763 printf("syncing");
3764 break;
3765 default:
3766 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3767 break;
3769 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3770 info502.name,
3771 c->opt_acls ? "including" : "without",
3772 c->opt_attrs ? "including" : "without",
3773 c->opt_timestamps ? "(preserving timestamps)" : "");
3775 cp_clistate.mem_ctx = mem_ctx;
3776 cp_clistate.cli_share_src = NULL;
3777 cp_clistate.cli_share_dst = NULL;
3778 cp_clistate.cwd = NULL;
3779 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3780 cp_clistate.c = c;
3782 /* open share source */
3783 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3784 &cli->dest_ss, cli->desthost,
3785 info502.name, "A:");
3786 if (!NT_STATUS_IS_OK(nt_status))
3787 goto done;
3789 got_src_share = true;
3791 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3792 /* open share destination */
3793 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3794 NULL, dst, info502.name, "A:");
3795 if (!NT_STATUS_IS_OK(nt_status))
3796 goto done;
3798 got_dst_share = true;
3801 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3802 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", info502.name);
3803 nt_status = NT_STATUS_UNSUCCESSFUL;
3804 goto done;
3807 if (!sync_files(&cp_clistate, mask)) {
3808 d_fprintf(stderr, "could not handle files for share: %s\n", info502.name);
3809 nt_status = NT_STATUS_UNSUCCESSFUL;
3810 goto done;
3814 nt_status = NT_STATUS_OK;
3816 done:
3818 if (got_src_share)
3819 cli_shutdown(cp_clistate.cli_share_src);
3821 if (got_dst_share)
3822 cli_shutdown(cp_clistate.cli_share_dst);
3824 SAFE_FREE(dst);
3825 return nt_status;
3829 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3832 if (!c->opt_host) {
3833 printf("no server to migrate\n");
3834 return -1;
3837 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3838 rpc_share_migrate_files_internals,
3839 argc, argv);
3843 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3845 * All parameters are provided by the run_rpc_command function, except for
3846 * argc, argv which are passes through.
3848 * @param domain_sid The domain sid acquired from the remote server
3849 * @param cli A cli_state connected to the server.
3850 * @param mem_ctx Talloc context, destoyed on completion of the function.
3851 * @param argc Standard main() style argc
3852 * @param argv Standard main() style argv. Initial components are already
3853 * stripped
3855 * @return Normal NTSTATUS return.
3858 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3859 const DOM_SID *domain_sid,
3860 const char *domain_name,
3861 struct cli_state *cli,
3862 struct rpc_pipe_client *pipe_hnd,
3863 TALLOC_CTX *mem_ctx,
3864 int argc,
3865 const char **argv)
3867 WERROR result;
3868 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3869 struct srvsvc_NetShareInfoCtr ctr_src;
3870 union srvsvc_NetShareInfo info;
3871 uint32 i;
3872 struct rpc_pipe_client *srvsvc_pipe = NULL;
3873 struct cli_state *cli_dst = NULL;
3874 uint32 level = 502; /* includes secdesc */
3875 uint32_t parm_error = 0;
3877 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3878 &ctr_src);
3880 if (!W_ERROR_IS_OK(result))
3881 goto done;
3883 /* connect destination PI_SRVSVC */
3884 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe, PI_SRVSVC);
3885 if (!NT_STATUS_IS_OK(nt_status))
3886 return nt_status;
3889 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3891 struct srvsvc_NetShareInfo502 info502 =
3892 ctr_src.ctr.ctr502->array[i];
3894 /* reset error-code */
3895 nt_status = NT_STATUS_UNSUCCESSFUL;
3897 if (!check_share_sanity(c, cli, info502.name, info502.type))
3898 continue;
3900 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3901 info502.name, info502.path, info502.comment);
3903 if (c->opt_verbose)
3904 display_sec_desc(info502.sd_buf.sd);
3906 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3907 info.info502 = &info502;
3909 /* finally modify the share on the dst server */
3910 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3911 srvsvc_pipe->desthost,
3912 info502.name,
3913 level,
3914 &info,
3915 &parm_error,
3916 &result);
3917 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3918 printf("cannot set share-acl: %s\n", dos_errstr(result));
3919 goto done;
3924 nt_status = NT_STATUS_OK;
3926 done:
3927 if (cli_dst) {
3928 cli_shutdown(cli_dst);
3931 return nt_status;
3936 * Migrate share-acls from a rpc-server to another
3938 * @param argc Standard main() style argc
3939 * @param argv Standard main() style argv. Initial components are already
3940 * stripped
3942 * @return A shell status integer (0 for success)
3944 static int rpc_share_migrate_security(struct net_context *c, int argc,
3945 const char **argv)
3948 if (!c->opt_host) {
3949 printf("no server to migrate\n");
3950 return -1;
3953 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3954 rpc_share_migrate_security_internals,
3955 argc, argv);
3959 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3960 * from one server to another
3962 * @param argc Standard main() style argc
3963 * @param argv Standard main() style argv. Initial components are already
3964 * stripped
3966 * @return A shell status integer (0 for success)
3969 static int rpc_share_migrate_all(struct net_context *c, int argc,
3970 const char **argv)
3972 int ret;
3974 if (!c->opt_host) {
3975 printf("no server to migrate\n");
3976 return -1;
3979 /* order is important. we don't want to be locked out by the share-acl
3980 * before copying files - gd */
3982 ret = run_rpc_command(c, NULL, PI_SRVSVC, 0,
3983 rpc_share_migrate_shares_internals, argc, argv);
3984 if (ret)
3985 return ret;
3987 ret = run_rpc_command(c, NULL, PI_SRVSVC, 0,
3988 rpc_share_migrate_files_internals, argc, argv);
3989 if (ret)
3990 return ret;
3992 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3993 rpc_share_migrate_security_internals, argc,
3994 argv);
3999 * 'net rpc share migrate' entrypoint.
4000 * @param argc Standard main() style argc
4001 * @param argv Standard main() style argv. Initial components are already
4002 * stripped
4004 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4007 struct functable func[] = {
4008 {"all", rpc_share_migrate_all},
4009 {"files", rpc_share_migrate_files},
4010 {"help", rpc_share_usage},
4011 {"security", rpc_share_migrate_security},
4012 {"shares", rpc_share_migrate_shares},
4013 {NULL, NULL}
4016 net_mode_share = NET_MODE_SHARE_MIGRATE;
4018 return net_run_function(c, argc, argv, func, rpc_share_usage);
4021 struct full_alias {
4022 DOM_SID sid;
4023 uint32 num_members;
4024 DOM_SID *members;
4027 static int num_server_aliases;
4028 static struct full_alias *server_aliases;
4031 * Add an alias to the static list.
4033 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4035 if (server_aliases == NULL)
4036 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4038 server_aliases[num_server_aliases] = *alias;
4039 num_server_aliases += 1;
4043 * For a specific domain on the server, fetch all the aliases
4044 * and their members. Add all of them to the server_aliases.
4047 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4048 TALLOC_CTX *mem_ctx,
4049 POLICY_HND *connect_pol,
4050 const DOM_SID *domain_sid)
4052 uint32 start_idx, max_entries, num_entries, i;
4053 struct samr_SamArray *groups = NULL;
4054 NTSTATUS result;
4055 POLICY_HND domain_pol;
4057 /* Get domain policy handle */
4059 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
4060 connect_pol,
4061 MAXIMUM_ALLOWED_ACCESS,
4062 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4063 &domain_pol);
4064 if (!NT_STATUS_IS_OK(result))
4065 return result;
4067 start_idx = 0;
4068 max_entries = 250;
4070 do {
4071 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
4072 &domain_pol,
4073 &start_idx,
4074 &groups,
4075 max_entries,
4076 &num_entries);
4077 for (i = 0; i < num_entries; i++) {
4079 POLICY_HND alias_pol;
4080 struct full_alias alias;
4081 struct lsa_SidArray sid_array;
4082 int j;
4084 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
4085 &domain_pol,
4086 MAXIMUM_ALLOWED_ACCESS,
4087 groups->entries[i].idx,
4088 &alias_pol);
4089 if (!NT_STATUS_IS_OK(result))
4090 goto done;
4092 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
4093 &alias_pol,
4094 &sid_array);
4095 if (!NT_STATUS_IS_OK(result))
4096 goto done;
4098 alias.num_members = sid_array.num_sids;
4100 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
4101 if (!NT_STATUS_IS_OK(result))
4102 goto done;
4104 alias.members = NULL;
4106 if (alias.num_members > 0) {
4107 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
4109 for (j = 0; j < alias.num_members; j++)
4110 sid_copy(&alias.members[j],
4111 sid_array.sids[j].sid);
4114 sid_copy(&alias.sid, domain_sid);
4115 sid_append_rid(&alias.sid, groups->entries[i].idx);
4117 push_alias(mem_ctx, &alias);
4119 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4121 result = NT_STATUS_OK;
4123 done:
4124 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4126 return result;
4130 * Dump server_aliases as names for debugging purposes.
4133 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4134 const DOM_SID *domain_sid,
4135 const char *domain_name,
4136 struct cli_state *cli,
4137 struct rpc_pipe_client *pipe_hnd,
4138 TALLOC_CTX *mem_ctx,
4139 int argc,
4140 const char **argv)
4142 int i;
4143 NTSTATUS result;
4144 POLICY_HND lsa_pol;
4146 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4147 SEC_RIGHTS_MAXIMUM_ALLOWED,
4148 &lsa_pol);
4149 if (!NT_STATUS_IS_OK(result))
4150 return result;
4152 for (i=0; i<num_server_aliases; i++) {
4153 char **names;
4154 char **domains;
4155 enum lsa_SidType *types;
4156 int j;
4158 struct full_alias *alias = &server_aliases[i];
4160 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4161 &alias->sid,
4162 &domains, &names, &types);
4163 if (!NT_STATUS_IS_OK(result))
4164 continue;
4166 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4168 if (alias->num_members == 0) {
4169 DEBUG(1, ("\n"));
4170 continue;
4173 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4174 alias->num_members,
4175 alias->members,
4176 &domains, &names, &types);
4178 if (!NT_STATUS_IS_OK(result) &&
4179 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4180 continue;
4182 for (j=0; j<alias->num_members; j++)
4183 DEBUG(1, ("%s\\%s (%d); ",
4184 domains[j] ? domains[j] : "*unknown*",
4185 names[j] ? names[j] : "*unknown*",types[j]));
4186 DEBUG(1, ("\n"));
4189 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4191 return NT_STATUS_OK;
4195 * Fetch a list of all server aliases and their members into
4196 * server_aliases.
4199 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4200 const DOM_SID *domain_sid,
4201 const char *domain_name,
4202 struct cli_state *cli,
4203 struct rpc_pipe_client *pipe_hnd,
4204 TALLOC_CTX *mem_ctx,
4205 int argc,
4206 const char **argv)
4208 NTSTATUS result;
4209 POLICY_HND connect_pol;
4211 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4212 pipe_hnd->desthost,
4213 MAXIMUM_ALLOWED_ACCESS,
4214 &connect_pol);
4216 if (!NT_STATUS_IS_OK(result))
4217 goto done;
4219 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4220 &global_sid_Builtin);
4222 if (!NT_STATUS_IS_OK(result))
4223 goto done;
4225 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4226 domain_sid);
4228 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4229 done:
4230 return result;
4233 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4235 token->num_sids = 4;
4237 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4238 d_fprintf(stderr, "malloc failed\n");
4239 token->num_sids = 0;
4240 return;
4243 token->user_sids[0] = *user_sid;
4244 sid_copy(&token->user_sids[1], &global_sid_World);
4245 sid_copy(&token->user_sids[2], &global_sid_Network);
4246 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4249 static void free_user_token(NT_USER_TOKEN *token)
4251 SAFE_FREE(token->user_sids);
4254 static bool is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4256 int i;
4258 for (i=0; i<token->num_sids; i++) {
4259 if (sid_compare(sid, &token->user_sids[i]) == 0)
4260 return true;
4262 return false;
4265 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4267 if (is_sid_in_token(token, sid))
4268 return;
4270 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4271 if (!token->user_sids) {
4272 return;
4275 sid_copy(&token->user_sids[token->num_sids], sid);
4277 token->num_sids += 1;
4280 struct user_token {
4281 fstring name;
4282 NT_USER_TOKEN token;
4285 static void dump_user_token(struct user_token *token)
4287 int i;
4289 d_printf("%s\n", token->name);
4291 for (i=0; i<token->token.num_sids; i++) {
4292 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4296 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4298 int i;
4300 for (i=0; i<alias->num_members; i++) {
4301 if (sid_compare(sid, &alias->members[i]) == 0)
4302 return true;
4305 return false;
4308 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4310 int i;
4312 for (i=0; i<num_server_aliases; i++) {
4313 if (is_alias_member(&sid, &server_aliases[i]))
4314 add_sid_to_token(token, &server_aliases[i].sid);
4319 * We got a user token with all the SIDs we can know about without asking the
4320 * server directly. These are the user and domain group sids. All of these can
4321 * be members of aliases. So scan the list of aliases for each of the SIDs and
4322 * add them to the token.
4325 static void collect_alias_memberships(NT_USER_TOKEN *token)
4327 int num_global_sids = token->num_sids;
4328 int i;
4330 for (i=0; i<num_global_sids; i++) {
4331 collect_sid_memberships(token, token->user_sids[i]);
4335 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4337 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4338 enum wbcSidType type;
4339 fstring full_name;
4340 struct wbcDomainSid wsid;
4341 char *sid_str = NULL;
4342 DOM_SID user_sid;
4343 uint32_t num_groups;
4344 gid_t *groups = NULL;
4345 uint32_t i;
4347 fstr_sprintf(full_name, "%s%c%s",
4348 domain, *lp_winbind_separator(), user);
4350 /* First let's find out the user sid */
4352 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4354 if (!WBC_ERROR_IS_OK(wbc_status)) {
4355 DEBUG(1, ("winbind could not find %s: %s\n",
4356 full_name, wbcErrorString(wbc_status)));
4357 return false;
4360 wbc_status = wbcSidToString(&wsid, &sid_str);
4361 if (!WBC_ERROR_IS_OK(wbc_status)) {
4362 return false;
4365 if (type != SID_NAME_USER) {
4366 wbcFreeMemory(sid_str);
4367 DEBUG(1, ("%s is not a user\n", full_name));
4368 return false;
4371 string_to_sid(&user_sid, sid_str);
4372 wbcFreeMemory(sid_str);
4373 sid_str = NULL;
4375 init_user_token(token, &user_sid);
4377 /* And now the groups winbind knows about */
4379 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4380 if (!WBC_ERROR_IS_OK(wbc_status)) {
4381 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4382 full_name, wbcErrorString(wbc_status)));
4383 return false;
4386 for (i = 0; i < num_groups; i++) {
4387 gid_t gid = groups[i];
4388 DOM_SID sid;
4390 wbc_status = wbcGidToSid(gid, &wsid);
4391 if (!WBC_ERROR_IS_OK(wbc_status)) {
4392 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4393 gid, wbcErrorString(wbc_status)));
4394 wbcFreeMemory(groups);
4395 return false;
4398 wbc_status = wbcSidToString(&wsid, &sid_str);
4399 if (!WBC_ERROR_IS_OK(wbc_status)) {
4400 wbcFreeMemory(groups);
4401 return false;
4404 DEBUG(3, (" %s\n", sid_str));
4406 string_to_sid(&sid, sid_str);
4407 wbcFreeMemory(sid_str);
4408 sid_str = NULL;
4410 add_sid_to_token(token, &sid);
4412 wbcFreeMemory(groups);
4414 return true;
4418 * Get a list of all user tokens we want to look at
4421 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4422 struct user_token **user_tokens)
4424 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4425 uint32_t i, num_users;
4426 const char **users;
4427 struct user_token *result;
4428 TALLOC_CTX *frame = NULL;
4430 if (lp_winbind_use_default_domain() &&
4431 (c->opt_target_workgroup == NULL)) {
4432 d_fprintf(stderr, "winbind use default domain = yes set, "
4433 "please specify a workgroup\n");
4434 return false;
4437 /* Send request to winbind daemon */
4439 wbc_status = wbcListUsers(NULL, &num_users, &users);
4440 if (!WBC_ERROR_IS_OK(wbc_status)) {
4441 DEBUG(1, ("winbind could not list users: %s\n",
4442 wbcErrorString(wbc_status)));
4443 return false;
4446 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4448 if (result == NULL) {
4449 DEBUG(1, ("Could not malloc sid array\n"));
4450 wbcFreeMemory(users);
4451 return false;
4454 frame = talloc_stackframe();
4455 for (i=0; i < num_users; i++) {
4456 fstring domain, user;
4457 char *p;
4459 fstrcpy(result[i].name, users[i]);
4461 p = strchr(users[i], *lp_winbind_separator());
4463 DEBUG(3, ("%s\n", users[i]));
4465 if (p == NULL) {
4466 fstrcpy(domain, c->opt_target_workgroup);
4467 fstrcpy(user, users[i]);
4468 } else {
4469 *p++ = '\0';
4470 fstrcpy(domain, users[i]);
4471 strupper_m(domain);
4472 fstrcpy(user, p);
4475 get_user_sids(domain, user, &(result[i].token));
4476 i+=1;
4478 TALLOC_FREE(frame);
4479 wbcFreeMemory(users);
4481 *num_tokens = num_users;
4482 *user_tokens = result;
4484 return true;
4487 static bool get_user_tokens_from_file(FILE *f,
4488 int *num_tokens,
4489 struct user_token **tokens)
4491 struct user_token *token = NULL;
4493 while (!feof(f)) {
4494 fstring line;
4496 if (fgets(line, sizeof(line)-1, f) == NULL) {
4497 return true;
4500 if (line[strlen(line)-1] == '\n')
4501 line[strlen(line)-1] = '\0';
4503 if (line[0] == ' ') {
4504 /* We have a SID */
4506 DOM_SID sid;
4507 string_to_sid(&sid, &line[1]);
4509 if (token == NULL) {
4510 DEBUG(0, ("File does not begin with username"));
4511 return false;
4514 add_sid_to_token(&token->token, &sid);
4515 continue;
4518 /* And a new user... */
4520 *num_tokens += 1;
4521 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4522 if (*tokens == NULL) {
4523 DEBUG(0, ("Could not realloc tokens\n"));
4524 return false;
4527 token = &((*tokens)[*num_tokens-1]);
4529 fstrcpy(token->name, line);
4530 token->token.num_sids = 0;
4531 token->token.user_sids = NULL;
4532 continue;
4535 return false;
4540 * Show the list of all users that have access to a share
4543 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4544 TALLOC_CTX *mem_ctx,
4545 const char *netname,
4546 int num_tokens,
4547 struct user_token *tokens)
4549 int fnum;
4550 SEC_DESC *share_sd = NULL;
4551 SEC_DESC *root_sd = NULL;
4552 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4553 int i;
4554 union srvsvc_NetShareInfo info;
4555 WERROR result;
4556 NTSTATUS status;
4557 uint16 cnum;
4559 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4560 pipe_hnd->desthost,
4561 netname,
4562 502,
4563 &info,
4564 &result);
4566 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4567 DEBUG(1, ("Coult not query secdesc for share %s\n",
4568 netname));
4569 return;
4572 share_sd = info.info502->sd_buf.sd;
4573 if (share_sd == NULL) {
4574 DEBUG(1, ("Got no secdesc for share %s\n",
4575 netname));
4578 cnum = cli->cnum;
4580 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4581 return;
4584 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4586 if (fnum != -1) {
4587 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4590 for (i=0; i<num_tokens; i++) {
4591 uint32 acc_granted;
4593 if (share_sd != NULL) {
4594 if (!se_access_check(share_sd, &tokens[i].token,
4595 1, &acc_granted, &status)) {
4596 DEBUG(1, ("Could not check share_sd for "
4597 "user %s\n",
4598 tokens[i].name));
4599 continue;
4602 if (!NT_STATUS_IS_OK(status))
4603 continue;
4606 if (root_sd == NULL) {
4607 d_printf(" %s\n", tokens[i].name);
4608 continue;
4611 if (!se_access_check(root_sd, &tokens[i].token,
4612 1, &acc_granted, &status)) {
4613 DEBUG(1, ("Could not check root_sd for user %s\n",
4614 tokens[i].name));
4615 continue;
4618 if (!NT_STATUS_IS_OK(status))
4619 continue;
4621 d_printf(" %s\n", tokens[i].name);
4624 if (fnum != -1)
4625 cli_close(cli, fnum);
4626 cli_tdis(cli);
4627 cli->cnum = cnum;
4629 return;
4632 struct share_list {
4633 int num_shares;
4634 char **shares;
4637 static void collect_share(const char *name, uint32 m,
4638 const char *comment, void *state)
4640 struct share_list *share_list = (struct share_list *)state;
4642 if (m != STYPE_DISKTREE)
4643 return;
4645 share_list->num_shares += 1;
4646 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4647 if (!share_list->shares) {
4648 share_list->num_shares = 0;
4649 return;
4651 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4654 static void rpc_share_userlist_usage(void)
4656 return;
4660 * List shares on a remote RPC server, including the security descriptors
4662 * All parameters are provided by the run_rpc_command function, except for
4663 * argc, argv which are passes through.
4665 * @param domain_sid The domain sid acquired from the remote server
4666 * @param cli A cli_state connected to the server.
4667 * @param mem_ctx Talloc context, destoyed on completion of the function.
4668 * @param argc Standard main() style argc
4669 * @param argv Standard main() style argv. Initial components are already
4670 * stripped
4672 * @return Normal NTSTATUS return.
4675 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4676 const DOM_SID *domain_sid,
4677 const char *domain_name,
4678 struct cli_state *cli,
4679 struct rpc_pipe_client *pipe_hnd,
4680 TALLOC_CTX *mem_ctx,
4681 int argc,
4682 const char **argv)
4684 int ret;
4685 bool r;
4686 ENUM_HND hnd;
4687 uint32 i;
4688 FILE *f;
4690 struct user_token *tokens = NULL;
4691 int num_tokens = 0;
4693 struct share_list share_list;
4695 if (argc > 1) {
4696 rpc_share_userlist_usage();
4697 return NT_STATUS_UNSUCCESSFUL;
4700 if (argc == 0) {
4701 f = stdin;
4702 } else {
4703 f = fopen(argv[0], "r");
4706 if (f == NULL) {
4707 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4708 return NT_STATUS_UNSUCCESSFUL;
4711 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4713 if (f != stdin)
4714 fclose(f);
4716 if (!r) {
4717 DEBUG(0, ("Could not read users from file\n"));
4718 return NT_STATUS_UNSUCCESSFUL;
4721 for (i=0; i<num_tokens; i++)
4722 collect_alias_memberships(&tokens[i].token);
4724 init_enum_hnd(&hnd, 0);
4726 share_list.num_shares = 0;
4727 share_list.shares = NULL;
4729 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4731 if (ret == -1) {
4732 DEBUG(0, ("Error returning browse list: %s\n",
4733 cli_errstr(cli)));
4734 goto done;
4737 for (i = 0; i < share_list.num_shares; i++) {
4738 char *netname = share_list.shares[i];
4740 if (netname[strlen(netname)-1] == '$')
4741 continue;
4743 d_printf("%s\n", netname);
4745 show_userlist(pipe_hnd, mem_ctx, netname,
4746 num_tokens, tokens);
4748 done:
4749 for (i=0; i<num_tokens; i++) {
4750 free_user_token(&tokens[i].token);
4752 SAFE_FREE(tokens);
4753 SAFE_FREE(share_list.shares);
4755 return NT_STATUS_OK;
4758 static int rpc_share_allowedusers(struct net_context *c, int argc,
4759 const char **argv)
4761 int result;
4763 result = run_rpc_command(c, NULL, PI_SAMR, 0,
4764 rpc_aliaslist_internals,
4765 argc, argv);
4766 if (result != 0)
4767 return result;
4769 result = run_rpc_command(c, NULL, PI_LSARPC, 0,
4770 rpc_aliaslist_dump,
4771 argc, argv);
4772 if (result != 0)
4773 return result;
4775 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
4776 rpc_share_allowedusers_internals,
4777 argc, argv);
4780 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4782 int num_tokens = 0;
4783 struct user_token *tokens = NULL;
4784 int i;
4786 if (argc != 0) {
4787 net_usersidlist_usage(c, argc, argv);
4788 return 0;
4791 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4792 DEBUG(0, ("Could not get the user/sid list\n"));
4793 return 0;
4796 for (i=0; i<num_tokens; i++) {
4797 dump_user_token(&tokens[i]);
4798 free_user_token(&tokens[i].token);
4801 SAFE_FREE(tokens);
4802 return 1;
4805 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4807 d_printf("net usersidlist\n"
4808 "\tprints out a list of all users the running winbind knows\n"
4809 "\tabout, together with all their SIDs. This is used as\n"
4810 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4812 net_common_flags_usage(c, argc, argv);
4813 return -1;
4817 * 'net rpc share' entrypoint.
4818 * @param argc Standard main() style argc
4819 * @param argv Standard main() style argv. Initial components are already
4820 * stripped
4823 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4825 struct functable func[] = {
4826 {"add", rpc_share_add},
4827 {"delete", rpc_share_delete},
4828 {"allowedusers", rpc_share_allowedusers},
4829 {"migrate", rpc_share_migrate},
4830 {"list", rpc_share_list},
4831 {NULL, NULL}
4834 if (argc == 0)
4835 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
4836 rpc_share_list_internals,
4837 argc, argv);
4839 return net_run_function(c, argc, argv, func, rpc_share_usage);
4842 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4843 TALLOC_CTX *mem_ctx,
4844 struct rpc_sh_ctx *ctx,
4845 struct rpc_pipe_client *pipe_hnd,
4846 int argc, const char **argv)
4848 return rpc_share_list_internals(c, ctx->domain_sid, ctx->domain_name,
4849 ctx->cli, pipe_hnd, mem_ctx,
4850 argc, argv);
4853 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4854 TALLOC_CTX *mem_ctx,
4855 struct rpc_sh_ctx *ctx,
4856 struct rpc_pipe_client *pipe_hnd,
4857 int argc, const char **argv)
4859 WERROR result;
4860 NTSTATUS status;
4861 uint32_t parm_err = 0;
4862 union srvsvc_NetShareInfo info;
4863 struct srvsvc_NetShareInfo2 info2;
4865 if ((argc < 2) || (argc > 3)) {
4866 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4867 ctx->whoami);
4868 return NT_STATUS_INVALID_PARAMETER;
4871 info2.name = argv[0];
4872 info2.type = STYPE_DISKTREE;
4873 info2.comment = (argc == 3) ? argv[2] : "";
4874 info2.permissions = 0;
4875 info2.max_users = 0;
4876 info2.current_users = 0;
4877 info2.path = argv[1];
4878 info2.password = NULL;
4880 info.info2 = &info2;
4882 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
4883 pipe_hnd->desthost,
4885 &info,
4886 &parm_err,
4887 &result);
4889 return status;
4892 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4893 TALLOC_CTX *mem_ctx,
4894 struct rpc_sh_ctx *ctx,
4895 struct rpc_pipe_client *pipe_hnd,
4896 int argc, const char **argv)
4898 WERROR result;
4899 NTSTATUS status;
4901 if (argc != 1) {
4902 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4903 return NT_STATUS_INVALID_PARAMETER;
4906 status = rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
4907 pipe_hnd->desthost,
4908 argv[0],
4910 &result);
4912 return status;
4915 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4916 TALLOC_CTX *mem_ctx,
4917 struct rpc_sh_ctx *ctx,
4918 struct rpc_pipe_client *pipe_hnd,
4919 int argc, const char **argv)
4921 union srvsvc_NetShareInfo info;
4922 WERROR result;
4923 NTSTATUS status;
4925 if (argc != 1) {
4926 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4927 return NT_STATUS_INVALID_PARAMETER;
4930 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4931 pipe_hnd->desthost,
4932 argv[0],
4934 &info,
4935 &result);
4936 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4937 goto done;
4940 d_printf("Name: %s\n", info.info2->name);
4941 d_printf("Comment: %s\n", info.info2->comment);
4942 d_printf("Path: %s\n", info.info2->path);
4943 d_printf("Password: %s\n", info.info2->password);
4945 done:
4946 return werror_to_ntstatus(result);
4949 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4950 struct rpc_sh_ctx *ctx)
4952 static struct rpc_sh_cmd cmds[] = {
4954 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
4955 "List available shares" },
4957 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
4958 "Add a share" },
4960 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
4961 "Delete a share" },
4963 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
4964 "Get information about a share" },
4966 { NULL, NULL, 0, NULL, NULL }
4969 return cmds;
4972 /****************************************************************************/
4974 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4976 return net_file_usage(c, argc, argv);
4980 * Close a file on a remote RPC server
4982 * All parameters are provided by the run_rpc_command function, except for
4983 * argc, argv which are passes through.
4985 * @param c A net_context structure
4986 * @param domain_sid The domain sid acquired from the remote server
4987 * @param cli A cli_state connected to the server.
4988 * @param mem_ctx Talloc context, destoyed on completion of the function.
4989 * @param argc Standard main() style argc
4990 * @param argv Standard main() style argv. Initial components are already
4991 * stripped
4993 * @return Normal NTSTATUS return.
4995 static NTSTATUS rpc_file_close_internals(struct net_context *c,
4996 const DOM_SID *domain_sid,
4997 const char *domain_name,
4998 struct cli_state *cli,
4999 struct rpc_pipe_client *pipe_hnd,
5000 TALLOC_CTX *mem_ctx,
5001 int argc,
5002 const char **argv)
5004 return rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx,
5005 pipe_hnd->desthost,
5006 atoi(argv[0]), NULL);
5010 * Close a file on a remote RPC server
5012 * @param argc Standard main() style argc
5013 * @param argv Standard main() style argv. Initial components are already
5014 * stripped
5016 * @return A shell status integer (0 for success)
5018 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5020 if (argc < 1) {
5021 DEBUG(1, ("No fileid given on close\n"));
5022 return rpc_file_usage(c, argc, argv);
5025 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
5026 rpc_file_close_internals,
5027 argc, argv);
5031 * Formatted print of open file info
5033 * @param r struct srvsvc_NetFileInfo3 contents
5036 static void display_file_info_3(struct srvsvc_NetFileInfo3 *r)
5038 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5039 r->fid, r->user, r->permissions, r->num_locks, r->path);
5043 * List open files on a remote RPC server
5045 * All parameters are provided by the run_rpc_command function, except for
5046 * argc, argv which are passes through.
5048 * @param c A net_context structure
5049 * @param domain_sid The domain sid acquired from the remote server
5050 * @param cli A cli_state connected to the server.
5051 * @param mem_ctx Talloc context, destoyed on completion of the function.
5052 * @param argc Standard main() style argc
5053 * @param argv Standard main() style argv. Initial components are already
5054 * stripped
5056 * @return Normal NTSTATUS return.
5059 static NTSTATUS rpc_file_list_internals(struct net_context *c,
5060 const DOM_SID *domain_sid,
5061 const char *domain_name,
5062 struct cli_state *cli,
5063 struct rpc_pipe_client *pipe_hnd,
5064 TALLOC_CTX *mem_ctx,
5065 int argc,
5066 const char **argv)
5068 struct srvsvc_NetFileInfoCtr info_ctr;
5069 struct srvsvc_NetFileCtr3 ctr3;
5070 WERROR result;
5071 NTSTATUS status;
5072 uint32 preferred_len = 0xffffffff, i;
5073 const char *username=NULL;
5074 uint32_t total_entries = 0;
5075 uint32_t resume_handle = 0;
5077 /* if argc > 0, must be user command */
5078 if (argc > 0)
5079 username = smb_xstrdup(argv[0]);
5081 ZERO_STRUCT(info_ctr);
5082 ZERO_STRUCT(ctr3);
5084 info_ctr.level = 3;
5085 info_ctr.ctr.ctr3 = &ctr3;
5087 status = rpccli_srvsvc_NetFileEnum(pipe_hnd, mem_ctx,
5088 pipe_hnd->desthost,
5089 NULL,
5090 username,
5091 &info_ctr,
5092 preferred_len,
5093 &total_entries,
5094 &resume_handle,
5095 &result);
5097 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
5098 goto done;
5100 /* Display results */
5102 d_printf(
5103 "\nEnumerating open files on remote server:\n\n"\
5104 "\nFileId Opened by Perms Locks Path"\
5105 "\n------ --------- ----- ----- ---- \n");
5106 for (i = 0; i < total_entries; i++)
5107 display_file_info_3(&info_ctr.ctr.ctr3->array[i]);
5108 done:
5109 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
5113 * List files for a user on a remote RPC server
5115 * @param argc Standard main() style argc
5116 * @param argv Standard main() style argv. Initial components are already
5117 * stripped
5119 * @return A shell status integer (0 for success)
5122 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5124 if (argc < 1) {
5125 DEBUG(1, ("No username given\n"));
5126 return rpc_file_usage(c, argc, argv);
5129 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
5130 rpc_file_list_internals,
5131 argc, argv);
5135 * 'net rpc file' entrypoint.
5136 * @param argc Standard main() style argc
5137 * @param argv Standard main() style argv. Initial components are already
5138 * stripped
5141 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5143 struct functable func[] = {
5144 {"close", rpc_file_close},
5145 {"user", rpc_file_user},
5146 #if 0
5147 {"info", rpc_file_info},
5148 #endif
5149 {NULL, NULL}
5152 if (argc == 0)
5153 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
5154 rpc_file_list_internals,
5155 argc, argv);
5157 return net_run_function(c, argc, argv, func, rpc_file_usage);
5161 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
5163 * All parameters are provided by the run_rpc_command function, except for
5164 * argc, argv which are passed through.
5166 * @param c A net_context structure
5167 * @param domain_sid The domain sid aquired from the remote server
5168 * @param cli A cli_state connected to the server.
5169 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5170 * @param argc Standard main() style argc
5171 * @param argv Standard main() style argv. Initial components are already
5172 * stripped
5174 * @return Normal NTSTATUS return.
5177 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5178 const DOM_SID *domain_sid,
5179 const char *domain_name,
5180 struct cli_state *cli,
5181 struct rpc_pipe_client *pipe_hnd,
5182 TALLOC_CTX *mem_ctx,
5183 int argc,
5184 const char **argv)
5186 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5188 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5190 if (NT_STATUS_IS_OK(result)) {
5191 d_printf("\nShutdown successfully aborted\n");
5192 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5193 } else
5194 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5196 return result;
5200 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5202 * All parameters are provided by the run_rpc_command function, except for
5203 * argc, argv which are passed through.
5205 * @param c A net_context structure
5206 * @param domain_sid The domain sid aquired from the remote server
5207 * @param cli A cli_state connected to the server.
5208 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5209 * @param argc Standard main() style argc
5210 * @param argv Standard main() style argv. Initial components are already
5211 * stripped
5213 * @return Normal NTSTATUS return.
5216 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5217 const DOM_SID *domain_sid,
5218 const char *domain_name,
5219 struct cli_state *cli,
5220 struct rpc_pipe_client *pipe_hnd,
5221 TALLOC_CTX *mem_ctx,
5222 int argc,
5223 const char **argv)
5225 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5227 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5229 if (NT_STATUS_IS_OK(result)) {
5230 d_printf("\nShutdown successfully aborted\n");
5231 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5232 } else
5233 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5235 return result;
5239 * ABORT the Shut down of a remote RPC server
5241 * @param argc Standard main() style argc
5242 * @param argv Standard main() style argv. Initial components are already
5243 * stripped
5245 * @return A shell status integer (0 for success)
5248 static int rpc_shutdown_abort(struct net_context *c, int argc,
5249 const char **argv)
5251 int rc = run_rpc_command(c, NULL, PI_INITSHUTDOWN, 0,
5252 rpc_shutdown_abort_internals,
5253 argc, argv);
5255 if (rc == 0)
5256 return rc;
5258 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5260 return run_rpc_command(c, NULL, PI_WINREG, 0,
5261 rpc_reg_shutdown_abort_internals,
5262 argc, argv);
5266 * Shut down a remote RPC Server via initshutdown pipe
5268 * All parameters are provided by the run_rpc_command function, except for
5269 * argc, argv which are passes through.
5271 * @param c A net_context structure
5272 * @param domain_sid The domain sid aquired from the remote server
5273 * @param cli A cli_state connected to the server.
5274 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5275 * @param argc Standard main() style argc
5276 * @param argc Standard main() style argv. Initial components are already
5277 * stripped
5279 * @return Normal NTSTATUS return.
5282 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5283 const DOM_SID *domain_sid,
5284 const char *domain_name,
5285 struct cli_state *cli,
5286 struct rpc_pipe_client *pipe_hnd,
5287 TALLOC_CTX *mem_ctx,
5288 int argc,
5289 const char **argv)
5291 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5292 const char *msg = "This machine will be shutdown shortly";
5293 uint32 timeout = 20;
5294 struct initshutdown_String msg_string;
5295 struct initshutdown_String_sub s;
5297 if (c->opt_comment) {
5298 msg = c->opt_comment;
5300 if (c->opt_timeout) {
5301 timeout = c->opt_timeout;
5304 s.name = msg;
5305 msg_string.name = &s;
5307 /* create an entry */
5308 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5309 &msg_string, timeout, c->opt_force, c->opt_reboot,
5310 NULL);
5312 if (NT_STATUS_IS_OK(result)) {
5313 d_printf("\nShutdown of remote machine succeeded\n");
5314 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5315 } else {
5316 DEBUG(1,("Shutdown of remote machine failed!\n"));
5318 return result;
5322 * Shut down a remote RPC Server via winreg pipe
5324 * All parameters are provided by the run_rpc_command function, except for
5325 * argc, argv which are passes through.
5327 * @param c A net_context structure
5328 * @param domain_sid The domain sid aquired from the remote server
5329 * @param cli A cli_state connected to the server.
5330 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5331 * @param argc Standard main() style argc
5332 * @param argc Standard main() style argv. Initial components are already
5333 * stripped
5335 * @return Normal NTSTATUS return.
5338 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5339 const DOM_SID *domain_sid,
5340 const char *domain_name,
5341 struct cli_state *cli,
5342 struct rpc_pipe_client *pipe_hnd,
5343 TALLOC_CTX *mem_ctx,
5344 int argc,
5345 const char **argv)
5347 const char *msg = "This machine will be shutdown shortly";
5348 uint32 timeout = 20;
5349 struct initshutdown_String msg_string;
5350 struct initshutdown_String_sub s;
5351 NTSTATUS result;
5352 WERROR werr;
5354 if (c->opt_comment) {
5355 msg = c->opt_comment;
5357 s.name = msg;
5358 msg_string.name = &s;
5360 if (c->opt_timeout) {
5361 timeout = c->opt_timeout;
5364 /* create an entry */
5365 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5366 &msg_string, timeout, c->opt_force, c->opt_reboot,
5367 &werr);
5369 if (NT_STATUS_IS_OK(result)) {
5370 d_printf("\nShutdown of remote machine succeeded\n");
5371 } else {
5372 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5373 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5374 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5375 else
5376 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(werr));
5379 return result;
5383 * Shut down a remote RPC server
5385 * @param argc Standard main() style argc
5386 * @param argc Standard main() style argv. Initial components are already
5387 * stripped
5389 * @return A shell status integer (0 for success)
5392 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5394 int rc = run_rpc_command(c, NULL, PI_INITSHUTDOWN, 0,
5395 rpc_init_shutdown_internals,
5396 argc, argv);
5398 if (rc) {
5399 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5400 rc = run_rpc_command(c, NULL, PI_WINREG, 0,
5401 rpc_reg_shutdown_internals, argc, argv);
5404 return rc;
5407 /***************************************************************************
5408 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5409 ***************************************************************************/
5412 * Add interdomain trust account to the RPC server.
5413 * All parameters (except for argc and argv) are passed by run_rpc_command
5414 * function.
5416 * @param c A net_context structure
5417 * @param domain_sid The domain sid acquired from the server
5418 * @param cli A cli_state connected to the server.
5419 * @param mem_ctx Talloc context, destoyed on completion of the function.
5420 * @param argc Standard main() style argc
5421 * @param argc Standard main() style argv. Initial components are already
5422 * stripped
5424 * @return normal NTSTATUS return code
5427 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5428 const DOM_SID *domain_sid,
5429 const char *domain_name,
5430 struct cli_state *cli,
5431 struct rpc_pipe_client *pipe_hnd,
5432 TALLOC_CTX *mem_ctx,
5433 int argc,
5434 const char **argv)
5436 POLICY_HND connect_pol, domain_pol, user_pol;
5437 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5438 char *acct_name;
5439 struct lsa_String lsa_acct_name;
5440 uint32 acb_info;
5441 uint32 acct_flags=0;
5442 uint32 user_rid;
5443 uint32_t access_granted = 0;
5444 union samr_UserInfo info;
5446 if (argc != 2) {
5447 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5448 return NT_STATUS_INVALID_PARAMETER;
5452 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5455 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5456 return NT_STATUS_NO_MEMORY;
5459 strupper_m(acct_name);
5461 init_lsa_String(&lsa_acct_name, acct_name);
5463 /* Get samr policy handle */
5464 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5465 pipe_hnd->desthost,
5466 MAXIMUM_ALLOWED_ACCESS,
5467 &connect_pol);
5468 if (!NT_STATUS_IS_OK(result)) {
5469 goto done;
5472 /* Get domain policy handle */
5473 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5474 &connect_pol,
5475 MAXIMUM_ALLOWED_ACCESS,
5476 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5477 &domain_pol);
5478 if (!NT_STATUS_IS_OK(result)) {
5479 goto done;
5482 /* Create trusting domain's account */
5483 acb_info = ACB_NORMAL;
5484 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5485 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5486 SAMR_USER_ACCESS_SET_PASSWORD |
5487 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5488 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5490 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5491 &domain_pol,
5492 &lsa_acct_name,
5493 acb_info,
5494 acct_flags,
5495 &user_pol,
5496 &access_granted,
5497 &user_rid);
5498 if (!NT_STATUS_IS_OK(result)) {
5499 goto done;
5503 NTTIME notime;
5504 struct samr_LogonHours hours;
5505 struct lsa_BinaryString parameters;
5506 const int units_per_week = 168;
5507 uchar pwbuf[516];
5509 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5511 ZERO_STRUCT(notime);
5512 ZERO_STRUCT(hours);
5513 ZERO_STRUCT(parameters);
5515 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
5516 if (!hours.bits) {
5517 result = NT_STATUS_NO_MEMORY;
5518 goto done;
5520 hours.units_per_week = units_per_week;
5521 memset(hours.bits, 0xFF, units_per_week);
5523 init_samr_user_info23(&info.info23,
5524 notime, notime, notime,
5525 notime, notime, notime,
5526 NULL, NULL, NULL, NULL, NULL,
5527 NULL, NULL, NULL, NULL, &parameters,
5528 0, 0, ACB_DOMTRUST, SAMR_FIELD_ACCT_FLAGS,
5529 hours,
5530 0, 0, 0, 0, 0, 0, 0,
5531 pwbuf, 24);
5533 SamOEMhashBlob(info.info23.password.data, 516,
5534 &cli->user_session_key);
5536 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5537 &user_pol,
5539 &info);
5541 if (!NT_STATUS_IS_OK(result)) {
5542 DEBUG(0,("Could not set trust account password: %s\n",
5543 nt_errstr(result)));
5544 goto done;
5548 done:
5549 SAFE_FREE(acct_name);
5550 return result;
5554 * Create interdomain trust account for a remote domain.
5556 * @param argc standard argc
5557 * @param argv standard argv without initial components
5559 * @return Integer status (0 means success)
5562 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5564 if (argc > 0) {
5565 return run_rpc_command(c, NULL, PI_SAMR, 0,
5566 rpc_trustdom_add_internals, argc, argv);
5567 } else {
5568 d_printf("Usage: net rpc trustdom add <domain>\n");
5569 return -1;
5575 * Remove interdomain trust account from the RPC server.
5576 * All parameters (except for argc and argv) are passed by run_rpc_command
5577 * function.
5579 * @param c A net_context structure
5580 * @param domain_sid The domain sid acquired from the server
5581 * @param cli A cli_state connected to the server.
5582 * @param mem_ctx Talloc context, destoyed on completion of the function.
5583 * @param argc Standard main() style argc
5584 * @param argc Standard main() style argv. Initial components are already
5585 * stripped
5587 * @return normal NTSTATUS return code
5590 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5591 const DOM_SID *domain_sid,
5592 const char *domain_name,
5593 struct cli_state *cli,
5594 struct rpc_pipe_client *pipe_hnd,
5595 TALLOC_CTX *mem_ctx,
5596 int argc,
5597 const char **argv)
5599 POLICY_HND connect_pol, domain_pol, user_pol;
5600 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5601 char *acct_name;
5602 DOM_SID trust_acct_sid;
5603 struct samr_Ids user_rids, name_types;
5604 struct lsa_String lsa_acct_name;
5606 if (argc != 1) {
5607 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5608 return NT_STATUS_INVALID_PARAMETER;
5612 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5614 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5616 if (acct_name == NULL)
5617 return NT_STATUS_NO_MEMORY;
5619 strupper_m(acct_name);
5621 /* Get samr policy handle */
5622 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5623 pipe_hnd->desthost,
5624 MAXIMUM_ALLOWED_ACCESS,
5625 &connect_pol);
5626 if (!NT_STATUS_IS_OK(result)) {
5627 goto done;
5630 /* Get domain policy handle */
5631 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5632 &connect_pol,
5633 MAXIMUM_ALLOWED_ACCESS,
5634 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5635 &domain_pol);
5636 if (!NT_STATUS_IS_OK(result)) {
5637 goto done;
5640 init_lsa_String(&lsa_acct_name, acct_name);
5642 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5643 &domain_pol,
5645 &lsa_acct_name,
5646 &user_rids,
5647 &name_types);
5649 if (!NT_STATUS_IS_OK(result)) {
5650 goto done;
5653 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5654 &domain_pol,
5655 MAXIMUM_ALLOWED_ACCESS,
5656 user_rids.ids[0],
5657 &user_pol);
5659 if (!NT_STATUS_IS_OK(result)) {
5660 goto done;
5663 /* append the rid to the domain sid */
5664 sid_copy(&trust_acct_sid, domain_sid);
5665 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5666 goto done;
5669 /* remove the sid */
5671 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5672 &user_pol,
5673 &trust_acct_sid);
5674 if (!NT_STATUS_IS_OK(result)) {
5675 goto done;
5678 /* Delete user */
5680 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5681 &user_pol);
5683 if (!NT_STATUS_IS_OK(result)) {
5684 goto done;
5687 if (!NT_STATUS_IS_OK(result)) {
5688 DEBUG(0,("Could not set trust account password: %s\n",
5689 nt_errstr(result)));
5690 goto done;
5693 done:
5694 return result;
5698 * Delete interdomain trust account for a remote domain.
5700 * @param argc standard argc
5701 * @param argv standard argv without initial components
5703 * @return Integer status (0 means success)
5706 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5708 if (argc > 0) {
5709 return run_rpc_command(c, NULL, PI_SAMR, 0,
5710 rpc_trustdom_del_internals, argc, argv);
5711 } else {
5712 d_printf("Usage: net rpc trustdom del <domain>\n");
5713 return -1;
5717 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5718 struct cli_state *cli,
5719 TALLOC_CTX *mem_ctx,
5720 const char *domain_name)
5722 char *dc_name = NULL;
5723 const char *buffer = NULL;
5724 struct rpc_pipe_client *netr;
5725 NTSTATUS status;
5727 /* Use NetServerEnum2 */
5729 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5730 SAFE_FREE(dc_name);
5731 return NT_STATUS_OK;
5734 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5735 for domain %s\n", domain_name));
5737 /* Try netr_GetDcName */
5739 netr = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &status);
5740 if (!netr) {
5741 return status;
5744 status = rpccli_netr_GetDcName(netr, mem_ctx,
5745 cli->desthost,
5746 domain_name,
5747 &buffer,
5748 NULL);
5749 TALLOC_FREE(netr);
5751 if (NT_STATUS_IS_OK(status)) {
5752 return status;
5755 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5756 for domain %s\n", domain_name));
5758 return status;
5762 * Establish trust relationship to a trusting domain.
5763 * Interdomain account must already be created on remote PDC.
5765 * @param c A net_context structure
5766 * @param argc standard argc
5767 * @param argv standard argv without initial components
5769 * @return Integer status (0 means success)
5772 static int rpc_trustdom_establish(struct net_context *c, int argc,
5773 const char **argv)
5775 struct cli_state *cli = NULL;
5776 struct sockaddr_storage server_ss;
5777 struct rpc_pipe_client *pipe_hnd = NULL;
5778 POLICY_HND connect_hnd;
5779 TALLOC_CTX *mem_ctx;
5780 NTSTATUS nt_status;
5781 DOM_SID *domain_sid;
5783 char* domain_name;
5784 char* acct_name;
5785 fstring pdc_name;
5786 union lsa_PolicyInformation *info = NULL;
5789 * Connect to \\server\ipc$ as 'our domain' account with password
5792 if (argc != 1) {
5793 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5794 return -1;
5797 domain_name = smb_xstrdup(argv[0]);
5798 strupper_m(domain_name);
5800 /* account name used at first is our domain's name with '$' */
5801 asprintf(&acct_name, "%s$", lp_workgroup());
5802 strupper_m(acct_name);
5805 * opt_workgroup will be used by connection functions further,
5806 * hence it should be set to remote domain name instead of ours
5808 if (c->opt_workgroup) {
5809 c->opt_workgroup = smb_xstrdup(domain_name);
5812 c->opt_user_name = acct_name;
5814 /* find the domain controller */
5815 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5816 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5817 return -1;
5820 /* connect to ipc$ as username/password */
5821 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5822 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5824 /* Is it trusting domain account for sure ? */
5825 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5826 nt_errstr(nt_status)));
5827 return -1;
5830 /* store who we connected to */
5832 saf_store( domain_name, pdc_name );
5835 * Connect to \\server\ipc$ again (this time anonymously)
5838 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5839 (char*)pdc_name);
5841 if (NT_STATUS_IS_ERR(nt_status)) {
5842 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5843 domain_name, nt_errstr(nt_status)));
5844 return -1;
5847 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5848 "domain %s", domain_name))) {
5849 DEBUG(0, ("talloc_init() failed\n"));
5850 cli_shutdown(cli);
5851 return -1;
5854 /* Make sure we're talking to a proper server */
5856 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5857 if (!NT_STATUS_IS_OK(nt_status)) {
5858 cli_shutdown(cli);
5859 talloc_destroy(mem_ctx);
5860 return -1;
5864 * Call LsaOpenPolicy and LsaQueryInfo
5867 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5868 if (!pipe_hnd) {
5869 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5870 cli_shutdown(cli);
5871 talloc_destroy(mem_ctx);
5872 return -1;
5875 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, SEC_RIGHTS_QUERY_VALUE,
5876 &connect_hnd);
5877 if (NT_STATUS_IS_ERR(nt_status)) {
5878 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5879 nt_errstr(nt_status)));
5880 cli_shutdown(cli);
5881 talloc_destroy(mem_ctx);
5882 return -1;
5885 /* Querying info level 5 */
5887 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5888 &connect_hnd,
5889 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5890 &info);
5891 if (NT_STATUS_IS_ERR(nt_status)) {
5892 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5893 nt_errstr(nt_status)));
5894 cli_shutdown(cli);
5895 talloc_destroy(mem_ctx);
5896 return -1;
5899 domain_sid = info->account_domain.sid;
5901 /* There should be actually query info level 3 (following nt serv behaviour),
5902 but I still don't know if it's _really_ necessary */
5905 * Store the password in secrets db
5908 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5909 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5910 cli_shutdown(cli);
5911 talloc_destroy(mem_ctx);
5912 return -1;
5916 * Close the pipes and clean up
5919 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5920 if (NT_STATUS_IS_ERR(nt_status)) {
5921 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5922 nt_errstr(nt_status)));
5923 cli_shutdown(cli);
5924 talloc_destroy(mem_ctx);
5925 return -1;
5928 cli_shutdown(cli);
5930 talloc_destroy(mem_ctx);
5932 d_printf("Trust to domain %s established\n", domain_name);
5933 return 0;
5937 * Revoke trust relationship to the remote domain
5939 * @param c A net_context structure
5940 * @param argc standard argc
5941 * @param argv standard argv without initial components
5943 * @return Integer status (0 means success)
5946 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5947 const char **argv)
5949 char* domain_name;
5950 int rc = -1;
5952 if (argc < 1) return -1;
5954 /* generate upper cased domain name */
5955 domain_name = smb_xstrdup(argv[0]);
5956 strupper_m(domain_name);
5958 /* delete password of the trust */
5959 if (!pdb_del_trusteddom_pw(domain_name)) {
5960 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5961 domain_name));
5962 goto done;
5965 rc = 0;
5966 done:
5967 SAFE_FREE(domain_name);
5968 return rc;
5972 * Usage for 'net rpc trustdom' command
5974 * @param argc standard argc
5975 * @param argv standard argv without inital components
5977 * @return Integer status returned to shell
5980 static int rpc_trustdom_usage(struct net_context *c, int argc,
5981 const char **argv)
5983 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
5984 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
5985 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
5986 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5987 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
5988 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5989 return -1;
5993 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5994 const DOM_SID *domain_sid,
5995 const char *domain_name,
5996 struct cli_state *cli,
5997 struct rpc_pipe_client *pipe_hnd,
5998 TALLOC_CTX *mem_ctx,
5999 int argc,
6000 const char **argv)
6002 fstring str_sid;
6003 sid_to_fstring(str_sid, domain_sid);
6004 d_printf("%s\n", str_sid);
6005 return NT_STATUS_OK;
6008 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
6010 fstring ascii_sid, padding;
6011 int pad_len, col_len = 20;
6013 /* convert sid into ascii string */
6014 sid_to_fstring(ascii_sid, dom_sid);
6016 /* calculate padding space for d_printf to look nicer */
6017 pad_len = col_len - strlen(trusted_dom_name);
6018 padding[pad_len] = 0;
6019 do padding[--pad_len] = ' '; while (pad_len);
6021 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
6024 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6025 TALLOC_CTX *mem_ctx,
6026 POLICY_HND *pol,
6027 DOM_SID dom_sid,
6028 const char *trusted_dom_name)
6030 NTSTATUS nt_status;
6031 union lsa_TrustedDomainInfo *info = NULL;
6032 char *cleartextpwd = NULL;
6033 uint8_t nt_hash[16];
6034 DATA_BLOB data;
6036 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
6037 pol,
6038 &dom_sid,
6039 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6040 &info);
6041 if (NT_STATUS_IS_ERR(nt_status)) {
6042 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6043 nt_errstr(nt_status)));
6044 goto done;
6047 data = data_blob(info->password.password->data,
6048 info->password.password->length);
6050 if (!rpccli_get_pwd_hash(pipe_hnd, nt_hash)) {
6051 DEBUG(0, ("Could not retrieve password hash\n"));
6052 goto done;
6055 cleartextpwd = decrypt_trustdom_secret(nt_hash, &data);
6057 if (cleartextpwd == NULL) {
6058 DEBUG(0,("retrieved NULL password\n"));
6059 nt_status = NT_STATUS_UNSUCCESSFUL;
6060 goto done;
6063 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6064 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6065 nt_status = NT_STATUS_UNSUCCESSFUL;
6066 goto done;
6069 #ifdef DEBUG_PASSWORD
6070 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6071 "password: [%s]\n", trusted_dom_name,
6072 sid_string_dbg(&dom_sid), cleartextpwd));
6073 #endif
6075 done:
6076 SAFE_FREE(cleartextpwd);
6077 data_blob_free(&data);
6079 return nt_status;
6082 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6083 const char **argv)
6085 /* common variables */
6086 TALLOC_CTX* mem_ctx;
6087 struct cli_state *cli = NULL;
6088 struct rpc_pipe_client *pipe_hnd = NULL;
6089 NTSTATUS nt_status;
6090 const char *domain_name = NULL;
6091 DOM_SID *queried_dom_sid;
6092 POLICY_HND connect_hnd;
6093 union lsa_PolicyInformation *info = NULL;
6095 /* trusted domains listing variables */
6096 unsigned int enum_ctx = 0;
6097 int i;
6098 struct lsa_DomainList dom_list;
6099 fstring pdc_name;
6102 * Listing trusted domains (stored in secrets.tdb, if local)
6105 mem_ctx = talloc_init("trust relationships vampire");
6108 * set domain and pdc name to local samba server (default)
6109 * or to remote one given in command line
6112 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6113 domain_name = c->opt_workgroup;
6114 c->opt_target_workgroup = c->opt_workgroup;
6115 } else {
6116 fstrcpy(pdc_name, global_myname());
6117 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6118 c->opt_target_workgroup = domain_name;
6121 /* open \PIPE\lsarpc and open policy handle */
6122 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6123 if (!NT_STATUS_IS_OK(nt_status)) {
6124 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6125 nt_errstr(nt_status)));
6126 talloc_destroy(mem_ctx);
6127 return -1;
6130 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6131 if (!pipe_hnd) {
6132 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6133 nt_errstr(nt_status) ));
6134 cli_shutdown(cli);
6135 talloc_destroy(mem_ctx);
6136 return -1;
6139 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6140 &connect_hnd);
6141 if (NT_STATUS_IS_ERR(nt_status)) {
6142 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6143 nt_errstr(nt_status)));
6144 cli_shutdown(cli);
6145 talloc_destroy(mem_ctx);
6146 return -1;
6149 /* query info level 5 to obtain sid of a domain being queried */
6150 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6151 &connect_hnd,
6152 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6153 &info);
6155 if (NT_STATUS_IS_ERR(nt_status)) {
6156 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6157 nt_errstr(nt_status)));
6158 cli_shutdown(cli);
6159 talloc_destroy(mem_ctx);
6160 return -1;
6163 queried_dom_sid = info->account_domain.sid;
6166 * Keep calling LsaEnumTrustdom over opened pipe until
6167 * the end of enumeration is reached
6170 d_printf("Vampire trusted domains:\n\n");
6172 do {
6173 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6174 &connect_hnd,
6175 &enum_ctx,
6176 &dom_list,
6177 (uint32_t)-1);
6178 if (NT_STATUS_IS_ERR(nt_status)) {
6179 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6180 nt_errstr(nt_status)));
6181 cli_shutdown(cli);
6182 talloc_destroy(mem_ctx);
6183 return -1;
6186 for (i = 0; i < dom_list.count; i++) {
6188 print_trusted_domain(dom_list.domains[i].sid,
6189 dom_list.domains[i].name.string);
6191 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6192 *dom_list.domains[i].sid,
6193 dom_list.domains[i].name.string);
6194 if (!NT_STATUS_IS_OK(nt_status)) {
6195 cli_shutdown(cli);
6196 talloc_destroy(mem_ctx);
6197 return -1;
6202 * in case of no trusted domains say something rather
6203 * than just display blank line
6205 if (!dom_list.count) d_printf("none\n");
6207 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6209 /* close this connection before doing next one */
6210 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6211 if (NT_STATUS_IS_ERR(nt_status)) {
6212 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6213 nt_errstr(nt_status)));
6214 cli_shutdown(cli);
6215 talloc_destroy(mem_ctx);
6216 return -1;
6219 /* close lsarpc pipe and connection to IPC$ */
6220 cli_shutdown(cli);
6222 talloc_destroy(mem_ctx);
6223 return 0;
6226 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6228 /* common variables */
6229 TALLOC_CTX* mem_ctx;
6230 struct cli_state *cli = NULL, *remote_cli = NULL;
6231 struct rpc_pipe_client *pipe_hnd = NULL;
6232 NTSTATUS nt_status;
6233 const char *domain_name = NULL;
6234 DOM_SID *queried_dom_sid;
6235 fstring padding;
6236 int ascii_dom_name_len;
6237 POLICY_HND connect_hnd;
6238 union lsa_PolicyInformation *info = NULL;
6240 /* trusted domains listing variables */
6241 unsigned int num_domains, enum_ctx = 0;
6242 int i, pad_len, col_len = 20;
6243 struct lsa_DomainList dom_list;
6244 fstring pdc_name;
6246 /* trusting domains listing variables */
6247 POLICY_HND domain_hnd;
6248 struct samr_SamArray *trusts = NULL;
6251 * Listing trusted domains (stored in secrets.tdb, if local)
6254 mem_ctx = talloc_init("trust relationships listing");
6257 * set domain and pdc name to local samba server (default)
6258 * or to remote one given in command line
6261 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6262 domain_name = c->opt_workgroup;
6263 c->opt_target_workgroup = c->opt_workgroup;
6264 } else {
6265 fstrcpy(pdc_name, global_myname());
6266 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6267 c->opt_target_workgroup = domain_name;
6270 /* open \PIPE\lsarpc and open policy handle */
6271 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6272 if (!NT_STATUS_IS_OK(nt_status)) {
6273 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6274 nt_errstr(nt_status)));
6275 talloc_destroy(mem_ctx);
6276 return -1;
6279 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6280 if (!pipe_hnd) {
6281 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6282 nt_errstr(nt_status) ));
6283 cli_shutdown(cli);
6284 talloc_destroy(mem_ctx);
6285 return -1;
6288 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6289 &connect_hnd);
6290 if (NT_STATUS_IS_ERR(nt_status)) {
6291 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6292 nt_errstr(nt_status)));
6293 cli_shutdown(cli);
6294 talloc_destroy(mem_ctx);
6295 return -1;
6298 /* query info level 5 to obtain sid of a domain being queried */
6299 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6300 &connect_hnd,
6301 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6302 &info);
6304 if (NT_STATUS_IS_ERR(nt_status)) {
6305 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6306 nt_errstr(nt_status)));
6307 cli_shutdown(cli);
6308 talloc_destroy(mem_ctx);
6309 return -1;
6312 queried_dom_sid = info->account_domain.sid;
6315 * Keep calling LsaEnumTrustdom over opened pipe until
6316 * the end of enumeration is reached
6319 d_printf("Trusted domains list:\n\n");
6321 do {
6322 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6323 &connect_hnd,
6324 &enum_ctx,
6325 &dom_list,
6326 (uint32_t)-1);
6327 if (NT_STATUS_IS_ERR(nt_status)) {
6328 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6329 nt_errstr(nt_status)));
6330 cli_shutdown(cli);
6331 talloc_destroy(mem_ctx);
6332 return -1;
6335 for (i = 0; i < dom_list.count; i++) {
6336 print_trusted_domain(dom_list.domains[i].sid,
6337 dom_list.domains[i].name.string);
6341 * in case of no trusted domains say something rather
6342 * than just display blank line
6344 if (!dom_list.count) d_printf("none\n");
6346 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6348 /* close this connection before doing next one */
6349 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6350 if (NT_STATUS_IS_ERR(nt_status)) {
6351 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6352 nt_errstr(nt_status)));
6353 cli_shutdown(cli);
6354 talloc_destroy(mem_ctx);
6355 return -1;
6358 TALLOC_FREE(pipe_hnd);
6361 * Listing trusting domains (stored in passdb backend, if local)
6364 d_printf("\nTrusting domains list:\n\n");
6367 * Open \PIPE\samr and get needed policy handles
6369 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6370 if (!pipe_hnd) {
6371 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6372 cli_shutdown(cli);
6373 talloc_destroy(mem_ctx);
6374 return -1;
6377 /* SamrConnect2 */
6378 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6379 pipe_hnd->desthost,
6380 SA_RIGHT_SAM_OPEN_DOMAIN,
6381 &connect_hnd);
6382 if (!NT_STATUS_IS_OK(nt_status)) {
6383 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6384 nt_errstr(nt_status)));
6385 cli_shutdown(cli);
6386 talloc_destroy(mem_ctx);
6387 return -1;
6390 /* SamrOpenDomain - we have to open domain policy handle in order to be
6391 able to enumerate accounts*/
6392 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6393 &connect_hnd,
6394 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6395 queried_dom_sid,
6396 &domain_hnd);
6397 if (!NT_STATUS_IS_OK(nt_status)) {
6398 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6399 nt_errstr(nt_status)));
6400 cli_shutdown(cli);
6401 talloc_destroy(mem_ctx);
6402 return -1;
6406 * perform actual enumeration
6409 enum_ctx = 0; /* reset enumeration context from last enumeration */
6410 do {
6412 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6413 &domain_hnd,
6414 &enum_ctx,
6415 ACB_DOMTRUST,
6416 &trusts,
6417 0xffff,
6418 &num_domains);
6419 if (NT_STATUS_IS_ERR(nt_status)) {
6420 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6421 nt_errstr(nt_status)));
6422 cli_shutdown(cli);
6423 talloc_destroy(mem_ctx);
6424 return -1;
6427 for (i = 0; i < num_domains; i++) {
6429 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6432 * get each single domain's sid (do we _really_ need this ?):
6433 * 1) connect to domain's pdc
6434 * 2) query the pdc for domain's sid
6437 /* get rid of '$' tail */
6438 ascii_dom_name_len = strlen(str);
6439 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6440 str[ascii_dom_name_len - 1] = '\0';
6442 /* calculate padding space for d_printf to look nicer */
6443 pad_len = col_len - strlen(str);
6444 padding[pad_len] = 0;
6445 do padding[--pad_len] = ' '; while (pad_len);
6447 /* set opt_* variables to remote domain */
6448 strupper_m(str);
6449 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6450 c->opt_target_workgroup = c->opt_workgroup;
6452 d_printf("%s%s", str, padding);
6454 /* connect to remote domain controller */
6455 nt_status = net_make_ipc_connection(c,
6456 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6457 &remote_cli);
6458 if (NT_STATUS_IS_OK(nt_status)) {
6459 /* query for domain's sid */
6460 if (run_rpc_command(c, remote_cli, PI_LSARPC, 0,
6461 rpc_query_domain_sid, argc,
6462 argv))
6463 d_fprintf(stderr, "couldn't get domain's sid\n");
6465 cli_shutdown(remote_cli);
6467 } else {
6468 d_fprintf(stderr, "domain controller is not "
6469 "responding: %s\n",
6470 nt_errstr(nt_status));
6474 if (!num_domains) d_printf("none\n");
6476 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6478 /* close opened samr and domain policy handles */
6479 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6480 if (!NT_STATUS_IS_OK(nt_status)) {
6481 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6484 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6485 if (!NT_STATUS_IS_OK(nt_status)) {
6486 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6489 /* close samr pipe and connection to IPC$ */
6490 cli_shutdown(cli);
6492 talloc_destroy(mem_ctx);
6493 return 0;
6497 * Entrypoint for 'net rpc trustdom' code
6499 * @param argc standard argc
6500 * @param argv standard argv without initial components
6502 * @return Integer status (0 means success)
6505 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6507 struct functable func[] = {
6508 {"add", rpc_trustdom_add},
6509 {"del", rpc_trustdom_del},
6510 {"establish", rpc_trustdom_establish},
6511 {"revoke", rpc_trustdom_revoke},
6512 {"help", rpc_trustdom_usage},
6513 {"list", rpc_trustdom_list},
6514 {"vampire", rpc_trustdom_vampire},
6515 {NULL, NULL}
6518 if (argc == 0) {
6519 rpc_trustdom_usage(c, argc, argv);
6520 return -1;
6523 return net_run_function(c, argc, argv, func, rpc_user_usage);
6527 * Check if a server will take rpc commands
6528 * @param flags Type of server to connect to (PDC, DMB, localhost)
6529 * if the host is not explicitly specified
6530 * @return bool (true means rpc supported)
6532 bool net_rpc_check(struct net_context *c, unsigned flags)
6534 struct cli_state *cli;
6535 bool ret = false;
6536 struct sockaddr_storage server_ss;
6537 char *server_name = NULL;
6538 NTSTATUS status;
6540 /* flags (i.e. server type) may depend on command */
6541 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6542 return false;
6544 if ((cli = cli_initialise()) == NULL) {
6545 return false;
6548 status = cli_connect(cli, server_name, &server_ss);
6549 if (!NT_STATUS_IS_OK(status))
6550 goto done;
6551 if (!attempt_netbios_session_request(&cli, global_myname(),
6552 server_name, &server_ss))
6553 goto done;
6554 if (!cli_negprot(cli))
6555 goto done;
6556 if (cli->protocol < PROTOCOL_NT1)
6557 goto done;
6559 ret = true;
6560 done:
6561 cli_shutdown(cli);
6562 return ret;
6565 /* dump sam database via samsync rpc calls */
6566 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6567 return run_rpc_command(c, NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS,
6568 rpc_samdump_internals, argc, argv);
6571 /* syncronise sam database via samsync rpc calls */
6572 static int rpc_vampire(struct net_context *c, int argc, const char **argv) {
6573 return run_rpc_command(c, NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS,
6574 rpc_vampire_internals, argc, argv);
6578 * Migrate everything from a print-server
6580 * @param c A net_context structure
6581 * @param argc Standard main() style argc
6582 * @param argv Standard main() style argv. Initial components are already
6583 * stripped
6585 * @return A shell status integer (0 for success)
6587 * The order is important !
6588 * To successfully add drivers the print-queues have to exist !
6589 * Applying ACLs should be the last step, because you're easily locked out
6592 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6593 const char **argv)
6595 int ret;
6597 if (!c->opt_host) {
6598 printf("no server to migrate\n");
6599 return -1;
6602 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6603 rpc_printer_migrate_printers_internals, argc,
6604 argv);
6605 if (ret)
6606 return ret;
6608 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6609 rpc_printer_migrate_drivers_internals, argc,
6610 argv);
6611 if (ret)
6612 return ret;
6614 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6615 rpc_printer_migrate_forms_internals, argc, argv);
6616 if (ret)
6617 return ret;
6619 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6620 rpc_printer_migrate_settings_internals, argc,
6621 argv);
6622 if (ret)
6623 return ret;
6625 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6626 rpc_printer_migrate_security_internals, argc,
6627 argv);
6632 * Migrate print-drivers from a print-server
6634 * @param c A net_context structure
6635 * @param argc Standard main() style argc
6636 * @param argv Standard main() style argv. Initial components are already
6637 * stripped
6639 * @return A shell status integer (0 for success)
6641 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6642 const char **argv)
6644 if (!c->opt_host) {
6645 printf("no server to migrate\n");
6646 return -1;
6649 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6650 rpc_printer_migrate_drivers_internals,
6651 argc, argv);
6655 * Migrate print-forms from a print-server
6657 * @param c A net_context structure
6658 * @param argc Standard main() style argc
6659 * @param argv Standard main() style argv. Initial components are already
6660 * stripped
6662 * @return A shell status integer (0 for success)
6664 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6665 const char **argv)
6667 if (!c->opt_host) {
6668 printf("no server to migrate\n");
6669 return -1;
6672 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6673 rpc_printer_migrate_forms_internals,
6674 argc, argv);
6678 * Migrate printers from a print-server
6680 * @param c A net_context structure
6681 * @param argc Standard main() style argc
6682 * @param argv Standard main() style argv. Initial components are already
6683 * stripped
6685 * @return A shell status integer (0 for success)
6687 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6688 const char **argv)
6690 if (!c->opt_host) {
6691 printf("no server to migrate\n");
6692 return -1;
6695 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6696 rpc_printer_migrate_printers_internals,
6697 argc, argv);
6701 * Migrate printer-ACLs from a print-server
6703 * @param c A net_context structure
6704 * @param argc Standard main() style argc
6705 * @param argv Standard main() style argv. Initial components are already
6706 * stripped
6708 * @return A shell status integer (0 for success)
6710 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6711 const char **argv)
6713 if (!c->opt_host) {
6714 printf("no server to migrate\n");
6715 return -1;
6718 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6719 rpc_printer_migrate_security_internals,
6720 argc, argv);
6724 * Migrate printer-settings from a print-server
6726 * @param c A net_context structure
6727 * @param argc Standard main() style argc
6728 * @param argv Standard main() style argv. Initial components are already
6729 * stripped
6731 * @return A shell status integer (0 for success)
6733 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6734 const char **argv)
6736 if (!c->opt_host) {
6737 printf("no server to migrate\n");
6738 return -1;
6741 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6742 rpc_printer_migrate_settings_internals,
6743 argc, argv);
6747 * 'net rpc printer' entrypoint.
6749 * @param c A net_context structure
6750 * @param argc Standard main() style argc
6751 * @param argv Standard main() style argv. Initial components are already
6752 * stripped
6755 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6758 /* ouch: when addriver and setdriver are called from within
6759 rpc_printer_migrate_drivers_internals, the printer-queue already
6760 *has* to exist */
6762 struct functable func[] = {
6763 {"all", rpc_printer_migrate_all},
6764 {"drivers", rpc_printer_migrate_drivers},
6765 {"forms", rpc_printer_migrate_forms},
6766 {"help", rpc_printer_usage},
6767 {"printers", rpc_printer_migrate_printers},
6768 {"security", rpc_printer_migrate_security},
6769 {"settings", rpc_printer_migrate_settings},
6770 {NULL, NULL}
6773 return net_run_function(c, argc, argv, func, rpc_printer_usage);
6778 * List printers on a remote RPC server
6780 * @param c A net_context structure
6781 * @param argc Standard main() style argc
6782 * @param argv Standard main() style argv. Initial components are already
6783 * stripped
6785 * @return A shell status integer (0 for success)
6787 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6790 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6791 rpc_printer_list_internals,
6792 argc, argv);
6796 * List printer-drivers on a remote RPC server
6798 * @param c A net_context structure
6799 * @param argc Standard main() style argc
6800 * @param argv Standard main() style argv. Initial components are already
6801 * stripped
6803 * @return A shell status integer (0 for success)
6805 static int rpc_printer_driver_list(struct net_context *c, int argc,
6806 const char **argv)
6809 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6810 rpc_printer_driver_list_internals,
6811 argc, argv);
6815 * Publish printer in ADS via MSRPC
6817 * @param c A net_context structure
6818 * @param argc Standard main() style argc
6819 * @param argv Standard main() style argv. Initial components are already
6820 * stripped
6822 * @return A shell status integer (0 for success)
6824 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6825 const char **argv)
6828 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6829 rpc_printer_publish_publish_internals,
6830 argc, argv);
6834 * Update printer in ADS via MSRPC
6836 * @param c A net_context structure
6837 * @param argc Standard main() style argc
6838 * @param argv Standard main() style argv. Initial components are already
6839 * stripped
6841 * @return A shell status integer (0 for success)
6843 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6846 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6847 rpc_printer_publish_update_internals,
6848 argc, argv);
6852 * UnPublish printer in ADS via MSRPC
6854 * @param c A net_context structure
6855 * @param argc Standard main() style argc
6856 * @param argv Standard main() style argv. Initial components are already
6857 * stripped
6859 * @return A shell status integer (0 for success)
6861 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
6862 const char **argv)
6865 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6866 rpc_printer_publish_unpublish_internals,
6867 argc, argv);
6871 * List published printers via MSRPC
6873 * @param c A net_context structure
6874 * @param argc Standard main() style argc
6875 * @param argv Standard main() style argv. Initial components are already
6876 * stripped
6878 * @return A shell status integer (0 for success)
6880 static int rpc_printer_publish_list(struct net_context *c, int argc,
6881 const char **argv)
6884 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6885 rpc_printer_publish_list_internals,
6886 argc, argv);
6891 * Publish printer in ADS
6893 * @param c A net_context structure
6894 * @param argc Standard main() style argc
6895 * @param argv Standard main() style argv. Initial components are already
6896 * stripped
6898 * @return A shell status integer (0 for success)
6900 static int rpc_printer_publish(struct net_context *c, int argc,
6901 const char **argv)
6904 struct functable func[] = {
6905 {"publish", rpc_printer_publish_publish},
6906 {"update", rpc_printer_publish_update},
6907 {"unpublish", rpc_printer_publish_unpublish},
6908 {"list", rpc_printer_publish_list},
6909 {"help", rpc_printer_usage},
6910 {NULL, NULL}
6913 if (argc == 0)
6914 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6915 rpc_printer_publish_list_internals,
6916 argc, argv);
6918 return net_run_function(c, argc, argv, func, rpc_printer_usage);
6924 * Display rpc printer help page.
6926 * @param c A net_context structure
6927 * @param argc Standard main() style argc
6928 * @param argv Standard main() style argv. Initial components are already
6929 * stripped
6931 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
6933 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"\
6934 "\tlists all printers on print-server\n\n");
6935 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"\
6936 "\tlists all printer-drivers on print-server\n\n");
6937 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"\
6938 "\tpublishes printer settings in Active Directory\n"
6939 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
6940 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"\
6941 "\n\tmigrates printers from remote to local server\n\n");
6942 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"\
6943 "\n\tmigrates printer-settings from remote to local server\n\n");
6944 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"\
6945 "\n\tmigrates printer-drivers from remote to local server\n\n");
6946 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"\
6947 "\n\tmigrates printer-forms from remote to local server\n\n");
6948 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"\
6949 "\n\tmigrates printer-ACLs from remote to local server\n\n");
6950 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"\
6951 "\n\tmigrates drivers, forms, queues, settings and acls from\n"\
6952 "\tremote to local print-server\n\n");
6953 net_common_methods_usage(c, argc, argv);
6954 net_common_flags_usage(c, argc, argv);
6955 d_printf(
6956 "\t-v or --verbose\t\t\tgive verbose output\n"
6957 "\t --destination\t\tmigration target server (default: localhost)\n");
6959 return -1;
6963 * 'net rpc printer' entrypoint.
6965 * @param c A net_context structure
6966 * @param argc Standard main() style argc
6967 * @param argv Standard main() style argv. Initial components are already
6968 * stripped
6970 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
6972 struct functable func[] = {
6973 {"list", rpc_printer_list},
6974 {"migrate", rpc_printer_migrate},
6975 {"driver", rpc_printer_driver_list},
6976 {"publish", rpc_printer_publish},
6977 {NULL, NULL}
6980 if (argc == 0)
6981 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6982 rpc_printer_list_internals,
6983 argc, argv);
6985 return net_run_function(c, argc, argv, func, rpc_printer_usage);
6988 /****************************************************************************/
6992 * Basic help function for 'net rpc'
6994 * @param c A net_context structure
6995 * @param argc Standard main() style argc
6996 * @param argv Standard main() style argv. Initial components are already
6997 * stripped
7000 int net_rpc_help(struct net_context *c, int argc, const char **argv)
7002 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
7003 d_printf(" net rpc join \t\t\tto join a domain \n");
7004 d_printf(" net rpc oldjoin \t\tto join a domain created in server manager\n");
7005 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
7006 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
7007 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
7008 d_printf(" net rpc group \t\tto list groups\n");
7009 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
7010 d_printf(" net rpc printer \t\tto list and migrate printers\n");
7011 d_printf(" net rpc file \t\t\tto list open files\n");
7012 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
7013 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
7014 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
7015 d_printf(" net rpc samdump \t\tdisplay an NT PDC's users, groups and other data\n");
7016 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
7017 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
7018 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
7019 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
7020 d_printf(" net rpc registry\t\tto manage registry hives\n");
7021 d_printf(" net rpc service\t\tto start, stop and query services\n");
7022 d_printf(" net rpc audit\t\t\tto modify global auditing settings\n");
7023 d_printf(" net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
7024 d_printf("\n");
7025 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
7026 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
7027 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
7028 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
7029 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
7030 return -1;
7035 * Help function for 'net rpc'. Calls command specific help if requested
7036 * or displays usage of net rpc
7038 * @param c A net_context structure
7039 * @param argc Standard main() style argc
7040 * @param argv Standard main() style argv. Initial components are already
7041 * stripped
7044 int net_rpc_usage(struct net_context *c, int argc, const char **argv)
7046 struct functable func[] = {
7047 {"join", rpc_join_usage},
7048 {"user", rpc_user_usage},
7049 {"group", rpc_group_usage},
7050 {"share", rpc_share_usage},
7051 /*{"changetrustpw", rpc_changetrustpw_usage}, */
7052 {"trustdom", rpc_trustdom_usage},
7053 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
7054 /*{"shutdown", rpc_shutdown_usage}, */
7055 {"vampire", rpc_vampire_usage},
7056 {"help", net_rpc_help},
7057 {NULL, NULL}
7060 if (argc == 0) {
7061 net_rpc_help(c, argc, argv);
7062 return -1;
7065 return net_run_function(c, argc, argv, func, net_rpc_help);
7069 * 'net rpc' entrypoint.
7071 * @param c A net_context structure
7072 * @param argc Standard main() style argc
7073 * @param argv Standard main() style argv. Initial components are already
7074 * stripped
7077 int net_rpc(struct net_context *c, int argc, const char **argv)
7079 struct functable func[] = {
7080 {"audit", net_rpc_audit},
7081 {"info", net_rpc_info},
7082 {"join", net_rpc_join},
7083 {"oldjoin", net_rpc_oldjoin},
7084 {"testjoin", net_rpc_testjoin},
7085 {"user", net_rpc_user},
7086 {"password", rpc_user_password},
7087 {"group", net_rpc_group},
7088 {"share", net_rpc_share},
7089 {"file", net_rpc_file},
7090 {"printer", net_rpc_printer},
7091 {"changetrustpw", net_rpc_changetrustpw},
7092 {"trustdom", rpc_trustdom},
7093 {"abortshutdown", rpc_shutdown_abort},
7094 {"shutdown", rpc_shutdown},
7095 {"samdump", rpc_samdump},
7096 {"vampire", rpc_vampire},
7097 {"getsid", net_rpc_getsid},
7098 {"rights", net_rpc_rights},
7099 {"service", net_rpc_service},
7100 {"registry", net_rpc_registry},
7101 {"shell", net_rpc_shell},
7102 {"help", net_rpc_help},
7103 {NULL, NULL}
7105 return net_run_function(c, argc, argv, func, net_rpc_usage);