s3-lsasd: Listen on parent messages
[Samba/bjacke.git] / source3 / utils / net_rpc.c
blobc2fc08693cb455b029117c41f7eb6b7b25974c7a
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"
25 #include "rpc_client/cli_pipe.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_samr_c.h"
28 #include "rpc_client/cli_samr.h"
29 #include "rpc_client/init_samr.h"
30 #include "../librpc/gen_ndr/ndr_lsa_c.h"
31 #include "rpc_client/cli_lsarpc.h"
32 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
33 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
34 #include "../librpc/gen_ndr/ndr_spoolss.h"
35 #include "../librpc/gen_ndr/ndr_initshutdown_c.h"
36 #include "../librpc/gen_ndr/ndr_winreg_c.h"
37 #include "secrets.h"
38 #include "lib/netapi/netapi.h"
39 #include "lib/netapi/netapi_net.h"
40 #include "rpc_client/init_lsa.h"
41 #include "../libcli/security/security.h"
42 #include "libsmb/libsmb.h"
43 #include "libsmb/clirap.h"
44 #include "nsswitch/libwbclient/wbclient.h"
45 #include "passdb.h"
47 static int net_mode_share;
48 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
50 /**
51 * @file net_rpc.c
53 * @brief RPC based subcommands for the 'net' utility.
55 * This file should contain much of the functionality that used to
56 * be found in rpcclient, execpt that the commands should change
57 * less often, and the fucntionality should be sane (the user is not
58 * expected to know a rid/sid before they conduct an operation etc.)
60 * @todo Perhaps eventually these should be split out into a number
61 * of files, as this could get quite big.
62 **/
65 /**
66 * Many of the RPC functions need the domain sid. This function gets
67 * it at the start of every run
69 * @param cli A cli_state already connected to the remote machine
71 * @return The Domain SID of the remote machine.
72 **/
74 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
75 struct dom_sid **domain_sid,
76 const char **domain_name)
78 struct rpc_pipe_client *lsa_pipe = NULL;
79 struct policy_handle pol;
80 NTSTATUS status, result;
81 union lsa_PolicyInformation *info = NULL;
82 struct dcerpc_binding_handle *b;
84 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
85 &lsa_pipe);
86 if (!NT_STATUS_IS_OK(status)) {
87 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
88 return status;
91 b = lsa_pipe->binding_handle;
93 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
94 SEC_FLAG_MAXIMUM_ALLOWED,
95 &pol);
96 if (!NT_STATUS_IS_OK(status)) {
97 d_fprintf(stderr, "open_policy %s: %s\n",
98 _("failed"),
99 nt_errstr(status));
100 return status;
103 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
104 &pol,
105 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
106 &info,
107 &result);
108 if (!NT_STATUS_IS_OK(status)) {
109 d_fprintf(stderr, "lsaquery %s: %s\n",
110 _("failed"),
111 nt_errstr(status));
112 return status;
114 if (!NT_STATUS_IS_OK(result)) {
115 d_fprintf(stderr, "lsaquery %s: %s\n",
116 _("failed"),
117 nt_errstr(result));
118 return result;
121 *domain_name = info->account_domain.name.string;
122 *domain_sid = info->account_domain.sid;
124 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
125 TALLOC_FREE(lsa_pipe);
127 return NT_STATUS_OK;
131 * Run a single RPC command, from start to finish.
133 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
134 * @param conn_flag a NET_FLAG_ combination. Passed to
135 * net_make_ipc_connection.
136 * @param argc Standard main() style argc.
137 * @param argv Standard main() style argv. Initial components are already
138 * stripped.
139 * @return A shell status integer (0 for success).
142 int run_rpc_command(struct net_context *c,
143 struct cli_state *cli_arg,
144 const struct ndr_syntax_id *interface,
145 int conn_flags,
146 rpc_command_fn fn,
147 int argc,
148 const char **argv)
150 struct cli_state *cli = NULL;
151 struct rpc_pipe_client *pipe_hnd = NULL;
152 TALLOC_CTX *mem_ctx;
153 NTSTATUS nt_status;
154 struct dom_sid *domain_sid;
155 const char *domain_name;
156 int ret = -1;
158 /* make use of cli_state handed over as an argument, if possible */
159 if (!cli_arg) {
160 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
161 if (!NT_STATUS_IS_OK(nt_status)) {
162 DEBUG(1, ("failed to make ipc connection: %s\n",
163 nt_errstr(nt_status)));
164 return -1;
166 } else {
167 cli = cli_arg;
170 if (!cli) {
171 return -1;
174 /* Create mem_ctx */
176 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
177 DEBUG(0, ("talloc_init() failed\n"));
178 goto fail;
181 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
182 &domain_name);
183 if (!NT_STATUS_IS_OK(nt_status)) {
184 goto fail;
187 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
188 if (lp_client_schannel()
189 && (ndr_syntax_id_equal(interface,
190 &ndr_table_netlogon.syntax_id))) {
191 /* Always try and create an schannel netlogon pipe. */
192 nt_status = cli_rpc_pipe_open_schannel(
193 cli, interface, NCACN_NP,
194 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
195 &pipe_hnd);
196 if (!NT_STATUS_IS_OK(nt_status)) {
197 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
198 nt_errstr(nt_status) ));
199 goto fail;
201 } else {
202 if (conn_flags & NET_FLAGS_SEAL) {
203 nt_status = cli_rpc_pipe_open_ntlmssp(
204 cli, interface,
205 (conn_flags & NET_FLAGS_TCP) ?
206 NCACN_IP_TCP : NCACN_NP,
207 DCERPC_AUTH_LEVEL_PRIVACY,
208 lp_workgroup(), c->opt_user_name,
209 c->opt_password, &pipe_hnd);
210 } else {
211 nt_status = cli_rpc_pipe_open_noauth(
212 cli, interface,
213 &pipe_hnd);
215 if (!NT_STATUS_IS_OK(nt_status)) {
216 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
217 get_pipe_name_from_syntax(
218 talloc_tos(), interface),
219 nt_errstr(nt_status) ));
220 goto fail;
225 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
227 if (!NT_STATUS_IS_OK(nt_status)) {
228 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
229 } else {
230 ret = 0;
231 DEBUG(5, ("rpc command function succedded\n"));
234 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
235 if (pipe_hnd) {
236 TALLOC_FREE(pipe_hnd);
240 fail:
241 /* close the connection only if it was opened here */
242 if (!cli_arg) {
243 cli_shutdown(cli);
246 talloc_destroy(mem_ctx);
247 return ret;
251 * Force a change of the trust acccount password.
253 * All parameters are provided by the run_rpc_command function, except for
254 * argc, argv which are passed through.
256 * @param domain_sid The domain sid acquired from the remote server.
257 * @param cli A cli_state connected to the server.
258 * @param mem_ctx Talloc context, destroyed on completion of the function.
259 * @param argc Standard main() style argc.
260 * @param argv Standard main() style argv. Initial components are already
261 * stripped.
263 * @return Normal NTSTATUS return.
266 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
267 const struct dom_sid *domain_sid,
268 const char *domain_name,
269 struct cli_state *cli,
270 struct rpc_pipe_client *pipe_hnd,
271 TALLOC_CTX *mem_ctx,
272 int argc,
273 const char **argv)
275 NTSTATUS status;
277 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
278 if (!NT_STATUS_IS_OK(status)) {
279 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
280 nt_errstr(status));
281 return status;
284 return NT_STATUS_OK;
288 * Force a change of the trust acccount password.
290 * @param argc Standard main() style argc.
291 * @param argv Standard main() style argv. Initial components are already
292 * stripped.
294 * @return A shell status integer (0 for success).
297 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
299 if (c->display_usage) {
300 d_printf( "%s\n"
301 "net rpc changetrustpw\n"
302 " %s\n",
303 _("Usage:"),
304 _("Change the machine trust password"));
305 return 0;
308 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
309 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
310 rpc_changetrustpw_internals,
311 argc, argv);
315 * Join a domain, the old way.
317 * This uses 'machinename' as the inital password, and changes it.
319 * The password should be created with 'server manager' or equiv first.
321 * All parameters are provided by the run_rpc_command function, except for
322 * argc, argv which are passed through.
324 * @param domain_sid The domain sid acquired from the remote server.
325 * @param cli A cli_state connected to the server.
326 * @param mem_ctx Talloc context, destroyed on completion of the function.
327 * @param argc Standard main() style argc.
328 * @param argv Standard main() style argv. Initial components are already
329 * stripped.
331 * @return Normal NTSTATUS return.
334 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
335 const struct dom_sid *domain_sid,
336 const char *domain_name,
337 struct cli_state *cli,
338 struct rpc_pipe_client *pipe_hnd,
339 TALLOC_CTX *mem_ctx,
340 int argc,
341 const char **argv)
344 fstring trust_passwd;
345 unsigned char orig_trust_passwd_hash[16];
346 NTSTATUS result;
347 enum netr_SchannelType sec_channel_type;
349 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
350 &pipe_hnd);
351 if (!NT_STATUS_IS_OK(result)) {
352 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
353 "error was %s\n",
354 cli_state_remote_name(cli),
355 nt_errstr(result) ));
356 return result;
360 check what type of join - if the user want's to join as
361 a BDC, the server must agree that we are a BDC.
363 if (argc >= 0) {
364 sec_channel_type = get_sec_channel_type(argv[0]);
365 } else {
366 sec_channel_type = get_sec_channel_type(NULL);
369 fstrcpy(trust_passwd, lp_netbios_name());
370 strlower_m(trust_passwd);
373 * Machine names can be 15 characters, but the max length on
374 * a password is 14. --jerry
377 trust_passwd[14] = '\0';
379 E_md4hash(trust_passwd, orig_trust_passwd_hash);
381 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
382 lp_netbios_name(),
383 orig_trust_passwd_hash,
384 sec_channel_type);
386 if (NT_STATUS_IS_OK(result))
387 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
390 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
391 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
392 result = NT_STATUS_UNSUCCESSFUL;
395 return result;
399 * Join a domain, the old way.
401 * @param argc Standard main() style argc.
402 * @param argv Standard main() style argv. Initial components are already
403 * stripped.
405 * @return A shell status integer (0 for success).
408 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
410 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
411 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
412 rpc_oldjoin_internals,
413 argc, argv);
417 * Join a domain, the old way. This function exists to allow
418 * the message to be displayed when oldjoin was explicitly
419 * requested, but not when it was implied by "net rpc join".
421 * @param argc Standard main() style argc.
422 * @param argv Standard main() style argv. Initial components are already
423 * stripped.
425 * @return A shell status integer (0 for success).
428 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
430 int rc = -1;
432 if (c->display_usage) {
433 d_printf( "%s\n"
434 "net rpc oldjoin\n"
435 " %s\n",
436 _("Usage:"),
437 _("Join a domain the old way"));
438 return 0;
441 rc = net_rpc_perform_oldjoin(c, argc, argv);
443 if (rc) {
444 d_fprintf(stderr, _("Failed to join domain\n"));
447 return rc;
451 * 'net rpc join' entrypoint.
452 * @param argc Standard main() style argc.
453 * @param argv Standard main() style argv. Initial components are already
454 * stripped
456 * Main 'net_rpc_join()' (where the admin username/password is used) is
457 * in net_rpc_join.c.
458 * Try to just change the password, but if that doesn't work, use/prompt
459 * for a username/password.
462 int net_rpc_join(struct net_context *c, int argc, const char **argv)
464 if (c->display_usage) {
465 d_printf("%s\n%s",
466 _("Usage:"),
467 _("net rpc join -U <username>[%%password] <type>\n"
468 " Join a domain\n"
469 " username\tName of the admin user"
470 " password\tPassword of the admin user, will "
471 "prompt if not specified\n"
472 " type\tCan be one of the following:\n"
473 "\t\tMEMBER\tJoin as member server (default)\n"
474 "\t\tBDC\tJoin as BDC\n"
475 "\t\tPDC\tJoin as PDC\n"));
476 return 0;
479 if (lp_server_role() == ROLE_STANDALONE) {
480 d_printf(_("cannot join as standalone machine\n"));
481 return -1;
484 if (strlen(lp_netbios_name()) > 15) {
485 d_printf(_("Our netbios name can be at most 15 chars long, "
486 "\"%s\" is %u chars long\n"),
487 lp_netbios_name(), (unsigned int)strlen(lp_netbios_name()));
488 return -1;
491 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
492 return 0;
494 return net_rpc_join_newstyle(c, argc, argv);
498 * display info about a rpc domain
500 * All parameters are provided by the run_rpc_command function, except for
501 * argc, argv which are passed through.
503 * @param domain_sid The domain sid acquired from the remote server
504 * @param cli A cli_state connected to the server.
505 * @param mem_ctx Talloc context, destroyed on completion of the function.
506 * @param argc Standard main() style argc.
507 * @param argv Standard main() style argv. Initial components are already
508 * stripped.
510 * @return Normal NTSTATUS return.
513 NTSTATUS rpc_info_internals(struct net_context *c,
514 const struct dom_sid *domain_sid,
515 const char *domain_name,
516 struct cli_state *cli,
517 struct rpc_pipe_client *pipe_hnd,
518 TALLOC_CTX *mem_ctx,
519 int argc,
520 const char **argv)
522 struct policy_handle connect_pol, domain_pol;
523 NTSTATUS status, result;
524 union samr_DomainInfo *info = NULL;
525 fstring sid_str;
526 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
528 sid_to_fstring(sid_str, domain_sid);
530 /* Get sam policy handle */
531 status = dcerpc_samr_Connect2(b, mem_ctx,
532 pipe_hnd->desthost,
533 MAXIMUM_ALLOWED_ACCESS,
534 &connect_pol,
535 &result);
536 if (!NT_STATUS_IS_OK(status)) {
537 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
538 nt_errstr(status));
539 goto done;
542 if (!NT_STATUS_IS_OK(result)) {
543 status = result;
544 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
545 nt_errstr(result));
546 goto done;
549 /* Get domain policy handle */
550 status = dcerpc_samr_OpenDomain(b, mem_ctx,
551 &connect_pol,
552 MAXIMUM_ALLOWED_ACCESS,
553 discard_const_p(struct dom_sid2, domain_sid),
554 &domain_pol,
555 &result);
556 if (!NT_STATUS_IS_OK(status)) {
557 d_fprintf(stderr, _("Could not open domain: %s\n"),
558 nt_errstr(status));
559 goto done;
561 if (!NT_STATUS_IS_OK(result)) {
562 status = result;
563 d_fprintf(stderr, _("Could not open domain: %s\n"),
564 nt_errstr(result));
565 goto done;
568 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
569 &domain_pol,
571 &info,
572 &result);
573 if (!NT_STATUS_IS_OK(status)) {
574 goto done;
576 status = result;
577 if (NT_STATUS_IS_OK(result)) {
578 d_printf(_("Domain Name: %s\n"),
579 info->general.domain_name.string);
580 d_printf(_("Domain SID: %s\n"), sid_str);
581 d_printf(_("Sequence number: %llu\n"),
582 (unsigned long long)info->general.sequence_num);
583 d_printf(_("Num users: %u\n"), info->general.num_users);
584 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
585 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
588 done:
589 return status;
593 * 'net rpc info' entrypoint.
594 * @param argc Standard main() style argc.
595 * @param argv Standard main() style argv. Initial components are already
596 * stripped.
599 int net_rpc_info(struct net_context *c, int argc, const char **argv)
601 if (c->display_usage) {
602 d_printf( "%s\n"
603 "net rpc info\n"
604 " %s\n",
605 _("Usage:"),
606 _("Display information about the domain"));
607 return 0;
610 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
611 NET_FLAGS_PDC, rpc_info_internals,
612 argc, argv);
616 * Fetch domain SID into the local secrets.tdb.
618 * All parameters are provided by the run_rpc_command function, except for
619 * argc, argv which are passed through.
621 * @param domain_sid The domain sid acquired from the remote server.
622 * @param cli A cli_state connected to the server.
623 * @param mem_ctx Talloc context, destroyed on completion of the function.
624 * @param argc Standard main() style argc.
625 * @param argv Standard main() style argv. Initial components are already
626 * stripped.
628 * @return Normal NTSTATUS return.
631 static NTSTATUS rpc_getsid_internals(struct net_context *c,
632 const struct dom_sid *domain_sid,
633 const char *domain_name,
634 struct cli_state *cli,
635 struct rpc_pipe_client *pipe_hnd,
636 TALLOC_CTX *mem_ctx,
637 int argc,
638 const char **argv)
640 fstring sid_str;
642 sid_to_fstring(sid_str, domain_sid);
643 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
644 sid_str, domain_name);
646 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
647 DEBUG(0,("Can't store domain SID\n"));
648 return NT_STATUS_UNSUCCESSFUL;
651 return NT_STATUS_OK;
655 * 'net rpc getsid' entrypoint.
656 * @param argc Standard main() style argc.
657 * @param argv Standard main() style argv. Initial components are already
658 * stripped.
661 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
663 int conn_flags = NET_FLAGS_PDC;
665 if (!c->opt_user_specified) {
666 conn_flags |= NET_FLAGS_ANONYMOUS;
669 if (c->display_usage) {
670 d_printf( "%s\n"
671 "net rpc getsid\n"
672 " %s\n",
673 _("Usage:"),
674 _("Fetch domain SID into local secrets.tdb"));
675 return 0;
678 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
679 conn_flags,
680 rpc_getsid_internals,
681 argc, argv);
684 /****************************************************************************/
687 * Basic usage function for 'net rpc user'.
688 * @param argc Standard main() style argc.
689 * @param argv Standard main() style argv. Initial components are already
690 * stripped.
693 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
695 return net_user_usage(c, argc, argv);
699 * Add a new user to a remote RPC server.
701 * @param argc Standard main() style argc.
702 * @param argv Standard main() style argv. Initial components are already
703 * stripped.
705 * @return A shell status integer (0 for success).
708 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
710 NET_API_STATUS status;
711 struct USER_INFO_1 info1;
712 uint32_t parm_error = 0;
714 if (argc < 1 || c->display_usage) {
715 rpc_user_usage(c, argc, argv);
716 return 0;
719 ZERO_STRUCT(info1);
721 info1.usri1_name = argv[0];
722 if (argc == 2) {
723 info1.usri1_password = argv[1];
726 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
728 if (status != 0) {
729 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
730 argv[0], libnetapi_get_error_string(c->netapi_ctx,
731 status));
732 return -1;
733 } else {
734 d_printf(_("Added user '%s'.\n"), argv[0]);
737 return 0;
741 * Rename a user on a remote RPC server.
743 * @param argc Standard main() style argc.
744 * @param argv Standard main() style argv. Initial components are already
745 * stripped.
747 * @return A shell status integer (0 for success).
750 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
752 NET_API_STATUS status;
753 struct USER_INFO_0 u0;
754 uint32_t parm_err = 0;
756 if (argc != 2 || c->display_usage) {
757 rpc_user_usage(c, argc, argv);
758 return 0;
761 u0.usri0_name = argv[1];
763 status = NetUserSetInfo(c->opt_host, argv[0],
764 0, (uint8_t *)&u0, &parm_err);
765 if (status) {
766 d_fprintf(stderr,
767 _("Failed to rename user from %s to %s - %s\n"),
768 argv[0], argv[1],
769 libnetapi_get_error_string(c->netapi_ctx, status));
770 } else {
771 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
774 return status;
778 * Set a user's primary group
780 * @param argc Standard main() style argc.
781 * @param argv Standard main() style argv. Initial components are already
782 * stripped.
784 * @return A shell status integer (0 for success).
787 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
788 const char **argv)
790 NET_API_STATUS status;
791 uint8_t *buffer;
792 struct GROUP_INFO_2 *g2;
793 struct USER_INFO_1051 u1051;
794 uint32_t parm_err = 0;
796 if (argc != 2 || c->display_usage) {
797 rpc_user_usage(c, argc, argv);
798 return 0;
801 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
802 if (status) {
803 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
804 argv[1],
805 libnetapi_get_error_string(c->netapi_ctx, status));
806 return status;
808 g2 = (struct GROUP_INFO_2 *)buffer;
810 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
812 NetApiBufferFree(buffer);
814 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
815 (uint8_t *)&u1051, &parm_err);
816 if (status) {
817 d_fprintf(stderr,
818 _("Failed to set user's primary group %s to %s - "
819 "%s\n"), argv[0], argv[1],
820 libnetapi_get_error_string(c->netapi_ctx, status));
821 } else {
822 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
823 argv[1]);
825 return status;
829 * Delete a user from a remote RPC server.
831 * @param argc Standard main() style argc.
832 * @param argv Standard main() style argv. Initial components are already
833 * stripped.
835 * @return A shell status integer (0 for success).
838 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
840 NET_API_STATUS status;
842 if (argc < 1 || c->display_usage) {
843 rpc_user_usage(c, argc, argv);
844 return 0;
847 status = NetUserDel(c->opt_host, argv[0]);
849 if (status != 0) {
850 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
851 argv[0],
852 libnetapi_get_error_string(c->netapi_ctx, status));
853 return -1;
854 } else {
855 d_printf(_("Deleted user '%s'.\n"), argv[0]);
858 return 0;
862 * Set a user's password on a remote RPC server.
864 * @param argc Standard main() style argc.
865 * @param argv Standard main() style argv. Initial components are already
866 * stripped.
868 * @return A shell status integer (0 for success).
871 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
873 NET_API_STATUS status;
874 char *prompt = NULL;
875 struct USER_INFO_1003 u1003;
876 uint32_t parm_err = 0;
877 int ret;
879 if (argc < 1 || c->display_usage) {
880 rpc_user_usage(c, argc, argv);
881 return 0;
884 if (argv[1]) {
885 u1003.usri1003_password = argv[1];
886 } else {
887 ret = asprintf(&prompt, _("Enter new password for %s:"),
888 argv[0]);
889 if (ret == -1) {
890 return -1;
892 u1003.usri1003_password = talloc_strdup(c, getpass(prompt));
893 SAFE_FREE(prompt);
894 if (u1003.usri1003_password == NULL) {
895 return -1;
899 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
901 /* Display results */
902 if (status != 0) {
903 d_fprintf(stderr,
904 _("Failed to set password for '%s' with error: %s.\n"),
905 argv[0], libnetapi_get_error_string(c->netapi_ctx,
906 status));
907 return -1;
910 return 0;
914 * List a user's groups from 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_info(struct net_context *c, int argc, const char **argv)
926 NET_API_STATUS status;
927 struct GROUP_USERS_INFO_0 *u0 = NULL;
928 uint32_t entries_read = 0;
929 uint32_t total_entries = 0;
930 int i;
933 if (argc < 1 || c->display_usage) {
934 rpc_user_usage(c, argc, argv);
935 return 0;
938 status = NetUserGetGroups(c->opt_host,
939 argv[0],
941 (uint8_t **)(void *)&u0,
942 (uint32_t)-1,
943 &entries_read,
944 &total_entries);
945 if (status != 0) {
946 d_fprintf(stderr,
947 _("Failed to get groups for '%s' with error: %s.\n"),
948 argv[0], libnetapi_get_error_string(c->netapi_ctx,
949 status));
950 return -1;
953 for (i=0; i < entries_read; i++) {
954 printf("%s\n", u0->grui0_name);
955 u0++;
958 return 0;
962 * List users on a remote RPC server.
964 * All parameters are provided by the run_rpc_command function, except for
965 * argc, argv which are passed through.
967 * @param domain_sid The domain sid acquired from the remote server.
968 * @param cli A cli_state connected to the server.
969 * @param mem_ctx Talloc context, destroyed on completion of the function.
970 * @param argc Standard main() style argc.
971 * @param argv Standard main() style argv. Initial components are already
972 * stripped.
974 * @return Normal NTSTATUS return.
977 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
979 NET_API_STATUS status;
980 uint32_t start_idx=0, num_entries, i, loop_count = 0;
981 struct NET_DISPLAY_USER *info = NULL;
982 void *buffer = NULL;
984 /* Query domain users */
985 if (c->opt_long_list_entries)
986 d_printf(_("\nUser name Comment"
987 "\n-----------------------------\n"));
988 do {
989 uint32_t max_entries, max_size;
991 dcerpc_get_query_dispinfo_params(
992 loop_count, &max_entries, &max_size);
994 status = NetQueryDisplayInformation(c->opt_host,
996 start_idx,
997 max_entries,
998 max_size,
999 &num_entries,
1000 &buffer);
1001 if (status != 0 && status != ERROR_MORE_DATA) {
1002 return status;
1005 info = (struct NET_DISPLAY_USER *)buffer;
1007 for (i = 0; i < num_entries; i++) {
1009 if (c->opt_long_list_entries)
1010 printf("%-21.21s %s\n", info->usri1_name,
1011 info->usri1_comment);
1012 else
1013 printf("%s\n", info->usri1_name);
1014 info++;
1017 NetApiBufferFree(buffer);
1019 loop_count++;
1020 start_idx += num_entries;
1022 } while (status == ERROR_MORE_DATA);
1024 return status;
1028 * 'net rpc user' entrypoint.
1029 * @param argc Standard main() style argc.
1030 * @param argv Standard main() style argv. Initial components are already
1031 * stripped.
1034 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1036 NET_API_STATUS status;
1038 struct functable func[] = {
1040 "add",
1041 rpc_user_add,
1042 NET_TRANSPORT_RPC,
1043 N_("Add specified user"),
1044 N_("net rpc user add\n"
1045 " Add specified user")
1048 "info",
1049 rpc_user_info,
1050 NET_TRANSPORT_RPC,
1051 N_("List domain groups of user"),
1052 N_("net rpc user info\n"
1053 " List domain groups of user")
1056 "delete",
1057 rpc_user_delete,
1058 NET_TRANSPORT_RPC,
1059 N_("Remove specified user"),
1060 N_("net rpc user delete\n"
1061 " Remove specified user")
1064 "password",
1065 rpc_user_password,
1066 NET_TRANSPORT_RPC,
1067 N_("Change user password"),
1068 N_("net rpc user password\n"
1069 " Change user password")
1072 "rename",
1073 rpc_user_rename,
1074 NET_TRANSPORT_RPC,
1075 N_("Rename specified user"),
1076 N_("net rpc user rename\n"
1077 " Rename specified user")
1080 "setprimarygroup",
1081 rpc_user_setprimarygroup,
1082 NET_TRANSPORT_RPC,
1083 "Set a user's primary group",
1084 "net rpc user setprimarygroup\n"
1085 " Set a user's primary group"
1087 {NULL, NULL, 0, NULL, NULL}
1090 status = libnetapi_net_init(&c->netapi_ctx);
1091 if (status != 0) {
1092 return -1;
1094 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1095 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1096 if (c->opt_kerberos) {
1097 libnetapi_set_use_kerberos(c->netapi_ctx);
1100 if (argc == 0) {
1101 if (c->display_usage) {
1102 d_printf( "%s\n"
1103 "net rpc user\n"
1104 " %s\n",
1105 _("Usage:"),
1106 _("List all users"));
1107 net_display_usage_from_functable(func);
1108 return 0;
1111 return rpc_user_list(c, argc, argv);
1114 return net_run_function(c, argc, argv, "net rpc user", func);
1117 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1118 TALLOC_CTX *mem_ctx,
1119 struct rpc_sh_ctx *ctx,
1120 struct rpc_pipe_client *pipe_hnd,
1121 int argc, const char **argv)
1123 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1126 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1127 TALLOC_CTX *mem_ctx,
1128 struct rpc_sh_ctx *ctx,
1129 struct rpc_pipe_client *pipe_hnd,
1130 int argc, const char **argv)
1132 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1135 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1136 TALLOC_CTX *mem_ctx,
1137 struct rpc_sh_ctx *ctx,
1138 struct rpc_pipe_client *pipe_hnd,
1139 int argc, const char **argv,
1140 NTSTATUS (*fn)(
1141 struct net_context *c,
1142 TALLOC_CTX *mem_ctx,
1143 struct rpc_sh_ctx *ctx,
1144 struct rpc_pipe_client *pipe_hnd,
1145 struct policy_handle *user_hnd,
1146 int argc, const char **argv))
1148 struct policy_handle connect_pol, domain_pol, user_pol;
1149 NTSTATUS status, result;
1150 struct dom_sid sid;
1151 uint32 rid;
1152 enum lsa_SidType type;
1153 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1155 if (argc == 0) {
1156 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1157 ctx->whoami);
1158 return NT_STATUS_INVALID_PARAMETER;
1161 ZERO_STRUCT(connect_pol);
1162 ZERO_STRUCT(domain_pol);
1163 ZERO_STRUCT(user_pol);
1165 status = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1166 argv[0], NULL, NULL, &sid, &type);
1167 if (!NT_STATUS_IS_OK(status)) {
1168 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1169 nt_errstr(status));
1170 goto done;
1173 if (type != SID_NAME_USER) {
1174 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1175 sid_type_lookup(type));
1176 status = NT_STATUS_NO_SUCH_USER;
1177 goto done;
1180 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1181 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1182 status = NT_STATUS_NO_SUCH_USER;
1183 goto done;
1186 status = dcerpc_samr_Connect2(b, mem_ctx,
1187 pipe_hnd->desthost,
1188 MAXIMUM_ALLOWED_ACCESS,
1189 &connect_pol,
1190 &result);
1191 if (!NT_STATUS_IS_OK(status)) {
1192 goto done;
1194 if (!NT_STATUS_IS_OK(result)) {
1195 status = result;
1196 goto done;
1199 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1200 &connect_pol,
1201 MAXIMUM_ALLOWED_ACCESS,
1202 ctx->domain_sid,
1203 &domain_pol,
1204 &result);
1205 if (!NT_STATUS_IS_OK(status)) {
1206 goto done;
1208 if (!NT_STATUS_IS_OK(result)) {
1209 status = result;
1210 goto done;
1213 status = dcerpc_samr_OpenUser(b, mem_ctx,
1214 &domain_pol,
1215 MAXIMUM_ALLOWED_ACCESS,
1216 rid,
1217 &user_pol,
1218 &result);
1219 if (!NT_STATUS_IS_OK(status)) {
1220 goto done;
1222 if (!NT_STATUS_IS_OK(result)) {
1223 status = result;
1224 goto done;
1227 status = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1229 done:
1230 if (is_valid_policy_hnd(&user_pol)) {
1231 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1233 if (is_valid_policy_hnd(&domain_pol)) {
1234 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1236 if (is_valid_policy_hnd(&connect_pol)) {
1237 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1239 return status;
1242 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1243 TALLOC_CTX *mem_ctx,
1244 struct rpc_sh_ctx *ctx,
1245 struct rpc_pipe_client *pipe_hnd,
1246 struct policy_handle *user_hnd,
1247 int argc, const char **argv)
1249 NTSTATUS status, result;
1250 union samr_UserInfo *info = NULL;
1251 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1253 if (argc != 0) {
1254 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1255 ctx->whoami);
1256 return NT_STATUS_INVALID_PARAMETER;
1259 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1260 user_hnd,
1262 &info,
1263 &result);
1264 if (!NT_STATUS_IS_OK(status)) {
1265 return status;
1267 if (!NT_STATUS_IS_OK(result)) {
1268 return result;
1271 d_printf(_("user rid: %d, group rid: %d\n"),
1272 info->info21.rid,
1273 info->info21.primary_gid);
1275 return result;
1278 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1279 TALLOC_CTX *mem_ctx,
1280 struct rpc_sh_ctx *ctx,
1281 struct rpc_pipe_client *pipe_hnd,
1282 int argc, const char **argv)
1284 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1285 rpc_sh_user_show_internals);
1288 #define FETCHSTR(name, rec) \
1289 do { if (strequal(ctx->thiscmd, name)) { \
1290 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1291 } while (0);
1293 #define SETSTR(name, rec, flag) \
1294 do { if (strequal(ctx->thiscmd, name)) { \
1295 init_lsa_String(&(info->info21.rec), argv[0]); \
1296 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1297 } while (0);
1299 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1300 TALLOC_CTX *mem_ctx,
1301 struct rpc_sh_ctx *ctx,
1302 struct rpc_pipe_client *pipe_hnd,
1303 struct policy_handle *user_hnd,
1304 int argc, const char **argv)
1306 NTSTATUS status, result;
1307 const char *username;
1308 const char *oldval = "";
1309 union samr_UserInfo *info = NULL;
1310 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1312 if (argc > 1) {
1313 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1314 _("Usage:"), ctx->whoami);
1315 return NT_STATUS_INVALID_PARAMETER;
1318 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1319 user_hnd,
1321 &info,
1322 &result);
1323 if (!NT_STATUS_IS_OK(status)) {
1324 return status;
1326 if (!NT_STATUS_IS_OK(result)) {
1327 return result;
1330 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1332 FETCHSTR("fullname", full_name);
1333 FETCHSTR("homedir", home_directory);
1334 FETCHSTR("homedrive", home_drive);
1335 FETCHSTR("logonscript", logon_script);
1336 FETCHSTR("profilepath", profile_path);
1337 FETCHSTR("description", description);
1339 if (argc == 0) {
1340 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1341 goto done;
1344 if (strcmp(argv[0], "NULL") == 0) {
1345 argv[0] = "";
1348 ZERO_STRUCT(info->info21);
1350 SETSTR("fullname", full_name, FULL_NAME);
1351 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1352 SETSTR("homedrive", home_drive, HOME_DRIVE);
1353 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1354 SETSTR("profilepath", profile_path, PROFILE_PATH);
1355 SETSTR("description", description, DESCRIPTION);
1357 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1358 user_hnd,
1360 info,
1361 &result);
1362 if (!NT_STATUS_IS_OK(status)) {
1363 return status;
1366 status = result;
1368 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1369 ctx->thiscmd, oldval, argv[0]);
1371 done:
1373 return status;
1376 #define HANDLEFLG(name, rec) \
1377 do { if (strequal(ctx->thiscmd, name)) { \
1378 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1379 if (newval) { \
1380 newflags = oldflags | ACB_##rec; \
1381 } else { \
1382 newflags = oldflags & ~ACB_##rec; \
1383 } } } while (0);
1385 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1386 TALLOC_CTX *mem_ctx,
1387 struct rpc_sh_ctx *ctx,
1388 struct rpc_pipe_client *pipe_hnd,
1389 int argc, const char **argv)
1391 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1392 rpc_sh_user_str_edit_internals);
1395 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1396 TALLOC_CTX *mem_ctx,
1397 struct rpc_sh_ctx *ctx,
1398 struct rpc_pipe_client *pipe_hnd,
1399 struct policy_handle *user_hnd,
1400 int argc, const char **argv)
1402 NTSTATUS status, result;
1403 const char *username;
1404 const char *oldval = "unknown";
1405 uint32 oldflags, newflags;
1406 bool newval;
1407 union samr_UserInfo *info = NULL;
1408 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1410 if ((argc > 1) ||
1411 ((argc == 1) && !strequal(argv[0], "yes") &&
1412 !strequal(argv[0], "no"))) {
1413 /* TRANSATORS: The yes|no here are program keywords. Please do
1414 not translate. */
1415 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1416 ctx->whoami);
1417 return NT_STATUS_INVALID_PARAMETER;
1420 newval = strequal(argv[0], "yes");
1422 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1423 user_hnd,
1425 &info,
1426 &result);
1427 if (!NT_STATUS_IS_OK(status)) {
1428 return status;
1430 if (!NT_STATUS_IS_OK(result)) {
1431 return result;
1434 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1435 oldflags = info->info21.acct_flags;
1436 newflags = info->info21.acct_flags;
1438 HANDLEFLG("disabled", DISABLED);
1439 HANDLEFLG("pwnotreq", PWNOTREQ);
1440 HANDLEFLG("autolock", AUTOLOCK);
1441 HANDLEFLG("pwnoexp", PWNOEXP);
1443 if (argc == 0) {
1444 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1445 oldval);
1446 goto done;
1449 ZERO_STRUCT(info->info21);
1451 info->info21.acct_flags = newflags;
1452 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1454 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1455 user_hnd,
1457 info,
1458 &result);
1459 if (!NT_STATUS_IS_OK(status)) {
1460 goto done;
1462 status = result;
1463 if (NT_STATUS_IS_OK(result)) {
1464 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1465 ctx->thiscmd, oldval, argv[0]);
1468 done:
1470 return status;
1473 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1474 TALLOC_CTX *mem_ctx,
1475 struct rpc_sh_ctx *ctx,
1476 struct rpc_pipe_client *pipe_hnd,
1477 int argc, const char **argv)
1479 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1480 rpc_sh_user_flag_edit_internals);
1483 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1484 TALLOC_CTX *mem_ctx,
1485 struct rpc_sh_ctx *ctx)
1487 static struct rpc_sh_cmd cmds[] = {
1489 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1490 N_("Show/Set a user's full name") },
1492 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1493 N_("Show/Set a user's home directory") },
1495 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1496 N_("Show/Set a user's home drive") },
1498 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1499 N_("Show/Set a user's logon script") },
1501 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1502 N_("Show/Set a user's profile path") },
1504 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1505 N_("Show/Set a user's description") },
1507 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1508 N_("Show/Set whether a user is disabled") },
1510 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1511 N_("Show/Set whether a user locked out") },
1513 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1514 N_("Show/Set whether a user does not need a password") },
1516 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1517 N_("Show/Set whether a user's password does not expire") },
1519 { NULL, NULL, 0, NULL, NULL }
1522 return cmds;
1525 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1526 TALLOC_CTX *mem_ctx,
1527 struct rpc_sh_ctx *ctx)
1529 static struct rpc_sh_cmd cmds[] = {
1531 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1532 N_("List available users") },
1534 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1535 N_("List the domain groups a user is member of") },
1537 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1538 N_("Show info about a user") },
1540 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1541 N_("Show/Modify a user's fields") },
1543 { NULL, NULL, 0, NULL, NULL }
1546 return cmds;
1549 /****************************************************************************/
1552 * Basic usage function for 'net rpc group'.
1553 * @param argc Standard main() style argc.
1554 * @param argv Standard main() style argv. Initial components are already
1555 * stripped.
1558 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1560 return net_group_usage(c, argc, argv);
1564 * Delete group on a remote RPC server.
1566 * All parameters are provided by the run_rpc_command function, except for
1567 * argc, argv which are passed through.
1569 * @param domain_sid The domain sid acquired from the remote server.
1570 * @param cli A cli_state connected to the server.
1571 * @param mem_ctx Talloc context, destroyed on completion of the function.
1572 * @param argc Standard main() style argc.
1573 * @param argv Standard main() style argv. Initial components are already
1574 * stripped.
1576 * @return Normal NTSTATUS return.
1579 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1580 const struct dom_sid *domain_sid,
1581 const char *domain_name,
1582 struct cli_state *cli,
1583 struct rpc_pipe_client *pipe_hnd,
1584 TALLOC_CTX *mem_ctx,
1585 int argc,
1586 const char **argv)
1588 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1589 bool group_is_primary = false;
1590 NTSTATUS status, result;
1591 uint32_t group_rid;
1592 struct samr_RidAttrArray *rids = NULL;
1593 /* char **names; */
1594 int i;
1595 /* struct samr_RidWithAttribute *user_gids; */
1596 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1598 struct samr_Ids group_rids, name_types;
1599 struct lsa_String lsa_acct_name;
1600 union samr_UserInfo *info = NULL;
1602 if (argc < 1 || c->display_usage) {
1603 rpc_group_usage(c, argc,argv);
1604 return NT_STATUS_OK; /* ok? */
1607 status = dcerpc_samr_Connect2(b, mem_ctx,
1608 pipe_hnd->desthost,
1609 MAXIMUM_ALLOWED_ACCESS,
1610 &connect_pol,
1611 &result);
1612 if (!NT_STATUS_IS_OK(status)) {
1613 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1614 goto done;
1617 if (!NT_STATUS_IS_OK(result)) {
1618 status = result;
1619 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1620 goto done;
1623 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1624 &connect_pol,
1625 MAXIMUM_ALLOWED_ACCESS,
1626 discard_const_p(struct dom_sid2, domain_sid),
1627 &domain_pol,
1628 &result);
1629 if (!NT_STATUS_IS_OK(status)) {
1630 d_fprintf(stderr, _("Request open_domain failed\n"));
1631 goto done;
1634 if (!NT_STATUS_IS_OK(result)) {
1635 status = result;
1636 d_fprintf(stderr, _("Request open_domain failed\n"));
1637 goto done;
1640 init_lsa_String(&lsa_acct_name, argv[0]);
1642 status = dcerpc_samr_LookupNames(b, mem_ctx,
1643 &domain_pol,
1645 &lsa_acct_name,
1646 &group_rids,
1647 &name_types,
1648 &result);
1649 if (!NT_STATUS_IS_OK(status)) {
1650 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1651 goto done;
1654 if (!NT_STATUS_IS_OK(result)) {
1655 status = result;
1656 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1657 goto done;
1660 switch (name_types.ids[0])
1662 case SID_NAME_DOM_GRP:
1663 status = dcerpc_samr_OpenGroup(b, mem_ctx,
1664 &domain_pol,
1665 MAXIMUM_ALLOWED_ACCESS,
1666 group_rids.ids[0],
1667 &group_pol,
1668 &result);
1669 if (!NT_STATUS_IS_OK(status)) {
1670 d_fprintf(stderr, _("Request open_group failed"));
1671 goto done;
1674 if (!NT_STATUS_IS_OK(result)) {
1675 status = result;
1676 d_fprintf(stderr, _("Request open_group failed"));
1677 goto done;
1680 group_rid = group_rids.ids[0];
1682 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
1683 &group_pol,
1684 &rids,
1685 &result);
1686 if (!NT_STATUS_IS_OK(status)) {
1687 d_fprintf(stderr,
1688 _("Unable to query group members of %s"),
1689 argv[0]);
1690 goto done;
1693 if (!NT_STATUS_IS_OK(result)) {
1694 status = result;
1695 d_fprintf(stderr,
1696 _("Unable to query group members of %s"),
1697 argv[0]);
1698 goto done;
1701 if (c->opt_verbose) {
1702 d_printf(
1703 _("Domain Group %s (rid: %d) has %d members\n"),
1704 argv[0],group_rid, rids->count);
1707 /* Check if group is anyone's primary group */
1708 for (i = 0; i < rids->count; i++)
1710 status = dcerpc_samr_OpenUser(b, mem_ctx,
1711 &domain_pol,
1712 MAXIMUM_ALLOWED_ACCESS,
1713 rids->rids[i],
1714 &user_pol,
1715 &result);
1716 if (!NT_STATUS_IS_OK(status)) {
1717 d_fprintf(stderr,
1718 _("Unable to open group member %d\n"),
1719 rids->rids[i]);
1720 goto done;
1723 if (!NT_STATUS_IS_OK(result)) {
1724 status = result;
1725 d_fprintf(stderr,
1726 _("Unable to open group member %d\n"),
1727 rids->rids[i]);
1728 goto done;
1731 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1732 &user_pol,
1734 &info,
1735 &result);
1736 if (!NT_STATUS_IS_OK(status)) {
1737 d_fprintf(stderr,
1738 _("Unable to lookup userinfo for group "
1739 "member %d\n"),
1740 rids->rids[i]);
1741 goto done;
1744 if (!NT_STATUS_IS_OK(result)) {
1745 status = result;
1746 d_fprintf(stderr,
1747 _("Unable to lookup userinfo for group "
1748 "member %d\n"),
1749 rids->rids[i]);
1750 goto done;
1753 if (info->info21.primary_gid == group_rid) {
1754 if (c->opt_verbose) {
1755 d_printf(_("Group is primary group "
1756 "of %s\n"),
1757 info->info21.account_name.string);
1759 group_is_primary = true;
1762 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1765 if (group_is_primary) {
1766 d_fprintf(stderr, _("Unable to delete group because "
1767 "some of it's members have it as primary "
1768 "group\n"));
1769 status = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1770 goto done;
1773 /* remove all group members */
1774 for (i = 0; i < rids->count; i++)
1776 if (c->opt_verbose)
1777 d_printf(_("Remove group member %d..."),
1778 rids->rids[i]);
1779 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
1780 &group_pol,
1781 rids->rids[i],
1782 &result);
1783 if (!NT_STATUS_IS_OK(status)) {
1784 goto done;
1786 status = result;
1787 if (NT_STATUS_IS_OK(result)) {
1788 if (c->opt_verbose)
1789 d_printf(_("ok\n"));
1790 } else {
1791 if (c->opt_verbose)
1792 d_printf("%s\n", _("failed"));
1793 goto done;
1797 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
1798 &group_pol,
1799 &result);
1800 if (!NT_STATUS_IS_OK(status)) {
1801 break;
1804 status = result;
1806 break;
1807 /* removing a local group is easier... */
1808 case SID_NAME_ALIAS:
1809 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1810 &domain_pol,
1811 MAXIMUM_ALLOWED_ACCESS,
1812 group_rids.ids[0],
1813 &group_pol,
1814 &result);
1815 if (!NT_STATUS_IS_OK(status)) {
1816 d_fprintf(stderr, _("Request open_alias failed\n"));
1817 goto done;
1819 if (!NT_STATUS_IS_OK(result)) {
1820 status = result;
1821 d_fprintf(stderr, _("Request open_alias failed\n"));
1822 goto done;
1825 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1826 &group_pol,
1827 &result);
1828 if (!NT_STATUS_IS_OK(status)) {
1829 break;
1832 status = result;
1834 break;
1835 default:
1836 d_fprintf(stderr, _("%s is of type %s. This command is only "
1837 "for deleting local or global groups\n"),
1838 argv[0],sid_type_lookup(name_types.ids[0]));
1839 status = NT_STATUS_UNSUCCESSFUL;
1840 goto done;
1843 if (NT_STATUS_IS_OK(status)) {
1844 if (c->opt_verbose)
1845 d_printf(_("Deleted %s '%s'\n"),
1846 sid_type_lookup(name_types.ids[0]), argv[0]);
1847 } else {
1848 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1849 get_friendly_nt_error_msg(status));
1852 done:
1853 return status;
1857 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1859 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1860 rpc_group_delete_internals, argc,argv);
1863 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1865 NET_API_STATUS status;
1866 struct GROUP_INFO_1 info1;
1867 uint32_t parm_error = 0;
1869 if (argc != 1 || c->display_usage) {
1870 rpc_group_usage(c, argc, argv);
1871 return 0;
1874 ZERO_STRUCT(info1);
1876 info1.grpi1_name = argv[0];
1877 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1878 info1.grpi1_comment = c->opt_comment;
1881 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1883 if (status != 0) {
1884 d_fprintf(stderr,
1885 _("Failed to add group '%s' with error: %s.\n"),
1886 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1887 status));
1888 return -1;
1889 } else {
1890 d_printf(_("Added group '%s'.\n"), argv[0]);
1893 return 0;
1896 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1898 NET_API_STATUS status;
1899 struct LOCALGROUP_INFO_1 info1;
1900 uint32_t parm_error = 0;
1902 if (argc != 1 || c->display_usage) {
1903 rpc_group_usage(c, argc, argv);
1904 return 0;
1907 ZERO_STRUCT(info1);
1909 info1.lgrpi1_name = argv[0];
1910 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1911 info1.lgrpi1_comment = c->opt_comment;
1914 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1916 if (status != 0) {
1917 d_fprintf(stderr,
1918 _("Failed to add alias '%s' with error: %s.\n"),
1919 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1920 status));
1921 return -1;
1922 } else {
1923 d_printf(_("Added alias '%s'.\n"), argv[0]);
1926 return 0;
1929 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1931 if (c->opt_localgroup)
1932 return rpc_alias_add_internals(c, argc, argv);
1934 return rpc_group_add_internals(c, argc, argv);
1937 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1938 TALLOC_CTX *mem_ctx,
1939 const char *name,
1940 struct dom_sid *sid,
1941 enum lsa_SidType *type)
1943 struct dom_sid *sids = NULL;
1944 enum lsa_SidType *types = NULL;
1945 struct rpc_pipe_client *pipe_hnd = NULL;
1946 struct policy_handle lsa_pol;
1947 NTSTATUS status, result;
1948 struct dcerpc_binding_handle *b;
1950 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1951 &pipe_hnd);
1952 if (!NT_STATUS_IS_OK(status)) {
1953 goto done;
1956 b = pipe_hnd->binding_handle;
1958 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1959 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1961 if (!NT_STATUS_IS_OK(status)) {
1962 goto done;
1965 status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1966 &name, NULL, 1, &sids, &types);
1968 if (NT_STATUS_IS_OK(status)) {
1969 sid_copy(sid, &sids[0]);
1970 *type = types[0];
1973 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
1975 done:
1976 if (pipe_hnd) {
1977 TALLOC_FREE(pipe_hnd);
1980 if (!NT_STATUS_IS_OK(status) && (strncasecmp_m(name, "S-", 2) == 0)) {
1982 /* Try as S-1-5-whatever */
1984 struct dom_sid tmp_sid;
1986 if (string_to_sid(&tmp_sid, name)) {
1987 sid_copy(sid, &tmp_sid);
1988 *type = SID_NAME_UNKNOWN;
1989 status = NT_STATUS_OK;
1993 return status;
1996 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1997 TALLOC_CTX *mem_ctx,
1998 const struct dom_sid *group_sid,
1999 const char *member)
2001 struct policy_handle connect_pol, domain_pol;
2002 NTSTATUS status, result;
2003 uint32 group_rid;
2004 struct policy_handle group_pol;
2005 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2007 struct samr_Ids rids, rid_types;
2008 struct lsa_String lsa_acct_name;
2010 struct dom_sid sid;
2012 sid_copy(&sid, group_sid);
2014 if (!sid_split_rid(&sid, &group_rid)) {
2015 return NT_STATUS_UNSUCCESSFUL;
2018 /* Get sam policy handle */
2019 status = dcerpc_samr_Connect2(b, mem_ctx,
2020 pipe_hnd->desthost,
2021 MAXIMUM_ALLOWED_ACCESS,
2022 &connect_pol,
2023 &result);
2024 if (!NT_STATUS_IS_OK(status)) {
2025 return status;
2027 if (!NT_STATUS_IS_OK(result)) {
2028 return result;
2031 /* Get domain policy handle */
2032 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2033 &connect_pol,
2034 MAXIMUM_ALLOWED_ACCESS,
2035 &sid,
2036 &domain_pol,
2037 &result);
2038 if (!NT_STATUS_IS_OK(status)) {
2039 return status;
2041 if (!NT_STATUS_IS_OK(result)) {
2042 return result;
2045 init_lsa_String(&lsa_acct_name, member);
2047 status = dcerpc_samr_LookupNames(b, mem_ctx,
2048 &domain_pol,
2050 &lsa_acct_name,
2051 &rids,
2052 &rid_types,
2053 &result);
2054 if (!NT_STATUS_IS_OK(status)) {
2055 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2056 member);
2057 goto done;
2060 if (!NT_STATUS_IS_OK(result)) {
2061 status = result;
2062 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2063 member);
2064 goto done;
2067 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2068 &domain_pol,
2069 MAXIMUM_ALLOWED_ACCESS,
2070 group_rid,
2071 &group_pol,
2072 &result);
2073 if (!NT_STATUS_IS_OK(status)) {
2074 goto done;
2077 if (!NT_STATUS_IS_OK(result)) {
2078 status = result;
2079 goto done;
2082 status = dcerpc_samr_AddGroupMember(b, mem_ctx,
2083 &group_pol,
2084 rids.ids[0],
2085 0x0005, /* unknown flags */
2086 &result);
2087 if (!NT_STATUS_IS_OK(status)) {
2088 goto done;
2091 status = result;
2093 done:
2094 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2095 return status;
2098 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2099 TALLOC_CTX *mem_ctx,
2100 const struct dom_sid *alias_sid,
2101 const char *member)
2103 struct policy_handle connect_pol, domain_pol;
2104 NTSTATUS status, result;
2105 uint32 alias_rid;
2106 struct policy_handle alias_pol;
2107 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2109 struct dom_sid member_sid;
2110 enum lsa_SidType member_type;
2112 struct dom_sid sid;
2114 sid_copy(&sid, alias_sid);
2116 if (!sid_split_rid(&sid, &alias_rid)) {
2117 return NT_STATUS_UNSUCCESSFUL;
2120 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2121 member, &member_sid, &member_type);
2123 if (!NT_STATUS_IS_OK(result)) {
2124 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2125 member);
2126 return result;
2129 /* Get sam policy handle */
2130 status = dcerpc_samr_Connect2(b, mem_ctx,
2131 pipe_hnd->desthost,
2132 MAXIMUM_ALLOWED_ACCESS,
2133 &connect_pol,
2134 &result);
2135 if (!NT_STATUS_IS_OK(status)) {
2136 goto done;
2138 if (!NT_STATUS_IS_OK(result)) {
2139 status = result;
2140 goto done;
2143 /* Get domain policy handle */
2144 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2145 &connect_pol,
2146 MAXIMUM_ALLOWED_ACCESS,
2147 &sid,
2148 &domain_pol,
2149 &result);
2150 if (!NT_STATUS_IS_OK(status)) {
2151 goto done;
2153 if (!NT_STATUS_IS_OK(result)) {
2154 status = result;
2155 goto done;
2158 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2159 &domain_pol,
2160 MAXIMUM_ALLOWED_ACCESS,
2161 alias_rid,
2162 &alias_pol,
2163 &result);
2164 if (!NT_STATUS_IS_OK(status)) {
2165 return status;
2167 if (!NT_STATUS_IS_OK(result)) {
2168 return result;
2171 status = dcerpc_samr_AddAliasMember(b, mem_ctx,
2172 &alias_pol,
2173 &member_sid,
2174 &result);
2175 if (!NT_STATUS_IS_OK(status)) {
2176 return status;
2179 status = result;
2181 done:
2182 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2183 return status;
2186 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2187 const struct dom_sid *domain_sid,
2188 const char *domain_name,
2189 struct cli_state *cli,
2190 struct rpc_pipe_client *pipe_hnd,
2191 TALLOC_CTX *mem_ctx,
2192 int argc,
2193 const char **argv)
2195 struct dom_sid group_sid;
2196 enum lsa_SidType group_type;
2198 if (argc != 2 || c->display_usage) {
2199 d_printf("%s\n%s",
2200 _("Usage:"),
2201 _("net rpc group addmem <group> <member>\n"
2202 " Add a member to a group\n"
2203 " group\tGroup to add member to\n"
2204 " member\tMember to add to group\n"));
2205 return NT_STATUS_UNSUCCESSFUL;
2208 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2209 &group_sid, &group_type))) {
2210 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2211 argv[0]);
2212 return NT_STATUS_UNSUCCESSFUL;
2215 if (group_type == SID_NAME_DOM_GRP) {
2216 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2217 &group_sid, argv[1]);
2219 if (!NT_STATUS_IS_OK(result)) {
2220 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2221 argv[1], argv[0], nt_errstr(result));
2223 return result;
2226 if (group_type == SID_NAME_ALIAS) {
2227 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2228 &group_sid, argv[1]);
2230 if (!NT_STATUS_IS_OK(result)) {
2231 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2232 argv[1], argv[0], nt_errstr(result));
2234 return result;
2237 d_fprintf(stderr, _("Can only add members to global or local groups "
2238 "which %s is not\n"), argv[0]);
2240 return NT_STATUS_UNSUCCESSFUL;
2243 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2245 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2246 rpc_group_addmem_internals,
2247 argc, argv);
2250 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2251 struct rpc_pipe_client *pipe_hnd,
2252 TALLOC_CTX *mem_ctx,
2253 const struct dom_sid *group_sid,
2254 const char *member)
2256 struct policy_handle connect_pol, domain_pol;
2257 NTSTATUS status, result;
2258 uint32 group_rid;
2259 struct policy_handle group_pol;
2260 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2262 struct samr_Ids rids, rid_types;
2263 struct lsa_String lsa_acct_name;
2265 struct dom_sid sid;
2267 sid_copy(&sid, group_sid);
2269 if (!sid_split_rid(&sid, &group_rid))
2270 return NT_STATUS_UNSUCCESSFUL;
2272 /* Get sam policy handle */
2273 status = dcerpc_samr_Connect2(b, mem_ctx,
2274 pipe_hnd->desthost,
2275 MAXIMUM_ALLOWED_ACCESS,
2276 &connect_pol,
2277 &result);
2278 if (!NT_STATUS_IS_OK(status)) {
2279 return status;
2281 if (!NT_STATUS_IS_OK(result)) {
2282 return result;
2286 /* Get domain policy handle */
2287 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2288 &connect_pol,
2289 MAXIMUM_ALLOWED_ACCESS,
2290 &sid,
2291 &domain_pol,
2292 &result);
2293 if (!NT_STATUS_IS_OK(status)) {
2294 return status;
2296 if (!NT_STATUS_IS_OK(result)) {
2297 return result;
2300 init_lsa_String(&lsa_acct_name, member);
2302 status = dcerpc_samr_LookupNames(b, mem_ctx,
2303 &domain_pol,
2305 &lsa_acct_name,
2306 &rids,
2307 &rid_types,
2308 &result);
2309 if (!NT_STATUS_IS_OK(status)) {
2310 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2311 member);
2312 goto done;
2315 if (!NT_STATUS_IS_OK(result)) {
2316 status = result;
2317 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2318 member);
2319 goto done;
2322 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2323 &domain_pol,
2324 MAXIMUM_ALLOWED_ACCESS,
2325 group_rid,
2326 &group_pol,
2327 &result);
2328 if (!NT_STATUS_IS_OK(status)) {
2329 goto done;
2331 if (!NT_STATUS_IS_OK(result)) {
2332 status = result;
2333 goto done;
2336 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2337 &group_pol,
2338 rids.ids[0],
2339 &result);
2340 if (!NT_STATUS_IS_OK(status)) {
2341 goto done;
2344 status = result;
2345 done:
2346 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2347 return status;
2350 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2351 TALLOC_CTX *mem_ctx,
2352 const struct dom_sid *alias_sid,
2353 const char *member)
2355 struct policy_handle connect_pol, domain_pol;
2356 NTSTATUS status, result;
2357 uint32 alias_rid;
2358 struct policy_handle alias_pol;
2359 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2361 struct dom_sid member_sid;
2362 enum lsa_SidType member_type;
2364 struct dom_sid sid;
2366 sid_copy(&sid, alias_sid);
2368 if (!sid_split_rid(&sid, &alias_rid))
2369 return NT_STATUS_UNSUCCESSFUL;
2371 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2372 member, &member_sid, &member_type);
2374 if (!NT_STATUS_IS_OK(result)) {
2375 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2376 member);
2377 return result;
2380 /* Get sam policy handle */
2381 status = dcerpc_samr_Connect2(b, mem_ctx,
2382 pipe_hnd->desthost,
2383 MAXIMUM_ALLOWED_ACCESS,
2384 &connect_pol,
2385 &result);
2386 if (!NT_STATUS_IS_OK(status)) {
2387 goto done;
2389 if (!NT_STATUS_IS_OK(result)) {
2390 status = result;
2391 goto done;
2394 /* Get domain policy handle */
2395 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2396 &connect_pol,
2397 MAXIMUM_ALLOWED_ACCESS,
2398 &sid,
2399 &domain_pol,
2400 &result);
2401 if (!NT_STATUS_IS_OK(status)) {
2402 goto done;
2404 if (!NT_STATUS_IS_OK(result)) {
2405 status = result;
2406 goto done;
2409 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2410 &domain_pol,
2411 MAXIMUM_ALLOWED_ACCESS,
2412 alias_rid,
2413 &alias_pol,
2414 &result);
2415 if (!NT_STATUS_IS_OK(status)) {
2416 return status;
2419 if (!NT_STATUS_IS_OK(result)) {
2420 return result;
2423 status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
2424 &alias_pol,
2425 &member_sid,
2426 &result);
2428 if (!NT_STATUS_IS_OK(status)) {
2429 return status;
2432 status = result;
2434 done:
2435 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2436 return status;
2439 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2440 const struct dom_sid *domain_sid,
2441 const char *domain_name,
2442 struct cli_state *cli,
2443 struct rpc_pipe_client *pipe_hnd,
2444 TALLOC_CTX *mem_ctx,
2445 int argc,
2446 const char **argv)
2448 struct dom_sid group_sid;
2449 enum lsa_SidType group_type;
2451 if (argc != 2 || c->display_usage) {
2452 d_printf("%s\n%s",
2453 _("Usage:"),
2454 _("net rpc group delmem <group> <member>\n"
2455 " Delete a member from a group\n"
2456 " group\tGroup to delete member from\n"
2457 " member\tMember to delete from group\n"));
2458 return NT_STATUS_UNSUCCESSFUL;
2461 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2462 &group_sid, &group_type))) {
2463 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2464 argv[0]);
2465 return NT_STATUS_UNSUCCESSFUL;
2468 if (group_type == SID_NAME_DOM_GRP) {
2469 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2470 &group_sid, argv[1]);
2472 if (!NT_STATUS_IS_OK(result)) {
2473 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2474 argv[1], argv[0], nt_errstr(result));
2476 return result;
2479 if (group_type == SID_NAME_ALIAS) {
2480 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2481 &group_sid, argv[1]);
2483 if (!NT_STATUS_IS_OK(result)) {
2484 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2485 argv[1], argv[0], nt_errstr(result));
2487 return result;
2490 d_fprintf(stderr, _("Can only delete members from global or local "
2491 "groups which %s is not\n"), argv[0]);
2493 return NT_STATUS_UNSUCCESSFUL;
2496 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2498 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2499 rpc_group_delmem_internals,
2500 argc, argv);
2504 * List groups on a remote RPC server.
2506 * All parameters are provided by the run_rpc_command function, except for
2507 * argc, argv which are passes through.
2509 * @param domain_sid The domain sid acquired from the remote server.
2510 * @param cli A cli_state connected to the server.
2511 * @param mem_ctx Talloc context, destroyed on completion of the function.
2512 * @param argc Standard main() style argc.
2513 * @param argv Standard main() style argv. Initial components are already
2514 * stripped.
2516 * @return Normal NTSTATUS return.
2519 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2520 const struct dom_sid *domain_sid,
2521 const char *domain_name,
2522 struct cli_state *cli,
2523 struct rpc_pipe_client *pipe_hnd,
2524 TALLOC_CTX *mem_ctx,
2525 int argc,
2526 const char **argv)
2528 struct policy_handle connect_pol, domain_pol;
2529 NTSTATUS status, result;
2530 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2531 struct samr_SamArray *groups = NULL;
2532 bool global = false;
2533 bool local = false;
2534 bool builtin = false;
2535 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2537 if (c->display_usage) {
2538 d_printf("%s\n%s",
2539 _("Usage:"),
2540 _("net rpc group list [global] [local] [builtin]\n"
2541 " List groups on RPC server\n"
2542 " global\tList global groups\n"
2543 " local\tList local groups\n"
2544 " builtin\tList builtin groups\n"
2545 " If none of global, local or builtin is "
2546 "specified, all three options are considered "
2547 "set\n"));
2548 return NT_STATUS_OK;
2551 if (argc == 0) {
2552 global = true;
2553 local = true;
2554 builtin = true;
2557 for (i=0; i<argc; i++) {
2558 if (strequal(argv[i], "global"))
2559 global = true;
2561 if (strequal(argv[i], "local"))
2562 local = true;
2564 if (strequal(argv[i], "builtin"))
2565 builtin = true;
2568 /* Get sam policy handle */
2570 status = dcerpc_samr_Connect2(b, mem_ctx,
2571 pipe_hnd->desthost,
2572 MAXIMUM_ALLOWED_ACCESS,
2573 &connect_pol,
2574 &result);
2575 if (!NT_STATUS_IS_OK(status)) {
2576 goto done;
2578 if (!NT_STATUS_IS_OK(result)) {
2579 status = result;
2580 goto done;
2583 /* Get domain policy handle */
2585 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2586 &connect_pol,
2587 MAXIMUM_ALLOWED_ACCESS,
2588 discard_const_p(struct dom_sid2, domain_sid),
2589 &domain_pol,
2590 &result);
2591 if (!NT_STATUS_IS_OK(status)) {
2592 goto done;
2594 if (!NT_STATUS_IS_OK(result)) {
2595 status = result;
2596 goto done;
2599 /* Query domain groups */
2600 if (c->opt_long_list_entries)
2601 d_printf(_("\nGroup name Comment"
2602 "\n-----------------------------\n"));
2603 do {
2604 uint32_t max_size, total_size, returned_size;
2605 union samr_DispInfo info;
2607 if (!global) break;
2609 dcerpc_get_query_dispinfo_params(
2610 loop_count, &max_entries, &max_size);
2612 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
2613 &domain_pol,
2615 start_idx,
2616 max_entries,
2617 max_size,
2618 &total_size,
2619 &returned_size,
2620 &info,
2621 &result);
2622 if (!NT_STATUS_IS_OK(status)) {
2623 goto done;
2625 num_entries = info.info3.count;
2626 start_idx += info.info3.count;
2628 if (!NT_STATUS_IS_OK(result) &&
2629 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2630 break;
2632 for (i = 0; i < num_entries; i++) {
2634 const char *group = NULL;
2635 const char *desc = NULL;
2637 group = info.info3.entries[i].account_name.string;
2638 desc = info.info3.entries[i].description.string;
2640 if (c->opt_long_list_entries)
2641 printf("%-21.21s %-50.50s\n",
2642 group, desc);
2643 else
2644 printf("%s\n", group);
2646 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2647 /* query domain aliases */
2648 start_idx = 0;
2649 do {
2650 if (!local) break;
2652 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2653 &domain_pol,
2654 &start_idx,
2655 &groups,
2656 0xffff,
2657 &num_entries,
2658 &result);
2659 if (!NT_STATUS_IS_OK(status)) {
2660 goto done;
2662 if (!NT_STATUS_IS_OK(result) &&
2663 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2664 break;
2666 for (i = 0; i < num_entries; i++) {
2668 const char *description = NULL;
2670 if (c->opt_long_list_entries) {
2672 struct policy_handle alias_pol;
2673 union samr_AliasInfo *info = NULL;
2674 NTSTATUS _result;
2676 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2677 &domain_pol,
2678 0x8,
2679 groups->entries[i].idx,
2680 &alias_pol,
2681 &_result);
2682 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2683 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2684 &alias_pol,
2686 &info,
2687 &_result);
2688 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2689 status = dcerpc_samr_Close(b, mem_ctx,
2690 &alias_pol,
2691 &_result);
2692 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2693 description = info->description.string;
2699 if (description != NULL) {
2700 printf("%-21.21s %-50.50s\n",
2701 groups->entries[i].name.string,
2702 description);
2703 } else {
2704 printf("%s\n", groups->entries[i].name.string);
2707 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2708 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2709 /* Get builtin policy handle */
2711 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2712 &connect_pol,
2713 MAXIMUM_ALLOWED_ACCESS,
2714 discard_const_p(struct dom_sid2, &global_sid_Builtin),
2715 &domain_pol,
2716 &result);
2717 if (!NT_STATUS_IS_OK(status)) {
2718 goto done;
2720 if (!NT_STATUS_IS_OK(result)) {
2721 status = result;
2722 goto done;
2725 /* query builtin aliases */
2726 start_idx = 0;
2727 do {
2728 if (!builtin) break;
2730 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2731 &domain_pol,
2732 &start_idx,
2733 &groups,
2734 max_entries,
2735 &num_entries,
2736 &result);
2737 if (!NT_STATUS_IS_OK(status)) {
2738 break;
2740 if (!NT_STATUS_IS_OK(result) &&
2741 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
2742 status = result;
2743 break;
2746 for (i = 0; i < num_entries; i++) {
2748 const char *description = NULL;
2750 if (c->opt_long_list_entries) {
2752 struct policy_handle alias_pol;
2753 union samr_AliasInfo *info = NULL;
2754 NTSTATUS _result;
2756 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2757 &domain_pol,
2758 0x8,
2759 groups->entries[i].idx,
2760 &alias_pol,
2761 &_result);
2762 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2763 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2764 &alias_pol,
2766 &info,
2767 &_result);
2768 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2769 status = dcerpc_samr_Close(b, mem_ctx,
2770 &alias_pol,
2771 &_result);
2772 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2773 description = info->description.string;
2779 if (description != NULL) {
2780 printf("%-21.21s %-50.50s\n",
2781 groups->entries[i].name.string,
2782 description);
2783 } else {
2784 printf("%s\n", groups->entries[i].name.string);
2787 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2789 status = result;
2791 done:
2792 return status;
2795 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2797 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2798 rpc_group_list_internals,
2799 argc, argv);
2802 static NTSTATUS rpc_list_group_members(struct net_context *c,
2803 struct rpc_pipe_client *pipe_hnd,
2804 TALLOC_CTX *mem_ctx,
2805 const char *domain_name,
2806 const struct dom_sid *domain_sid,
2807 struct policy_handle *domain_pol,
2808 uint32 rid)
2810 NTSTATUS result, status;
2811 struct policy_handle group_pol;
2812 uint32 num_members, *group_rids;
2813 int i;
2814 struct samr_RidAttrArray *rids = NULL;
2815 struct lsa_Strings names;
2816 struct samr_Ids types;
2817 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2819 fstring sid_str;
2820 sid_to_fstring(sid_str, domain_sid);
2822 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2823 domain_pol,
2824 MAXIMUM_ALLOWED_ACCESS,
2825 rid,
2826 &group_pol,
2827 &result);
2828 if (!NT_STATUS_IS_OK(status)) {
2829 return status;
2831 if (!NT_STATUS_IS_OK(result)) {
2832 return result;
2835 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
2836 &group_pol,
2837 &rids,
2838 &result);
2839 if (!NT_STATUS_IS_OK(status)) {
2840 return status;
2842 if (!NT_STATUS_IS_OK(result)) {
2843 return result;
2846 num_members = rids->count;
2847 group_rids = rids->rids;
2849 while (num_members > 0) {
2850 int this_time = 512;
2852 if (num_members < this_time)
2853 this_time = num_members;
2855 status = dcerpc_samr_LookupRids(b, mem_ctx,
2856 domain_pol,
2857 this_time,
2858 group_rids,
2859 &names,
2860 &types,
2861 &result);
2862 if (!NT_STATUS_IS_OK(status)) {
2863 return status;
2865 if (!NT_STATUS_IS_OK(result)) {
2866 return result;
2869 /* We only have users as members, but make the output
2870 the same as the output of alias members */
2872 for (i = 0; i < this_time; i++) {
2874 if (c->opt_long_list_entries) {
2875 printf("%s-%d %s\\%s %d\n", sid_str,
2876 group_rids[i], domain_name,
2877 names.names[i].string,
2878 SID_NAME_USER);
2879 } else {
2880 printf("%s\\%s\n", domain_name,
2881 names.names[i].string);
2885 num_members -= this_time;
2886 group_rids += 512;
2889 return NT_STATUS_OK;
2892 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2893 struct rpc_pipe_client *pipe_hnd,
2894 TALLOC_CTX *mem_ctx,
2895 struct policy_handle *domain_pol,
2896 uint32 rid)
2898 NTSTATUS result, status;
2899 struct rpc_pipe_client *lsa_pipe;
2900 struct policy_handle alias_pol, lsa_pol;
2901 uint32 num_members;
2902 struct dom_sid *alias_sids;
2903 char **domains;
2904 char **names;
2905 enum lsa_SidType *types;
2906 int i;
2907 struct lsa_SidArray sid_array;
2908 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2910 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2911 domain_pol,
2912 MAXIMUM_ALLOWED_ACCESS,
2913 rid,
2914 &alias_pol,
2915 &result);
2916 if (!NT_STATUS_IS_OK(status)) {
2917 return status;
2919 if (!NT_STATUS_IS_OK(result)) {
2920 return result;
2923 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
2924 &alias_pol,
2925 &sid_array,
2926 &result);
2927 if (!NT_STATUS_IS_OK(status)) {
2928 d_fprintf(stderr, _("Couldn't list alias members\n"));
2929 return status;
2931 if (!NT_STATUS_IS_OK(result)) {
2932 d_fprintf(stderr, _("Couldn't list alias members\n"));
2933 return result;
2936 num_members = sid_array.num_sids;
2938 if (num_members == 0) {
2939 return NT_STATUS_OK;
2942 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2943 &ndr_table_lsarpc.syntax_id,
2944 &lsa_pipe);
2945 if (!NT_STATUS_IS_OK(result)) {
2946 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2947 nt_errstr(result) );
2948 return result;
2951 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2952 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2954 if (!NT_STATUS_IS_OK(result)) {
2955 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2956 TALLOC_FREE(lsa_pipe);
2957 return result;
2960 alias_sids = talloc_zero_array(mem_ctx, struct dom_sid, num_members);
2961 if (!alias_sids) {
2962 d_fprintf(stderr, _("Out of memory\n"));
2963 TALLOC_FREE(lsa_pipe);
2964 return NT_STATUS_NO_MEMORY;
2967 for (i=0; i<num_members; i++) {
2968 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2971 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2972 num_members, alias_sids,
2973 &domains, &names, &types);
2975 if (!NT_STATUS_IS_OK(result) &&
2976 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2977 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2978 TALLOC_FREE(lsa_pipe);
2979 return result;
2982 for (i = 0; i < num_members; i++) {
2983 fstring sid_str;
2984 sid_to_fstring(sid_str, &alias_sids[i]);
2986 if (c->opt_long_list_entries) {
2987 printf("%s %s\\%s %d\n", sid_str,
2988 domains[i] ? domains[i] : _("*unknown*"),
2989 names[i] ? names[i] : _("*unknown*"), types[i]);
2990 } else {
2991 if (domains[i])
2992 printf("%s\\%s\n", domains[i], names[i]);
2993 else
2994 printf("%s\n", sid_str);
2998 TALLOC_FREE(lsa_pipe);
2999 return NT_STATUS_OK;
3002 static NTSTATUS rpc_group_members_internals(struct net_context *c,
3003 const struct dom_sid *domain_sid,
3004 const char *domain_name,
3005 struct cli_state *cli,
3006 struct rpc_pipe_client *pipe_hnd,
3007 TALLOC_CTX *mem_ctx,
3008 int argc,
3009 const char **argv)
3011 NTSTATUS result, status;
3012 struct policy_handle connect_pol, domain_pol;
3013 struct samr_Ids rids, rid_types;
3014 struct lsa_String lsa_acct_name;
3015 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3017 /* Get sam policy handle */
3019 status = dcerpc_samr_Connect2(b, mem_ctx,
3020 pipe_hnd->desthost,
3021 MAXIMUM_ALLOWED_ACCESS,
3022 &connect_pol,
3023 &result);
3024 if (!NT_STATUS_IS_OK(status)) {
3025 return status;
3027 if (!NT_STATUS_IS_OK(result)) {
3028 return result;
3031 /* Get domain policy handle */
3033 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3034 &connect_pol,
3035 MAXIMUM_ALLOWED_ACCESS,
3036 discard_const_p(struct dom_sid2, domain_sid),
3037 &domain_pol,
3038 &result);
3039 if (!NT_STATUS_IS_OK(status)) {
3040 return status;
3042 if (!NT_STATUS_IS_OK(result)) {
3043 return result;
3046 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3048 status = dcerpc_samr_LookupNames(b, mem_ctx,
3049 &domain_pol,
3051 &lsa_acct_name,
3052 &rids,
3053 &rid_types,
3054 &result);
3055 if (!NT_STATUS_IS_OK(status)) {
3056 return status;
3059 if (!NT_STATUS_IS_OK(result)) {
3061 /* Ok, did not find it in the global sam, try with builtin */
3063 struct dom_sid sid_Builtin;
3065 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3067 sid_copy(&sid_Builtin, &global_sid_Builtin);
3069 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3070 &connect_pol,
3071 MAXIMUM_ALLOWED_ACCESS,
3072 &sid_Builtin,
3073 &domain_pol,
3074 &result);
3075 if (!NT_STATUS_IS_OK(status)) {
3076 return status;
3078 if (!NT_STATUS_IS_OK(result)) {
3079 d_fprintf(stderr, _("Couldn't find group %s\n"),
3080 argv[0]);
3081 return result;
3084 status = dcerpc_samr_LookupNames(b, mem_ctx,
3085 &domain_pol,
3087 &lsa_acct_name,
3088 &rids,
3089 &rid_types,
3090 &result);
3091 if (!NT_STATUS_IS_OK(status)) {
3092 return status;
3094 if (!NT_STATUS_IS_OK(result)) {
3095 d_fprintf(stderr, _("Couldn't find group %s\n"),
3096 argv[0]);
3097 return result;
3101 if (rids.count != 1) {
3102 d_fprintf(stderr, _("Couldn't find group %s\n"),
3103 argv[0]);
3104 return result;
3107 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3108 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
3109 domain_sid, &domain_pol,
3110 rids.ids[0]);
3113 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3114 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
3115 rids.ids[0]);
3118 return NT_STATUS_NO_SUCH_GROUP;
3121 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
3123 if (argc != 1 || c->display_usage) {
3124 return rpc_group_usage(c, argc, argv);
3127 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
3128 rpc_group_members_internals,
3129 argc, argv);
3132 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
3134 NET_API_STATUS status;
3135 struct GROUP_INFO_0 g0;
3136 uint32_t parm_err;
3138 if (argc != 2) {
3139 d_printf(_("Usage:\n"));
3140 d_printf("net rpc group rename group newname\n");
3141 return -1;
3144 g0.grpi0_name = argv[1];
3146 status = NetGroupSetInfo(c->opt_host,
3147 argv[0],
3149 (uint8_t *)&g0,
3150 &parm_err);
3152 if (status != 0) {
3153 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
3154 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3155 status));
3156 return -1;
3159 return 0;
3162 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3164 if (argc != 2 || c->display_usage) {
3165 return rpc_group_usage(c, argc, argv);
3168 return rpc_group_rename_internals(c, argc, argv);
3172 * 'net rpc group' entrypoint.
3173 * @param argc Standard main() style argc.
3174 * @param argv Standard main() style argv. Initial components are already
3175 * stripped.
3178 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3180 NET_API_STATUS status;
3182 struct functable func[] = {
3184 "add",
3185 rpc_group_add,
3186 NET_TRANSPORT_RPC,
3187 N_("Create specified group"),
3188 N_("net rpc group add\n"
3189 " Create specified group")
3192 "delete",
3193 rpc_group_delete,
3194 NET_TRANSPORT_RPC,
3195 N_("Delete specified group"),
3196 N_("net rpc group delete\n"
3197 " Delete specified group")
3200 "addmem",
3201 rpc_group_addmem,
3202 NET_TRANSPORT_RPC,
3203 N_("Add member to group"),
3204 N_("net rpc group addmem\n"
3205 " Add member to group")
3208 "delmem",
3209 rpc_group_delmem,
3210 NET_TRANSPORT_RPC,
3211 N_("Remove member from group"),
3212 N_("net rpc group delmem\n"
3213 " Remove member from group")
3216 "list",
3217 rpc_group_list,
3218 NET_TRANSPORT_RPC,
3219 N_("List groups"),
3220 N_("net rpc group list\n"
3221 " List groups")
3224 "members",
3225 rpc_group_members,
3226 NET_TRANSPORT_RPC,
3227 N_("List group members"),
3228 N_("net rpc group members\n"
3229 " List group members")
3232 "rename",
3233 rpc_group_rename,
3234 NET_TRANSPORT_RPC,
3235 N_("Rename group"),
3236 N_("net rpc group rename\n"
3237 " Rename group")
3239 {NULL, NULL, 0, NULL, NULL}
3242 status = libnetapi_net_init(&c->netapi_ctx);
3243 if (status != 0) {
3244 return -1;
3246 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3247 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3248 if (c->opt_kerberos) {
3249 libnetapi_set_use_kerberos(c->netapi_ctx);
3252 if (argc == 0) {
3253 if (c->display_usage) {
3254 d_printf(_("Usage:\n"));
3255 d_printf(_("net rpc group\n"
3256 " Alias for net rpc group list global "
3257 "local builtin\n"));
3258 net_display_usage_from_functable(func);
3259 return 0;
3262 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
3263 rpc_group_list_internals,
3264 argc, argv);
3267 return net_run_function(c, argc, argv, "net rpc group", func);
3270 /****************************************************************************/
3272 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3274 return net_share_usage(c, argc, argv);
3278 * Add a share on a remote RPC server.
3280 * @param argc Standard main() style argc.
3281 * @param argv Standard main() style argv. Initial components are already
3282 * stripped.
3284 * @return A shell status integer (0 for success).
3287 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3289 NET_API_STATUS status;
3290 char *sharename;
3291 char *path;
3292 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3293 uint32 num_users=0, perms=0;
3294 char *password=NULL; /* don't allow a share password */
3295 struct SHARE_INFO_2 i2;
3296 uint32_t parm_error = 0;
3298 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3299 return rpc_share_usage(c, argc, argv);
3302 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3303 return -1;
3306 path = strchr(sharename, '=');
3307 if (!path) {
3308 return -1;
3311 *path++ = '\0';
3313 i2.shi2_netname = sharename;
3314 i2.shi2_type = type;
3315 i2.shi2_remark = c->opt_comment;
3316 i2.shi2_permissions = perms;
3317 i2.shi2_max_uses = c->opt_maxusers;
3318 i2.shi2_current_uses = num_users;
3319 i2.shi2_path = path;
3320 i2.shi2_passwd = password;
3322 status = NetShareAdd(c->opt_host,
3324 (uint8_t *)&i2,
3325 &parm_error);
3326 if (status != 0) {
3327 printf(_("NetShareAdd failed with: %s\n"),
3328 libnetapi_get_error_string(c->netapi_ctx, status));
3331 return status;
3335 * Delete a share on a remote RPC server.
3337 * @param domain_sid The domain sid acquired from the remote server.
3338 * @param argc Standard main() style argc.
3339 * @param argv Standard main() style argv. Initial components are already
3340 * stripped.
3342 * @return A shell status integer (0 for success).
3344 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3346 if (argc < 1 || c->display_usage) {
3347 return rpc_share_usage(c, argc, argv);
3350 return NetShareDel(c->opt_host, argv[0], 0);
3354 * Formatted print of share info
3356 * @param r pointer to SHARE_INFO_1 to format
3359 static void display_share_info_1(struct net_context *c,
3360 struct SHARE_INFO_1 *r)
3362 if (c->opt_long_list_entries) {
3363 d_printf("%-12s %-8.8s %-50s\n",
3364 r->shi1_netname,
3365 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3366 r->shi1_remark);
3367 } else {
3368 d_printf("%s\n", r->shi1_netname);
3372 static WERROR get_share_info(struct net_context *c,
3373 struct rpc_pipe_client *pipe_hnd,
3374 TALLOC_CTX *mem_ctx,
3375 uint32 level,
3376 int argc,
3377 const char **argv,
3378 struct srvsvc_NetShareInfoCtr *info_ctr)
3380 WERROR result;
3381 NTSTATUS status;
3382 union srvsvc_NetShareInfo info;
3383 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3385 /* no specific share requested, enumerate all */
3386 if (argc == 0) {
3388 uint32_t preferred_len = 0xffffffff;
3389 uint32_t total_entries = 0;
3390 uint32_t resume_handle = 0;
3392 info_ctr->level = level;
3394 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3395 pipe_hnd->desthost,
3396 info_ctr,
3397 preferred_len,
3398 &total_entries,
3399 &resume_handle,
3400 &result);
3401 if (!NT_STATUS_IS_OK(status)) {
3402 return ntstatus_to_werror(status);
3404 return result;
3407 /* request just one share */
3408 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3409 pipe_hnd->desthost,
3410 argv[0],
3411 level,
3412 &info,
3413 &result);
3415 if (!NT_STATUS_IS_OK(status)) {
3416 result = ntstatus_to_werror(status);
3417 goto done;
3420 if (!W_ERROR_IS_OK(result)) {
3421 goto done;
3424 /* construct ctr */
3425 ZERO_STRUCTP(info_ctr);
3427 info_ctr->level = level;
3429 switch (level) {
3430 case 1:
3432 struct srvsvc_NetShareCtr1 *ctr1;
3434 ctr1 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr1);
3435 W_ERROR_HAVE_NO_MEMORY(ctr1);
3437 ctr1->count = 1;
3438 ctr1->array = info.info1;
3440 info_ctr->ctr.ctr1 = ctr1;
3442 break;
3444 case 2:
3446 struct srvsvc_NetShareCtr2 *ctr2;
3448 ctr2 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr2);
3449 W_ERROR_HAVE_NO_MEMORY(ctr2);
3451 ctr2->count = 1;
3452 ctr2->array = info.info2;
3454 info_ctr->ctr.ctr2 = ctr2;
3456 break;
3458 case 502:
3460 struct srvsvc_NetShareCtr502 *ctr502;
3462 ctr502 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr502);
3463 W_ERROR_HAVE_NO_MEMORY(ctr502);
3465 ctr502->count = 1;
3466 ctr502->array = info.info502;
3468 info_ctr->ctr.ctr502 = ctr502;
3470 break;
3472 } /* switch */
3473 done:
3474 return result;
3477 /***
3478 * 'net rpc share list' entrypoint.
3479 * @param argc Standard main() style argc.
3480 * @param argv Standard main() style argv. Initial components are already
3481 * stripped.
3483 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3485 NET_API_STATUS status;
3486 struct SHARE_INFO_1 *i1 = NULL;
3487 uint32_t entries_read = 0;
3488 uint32_t total_entries = 0;
3489 uint32_t resume_handle = 0;
3490 uint32_t i, level = 1;
3492 if (c->display_usage) {
3493 d_printf( "%s\n"
3494 "net rpc share list\n"
3495 " %s\n",
3496 _("Usage:"),
3497 _("List shares on remote server"));
3498 return 0;
3501 status = NetShareEnum(c->opt_host,
3502 level,
3503 (uint8_t **)(void *)&i1,
3504 (uint32_t)-1,
3505 &entries_read,
3506 &total_entries,
3507 &resume_handle);
3508 if (status != 0) {
3509 goto done;
3512 /* Display results */
3514 if (c->opt_long_list_entries) {
3515 d_printf(_(
3516 "\nEnumerating shared resources (exports) on remote server:\n\n"
3517 "\nShare name Type Description\n"
3518 "---------- ---- -----------\n"));
3520 for (i = 0; i < entries_read; i++)
3521 display_share_info_1(c, &i1[i]);
3522 done:
3523 return status;
3526 static bool check_share_availability(struct cli_state *cli, const char *netname)
3528 NTSTATUS status;
3530 status = cli_tcon_andx(cli, netname, "A:", "", 0);
3531 if (!NT_STATUS_IS_OK(status)) {
3532 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3533 return false;
3536 status = cli_tdis(cli);
3537 if (!NT_STATUS_IS_OK(status)) {
3538 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3539 return false;
3542 return true;
3545 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3546 const char *netname, uint32 type)
3548 /* only support disk shares */
3549 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3550 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3551 type);
3552 return false;
3555 /* skip builtin shares */
3556 /* FIXME: should print$ be added too ? */
3557 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3558 strequal(netname,"global"))
3559 return false;
3561 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3562 printf(_("excluding [%s]\n"), netname);
3563 return false;
3566 return check_share_availability(cli, netname);
3570 * Migrate shares from a remote RPC server to the local RPC server.
3572 * All parameters are provided by the run_rpc_command function, except for
3573 * argc, argv which are passed through.
3575 * @param domain_sid The domain sid acquired from the remote server.
3576 * @param cli A cli_state connected to the server.
3577 * @param mem_ctx Talloc context, destroyed on completion of the function.
3578 * @param argc Standard main() style argc.
3579 * @param argv Standard main() style argv. Initial components are already
3580 * stripped.
3582 * @return Normal NTSTATUS return.
3585 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3586 const struct dom_sid *domain_sid,
3587 const char *domain_name,
3588 struct cli_state *cli,
3589 struct rpc_pipe_client *pipe_hnd,
3590 TALLOC_CTX *mem_ctx,
3591 int argc,
3592 const char **argv)
3594 WERROR result;
3595 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3596 struct srvsvc_NetShareInfoCtr ctr_src;
3597 uint32 i;
3598 struct rpc_pipe_client *srvsvc_pipe = NULL;
3599 struct cli_state *cli_dst = NULL;
3600 uint32 level = 502; /* includes secdesc */
3601 uint32_t parm_error = 0;
3602 struct dcerpc_binding_handle *b;
3604 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3605 &ctr_src);
3606 if (!W_ERROR_IS_OK(result))
3607 goto done;
3609 /* connect destination PI_SRVSVC */
3610 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3611 &ndr_table_srvsvc.syntax_id);
3612 if (!NT_STATUS_IS_OK(nt_status))
3613 return nt_status;
3615 b = srvsvc_pipe->binding_handle;
3617 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3619 union srvsvc_NetShareInfo info;
3620 struct srvsvc_NetShareInfo502 info502 =
3621 ctr_src.ctr.ctr502->array[i];
3623 /* reset error-code */
3624 nt_status = NT_STATUS_UNSUCCESSFUL;
3626 if (!check_share_sanity(c, cli, info502.name, info502.type))
3627 continue;
3629 /* finally add the share on the dst server */
3631 printf(_("migrating: [%s], path: %s, comment: %s, without "
3632 "share-ACLs\n"),
3633 info502.name, info502.path, info502.comment);
3635 info.info502 = &info502;
3637 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3638 srvsvc_pipe->desthost,
3639 502,
3640 &info,
3641 &parm_error,
3642 &result);
3643 if (!NT_STATUS_IS_OK(nt_status)) {
3644 printf(_("cannot add share: %s\n"),
3645 nt_errstr(nt_status));
3646 goto done;
3648 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3649 printf(_(" [%s] does already exist\n"),
3650 info502.name);
3651 continue;
3654 if (!W_ERROR_IS_OK(result)) {
3655 nt_status = werror_to_ntstatus(result);
3656 printf(_("cannot add share: %s\n"),
3657 win_errstr(result));
3658 goto done;
3663 nt_status = NT_STATUS_OK;
3665 done:
3666 if (cli_dst) {
3667 cli_shutdown(cli_dst);
3670 return nt_status;
3675 * Migrate shares from a RPC server to another.
3677 * @param argc Standard main() style argc.
3678 * @param argv Standard main() style argv. Initial components are already
3679 * stripped.
3681 * @return A shell status integer (0 for success).
3683 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3684 const char **argv)
3686 if (c->display_usage) {
3687 d_printf( "%s\n"
3688 "net rpc share migrate shares\n"
3689 " %s\n",
3690 _("Usage:"),
3691 _("Migrate shares to local server"));
3692 return 0;
3695 if (!c->opt_host) {
3696 printf(_("no server to migrate\n"));
3697 return -1;
3700 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3701 rpc_share_migrate_shares_internals,
3702 argc, argv);
3706 * Copy a file/dir
3708 * @param f file_info
3709 * @param mask current search mask
3710 * @param state arg-pointer
3713 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3714 const char *mask, void *state)
3716 static NTSTATUS nt_status;
3717 static struct copy_clistate *local_state;
3718 static fstring filename, new_mask;
3719 fstring dir;
3720 char *old_dir;
3721 struct net_context *c;
3723 local_state = (struct copy_clistate *)state;
3724 nt_status = NT_STATUS_UNSUCCESSFUL;
3726 c = local_state->c;
3728 if (strequal(f->name, ".") || strequal(f->name, ".."))
3729 return NT_STATUS_OK;
3731 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3733 /* DIRECTORY */
3734 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
3736 DEBUG(3,("got dir: %s\n", f->name));
3738 fstrcpy(dir, local_state->cwd);
3739 fstrcat(dir, "\\");
3740 fstrcat(dir, f->name);
3742 switch (net_mode_share)
3744 case NET_MODE_SHARE_MIGRATE:
3745 /* create that directory */
3746 nt_status = net_copy_file(c, local_state->mem_ctx,
3747 local_state->cli_share_src,
3748 local_state->cli_share_dst,
3749 dir, dir,
3750 c->opt_acls? true : false,
3751 c->opt_attrs? true : false,
3752 c->opt_timestamps? true:false,
3753 false);
3754 break;
3755 default:
3756 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3757 return NT_STATUS_INTERNAL_ERROR;
3760 if (!NT_STATUS_IS_OK(nt_status)) {
3761 printf(_("could not handle dir %s: %s\n"),
3762 dir, nt_errstr(nt_status));
3763 return nt_status;
3766 /* search below that directory */
3767 strlcpy(new_mask, dir, sizeof(new_mask));
3768 strlcat(new_mask, "\\*", sizeof(new_mask));
3770 old_dir = local_state->cwd;
3771 local_state->cwd = dir;
3772 nt_status = sync_files(local_state, new_mask);
3773 if (!NT_STATUS_IS_OK(nt_status)) {
3774 printf(_("could not handle files\n"));
3776 local_state->cwd = old_dir;
3778 return nt_status;
3782 /* FILE */
3783 fstrcpy(filename, local_state->cwd);
3784 fstrcat(filename, "\\");
3785 fstrcat(filename, f->name);
3787 DEBUG(3,("got file: %s\n", filename));
3789 switch (net_mode_share)
3791 case NET_MODE_SHARE_MIGRATE:
3792 nt_status = net_copy_file(c, local_state->mem_ctx,
3793 local_state->cli_share_src,
3794 local_state->cli_share_dst,
3795 filename, filename,
3796 c->opt_acls? true : false,
3797 c->opt_attrs? true : false,
3798 c->opt_timestamps? true: false,
3799 true);
3800 break;
3801 default:
3802 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3803 net_mode_share);
3804 return NT_STATUS_INTERNAL_ERROR;
3807 if (!NT_STATUS_IS_OK(nt_status))
3808 printf(_("could not handle file %s: %s\n"),
3809 filename, nt_errstr(nt_status));
3810 return nt_status;
3814 * sync files, can be called recursivly to list files
3815 * and then call copy_fn for each file
3817 * @param cp_clistate pointer to the copy_clistate we work with
3818 * @param mask the current search mask
3820 * @return Boolean result
3822 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
3824 struct cli_state *targetcli;
3825 char *targetpath = NULL;
3826 NTSTATUS status;
3828 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3830 status = cli_resolve_path(talloc_tos(), "", NULL,
3831 cp_clistate->cli_share_src,
3832 mask, &targetcli, &targetpath);
3833 if (!NT_STATUS_IS_OK(status)) {
3834 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3835 "%s\n"),
3836 mask, nt_errstr(status));
3837 return status;
3840 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
3841 copy_fn, cp_clistate);
3842 if (!NT_STATUS_IS_OK(status)) {
3843 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3844 mask, nt_errstr(status));
3847 return status;
3852 * Set the top level directory permissions before we do any further copies.
3853 * Should set up ACL inheritance.
3856 bool copy_top_level_perms(struct net_context *c,
3857 struct copy_clistate *cp_clistate,
3858 const char *sharename)
3860 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3862 switch (net_mode_share) {
3863 case NET_MODE_SHARE_MIGRATE:
3864 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3865 nt_status = net_copy_fileattr(c,
3866 cp_clistate->mem_ctx,
3867 cp_clistate->cli_share_src,
3868 cp_clistate->cli_share_dst,
3869 "\\", "\\",
3870 c->opt_acls? true : false,
3871 c->opt_attrs? true : false,
3872 c->opt_timestamps? true: false,
3873 false);
3874 break;
3875 default:
3876 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3877 break;
3880 if (!NT_STATUS_IS_OK(nt_status)) {
3881 printf(_("Could handle directory attributes for top level "
3882 "directory of share %s. Error %s\n"),
3883 sharename, nt_errstr(nt_status));
3884 return false;
3887 return true;
3891 * Sync all files inside a remote share to another share (over smb).
3893 * All parameters are provided by the run_rpc_command function, except for
3894 * argc, argv which are passed through.
3896 * @param domain_sid The domain sid acquired from the remote server.
3897 * @param cli A cli_state connected to the server.
3898 * @param mem_ctx Talloc context, destroyed on completion of the function.
3899 * @param argc Standard main() style argc.
3900 * @param argv Standard main() style argv. Initial components are already
3901 * stripped.
3903 * @return Normal NTSTATUS return.
3906 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3907 const struct dom_sid *domain_sid,
3908 const char *domain_name,
3909 struct cli_state *cli,
3910 struct rpc_pipe_client *pipe_hnd,
3911 TALLOC_CTX *mem_ctx,
3912 int argc,
3913 const char **argv)
3915 WERROR result;
3916 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3917 struct srvsvc_NetShareInfoCtr ctr_src;
3918 uint32 i;
3919 uint32 level = 502;
3920 struct copy_clistate cp_clistate;
3921 bool got_src_share = false;
3922 bool got_dst_share = false;
3923 const char *mask = "\\*";
3924 char *dst = NULL;
3926 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3927 if (dst == NULL) {
3928 nt_status = NT_STATUS_NO_MEMORY;
3929 goto done;
3932 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3933 &ctr_src);
3935 if (!W_ERROR_IS_OK(result))
3936 goto done;
3938 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3940 struct srvsvc_NetShareInfo502 info502 =
3941 ctr_src.ctr.ctr502->array[i];
3943 if (!check_share_sanity(c, cli, info502.name, info502.type))
3944 continue;
3946 /* one might not want to mirror whole discs :) */
3947 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3948 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3949 info502.name);
3950 continue;
3953 switch (net_mode_share)
3955 case NET_MODE_SHARE_MIGRATE:
3956 printf("syncing");
3957 break;
3958 default:
3959 d_fprintf(stderr, _("Unsupported mode %d\n"),
3960 net_mode_share);
3961 break;
3963 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3964 "Attributes %s\n"),
3965 info502.name,
3966 c->opt_acls ? _("including") : _("without"),
3967 c->opt_attrs ? _("including") : _("without"),
3968 c->opt_timestamps ? _("(preserving timestamps)") : "");
3970 cp_clistate.mem_ctx = mem_ctx;
3971 cp_clistate.cli_share_src = NULL;
3972 cp_clistate.cli_share_dst = NULL;
3973 cp_clistate.cwd = NULL;
3974 cp_clistate.attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
3975 cp_clistate.c = c;
3977 /* open share source */
3978 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3979 cli_state_remote_sockaddr(cli),
3980 cli_state_remote_name(cli),
3981 info502.name, "A:");
3982 if (!NT_STATUS_IS_OK(nt_status))
3983 goto done;
3985 got_src_share = true;
3987 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3988 /* open share destination */
3989 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3990 NULL, dst, info502.name, "A:");
3991 if (!NT_STATUS_IS_OK(nt_status))
3992 goto done;
3994 got_dst_share = true;
3997 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3998 d_fprintf(stderr, _("Could not handle the top level "
3999 "directory permissions for the "
4000 "share: %s\n"), info502.name);
4001 nt_status = NT_STATUS_UNSUCCESSFUL;
4002 goto done;
4005 nt_status = sync_files(&cp_clistate, mask);
4006 if (!NT_STATUS_IS_OK(nt_status)) {
4007 d_fprintf(stderr, _("could not handle files for share: "
4008 "%s\n"), info502.name);
4009 goto done;
4013 nt_status = NT_STATUS_OK;
4015 done:
4017 if (got_src_share)
4018 cli_shutdown(cp_clistate.cli_share_src);
4020 if (got_dst_share)
4021 cli_shutdown(cp_clistate.cli_share_dst);
4023 SAFE_FREE(dst);
4024 return nt_status;
4028 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
4030 if (c->display_usage) {
4031 d_printf( "%s\n"
4032 "net share migrate files\n"
4033 " %s\n",
4034 _("Usage:"),
4035 _("Migrate files to local server"));
4036 return 0;
4039 if (!c->opt_host) {
4040 d_printf(_("no server to migrate\n"));
4041 return -1;
4044 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4045 rpc_share_migrate_files_internals,
4046 argc, argv);
4050 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4052 * All parameters are provided by the run_rpc_command function, except for
4053 * argc, argv which are passed through.
4055 * @param domain_sid The domain sid acquired from the remote server.
4056 * @param cli A cli_state connected to the server.
4057 * @param mem_ctx Talloc context, destroyed on completion of the function.
4058 * @param argc Standard main() style argc.
4059 * @param argv Standard main() style argv. Initial components are already
4060 * stripped.
4062 * @return Normal NTSTATUS return.
4065 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
4066 const struct dom_sid *domain_sid,
4067 const char *domain_name,
4068 struct cli_state *cli,
4069 struct rpc_pipe_client *pipe_hnd,
4070 TALLOC_CTX *mem_ctx,
4071 int argc,
4072 const char **argv)
4074 WERROR result;
4075 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4076 struct srvsvc_NetShareInfoCtr ctr_src;
4077 union srvsvc_NetShareInfo info;
4078 uint32 i;
4079 struct rpc_pipe_client *srvsvc_pipe = NULL;
4080 struct cli_state *cli_dst = NULL;
4081 uint32 level = 502; /* includes secdesc */
4082 uint32_t parm_error = 0;
4083 struct dcerpc_binding_handle *b;
4085 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4086 &ctr_src);
4088 if (!W_ERROR_IS_OK(result))
4089 goto done;
4091 /* connect destination PI_SRVSVC */
4092 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
4093 &ndr_table_srvsvc.syntax_id);
4094 if (!NT_STATUS_IS_OK(nt_status))
4095 return nt_status;
4097 b = srvsvc_pipe->binding_handle;
4099 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4101 struct srvsvc_NetShareInfo502 info502 =
4102 ctr_src.ctr.ctr502->array[i];
4104 /* reset error-code */
4105 nt_status = NT_STATUS_UNSUCCESSFUL;
4107 if (!check_share_sanity(c, cli, info502.name, info502.type))
4108 continue;
4110 printf(_("migrating: [%s], path: %s, comment: %s, including "
4111 "share-ACLs\n"),
4112 info502.name, info502.path, info502.comment);
4114 if (c->opt_verbose)
4115 display_sec_desc(info502.sd_buf.sd);
4117 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4118 info.info502 = &info502;
4120 /* finally modify the share on the dst server */
4121 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
4122 srvsvc_pipe->desthost,
4123 info502.name,
4124 level,
4125 &info,
4126 &parm_error,
4127 &result);
4128 if (!NT_STATUS_IS_OK(nt_status)) {
4129 printf(_("cannot set share-acl: %s\n"),
4130 nt_errstr(nt_status));
4131 goto done;
4133 if (!W_ERROR_IS_OK(result)) {
4134 nt_status = werror_to_ntstatus(result);
4135 printf(_("cannot set share-acl: %s\n"),
4136 win_errstr(result));
4137 goto done;
4142 nt_status = NT_STATUS_OK;
4144 done:
4145 if (cli_dst) {
4146 cli_shutdown(cli_dst);
4149 return nt_status;
4154 * Migrate share-acls from a RPC server to another.
4156 * @param argc Standard main() style argc.
4157 * @param argv Standard main() style argv. Initial components are already
4158 * stripped.
4160 * @return A shell status integer (0 for success).
4162 static int rpc_share_migrate_security(struct net_context *c, int argc,
4163 const char **argv)
4165 if (c->display_usage) {
4166 d_printf( "%s\n"
4167 "net rpc share migrate security\n"
4168 " %s\n",
4169 _("Usage:"),
4170 _("Migrate share-acls to local server"));
4171 return 0;
4174 if (!c->opt_host) {
4175 d_printf(_("no server to migrate\n"));
4176 return -1;
4179 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4180 rpc_share_migrate_security_internals,
4181 argc, argv);
4185 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4186 * from one server to another.
4188 * @param argc Standard main() style argc.
4189 * @param argv Standard main() style argv. Initial components are already
4190 * stripped.
4192 * @return A shell status integer (0 for success).
4195 static int rpc_share_migrate_all(struct net_context *c, int argc,
4196 const char **argv)
4198 int ret;
4200 if (c->display_usage) {
4201 d_printf( "%s\n"
4202 "net rpc share migrate all\n"
4203 " %s\n",
4204 _("Usage:"),
4205 _("Migrates shares including all share settings"));
4206 return 0;
4209 if (!c->opt_host) {
4210 d_printf(_("no server to migrate\n"));
4211 return -1;
4214 /* order is important. we don't want to be locked out by the share-acl
4215 * before copying files - gd */
4217 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4218 rpc_share_migrate_shares_internals, argc, argv);
4219 if (ret)
4220 return ret;
4222 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4223 rpc_share_migrate_files_internals, argc, argv);
4224 if (ret)
4225 return ret;
4227 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4228 rpc_share_migrate_security_internals, argc,
4229 argv);
4234 * 'net rpc share migrate' entrypoint.
4235 * @param argc Standard main() style argc.
4236 * @param argv Standard main() style argv. Initial components are already
4237 * stripped.
4239 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4242 struct functable func[] = {
4244 "all",
4245 rpc_share_migrate_all,
4246 NET_TRANSPORT_RPC,
4247 N_("Migrate shares from remote to local server"),
4248 N_("net rpc share migrate all\n"
4249 " Migrate shares from remote to local server")
4252 "files",
4253 rpc_share_migrate_files,
4254 NET_TRANSPORT_RPC,
4255 N_("Migrate files from remote to local server"),
4256 N_("net rpc share migrate files\n"
4257 " Migrate files from remote to local server")
4260 "security",
4261 rpc_share_migrate_security,
4262 NET_TRANSPORT_RPC,
4263 N_("Migrate share-ACLs from remote to local server"),
4264 N_("net rpc share migrate security\n"
4265 " Migrate share-ACLs from remote to local server")
4268 "shares",
4269 rpc_share_migrate_shares,
4270 NET_TRANSPORT_RPC,
4271 N_("Migrate shares from remote to local server"),
4272 N_("net rpc share migrate shares\n"
4273 " Migrate shares from remote to local server")
4275 {NULL, NULL, 0, NULL, NULL}
4278 net_mode_share = NET_MODE_SHARE_MIGRATE;
4280 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4283 struct full_alias {
4284 struct dom_sid sid;
4285 uint32 num_members;
4286 struct dom_sid *members;
4289 static int num_server_aliases;
4290 static struct full_alias *server_aliases;
4293 * Add an alias to the static list.
4295 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4297 if (server_aliases == NULL)
4298 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4300 server_aliases[num_server_aliases] = *alias;
4301 num_server_aliases += 1;
4305 * For a specific domain on the server, fetch all the aliases
4306 * and their members. Add all of them to the server_aliases.
4309 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4310 TALLOC_CTX *mem_ctx,
4311 struct policy_handle *connect_pol,
4312 const struct dom_sid *domain_sid)
4314 uint32 start_idx, max_entries, num_entries, i;
4315 struct samr_SamArray *groups = NULL;
4316 NTSTATUS result, status;
4317 struct policy_handle domain_pol;
4318 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4320 /* Get domain policy handle */
4322 status = dcerpc_samr_OpenDomain(b, mem_ctx,
4323 connect_pol,
4324 MAXIMUM_ALLOWED_ACCESS,
4325 discard_const_p(struct dom_sid2, domain_sid),
4326 &domain_pol,
4327 &result);
4328 if (!NT_STATUS_IS_OK(status)) {
4329 return status;
4331 if (!NT_STATUS_IS_OK(result)) {
4332 return result;
4335 start_idx = 0;
4336 max_entries = 250;
4338 do {
4339 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
4340 &domain_pol,
4341 &start_idx,
4342 &groups,
4343 max_entries,
4344 &num_entries,
4345 &result);
4346 if (!NT_STATUS_IS_OK(status)) {
4347 goto done;
4349 for (i = 0; i < num_entries; i++) {
4351 struct policy_handle alias_pol;
4352 struct full_alias alias;
4353 struct lsa_SidArray sid_array;
4354 int j;
4355 NTSTATUS _result;
4357 status = dcerpc_samr_OpenAlias(b, mem_ctx,
4358 &domain_pol,
4359 MAXIMUM_ALLOWED_ACCESS,
4360 groups->entries[i].idx,
4361 &alias_pol,
4362 &_result);
4363 if (!NT_STATUS_IS_OK(status)) {
4364 goto done;
4366 if (!NT_STATUS_IS_OK(_result)) {
4367 status = _result;
4368 goto done;
4371 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
4372 &alias_pol,
4373 &sid_array,
4374 &_result);
4375 if (!NT_STATUS_IS_OK(status)) {
4376 goto done;
4378 if (!NT_STATUS_IS_OK(_result)) {
4379 status = _result;
4380 goto done;
4383 alias.num_members = sid_array.num_sids;
4385 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
4386 if (!NT_STATUS_IS_OK(status)) {
4387 goto done;
4389 if (!NT_STATUS_IS_OK(_result)) {
4390 status = _result;
4391 goto done;
4394 alias.members = NULL;
4396 if (alias.num_members > 0) {
4397 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4399 for (j = 0; j < alias.num_members; j++)
4400 sid_copy(&alias.members[j],
4401 sid_array.sids[j].sid);
4404 sid_compose(&alias.sid, domain_sid,
4405 groups->entries[i].idx);
4407 push_alias(mem_ctx, &alias);
4409 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4411 status = NT_STATUS_OK;
4413 done:
4414 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
4416 return status;
4420 * Dump server_aliases as names for debugging purposes.
4423 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4424 const struct dom_sid *domain_sid,
4425 const char *domain_name,
4426 struct cli_state *cli,
4427 struct rpc_pipe_client *pipe_hnd,
4428 TALLOC_CTX *mem_ctx,
4429 int argc,
4430 const char **argv)
4432 int i;
4433 NTSTATUS result;
4434 struct policy_handle lsa_pol;
4435 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4437 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4438 SEC_FLAG_MAXIMUM_ALLOWED,
4439 &lsa_pol);
4440 if (!NT_STATUS_IS_OK(result))
4441 return result;
4443 for (i=0; i<num_server_aliases; i++) {
4444 char **names;
4445 char **domains;
4446 enum lsa_SidType *types;
4447 int j;
4449 struct full_alias *alias = &server_aliases[i];
4451 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4452 &alias->sid,
4453 &domains, &names, &types);
4454 if (!NT_STATUS_IS_OK(result))
4455 continue;
4457 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4459 if (alias->num_members == 0) {
4460 DEBUG(1, ("\n"));
4461 continue;
4464 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4465 alias->num_members,
4466 alias->members,
4467 &domains, &names, &types);
4469 if (!NT_STATUS_IS_OK(result) &&
4470 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4471 continue;
4473 for (j=0; j<alias->num_members; j++)
4474 DEBUG(1, ("%s\\%s (%d); ",
4475 domains[j] ? domains[j] : "*unknown*",
4476 names[j] ? names[j] : "*unknown*",types[j]));
4477 DEBUG(1, ("\n"));
4480 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
4482 return NT_STATUS_OK;
4486 * Fetch a list of all server aliases and their members into
4487 * server_aliases.
4490 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4491 const struct dom_sid *domain_sid,
4492 const char *domain_name,
4493 struct cli_state *cli,
4494 struct rpc_pipe_client *pipe_hnd,
4495 TALLOC_CTX *mem_ctx,
4496 int argc,
4497 const char **argv)
4499 NTSTATUS result, status;
4500 struct policy_handle connect_pol;
4501 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4503 status = dcerpc_samr_Connect2(b, mem_ctx,
4504 pipe_hnd->desthost,
4505 MAXIMUM_ALLOWED_ACCESS,
4506 &connect_pol,
4507 &result);
4508 if (!NT_STATUS_IS_OK(status)) {
4509 goto done;
4511 if (!NT_STATUS_IS_OK(result)) {
4512 status = result;
4513 goto done;
4516 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4517 &global_sid_Builtin);
4518 if (!NT_STATUS_IS_OK(status)) {
4519 goto done;
4522 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4523 domain_sid);
4525 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
4526 done:
4527 return status;
4530 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4532 token->num_sids = 4;
4534 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4535 d_fprintf(stderr, "malloc %s\n",_("failed"));
4536 token->num_sids = 0;
4537 return;
4540 token->sids[0] = *user_sid;
4541 sid_copy(&token->sids[1], &global_sid_World);
4542 sid_copy(&token->sids[2], &global_sid_Network);
4543 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4546 static void free_user_token(struct security_token *token)
4548 SAFE_FREE(token->sids);
4551 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4553 if (security_token_has_sid(token, sid))
4554 return;
4556 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4557 if (!token->sids) {
4558 return;
4561 sid_copy(&token->sids[token->num_sids], sid);
4563 token->num_sids += 1;
4566 struct user_token {
4567 fstring name;
4568 struct security_token token;
4571 static void dump_user_token(struct user_token *token)
4573 int i;
4575 d_printf("%s\n", token->name);
4577 for (i=0; i<token->token.num_sids; i++) {
4578 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4582 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4584 int i;
4586 for (i=0; i<alias->num_members; i++) {
4587 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4588 return true;
4591 return false;
4594 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4596 int i;
4598 for (i=0; i<num_server_aliases; i++) {
4599 if (is_alias_member(&sid, &server_aliases[i]))
4600 add_sid_to_token(token, &server_aliases[i].sid);
4605 * We got a user token with all the SIDs we can know about without asking the
4606 * server directly. These are the user and domain group sids. All of these can
4607 * be members of aliases. So scan the list of aliases for each of the SIDs and
4608 * add them to the token.
4611 static void collect_alias_memberships(struct security_token *token)
4613 int num_global_sids = token->num_sids;
4614 int i;
4616 for (i=0; i<num_global_sids; i++) {
4617 collect_sid_memberships(token, token->sids[i]);
4621 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4623 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4624 enum wbcSidType type;
4625 fstring full_name;
4626 struct wbcDomainSid wsid;
4627 char sid_str[WBC_SID_STRING_BUFLEN];
4628 struct dom_sid user_sid;
4629 uint32_t num_groups;
4630 gid_t *groups = NULL;
4631 uint32_t i;
4633 fstr_sprintf(full_name, "%s%c%s",
4634 domain, *lp_winbind_separator(), user);
4636 /* First let's find out the user sid */
4638 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4640 if (!WBC_ERROR_IS_OK(wbc_status)) {
4641 DEBUG(1, ("winbind could not find %s: %s\n",
4642 full_name, wbcErrorString(wbc_status)));
4643 return false;
4646 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4648 if (type != WBC_SID_NAME_USER) {
4649 DEBUG(1, ("%s is not a user\n", full_name));
4650 return false;
4653 if (!string_to_sid(&user_sid, sid_str)) {
4654 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4655 return false;
4658 init_user_token(token, &user_sid);
4660 /* And now the groups winbind knows about */
4662 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4663 if (!WBC_ERROR_IS_OK(wbc_status)) {
4664 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4665 full_name, wbcErrorString(wbc_status)));
4666 return false;
4669 for (i = 0; i < num_groups; i++) {
4670 gid_t gid = groups[i];
4671 struct dom_sid sid;
4673 wbc_status = wbcGidToSid(gid, &wsid);
4674 if (!WBC_ERROR_IS_OK(wbc_status)) {
4675 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4676 (unsigned int)gid, wbcErrorString(wbc_status)));
4677 wbcFreeMemory(groups);
4678 return false;
4681 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4683 DEBUG(3, (" %s\n", sid_str));
4685 string_to_sid(&sid, sid_str);
4686 add_sid_to_token(token, &sid);
4688 wbcFreeMemory(groups);
4690 return true;
4694 * Get a list of all user tokens we want to look at
4697 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4698 struct user_token **user_tokens)
4700 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4701 uint32_t i, num_users;
4702 const char **users;
4703 struct user_token *result;
4704 TALLOC_CTX *frame = NULL;
4706 if (lp_winbind_use_default_domain() &&
4707 (c->opt_target_workgroup == NULL)) {
4708 d_fprintf(stderr, _("winbind use default domain = yes set, "
4709 "please specify a workgroup\n"));
4710 return false;
4713 /* Send request to winbind daemon */
4715 wbc_status = wbcListUsers(NULL, &num_users, &users);
4716 if (!WBC_ERROR_IS_OK(wbc_status)) {
4717 DEBUG(1, (_("winbind could not list users: %s\n"),
4718 wbcErrorString(wbc_status)));
4719 return false;
4722 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4724 if (result == NULL) {
4725 DEBUG(1, ("Could not malloc sid array\n"));
4726 wbcFreeMemory(users);
4727 return false;
4730 frame = talloc_stackframe();
4731 for (i=0; i < num_users; i++) {
4732 fstring domain, user;
4733 char *p;
4735 fstrcpy(result[i].name, users[i]);
4737 p = strchr(users[i], *lp_winbind_separator());
4739 DEBUG(3, ("%s\n", users[i]));
4741 if (p == NULL) {
4742 fstrcpy(domain, c->opt_target_workgroup);
4743 fstrcpy(user, users[i]);
4744 } else {
4745 *p++ = '\0';
4746 fstrcpy(domain, users[i]);
4747 strupper_m(domain);
4748 fstrcpy(user, p);
4751 get_user_sids(domain, user, &(result[i].token));
4753 TALLOC_FREE(frame);
4754 wbcFreeMemory(users);
4756 *num_tokens = num_users;
4757 *user_tokens = result;
4759 return true;
4762 static bool get_user_tokens_from_file(FILE *f,
4763 int *num_tokens,
4764 struct user_token **tokens)
4766 struct user_token *token = NULL;
4768 while (!feof(f)) {
4769 fstring line;
4771 if (fgets(line, sizeof(line)-1, f) == NULL) {
4772 return true;
4775 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4776 line[strlen(line)-1] = '\0';
4779 if (line[0] == ' ') {
4780 /* We have a SID */
4782 struct dom_sid sid;
4783 if(!string_to_sid(&sid, &line[1])) {
4784 DEBUG(1,("get_user_tokens_from_file: Could "
4785 "not convert sid %s \n",&line[1]));
4786 return false;
4789 if (token == NULL) {
4790 DEBUG(0, ("File does not begin with username"));
4791 return false;
4794 add_sid_to_token(&token->token, &sid);
4795 continue;
4798 /* And a new user... */
4800 *num_tokens += 1;
4801 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4802 if (*tokens == NULL) {
4803 DEBUG(0, ("Could not realloc tokens\n"));
4804 return false;
4807 token = &((*tokens)[*num_tokens-1]);
4809 strlcpy(token->name, line, sizeof(token->name));
4810 token->token.num_sids = 0;
4811 token->token.sids = NULL;
4812 continue;
4815 return false;
4820 * Show the list of all users that have access to a share
4823 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4824 TALLOC_CTX *mem_ctx,
4825 const char *netname,
4826 int num_tokens,
4827 struct user_token *tokens)
4829 uint16_t fnum;
4830 struct security_descriptor *share_sd = NULL;
4831 struct security_descriptor *root_sd = NULL;
4832 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4833 int i;
4834 union srvsvc_NetShareInfo info;
4835 WERROR result;
4836 NTSTATUS status;
4837 uint16 cnum;
4838 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4840 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
4841 pipe_hnd->desthost,
4842 netname,
4843 502,
4844 &info,
4845 &result);
4847 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4848 DEBUG(1, ("Coult not query secdesc for share %s\n",
4849 netname));
4850 return;
4853 share_sd = info.info502->sd_buf.sd;
4854 if (share_sd == NULL) {
4855 DEBUG(1, ("Got no secdesc for share %s\n",
4856 netname));
4859 cnum = cli_state_get_tid(cli);
4861 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4862 return;
4865 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4866 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4867 cli_query_secdesc(cli, fnum, mem_ctx, &root_sd);
4870 for (i=0; i<num_tokens; i++) {
4871 uint32 acc_granted;
4873 if (share_sd != NULL) {
4874 status = se_access_check(share_sd, &tokens[i].token,
4875 1, &acc_granted);
4877 if (!NT_STATUS_IS_OK(status)) {
4878 DEBUG(1, ("Could not check share_sd for "
4879 "user %s\n",
4880 tokens[i].name));
4881 continue;
4885 if (root_sd == NULL) {
4886 d_printf(" %s\n", tokens[i].name);
4887 continue;
4890 status = se_access_check(root_sd, &tokens[i].token,
4891 1, &acc_granted);
4892 if (!NT_STATUS_IS_OK(status)) {
4893 DEBUG(1, ("Could not check root_sd for user %s\n",
4894 tokens[i].name));
4895 continue;
4897 d_printf(" %s\n", tokens[i].name);
4900 if (fnum != (uint16_t)-1)
4901 cli_close(cli, fnum);
4902 cli_tdis(cli);
4903 cli_state_set_tid(cli, cnum);
4905 return;
4908 struct share_list {
4909 int num_shares;
4910 char **shares;
4913 static void collect_share(const char *name, uint32 m,
4914 const char *comment, void *state)
4916 struct share_list *share_list = (struct share_list *)state;
4918 if (m != STYPE_DISKTREE)
4919 return;
4921 share_list->num_shares += 1;
4922 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4923 if (!share_list->shares) {
4924 share_list->num_shares = 0;
4925 return;
4927 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4931 * List shares on a remote RPC server, including the security descriptors.
4933 * All parameters are provided by the run_rpc_command function, except for
4934 * argc, argv which are passed through.
4936 * @param domain_sid The domain sid acquired from the remote server.
4937 * @param cli A cli_state connected to the server.
4938 * @param mem_ctx Talloc context, destroyed on completion of the function.
4939 * @param argc Standard main() style argc.
4940 * @param argv Standard main() style argv. Initial components are already
4941 * stripped.
4943 * @return Normal NTSTATUS return.
4946 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4947 const struct dom_sid *domain_sid,
4948 const char *domain_name,
4949 struct cli_state *cli,
4950 struct rpc_pipe_client *pipe_hnd,
4951 TALLOC_CTX *mem_ctx,
4952 int argc,
4953 const char **argv)
4955 int ret;
4956 bool r;
4957 uint32 i;
4958 FILE *f;
4960 struct user_token *tokens = NULL;
4961 int num_tokens = 0;
4963 struct share_list share_list;
4965 if (argc == 0) {
4966 f = stdin;
4967 } else {
4968 f = fopen(argv[0], "r");
4971 if (f == NULL) {
4972 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4973 return NT_STATUS_UNSUCCESSFUL;
4976 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4978 if (f != stdin)
4979 fclose(f);
4981 if (!r) {
4982 DEBUG(0, ("Could not read users from file\n"));
4983 return NT_STATUS_UNSUCCESSFUL;
4986 for (i=0; i<num_tokens; i++)
4987 collect_alias_memberships(&tokens[i].token);
4989 share_list.num_shares = 0;
4990 share_list.shares = NULL;
4992 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4994 if (ret == -1) {
4995 DEBUG(0, ("Error returning browse list: %s\n",
4996 cli_errstr(cli)));
4997 goto done;
5000 for (i = 0; i < share_list.num_shares; i++) {
5001 char *netname = share_list.shares[i];
5003 if (netname[strlen(netname)-1] == '$')
5004 continue;
5006 d_printf("%s\n", netname);
5008 show_userlist(pipe_hnd, mem_ctx, netname,
5009 num_tokens, tokens);
5011 done:
5012 for (i=0; i<num_tokens; i++) {
5013 free_user_token(&tokens[i].token);
5015 SAFE_FREE(tokens);
5016 SAFE_FREE(share_list.shares);
5018 return NT_STATUS_OK;
5021 static int rpc_share_allowedusers(struct net_context *c, int argc,
5022 const char **argv)
5024 int result;
5026 if (c->display_usage) {
5027 d_printf( "%s\n"
5028 "net rpc share allowedusers\n"
5029 " %s\n",
5030 _("Usage:"),
5031 _("List allowed users"));
5032 return 0;
5035 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5036 rpc_aliaslist_internals,
5037 argc, argv);
5038 if (result != 0)
5039 return result;
5041 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
5042 rpc_aliaslist_dump,
5043 argc, argv);
5044 if (result != 0)
5045 return result;
5047 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
5048 rpc_share_allowedusers_internals,
5049 argc, argv);
5052 int net_usersidlist(struct net_context *c, int argc, const char **argv)
5054 int num_tokens = 0;
5055 struct user_token *tokens = NULL;
5056 int i;
5058 if (argc != 0) {
5059 net_usersidlist_usage(c, argc, argv);
5060 return 0;
5063 if (!get_user_tokens(c, &num_tokens, &tokens)) {
5064 DEBUG(0, ("Could not get the user/sid list\n"));
5065 return 0;
5068 for (i=0; i<num_tokens; i++) {
5069 dump_user_token(&tokens[i]);
5070 free_user_token(&tokens[i].token);
5073 SAFE_FREE(tokens);
5074 return 1;
5077 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
5079 d_printf(_("net usersidlist\n"
5080 "\tprints out a list of all users the running winbind knows\n"
5081 "\tabout, together with all their SIDs. This is used as\n"
5082 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5084 net_common_flags_usage(c, argc, argv);
5085 return -1;
5089 * 'net rpc share' entrypoint.
5090 * @param argc Standard main() style argc.
5091 * @param argv Standard main() style argv. Initial components are already
5092 * stripped.
5095 int net_rpc_share(struct net_context *c, int argc, const char **argv)
5097 NET_API_STATUS status;
5099 struct functable func[] = {
5101 "add",
5102 rpc_share_add,
5103 NET_TRANSPORT_RPC,
5104 N_("Add share"),
5105 N_("net rpc share add\n"
5106 " Add share")
5109 "delete",
5110 rpc_share_delete,
5111 NET_TRANSPORT_RPC,
5112 N_("Remove share"),
5113 N_("net rpc share delete\n"
5114 " Remove share")
5117 "allowedusers",
5118 rpc_share_allowedusers,
5119 NET_TRANSPORT_RPC,
5120 N_("Modify allowed users"),
5121 N_("net rpc share allowedusers\n"
5122 " Modify allowed users")
5125 "migrate",
5126 rpc_share_migrate,
5127 NET_TRANSPORT_RPC,
5128 N_("Migrate share to local server"),
5129 N_("net rpc share migrate\n"
5130 " Migrate share to local server")
5133 "list",
5134 rpc_share_list,
5135 NET_TRANSPORT_RPC,
5136 N_("List shares"),
5137 N_("net rpc share list\n"
5138 " List shares")
5140 {NULL, NULL, 0, NULL, NULL}
5143 status = libnetapi_net_init(&c->netapi_ctx);
5144 if (status != 0) {
5145 return -1;
5147 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5148 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5149 if (c->opt_kerberos) {
5150 libnetapi_set_use_kerberos(c->netapi_ctx);
5153 if (argc == 0) {
5154 if (c->display_usage) {
5155 d_printf("%s\n%s",
5156 _("Usage:"),
5157 _("net rpc share\n"
5158 " List shares\n"
5159 " Alias for net rpc share list\n"));
5160 net_display_usage_from_functable(func);
5161 return 0;
5164 return rpc_share_list(c, argc, argv);
5167 return net_run_function(c, argc, argv, "net rpc share", func);
5170 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5171 TALLOC_CTX *mem_ctx,
5172 struct rpc_sh_ctx *ctx,
5173 struct rpc_pipe_client *pipe_hnd,
5174 int argc, const char **argv)
5177 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
5180 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5181 TALLOC_CTX *mem_ctx,
5182 struct rpc_sh_ctx *ctx,
5183 struct rpc_pipe_client *pipe_hnd,
5184 int argc, const char **argv)
5186 NET_API_STATUS status;
5187 uint32_t parm_err = 0;
5188 struct SHARE_INFO_2 i2;
5190 if ((argc < 2) || (argc > 3)) {
5191 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
5192 ctx->whoami);
5193 return NT_STATUS_INVALID_PARAMETER;
5196 i2.shi2_netname = argv[0];
5197 i2.shi2_type = STYPE_DISKTREE;
5198 i2.shi2_remark = (argc == 3) ? argv[2] : "";
5199 i2.shi2_permissions = 0;
5200 i2.shi2_max_uses = 0;
5201 i2.shi2_current_uses = 0;
5202 i2.shi2_path = argv[1];
5203 i2.shi2_passwd = NULL;
5205 status = NetShareAdd(pipe_hnd->desthost,
5207 (uint8_t *)&i2,
5208 &parm_err);
5210 return werror_to_ntstatus(W_ERROR(status));
5213 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5214 TALLOC_CTX *mem_ctx,
5215 struct rpc_sh_ctx *ctx,
5216 struct rpc_pipe_client *pipe_hnd,
5217 int argc, const char **argv)
5219 if (argc != 1) {
5220 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5221 return NT_STATUS_INVALID_PARAMETER;
5224 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5227 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5228 TALLOC_CTX *mem_ctx,
5229 struct rpc_sh_ctx *ctx,
5230 struct rpc_pipe_client *pipe_hnd,
5231 int argc, const char **argv)
5233 union srvsvc_NetShareInfo info;
5234 WERROR result;
5235 NTSTATUS status;
5236 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5238 if (argc != 1) {
5239 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5240 return NT_STATUS_INVALID_PARAMETER;
5243 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5244 pipe_hnd->desthost,
5245 argv[0],
5247 &info,
5248 &result);
5249 if (!NT_STATUS_IS_OK(status)) {
5250 result = ntstatus_to_werror(status);
5251 goto done;
5253 if (!W_ERROR_IS_OK(result)) {
5254 goto done;
5257 d_printf(_("Name: %s\n"), info.info2->name);
5258 d_printf(_("Comment: %s\n"), info.info2->comment);
5259 d_printf(_("Path: %s\n"), info.info2->path);
5260 d_printf(_("Password: %s\n"), info.info2->password);
5262 done:
5263 return werror_to_ntstatus(result);
5266 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5267 struct rpc_sh_ctx *ctx)
5269 static struct rpc_sh_cmd cmds[] = {
5271 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
5272 N_("List available shares") },
5274 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
5275 N_("Add a share") },
5277 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
5278 N_("Delete a share") },
5280 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
5281 N_("Get information about a share") },
5283 { NULL, NULL, 0, NULL, NULL }
5286 return cmds;
5289 /****************************************************************************/
5291 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5293 return net_file_usage(c, argc, argv);
5297 * Close a file on a remote RPC server.
5299 * @param argc Standard main() style argc.
5300 * @param argv Standard main() style argv. Initial components are already
5301 * stripped.
5303 * @return A shell status integer (0 for success).
5305 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5307 if (argc < 1 || c->display_usage) {
5308 return rpc_file_usage(c, argc, argv);
5311 return NetFileClose(c->opt_host, atoi(argv[0]));
5315 * Formatted print of open file info
5317 * @param r struct FILE_INFO_3 contents
5320 static void display_file_info_3(struct FILE_INFO_3 *r)
5322 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5323 r->fi3_id, r->fi3_username, r->fi3_permissions,
5324 r->fi3_num_locks, r->fi3_pathname);
5328 * List files for a user on a remote RPC server.
5330 * @param argc Standard main() style argc.
5331 * @param argv Standard main() style argv. Initial components are already
5332 * stripped.
5334 * @return A shell status integer (0 for success)..
5337 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5339 NET_API_STATUS status;
5340 uint32 preferred_len = 0xffffffff, i;
5341 const char *username=NULL;
5342 uint32_t total_entries = 0;
5343 uint32_t entries_read = 0;
5344 uint32_t resume_handle = 0;
5345 struct FILE_INFO_3 *i3 = NULL;
5347 if (c->display_usage) {
5348 return rpc_file_usage(c, argc, argv);
5351 /* if argc > 0, must be user command */
5352 if (argc > 0) {
5353 username = smb_xstrdup(argv[0]);
5356 status = NetFileEnum(c->opt_host,
5357 NULL,
5358 username,
5360 (uint8_t **)(void *)&i3,
5361 preferred_len,
5362 &entries_read,
5363 &total_entries,
5364 &resume_handle);
5366 if (status != 0) {
5367 goto done;
5370 /* Display results */
5372 d_printf(_(
5373 "\nEnumerating open files on remote server:\n\n"
5374 "\nFileId Opened by Perms Locks Path"
5375 "\n------ --------- ----- ----- ---- \n"));
5376 for (i = 0; i < entries_read; i++) {
5377 display_file_info_3(&i3[i]);
5379 done:
5380 return status;
5384 * 'net rpc file' entrypoint.
5385 * @param argc Standard main() style argc.
5386 * @param argv Standard main() style argv. Initial components are already
5387 * stripped.
5390 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5392 NET_API_STATUS status;
5394 struct functable func[] = {
5396 "close",
5397 rpc_file_close,
5398 NET_TRANSPORT_RPC,
5399 N_("Close opened file"),
5400 N_("net rpc file close\n"
5401 " Close opened file")
5404 "user",
5405 rpc_file_user,
5406 NET_TRANSPORT_RPC,
5407 N_("List files opened by user"),
5408 N_("net rpc file user\n"
5409 " List files opened by user")
5411 #if 0
5413 "info",
5414 rpc_file_info,
5415 NET_TRANSPORT_RPC,
5416 N_("Display information about opened file"),
5417 N_("net rpc file info\n"
5418 " Display information about opened file")
5420 #endif
5421 {NULL, NULL, 0, NULL, NULL}
5424 status = libnetapi_net_init(&c->netapi_ctx);
5425 if (status != 0) {
5426 return -1;
5428 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5429 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5430 if (c->opt_kerberos) {
5431 libnetapi_set_use_kerberos(c->netapi_ctx);
5434 if (argc == 0) {
5435 if (c->display_usage) {
5436 d_printf(_("Usage:\n"));
5437 d_printf(_("net rpc file\n"
5438 " List opened files\n"));
5439 net_display_usage_from_functable(func);
5440 return 0;
5443 return rpc_file_user(c, argc, argv);
5446 return net_run_function(c, argc, argv, "net rpc file", func);
5450 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5452 * All parameters are provided by the run_rpc_command function, except for
5453 * argc, argv which are passed through.
5455 * @param c A net_context structure.
5456 * @param domain_sid The domain sid acquired from the remote server.
5457 * @param cli A cli_state connected to the server.
5458 * @param mem_ctx Talloc context, destroyed on completion of the function.
5459 * @param argc Standard main() style argc.
5460 * @param argv Standard main() style argv. Initial components are already
5461 * stripped.
5463 * @return Normal NTSTATUS return.
5466 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5467 const struct dom_sid *domain_sid,
5468 const char *domain_name,
5469 struct cli_state *cli,
5470 struct rpc_pipe_client *pipe_hnd,
5471 TALLOC_CTX *mem_ctx,
5472 int argc,
5473 const char **argv)
5475 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5476 WERROR result;
5477 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5479 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5480 if (!NT_STATUS_IS_OK(status)) {
5481 return status;
5483 if (W_ERROR_IS_OK(result)) {
5484 d_printf(_("\nShutdown successfully aborted\n"));
5485 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5486 } else
5487 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5489 return werror_to_ntstatus(result);
5493 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5495 * All parameters are provided by the run_rpc_command function, except for
5496 * argc, argv which are passed through.
5498 * @param c A net_context structure.
5499 * @param domain_sid The domain sid acquired from the remote server.
5500 * @param cli A cli_state connected to the server.
5501 * @param mem_ctx Talloc context, destroyed on completion of the function.
5502 * @param argc Standard main() style argc.
5503 * @param argv Standard main() style argv. Initial components are already
5504 * stripped.
5506 * @return Normal NTSTATUS return.
5509 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5510 const struct dom_sid *domain_sid,
5511 const char *domain_name,
5512 struct cli_state *cli,
5513 struct rpc_pipe_client *pipe_hnd,
5514 TALLOC_CTX *mem_ctx,
5515 int argc,
5516 const char **argv)
5518 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5519 WERROR werr;
5520 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5522 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5524 if (!NT_STATUS_IS_OK(result)) {
5525 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5526 return result;
5528 if (W_ERROR_IS_OK(werr)) {
5529 d_printf(_("\nShutdown successfully aborted\n"));
5530 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5531 } else
5532 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5534 return werror_to_ntstatus(werr);
5538 * ABORT the shutdown of a remote RPC server.
5540 * @param argc Standard main() style argc.
5541 * @param argv Standard main() style argv. Initial components are already
5542 * stripped.
5544 * @return A shell status integer (0 for success).
5547 static int rpc_shutdown_abort(struct net_context *c, int argc,
5548 const char **argv)
5550 int rc = -1;
5552 if (c->display_usage) {
5553 d_printf( "%s\n"
5554 "net rpc abortshutdown\n"
5555 " %s\n",
5556 _("Usage:"),
5557 _("Abort a scheduled shutdown"));
5558 return 0;
5561 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5562 rpc_shutdown_abort_internals, argc, argv);
5564 if (rc == 0)
5565 return rc;
5567 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5569 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5570 rpc_reg_shutdown_abort_internals,
5571 argc, argv);
5575 * Shut down a remote RPC Server via initshutdown pipe.
5577 * All parameters are provided by the run_rpc_command function, except for
5578 * argc, argv which are passed through.
5580 * @param c A net_context structure.
5581 * @param domain_sid The domain sid acquired from the remote server.
5582 * @param cli A cli_state connected to the server.
5583 * @param mem_ctx Talloc context, destroyed on completion of the function.
5584 * @param argc Standard main() style argc.
5585 * @param argv Standard main() style argv. Initial components are already
5586 * stripped.
5588 * @return Normal NTSTATUS return.
5591 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5592 const struct dom_sid *domain_sid,
5593 const char *domain_name,
5594 struct cli_state *cli,
5595 struct rpc_pipe_client *pipe_hnd,
5596 TALLOC_CTX *mem_ctx,
5597 int argc,
5598 const char **argv)
5600 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5601 WERROR result;
5602 const char *msg = N_("This machine will be shutdown shortly");
5603 uint32 timeout = 20;
5604 struct lsa_StringLarge msg_string;
5605 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5607 if (c->opt_comment) {
5608 msg = c->opt_comment;
5610 if (c->opt_timeout) {
5611 timeout = c->opt_timeout;
5614 msg_string.string = msg;
5616 /* create an entry */
5617 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5618 &msg_string, timeout, c->opt_force, c->opt_reboot,
5619 &result);
5620 if (!NT_STATUS_IS_OK(status)) {
5621 return status;
5623 if (W_ERROR_IS_OK(result)) {
5624 d_printf(_("\nShutdown of remote machine succeeded\n"));
5625 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5626 } else {
5627 DEBUG(1,("Shutdown of remote machine failed!\n"));
5629 return werror_to_ntstatus(result);
5633 * Shut down a remote RPC Server via winreg pipe.
5635 * All parameters are provided by the run_rpc_command function, except for
5636 * argc, argv which are passed through.
5638 * @param c A net_context structure.
5639 * @param domain_sid The domain sid acquired from the remote server.
5640 * @param cli A cli_state connected to the server.
5641 * @param mem_ctx Talloc context, destroyed on completion of the function.
5642 * @param argc Standard main() style argc.
5643 * @param argv Standard main() style argv. Initial components are already
5644 * stripped.
5646 * @return Normal NTSTATUS return.
5649 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5650 const struct dom_sid *domain_sid,
5651 const char *domain_name,
5652 struct cli_state *cli,
5653 struct rpc_pipe_client *pipe_hnd,
5654 TALLOC_CTX *mem_ctx,
5655 int argc,
5656 const char **argv)
5658 const char *msg = N_("This machine will be shutdown shortly");
5659 uint32 timeout = 20;
5660 struct lsa_StringLarge msg_string;
5661 NTSTATUS result;
5662 WERROR werr;
5663 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5665 if (c->opt_comment) {
5666 msg = c->opt_comment;
5668 msg_string.string = msg;
5670 if (c->opt_timeout) {
5671 timeout = c->opt_timeout;
5674 /* create an entry */
5675 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5676 &msg_string, timeout, c->opt_force, c->opt_reboot,
5677 &werr);
5678 if (!NT_STATUS_IS_OK(result)) {
5679 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5680 return result;
5683 if (W_ERROR_IS_OK(werr)) {
5684 d_printf(_("\nShutdown of remote machine succeeded\n"));
5685 } else {
5686 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5687 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5688 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5689 else
5690 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5693 return werror_to_ntstatus(werr);
5697 * Shut down a remote RPC server.
5699 * @param argc Standard main() style argc.
5700 * @param argv Standard main() style argv. Initial components are already
5701 * stripped.
5703 * @return A shell status integer (0 for success).
5706 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5708 int rc = -1;
5710 if (c->display_usage) {
5711 d_printf( "%s\n"
5712 "net rpc shutdown\n"
5713 " %s\n",
5714 _("Usage:"),
5715 _("Shut down a remote RPC server"));
5716 return 0;
5719 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5720 rpc_init_shutdown_internals, argc, argv);
5722 if (rc) {
5723 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5724 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5725 rpc_reg_shutdown_internals, argc, argv);
5728 return rc;
5731 /***************************************************************************
5732 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5733 ***************************************************************************/
5736 * Add interdomain trust account to the RPC server.
5737 * All parameters (except for argc and argv) are passed by run_rpc_command
5738 * function.
5740 * @param c A net_context structure.
5741 * @param domain_sid The domain sid acquired from the server.
5742 * @param cli A cli_state connected to the server.
5743 * @param mem_ctx Talloc context, destroyed on completion of the function.
5744 * @param argc Standard main() style argc.
5745 * @param argv Standard main() style argv. Initial components are already
5746 * stripped.
5748 * @return normal NTSTATUS return code.
5751 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5752 const struct dom_sid *domain_sid,
5753 const char *domain_name,
5754 struct cli_state *cli,
5755 struct rpc_pipe_client *pipe_hnd,
5756 TALLOC_CTX *mem_ctx,
5757 int argc,
5758 const char **argv)
5760 struct policy_handle connect_pol, domain_pol, user_pol;
5761 NTSTATUS status, result;
5762 char *acct_name;
5763 struct lsa_String lsa_acct_name;
5764 uint32 acb_info;
5765 uint32 acct_flags=0;
5766 uint32 user_rid;
5767 uint32_t access_granted = 0;
5768 union samr_UserInfo info;
5769 unsigned int orig_timeout;
5770 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5772 if (argc != 2) {
5773 d_printf("%s\n%s",
5774 _("Usage:"),
5775 _(" net rpc trustdom add <domain_name> "
5776 "<trust password>\n"));
5777 return NT_STATUS_INVALID_PARAMETER;
5781 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5784 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5785 return NT_STATUS_NO_MEMORY;
5788 strupper_m(acct_name);
5790 init_lsa_String(&lsa_acct_name, acct_name);
5792 /* Get samr policy handle */
5793 status = dcerpc_samr_Connect2(b, mem_ctx,
5794 pipe_hnd->desthost,
5795 MAXIMUM_ALLOWED_ACCESS,
5796 &connect_pol,
5797 &result);
5798 if (!NT_STATUS_IS_OK(status)) {
5799 goto done;
5801 if (!NT_STATUS_IS_OK(result)) {
5802 status = result;
5803 goto done;
5806 /* Get domain policy handle */
5807 status = dcerpc_samr_OpenDomain(b, mem_ctx,
5808 &connect_pol,
5809 MAXIMUM_ALLOWED_ACCESS,
5810 discard_const_p(struct dom_sid2, domain_sid),
5811 &domain_pol,
5812 &result);
5813 if (!NT_STATUS_IS_OK(status)) {
5814 goto done;
5816 if (!NT_STATUS_IS_OK(result)) {
5817 status = result;
5818 goto done;
5821 /* This call can take a long time - allow the server to time out.
5822 * 35 seconds should do it. */
5824 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5826 /* Create trusting domain's account */
5827 acb_info = ACB_NORMAL;
5828 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5829 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5830 SAMR_USER_ACCESS_SET_PASSWORD |
5831 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5832 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5834 status = dcerpc_samr_CreateUser2(b, mem_ctx,
5835 &domain_pol,
5836 &lsa_acct_name,
5837 acb_info,
5838 acct_flags,
5839 &user_pol,
5840 &access_granted,
5841 &user_rid,
5842 &result);
5843 if (!NT_STATUS_IS_OK(status)) {
5844 goto done;
5846 /* And restore our original timeout. */
5847 rpccli_set_timeout(pipe_hnd, orig_timeout);
5849 if (!NT_STATUS_IS_OK(result)) {
5850 status = result;
5851 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5852 acct_name, nt_errstr(result));
5853 goto done;
5857 struct samr_CryptPassword crypt_pwd;
5859 ZERO_STRUCT(info.info23);
5861 init_samr_CryptPassword(argv[1],
5862 &cli->user_session_key,
5863 &crypt_pwd);
5865 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5866 SAMR_FIELD_NT_PASSWORD_PRESENT;
5867 info.info23.info.acct_flags = ACB_DOMTRUST;
5868 info.info23.password = crypt_pwd;
5870 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
5871 &user_pol,
5873 &info,
5874 &result);
5875 if (!NT_STATUS_IS_OK(status)) {
5876 goto done;
5879 if (!NT_STATUS_IS_OK(result)) {
5880 status = result;
5881 DEBUG(0,("Could not set trust account password: %s\n",
5882 nt_errstr(result)));
5883 goto done;
5887 done:
5888 SAFE_FREE(acct_name);
5889 return status;
5893 * Create interdomain trust account for a remote domain.
5895 * @param argc Standard argc.
5896 * @param argv Standard argv without initial components.
5898 * @return Integer status (0 means success).
5901 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5903 if (argc > 0 && !c->display_usage) {
5904 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5905 rpc_trustdom_add_internals, argc, argv);
5906 } else {
5907 d_printf("%s\n%s",
5908 _("Usage:"),
5909 _("net rpc trustdom add <domain_name> <trust "
5910 "password>\n"));
5911 return -1;
5917 * Remove interdomain trust account from the RPC server.
5918 * All parameters (except for argc and argv) are passed by run_rpc_command
5919 * function.
5921 * @param c A net_context structure.
5922 * @param domain_sid The domain sid acquired from the server.
5923 * @param cli A cli_state connected to the server.
5924 * @param mem_ctx Talloc context, destroyed on completion of the function.
5925 * @param argc Standard main() style argc.
5926 * @param argv Standard main() style argv. Initial components are already
5927 * stripped.
5929 * @return normal NTSTATUS return code.
5932 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5933 const struct dom_sid *domain_sid,
5934 const char *domain_name,
5935 struct cli_state *cli,
5936 struct rpc_pipe_client *pipe_hnd,
5937 TALLOC_CTX *mem_ctx,
5938 int argc,
5939 const char **argv)
5941 struct policy_handle connect_pol, domain_pol, user_pol;
5942 NTSTATUS status, result;
5943 char *acct_name;
5944 struct dom_sid trust_acct_sid;
5945 struct samr_Ids user_rids, name_types;
5946 struct lsa_String lsa_acct_name;
5947 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5949 if (argc != 1) {
5950 d_printf("%s\n%s",
5951 _("Usage:"),
5952 _(" net rpc trustdom del <domain_name>\n"));
5953 return NT_STATUS_INVALID_PARAMETER;
5957 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5959 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5961 if (acct_name == NULL)
5962 return NT_STATUS_NO_MEMORY;
5964 strupper_m(acct_name);
5966 /* Get samr policy handle */
5967 status = dcerpc_samr_Connect2(b, mem_ctx,
5968 pipe_hnd->desthost,
5969 MAXIMUM_ALLOWED_ACCESS,
5970 &connect_pol,
5971 &result);
5972 if (!NT_STATUS_IS_OK(status)) {
5973 goto done;
5975 if (!NT_STATUS_IS_OK(result)) {
5976 status = result;
5977 goto done;
5980 /* Get domain policy handle */
5981 status = dcerpc_samr_OpenDomain(b, mem_ctx,
5982 &connect_pol,
5983 MAXIMUM_ALLOWED_ACCESS,
5984 discard_const_p(struct dom_sid2, domain_sid),
5985 &domain_pol,
5986 &result);
5987 if (!NT_STATUS_IS_OK(status)) {
5988 goto done;
5990 if (!NT_STATUS_IS_OK(result)) {
5991 status = result;
5992 goto done;
5995 init_lsa_String(&lsa_acct_name, acct_name);
5997 status = dcerpc_samr_LookupNames(b, mem_ctx,
5998 &domain_pol,
6000 &lsa_acct_name,
6001 &user_rids,
6002 &name_types,
6003 &result);
6004 if (!NT_STATUS_IS_OK(status)) {
6005 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6006 "failed %s\n"),
6007 acct_name, nt_errstr(status));
6008 goto done;
6010 if (!NT_STATUS_IS_OK(result)) {
6011 status = result;
6012 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6013 "failed %s\n"),
6014 acct_name, nt_errstr(result) );
6015 goto done;
6018 status = dcerpc_samr_OpenUser(b, mem_ctx,
6019 &domain_pol,
6020 MAXIMUM_ALLOWED_ACCESS,
6021 user_rids.ids[0],
6022 &user_pol,
6023 &result);
6024 if (!NT_STATUS_IS_OK(status)) {
6025 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6026 "%s\n"),
6027 acct_name, nt_errstr(status) );
6028 goto done;
6031 if (!NT_STATUS_IS_OK(result)) {
6032 status = result;
6033 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6034 "%s\n"),
6035 acct_name, nt_errstr(result) );
6036 goto done;
6039 /* append the rid to the domain sid */
6040 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6041 goto done;
6044 /* remove the sid */
6046 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6047 &user_pol,
6048 &trust_acct_sid,
6049 &result);
6050 if (!NT_STATUS_IS_OK(status)) {
6051 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6052 " on user %s failed %s\n"),
6053 acct_name, nt_errstr(status));
6054 goto done;
6056 if (!NT_STATUS_IS_OK(result)) {
6057 status = result;
6058 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6059 " on user %s failed %s\n"),
6060 acct_name, nt_errstr(result) );
6061 goto done;
6065 /* Delete user */
6067 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6068 &user_pol,
6069 &result);
6070 if (!NT_STATUS_IS_OK(status)) {
6071 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6072 "%s\n"),
6073 acct_name, nt_errstr(status));
6074 goto done;
6077 if (!NT_STATUS_IS_OK(result)) {
6078 result = status;
6079 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6080 "%s\n"),
6081 acct_name, nt_errstr(result) );
6082 goto done;
6085 if (!NT_STATUS_IS_OK(result)) {
6086 d_printf(_("Could not set trust account password: %s\n"),
6087 nt_errstr(result));
6088 goto done;
6091 done:
6092 return status;
6096 * Delete interdomain trust account for a remote domain.
6098 * @param argc Standard argc.
6099 * @param argv Standard argv without initial components.
6101 * @return Integer status (0 means success).
6104 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6106 if (argc > 0 && !c->display_usage) {
6107 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
6108 rpc_trustdom_del_internals, argc, argv);
6109 } else {
6110 d_printf("%s\n%s",
6111 _("Usage:"),
6112 _("net rpc trustdom del <domain>\n"));
6113 return -1;
6117 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6118 struct cli_state *cli,
6119 TALLOC_CTX *mem_ctx,
6120 const char *domain_name)
6122 char *dc_name = NULL;
6123 const char *buffer = NULL;
6124 struct rpc_pipe_client *netr;
6125 NTSTATUS status;
6126 WERROR result;
6127 struct dcerpc_binding_handle *b;
6129 /* Use NetServerEnum2 */
6131 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6132 SAFE_FREE(dc_name);
6133 return NT_STATUS_OK;
6136 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6137 for domain %s\n", domain_name));
6139 /* Try netr_GetDcName */
6141 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
6142 &netr);
6143 if (!NT_STATUS_IS_OK(status)) {
6144 return status;
6147 b = netr->binding_handle;
6149 status = dcerpc_netr_GetDcName(b, mem_ctx,
6150 netr->desthost,
6151 domain_name,
6152 &buffer,
6153 &result);
6154 TALLOC_FREE(netr);
6156 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6157 return status;
6160 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6161 for domain %s\n", domain_name));
6163 if (!NT_STATUS_IS_OK(status)) {
6164 return status;
6167 return werror_to_ntstatus(result);
6171 * Establish trust relationship to a trusting domain.
6172 * Interdomain account must already be created on remote PDC.
6174 * @param c A net_context structure.
6175 * @param argc Standard argc.
6176 * @param argv Standard argv without initial components.
6178 * @return Integer status (0 means success).
6181 static int rpc_trustdom_establish(struct net_context *c, int argc,
6182 const char **argv)
6184 struct cli_state *cli = NULL;
6185 struct sockaddr_storage server_ss;
6186 struct rpc_pipe_client *pipe_hnd = NULL;
6187 struct policy_handle connect_hnd;
6188 TALLOC_CTX *mem_ctx;
6189 NTSTATUS nt_status, result;
6190 struct dom_sid *domain_sid;
6192 char* domain_name;
6193 char* acct_name;
6194 fstring pdc_name;
6195 union lsa_PolicyInformation *info = NULL;
6196 struct dcerpc_binding_handle *b;
6199 * Connect to \\server\ipc$ as 'our domain' account with password
6202 if (argc != 1 || c->display_usage) {
6203 d_printf("%s\n%s",
6204 _("Usage:"),
6205 _("net rpc trustdom establish <domain_name>\n"));
6206 return -1;
6209 domain_name = smb_xstrdup(argv[0]);
6210 strupper_m(domain_name);
6212 /* account name used at first is our domain's name with '$' */
6213 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6214 return -1;
6216 strupper_m(acct_name);
6219 * opt_workgroup will be used by connection functions further,
6220 * hence it should be set to remote domain name instead of ours
6222 if (c->opt_workgroup) {
6223 c->opt_workgroup = smb_xstrdup(domain_name);
6226 c->opt_user_name = acct_name;
6228 /* find the domain controller */
6229 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6230 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6231 return -1;
6234 /* connect to ipc$ as username/password */
6235 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6236 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6238 /* Is it trusting domain account for sure ? */
6239 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6240 nt_errstr(nt_status)));
6241 return -1;
6244 /* store who we connected to */
6246 saf_store( domain_name, pdc_name );
6249 * Connect to \\server\ipc$ again (this time anonymously)
6252 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6253 (char*)pdc_name);
6255 if (NT_STATUS_IS_ERR(nt_status)) {
6256 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6257 domain_name, nt_errstr(nt_status)));
6258 return -1;
6261 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6262 "domain %s", domain_name))) {
6263 DEBUG(0, ("talloc_init() failed\n"));
6264 cli_shutdown(cli);
6265 return -1;
6268 /* Make sure we're talking to a proper server */
6270 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6271 if (!NT_STATUS_IS_OK(nt_status)) {
6272 cli_shutdown(cli);
6273 talloc_destroy(mem_ctx);
6274 return -1;
6278 * Call LsaOpenPolicy and LsaQueryInfo
6281 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6282 &pipe_hnd);
6283 if (!NT_STATUS_IS_OK(nt_status)) {
6284 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6285 cli_shutdown(cli);
6286 talloc_destroy(mem_ctx);
6287 return -1;
6290 b = pipe_hnd->binding_handle;
6292 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6293 &connect_hnd);
6294 if (NT_STATUS_IS_ERR(nt_status)) {
6295 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6296 nt_errstr(nt_status)));
6297 cli_shutdown(cli);
6298 talloc_destroy(mem_ctx);
6299 return -1;
6302 /* Querying info level 5 */
6304 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6305 &connect_hnd,
6306 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6307 &info,
6308 &result);
6309 if (NT_STATUS_IS_ERR(nt_status)) {
6310 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6311 nt_errstr(nt_status)));
6312 cli_shutdown(cli);
6313 talloc_destroy(mem_ctx);
6314 return -1;
6316 if (NT_STATUS_IS_ERR(result)) {
6317 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6318 nt_errstr(result)));
6319 cli_shutdown(cli);
6320 talloc_destroy(mem_ctx);
6321 return -1;
6324 domain_sid = info->account_domain.sid;
6326 /* There should be actually query info level 3 (following nt serv behaviour),
6327 but I still don't know if it's _really_ necessary */
6330 * Store the password in secrets db
6333 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6334 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6335 cli_shutdown(cli);
6336 talloc_destroy(mem_ctx);
6337 return -1;
6341 * Close the pipes and clean up
6344 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6345 if (NT_STATUS_IS_ERR(nt_status)) {
6346 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6347 nt_errstr(nt_status)));
6348 cli_shutdown(cli);
6349 talloc_destroy(mem_ctx);
6350 return -1;
6353 cli_shutdown(cli);
6355 talloc_destroy(mem_ctx);
6357 d_printf(_("Trust to domain %s established\n"), domain_name);
6358 return 0;
6362 * Revoke trust relationship to the remote domain.
6364 * @param c A net_context structure.
6365 * @param argc Standard argc.
6366 * @param argv Standard argv without initial components.
6368 * @return Integer status (0 means success).
6371 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6372 const char **argv)
6374 char* domain_name;
6375 int rc = -1;
6377 if (argc < 1 || c->display_usage) {
6378 d_printf("%s\n%s",
6379 _("Usage:"),
6380 _("net rpc trustdom revoke <domain_name>\n"
6381 " Revoke trust relationship\n"
6382 " domain_name\tName of domain to revoke trust\n"));
6383 return -1;
6386 /* generate upper cased domain name */
6387 domain_name = smb_xstrdup(argv[0]);
6388 strupper_m(domain_name);
6390 /* delete password of the trust */
6391 if (!pdb_del_trusteddom_pw(domain_name)) {
6392 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6393 domain_name));
6394 goto done;
6397 rc = 0;
6398 done:
6399 SAFE_FREE(domain_name);
6400 return rc;
6403 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6404 const struct dom_sid *domain_sid,
6405 const char *domain_name,
6406 struct cli_state *cli,
6407 struct rpc_pipe_client *pipe_hnd,
6408 TALLOC_CTX *mem_ctx,
6409 int argc,
6410 const char **argv)
6412 fstring str_sid;
6413 if (!sid_to_fstring(str_sid, domain_sid)) {
6414 return NT_STATUS_UNSUCCESSFUL;
6416 d_printf("%s\n", str_sid);
6417 return NT_STATUS_OK;
6420 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6422 fstring ascii_sid;
6424 /* convert sid into ascii string */
6425 sid_to_fstring(ascii_sid, dom_sid);
6427 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
6430 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6431 TALLOC_CTX *mem_ctx,
6432 struct policy_handle *pol,
6433 struct dom_sid dom_sid,
6434 const char *trusted_dom_name)
6436 NTSTATUS nt_status, result;
6437 union lsa_TrustedDomainInfo *info = NULL;
6438 char *cleartextpwd = NULL;
6439 DATA_BLOB session_key;
6440 DATA_BLOB data = data_blob_null;
6441 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6443 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6444 pol,
6445 &dom_sid,
6446 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6447 &info,
6448 &result);
6449 if (NT_STATUS_IS_ERR(nt_status)) {
6450 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6451 nt_errstr(nt_status)));
6452 goto done;
6454 if (NT_STATUS_IS_ERR(result)) {
6455 nt_status = result;
6456 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6457 nt_errstr(result)));
6458 goto done;
6461 data = data_blob(info->password.password->data,
6462 info->password.password->length);
6464 nt_status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6465 if (!NT_STATUS_IS_OK(nt_status)) {
6466 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status)));
6467 goto done;
6470 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key);
6471 data_blob_free(&session_key);
6473 if (cleartextpwd == NULL) {
6474 DEBUG(0,("retrieved NULL password\n"));
6475 nt_status = NT_STATUS_UNSUCCESSFUL;
6476 goto done;
6479 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6480 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6481 nt_status = NT_STATUS_UNSUCCESSFUL;
6482 goto done;
6485 #ifdef DEBUG_PASSWORD
6486 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6487 "password: [%s]\n", trusted_dom_name,
6488 sid_string_dbg(&dom_sid), cleartextpwd));
6489 #endif
6491 done:
6492 SAFE_FREE(cleartextpwd);
6493 data_blob_free(&data);
6495 return nt_status;
6498 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6499 const char **argv)
6501 /* common variables */
6502 TALLOC_CTX* mem_ctx;
6503 struct cli_state *cli = NULL;
6504 struct rpc_pipe_client *pipe_hnd = NULL;
6505 NTSTATUS nt_status, result;
6506 const char *domain_name = NULL;
6507 struct policy_handle connect_hnd;
6508 union lsa_PolicyInformation *info = NULL;
6510 /* trusted domains listing variables */
6511 unsigned int enum_ctx = 0;
6512 int i;
6513 struct lsa_DomainList dom_list;
6514 fstring pdc_name;
6515 struct dcerpc_binding_handle *b;
6517 if (c->display_usage) {
6518 d_printf( "%s\n"
6519 "net rpc trustdom vampire\n"
6520 " %s\n",
6521 _("Usage:"),
6522 _("Vampire trust relationship from remote server"));
6523 return 0;
6527 * Listing trusted domains (stored in secrets.tdb, if local)
6530 mem_ctx = talloc_init("trust relationships vampire");
6533 * set domain and pdc name to local samba server (default)
6534 * or to remote one given in command line
6537 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6538 domain_name = c->opt_workgroup;
6539 c->opt_target_workgroup = c->opt_workgroup;
6540 } else {
6541 fstrcpy(pdc_name, lp_netbios_name());
6542 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6543 c->opt_target_workgroup = domain_name;
6546 /* open \PIPE\lsarpc and open policy handle */
6547 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6548 if (!NT_STATUS_IS_OK(nt_status)) {
6549 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6550 nt_errstr(nt_status)));
6551 talloc_destroy(mem_ctx);
6552 return -1;
6555 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6556 &pipe_hnd);
6557 if (!NT_STATUS_IS_OK(nt_status)) {
6558 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6559 nt_errstr(nt_status) ));
6560 cli_shutdown(cli);
6561 talloc_destroy(mem_ctx);
6562 return -1;
6565 b = pipe_hnd->binding_handle;
6567 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6568 &connect_hnd);
6569 if (NT_STATUS_IS_ERR(nt_status)) {
6570 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6571 nt_errstr(nt_status)));
6572 cli_shutdown(cli);
6573 talloc_destroy(mem_ctx);
6574 return -1;
6577 /* query info level 5 to obtain sid of a domain being queried */
6578 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6579 &connect_hnd,
6580 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6581 &info,
6582 &result);
6584 if (NT_STATUS_IS_ERR(nt_status)) {
6585 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6586 nt_errstr(nt_status)));
6587 cli_shutdown(cli);
6588 talloc_destroy(mem_ctx);
6589 return -1;
6591 if (NT_STATUS_IS_ERR(result)) {
6592 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6593 nt_errstr(result)));
6594 cli_shutdown(cli);
6595 talloc_destroy(mem_ctx);
6596 return -1;
6600 * Keep calling LsaEnumTrustdom over opened pipe until
6601 * the end of enumeration is reached
6604 d_printf(_("Vampire trusted domains:\n\n"));
6606 do {
6607 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6608 &connect_hnd,
6609 &enum_ctx,
6610 &dom_list,
6611 (uint32_t)-1,
6612 &result);
6613 if (NT_STATUS_IS_ERR(nt_status)) {
6614 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6615 nt_errstr(nt_status)));
6616 cli_shutdown(cli);
6617 talloc_destroy(mem_ctx);
6618 return -1;
6620 if (NT_STATUS_IS_ERR(result)) {
6621 nt_status = result;
6622 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6623 nt_errstr(result)));
6624 cli_shutdown(cli);
6625 talloc_destroy(mem_ctx);
6626 return -1;
6630 for (i = 0; i < dom_list.count; i++) {
6632 print_trusted_domain(dom_list.domains[i].sid,
6633 dom_list.domains[i].name.string);
6635 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6636 *dom_list.domains[i].sid,
6637 dom_list.domains[i].name.string);
6638 if (!NT_STATUS_IS_OK(nt_status)) {
6639 cli_shutdown(cli);
6640 talloc_destroy(mem_ctx);
6641 return -1;
6646 * in case of no trusted domains say something rather
6647 * than just display blank line
6649 if (!dom_list.count) d_printf(_("none\n"));
6651 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6653 /* close this connection before doing next one */
6654 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6655 if (NT_STATUS_IS_ERR(nt_status)) {
6656 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6657 nt_errstr(nt_status)));
6658 cli_shutdown(cli);
6659 talloc_destroy(mem_ctx);
6660 return -1;
6663 /* close lsarpc pipe and connection to IPC$ */
6664 cli_shutdown(cli);
6666 talloc_destroy(mem_ctx);
6667 return 0;
6670 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6672 /* common variables */
6673 TALLOC_CTX* mem_ctx;
6674 struct cli_state *cli = NULL, *remote_cli = NULL;
6675 struct rpc_pipe_client *pipe_hnd = NULL;
6676 NTSTATUS nt_status, result;
6677 const char *domain_name = NULL;
6678 struct dom_sid *queried_dom_sid;
6679 int ascii_dom_name_len;
6680 struct policy_handle connect_hnd;
6681 union lsa_PolicyInformation *info = NULL;
6682 struct dcerpc_binding_handle *b = NULL;
6684 /* trusted domains listing variables */
6685 unsigned int num_domains, enum_ctx = 0;
6686 int i;
6687 struct lsa_DomainList dom_list;
6688 fstring pdc_name;
6689 bool found_domain;
6691 /* trusting domains listing variables */
6692 struct policy_handle domain_hnd;
6693 struct samr_SamArray *trusts = NULL;
6695 if (c->display_usage) {
6696 d_printf( "%s\n"
6697 "net rpc trustdom list\n"
6698 " %s\n",
6699 _("Usage:"),
6700 _("List incoming and outgoing trust relationships"));
6701 return 0;
6705 * Listing trusted domains (stored in secrets.tdb, if local)
6708 mem_ctx = talloc_init("trust relationships listing");
6711 * set domain and pdc name to local samba server (default)
6712 * or to remote one given in command line
6715 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6716 domain_name = c->opt_workgroup;
6717 c->opt_target_workgroup = c->opt_workgroup;
6718 } else {
6719 fstrcpy(pdc_name, lp_netbios_name());
6720 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6721 c->opt_target_workgroup = domain_name;
6724 /* open \PIPE\lsarpc and open policy handle */
6725 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6726 if (!NT_STATUS_IS_OK(nt_status)) {
6727 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6728 nt_errstr(nt_status)));
6729 talloc_destroy(mem_ctx);
6730 return -1;
6733 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6734 &pipe_hnd);
6735 if (!NT_STATUS_IS_OK(nt_status)) {
6736 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6737 nt_errstr(nt_status) ));
6738 cli_shutdown(cli);
6739 talloc_destroy(mem_ctx);
6740 return -1;
6743 b = pipe_hnd->binding_handle;
6745 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6746 &connect_hnd);
6747 if (NT_STATUS_IS_ERR(nt_status)) {
6748 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6749 nt_errstr(nt_status)));
6750 cli_shutdown(cli);
6751 talloc_destroy(mem_ctx);
6752 return -1;
6755 /* query info level 5 to obtain sid of a domain being queried */
6756 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6757 &connect_hnd,
6758 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6759 &info,
6760 &result);
6762 if (NT_STATUS_IS_ERR(nt_status)) {
6763 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6764 nt_errstr(nt_status)));
6765 cli_shutdown(cli);
6766 talloc_destroy(mem_ctx);
6767 return -1;
6769 if (NT_STATUS_IS_ERR(result)) {
6770 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6771 nt_errstr(result)));
6772 cli_shutdown(cli);
6773 talloc_destroy(mem_ctx);
6774 return -1;
6777 queried_dom_sid = info->account_domain.sid;
6780 * Keep calling LsaEnumTrustdom over opened pipe until
6781 * the end of enumeration is reached
6784 d_printf(_("Trusted domains list:\n\n"));
6786 found_domain = false;
6788 do {
6789 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6790 &connect_hnd,
6791 &enum_ctx,
6792 &dom_list,
6793 (uint32_t)-1,
6794 &result);
6795 if (NT_STATUS_IS_ERR(nt_status)) {
6796 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6797 nt_errstr(nt_status)));
6798 cli_shutdown(cli);
6799 talloc_destroy(mem_ctx);
6800 return -1;
6802 if (NT_STATUS_IS_ERR(result)) {
6803 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6804 nt_errstr(result)));
6805 cli_shutdown(cli);
6806 talloc_destroy(mem_ctx);
6807 return -1;
6811 for (i = 0; i < dom_list.count; i++) {
6812 print_trusted_domain(dom_list.domains[i].sid,
6813 dom_list.domains[i].name.string);
6814 found_domain = true;
6818 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6821 * in case of no trusted domains say something rather
6822 * than just display blank line
6824 if (!found_domain) {
6825 d_printf(_("none\n"));
6828 /* close this connection before doing next one */
6829 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6830 if (NT_STATUS_IS_ERR(nt_status)) {
6831 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6832 nt_errstr(nt_status)));
6833 cli_shutdown(cli);
6834 talloc_destroy(mem_ctx);
6835 return -1;
6838 TALLOC_FREE(pipe_hnd);
6841 * Listing trusting domains (stored in passdb backend, if local)
6844 d_printf(_("\nTrusting domains list:\n\n"));
6847 * Open \PIPE\samr and get needed policy handles
6849 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6850 &pipe_hnd);
6851 if (!NT_STATUS_IS_OK(nt_status)) {
6852 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6853 cli_shutdown(cli);
6854 talloc_destroy(mem_ctx);
6855 return -1;
6858 b = pipe_hnd->binding_handle;
6860 /* SamrConnect2 */
6861 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
6862 pipe_hnd->desthost,
6863 SAMR_ACCESS_LOOKUP_DOMAIN,
6864 &connect_hnd,
6865 &result);
6866 if (!NT_STATUS_IS_OK(nt_status)) {
6867 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6868 nt_errstr(nt_status)));
6869 cli_shutdown(cli);
6870 talloc_destroy(mem_ctx);
6871 return -1;
6873 if (!NT_STATUS_IS_OK(result)) {
6874 nt_status = result;
6875 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6876 nt_errstr(result)));
6877 cli_shutdown(cli);
6878 talloc_destroy(mem_ctx);
6879 return -1;
6882 /* SamrOpenDomain - we have to open domain policy handle in order to be
6883 able to enumerate accounts*/
6884 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
6885 &connect_hnd,
6886 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6887 queried_dom_sid,
6888 &domain_hnd,
6889 &result);
6890 if (!NT_STATUS_IS_OK(nt_status)) {
6891 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6892 nt_errstr(nt_status)));
6893 cli_shutdown(cli);
6894 talloc_destroy(mem_ctx);
6895 return -1;
6897 if (!NT_STATUS_IS_OK(result)) {
6898 nt_status = result;
6899 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6900 nt_errstr(result)));
6901 cli_shutdown(cli);
6902 talloc_destroy(mem_ctx);
6903 return -1;
6907 * perform actual enumeration
6910 found_domain = false;
6912 enum_ctx = 0; /* reset enumeration context from last enumeration */
6913 do {
6915 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
6916 &domain_hnd,
6917 &enum_ctx,
6918 ACB_DOMTRUST,
6919 &trusts,
6920 0xffff,
6921 &num_domains,
6922 &result);
6923 if (NT_STATUS_IS_ERR(nt_status)) {
6924 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6925 nt_errstr(nt_status)));
6926 cli_shutdown(cli);
6927 talloc_destroy(mem_ctx);
6928 return -1;
6930 if (NT_STATUS_IS_ERR(result)) {
6931 nt_status = result;
6932 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6933 nt_errstr(result)));
6934 cli_shutdown(cli);
6935 talloc_destroy(mem_ctx);
6936 return -1;
6939 for (i = 0; i < num_domains; i++) {
6941 char *str = discard_const_p(char, trusts->entries[i].name.string);
6943 found_domain = true;
6946 * get each single domain's sid (do we _really_ need this ?):
6947 * 1) connect to domain's pdc
6948 * 2) query the pdc for domain's sid
6951 /* get rid of '$' tail */
6952 ascii_dom_name_len = strlen(str);
6953 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6954 str[ascii_dom_name_len - 1] = '\0';
6956 /* set opt_* variables to remote domain */
6957 strupper_m(str);
6958 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6959 c->opt_target_workgroup = c->opt_workgroup;
6961 d_printf("%-20s", str);
6963 /* connect to remote domain controller */
6964 nt_status = net_make_ipc_connection(c,
6965 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6966 &remote_cli);
6967 if (NT_STATUS_IS_OK(nt_status)) {
6968 /* query for domain's sid */
6969 if (run_rpc_command(
6970 c, remote_cli,
6971 &ndr_table_lsarpc.syntax_id, 0,
6972 rpc_query_domain_sid, argc,
6973 argv))
6974 d_printf(_("strange - couldn't get domain's sid\n"));
6976 cli_shutdown(remote_cli);
6978 } else {
6979 d_fprintf(stderr, _("domain controller is not "
6980 "responding: %s\n"),
6981 nt_errstr(nt_status));
6982 d_printf(_("couldn't get domain's sid\n"));
6986 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
6988 if (!found_domain) {
6989 d_printf("none\n");
6992 /* close opened samr and domain policy handles */
6993 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
6994 if (!NT_STATUS_IS_OK(nt_status)) {
6995 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6998 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
6999 if (!NT_STATUS_IS_OK(nt_status)) {
7000 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
7003 /* close samr pipe and connection to IPC$ */
7004 cli_shutdown(cli);
7006 talloc_destroy(mem_ctx);
7007 return 0;
7011 * Entrypoint for 'net rpc trustdom' code.
7013 * @param argc Standard argc.
7014 * @param argv Standard argv without initial components.
7016 * @return Integer status (0 means success).
7019 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7021 struct functable func[] = {
7023 "add",
7024 rpc_trustdom_add,
7025 NET_TRANSPORT_RPC,
7026 N_("Add trusting domain's account"),
7027 N_("net rpc trustdom add\n"
7028 " Add trusting domain's account")
7031 "del",
7032 rpc_trustdom_del,
7033 NET_TRANSPORT_RPC,
7034 N_("Remove trusting domain's account"),
7035 N_("net rpc trustdom del\n"
7036 " Remove trusting domain's account")
7039 "establish",
7040 rpc_trustdom_establish,
7041 NET_TRANSPORT_RPC,
7042 N_("Establish outgoing trust relationship"),
7043 N_("net rpc trustdom establish\n"
7044 " Establish outgoing trust relationship")
7047 "revoke",
7048 rpc_trustdom_revoke,
7049 NET_TRANSPORT_RPC,
7050 N_("Revoke outgoing trust relationship"),
7051 N_("net rpc trustdom revoke\n"
7052 " Revoke outgoing trust relationship")
7055 "list",
7056 rpc_trustdom_list,
7057 NET_TRANSPORT_RPC,
7058 N_("List in- and outgoing domain trusts"),
7059 N_("net rpc trustdom list\n"
7060 " List in- and outgoing domain trusts")
7063 "vampire",
7064 rpc_trustdom_vampire,
7065 NET_TRANSPORT_RPC,
7066 N_("Vampire trusts from remote server"),
7067 N_("net rpc trustdom vampire\n"
7068 " Vampire trusts from remote server")
7070 {NULL, NULL, 0, NULL, NULL}
7073 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7077 * Check if a server will take rpc commands
7078 * @param flags Type of server to connect to (PDC, DMB, localhost)
7079 * if the host is not explicitly specified
7080 * @return bool (true means rpc supported)
7082 bool net_rpc_check(struct net_context *c, unsigned flags)
7084 struct cli_state *cli;
7085 bool ret = false;
7086 struct sockaddr_storage server_ss;
7087 char *server_name = NULL;
7088 NTSTATUS status;
7090 /* flags (i.e. server type) may depend on command */
7091 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7092 return false;
7094 status = cli_connect_nb(server_name, &server_ss, 0, 0x20,
7095 lp_netbios_name(), Undefined, &cli);
7096 if (!NT_STATUS_IS_OK(status)) {
7097 return false;
7099 status = cli_negprot(cli);
7100 if (!NT_STATUS_IS_OK(status))
7101 goto done;
7102 if (cli_state_protocol(cli) < PROTOCOL_NT1)
7103 goto done;
7105 ret = true;
7106 done:
7107 cli_shutdown(cli);
7108 return ret;
7111 /* dump sam database via samsync rpc calls */
7112 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
7113 if (c->display_usage) {
7114 d_printf( "%s\n"
7115 "net rpc samdump\n"
7116 " %s\n",
7117 _("Usage:"),
7118 _("Dump remote SAM database"));
7119 return 0;
7122 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
7123 NET_FLAGS_ANONYMOUS,
7124 rpc_samdump_internals, argc, argv);
7127 /* syncronise sam database via samsync rpc calls */
7128 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7130 struct functable func[] = {
7132 "ldif",
7133 rpc_vampire_ldif,
7134 NET_TRANSPORT_RPC,
7135 N_("Dump remote SAM database to ldif"),
7136 N_("net rpc vampire ldif\n"
7137 " Dump remote SAM database to LDIF file or "
7138 "stdout")
7141 "keytab",
7142 rpc_vampire_keytab,
7143 NET_TRANSPORT_RPC,
7144 N_("Dump remote SAM database to Kerberos Keytab"),
7145 N_("net rpc vampire keytab\n"
7146 " Dump remote SAM database to Kerberos keytab "
7147 "file")
7150 "passdb",
7151 rpc_vampire_passdb,
7152 NET_TRANSPORT_RPC,
7153 N_("Dump remote SAM database to passdb"),
7154 N_("net rpc vampire passdb\n"
7155 " Dump remote SAM database to passdb")
7158 {NULL, NULL, 0, NULL, NULL}
7161 if (argc == 0) {
7162 if (c->display_usage) {
7163 d_printf( "%s\n"
7164 "net rpc vampire\n"
7165 " %s\n",
7166 _("Usage:"),
7167 _("Vampire remote SAM database"));
7168 return 0;
7171 return rpc_vampire_passdb(c, argc, argv);
7174 return net_run_function(c, argc, argv, "net rpc vampire", func);
7178 * Migrate everything from a print server.
7180 * @param c A net_context structure.
7181 * @param argc Standard main() style argc.
7182 * @param argv Standard main() style argv. Initial components are already
7183 * stripped.
7185 * @return A shell status integer (0 for success).
7187 * The order is important !
7188 * To successfully add drivers the print queues have to exist !
7189 * Applying ACLs should be the last step, because you're easily locked out.
7192 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7193 const char **argv)
7195 int ret;
7197 if (c->display_usage) {
7198 d_printf( "%s\n"
7199 "net rpc printer migrate all\n"
7200 " %s\n",
7201 _("Usage:"),
7202 _("Migrate everything from a print server"));
7203 return 0;
7206 if (!c->opt_host) {
7207 d_printf(_("no server to migrate\n"));
7208 return -1;
7211 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7212 rpc_printer_migrate_printers_internals, argc,
7213 argv);
7214 if (ret)
7215 return ret;
7217 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7218 rpc_printer_migrate_drivers_internals, argc,
7219 argv);
7220 if (ret)
7221 return ret;
7223 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7224 rpc_printer_migrate_forms_internals, argc, argv);
7225 if (ret)
7226 return ret;
7228 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7229 rpc_printer_migrate_settings_internals, argc,
7230 argv);
7231 if (ret)
7232 return ret;
7234 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7235 rpc_printer_migrate_security_internals, argc,
7236 argv);
7241 * Migrate print drivers from a print server.
7243 * @param c A net_context structure.
7244 * @param argc Standard main() style argc.
7245 * @param argv Standard main() style argv. Initial components are already
7246 * stripped.
7248 * @return A shell status integer (0 for success).
7250 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7251 const char **argv)
7253 if (c->display_usage) {
7254 d_printf( "%s\n"
7255 "net rpc printer migrate drivers\n"
7256 " %s\n",
7257 _("Usage:"),
7258 _("Migrate print-drivers from a print-server"));
7259 return 0;
7262 if (!c->opt_host) {
7263 d_printf(_("no server to migrate\n"));
7264 return -1;
7267 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7268 rpc_printer_migrate_drivers_internals,
7269 argc, argv);
7273 * Migrate print-forms from a print-server.
7275 * @param c A net_context structure.
7276 * @param argc Standard main() style argc.
7277 * @param argv Standard main() style argv. Initial components are already
7278 * stripped.
7280 * @return A shell status integer (0 for success).
7282 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7283 const char **argv)
7285 if (c->display_usage) {
7286 d_printf( "%s\n"
7287 "net rpc printer migrate forms\n"
7288 " %s\n",
7289 _("Usage:"),
7290 _("Migrate print-forms from a print-server"));
7291 return 0;
7294 if (!c->opt_host) {
7295 d_printf(_("no server to migrate\n"));
7296 return -1;
7299 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7300 rpc_printer_migrate_forms_internals,
7301 argc, argv);
7305 * Migrate printers from a print-server.
7307 * @param c A net_context structure.
7308 * @param argc Standard main() style argc.
7309 * @param argv Standard main() style argv. Initial components are already
7310 * stripped.
7312 * @return A shell status integer (0 for success).
7314 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7315 const char **argv)
7317 if (c->display_usage) {
7318 d_printf( "%s\n"
7319 "net rpc printer migrate printers\n"
7320 " %s\n",
7321 _("Usage:"),
7322 _("Migrate printers from a print-server"));
7323 return 0;
7326 if (!c->opt_host) {
7327 d_printf(_("no server to migrate\n"));
7328 return -1;
7331 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7332 rpc_printer_migrate_printers_internals,
7333 argc, argv);
7337 * Migrate printer-ACLs from a print-server
7339 * @param c A net_context structure.
7340 * @param argc Standard main() style argc.
7341 * @param argv Standard main() style argv. Initial components are already
7342 * stripped.
7344 * @return A shell status integer (0 for success).
7346 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7347 const char **argv)
7349 if (c->display_usage) {
7350 d_printf( "%s\n"
7351 "net rpc printer migrate security\n"
7352 " %s\n",
7353 _("Usage:"),
7354 _("Migrate printer-ACLs from a print-server"));
7355 return 0;
7358 if (!c->opt_host) {
7359 d_printf(_("no server to migrate\n"));
7360 return -1;
7363 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7364 rpc_printer_migrate_security_internals,
7365 argc, argv);
7369 * Migrate printer-settings from a print-server.
7371 * @param c A net_context structure.
7372 * @param argc Standard main() style argc.
7373 * @param argv Standard main() style argv. Initial components are already
7374 * stripped.
7376 * @return A shell status integer (0 for success).
7378 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7379 const char **argv)
7381 if (c->display_usage) {
7382 d_printf( "%s\n"
7383 "net rpc printer migrate settings\n"
7384 " %s\n",
7385 _("Usage:"),
7386 _("Migrate printer-settings from a "
7387 "print-server"));
7388 return 0;
7391 if (!c->opt_host) {
7392 d_printf(_("no server to migrate\n"));
7393 return -1;
7396 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7397 rpc_printer_migrate_settings_internals,
7398 argc, argv);
7402 * 'net rpc printer' entrypoint.
7404 * @param c A net_context structure.
7405 * @param argc Standard main() style argc.
7406 * @param argv Standard main() style argv. Initial components are already
7407 * stripped.
7410 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7413 /* ouch: when addriver and setdriver are called from within
7414 rpc_printer_migrate_drivers_internals, the printer-queue already
7415 *has* to exist */
7417 struct functable func[] = {
7419 "all",
7420 rpc_printer_migrate_all,
7421 NET_TRANSPORT_RPC,
7422 N_("Migrate all from remote to local print server"),
7423 N_("net rpc printer migrate all\n"
7424 " Migrate all from remote to local print server")
7427 "drivers",
7428 rpc_printer_migrate_drivers,
7429 NET_TRANSPORT_RPC,
7430 N_("Migrate drivers to local server"),
7431 N_("net rpc printer migrate drivers\n"
7432 " Migrate drivers to local server")
7435 "forms",
7436 rpc_printer_migrate_forms,
7437 NET_TRANSPORT_RPC,
7438 N_("Migrate froms to local server"),
7439 N_("net rpc printer migrate forms\n"
7440 " Migrate froms to local server")
7443 "printers",
7444 rpc_printer_migrate_printers,
7445 NET_TRANSPORT_RPC,
7446 N_("Migrate printers to local server"),
7447 N_("net rpc printer migrate printers\n"
7448 " Migrate printers to local server")
7451 "security",
7452 rpc_printer_migrate_security,
7453 NET_TRANSPORT_RPC,
7454 N_("Mirgate printer ACLs to local server"),
7455 N_("net rpc printer migrate security\n"
7456 " Mirgate printer ACLs to local server")
7459 "settings",
7460 rpc_printer_migrate_settings,
7461 NET_TRANSPORT_RPC,
7462 N_("Migrate printer settings to local server"),
7463 N_("net rpc printer migrate settings\n"
7464 " Migrate printer settings to local server")
7466 {NULL, NULL, 0, NULL, NULL}
7469 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7474 * List printers on a remote RPC server.
7476 * @param c A net_context structure.
7477 * @param argc Standard main() style argc.
7478 * @param argv Standard main() style argv. Initial components are already
7479 * stripped.
7481 * @return A shell status integer (0 for success).
7483 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7485 if (c->display_usage) {
7486 d_printf( "%s\n"
7487 "net rpc printer list\n"
7488 " %s\n",
7489 _("Usage:"),
7490 _("List printers on a remote RPC server"));
7491 return 0;
7494 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7495 rpc_printer_list_internals,
7496 argc, argv);
7500 * List printer-drivers on a remote RPC server.
7502 * @param c A net_context structure.
7503 * @param argc Standard main() style argc.
7504 * @param argv Standard main() style argv. Initial components are already
7505 * stripped.
7507 * @return A shell status integer (0 for success).
7509 static int rpc_printer_driver_list(struct net_context *c, int argc,
7510 const char **argv)
7512 if (c->display_usage) {
7513 d_printf( "%s\n"
7514 "net rpc printer driver\n"
7515 " %s\n",
7516 _("Usage:"),
7517 _("List printer-drivers on a remote RPC server"));
7518 return 0;
7521 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7522 rpc_printer_driver_list_internals,
7523 argc, argv);
7527 * Publish printer in ADS via MSRPC.
7529 * @param c A net_context structure.
7530 * @param argc Standard main() style argc.
7531 * @param argv Standard main() style argv. Initial components are already
7532 * stripped.
7534 * @return A shell status integer (0 for success).
7536 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7537 const char **argv)
7539 if (c->display_usage) {
7540 d_printf( "%s\n"
7541 "net rpc printer publish publish\n"
7542 " %s\n",
7543 _("Usage:"),
7544 _("Publish printer in ADS via MSRPC"));
7545 return 0;
7548 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7549 rpc_printer_publish_publish_internals,
7550 argc, argv);
7554 * Update printer in ADS via MSRPC.
7556 * @param c A net_context structure.
7557 * @param argc Standard main() style argc.
7558 * @param argv Standard main() style argv. Initial components are already
7559 * stripped.
7561 * @return A shell status integer (0 for success).
7563 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7565 if (c->display_usage) {
7566 d_printf( "%s\n"
7567 "net rpc printer publish update\n"
7568 " %s\n",
7569 _("Usage:"),
7570 _("Update printer in ADS via MSRPC"));
7571 return 0;
7574 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7575 rpc_printer_publish_update_internals,
7576 argc, argv);
7580 * UnPublish printer in ADS via MSRPC.
7582 * @param c A net_context structure.
7583 * @param argc Standard main() style argc.
7584 * @param argv Standard main() style argv. Initial components are already
7585 * stripped.
7587 * @return A shell status integer (0 for success).
7589 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7590 const char **argv)
7592 if (c->display_usage) {
7593 d_printf( "%s\n"
7594 "net rpc printer publish unpublish\n"
7595 " %s\n",
7596 _("Usage:\n"),
7597 _("UnPublish printer in ADS via MSRPC"));
7598 return 0;
7601 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7602 rpc_printer_publish_unpublish_internals,
7603 argc, argv);
7607 * List published printers via MSRPC.
7609 * @param c A net_context structure.
7610 * @param argc Standard main() style argc.
7611 * @param argv Standard main() style argv. Initial components are already
7612 * stripped.
7614 * @return A shell status integer (0 for success).
7616 static int rpc_printer_publish_list(struct net_context *c, int argc,
7617 const char **argv)
7619 if (c->display_usage) {
7620 d_printf( "%s\n"
7621 "net rpc printer publish list\n"
7622 " %s\n",
7623 _("Usage:"),
7624 _("List published printers via MSRPC"));
7625 return 0;
7628 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7629 rpc_printer_publish_list_internals,
7630 argc, argv);
7635 * Publish printer in ADS.
7637 * @param c A net_context structure.
7638 * @param argc Standard main() style argc.
7639 * @param argv Standard main() style argv. Initial components are already
7640 * stripped.
7642 * @return A shell status integer (0 for success).
7644 static int rpc_printer_publish(struct net_context *c, int argc,
7645 const char **argv)
7648 struct functable func[] = {
7650 "publish",
7651 rpc_printer_publish_publish,
7652 NET_TRANSPORT_RPC,
7653 N_("Publish printer in AD"),
7654 N_("net rpc printer publish publish\n"
7655 " Publish printer in AD")
7658 "update",
7659 rpc_printer_publish_update,
7660 NET_TRANSPORT_RPC,
7661 N_("Update printer in AD"),
7662 N_("net rpc printer publish update\n"
7663 " Update printer in AD")
7666 "unpublish",
7667 rpc_printer_publish_unpublish,
7668 NET_TRANSPORT_RPC,
7669 N_("Unpublish printer"),
7670 N_("net rpc printer publish unpublish\n"
7671 " Unpublish printer")
7674 "list",
7675 rpc_printer_publish_list,
7676 NET_TRANSPORT_RPC,
7677 N_("List published printers"),
7678 N_("net rpc printer publish list\n"
7679 " List published printers")
7681 {NULL, NULL, 0, NULL, NULL}
7684 if (argc == 0) {
7685 if (c->display_usage) {
7686 d_printf(_("Usage:\n"));
7687 d_printf(_("net rpc printer publish\n"
7688 " List published printers\n"
7689 " Alias of net rpc printer publish "
7690 "list\n"));
7691 net_display_usage_from_functable(func);
7692 return 0;
7694 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7695 rpc_printer_publish_list_internals,
7696 argc, argv);
7699 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7705 * Display rpc printer help page.
7707 * @param c A net_context structure.
7708 * @param argc Standard main() style argc.
7709 * @param argv Standard main() style argv. Initial components are already
7710 * stripped.
7712 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7714 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7715 "\tlists all printers on print-server\n\n"));
7716 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7717 "\tlists all printer-drivers on print-server\n\n"));
7718 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7719 "\tpublishes printer settings in Active Directory\n"
7720 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7721 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7722 "\n\tmigrates printers from remote to local server\n\n"));
7723 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7724 "\n\tmigrates printer-settings from remote to local server\n\n"));
7725 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7726 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7727 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7728 "\n\tmigrates printer-forms from remote to local server\n\n"));
7729 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7730 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7731 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7732 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7733 "\tremote to local print-server\n\n"));
7734 net_common_methods_usage(c, argc, argv);
7735 net_common_flags_usage(c, argc, argv);
7736 d_printf(_(
7737 "\t-v or --verbose\t\t\tgive verbose output\n"
7738 "\t --destination\t\tmigration target server (default: localhost)\n"));
7740 return -1;
7744 * 'net rpc printer' entrypoint.
7746 * @param c A net_context structure.
7747 * @param argc Standard main() style argc.
7748 * @param argv Standard main() style argv. Initial components are already
7749 * stripped.
7751 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7753 struct functable func[] = {
7755 "list",
7756 rpc_printer_list,
7757 NET_TRANSPORT_RPC,
7758 N_("List all printers on print server"),
7759 N_("net rpc printer list\n"
7760 " List all printers on print server")
7763 "migrate",
7764 rpc_printer_migrate,
7765 NET_TRANSPORT_RPC,
7766 N_("Migrate printer to local server"),
7767 N_("net rpc printer migrate\n"
7768 " Migrate printer to local server")
7771 "driver",
7772 rpc_printer_driver_list,
7773 NET_TRANSPORT_RPC,
7774 N_("List printer drivers"),
7775 N_("net rpc printer driver\n"
7776 " List printer drivers")
7779 "publish",
7780 rpc_printer_publish,
7781 NET_TRANSPORT_RPC,
7782 N_("Publish printer in AD"),
7783 N_("net rpc printer publish\n"
7784 " Publish printer in AD")
7786 {NULL, NULL, 0, NULL, NULL}
7789 if (argc == 0) {
7790 if (c->display_usage) {
7791 d_printf(_("Usage:\n"));
7792 d_printf(_("net rpc printer\n"
7793 " List printers\n"));
7794 net_display_usage_from_functable(func);
7795 return 0;
7797 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7798 rpc_printer_list_internals,
7799 argc, argv);
7802 return net_run_function(c, argc, argv, "net rpc printer", func);
7806 * 'net rpc' entrypoint.
7808 * @param c A net_context structure.
7809 * @param argc Standard main() style argc.
7810 * @param argv Standard main() style argv. Initial components are already
7811 * stripped.
7814 int net_rpc(struct net_context *c, int argc, const char **argv)
7816 NET_API_STATUS status;
7818 struct functable func[] = {
7820 "audit",
7821 net_rpc_audit,
7822 NET_TRANSPORT_RPC,
7823 N_("Modify global audit settings"),
7824 N_("net rpc audit\n"
7825 " Modify global audit settings")
7828 "info",
7829 net_rpc_info,
7830 NET_TRANSPORT_RPC,
7831 N_("Show basic info about a domain"),
7832 N_("net rpc info\n"
7833 " Show basic info about a domain")
7836 "join",
7837 net_rpc_join,
7838 NET_TRANSPORT_RPC,
7839 N_("Join a domain"),
7840 N_("net rpc join\n"
7841 " Join a domain")
7844 "oldjoin",
7845 net_rpc_oldjoin,
7846 NET_TRANSPORT_RPC,
7847 N_("Join a domain created in server manager"),
7848 N_("net rpc oldjoin\n"
7849 " Join a domain created in server manager")
7852 "testjoin",
7853 net_rpc_testjoin,
7854 NET_TRANSPORT_RPC,
7855 N_("Test that a join is valid"),
7856 N_("net rpc testjoin\n"
7857 " Test that a join is valid")
7860 "user",
7861 net_rpc_user,
7862 NET_TRANSPORT_RPC,
7863 N_("List/modify users"),
7864 N_("net rpc user\n"
7865 " List/modify users")
7868 "password",
7869 rpc_user_password,
7870 NET_TRANSPORT_RPC,
7871 N_("Change a user password"),
7872 N_("net rpc password\n"
7873 " Change a user password\n"
7874 " Alias for net rpc user password")
7877 "group",
7878 net_rpc_group,
7879 NET_TRANSPORT_RPC,
7880 N_("List/modify groups"),
7881 N_("net rpc group\n"
7882 " List/modify groups")
7885 "share",
7886 net_rpc_share,
7887 NET_TRANSPORT_RPC,
7888 N_("List/modify shares"),
7889 N_("net rpc share\n"
7890 " List/modify shares")
7893 "file",
7894 net_rpc_file,
7895 NET_TRANSPORT_RPC,
7896 N_("List open files"),
7897 N_("net rpc file\n"
7898 " List open files")
7901 "printer",
7902 net_rpc_printer,
7903 NET_TRANSPORT_RPC,
7904 N_("List/modify printers"),
7905 N_("net rpc printer\n"
7906 " List/modify printers")
7909 "changetrustpw",
7910 net_rpc_changetrustpw,
7911 NET_TRANSPORT_RPC,
7912 N_("Change trust account password"),
7913 N_("net rpc changetrustpw\n"
7914 " Change trust account password")
7917 "trustdom",
7918 rpc_trustdom,
7919 NET_TRANSPORT_RPC,
7920 N_("Modify domain trusts"),
7921 N_("net rpc trustdom\n"
7922 " Modify domain trusts")
7925 "abortshutdown",
7926 rpc_shutdown_abort,
7927 NET_TRANSPORT_RPC,
7928 N_("Abort a remote shutdown"),
7929 N_("net rpc abortshutdown\n"
7930 " Abort a remote shutdown")
7933 "shutdown",
7934 rpc_shutdown,
7935 NET_TRANSPORT_RPC,
7936 N_("Shutdown a remote server"),
7937 N_("net rpc shutdown\n"
7938 " Shutdown a remote server")
7941 "samdump",
7942 rpc_samdump,
7943 NET_TRANSPORT_RPC,
7944 N_("Dump SAM data of remote NT PDC"),
7945 N_("net rpc samdump\n"
7946 " Dump SAM data of remote NT PDC")
7949 "vampire",
7950 rpc_vampire,
7951 NET_TRANSPORT_RPC,
7952 N_("Sync a remote NT PDC's data into local passdb"),
7953 N_("net rpc vampire\n"
7954 " Sync a remote NT PDC's data into local passdb")
7957 "getsid",
7958 net_rpc_getsid,
7959 NET_TRANSPORT_RPC,
7960 N_("Fetch the domain sid into local secrets.tdb"),
7961 N_("net rpc getsid\n"
7962 " Fetch the domain sid into local secrets.tdb")
7965 "rights",
7966 net_rpc_rights,
7967 NET_TRANSPORT_RPC,
7968 N_("Manage privileges assigned to SID"),
7969 N_("net rpc rights\n"
7970 " Manage privileges assigned to SID")
7973 "service",
7974 net_rpc_service,
7975 NET_TRANSPORT_RPC,
7976 N_("Start/stop/query remote services"),
7977 N_("net rpc service\n"
7978 " Start/stop/query remote services")
7981 "registry",
7982 net_rpc_registry,
7983 NET_TRANSPORT_RPC,
7984 N_("Manage registry hives"),
7985 N_("net rpc registry\n"
7986 " Manage registry hives")
7989 "shell",
7990 net_rpc_shell,
7991 NET_TRANSPORT_RPC,
7992 N_("Open interactive shell on remote server"),
7993 N_("net rpc shell\n"
7994 " Open interactive shell on remote server")
7997 "trust",
7998 net_rpc_trust,
7999 NET_TRANSPORT_RPC,
8000 N_("Manage trusts"),
8001 N_("net rpc trust\n"
8002 " Manage trusts")
8004 {NULL, NULL, 0, NULL, NULL}
8007 status = libnetapi_net_init(&c->netapi_ctx);
8008 if (status != 0) {
8009 return -1;
8011 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
8012 libnetapi_set_password(c->netapi_ctx, c->opt_password);
8013 if (c->opt_kerberos) {
8014 libnetapi_set_use_kerberos(c->netapi_ctx);
8016 if (c->opt_ccache) {
8017 libnetapi_set_use_ccache(c->netapi_ctx);
8020 return net_run_function(c, argc, argv, "net rpc", func);