s3-dcerpc: Pull packet in the caller, before validation
[Samba.git] / source3 / utils / net_rpc.c
blob56eb42e0ed6b0f7ccc8a266fdd62a2a8b547ce28
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 "../libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/cli_samr.h"
27 #include "rpc_client/cli_samr.h"
28 #include "rpc_client/init_samr.h"
29 #include "../librpc/gen_ndr/cli_lsa.h"
30 #include "rpc_client/cli_lsarpc.h"
31 #include "../librpc/gen_ndr/cli_netlogon.h"
32 #include "../librpc/gen_ndr/cli_srvsvc.h"
33 #include "../librpc/gen_ndr/cli_spoolss.h"
34 #include "../librpc/gen_ndr/cli_initshutdown.h"
35 #include "../librpc/gen_ndr/cli_winreg.h"
36 #include "secrets.h"
37 #include "lib/netapi/netapi.h"
39 static int net_mode_share;
40 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
42 /**
43 * @file net_rpc.c
45 * @brief RPC based subcommands for the 'net' utility.
47 * This file should contain much of the functionality that used to
48 * be found in rpcclient, execpt that the commands should change
49 * less often, and the fucntionality should be sane (the user is not
50 * expected to know a rid/sid before they conduct an operation etc.)
52 * @todo Perhaps eventually these should be split out into a number
53 * of files, as this could get quite big.
54 **/
57 /**
58 * Many of the RPC functions need the domain sid. This function gets
59 * it at the start of every run
61 * @param cli A cli_state already connected to the remote machine
63 * @return The Domain SID of the remote machine.
64 **/
66 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
67 struct dom_sid **domain_sid,
68 const char **domain_name)
70 struct rpc_pipe_client *lsa_pipe = NULL;
71 struct policy_handle pol;
72 NTSTATUS result = NT_STATUS_OK;
73 union lsa_PolicyInformation *info = NULL;
75 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
76 &lsa_pipe);
77 if (!NT_STATUS_IS_OK(result)) {
78 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
79 return result;
82 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
83 SEC_FLAG_MAXIMUM_ALLOWED,
84 &pol);
85 if (!NT_STATUS_IS_OK(result)) {
86 d_fprintf(stderr, "open_policy %s: %s\n",
87 _("failed"),
88 nt_errstr(result));
89 return result;
92 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
93 &pol,
94 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
95 &info);
96 if (!NT_STATUS_IS_OK(result)) {
97 d_fprintf(stderr, "lsaquery %s: %s\n",
98 _("failed"),
99 nt_errstr(result));
100 return result;
103 *domain_name = info->account_domain.name.string;
104 *domain_sid = info->account_domain.sid;
106 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
107 TALLOC_FREE(lsa_pipe);
109 return NT_STATUS_OK;
113 * Run a single RPC command, from start to finish.
115 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
116 * @param conn_flag a NET_FLAG_ combination. Passed to
117 * net_make_ipc_connection.
118 * @param argc Standard main() style argc.
119 * @param argv Standard main() style argv. Initial components are already
120 * stripped.
121 * @return A shell status integer (0 for success).
124 int run_rpc_command(struct net_context *c,
125 struct cli_state *cli_arg,
126 const struct ndr_syntax_id *interface,
127 int conn_flags,
128 rpc_command_fn fn,
129 int argc,
130 const char **argv)
132 struct cli_state *cli = NULL;
133 struct rpc_pipe_client *pipe_hnd = NULL;
134 TALLOC_CTX *mem_ctx;
135 NTSTATUS nt_status;
136 struct dom_sid *domain_sid;
137 const char *domain_name;
138 int ret = -1;
140 /* make use of cli_state handed over as an argument, if possible */
141 if (!cli_arg) {
142 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
143 if (!NT_STATUS_IS_OK(nt_status)) {
144 DEBUG(1, ("failed to make ipc connection: %s\n",
145 nt_errstr(nt_status)));
146 return -1;
148 } else {
149 cli = cli_arg;
152 if (!cli) {
153 return -1;
156 /* Create mem_ctx */
158 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
159 DEBUG(0, ("talloc_init() failed\n"));
160 goto fail;
163 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
164 &domain_name);
165 if (!NT_STATUS_IS_OK(nt_status)) {
166 goto fail;
169 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
170 if (lp_client_schannel()
171 && (ndr_syntax_id_equal(interface,
172 &ndr_table_netlogon.syntax_id))) {
173 /* Always try and create an schannel netlogon pipe. */
174 nt_status = cli_rpc_pipe_open_schannel(
175 cli, interface, NCACN_NP,
176 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
177 &pipe_hnd);
178 if (!NT_STATUS_IS_OK(nt_status)) {
179 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
180 nt_errstr(nt_status) ));
181 goto fail;
183 } else {
184 if (conn_flags & NET_FLAGS_SEAL) {
185 nt_status = cli_rpc_pipe_open_ntlmssp(
186 cli, interface,
187 (conn_flags & NET_FLAGS_TCP) ?
188 NCACN_IP_TCP : NCACN_NP,
189 DCERPC_AUTH_LEVEL_PRIVACY,
190 lp_workgroup(), c->opt_user_name,
191 c->opt_password, &pipe_hnd);
192 } else {
193 nt_status = cli_rpc_pipe_open_noauth(
194 cli, interface,
195 &pipe_hnd);
197 if (!NT_STATUS_IS_OK(nt_status)) {
198 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
199 get_pipe_name_from_syntax(
200 talloc_tos(), interface),
201 nt_errstr(nt_status) ));
202 goto fail;
207 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
209 if (!NT_STATUS_IS_OK(nt_status)) {
210 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
211 } else {
212 ret = 0;
213 DEBUG(5, ("rpc command function succedded\n"));
216 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
217 if (pipe_hnd) {
218 TALLOC_FREE(pipe_hnd);
222 fail:
223 /* close the connection only if it was opened here */
224 if (!cli_arg) {
225 cli_shutdown(cli);
228 talloc_destroy(mem_ctx);
229 return ret;
233 * Force a change of the trust acccount password.
235 * All parameters are provided by the run_rpc_command function, except for
236 * argc, argv which are passed through.
238 * @param domain_sid The domain sid acquired from the remote server.
239 * @param cli A cli_state connected to the server.
240 * @param mem_ctx Talloc context, destroyed on completion of the function.
241 * @param argc Standard main() style argc.
242 * @param argv Standard main() style argv. Initial components are already
243 * stripped.
245 * @return Normal NTSTATUS return.
248 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
249 const struct dom_sid *domain_sid,
250 const char *domain_name,
251 struct cli_state *cli,
252 struct rpc_pipe_client *pipe_hnd,
253 TALLOC_CTX *mem_ctx,
254 int argc,
255 const char **argv)
257 NTSTATUS status;
259 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
260 if (!NT_STATUS_IS_OK(status)) {
261 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
262 nt_errstr(status));
263 return status;
266 return NT_STATUS_OK;
270 * Force a change of the trust acccount password.
272 * @param argc Standard main() style argc.
273 * @param argv Standard main() style argv. Initial components are already
274 * stripped.
276 * @return A shell status integer (0 for success).
279 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
281 if (c->display_usage) {
282 d_printf( "%s\n"
283 "net rpc changetrustpw\n"
284 " %s\n",
285 _("Usage:"),
286 _("Change the machine trust password"));
287 return 0;
290 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
291 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
292 rpc_changetrustpw_internals,
293 argc, argv);
297 * Join a domain, the old way.
299 * This uses 'machinename' as the inital password, and changes it.
301 * The password should be created with 'server manager' or equiv first.
303 * All parameters are provided by the run_rpc_command function, except for
304 * argc, argv which are passed through.
306 * @param domain_sid The domain sid acquired from the remote server.
307 * @param cli A cli_state connected to the server.
308 * @param mem_ctx Talloc context, destroyed on completion of the function.
309 * @param argc Standard main() style argc.
310 * @param argv Standard main() style argv. Initial components are already
311 * stripped.
313 * @return Normal NTSTATUS return.
316 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
317 const struct dom_sid *domain_sid,
318 const char *domain_name,
319 struct cli_state *cli,
320 struct rpc_pipe_client *pipe_hnd,
321 TALLOC_CTX *mem_ctx,
322 int argc,
323 const char **argv)
326 fstring trust_passwd;
327 unsigned char orig_trust_passwd_hash[16];
328 NTSTATUS result;
329 enum netr_SchannelType sec_channel_type;
331 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
332 &pipe_hnd);
333 if (!NT_STATUS_IS_OK(result)) {
334 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
335 "error was %s\n",
336 cli->desthost,
337 nt_errstr(result) ));
338 return result;
342 check what type of join - if the user want's to join as
343 a BDC, the server must agree that we are a BDC.
345 if (argc >= 0) {
346 sec_channel_type = get_sec_channel_type(argv[0]);
347 } else {
348 sec_channel_type = get_sec_channel_type(NULL);
351 fstrcpy(trust_passwd, global_myname());
352 strlower_m(trust_passwd);
355 * Machine names can be 15 characters, but the max length on
356 * a password is 14. --jerry
359 trust_passwd[14] = '\0';
361 E_md4hash(trust_passwd, orig_trust_passwd_hash);
363 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
364 global_myname(),
365 orig_trust_passwd_hash,
366 sec_channel_type);
368 if (NT_STATUS_IS_OK(result))
369 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
372 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
373 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
374 result = NT_STATUS_UNSUCCESSFUL;
377 return result;
381 * Join a domain, the old way.
383 * @param argc Standard main() style argc.
384 * @param argv Standard main() style argv. Initial components are already
385 * stripped.
387 * @return A shell status integer (0 for success).
390 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
392 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
393 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
394 rpc_oldjoin_internals,
395 argc, argv);
399 * Join a domain, the old way. This function exists to allow
400 * the message to be displayed when oldjoin was explicitly
401 * requested, but not when it was implied by "net rpc join".
403 * @param argc Standard main() style argc.
404 * @param argv Standard main() style argv. Initial components are already
405 * stripped.
407 * @return A shell status integer (0 for success).
410 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
412 int rc = -1;
414 if (c->display_usage) {
415 d_printf( "%s\n"
416 "net rpc oldjoin\n"
417 " %s\n",
418 _("Usage:"),
419 _("Join a domain the old way"));
420 return 0;
423 rc = net_rpc_perform_oldjoin(c, argc, argv);
425 if (rc) {
426 d_fprintf(stderr, _("Failed to join domain\n"));
429 return rc;
433 * 'net rpc join' entrypoint.
434 * @param argc Standard main() style argc.
435 * @param argv Standard main() style argv. Initial components are already
436 * stripped
438 * Main 'net_rpc_join()' (where the admin username/password is used) is
439 * in net_rpc_join.c.
440 * Try to just change the password, but if that doesn't work, use/prompt
441 * for a username/password.
444 int net_rpc_join(struct net_context *c, int argc, const char **argv)
446 if (c->display_usage) {
447 d_printf("%s\n%s",
448 _("Usage:"),
449 _("net rpc join -U <username>[%%password] <type>\n"
450 " Join a domain\n"
451 " username\tName of the admin user"
452 " password\tPassword of the admin user, will "
453 "prompt if not specified\n"
454 " type\tCan be one of the following:\n"
455 "\t\tMEMBER\tJoin as member server (default)\n"
456 "\t\tBDC\tJoin as BDC\n"
457 "\t\tPDC\tJoin as PDC\n"));
458 return 0;
461 if (lp_server_role() == ROLE_STANDALONE) {
462 d_printf(_("cannot join as standalone machine\n"));
463 return -1;
466 if (strlen(global_myname()) > 15) {
467 d_printf(_("Our netbios name can be at most 15 chars long, "
468 "\"%s\" is %u chars long\n"),
469 global_myname(), (unsigned int)strlen(global_myname()));
470 return -1;
473 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
474 return 0;
476 return net_rpc_join_newstyle(c, argc, argv);
480 * display info about a rpc domain
482 * All parameters are provided by the run_rpc_command function, except for
483 * argc, argv which are passed through.
485 * @param domain_sid The domain sid acquired from the remote server
486 * @param cli A cli_state connected to the server.
487 * @param mem_ctx Talloc context, destroyed on completion of the function.
488 * @param argc Standard main() style argc.
489 * @param argv Standard main() style argv. Initial components are already
490 * stripped.
492 * @return Normal NTSTATUS return.
495 NTSTATUS rpc_info_internals(struct net_context *c,
496 const struct dom_sid *domain_sid,
497 const char *domain_name,
498 struct cli_state *cli,
499 struct rpc_pipe_client *pipe_hnd,
500 TALLOC_CTX *mem_ctx,
501 int argc,
502 const char **argv)
504 struct policy_handle connect_pol, domain_pol;
505 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
506 union samr_DomainInfo *info = NULL;
507 fstring sid_str;
509 sid_to_fstring(sid_str, domain_sid);
511 /* Get sam policy handle */
512 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
513 pipe_hnd->desthost,
514 MAXIMUM_ALLOWED_ACCESS,
515 &connect_pol);
516 if (!NT_STATUS_IS_OK(result)) {
517 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
518 nt_errstr(result));
519 goto done;
522 /* Get domain policy handle */
523 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
524 &connect_pol,
525 MAXIMUM_ALLOWED_ACCESS,
526 CONST_DISCARD(struct dom_sid2 *, domain_sid),
527 &domain_pol);
528 if (!NT_STATUS_IS_OK(result)) {
529 d_fprintf(stderr, _("Could not open domain: %s\n"),
530 nt_errstr(result));
531 goto done;
534 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
535 &domain_pol,
537 &info);
538 if (NT_STATUS_IS_OK(result)) {
539 d_printf(_("Domain Name: %s\n"),
540 info->general.domain_name.string);
541 d_printf(_("Domain SID: %s\n"), sid_str);
542 d_printf(_("Sequence number: %llu\n"),
543 (unsigned long long)info->general.sequence_num);
544 d_printf(_("Num users: %u\n"), info->general.num_users);
545 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
546 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
549 done:
550 return result;
554 * 'net rpc info' entrypoint.
555 * @param argc Standard main() style argc.
556 * @param argv Standard main() style argv. Initial components are already
557 * stripped.
560 int net_rpc_info(struct net_context *c, int argc, const char **argv)
562 if (c->display_usage) {
563 d_printf( "%s\n"
564 "net rpc info\n"
565 " %s\n",
566 _("Usage:"),
567 _("Display information about the domain"));
568 return 0;
571 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
572 NET_FLAGS_PDC, rpc_info_internals,
573 argc, argv);
577 * Fetch domain SID into the local secrets.tdb.
579 * All parameters are provided by the run_rpc_command function, except for
580 * argc, argv which are passed through.
582 * @param domain_sid The domain sid acquired from the remote server.
583 * @param cli A cli_state connected to the server.
584 * @param mem_ctx Talloc context, destroyed on completion of the function.
585 * @param argc Standard main() style argc.
586 * @param argv Standard main() style argv. Initial components are already
587 * stripped.
589 * @return Normal NTSTATUS return.
592 static NTSTATUS rpc_getsid_internals(struct net_context *c,
593 const struct dom_sid *domain_sid,
594 const char *domain_name,
595 struct cli_state *cli,
596 struct rpc_pipe_client *pipe_hnd,
597 TALLOC_CTX *mem_ctx,
598 int argc,
599 const char **argv)
601 fstring sid_str;
603 sid_to_fstring(sid_str, domain_sid);
604 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
605 sid_str, domain_name);
607 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
608 DEBUG(0,("Can't store domain SID\n"));
609 return NT_STATUS_UNSUCCESSFUL;
612 return NT_STATUS_OK;
616 * 'net rpc getsid' entrypoint.
617 * @param argc Standard main() style argc.
618 * @param argv Standard main() style argv. Initial components are already
619 * stripped.
622 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
624 int conn_flags = NET_FLAGS_PDC;
626 if (!c->opt_user_specified) {
627 conn_flags |= NET_FLAGS_ANONYMOUS;
630 if (c->display_usage) {
631 d_printf( "%s\n"
632 "net rpc getsid\n"
633 " %s\n",
634 _("Usage:"),
635 _("Fetch domain SID into local secrets.tdb"));
636 return 0;
639 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
640 conn_flags,
641 rpc_getsid_internals,
642 argc, argv);
645 /****************************************************************************/
648 * Basic usage function for 'net rpc user'.
649 * @param argc Standard main() style argc.
650 * @param argv Standard main() style argv. Initial components are already
651 * stripped.
654 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
656 return net_user_usage(c, argc, argv);
660 * Add a new user to a remote RPC server.
662 * @param argc Standard main() style argc.
663 * @param argv Standard main() style argv. Initial components are already
664 * stripped.
666 * @return A shell status integer (0 for success).
669 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
671 NET_API_STATUS status;
672 struct USER_INFO_1 info1;
673 uint32_t parm_error = 0;
675 if (argc < 1 || c->display_usage) {
676 rpc_user_usage(c, argc, argv);
677 return 0;
680 ZERO_STRUCT(info1);
682 info1.usri1_name = argv[0];
683 if (argc == 2) {
684 info1.usri1_password = argv[1];
687 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
689 if (status != 0) {
690 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
691 argv[0], libnetapi_get_error_string(c->netapi_ctx,
692 status));
693 return -1;
694 } else {
695 d_printf(_("Added user '%s'.\n"), argv[0]);
698 return 0;
702 * Rename a user on a remote RPC server.
704 * @param argc Standard main() style argc.
705 * @param argv Standard main() style argv. Initial components are already
706 * stripped.
708 * @return A shell status integer (0 for success).
711 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
713 NET_API_STATUS status;
714 struct USER_INFO_0 u0;
715 uint32_t parm_err = 0;
717 if (argc != 2 || c->display_usage) {
718 rpc_user_usage(c, argc, argv);
719 return 0;
722 u0.usri0_name = argv[1];
724 status = NetUserSetInfo(c->opt_host, argv[0],
725 0, (uint8_t *)&u0, &parm_err);
726 if (status) {
727 d_fprintf(stderr,
728 _("Failed to rename user from %s to %s - %s\n"),
729 argv[0], argv[1],
730 libnetapi_get_error_string(c->netapi_ctx, status));
731 } else {
732 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
735 return status;
739 * Set a user's primary group
741 * @param argc Standard main() style argc.
742 * @param argv Standard main() style argv. Initial components are already
743 * stripped.
745 * @return A shell status integer (0 for success).
748 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
749 const char **argv)
751 NET_API_STATUS status;
752 uint8_t *buffer;
753 struct GROUP_INFO_2 *g2;
754 struct USER_INFO_1051 u1051;
755 uint32_t parm_err = 0;
757 if (argc != 2 || c->display_usage) {
758 rpc_user_usage(c, argc, argv);
759 return 0;
762 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
763 if (status) {
764 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
765 argv[1],
766 libnetapi_get_error_string(c->netapi_ctx, status));
767 return status;
769 g2 = (struct GROUP_INFO_2 *)buffer;
771 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
773 NetApiBufferFree(buffer);
775 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
776 (uint8_t *)&u1051, &parm_err);
777 if (status) {
778 d_fprintf(stderr,
779 _("Failed to set user's primary group %s to %s - "
780 "%s\n"), argv[0], argv[1],
781 libnetapi_get_error_string(c->netapi_ctx, status));
782 } else {
783 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
784 argv[1]);
786 return status;
790 * Delete a user from a remote RPC server.
792 * @param argc Standard main() style argc.
793 * @param argv Standard main() style argv. Initial components are already
794 * stripped.
796 * @return A shell status integer (0 for success).
799 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
801 NET_API_STATUS status;
803 if (argc < 1 || c->display_usage) {
804 rpc_user_usage(c, argc, argv);
805 return 0;
808 status = NetUserDel(c->opt_host, argv[0]);
810 if (status != 0) {
811 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
812 argv[0],
813 libnetapi_get_error_string(c->netapi_ctx, status));
814 return -1;
815 } else {
816 d_printf(_("Deleted user '%s'.\n"), argv[0]);
819 return 0;
823 * Set a user's password on a remote RPC server.
825 * @param argc Standard main() style argc.
826 * @param argv Standard main() style argv. Initial components are already
827 * stripped.
829 * @return A shell status integer (0 for success).
832 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
834 NET_API_STATUS status;
835 char *prompt = NULL;
836 struct USER_INFO_1003 u1003;
837 uint32_t parm_err = 0;
838 int ret;
840 if (argc < 1 || c->display_usage) {
841 rpc_user_usage(c, argc, argv);
842 return 0;
845 if (argv[1]) {
846 u1003.usri1003_password = argv[1];
847 } else {
848 ret = asprintf(&prompt, _("Enter new password for %s:"),
849 argv[0]);
850 if (ret == -1) {
851 return -1;
853 u1003.usri1003_password = talloc_strdup(c, getpass(prompt));
854 SAFE_FREE(prompt);
855 if (u1003.usri1003_password == NULL) {
856 return -1;
860 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
862 /* Display results */
863 if (status != 0) {
864 d_fprintf(stderr,
865 _("Failed to set password for '%s' with error: %s.\n"),
866 argv[0], libnetapi_get_error_string(c->netapi_ctx,
867 status));
868 return -1;
871 return 0;
875 * List a user's groups from a remote RPC server.
877 * @param argc Standard main() style argc.
878 * @param argv Standard main() style argv. Initial components are already
879 * stripped.
881 * @return A shell status integer (0 for success)
884 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
887 NET_API_STATUS status;
888 struct GROUP_USERS_INFO_0 *u0 = NULL;
889 uint32_t entries_read = 0;
890 uint32_t total_entries = 0;
891 int i;
894 if (argc < 1 || c->display_usage) {
895 rpc_user_usage(c, argc, argv);
896 return 0;
899 status = NetUserGetGroups(c->opt_host,
900 argv[0],
902 (uint8_t **)(void *)&u0,
903 (uint32_t)-1,
904 &entries_read,
905 &total_entries);
906 if (status != 0) {
907 d_fprintf(stderr,
908 _("Failed to get groups for '%s' with error: %s.\n"),
909 argv[0], libnetapi_get_error_string(c->netapi_ctx,
910 status));
911 return -1;
914 for (i=0; i < entries_read; i++) {
915 printf("%s\n", u0->grui0_name);
916 u0++;
919 return 0;
923 * List users on a remote RPC server.
925 * All parameters are provided by the run_rpc_command function, except for
926 * argc, argv which are passed through.
928 * @param domain_sid The domain sid acquired from the remote server.
929 * @param cli A cli_state connected to the server.
930 * @param mem_ctx Talloc context, destroyed on completion of the function.
931 * @param argc Standard main() style argc.
932 * @param argv Standard main() style argv. Initial components are already
933 * stripped.
935 * @return Normal NTSTATUS return.
938 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
940 NET_API_STATUS status;
941 uint32_t start_idx=0, num_entries, i, loop_count = 0;
942 struct NET_DISPLAY_USER *info = NULL;
943 void *buffer = NULL;
945 /* Query domain users */
946 if (c->opt_long_list_entries)
947 d_printf(_("\nUser name Comment"
948 "\n-----------------------------\n"));
949 do {
950 uint32_t max_entries, max_size;
952 get_query_dispinfo_params(
953 loop_count, &max_entries, &max_size);
955 status = NetQueryDisplayInformation(c->opt_host,
957 start_idx,
958 max_entries,
959 max_size,
960 &num_entries,
961 &buffer);
962 if (status != 0 && status != ERROR_MORE_DATA) {
963 return status;
966 info = (struct NET_DISPLAY_USER *)buffer;
968 for (i = 0; i < num_entries; i++) {
970 if (c->opt_long_list_entries)
971 printf("%-21.21s %s\n", info->usri1_name,
972 info->usri1_comment);
973 else
974 printf("%s\n", info->usri1_name);
975 info++;
978 NetApiBufferFree(buffer);
980 loop_count++;
981 start_idx += num_entries;
983 } while (status == ERROR_MORE_DATA);
985 return status;
989 * 'net rpc user' entrypoint.
990 * @param argc Standard main() style argc.
991 * @param argv Standard main() style argv. Initial components are already
992 * stripped.
995 int net_rpc_user(struct net_context *c, int argc, const char **argv)
997 NET_API_STATUS status;
999 struct functable func[] = {
1001 "add",
1002 rpc_user_add,
1003 NET_TRANSPORT_RPC,
1004 N_("Add specified user"),
1005 N_("net rpc user add\n"
1006 " Add specified user")
1009 "info",
1010 rpc_user_info,
1011 NET_TRANSPORT_RPC,
1012 N_("List domain groups of user"),
1013 N_("net rpc user info\n"
1014 " List domain groups of user")
1017 "delete",
1018 rpc_user_delete,
1019 NET_TRANSPORT_RPC,
1020 N_("Remove specified user"),
1021 N_("net rpc user delete\n"
1022 " Remove specified user")
1025 "password",
1026 rpc_user_password,
1027 NET_TRANSPORT_RPC,
1028 N_("Change user password"),
1029 N_("net rpc user password\n"
1030 " Change user password")
1033 "rename",
1034 rpc_user_rename,
1035 NET_TRANSPORT_RPC,
1036 N_("Rename specified user"),
1037 N_("net rpc user rename\n"
1038 " Rename specified user")
1041 "setprimarygroup",
1042 rpc_user_setprimarygroup,
1043 NET_TRANSPORT_RPC,
1044 "Set a user's primary group",
1045 "net rpc user setprimarygroup\n"
1046 " Set a user's primary group"
1048 {NULL, NULL, 0, NULL, NULL}
1051 status = libnetapi_init(&c->netapi_ctx);
1052 if (status != 0) {
1053 return -1;
1055 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1056 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1057 if (c->opt_kerberos) {
1058 libnetapi_set_use_kerberos(c->netapi_ctx);
1061 if (argc == 0) {
1062 if (c->display_usage) {
1063 d_printf( "%s\n"
1064 "net rpc user\n"
1065 " %s\n",
1066 _("Usage:"),
1067 _("List all users"));
1068 net_display_usage_from_functable(func);
1069 return 0;
1072 return rpc_user_list(c, argc, argv);
1075 return net_run_function(c, argc, argv, "net rpc user", func);
1078 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1079 TALLOC_CTX *mem_ctx,
1080 struct rpc_sh_ctx *ctx,
1081 struct rpc_pipe_client *pipe_hnd,
1082 int argc, const char **argv)
1084 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1087 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1088 TALLOC_CTX *mem_ctx,
1089 struct rpc_sh_ctx *ctx,
1090 struct rpc_pipe_client *pipe_hnd,
1091 int argc, const char **argv)
1093 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1096 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1097 TALLOC_CTX *mem_ctx,
1098 struct rpc_sh_ctx *ctx,
1099 struct rpc_pipe_client *pipe_hnd,
1100 int argc, const char **argv,
1101 NTSTATUS (*fn)(
1102 struct net_context *c,
1103 TALLOC_CTX *mem_ctx,
1104 struct rpc_sh_ctx *ctx,
1105 struct rpc_pipe_client *pipe_hnd,
1106 struct policy_handle *user_hnd,
1107 int argc, const char **argv))
1109 struct policy_handle connect_pol, domain_pol, user_pol;
1110 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1111 struct dom_sid sid;
1112 uint32 rid;
1113 enum lsa_SidType type;
1115 if (argc == 0) {
1116 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1117 ctx->whoami);
1118 return NT_STATUS_INVALID_PARAMETER;
1121 ZERO_STRUCT(connect_pol);
1122 ZERO_STRUCT(domain_pol);
1123 ZERO_STRUCT(user_pol);
1125 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1126 argv[0], NULL, NULL, &sid, &type);
1127 if (!NT_STATUS_IS_OK(result)) {
1128 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1129 nt_errstr(result));
1130 goto done;
1133 if (type != SID_NAME_USER) {
1134 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1135 sid_type_lookup(type));
1136 result = NT_STATUS_NO_SUCH_USER;
1137 goto done;
1140 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1141 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1142 result = NT_STATUS_NO_SUCH_USER;
1143 goto done;
1146 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1147 pipe_hnd->desthost,
1148 MAXIMUM_ALLOWED_ACCESS,
1149 &connect_pol);
1150 if (!NT_STATUS_IS_OK(result)) {
1151 goto done;
1154 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1155 &connect_pol,
1156 MAXIMUM_ALLOWED_ACCESS,
1157 ctx->domain_sid,
1158 &domain_pol);
1159 if (!NT_STATUS_IS_OK(result)) {
1160 goto done;
1163 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1164 &domain_pol,
1165 MAXIMUM_ALLOWED_ACCESS,
1166 rid,
1167 &user_pol);
1168 if (!NT_STATUS_IS_OK(result)) {
1169 goto done;
1172 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1174 done:
1175 if (is_valid_policy_hnd(&user_pol)) {
1176 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1178 if (is_valid_policy_hnd(&domain_pol)) {
1179 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1181 if (is_valid_policy_hnd(&connect_pol)) {
1182 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1184 return result;
1187 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1188 TALLOC_CTX *mem_ctx,
1189 struct rpc_sh_ctx *ctx,
1190 struct rpc_pipe_client *pipe_hnd,
1191 struct policy_handle *user_hnd,
1192 int argc, const char **argv)
1194 NTSTATUS result;
1195 union samr_UserInfo *info = NULL;
1197 if (argc != 0) {
1198 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1199 ctx->whoami);
1200 return NT_STATUS_INVALID_PARAMETER;
1203 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1204 user_hnd,
1206 &info);
1207 if (!NT_STATUS_IS_OK(result)) {
1208 return result;
1211 d_printf(_("user rid: %d, group rid: %d\n"),
1212 info->info21.rid,
1213 info->info21.primary_gid);
1215 return result;
1218 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1219 TALLOC_CTX *mem_ctx,
1220 struct rpc_sh_ctx *ctx,
1221 struct rpc_pipe_client *pipe_hnd,
1222 int argc, const char **argv)
1224 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1225 rpc_sh_user_show_internals);
1228 #define FETCHSTR(name, rec) \
1229 do { if (strequal(ctx->thiscmd, name)) { \
1230 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1231 } while (0);
1233 #define SETSTR(name, rec, flag) \
1234 do { if (strequal(ctx->thiscmd, name)) { \
1235 init_lsa_String(&(info->info21.rec), argv[0]); \
1236 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1237 } while (0);
1239 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1240 TALLOC_CTX *mem_ctx,
1241 struct rpc_sh_ctx *ctx,
1242 struct rpc_pipe_client *pipe_hnd,
1243 struct policy_handle *user_hnd,
1244 int argc, const char **argv)
1246 NTSTATUS result;
1247 const char *username;
1248 const char *oldval = "";
1249 union samr_UserInfo *info = NULL;
1251 if (argc > 1) {
1252 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1253 _("Usage:"), ctx->whoami);
1254 return NT_STATUS_INVALID_PARAMETER;
1257 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1258 user_hnd,
1260 &info);
1261 if (!NT_STATUS_IS_OK(result)) {
1262 return result;
1265 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1267 FETCHSTR("fullname", full_name);
1268 FETCHSTR("homedir", home_directory);
1269 FETCHSTR("homedrive", home_drive);
1270 FETCHSTR("logonscript", logon_script);
1271 FETCHSTR("profilepath", profile_path);
1272 FETCHSTR("description", description);
1274 if (argc == 0) {
1275 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1276 goto done;
1279 if (strcmp(argv[0], "NULL") == 0) {
1280 argv[0] = "";
1283 ZERO_STRUCT(info->info21);
1285 SETSTR("fullname", full_name, FULL_NAME);
1286 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1287 SETSTR("homedrive", home_drive, HOME_DRIVE);
1288 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1289 SETSTR("profilepath", profile_path, PROFILE_PATH);
1290 SETSTR("description", description, DESCRIPTION);
1292 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1293 user_hnd,
1295 info);
1297 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1298 ctx->thiscmd, oldval, argv[0]);
1300 done:
1302 return result;
1305 #define HANDLEFLG(name, rec) \
1306 do { if (strequal(ctx->thiscmd, name)) { \
1307 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1308 if (newval) { \
1309 newflags = oldflags | ACB_##rec; \
1310 } else { \
1311 newflags = oldflags & ~ACB_##rec; \
1312 } } } while (0);
1314 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1315 TALLOC_CTX *mem_ctx,
1316 struct rpc_sh_ctx *ctx,
1317 struct rpc_pipe_client *pipe_hnd,
1318 int argc, const char **argv)
1320 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1321 rpc_sh_user_str_edit_internals);
1324 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1325 TALLOC_CTX *mem_ctx,
1326 struct rpc_sh_ctx *ctx,
1327 struct rpc_pipe_client *pipe_hnd,
1328 struct policy_handle *user_hnd,
1329 int argc, const char **argv)
1331 NTSTATUS result;
1332 const char *username;
1333 const char *oldval = "unknown";
1334 uint32 oldflags, newflags;
1335 bool newval;
1336 union samr_UserInfo *info = NULL;
1338 if ((argc > 1) ||
1339 ((argc == 1) && !strequal(argv[0], "yes") &&
1340 !strequal(argv[0], "no"))) {
1341 /* TRANSATORS: The yes|no here are program keywords. Please do
1342 not translate. */
1343 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1344 ctx->whoami);
1345 return NT_STATUS_INVALID_PARAMETER;
1348 newval = strequal(argv[0], "yes");
1350 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1351 user_hnd,
1353 &info);
1354 if (!NT_STATUS_IS_OK(result)) {
1355 return result;
1358 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1359 oldflags = info->info21.acct_flags;
1360 newflags = info->info21.acct_flags;
1362 HANDLEFLG("disabled", DISABLED);
1363 HANDLEFLG("pwnotreq", PWNOTREQ);
1364 HANDLEFLG("autolock", AUTOLOCK);
1365 HANDLEFLG("pwnoexp", PWNOEXP);
1367 if (argc == 0) {
1368 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1369 oldval);
1370 goto done;
1373 ZERO_STRUCT(info->info21);
1375 info->info21.acct_flags = newflags;
1376 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1378 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1379 user_hnd,
1381 info);
1383 if (NT_STATUS_IS_OK(result)) {
1384 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1385 ctx->thiscmd, oldval, argv[0]);
1388 done:
1390 return result;
1393 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1394 TALLOC_CTX *mem_ctx,
1395 struct rpc_sh_ctx *ctx,
1396 struct rpc_pipe_client *pipe_hnd,
1397 int argc, const char **argv)
1399 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1400 rpc_sh_user_flag_edit_internals);
1403 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1404 TALLOC_CTX *mem_ctx,
1405 struct rpc_sh_ctx *ctx)
1407 static struct rpc_sh_cmd cmds[] = {
1409 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1410 N_("Show/Set a user's full name") },
1412 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1413 N_("Show/Set a user's home directory") },
1415 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1416 N_("Show/Set a user's home drive") },
1418 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1419 N_("Show/Set a user's logon script") },
1421 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1422 N_("Show/Set a user's profile path") },
1424 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1425 N_("Show/Set a user's description") },
1427 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1428 N_("Show/Set whether a user is disabled") },
1430 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1431 N_("Show/Set whether a user locked out") },
1433 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1434 N_("Show/Set whether a user does not need a password") },
1436 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1437 N_("Show/Set whether a user's password does not expire") },
1439 { NULL, NULL, 0, NULL, NULL }
1442 return cmds;
1445 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1446 TALLOC_CTX *mem_ctx,
1447 struct rpc_sh_ctx *ctx)
1449 static struct rpc_sh_cmd cmds[] = {
1451 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1452 N_("List available users") },
1454 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1455 N_("List the domain groups a user is member of") },
1457 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1458 N_("Show info about a user") },
1460 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1461 N_("Show/Modify a user's fields") },
1463 { NULL, NULL, 0, NULL, NULL }
1466 return cmds;
1469 /****************************************************************************/
1472 * Basic usage function for 'net rpc group'.
1473 * @param argc Standard main() style argc.
1474 * @param argv Standard main() style argv. Initial components are already
1475 * stripped.
1478 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1480 return net_group_usage(c, argc, argv);
1484 * Delete group on a remote RPC server.
1486 * All parameters are provided by the run_rpc_command function, except for
1487 * argc, argv which are passed through.
1489 * @param domain_sid The domain sid acquired from the remote server.
1490 * @param cli A cli_state connected to the server.
1491 * @param mem_ctx Talloc context, destroyed on completion of the function.
1492 * @param argc Standard main() style argc.
1493 * @param argv Standard main() style argv. Initial components are already
1494 * stripped.
1496 * @return Normal NTSTATUS return.
1499 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1500 const struct dom_sid *domain_sid,
1501 const char *domain_name,
1502 struct cli_state *cli,
1503 struct rpc_pipe_client *pipe_hnd,
1504 TALLOC_CTX *mem_ctx,
1505 int argc,
1506 const char **argv)
1508 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1509 bool group_is_primary = false;
1510 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1511 uint32_t group_rid;
1512 struct samr_RidTypeArray *rids = NULL;
1513 /* char **names; */
1514 int i;
1515 /* struct samr_RidWithAttribute *user_gids; */
1517 struct samr_Ids group_rids, name_types;
1518 struct lsa_String lsa_acct_name;
1519 union samr_UserInfo *info = NULL;
1521 if (argc < 1 || c->display_usage) {
1522 rpc_group_usage(c, argc,argv);
1523 return NT_STATUS_OK; /* ok? */
1526 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1527 pipe_hnd->desthost,
1528 MAXIMUM_ALLOWED_ACCESS,
1529 &connect_pol);
1531 if (!NT_STATUS_IS_OK(result)) {
1532 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1533 goto done;
1536 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1537 &connect_pol,
1538 MAXIMUM_ALLOWED_ACCESS,
1539 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1540 &domain_pol);
1542 if (!NT_STATUS_IS_OK(result)) {
1543 d_fprintf(stderr, _("Request open_domain failed\n"));
1544 goto done;
1547 init_lsa_String(&lsa_acct_name, argv[0]);
1549 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1550 &domain_pol,
1552 &lsa_acct_name,
1553 &group_rids,
1554 &name_types);
1555 if (!NT_STATUS_IS_OK(result)) {
1556 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1557 goto done;
1560 switch (name_types.ids[0])
1562 case SID_NAME_DOM_GRP:
1563 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1564 &domain_pol,
1565 MAXIMUM_ALLOWED_ACCESS,
1566 group_rids.ids[0],
1567 &group_pol);
1568 if (!NT_STATUS_IS_OK(result)) {
1569 d_fprintf(stderr, _("Request open_group failed"));
1570 goto done;
1573 group_rid = group_rids.ids[0];
1575 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1576 &group_pol,
1577 &rids);
1579 if (!NT_STATUS_IS_OK(result)) {
1580 d_fprintf(stderr,
1581 _("Unable to query group members of %s"),
1582 argv[0]);
1583 goto done;
1586 if (c->opt_verbose) {
1587 d_printf(
1588 _("Domain Group %s (rid: %d) has %d members\n"),
1589 argv[0],group_rid, rids->count);
1592 /* Check if group is anyone's primary group */
1593 for (i = 0; i < rids->count; i++)
1595 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1596 &domain_pol,
1597 MAXIMUM_ALLOWED_ACCESS,
1598 rids->rids[i],
1599 &user_pol);
1601 if (!NT_STATUS_IS_OK(result)) {
1602 d_fprintf(stderr,
1603 _("Unable to open group member %d\n"),
1604 rids->rids[i]);
1605 goto done;
1608 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1609 &user_pol,
1611 &info);
1613 if (!NT_STATUS_IS_OK(result)) {
1614 d_fprintf(stderr,
1615 _("Unable to lookup userinfo for group "
1616 "member %d\n"),
1617 rids->rids[i]);
1618 goto done;
1621 if (info->info21.primary_gid == group_rid) {
1622 if (c->opt_verbose) {
1623 d_printf(_("Group is primary group "
1624 "of %s\n"),
1625 info->info21.account_name.string);
1627 group_is_primary = true;
1630 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1633 if (group_is_primary) {
1634 d_fprintf(stderr, _("Unable to delete group because "
1635 "some of it's members have it as primary "
1636 "group\n"));
1637 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1638 goto done;
1641 /* remove all group members */
1642 for (i = 0; i < rids->count; i++)
1644 if (c->opt_verbose)
1645 d_printf(_("Remove group member %d..."),
1646 rids->rids[i]);
1647 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1648 &group_pol,
1649 rids->rids[i]);
1651 if (NT_STATUS_IS_OK(result)) {
1652 if (c->opt_verbose)
1653 d_printf(_("ok\n"));
1654 } else {
1655 if (c->opt_verbose)
1656 d_printf("%s\n", _("failed"));
1657 goto done;
1661 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1662 &group_pol);
1664 break;
1665 /* removing a local group is easier... */
1666 case SID_NAME_ALIAS:
1667 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1668 &domain_pol,
1669 MAXIMUM_ALLOWED_ACCESS,
1670 group_rids.ids[0],
1671 &group_pol);
1673 if (!NT_STATUS_IS_OK(result)) {
1674 d_fprintf(stderr, _("Request open_alias failed\n"));
1675 goto done;
1678 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1679 &group_pol);
1680 break;
1681 default:
1682 d_fprintf(stderr, _("%s is of type %s. This command is only "
1683 "for deleting local or global groups\n"),
1684 argv[0],sid_type_lookup(name_types.ids[0]));
1685 result = NT_STATUS_UNSUCCESSFUL;
1686 goto done;
1689 if (NT_STATUS_IS_OK(result)) {
1690 if (c->opt_verbose)
1691 d_printf(_("Deleted %s '%s'\n"),
1692 sid_type_lookup(name_types.ids[0]), argv[0]);
1693 } else {
1694 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1695 get_friendly_nt_error_msg(result));
1698 done:
1699 return result;
1703 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1705 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1706 rpc_group_delete_internals, argc,argv);
1709 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1711 NET_API_STATUS status;
1712 struct GROUP_INFO_1 info1;
1713 uint32_t parm_error = 0;
1715 if (argc != 1 || c->display_usage) {
1716 rpc_group_usage(c, argc, argv);
1717 return 0;
1720 ZERO_STRUCT(info1);
1722 info1.grpi1_name = argv[0];
1723 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1724 info1.grpi1_comment = c->opt_comment;
1727 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1729 if (status != 0) {
1730 d_fprintf(stderr,
1731 _("Failed to add group '%s' with error: %s.\n"),
1732 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1733 status));
1734 return -1;
1735 } else {
1736 d_printf(_("Added group '%s'.\n"), argv[0]);
1739 return 0;
1742 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1744 NET_API_STATUS status;
1745 struct LOCALGROUP_INFO_1 info1;
1746 uint32_t parm_error = 0;
1748 if (argc != 1 || c->display_usage) {
1749 rpc_group_usage(c, argc, argv);
1750 return 0;
1753 ZERO_STRUCT(info1);
1755 info1.lgrpi1_name = argv[0];
1756 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1757 info1.lgrpi1_comment = c->opt_comment;
1760 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1762 if (status != 0) {
1763 d_fprintf(stderr,
1764 _("Failed to add alias '%s' with error: %s.\n"),
1765 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1766 status));
1767 return -1;
1768 } else {
1769 d_printf(_("Added alias '%s'.\n"), argv[0]);
1772 return 0;
1775 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1777 if (c->opt_localgroup)
1778 return rpc_alias_add_internals(c, argc, argv);
1780 return rpc_group_add_internals(c, argc, argv);
1783 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1784 TALLOC_CTX *mem_ctx,
1785 const char *name,
1786 struct dom_sid *sid,
1787 enum lsa_SidType *type)
1789 struct dom_sid *sids = NULL;
1790 enum lsa_SidType *types = NULL;
1791 struct rpc_pipe_client *pipe_hnd = NULL;
1792 struct policy_handle lsa_pol;
1793 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1795 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1796 &pipe_hnd);
1797 if (!NT_STATUS_IS_OK(result)) {
1798 goto done;
1801 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1802 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1804 if (!NT_STATUS_IS_OK(result)) {
1805 goto done;
1808 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1809 &name, NULL, 1, &sids, &types);
1811 if (NT_STATUS_IS_OK(result)) {
1812 sid_copy(sid, &sids[0]);
1813 *type = types[0];
1816 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1818 done:
1819 if (pipe_hnd) {
1820 TALLOC_FREE(pipe_hnd);
1823 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1825 /* Try as S-1-5-whatever */
1827 struct dom_sid tmp_sid;
1829 if (string_to_sid(&tmp_sid, name)) {
1830 sid_copy(sid, &tmp_sid);
1831 *type = SID_NAME_UNKNOWN;
1832 result = NT_STATUS_OK;
1836 return result;
1839 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1840 TALLOC_CTX *mem_ctx,
1841 const struct dom_sid *group_sid,
1842 const char *member)
1844 struct policy_handle connect_pol, domain_pol;
1845 NTSTATUS result;
1846 uint32 group_rid;
1847 struct policy_handle group_pol;
1849 struct samr_Ids rids, rid_types;
1850 struct lsa_String lsa_acct_name;
1852 struct dom_sid sid;
1854 sid_copy(&sid, group_sid);
1856 if (!sid_split_rid(&sid, &group_rid)) {
1857 return NT_STATUS_UNSUCCESSFUL;
1860 /* Get sam policy handle */
1861 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1862 pipe_hnd->desthost,
1863 MAXIMUM_ALLOWED_ACCESS,
1864 &connect_pol);
1865 if (!NT_STATUS_IS_OK(result)) {
1866 return result;
1869 /* Get domain policy handle */
1870 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1871 &connect_pol,
1872 MAXIMUM_ALLOWED_ACCESS,
1873 &sid,
1874 &domain_pol);
1875 if (!NT_STATUS_IS_OK(result)) {
1876 return result;
1879 init_lsa_String(&lsa_acct_name, member);
1881 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1882 &domain_pol,
1884 &lsa_acct_name,
1885 &rids,
1886 &rid_types);
1888 if (!NT_STATUS_IS_OK(result)) {
1889 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1890 member);
1891 goto done;
1894 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1895 &domain_pol,
1896 MAXIMUM_ALLOWED_ACCESS,
1897 group_rid,
1898 &group_pol);
1900 if (!NT_STATUS_IS_OK(result)) {
1901 goto done;
1904 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1905 &group_pol,
1906 rids.ids[0],
1907 0x0005); /* unknown flags */
1909 done:
1910 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1911 return result;
1914 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1915 TALLOC_CTX *mem_ctx,
1916 const struct dom_sid *alias_sid,
1917 const char *member)
1919 struct policy_handle connect_pol, domain_pol;
1920 NTSTATUS result;
1921 uint32 alias_rid;
1922 struct policy_handle alias_pol;
1924 struct dom_sid member_sid;
1925 enum lsa_SidType member_type;
1927 struct dom_sid sid;
1929 sid_copy(&sid, alias_sid);
1931 if (!sid_split_rid(&sid, &alias_rid)) {
1932 return NT_STATUS_UNSUCCESSFUL;
1935 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1936 member, &member_sid, &member_type);
1938 if (!NT_STATUS_IS_OK(result)) {
1939 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1940 member);
1941 return result;
1944 /* Get sam policy handle */
1945 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1946 pipe_hnd->desthost,
1947 MAXIMUM_ALLOWED_ACCESS,
1948 &connect_pol);
1949 if (!NT_STATUS_IS_OK(result)) {
1950 goto done;
1953 /* Get domain policy handle */
1954 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1955 &connect_pol,
1956 MAXIMUM_ALLOWED_ACCESS,
1957 &sid,
1958 &domain_pol);
1959 if (!NT_STATUS_IS_OK(result)) {
1960 goto done;
1963 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1964 &domain_pol,
1965 MAXIMUM_ALLOWED_ACCESS,
1966 alias_rid,
1967 &alias_pol);
1969 if (!NT_STATUS_IS_OK(result)) {
1970 return result;
1973 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1974 &alias_pol,
1975 &member_sid);
1977 if (!NT_STATUS_IS_OK(result)) {
1978 return result;
1981 done:
1982 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1983 return result;
1986 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1987 const struct dom_sid *domain_sid,
1988 const char *domain_name,
1989 struct cli_state *cli,
1990 struct rpc_pipe_client *pipe_hnd,
1991 TALLOC_CTX *mem_ctx,
1992 int argc,
1993 const char **argv)
1995 struct dom_sid group_sid;
1996 enum lsa_SidType group_type;
1998 if (argc != 2 || c->display_usage) {
1999 d_printf("%s\n%s",
2000 _("Usage:"),
2001 _("net rpc group addmem <group> <member>\n"
2002 " Add a member to a group\n"
2003 " group\tGroup to add member to\n"
2004 " member\tMember to add to group\n"));
2005 return NT_STATUS_UNSUCCESSFUL;
2008 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2009 &group_sid, &group_type))) {
2010 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2011 argv[0]);
2012 return NT_STATUS_UNSUCCESSFUL;
2015 if (group_type == SID_NAME_DOM_GRP) {
2016 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2017 &group_sid, argv[1]);
2019 if (!NT_STATUS_IS_OK(result)) {
2020 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2021 argv[1], argv[0], nt_errstr(result));
2023 return result;
2026 if (group_type == SID_NAME_ALIAS) {
2027 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2028 &group_sid, argv[1]);
2030 if (!NT_STATUS_IS_OK(result)) {
2031 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2032 argv[1], argv[0], nt_errstr(result));
2034 return result;
2037 d_fprintf(stderr, _("Can only add members to global or local groups "
2038 "which %s is not\n"), argv[0]);
2040 return NT_STATUS_UNSUCCESSFUL;
2043 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2045 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2046 rpc_group_addmem_internals,
2047 argc, argv);
2050 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2051 struct rpc_pipe_client *pipe_hnd,
2052 TALLOC_CTX *mem_ctx,
2053 const struct dom_sid *group_sid,
2054 const char *member)
2056 struct policy_handle connect_pol, domain_pol;
2057 NTSTATUS result;
2058 uint32 group_rid;
2059 struct policy_handle group_pol;
2061 struct samr_Ids rids, rid_types;
2062 struct lsa_String lsa_acct_name;
2064 struct dom_sid sid;
2066 sid_copy(&sid, group_sid);
2068 if (!sid_split_rid(&sid, &group_rid))
2069 return NT_STATUS_UNSUCCESSFUL;
2071 /* Get sam policy handle */
2072 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2073 pipe_hnd->desthost,
2074 MAXIMUM_ALLOWED_ACCESS,
2075 &connect_pol);
2076 if (!NT_STATUS_IS_OK(result))
2077 return result;
2079 /* Get domain policy handle */
2080 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2081 &connect_pol,
2082 MAXIMUM_ALLOWED_ACCESS,
2083 &sid,
2084 &domain_pol);
2085 if (!NT_STATUS_IS_OK(result))
2086 return result;
2088 init_lsa_String(&lsa_acct_name, member);
2090 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2091 &domain_pol,
2093 &lsa_acct_name,
2094 &rids,
2095 &rid_types);
2096 if (!NT_STATUS_IS_OK(result)) {
2097 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2098 member);
2099 goto done;
2102 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2103 &domain_pol,
2104 MAXIMUM_ALLOWED_ACCESS,
2105 group_rid,
2106 &group_pol);
2108 if (!NT_STATUS_IS_OK(result))
2109 goto done;
2111 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2112 &group_pol,
2113 rids.ids[0]);
2115 done:
2116 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2117 return result;
2120 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2121 TALLOC_CTX *mem_ctx,
2122 const struct dom_sid *alias_sid,
2123 const char *member)
2125 struct policy_handle connect_pol, domain_pol;
2126 NTSTATUS result;
2127 uint32 alias_rid;
2128 struct policy_handle alias_pol;
2130 struct dom_sid member_sid;
2131 enum lsa_SidType member_type;
2133 struct dom_sid sid;
2135 sid_copy(&sid, alias_sid);
2137 if (!sid_split_rid(&sid, &alias_rid))
2138 return NT_STATUS_UNSUCCESSFUL;
2140 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2141 member, &member_sid, &member_type);
2143 if (!NT_STATUS_IS_OK(result)) {
2144 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2145 member);
2146 return result;
2149 /* Get sam policy handle */
2150 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2151 pipe_hnd->desthost,
2152 MAXIMUM_ALLOWED_ACCESS,
2153 &connect_pol);
2154 if (!NT_STATUS_IS_OK(result)) {
2155 goto done;
2158 /* Get domain policy handle */
2159 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2160 &connect_pol,
2161 MAXIMUM_ALLOWED_ACCESS,
2162 &sid,
2163 &domain_pol);
2164 if (!NT_STATUS_IS_OK(result)) {
2165 goto done;
2168 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2169 &domain_pol,
2170 MAXIMUM_ALLOWED_ACCESS,
2171 alias_rid,
2172 &alias_pol);
2174 if (!NT_STATUS_IS_OK(result))
2175 return result;
2177 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2178 &alias_pol,
2179 &member_sid);
2181 if (!NT_STATUS_IS_OK(result))
2182 return result;
2184 done:
2185 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2186 return result;
2189 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2190 const struct dom_sid *domain_sid,
2191 const char *domain_name,
2192 struct cli_state *cli,
2193 struct rpc_pipe_client *pipe_hnd,
2194 TALLOC_CTX *mem_ctx,
2195 int argc,
2196 const char **argv)
2198 struct dom_sid group_sid;
2199 enum lsa_SidType group_type;
2201 if (argc != 2 || c->display_usage) {
2202 d_printf("%s\n%s",
2203 _("Usage:"),
2204 _("net rpc group delmem <group> <member>\n"
2205 " Delete a member from a group\n"
2206 " group\tGroup to delete member from\n"
2207 " member\tMember to delete from group\n"));
2208 return NT_STATUS_UNSUCCESSFUL;
2211 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2212 &group_sid, &group_type))) {
2213 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2214 argv[0]);
2215 return NT_STATUS_UNSUCCESSFUL;
2218 if (group_type == SID_NAME_DOM_GRP) {
2219 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2220 &group_sid, argv[1]);
2222 if (!NT_STATUS_IS_OK(result)) {
2223 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2224 argv[1], argv[0], nt_errstr(result));
2226 return result;
2229 if (group_type == SID_NAME_ALIAS) {
2230 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2231 &group_sid, argv[1]);
2233 if (!NT_STATUS_IS_OK(result)) {
2234 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2235 argv[1], argv[0], nt_errstr(result));
2237 return result;
2240 d_fprintf(stderr, _("Can only delete members from global or local "
2241 "groups which %s is not\n"), argv[0]);
2243 return NT_STATUS_UNSUCCESSFUL;
2246 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2248 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2249 rpc_group_delmem_internals,
2250 argc, argv);
2254 * List groups on a remote RPC server.
2256 * All parameters are provided by the run_rpc_command function, except for
2257 * argc, argv which are passes through.
2259 * @param domain_sid The domain sid acquired from the remote server.
2260 * @param cli A cli_state connected to the server.
2261 * @param mem_ctx Talloc context, destroyed on completion of the function.
2262 * @param argc Standard main() style argc.
2263 * @param argv Standard main() style argv. Initial components are already
2264 * stripped.
2266 * @return Normal NTSTATUS return.
2269 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2270 const struct dom_sid *domain_sid,
2271 const char *domain_name,
2272 struct cli_state *cli,
2273 struct rpc_pipe_client *pipe_hnd,
2274 TALLOC_CTX *mem_ctx,
2275 int argc,
2276 const char **argv)
2278 struct policy_handle connect_pol, domain_pol;
2279 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2280 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2281 struct samr_SamArray *groups = NULL;
2282 bool global = false;
2283 bool local = false;
2284 bool builtin = false;
2286 if (c->display_usage) {
2287 d_printf("%s\n%s",
2288 _("Usage:"),
2289 _("net rpc group list [global] [local] [builtin]\n"
2290 " List groups on RPC server\n"
2291 " global\tList global groups\n"
2292 " local\tList local groups\n"
2293 " builtin\tList builtin groups\n"
2294 " If none of global, local or builtin is "
2295 "specified, all three options are considered "
2296 "set\n"));
2297 return NT_STATUS_OK;
2300 if (argc == 0) {
2301 global = true;
2302 local = true;
2303 builtin = true;
2306 for (i=0; i<argc; i++) {
2307 if (strequal(argv[i], "global"))
2308 global = true;
2310 if (strequal(argv[i], "local"))
2311 local = true;
2313 if (strequal(argv[i], "builtin"))
2314 builtin = true;
2317 /* Get sam policy handle */
2319 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2320 pipe_hnd->desthost,
2321 MAXIMUM_ALLOWED_ACCESS,
2322 &connect_pol);
2323 if (!NT_STATUS_IS_OK(result)) {
2324 goto done;
2327 /* Get domain policy handle */
2329 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2330 &connect_pol,
2331 MAXIMUM_ALLOWED_ACCESS,
2332 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2333 &domain_pol);
2334 if (!NT_STATUS_IS_OK(result)) {
2335 goto done;
2338 /* Query domain groups */
2339 if (c->opt_long_list_entries)
2340 d_printf(_("\nGroup name Comment"
2341 "\n-----------------------------\n"));
2342 do {
2343 uint32_t max_size, total_size, returned_size;
2344 union samr_DispInfo info;
2346 if (!global) break;
2348 get_query_dispinfo_params(
2349 loop_count, &max_entries, &max_size);
2351 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2352 &domain_pol,
2354 start_idx,
2355 max_entries,
2356 max_size,
2357 &total_size,
2358 &returned_size,
2359 &info);
2360 num_entries = info.info3.count;
2361 start_idx += info.info3.count;
2363 if (!NT_STATUS_IS_OK(result) &&
2364 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2365 break;
2367 for (i = 0; i < num_entries; i++) {
2369 const char *group = NULL;
2370 const char *desc = NULL;
2372 group = info.info3.entries[i].account_name.string;
2373 desc = info.info3.entries[i].description.string;
2375 if (c->opt_long_list_entries)
2376 printf("%-21.21s %-50.50s\n",
2377 group, desc);
2378 else
2379 printf("%s\n", group);
2381 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2382 /* query domain aliases */
2383 start_idx = 0;
2384 do {
2385 if (!local) break;
2387 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2388 &domain_pol,
2389 &start_idx,
2390 &groups,
2391 0xffff,
2392 &num_entries);
2393 if (!NT_STATUS_IS_OK(result) &&
2394 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2395 break;
2397 for (i = 0; i < num_entries; i++) {
2399 const char *description = NULL;
2401 if (c->opt_long_list_entries) {
2403 struct policy_handle alias_pol;
2404 union samr_AliasInfo *info = NULL;
2406 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2407 &domain_pol,
2408 0x8,
2409 groups->entries[i].idx,
2410 &alias_pol))) &&
2411 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2412 &alias_pol,
2414 &info))) &&
2415 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2416 &alias_pol)))) {
2417 description = info->description.string;
2421 if (description != NULL) {
2422 printf("%-21.21s %-50.50s\n",
2423 groups->entries[i].name.string,
2424 description);
2425 } else {
2426 printf("%s\n", groups->entries[i].name.string);
2429 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2430 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2431 /* Get builtin policy handle */
2433 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2434 &connect_pol,
2435 MAXIMUM_ALLOWED_ACCESS,
2436 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2437 &domain_pol);
2438 if (!NT_STATUS_IS_OK(result)) {
2439 goto done;
2441 /* query builtin aliases */
2442 start_idx = 0;
2443 do {
2444 if (!builtin) break;
2446 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2447 &domain_pol,
2448 &start_idx,
2449 &groups,
2450 max_entries,
2451 &num_entries);
2452 if (!NT_STATUS_IS_OK(result) &&
2453 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2454 break;
2456 for (i = 0; i < num_entries; i++) {
2458 const char *description = NULL;
2460 if (c->opt_long_list_entries) {
2462 struct policy_handle alias_pol;
2463 union samr_AliasInfo *info = NULL;
2465 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2466 &domain_pol,
2467 0x8,
2468 groups->entries[i].idx,
2469 &alias_pol))) &&
2470 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2471 &alias_pol,
2473 &info))) &&
2474 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2475 &alias_pol)))) {
2476 description = info->description.string;
2480 if (description != NULL) {
2481 printf("%-21.21s %-50.50s\n",
2482 groups->entries[i].name.string,
2483 description);
2484 } else {
2485 printf("%s\n", groups->entries[i].name.string);
2488 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2490 done:
2491 return result;
2494 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2496 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2497 rpc_group_list_internals,
2498 argc, argv);
2501 static NTSTATUS rpc_list_group_members(struct net_context *c,
2502 struct rpc_pipe_client *pipe_hnd,
2503 TALLOC_CTX *mem_ctx,
2504 const char *domain_name,
2505 const struct dom_sid *domain_sid,
2506 struct policy_handle *domain_pol,
2507 uint32 rid)
2509 NTSTATUS result;
2510 struct policy_handle group_pol;
2511 uint32 num_members, *group_rids;
2512 int i;
2513 struct samr_RidTypeArray *rids = NULL;
2514 struct lsa_Strings names;
2515 struct samr_Ids types;
2517 fstring sid_str;
2518 sid_to_fstring(sid_str, domain_sid);
2520 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2521 domain_pol,
2522 MAXIMUM_ALLOWED_ACCESS,
2523 rid,
2524 &group_pol);
2526 if (!NT_STATUS_IS_OK(result))
2527 return result;
2529 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2530 &group_pol,
2531 &rids);
2533 if (!NT_STATUS_IS_OK(result))
2534 return result;
2536 num_members = rids->count;
2537 group_rids = rids->rids;
2539 while (num_members > 0) {
2540 int this_time = 512;
2542 if (num_members < this_time)
2543 this_time = num_members;
2545 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2546 domain_pol,
2547 this_time,
2548 group_rids,
2549 &names,
2550 &types);
2552 if (!NT_STATUS_IS_OK(result))
2553 return result;
2555 /* We only have users as members, but make the output
2556 the same as the output of alias members */
2558 for (i = 0; i < this_time; i++) {
2560 if (c->opt_long_list_entries) {
2561 printf("%s-%d %s\\%s %d\n", sid_str,
2562 group_rids[i], domain_name,
2563 names.names[i].string,
2564 SID_NAME_USER);
2565 } else {
2566 printf("%s\\%s\n", domain_name,
2567 names.names[i].string);
2571 num_members -= this_time;
2572 group_rids += 512;
2575 return NT_STATUS_OK;
2578 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2579 struct rpc_pipe_client *pipe_hnd,
2580 TALLOC_CTX *mem_ctx,
2581 struct policy_handle *domain_pol,
2582 uint32 rid)
2584 NTSTATUS result;
2585 struct rpc_pipe_client *lsa_pipe;
2586 struct policy_handle alias_pol, lsa_pol;
2587 uint32 num_members;
2588 struct dom_sid *alias_sids;
2589 char **domains;
2590 char **names;
2591 enum lsa_SidType *types;
2592 int i;
2593 struct lsa_SidArray sid_array;
2595 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2596 domain_pol,
2597 MAXIMUM_ALLOWED_ACCESS,
2598 rid,
2599 &alias_pol);
2601 if (!NT_STATUS_IS_OK(result))
2602 return result;
2604 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2605 &alias_pol,
2606 &sid_array);
2608 if (!NT_STATUS_IS_OK(result)) {
2609 d_fprintf(stderr, _("Couldn't list alias members\n"));
2610 return result;
2613 num_members = sid_array.num_sids;
2615 if (num_members == 0) {
2616 return NT_STATUS_OK;
2619 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2620 &ndr_table_lsarpc.syntax_id,
2621 &lsa_pipe);
2622 if (!NT_STATUS_IS_OK(result)) {
2623 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2624 nt_errstr(result) );
2625 return result;
2628 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2629 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2631 if (!NT_STATUS_IS_OK(result)) {
2632 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2633 TALLOC_FREE(lsa_pipe);
2634 return result;
2637 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, num_members);
2638 if (!alias_sids) {
2639 d_fprintf(stderr, _("Out of memory\n"));
2640 TALLOC_FREE(lsa_pipe);
2641 return NT_STATUS_NO_MEMORY;
2644 for (i=0; i<num_members; i++) {
2645 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2648 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2649 num_members, alias_sids,
2650 &domains, &names, &types);
2652 if (!NT_STATUS_IS_OK(result) &&
2653 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2654 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2655 TALLOC_FREE(lsa_pipe);
2656 return result;
2659 for (i = 0; i < num_members; i++) {
2660 fstring sid_str;
2661 sid_to_fstring(sid_str, &alias_sids[i]);
2663 if (c->opt_long_list_entries) {
2664 printf("%s %s\\%s %d\n", sid_str,
2665 domains[i] ? domains[i] : _("*unknown*"),
2666 names[i] ? names[i] : _("*unknown*"), types[i]);
2667 } else {
2668 if (domains[i])
2669 printf("%s\\%s\n", domains[i], names[i]);
2670 else
2671 printf("%s\n", sid_str);
2675 TALLOC_FREE(lsa_pipe);
2676 return NT_STATUS_OK;
2679 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2680 const struct dom_sid *domain_sid,
2681 const char *domain_name,
2682 struct cli_state *cli,
2683 struct rpc_pipe_client *pipe_hnd,
2684 TALLOC_CTX *mem_ctx,
2685 int argc,
2686 const char **argv)
2688 NTSTATUS result;
2689 struct policy_handle connect_pol, domain_pol;
2690 struct samr_Ids rids, rid_types;
2691 struct lsa_String lsa_acct_name;
2693 /* Get sam policy handle */
2695 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2696 pipe_hnd->desthost,
2697 MAXIMUM_ALLOWED_ACCESS,
2698 &connect_pol);
2700 if (!NT_STATUS_IS_OK(result))
2701 return result;
2703 /* Get domain policy handle */
2705 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2706 &connect_pol,
2707 MAXIMUM_ALLOWED_ACCESS,
2708 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2709 &domain_pol);
2711 if (!NT_STATUS_IS_OK(result))
2712 return result;
2714 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2716 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2717 &domain_pol,
2719 &lsa_acct_name,
2720 &rids,
2721 &rid_types);
2723 if (!NT_STATUS_IS_OK(result)) {
2725 /* Ok, did not find it in the global sam, try with builtin */
2727 struct dom_sid sid_Builtin;
2729 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2731 sid_copy(&sid_Builtin, &global_sid_Builtin);
2733 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2734 &connect_pol,
2735 MAXIMUM_ALLOWED_ACCESS,
2736 &sid_Builtin,
2737 &domain_pol);
2739 if (!NT_STATUS_IS_OK(result)) {
2740 d_fprintf(stderr, _("Couldn't find group %s\n"),
2741 argv[0]);
2742 return result;
2745 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2746 &domain_pol,
2748 &lsa_acct_name,
2749 &rids,
2750 &rid_types);
2752 if (!NT_STATUS_IS_OK(result)) {
2753 d_fprintf(stderr, _("Couldn't find group %s\n"),
2754 argv[0]);
2755 return result;
2759 if (rids.count != 1) {
2760 d_fprintf(stderr, _("Couldn't find group %s\n"),
2761 argv[0]);
2762 return result;
2765 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2766 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2767 domain_sid, &domain_pol,
2768 rids.ids[0]);
2771 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2772 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2773 rids.ids[0]);
2776 return NT_STATUS_NO_SUCH_GROUP;
2779 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2781 if (argc != 1 || c->display_usage) {
2782 return rpc_group_usage(c, argc, argv);
2785 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2786 rpc_group_members_internals,
2787 argc, argv);
2790 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2792 NET_API_STATUS status;
2793 struct GROUP_INFO_0 g0;
2794 uint32_t parm_err;
2796 if (argc != 2) {
2797 d_printf(_("Usage:\n"));
2798 d_printf("net rpc group rename group newname\n");
2799 return -1;
2802 g0.grpi0_name = argv[1];
2804 status = NetGroupSetInfo(c->opt_host,
2805 argv[0],
2807 (uint8_t *)&g0,
2808 &parm_err);
2810 if (status != 0) {
2811 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
2812 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2813 status));
2814 return -1;
2817 return 0;
2820 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2822 if (argc != 2 || c->display_usage) {
2823 return rpc_group_usage(c, argc, argv);
2826 return rpc_group_rename_internals(c, argc, argv);
2830 * 'net rpc group' entrypoint.
2831 * @param argc Standard main() style argc.
2832 * @param argv Standard main() style argv. Initial components are already
2833 * stripped.
2836 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2838 NET_API_STATUS status;
2840 struct functable func[] = {
2842 "add",
2843 rpc_group_add,
2844 NET_TRANSPORT_RPC,
2845 N_("Create specified group"),
2846 N_("net rpc group add\n"
2847 " Create specified group")
2850 "delete",
2851 rpc_group_delete,
2852 NET_TRANSPORT_RPC,
2853 N_("Delete specified group"),
2854 N_("net rpc group delete\n"
2855 " Delete specified group")
2858 "addmem",
2859 rpc_group_addmem,
2860 NET_TRANSPORT_RPC,
2861 N_("Add member to group"),
2862 N_("net rpc group addmem\n"
2863 " Add member to group")
2866 "delmem",
2867 rpc_group_delmem,
2868 NET_TRANSPORT_RPC,
2869 N_("Remove member from group"),
2870 N_("net rpc group delmem\n"
2871 " Remove member from group")
2874 "list",
2875 rpc_group_list,
2876 NET_TRANSPORT_RPC,
2877 N_("List groups"),
2878 N_("net rpc group list\n"
2879 " List groups")
2882 "members",
2883 rpc_group_members,
2884 NET_TRANSPORT_RPC,
2885 N_("List group members"),
2886 N_("net rpc group members\n"
2887 " List group members")
2890 "rename",
2891 rpc_group_rename,
2892 NET_TRANSPORT_RPC,
2893 N_("Rename group"),
2894 N_("net rpc group rename\n"
2895 " Rename group")
2897 {NULL, NULL, 0, NULL, NULL}
2900 status = libnetapi_init(&c->netapi_ctx);
2901 if (status != 0) {
2902 return -1;
2904 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2905 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2906 if (c->opt_kerberos) {
2907 libnetapi_set_use_kerberos(c->netapi_ctx);
2910 if (argc == 0) {
2911 if (c->display_usage) {
2912 d_printf(_("Usage:\n"));
2913 d_printf(_("net rpc group\n"
2914 " Alias for net rpc group list global "
2915 "local builtin\n"));
2916 net_display_usage_from_functable(func);
2917 return 0;
2920 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2921 rpc_group_list_internals,
2922 argc, argv);
2925 return net_run_function(c, argc, argv, "net rpc group", func);
2928 /****************************************************************************/
2930 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2932 return net_share_usage(c, argc, argv);
2936 * Add a share on a remote RPC server.
2938 * @param argc Standard main() style argc.
2939 * @param argv Standard main() style argv. Initial components are already
2940 * stripped.
2942 * @return A shell status integer (0 for success).
2945 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2947 NET_API_STATUS status;
2948 char *sharename;
2949 char *path;
2950 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2951 uint32 num_users=0, perms=0;
2952 char *password=NULL; /* don't allow a share password */
2953 struct SHARE_INFO_2 i2;
2954 uint32_t parm_error = 0;
2956 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2957 return rpc_share_usage(c, argc, argv);
2960 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2961 return -1;
2964 path = strchr(sharename, '=');
2965 if (!path) {
2966 return -1;
2969 *path++ = '\0';
2971 i2.shi2_netname = sharename;
2972 i2.shi2_type = type;
2973 i2.shi2_remark = c->opt_comment;
2974 i2.shi2_permissions = perms;
2975 i2.shi2_max_uses = c->opt_maxusers;
2976 i2.shi2_current_uses = num_users;
2977 i2.shi2_path = path;
2978 i2.shi2_passwd = password;
2980 status = NetShareAdd(c->opt_host,
2982 (uint8_t *)&i2,
2983 &parm_error);
2984 if (status != 0) {
2985 printf(_("NetShareAdd failed with: %s\n"),
2986 libnetapi_get_error_string(c->netapi_ctx, status));
2989 return status;
2993 * Delete a share on a remote RPC server.
2995 * @param domain_sid The domain sid acquired from the remote server.
2996 * @param argc Standard main() style argc.
2997 * @param argv Standard main() style argv. Initial components are already
2998 * stripped.
3000 * @return A shell status integer (0 for success).
3002 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3004 if (argc < 1 || c->display_usage) {
3005 return rpc_share_usage(c, argc, argv);
3008 return NetShareDel(c->opt_host, argv[0], 0);
3012 * Formatted print of share info
3014 * @param r pointer to SHARE_INFO_1 to format
3017 static void display_share_info_1(struct net_context *c,
3018 struct SHARE_INFO_1 *r)
3020 if (c->opt_long_list_entries) {
3021 d_printf("%-12s %-8.8s %-50s\n",
3022 r->shi1_netname,
3023 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3024 r->shi1_remark);
3025 } else {
3026 d_printf("%s\n", r->shi1_netname);
3030 static WERROR get_share_info(struct net_context *c,
3031 struct rpc_pipe_client *pipe_hnd,
3032 TALLOC_CTX *mem_ctx,
3033 uint32 level,
3034 int argc,
3035 const char **argv,
3036 struct srvsvc_NetShareInfoCtr *info_ctr)
3038 WERROR result;
3039 NTSTATUS status;
3040 union srvsvc_NetShareInfo info;
3042 /* no specific share requested, enumerate all */
3043 if (argc == 0) {
3045 uint32_t preferred_len = 0xffffffff;
3046 uint32_t total_entries = 0;
3047 uint32_t resume_handle = 0;
3049 info_ctr->level = level;
3051 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
3052 pipe_hnd->desthost,
3053 info_ctr,
3054 preferred_len,
3055 &total_entries,
3056 &resume_handle,
3057 &result);
3058 return result;
3061 /* request just one share */
3062 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
3063 pipe_hnd->desthost,
3064 argv[0],
3065 level,
3066 &info,
3067 &result);
3069 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
3070 goto done;
3073 /* construct ctr */
3074 ZERO_STRUCTP(info_ctr);
3076 info_ctr->level = level;
3078 switch (level) {
3079 case 1:
3081 struct srvsvc_NetShareCtr1 *ctr1;
3083 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3084 W_ERROR_HAVE_NO_MEMORY(ctr1);
3086 ctr1->count = 1;
3087 ctr1->array = info.info1;
3089 info_ctr->ctr.ctr1 = ctr1;
3091 case 2:
3093 struct srvsvc_NetShareCtr2 *ctr2;
3095 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3096 W_ERROR_HAVE_NO_MEMORY(ctr2);
3098 ctr2->count = 1;
3099 ctr2->array = info.info2;
3101 info_ctr->ctr.ctr2 = ctr2;
3103 case 502:
3105 struct srvsvc_NetShareCtr502 *ctr502;
3107 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3108 W_ERROR_HAVE_NO_MEMORY(ctr502);
3110 ctr502->count = 1;
3111 ctr502->array = info.info502;
3113 info_ctr->ctr.ctr502 = ctr502;
3115 } /* switch */
3116 done:
3117 return result;
3120 /***
3121 * 'net rpc share list' entrypoint.
3122 * @param argc Standard main() style argc.
3123 * @param argv Standard main() style argv. Initial components are already
3124 * stripped.
3126 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3128 NET_API_STATUS status;
3129 struct SHARE_INFO_1 *i1 = NULL;
3130 uint32_t entries_read = 0;
3131 uint32_t total_entries = 0;
3132 uint32_t resume_handle = 0;
3133 uint32_t i, level = 1;
3135 if (c->display_usage) {
3136 d_printf( "%s\n"
3137 "net rpc share list\n"
3138 " %s\n",
3139 _("Usage:"),
3140 _("List shares on remote server"));
3141 return 0;
3144 status = NetShareEnum(c->opt_host,
3145 level,
3146 (uint8_t **)(void *)&i1,
3147 (uint32_t)-1,
3148 &entries_read,
3149 &total_entries,
3150 &resume_handle);
3151 if (status != 0) {
3152 goto done;
3155 /* Display results */
3157 if (c->opt_long_list_entries) {
3158 d_printf(_(
3159 "\nEnumerating shared resources (exports) on remote server:\n\n"
3160 "\nShare name Type Description\n"
3161 "---------- ---- -----------\n"));
3163 for (i = 0; i < entries_read; i++)
3164 display_share_info_1(c, &i1[i]);
3165 done:
3166 return status;
3169 static bool check_share_availability(struct cli_state *cli, const char *netname)
3171 NTSTATUS status;
3173 status = cli_tcon_andx(cli, netname, "A:", "", 0);
3174 if (!NT_STATUS_IS_OK(status)) {
3175 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3176 return false;
3179 status = cli_tdis(cli);
3180 if (!NT_STATUS_IS_OK(status)) {
3181 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3182 return false;
3185 return true;
3188 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3189 const char *netname, uint32 type)
3191 /* only support disk shares */
3192 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3193 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3194 type);
3195 return false;
3198 /* skip builtin shares */
3199 /* FIXME: should print$ be added too ? */
3200 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3201 strequal(netname,"global"))
3202 return false;
3204 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3205 printf(_("excluding [%s]\n"), netname);
3206 return false;
3209 return check_share_availability(cli, netname);
3213 * Migrate shares from a remote RPC server to the local RPC server.
3215 * All parameters are provided by the run_rpc_command function, except for
3216 * argc, argv which are passed through.
3218 * @param domain_sid The domain sid acquired from the remote server.
3219 * @param cli A cli_state connected to the server.
3220 * @param mem_ctx Talloc context, destroyed on completion of the function.
3221 * @param argc Standard main() style argc.
3222 * @param argv Standard main() style argv. Initial components are already
3223 * stripped.
3225 * @return Normal NTSTATUS return.
3228 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3229 const struct dom_sid *domain_sid,
3230 const char *domain_name,
3231 struct cli_state *cli,
3232 struct rpc_pipe_client *pipe_hnd,
3233 TALLOC_CTX *mem_ctx,
3234 int argc,
3235 const char **argv)
3237 WERROR result;
3238 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3239 struct srvsvc_NetShareInfoCtr ctr_src;
3240 uint32 i;
3241 struct rpc_pipe_client *srvsvc_pipe = NULL;
3242 struct cli_state *cli_dst = NULL;
3243 uint32 level = 502; /* includes secdesc */
3244 uint32_t parm_error = 0;
3246 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3247 &ctr_src);
3248 if (!W_ERROR_IS_OK(result))
3249 goto done;
3251 /* connect destination PI_SRVSVC */
3252 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3253 &ndr_table_srvsvc.syntax_id);
3254 if (!NT_STATUS_IS_OK(nt_status))
3255 return nt_status;
3258 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3260 union srvsvc_NetShareInfo info;
3261 struct srvsvc_NetShareInfo502 info502 =
3262 ctr_src.ctr.ctr502->array[i];
3264 /* reset error-code */
3265 nt_status = NT_STATUS_UNSUCCESSFUL;
3267 if (!check_share_sanity(c, cli, info502.name, info502.type))
3268 continue;
3270 /* finally add the share on the dst server */
3272 printf(_("migrating: [%s], path: %s, comment: %s, without "
3273 "share-ACLs\n"),
3274 info502.name, info502.path, info502.comment);
3276 info.info502 = &info502;
3278 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3279 srvsvc_pipe->desthost,
3280 502,
3281 &info,
3282 &parm_error,
3283 &result);
3285 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3286 printf(_(" [%s] does already exist\n"),
3287 info502.name);
3288 continue;
3291 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3292 printf(_("cannot add share: %s\n"), win_errstr(result));
3293 goto done;
3298 nt_status = NT_STATUS_OK;
3300 done:
3301 if (cli_dst) {
3302 cli_shutdown(cli_dst);
3305 return nt_status;
3310 * Migrate shares from a RPC server to another.
3312 * @param argc Standard main() style argc.
3313 * @param argv Standard main() style argv. Initial components are already
3314 * stripped.
3316 * @return A shell status integer (0 for success).
3318 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3319 const char **argv)
3321 if (c->display_usage) {
3322 d_printf( "%s\n"
3323 "net rpc share migrate shares\n"
3324 " %s\n",
3325 _("Usage:"),
3326 _("Migrate shares to local server"));
3327 return 0;
3330 if (!c->opt_host) {
3331 printf(_("no server to migrate\n"));
3332 return -1;
3335 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3336 rpc_share_migrate_shares_internals,
3337 argc, argv);
3341 * Copy a file/dir
3343 * @param f file_info
3344 * @param mask current search mask
3345 * @param state arg-pointer
3348 static void copy_fn(const char *mnt, struct file_info *f,
3349 const char *mask, void *state)
3351 static NTSTATUS nt_status;
3352 static struct copy_clistate *local_state;
3353 static fstring filename, new_mask;
3354 fstring dir;
3355 char *old_dir;
3356 struct net_context *c;
3358 local_state = (struct copy_clistate *)state;
3359 nt_status = NT_STATUS_UNSUCCESSFUL;
3361 c = local_state->c;
3363 if (strequal(f->name, ".") || strequal(f->name, ".."))
3364 return;
3366 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3368 /* DIRECTORY */
3369 if (f->mode & aDIR) {
3371 DEBUG(3,("got dir: %s\n", f->name));
3373 fstrcpy(dir, local_state->cwd);
3374 fstrcat(dir, "\\");
3375 fstrcat(dir, f->name);
3377 switch (net_mode_share)
3379 case NET_MODE_SHARE_MIGRATE:
3380 /* create that directory */
3381 nt_status = net_copy_file(c, local_state->mem_ctx,
3382 local_state->cli_share_src,
3383 local_state->cli_share_dst,
3384 dir, dir,
3385 c->opt_acls? true : false,
3386 c->opt_attrs? true : false,
3387 c->opt_timestamps? true:false,
3388 false);
3389 break;
3390 default:
3391 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3392 return;
3395 if (!NT_STATUS_IS_OK(nt_status))
3396 printf(_("could not handle dir %s: %s\n"),
3397 dir, nt_errstr(nt_status));
3399 /* search below that directory */
3400 fstrcpy(new_mask, dir);
3401 fstrcat(new_mask, "\\*");
3403 old_dir = local_state->cwd;
3404 local_state->cwd = dir;
3405 if (!sync_files(local_state, new_mask))
3406 printf(_("could not handle files\n"));
3407 local_state->cwd = old_dir;
3409 return;
3413 /* FILE */
3414 fstrcpy(filename, local_state->cwd);
3415 fstrcat(filename, "\\");
3416 fstrcat(filename, f->name);
3418 DEBUG(3,("got file: %s\n", filename));
3420 switch (net_mode_share)
3422 case NET_MODE_SHARE_MIGRATE:
3423 nt_status = net_copy_file(c, local_state->mem_ctx,
3424 local_state->cli_share_src,
3425 local_state->cli_share_dst,
3426 filename, filename,
3427 c->opt_acls? true : false,
3428 c->opt_attrs? true : false,
3429 c->opt_timestamps? true: false,
3430 true);
3431 break;
3432 default:
3433 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3434 net_mode_share);
3435 return;
3438 if (!NT_STATUS_IS_OK(nt_status))
3439 printf(_("could not handle file %s: %s\n"),
3440 filename, nt_errstr(nt_status));
3445 * sync files, can be called recursivly to list files
3446 * and then call copy_fn for each file
3448 * @param cp_clistate pointer to the copy_clistate we work with
3449 * @param mask the current search mask
3451 * @return Boolean result
3453 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3455 struct cli_state *targetcli;
3456 char *targetpath = NULL;
3457 NTSTATUS status;
3459 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3461 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3462 mask, &targetcli, &targetpath ) ) {
3463 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3464 "%s\n"),
3465 mask, cli_errstr(cp_clistate->cli_share_src));
3466 return false;
3469 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
3470 copy_fn, cp_clistate);
3471 if (!NT_STATUS_IS_OK(status)) {
3472 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3473 mask, nt_errstr(status));
3474 return false;
3477 return true;
3482 * Set the top level directory permissions before we do any further copies.
3483 * Should set up ACL inheritance.
3486 bool copy_top_level_perms(struct net_context *c,
3487 struct copy_clistate *cp_clistate,
3488 const char *sharename)
3490 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3492 switch (net_mode_share) {
3493 case NET_MODE_SHARE_MIGRATE:
3494 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3495 nt_status = net_copy_fileattr(c,
3496 cp_clistate->mem_ctx,
3497 cp_clistate->cli_share_src,
3498 cp_clistate->cli_share_dst,
3499 "\\", "\\",
3500 c->opt_acls? true : false,
3501 c->opt_attrs? true : false,
3502 c->opt_timestamps? true: false,
3503 false);
3504 break;
3505 default:
3506 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3507 break;
3510 if (!NT_STATUS_IS_OK(nt_status)) {
3511 printf(_("Could handle directory attributes for top level "
3512 "directory of share %s. Error %s\n"),
3513 sharename, nt_errstr(nt_status));
3514 return false;
3517 return true;
3521 * Sync all files inside a remote share to another share (over smb).
3523 * All parameters are provided by the run_rpc_command function, except for
3524 * argc, argv which are passed through.
3526 * @param domain_sid The domain sid acquired from the remote server.
3527 * @param cli A cli_state connected to the server.
3528 * @param mem_ctx Talloc context, destroyed on completion of the function.
3529 * @param argc Standard main() style argc.
3530 * @param argv Standard main() style argv. Initial components are already
3531 * stripped.
3533 * @return Normal NTSTATUS return.
3536 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3537 const struct dom_sid *domain_sid,
3538 const char *domain_name,
3539 struct cli_state *cli,
3540 struct rpc_pipe_client *pipe_hnd,
3541 TALLOC_CTX *mem_ctx,
3542 int argc,
3543 const char **argv)
3545 WERROR result;
3546 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3547 struct srvsvc_NetShareInfoCtr ctr_src;
3548 uint32 i;
3549 uint32 level = 502;
3550 struct copy_clistate cp_clistate;
3551 bool got_src_share = false;
3552 bool got_dst_share = false;
3553 const char *mask = "\\*";
3554 char *dst = NULL;
3556 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3557 if (dst == NULL) {
3558 nt_status = NT_STATUS_NO_MEMORY;
3559 goto done;
3562 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3563 &ctr_src);
3565 if (!W_ERROR_IS_OK(result))
3566 goto done;
3568 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3570 struct srvsvc_NetShareInfo502 info502 =
3571 ctr_src.ctr.ctr502->array[i];
3573 if (!check_share_sanity(c, cli, info502.name, info502.type))
3574 continue;
3576 /* one might not want to mirror whole discs :) */
3577 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3578 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3579 info502.name);
3580 continue;
3583 switch (net_mode_share)
3585 case NET_MODE_SHARE_MIGRATE:
3586 printf("syncing");
3587 break;
3588 default:
3589 d_fprintf(stderr, _("Unsupported mode %d\n"),
3590 net_mode_share);
3591 break;
3593 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3594 "Attributes %s\n"),
3595 info502.name,
3596 c->opt_acls ? _("including") : _("without"),
3597 c->opt_attrs ? _("including") : _("without"),
3598 c->opt_timestamps ? _("(preserving timestamps)") : "");
3600 cp_clistate.mem_ctx = mem_ctx;
3601 cp_clistate.cli_share_src = NULL;
3602 cp_clistate.cli_share_dst = NULL;
3603 cp_clistate.cwd = NULL;
3604 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3605 cp_clistate.c = c;
3607 /* open share source */
3608 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3609 &cli->dest_ss, cli->desthost,
3610 info502.name, "A:");
3611 if (!NT_STATUS_IS_OK(nt_status))
3612 goto done;
3614 got_src_share = true;
3616 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3617 /* open share destination */
3618 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3619 NULL, dst, info502.name, "A:");
3620 if (!NT_STATUS_IS_OK(nt_status))
3621 goto done;
3623 got_dst_share = true;
3626 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3627 d_fprintf(stderr, _("Could not handle the top level "
3628 "directory permissions for the "
3629 "share: %s\n"), info502.name);
3630 nt_status = NT_STATUS_UNSUCCESSFUL;
3631 goto done;
3634 if (!sync_files(&cp_clistate, mask)) {
3635 d_fprintf(stderr, _("could not handle files for share: "
3636 "%s\n"), info502.name);
3637 nt_status = NT_STATUS_UNSUCCESSFUL;
3638 goto done;
3642 nt_status = NT_STATUS_OK;
3644 done:
3646 if (got_src_share)
3647 cli_shutdown(cp_clistate.cli_share_src);
3649 if (got_dst_share)
3650 cli_shutdown(cp_clistate.cli_share_dst);
3652 SAFE_FREE(dst);
3653 return nt_status;
3657 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3659 if (c->display_usage) {
3660 d_printf( "%s\n"
3661 "net share migrate files\n"
3662 " %s\n",
3663 _("Usage:"),
3664 _("Migrate files to local server"));
3665 return 0;
3668 if (!c->opt_host) {
3669 d_printf(_("no server to migrate\n"));
3670 return -1;
3673 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3674 rpc_share_migrate_files_internals,
3675 argc, argv);
3679 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3681 * All parameters are provided by the run_rpc_command function, except for
3682 * argc, argv which are passed through.
3684 * @param domain_sid The domain sid acquired from the remote server.
3685 * @param cli A cli_state connected to the server.
3686 * @param mem_ctx Talloc context, destroyed on completion of the function.
3687 * @param argc Standard main() style argc.
3688 * @param argv Standard main() style argv. Initial components are already
3689 * stripped.
3691 * @return Normal NTSTATUS return.
3694 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3695 const struct dom_sid *domain_sid,
3696 const char *domain_name,
3697 struct cli_state *cli,
3698 struct rpc_pipe_client *pipe_hnd,
3699 TALLOC_CTX *mem_ctx,
3700 int argc,
3701 const char **argv)
3703 WERROR result;
3704 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3705 struct srvsvc_NetShareInfoCtr ctr_src;
3706 union srvsvc_NetShareInfo info;
3707 uint32 i;
3708 struct rpc_pipe_client *srvsvc_pipe = NULL;
3709 struct cli_state *cli_dst = NULL;
3710 uint32 level = 502; /* includes secdesc */
3711 uint32_t parm_error = 0;
3713 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3714 &ctr_src);
3716 if (!W_ERROR_IS_OK(result))
3717 goto done;
3719 /* connect destination PI_SRVSVC */
3720 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3721 &ndr_table_srvsvc.syntax_id);
3722 if (!NT_STATUS_IS_OK(nt_status))
3723 return nt_status;
3726 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3728 struct srvsvc_NetShareInfo502 info502 =
3729 ctr_src.ctr.ctr502->array[i];
3731 /* reset error-code */
3732 nt_status = NT_STATUS_UNSUCCESSFUL;
3734 if (!check_share_sanity(c, cli, info502.name, info502.type))
3735 continue;
3737 printf(_("migrating: [%s], path: %s, comment: %s, including "
3738 "share-ACLs\n"),
3739 info502.name, info502.path, info502.comment);
3741 if (c->opt_verbose)
3742 display_sec_desc(info502.sd_buf.sd);
3744 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3745 info.info502 = &info502;
3747 /* finally modify the share on the dst server */
3748 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3749 srvsvc_pipe->desthost,
3750 info502.name,
3751 level,
3752 &info,
3753 &parm_error,
3754 &result);
3755 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3756 printf(_("cannot set share-acl: %s\n"),
3757 win_errstr(result));
3758 goto done;
3763 nt_status = NT_STATUS_OK;
3765 done:
3766 if (cli_dst) {
3767 cli_shutdown(cli_dst);
3770 return nt_status;
3775 * Migrate share-acls from a RPC server to another.
3777 * @param argc Standard main() style argc.
3778 * @param argv Standard main() style argv. Initial components are already
3779 * stripped.
3781 * @return A shell status integer (0 for success).
3783 static int rpc_share_migrate_security(struct net_context *c, int argc,
3784 const char **argv)
3786 if (c->display_usage) {
3787 d_printf( "%s\n"
3788 "net rpc share migrate security\n"
3789 " %s\n",
3790 _("Usage:"),
3791 _("Migrate share-acls to local server"));
3792 return 0;
3795 if (!c->opt_host) {
3796 d_printf(_("no server to migrate\n"));
3797 return -1;
3800 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3801 rpc_share_migrate_security_internals,
3802 argc, argv);
3806 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3807 * from one server to another.
3809 * @param argc Standard main() style argc.
3810 * @param argv Standard main() style argv. Initial components are already
3811 * stripped.
3813 * @return A shell status integer (0 for success).
3816 static int rpc_share_migrate_all(struct net_context *c, int argc,
3817 const char **argv)
3819 int ret;
3821 if (c->display_usage) {
3822 d_printf( "%s\n"
3823 "net rpc share migrate all\n"
3824 " %s\n",
3825 _("Usage:"),
3826 _("Migrates shares including all share settings"));
3827 return 0;
3830 if (!c->opt_host) {
3831 d_printf(_("no server to migrate\n"));
3832 return -1;
3835 /* order is important. we don't want to be locked out by the share-acl
3836 * before copying files - gd */
3838 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3839 rpc_share_migrate_shares_internals, argc, argv);
3840 if (ret)
3841 return ret;
3843 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3844 rpc_share_migrate_files_internals, argc, argv);
3845 if (ret)
3846 return ret;
3848 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3849 rpc_share_migrate_security_internals, argc,
3850 argv);
3855 * 'net rpc share migrate' entrypoint.
3856 * @param argc Standard main() style argc.
3857 * @param argv Standard main() style argv. Initial components are already
3858 * stripped.
3860 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3863 struct functable func[] = {
3865 "all",
3866 rpc_share_migrate_all,
3867 NET_TRANSPORT_RPC,
3868 N_("Migrate shares from remote to local server"),
3869 N_("net rpc share migrate all\n"
3870 " Migrate shares from remote to local server")
3873 "files",
3874 rpc_share_migrate_files,
3875 NET_TRANSPORT_RPC,
3876 N_("Migrate files from remote to local server"),
3877 N_("net rpc share migrate files\n"
3878 " Migrate files from remote to local server")
3881 "security",
3882 rpc_share_migrate_security,
3883 NET_TRANSPORT_RPC,
3884 N_("Migrate share-ACLs from remote to local server"),
3885 N_("net rpc share migrate security\n"
3886 " Migrate share-ACLs from remote to local server")
3889 "shares",
3890 rpc_share_migrate_shares,
3891 NET_TRANSPORT_RPC,
3892 N_("Migrate shares from remote to local server"),
3893 N_("net rpc share migrate shares\n"
3894 " Migrate shares from remote to local server")
3896 {NULL, NULL, 0, NULL, NULL}
3899 net_mode_share = NET_MODE_SHARE_MIGRATE;
3901 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3904 struct full_alias {
3905 struct dom_sid sid;
3906 uint32 num_members;
3907 struct dom_sid *members;
3910 static int num_server_aliases;
3911 static struct full_alias *server_aliases;
3914 * Add an alias to the static list.
3916 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3918 if (server_aliases == NULL)
3919 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3921 server_aliases[num_server_aliases] = *alias;
3922 num_server_aliases += 1;
3926 * For a specific domain on the server, fetch all the aliases
3927 * and their members. Add all of them to the server_aliases.
3930 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3931 TALLOC_CTX *mem_ctx,
3932 struct policy_handle *connect_pol,
3933 const struct dom_sid *domain_sid)
3935 uint32 start_idx, max_entries, num_entries, i;
3936 struct samr_SamArray *groups = NULL;
3937 NTSTATUS result;
3938 struct policy_handle domain_pol;
3940 /* Get domain policy handle */
3942 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3943 connect_pol,
3944 MAXIMUM_ALLOWED_ACCESS,
3945 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3946 &domain_pol);
3947 if (!NT_STATUS_IS_OK(result))
3948 return result;
3950 start_idx = 0;
3951 max_entries = 250;
3953 do {
3954 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3955 &domain_pol,
3956 &start_idx,
3957 &groups,
3958 max_entries,
3959 &num_entries);
3960 for (i = 0; i < num_entries; i++) {
3962 struct policy_handle alias_pol;
3963 struct full_alias alias;
3964 struct lsa_SidArray sid_array;
3965 int j;
3967 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3968 &domain_pol,
3969 MAXIMUM_ALLOWED_ACCESS,
3970 groups->entries[i].idx,
3971 &alias_pol);
3972 if (!NT_STATUS_IS_OK(result))
3973 goto done;
3975 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3976 &alias_pol,
3977 &sid_array);
3978 if (!NT_STATUS_IS_OK(result))
3979 goto done;
3981 alias.num_members = sid_array.num_sids;
3983 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3984 if (!NT_STATUS_IS_OK(result))
3985 goto done;
3987 alias.members = NULL;
3989 if (alias.num_members > 0) {
3990 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
3992 for (j = 0; j < alias.num_members; j++)
3993 sid_copy(&alias.members[j],
3994 sid_array.sids[j].sid);
3997 sid_compose(&alias.sid, domain_sid,
3998 groups->entries[i].idx);
4000 push_alias(mem_ctx, &alias);
4002 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4004 result = NT_STATUS_OK;
4006 done:
4007 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4009 return result;
4013 * Dump server_aliases as names for debugging purposes.
4016 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4017 const struct dom_sid *domain_sid,
4018 const char *domain_name,
4019 struct cli_state *cli,
4020 struct rpc_pipe_client *pipe_hnd,
4021 TALLOC_CTX *mem_ctx,
4022 int argc,
4023 const char **argv)
4025 int i;
4026 NTSTATUS result;
4027 struct policy_handle lsa_pol;
4029 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4030 SEC_FLAG_MAXIMUM_ALLOWED,
4031 &lsa_pol);
4032 if (!NT_STATUS_IS_OK(result))
4033 return result;
4035 for (i=0; i<num_server_aliases; i++) {
4036 char **names;
4037 char **domains;
4038 enum lsa_SidType *types;
4039 int j;
4041 struct full_alias *alias = &server_aliases[i];
4043 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4044 &alias->sid,
4045 &domains, &names, &types);
4046 if (!NT_STATUS_IS_OK(result))
4047 continue;
4049 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4051 if (alias->num_members == 0) {
4052 DEBUG(1, ("\n"));
4053 continue;
4056 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4057 alias->num_members,
4058 alias->members,
4059 &domains, &names, &types);
4061 if (!NT_STATUS_IS_OK(result) &&
4062 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4063 continue;
4065 for (j=0; j<alias->num_members; j++)
4066 DEBUG(1, ("%s\\%s (%d); ",
4067 domains[j] ? domains[j] : "*unknown*",
4068 names[j] ? names[j] : "*unknown*",types[j]));
4069 DEBUG(1, ("\n"));
4072 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4074 return NT_STATUS_OK;
4078 * Fetch a list of all server aliases and their members into
4079 * server_aliases.
4082 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4083 const struct dom_sid *domain_sid,
4084 const char *domain_name,
4085 struct cli_state *cli,
4086 struct rpc_pipe_client *pipe_hnd,
4087 TALLOC_CTX *mem_ctx,
4088 int argc,
4089 const char **argv)
4091 NTSTATUS result;
4092 struct policy_handle connect_pol;
4094 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4095 pipe_hnd->desthost,
4096 MAXIMUM_ALLOWED_ACCESS,
4097 &connect_pol);
4099 if (!NT_STATUS_IS_OK(result))
4100 goto done;
4102 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4103 &global_sid_Builtin);
4105 if (!NT_STATUS_IS_OK(result))
4106 goto done;
4108 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4109 domain_sid);
4111 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4112 done:
4113 return result;
4116 static void init_user_token(NT_USER_TOKEN *token, struct dom_sid *user_sid)
4118 token->num_sids = 4;
4120 if (!(token->user_sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4121 d_fprintf(stderr, "malloc %s\n",_("failed"));
4122 token->num_sids = 0;
4123 return;
4126 token->user_sids[0] = *user_sid;
4127 sid_copy(&token->user_sids[1], &global_sid_World);
4128 sid_copy(&token->user_sids[2], &global_sid_Network);
4129 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4132 static void free_user_token(NT_USER_TOKEN *token)
4134 SAFE_FREE(token->user_sids);
4137 static void add_sid_to_token(NT_USER_TOKEN *token, struct dom_sid *sid)
4139 if (is_sid_in_token(token, sid))
4140 return;
4142 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, struct dom_sid, token->num_sids+1);
4143 if (!token->user_sids) {
4144 return;
4147 sid_copy(&token->user_sids[token->num_sids], sid);
4149 token->num_sids += 1;
4152 struct user_token {
4153 fstring name;
4154 NT_USER_TOKEN token;
4157 static void dump_user_token(struct user_token *token)
4159 int i;
4161 d_printf("%s\n", token->name);
4163 for (i=0; i<token->token.num_sids; i++) {
4164 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4168 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4170 int i;
4172 for (i=0; i<alias->num_members; i++) {
4173 if (sid_compare(sid, &alias->members[i]) == 0)
4174 return true;
4177 return false;
4180 static void collect_sid_memberships(NT_USER_TOKEN *token, struct dom_sid sid)
4182 int i;
4184 for (i=0; i<num_server_aliases; i++) {
4185 if (is_alias_member(&sid, &server_aliases[i]))
4186 add_sid_to_token(token, &server_aliases[i].sid);
4191 * We got a user token with all the SIDs we can know about without asking the
4192 * server directly. These are the user and domain group sids. All of these can
4193 * be members of aliases. So scan the list of aliases for each of the SIDs and
4194 * add them to the token.
4197 static void collect_alias_memberships(NT_USER_TOKEN *token)
4199 int num_global_sids = token->num_sids;
4200 int i;
4202 for (i=0; i<num_global_sids; i++) {
4203 collect_sid_memberships(token, token->user_sids[i]);
4207 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4209 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4210 enum wbcSidType type;
4211 fstring full_name;
4212 struct wbcDomainSid wsid;
4213 char *sid_str = NULL;
4214 struct dom_sid user_sid;
4215 uint32_t num_groups;
4216 gid_t *groups = NULL;
4217 uint32_t i;
4219 fstr_sprintf(full_name, "%s%c%s",
4220 domain, *lp_winbind_separator(), user);
4222 /* First let's find out the user sid */
4224 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4226 if (!WBC_ERROR_IS_OK(wbc_status)) {
4227 DEBUG(1, ("winbind could not find %s: %s\n",
4228 full_name, wbcErrorString(wbc_status)));
4229 return false;
4232 wbc_status = wbcSidToString(&wsid, &sid_str);
4233 if (!WBC_ERROR_IS_OK(wbc_status)) {
4234 return false;
4237 if (type != SID_NAME_USER) {
4238 wbcFreeMemory(sid_str);
4239 DEBUG(1, ("%s is not a user\n", full_name));
4240 return false;
4243 if (!string_to_sid(&user_sid, sid_str)) {
4244 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4245 return false;
4248 wbcFreeMemory(sid_str);
4249 sid_str = NULL;
4251 init_user_token(token, &user_sid);
4253 /* And now the groups winbind knows about */
4255 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4256 if (!WBC_ERROR_IS_OK(wbc_status)) {
4257 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4258 full_name, wbcErrorString(wbc_status)));
4259 return false;
4262 for (i = 0; i < num_groups; i++) {
4263 gid_t gid = groups[i];
4264 struct dom_sid sid;
4266 wbc_status = wbcGidToSid(gid, &wsid);
4267 if (!WBC_ERROR_IS_OK(wbc_status)) {
4268 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4269 (unsigned int)gid, wbcErrorString(wbc_status)));
4270 wbcFreeMemory(groups);
4271 return false;
4274 wbc_status = wbcSidToString(&wsid, &sid_str);
4275 if (!WBC_ERROR_IS_OK(wbc_status)) {
4276 wbcFreeMemory(groups);
4277 return false;
4280 DEBUG(3, (" %s\n", sid_str));
4282 string_to_sid(&sid, sid_str);
4283 wbcFreeMemory(sid_str);
4284 sid_str = NULL;
4286 add_sid_to_token(token, &sid);
4288 wbcFreeMemory(groups);
4290 return true;
4294 * Get a list of all user tokens we want to look at
4297 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4298 struct user_token **user_tokens)
4300 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4301 uint32_t i, num_users;
4302 const char **users;
4303 struct user_token *result;
4304 TALLOC_CTX *frame = NULL;
4306 if (lp_winbind_use_default_domain() &&
4307 (c->opt_target_workgroup == NULL)) {
4308 d_fprintf(stderr, _("winbind use default domain = yes set, "
4309 "please specify a workgroup\n"));
4310 return false;
4313 /* Send request to winbind daemon */
4315 wbc_status = wbcListUsers(NULL, &num_users, &users);
4316 if (!WBC_ERROR_IS_OK(wbc_status)) {
4317 DEBUG(1, (_("winbind could not list users: %s\n"),
4318 wbcErrorString(wbc_status)));
4319 return false;
4322 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4324 if (result == NULL) {
4325 DEBUG(1, ("Could not malloc sid array\n"));
4326 wbcFreeMemory(users);
4327 return false;
4330 frame = talloc_stackframe();
4331 for (i=0; i < num_users; i++) {
4332 fstring domain, user;
4333 char *p;
4335 fstrcpy(result[i].name, users[i]);
4337 p = strchr(users[i], *lp_winbind_separator());
4339 DEBUG(3, ("%s\n", users[i]));
4341 if (p == NULL) {
4342 fstrcpy(domain, c->opt_target_workgroup);
4343 fstrcpy(user, users[i]);
4344 } else {
4345 *p++ = '\0';
4346 fstrcpy(domain, users[i]);
4347 strupper_m(domain);
4348 fstrcpy(user, p);
4351 get_user_sids(domain, user, &(result[i].token));
4352 i+=1;
4354 TALLOC_FREE(frame);
4355 wbcFreeMemory(users);
4357 *num_tokens = num_users;
4358 *user_tokens = result;
4360 return true;
4363 static bool get_user_tokens_from_file(FILE *f,
4364 int *num_tokens,
4365 struct user_token **tokens)
4367 struct user_token *token = NULL;
4369 while (!feof(f)) {
4370 fstring line;
4372 if (fgets(line, sizeof(line)-1, f) == NULL) {
4373 return true;
4376 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4377 line[strlen(line)-1] = '\0';
4380 if (line[0] == ' ') {
4381 /* We have a SID */
4383 struct dom_sid sid;
4384 if(!string_to_sid(&sid, &line[1])) {
4385 DEBUG(1,("get_user_tokens_from_file: Could "
4386 "not convert sid %s \n",&line[1]));
4387 return false;
4390 if (token == NULL) {
4391 DEBUG(0, ("File does not begin with username"));
4392 return false;
4395 add_sid_to_token(&token->token, &sid);
4396 continue;
4399 /* And a new user... */
4401 *num_tokens += 1;
4402 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4403 if (*tokens == NULL) {
4404 DEBUG(0, ("Could not realloc tokens\n"));
4405 return false;
4408 token = &((*tokens)[*num_tokens-1]);
4410 fstrcpy(token->name, line);
4411 token->token.num_sids = 0;
4412 token->token.user_sids = NULL;
4413 continue;
4416 return false;
4421 * Show the list of all users that have access to a share
4424 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4425 TALLOC_CTX *mem_ctx,
4426 const char *netname,
4427 int num_tokens,
4428 struct user_token *tokens)
4430 uint16_t fnum;
4431 struct security_descriptor *share_sd = NULL;
4432 struct security_descriptor *root_sd = NULL;
4433 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4434 int i;
4435 union srvsvc_NetShareInfo info;
4436 WERROR result;
4437 NTSTATUS status;
4438 uint16 cnum;
4440 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4441 pipe_hnd->desthost,
4442 netname,
4443 502,
4444 &info,
4445 &result);
4447 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4448 DEBUG(1, ("Coult not query secdesc for share %s\n",
4449 netname));
4450 return;
4453 share_sd = info.info502->sd_buf.sd;
4454 if (share_sd == NULL) {
4455 DEBUG(1, ("Got no secdesc for share %s\n",
4456 netname));
4459 cnum = cli->cnum;
4461 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4462 return;
4465 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4466 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4467 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4470 for (i=0; i<num_tokens; i++) {
4471 uint32 acc_granted;
4473 if (share_sd != NULL) {
4474 status = se_access_check(share_sd, &tokens[i].token,
4475 1, &acc_granted);
4477 if (!NT_STATUS_IS_OK(status)) {
4478 DEBUG(1, ("Could not check share_sd for "
4479 "user %s\n",
4480 tokens[i].name));
4481 continue;
4485 if (root_sd == NULL) {
4486 d_printf(" %s\n", tokens[i].name);
4487 continue;
4490 status = se_access_check(root_sd, &tokens[i].token,
4491 1, &acc_granted);
4492 if (!NT_STATUS_IS_OK(status)) {
4493 DEBUG(1, ("Could not check root_sd for user %s\n",
4494 tokens[i].name));
4495 continue;
4497 d_printf(" %s\n", tokens[i].name);
4500 if (fnum != (uint16_t)-1)
4501 cli_close(cli, fnum);
4502 cli_tdis(cli);
4503 cli->cnum = cnum;
4505 return;
4508 struct share_list {
4509 int num_shares;
4510 char **shares;
4513 static void collect_share(const char *name, uint32 m,
4514 const char *comment, void *state)
4516 struct share_list *share_list = (struct share_list *)state;
4518 if (m != STYPE_DISKTREE)
4519 return;
4521 share_list->num_shares += 1;
4522 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4523 if (!share_list->shares) {
4524 share_list->num_shares = 0;
4525 return;
4527 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4531 * List shares on a remote RPC server, including the security descriptors.
4533 * All parameters are provided by the run_rpc_command function, except for
4534 * argc, argv which are passed through.
4536 * @param domain_sid The domain sid acquired from the remote server.
4537 * @param cli A cli_state connected to the server.
4538 * @param mem_ctx Talloc context, destroyed on completion of the function.
4539 * @param argc Standard main() style argc.
4540 * @param argv Standard main() style argv. Initial components are already
4541 * stripped.
4543 * @return Normal NTSTATUS return.
4546 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4547 const struct dom_sid *domain_sid,
4548 const char *domain_name,
4549 struct cli_state *cli,
4550 struct rpc_pipe_client *pipe_hnd,
4551 TALLOC_CTX *mem_ctx,
4552 int argc,
4553 const char **argv)
4555 int ret;
4556 bool r;
4557 uint32 i;
4558 FILE *f;
4560 struct user_token *tokens = NULL;
4561 int num_tokens = 0;
4563 struct share_list share_list;
4565 if (argc == 0) {
4566 f = stdin;
4567 } else {
4568 f = fopen(argv[0], "r");
4571 if (f == NULL) {
4572 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4573 return NT_STATUS_UNSUCCESSFUL;
4576 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4578 if (f != stdin)
4579 fclose(f);
4581 if (!r) {
4582 DEBUG(0, ("Could not read users from file\n"));
4583 return NT_STATUS_UNSUCCESSFUL;
4586 for (i=0; i<num_tokens; i++)
4587 collect_alias_memberships(&tokens[i].token);
4589 share_list.num_shares = 0;
4590 share_list.shares = NULL;
4592 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4594 if (ret == -1) {
4595 DEBUG(0, ("Error returning browse list: %s\n",
4596 cli_errstr(cli)));
4597 goto done;
4600 for (i = 0; i < share_list.num_shares; i++) {
4601 char *netname = share_list.shares[i];
4603 if (netname[strlen(netname)-1] == '$')
4604 continue;
4606 d_printf("%s\n", netname);
4608 show_userlist(pipe_hnd, mem_ctx, netname,
4609 num_tokens, tokens);
4611 done:
4612 for (i=0; i<num_tokens; i++) {
4613 free_user_token(&tokens[i].token);
4615 SAFE_FREE(tokens);
4616 SAFE_FREE(share_list.shares);
4618 return NT_STATUS_OK;
4621 static int rpc_share_allowedusers(struct net_context *c, int argc,
4622 const char **argv)
4624 int result;
4626 if (c->display_usage) {
4627 d_printf( "%s\n"
4628 "net rpc share allowedusers\n"
4629 " %s\n",
4630 _("Usage:"),
4631 _("List allowed users"));
4632 return 0;
4635 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4636 rpc_aliaslist_internals,
4637 argc, argv);
4638 if (result != 0)
4639 return result;
4641 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4642 rpc_aliaslist_dump,
4643 argc, argv);
4644 if (result != 0)
4645 return result;
4647 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4648 rpc_share_allowedusers_internals,
4649 argc, argv);
4652 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4654 int num_tokens = 0;
4655 struct user_token *tokens = NULL;
4656 int i;
4658 if (argc != 0) {
4659 net_usersidlist_usage(c, argc, argv);
4660 return 0;
4663 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4664 DEBUG(0, ("Could not get the user/sid list\n"));
4665 return 0;
4668 for (i=0; i<num_tokens; i++) {
4669 dump_user_token(&tokens[i]);
4670 free_user_token(&tokens[i].token);
4673 SAFE_FREE(tokens);
4674 return 1;
4677 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4679 d_printf(_("net usersidlist\n"
4680 "\tprints out a list of all users the running winbind knows\n"
4681 "\tabout, together with all their SIDs. This is used as\n"
4682 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
4684 net_common_flags_usage(c, argc, argv);
4685 return -1;
4689 * 'net rpc share' entrypoint.
4690 * @param argc Standard main() style argc.
4691 * @param argv Standard main() style argv. Initial components are already
4692 * stripped.
4695 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4697 NET_API_STATUS status;
4699 struct functable func[] = {
4701 "add",
4702 rpc_share_add,
4703 NET_TRANSPORT_RPC,
4704 N_("Add share"),
4705 N_("net rpc share add\n"
4706 " Add share")
4709 "delete",
4710 rpc_share_delete,
4711 NET_TRANSPORT_RPC,
4712 N_("Remove share"),
4713 N_("net rpc share delete\n"
4714 " Remove share")
4717 "allowedusers",
4718 rpc_share_allowedusers,
4719 NET_TRANSPORT_RPC,
4720 N_("Modify allowed users"),
4721 N_("net rpc share allowedusers\n"
4722 " Modify allowed users")
4725 "migrate",
4726 rpc_share_migrate,
4727 NET_TRANSPORT_RPC,
4728 N_("Migrate share to local server"),
4729 N_("net rpc share migrate\n"
4730 " Migrate share to local server")
4733 "list",
4734 rpc_share_list,
4735 NET_TRANSPORT_RPC,
4736 N_("List shares"),
4737 N_("net rpc share list\n"
4738 " List shares")
4740 {NULL, NULL, 0, NULL, NULL}
4743 status = libnetapi_init(&c->netapi_ctx);
4744 if (status != 0) {
4745 return -1;
4747 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4748 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4749 if (c->opt_kerberos) {
4750 libnetapi_set_use_kerberos(c->netapi_ctx);
4753 if (argc == 0) {
4754 if (c->display_usage) {
4755 d_printf("%s\n%s",
4756 _("Usage:"),
4757 _("net rpc share\n"
4758 " List shares\n"
4759 " Alias for net rpc share list\n"));
4760 net_display_usage_from_functable(func);
4761 return 0;
4764 return rpc_share_list(c, argc, argv);
4767 return net_run_function(c, argc, argv, "net rpc share", func);
4770 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4771 TALLOC_CTX *mem_ctx,
4772 struct rpc_sh_ctx *ctx,
4773 struct rpc_pipe_client *pipe_hnd,
4774 int argc, const char **argv)
4777 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4780 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4781 TALLOC_CTX *mem_ctx,
4782 struct rpc_sh_ctx *ctx,
4783 struct rpc_pipe_client *pipe_hnd,
4784 int argc, const char **argv)
4786 NET_API_STATUS status;
4787 uint32_t parm_err = 0;
4788 struct SHARE_INFO_2 i2;
4790 if ((argc < 2) || (argc > 3)) {
4791 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
4792 ctx->whoami);
4793 return NT_STATUS_INVALID_PARAMETER;
4796 i2.shi2_netname = argv[0];
4797 i2.shi2_type = STYPE_DISKTREE;
4798 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4799 i2.shi2_permissions = 0;
4800 i2.shi2_max_uses = 0;
4801 i2.shi2_current_uses = 0;
4802 i2.shi2_path = argv[1];
4803 i2.shi2_passwd = NULL;
4805 status = NetShareAdd(pipe_hnd->desthost,
4807 (uint8_t *)&i2,
4808 &parm_err);
4810 return werror_to_ntstatus(W_ERROR(status));
4813 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4814 TALLOC_CTX *mem_ctx,
4815 struct rpc_sh_ctx *ctx,
4816 struct rpc_pipe_client *pipe_hnd,
4817 int argc, const char **argv)
4819 if (argc != 1) {
4820 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
4821 return NT_STATUS_INVALID_PARAMETER;
4824 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4827 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4828 TALLOC_CTX *mem_ctx,
4829 struct rpc_sh_ctx *ctx,
4830 struct rpc_pipe_client *pipe_hnd,
4831 int argc, const char **argv)
4833 union srvsvc_NetShareInfo info;
4834 WERROR result;
4835 NTSTATUS status;
4837 if (argc != 1) {
4838 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
4839 return NT_STATUS_INVALID_PARAMETER;
4842 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4843 pipe_hnd->desthost,
4844 argv[0],
4846 &info,
4847 &result);
4848 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4849 goto done;
4852 d_printf(_("Name: %s\n"), info.info2->name);
4853 d_printf(_("Comment: %s\n"), info.info2->comment);
4854 d_printf(_("Path: %s\n"), info.info2->path);
4855 d_printf(_("Password: %s\n"), info.info2->password);
4857 done:
4858 return werror_to_ntstatus(result);
4861 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4862 struct rpc_sh_ctx *ctx)
4864 static struct rpc_sh_cmd cmds[] = {
4866 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4867 N_("List available shares") },
4869 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4870 N_("Add a share") },
4872 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4873 N_("Delete a share") },
4875 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4876 N_("Get information about a share") },
4878 { NULL, NULL, 0, NULL, NULL }
4881 return cmds;
4884 /****************************************************************************/
4886 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4888 return net_file_usage(c, argc, argv);
4892 * Close a file on a remote RPC server.
4894 * @param argc Standard main() style argc.
4895 * @param argv Standard main() style argv. Initial components are already
4896 * stripped.
4898 * @return A shell status integer (0 for success).
4900 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4902 if (argc < 1 || c->display_usage) {
4903 return rpc_file_usage(c, argc, argv);
4906 return NetFileClose(c->opt_host, atoi(argv[0]));
4910 * Formatted print of open file info
4912 * @param r struct FILE_INFO_3 contents
4915 static void display_file_info_3(struct FILE_INFO_3 *r)
4917 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4918 r->fi3_id, r->fi3_username, r->fi3_permissions,
4919 r->fi3_num_locks, r->fi3_pathname);
4923 * List files for a user on a remote RPC server.
4925 * @param argc Standard main() style argc.
4926 * @param argv Standard main() style argv. Initial components are already
4927 * stripped.
4929 * @return A shell status integer (0 for success)..
4932 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4934 NET_API_STATUS status;
4935 uint32 preferred_len = 0xffffffff, i;
4936 const char *username=NULL;
4937 uint32_t total_entries = 0;
4938 uint32_t entries_read = 0;
4939 uint32_t resume_handle = 0;
4940 struct FILE_INFO_3 *i3 = NULL;
4942 if (c->display_usage) {
4943 return rpc_file_usage(c, argc, argv);
4946 /* if argc > 0, must be user command */
4947 if (argc > 0) {
4948 username = smb_xstrdup(argv[0]);
4951 status = NetFileEnum(c->opt_host,
4952 NULL,
4953 username,
4955 (uint8_t **)(void *)&i3,
4956 preferred_len,
4957 &entries_read,
4958 &total_entries,
4959 &resume_handle);
4961 if (status != 0) {
4962 goto done;
4965 /* Display results */
4967 d_printf(_(
4968 "\nEnumerating open files on remote server:\n\n"
4969 "\nFileId Opened by Perms Locks Path"
4970 "\n------ --------- ----- ----- ---- \n"));
4971 for (i = 0; i < entries_read; i++) {
4972 display_file_info_3(&i3[i]);
4974 done:
4975 return status;
4979 * 'net rpc file' entrypoint.
4980 * @param argc Standard main() style argc.
4981 * @param argv Standard main() style argv. Initial components are already
4982 * stripped.
4985 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4987 NET_API_STATUS status;
4989 struct functable func[] = {
4991 "close",
4992 rpc_file_close,
4993 NET_TRANSPORT_RPC,
4994 N_("Close opened file"),
4995 N_("net rpc file close\n"
4996 " Close opened file")
4999 "user",
5000 rpc_file_user,
5001 NET_TRANSPORT_RPC,
5002 N_("List files opened by user"),
5003 N_("net rpc file user\n"
5004 " List files opened by user")
5006 #if 0
5008 "info",
5009 rpc_file_info,
5010 NET_TRANSPORT_RPC,
5011 N_("Display information about opened file"),
5012 N_("net rpc file info\n"
5013 " Display information about opened file")
5015 #endif
5016 {NULL, NULL, 0, NULL, NULL}
5019 status = libnetapi_init(&c->netapi_ctx);
5020 if (status != 0) {
5021 return -1;
5023 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5024 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5025 if (c->opt_kerberos) {
5026 libnetapi_set_use_kerberos(c->netapi_ctx);
5029 if (argc == 0) {
5030 if (c->display_usage) {
5031 d_printf(_("Usage:\n"));
5032 d_printf(_("net rpc file\n"
5033 " List opened files\n"));
5034 net_display_usage_from_functable(func);
5035 return 0;
5038 return rpc_file_user(c, argc, argv);
5041 return net_run_function(c, argc, argv, "net rpc file", func);
5045 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5047 * All parameters are provided by the run_rpc_command function, except for
5048 * argc, argv which are passed through.
5050 * @param c A net_context structure.
5051 * @param domain_sid The domain sid acquired from the remote server.
5052 * @param cli A cli_state connected to the server.
5053 * @param mem_ctx Talloc context, destroyed on completion of the function.
5054 * @param argc Standard main() style argc.
5055 * @param argv Standard main() style argv. Initial components are already
5056 * stripped.
5058 * @return Normal NTSTATUS return.
5061 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5062 const struct dom_sid *domain_sid,
5063 const char *domain_name,
5064 struct cli_state *cli,
5065 struct rpc_pipe_client *pipe_hnd,
5066 TALLOC_CTX *mem_ctx,
5067 int argc,
5068 const char **argv)
5070 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5072 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5074 if (NT_STATUS_IS_OK(result)) {
5075 d_printf(_("\nShutdown successfully aborted\n"));
5076 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5077 } else
5078 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5080 return result;
5084 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5086 * All parameters are provided by the run_rpc_command function, except for
5087 * argc, argv which are passed through.
5089 * @param c A net_context structure.
5090 * @param domain_sid The domain sid acquired from the remote server.
5091 * @param cli A cli_state connected to the server.
5092 * @param mem_ctx Talloc context, destroyed on completion of the function.
5093 * @param argc Standard main() style argc.
5094 * @param argv Standard main() style argv. Initial components are already
5095 * stripped.
5097 * @return Normal NTSTATUS return.
5100 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5101 const struct dom_sid *domain_sid,
5102 const char *domain_name,
5103 struct cli_state *cli,
5104 struct rpc_pipe_client *pipe_hnd,
5105 TALLOC_CTX *mem_ctx,
5106 int argc,
5107 const char **argv)
5109 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5111 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5113 if (NT_STATUS_IS_OK(result)) {
5114 d_printf(_("\nShutdown successfully aborted\n"));
5115 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5116 } else
5117 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5119 return result;
5123 * ABORT the shutdown of a remote RPC server.
5125 * @param argc Standard main() style argc.
5126 * @param argv Standard main() style argv. Initial components are already
5127 * stripped.
5129 * @return A shell status integer (0 for success).
5132 static int rpc_shutdown_abort(struct net_context *c, int argc,
5133 const char **argv)
5135 int rc = -1;
5137 if (c->display_usage) {
5138 d_printf( "%s\n"
5139 "net rpc abortshutdown\n"
5140 " %s\n",
5141 _("Usage:"),
5142 _("Abort a scheduled shutdown"));
5143 return 0;
5146 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5147 rpc_shutdown_abort_internals, argc, argv);
5149 if (rc == 0)
5150 return rc;
5152 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5154 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5155 rpc_reg_shutdown_abort_internals,
5156 argc, argv);
5160 * Shut down a remote RPC Server via initshutdown pipe.
5162 * All parameters are provided by the run_rpc_command function, except for
5163 * argc, argv which are passed through.
5165 * @param c A net_context structure.
5166 * @param domain_sid The domain sid acquired from the remote server.
5167 * @param cli A cli_state connected to the server.
5168 * @param mem_ctx Talloc context, destroyed on completion of the function.
5169 * @param argc Standard main() style argc.
5170 * @param argv Standard main() style argv. Initial components are already
5171 * stripped.
5173 * @return Normal NTSTATUS return.
5176 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5177 const struct dom_sid *domain_sid,
5178 const char *domain_name,
5179 struct cli_state *cli,
5180 struct rpc_pipe_client *pipe_hnd,
5181 TALLOC_CTX *mem_ctx,
5182 int argc,
5183 const char **argv)
5185 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5186 const char *msg = N_("This machine will be shutdown shortly");
5187 uint32 timeout = 20;
5188 struct lsa_StringLarge msg_string;
5190 if (c->opt_comment) {
5191 msg = c->opt_comment;
5193 if (c->opt_timeout) {
5194 timeout = c->opt_timeout;
5197 msg_string.string = msg;
5199 /* create an entry */
5200 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5201 &msg_string, timeout, c->opt_force, c->opt_reboot,
5202 NULL);
5204 if (NT_STATUS_IS_OK(result)) {
5205 d_printf(_("\nShutdown of remote machine succeeded\n"));
5206 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5207 } else {
5208 DEBUG(1,("Shutdown of remote machine failed!\n"));
5210 return result;
5214 * Shut down a remote RPC Server via winreg pipe.
5216 * All parameters are provided by the run_rpc_command function, except for
5217 * argc, argv which are passed through.
5219 * @param c A net_context structure.
5220 * @param domain_sid The domain sid acquired from the remote server.
5221 * @param cli A cli_state connected to the server.
5222 * @param mem_ctx Talloc context, destroyed on completion of the function.
5223 * @param argc Standard main() style argc.
5224 * @param argv Standard main() style argv. Initial components are already
5225 * stripped.
5227 * @return Normal NTSTATUS return.
5230 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5231 const struct dom_sid *domain_sid,
5232 const char *domain_name,
5233 struct cli_state *cli,
5234 struct rpc_pipe_client *pipe_hnd,
5235 TALLOC_CTX *mem_ctx,
5236 int argc,
5237 const char **argv)
5239 const char *msg = N_("This machine will be shutdown shortly");
5240 uint32 timeout = 20;
5241 struct lsa_StringLarge msg_string;
5242 NTSTATUS result;
5243 WERROR werr;
5245 if (c->opt_comment) {
5246 msg = c->opt_comment;
5248 msg_string.string = msg;
5250 if (c->opt_timeout) {
5251 timeout = c->opt_timeout;
5254 /* create an entry */
5255 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5256 &msg_string, timeout, c->opt_force, c->opt_reboot,
5257 &werr);
5259 if (NT_STATUS_IS_OK(result)) {
5260 d_printf(_("\nShutdown of remote machine succeeded\n"));
5261 } else {
5262 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5263 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5264 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5265 else
5266 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5269 return result;
5273 * Shut down a remote RPC server.
5275 * @param argc Standard main() style argc.
5276 * @param argv Standard main() style argv. Initial components are already
5277 * stripped.
5279 * @return A shell status integer (0 for success).
5282 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5284 int rc = -1;
5286 if (c->display_usage) {
5287 d_printf( "%s\n"
5288 "net rpc shutdown\n"
5289 " %s\n",
5290 _("Usage:"),
5291 _("Shut down a remote RPC server"));
5292 return 0;
5295 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5296 rpc_init_shutdown_internals, argc, argv);
5298 if (rc) {
5299 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5300 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5301 rpc_reg_shutdown_internals, argc, argv);
5304 return rc;
5307 /***************************************************************************
5308 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5309 ***************************************************************************/
5312 * Add interdomain trust account to the RPC server.
5313 * All parameters (except for argc and argv) are passed by run_rpc_command
5314 * function.
5316 * @param c A net_context structure.
5317 * @param domain_sid The domain sid acquired from the server.
5318 * @param cli A cli_state connected to the server.
5319 * @param mem_ctx Talloc context, destroyed on completion of the function.
5320 * @param argc Standard main() style argc.
5321 * @param argv Standard main() style argv. Initial components are already
5322 * stripped.
5324 * @return normal NTSTATUS return code.
5327 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5328 const struct dom_sid *domain_sid,
5329 const char *domain_name,
5330 struct cli_state *cli,
5331 struct rpc_pipe_client *pipe_hnd,
5332 TALLOC_CTX *mem_ctx,
5333 int argc,
5334 const char **argv)
5336 struct policy_handle connect_pol, domain_pol, user_pol;
5337 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5338 char *acct_name;
5339 struct lsa_String lsa_acct_name;
5340 uint32 acb_info;
5341 uint32 acct_flags=0;
5342 uint32 user_rid;
5343 uint32_t access_granted = 0;
5344 union samr_UserInfo info;
5345 unsigned int orig_timeout;
5347 if (argc != 2) {
5348 d_printf("%s\n%s",
5349 _("Usage:"),
5350 _(" net rpc trustdom add <domain_name> "
5351 "<trust password>\n"));
5352 return NT_STATUS_INVALID_PARAMETER;
5356 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5359 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5360 return NT_STATUS_NO_MEMORY;
5363 strupper_m(acct_name);
5365 init_lsa_String(&lsa_acct_name, acct_name);
5367 /* Get samr policy handle */
5368 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5369 pipe_hnd->desthost,
5370 MAXIMUM_ALLOWED_ACCESS,
5371 &connect_pol);
5372 if (!NT_STATUS_IS_OK(result)) {
5373 goto done;
5376 /* Get domain policy handle */
5377 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5378 &connect_pol,
5379 MAXIMUM_ALLOWED_ACCESS,
5380 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5381 &domain_pol);
5382 if (!NT_STATUS_IS_OK(result)) {
5383 goto done;
5386 /* This call can take a long time - allow the server to time out.
5387 * 35 seconds should do it. */
5389 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5391 /* Create trusting domain's account */
5392 acb_info = ACB_NORMAL;
5393 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5394 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5395 SAMR_USER_ACCESS_SET_PASSWORD |
5396 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5397 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5399 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5400 &domain_pol,
5401 &lsa_acct_name,
5402 acb_info,
5403 acct_flags,
5404 &user_pol,
5405 &access_granted,
5406 &user_rid);
5408 /* And restore our original timeout. */
5409 rpccli_set_timeout(pipe_hnd, orig_timeout);
5411 if (!NT_STATUS_IS_OK(result)) {
5412 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5413 acct_name, nt_errstr(result));
5414 goto done;
5418 struct samr_CryptPassword crypt_pwd;
5420 ZERO_STRUCT(info.info23);
5422 init_samr_CryptPassword(argv[1],
5423 &cli->user_session_key,
5424 &crypt_pwd);
5426 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5427 SAMR_FIELD_NT_PASSWORD_PRESENT;
5428 info.info23.info.acct_flags = ACB_DOMTRUST;
5429 info.info23.password = crypt_pwd;
5431 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5432 &user_pol,
5434 &info);
5436 if (!NT_STATUS_IS_OK(result)) {
5437 DEBUG(0,("Could not set trust account password: %s\n",
5438 nt_errstr(result)));
5439 goto done;
5443 done:
5444 SAFE_FREE(acct_name);
5445 return result;
5449 * Create interdomain trust account for a remote domain.
5451 * @param argc Standard argc.
5452 * @param argv Standard argv without initial components.
5454 * @return Integer status (0 means success).
5457 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5459 if (argc > 0 && !c->display_usage) {
5460 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5461 rpc_trustdom_add_internals, argc, argv);
5462 } else {
5463 d_printf("%s\n%s",
5464 _("Usage:"),
5465 _("net rpc trustdom add <domain_name> <trust "
5466 "password>\n"));
5467 return -1;
5473 * Remove interdomain trust account from the RPC server.
5474 * All parameters (except for argc and argv) are passed by run_rpc_command
5475 * function.
5477 * @param c A net_context structure.
5478 * @param domain_sid The domain sid acquired from the server.
5479 * @param cli A cli_state connected to the server.
5480 * @param mem_ctx Talloc context, destroyed on completion of the function.
5481 * @param argc Standard main() style argc.
5482 * @param argv Standard main() style argv. Initial components are already
5483 * stripped.
5485 * @return normal NTSTATUS return code.
5488 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5489 const struct dom_sid *domain_sid,
5490 const char *domain_name,
5491 struct cli_state *cli,
5492 struct rpc_pipe_client *pipe_hnd,
5493 TALLOC_CTX *mem_ctx,
5494 int argc,
5495 const char **argv)
5497 struct policy_handle connect_pol, domain_pol, user_pol;
5498 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5499 char *acct_name;
5500 struct dom_sid trust_acct_sid;
5501 struct samr_Ids user_rids, name_types;
5502 struct lsa_String lsa_acct_name;
5504 if (argc != 1) {
5505 d_printf("%s\n%s",
5506 _("Usage:"),
5507 _(" net rpc trustdom del <domain_name>\n"));
5508 return NT_STATUS_INVALID_PARAMETER;
5512 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5514 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5516 if (acct_name == NULL)
5517 return NT_STATUS_NO_MEMORY;
5519 strupper_m(acct_name);
5521 /* Get samr policy handle */
5522 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5523 pipe_hnd->desthost,
5524 MAXIMUM_ALLOWED_ACCESS,
5525 &connect_pol);
5526 if (!NT_STATUS_IS_OK(result)) {
5527 goto done;
5530 /* Get domain policy handle */
5531 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5532 &connect_pol,
5533 MAXIMUM_ALLOWED_ACCESS,
5534 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5535 &domain_pol);
5536 if (!NT_STATUS_IS_OK(result)) {
5537 goto done;
5540 init_lsa_String(&lsa_acct_name, acct_name);
5542 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5543 &domain_pol,
5545 &lsa_acct_name,
5546 &user_rids,
5547 &name_types);
5549 if (!NT_STATUS_IS_OK(result)) {
5550 d_printf(_("net rpc trustdom del: LookupNames on user %s "
5551 "failed %s\n"),
5552 acct_name, nt_errstr(result) );
5553 goto done;
5556 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5557 &domain_pol,
5558 MAXIMUM_ALLOWED_ACCESS,
5559 user_rids.ids[0],
5560 &user_pol);
5562 if (!NT_STATUS_IS_OK(result)) {
5563 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
5564 "%s\n"),
5565 acct_name, nt_errstr(result) );
5566 goto done;
5569 /* append the rid to the domain sid */
5570 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
5571 goto done;
5574 /* remove the sid */
5576 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5577 &user_pol,
5578 &trust_acct_sid);
5579 if (!NT_STATUS_IS_OK(result)) {
5580 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
5581 " on user %s failed %s\n"),
5582 acct_name, nt_errstr(result) );
5583 goto done;
5586 /* Delete user */
5588 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5589 &user_pol);
5591 if (!NT_STATUS_IS_OK(result)) {
5592 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
5593 "%s\n"),
5594 acct_name, nt_errstr(result) );
5595 goto done;
5598 if (!NT_STATUS_IS_OK(result)) {
5599 d_printf(_("Could not set trust account password: %s\n"),
5600 nt_errstr(result));
5601 goto done;
5604 done:
5605 return result;
5609 * Delete interdomain trust account for a remote domain.
5611 * @param argc Standard argc.
5612 * @param argv Standard argv without initial components.
5614 * @return Integer status (0 means success).
5617 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5619 if (argc > 0 && !c->display_usage) {
5620 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5621 rpc_trustdom_del_internals, argc, argv);
5622 } else {
5623 d_printf("%s\n%s",
5624 _("Usage:"),
5625 _("net rpc trustdom del <domain>\n"));
5626 return -1;
5630 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5631 struct cli_state *cli,
5632 TALLOC_CTX *mem_ctx,
5633 const char *domain_name)
5635 char *dc_name = NULL;
5636 const char *buffer = NULL;
5637 struct rpc_pipe_client *netr;
5638 NTSTATUS status;
5640 /* Use NetServerEnum2 */
5642 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5643 SAFE_FREE(dc_name);
5644 return NT_STATUS_OK;
5647 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5648 for domain %s\n", domain_name));
5650 /* Try netr_GetDcName */
5652 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5653 &netr);
5654 if (!NT_STATUS_IS_OK(status)) {
5655 return status;
5658 status = rpccli_netr_GetDcName(netr, mem_ctx,
5659 cli->desthost,
5660 domain_name,
5661 &buffer,
5662 NULL);
5663 TALLOC_FREE(netr);
5665 if (NT_STATUS_IS_OK(status)) {
5666 return status;
5669 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5670 for domain %s\n", domain_name));
5672 return status;
5676 * Establish trust relationship to a trusting domain.
5677 * Interdomain account must already be created on remote PDC.
5679 * @param c A net_context structure.
5680 * @param argc Standard argc.
5681 * @param argv Standard argv without initial components.
5683 * @return Integer status (0 means success).
5686 static int rpc_trustdom_establish(struct net_context *c, int argc,
5687 const char **argv)
5689 struct cli_state *cli = NULL;
5690 struct sockaddr_storage server_ss;
5691 struct rpc_pipe_client *pipe_hnd = NULL;
5692 struct policy_handle connect_hnd;
5693 TALLOC_CTX *mem_ctx;
5694 NTSTATUS nt_status;
5695 struct dom_sid *domain_sid;
5697 char* domain_name;
5698 char* acct_name;
5699 fstring pdc_name;
5700 union lsa_PolicyInformation *info = NULL;
5703 * Connect to \\server\ipc$ as 'our domain' account with password
5706 if (argc != 1 || c->display_usage) {
5707 d_printf("%s\n%s",
5708 _("Usage:"),
5709 _("net rpc trustdom establish <domain_name>\n"));
5710 return -1;
5713 domain_name = smb_xstrdup(argv[0]);
5714 strupper_m(domain_name);
5716 /* account name used at first is our domain's name with '$' */
5717 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
5718 return -1;
5720 strupper_m(acct_name);
5723 * opt_workgroup will be used by connection functions further,
5724 * hence it should be set to remote domain name instead of ours
5726 if (c->opt_workgroup) {
5727 c->opt_workgroup = smb_xstrdup(domain_name);
5730 c->opt_user_name = acct_name;
5732 /* find the domain controller */
5733 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5734 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5735 return -1;
5738 /* connect to ipc$ as username/password */
5739 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5740 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5742 /* Is it trusting domain account for sure ? */
5743 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5744 nt_errstr(nt_status)));
5745 return -1;
5748 /* store who we connected to */
5750 saf_store( domain_name, pdc_name );
5753 * Connect to \\server\ipc$ again (this time anonymously)
5756 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5757 (char*)pdc_name);
5759 if (NT_STATUS_IS_ERR(nt_status)) {
5760 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5761 domain_name, nt_errstr(nt_status)));
5762 return -1;
5765 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5766 "domain %s", domain_name))) {
5767 DEBUG(0, ("talloc_init() failed\n"));
5768 cli_shutdown(cli);
5769 return -1;
5772 /* Make sure we're talking to a proper server */
5774 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5775 if (!NT_STATUS_IS_OK(nt_status)) {
5776 cli_shutdown(cli);
5777 talloc_destroy(mem_ctx);
5778 return -1;
5782 * Call LsaOpenPolicy and LsaQueryInfo
5785 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5786 &pipe_hnd);
5787 if (!NT_STATUS_IS_OK(nt_status)) {
5788 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5789 cli_shutdown(cli);
5790 talloc_destroy(mem_ctx);
5791 return -1;
5794 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
5795 &connect_hnd);
5796 if (NT_STATUS_IS_ERR(nt_status)) {
5797 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5798 nt_errstr(nt_status)));
5799 cli_shutdown(cli);
5800 talloc_destroy(mem_ctx);
5801 return -1;
5804 /* Querying info level 5 */
5806 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5807 &connect_hnd,
5808 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5809 &info);
5810 if (NT_STATUS_IS_ERR(nt_status)) {
5811 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5812 nt_errstr(nt_status)));
5813 cli_shutdown(cli);
5814 talloc_destroy(mem_ctx);
5815 return -1;
5818 domain_sid = info->account_domain.sid;
5820 /* There should be actually query info level 3 (following nt serv behaviour),
5821 but I still don't know if it's _really_ necessary */
5824 * Store the password in secrets db
5827 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5828 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5829 cli_shutdown(cli);
5830 talloc_destroy(mem_ctx);
5831 return -1;
5835 * Close the pipes and clean up
5838 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5839 if (NT_STATUS_IS_ERR(nt_status)) {
5840 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5841 nt_errstr(nt_status)));
5842 cli_shutdown(cli);
5843 talloc_destroy(mem_ctx);
5844 return -1;
5847 cli_shutdown(cli);
5849 talloc_destroy(mem_ctx);
5851 d_printf(_("Trust to domain %s established\n"), domain_name);
5852 return 0;
5856 * Revoke trust relationship to the remote domain.
5858 * @param c A net_context structure.
5859 * @param argc Standard argc.
5860 * @param argv Standard argv without initial components.
5862 * @return Integer status (0 means success).
5865 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5866 const char **argv)
5868 char* domain_name;
5869 int rc = -1;
5871 if (argc < 1 || c->display_usage) {
5872 d_printf("%s\n%s",
5873 _("Usage:"),
5874 _("net rpc trustdom revoke <domain_name>\n"
5875 " Revoke trust relationship\n"
5876 " domain_name\tName of domain to revoke trust\n"));
5877 return -1;
5880 /* generate upper cased domain name */
5881 domain_name = smb_xstrdup(argv[0]);
5882 strupper_m(domain_name);
5884 /* delete password of the trust */
5885 if (!pdb_del_trusteddom_pw(domain_name)) {
5886 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5887 domain_name));
5888 goto done;
5891 rc = 0;
5892 done:
5893 SAFE_FREE(domain_name);
5894 return rc;
5897 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5898 const struct dom_sid *domain_sid,
5899 const char *domain_name,
5900 struct cli_state *cli,
5901 struct rpc_pipe_client *pipe_hnd,
5902 TALLOC_CTX *mem_ctx,
5903 int argc,
5904 const char **argv)
5906 fstring str_sid;
5907 if (!sid_to_fstring(str_sid, domain_sid)) {
5908 return NT_STATUS_UNSUCCESSFUL;
5910 d_printf("%s\n", str_sid);
5911 return NT_STATUS_OK;
5914 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
5916 fstring ascii_sid;
5918 /* convert sid into ascii string */
5919 sid_to_fstring(ascii_sid, dom_sid);
5921 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
5924 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5925 TALLOC_CTX *mem_ctx,
5926 struct policy_handle *pol,
5927 struct dom_sid dom_sid,
5928 const char *trusted_dom_name)
5930 NTSTATUS nt_status;
5931 union lsa_TrustedDomainInfo *info = NULL;
5932 char *cleartextpwd = NULL;
5933 uint8_t session_key[16];
5934 DATA_BLOB session_key_blob;
5935 DATA_BLOB data = data_blob_null;
5937 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5938 pol,
5939 &dom_sid,
5940 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5941 &info);
5942 if (NT_STATUS_IS_ERR(nt_status)) {
5943 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5944 nt_errstr(nt_status)));
5945 goto done;
5948 data = data_blob(info->password.password->data,
5949 info->password.password->length);
5951 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
5952 DEBUG(0, ("Could not retrieve password hash\n"));
5953 goto done;
5956 session_key_blob = data_blob_const(session_key, sizeof(session_key));
5957 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
5959 if (cleartextpwd == NULL) {
5960 DEBUG(0,("retrieved NULL password\n"));
5961 nt_status = NT_STATUS_UNSUCCESSFUL;
5962 goto done;
5965 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5966 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5967 nt_status = NT_STATUS_UNSUCCESSFUL;
5968 goto done;
5971 #ifdef DEBUG_PASSWORD
5972 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5973 "password: [%s]\n", trusted_dom_name,
5974 sid_string_dbg(&dom_sid), cleartextpwd));
5975 #endif
5977 done:
5978 SAFE_FREE(cleartextpwd);
5979 data_blob_free(&data);
5981 return nt_status;
5984 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5985 const char **argv)
5987 /* common variables */
5988 TALLOC_CTX* mem_ctx;
5989 struct cli_state *cli = NULL;
5990 struct rpc_pipe_client *pipe_hnd = NULL;
5991 NTSTATUS nt_status;
5992 const char *domain_name = NULL;
5993 struct dom_sid *queried_dom_sid;
5994 struct policy_handle connect_hnd;
5995 union lsa_PolicyInformation *info = NULL;
5997 /* trusted domains listing variables */
5998 unsigned int enum_ctx = 0;
5999 int i;
6000 struct lsa_DomainList dom_list;
6001 fstring pdc_name;
6003 if (c->display_usage) {
6004 d_printf( "%s\n"
6005 "net rpc trustdom vampire\n"
6006 " %s\n",
6007 _("Usage:"),
6008 _("Vampire trust relationship from remote server"));
6009 return 0;
6013 * Listing trusted domains (stored in secrets.tdb, if local)
6016 mem_ctx = talloc_init("trust relationships vampire");
6019 * set domain and pdc name to local samba server (default)
6020 * or to remote one given in command line
6023 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6024 domain_name = c->opt_workgroup;
6025 c->opt_target_workgroup = c->opt_workgroup;
6026 } else {
6027 fstrcpy(pdc_name, global_myname());
6028 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6029 c->opt_target_workgroup = domain_name;
6032 /* open \PIPE\lsarpc and open policy handle */
6033 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6034 if (!NT_STATUS_IS_OK(nt_status)) {
6035 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6036 nt_errstr(nt_status)));
6037 talloc_destroy(mem_ctx);
6038 return -1;
6041 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6042 &pipe_hnd);
6043 if (!NT_STATUS_IS_OK(nt_status)) {
6044 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6045 nt_errstr(nt_status) ));
6046 cli_shutdown(cli);
6047 talloc_destroy(mem_ctx);
6048 return -1;
6051 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6052 &connect_hnd);
6053 if (NT_STATUS_IS_ERR(nt_status)) {
6054 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6055 nt_errstr(nt_status)));
6056 cli_shutdown(cli);
6057 talloc_destroy(mem_ctx);
6058 return -1;
6061 /* query info level 5 to obtain sid of a domain being queried */
6062 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6063 &connect_hnd,
6064 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6065 &info);
6067 if (NT_STATUS_IS_ERR(nt_status)) {
6068 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6069 nt_errstr(nt_status)));
6070 cli_shutdown(cli);
6071 talloc_destroy(mem_ctx);
6072 return -1;
6075 queried_dom_sid = info->account_domain.sid;
6078 * Keep calling LsaEnumTrustdom over opened pipe until
6079 * the end of enumeration is reached
6082 d_printf(_("Vampire trusted domains:\n\n"));
6084 do {
6085 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6086 &connect_hnd,
6087 &enum_ctx,
6088 &dom_list,
6089 (uint32_t)-1);
6090 if (NT_STATUS_IS_ERR(nt_status)) {
6091 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6092 nt_errstr(nt_status)));
6093 cli_shutdown(cli);
6094 talloc_destroy(mem_ctx);
6095 return -1;
6098 for (i = 0; i < dom_list.count; i++) {
6100 print_trusted_domain(dom_list.domains[i].sid,
6101 dom_list.domains[i].name.string);
6103 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6104 *dom_list.domains[i].sid,
6105 dom_list.domains[i].name.string);
6106 if (!NT_STATUS_IS_OK(nt_status)) {
6107 cli_shutdown(cli);
6108 talloc_destroy(mem_ctx);
6109 return -1;
6114 * in case of no trusted domains say something rather
6115 * than just display blank line
6117 if (!dom_list.count) d_printf(_("none\n"));
6119 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6121 /* close this connection before doing next one */
6122 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6123 if (NT_STATUS_IS_ERR(nt_status)) {
6124 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6125 nt_errstr(nt_status)));
6126 cli_shutdown(cli);
6127 talloc_destroy(mem_ctx);
6128 return -1;
6131 /* close lsarpc pipe and connection to IPC$ */
6132 cli_shutdown(cli);
6134 talloc_destroy(mem_ctx);
6135 return 0;
6138 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6140 /* common variables */
6141 TALLOC_CTX* mem_ctx;
6142 struct cli_state *cli = NULL, *remote_cli = NULL;
6143 struct rpc_pipe_client *pipe_hnd = NULL;
6144 NTSTATUS nt_status;
6145 const char *domain_name = NULL;
6146 struct dom_sid *queried_dom_sid;
6147 int ascii_dom_name_len;
6148 struct policy_handle connect_hnd;
6149 union lsa_PolicyInformation *info = NULL;
6151 /* trusted domains listing variables */
6152 unsigned int num_domains, enum_ctx = 0;
6153 int i;
6154 struct lsa_DomainList dom_list;
6155 fstring pdc_name;
6156 bool found_domain;
6158 /* trusting domains listing variables */
6159 struct policy_handle domain_hnd;
6160 struct samr_SamArray *trusts = NULL;
6162 if (c->display_usage) {
6163 d_printf( "%s\n"
6164 "net rpc trustdom list\n"
6165 " %s\n",
6166 _("Usage:"),
6167 _("List incoming and outgoing trust relationships"));
6168 return 0;
6172 * Listing trusted domains (stored in secrets.tdb, if local)
6175 mem_ctx = talloc_init("trust relationships listing");
6178 * set domain and pdc name to local samba server (default)
6179 * or to remote one given in command line
6182 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6183 domain_name = c->opt_workgroup;
6184 c->opt_target_workgroup = c->opt_workgroup;
6185 } else {
6186 fstrcpy(pdc_name, global_myname());
6187 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6188 c->opt_target_workgroup = domain_name;
6191 /* open \PIPE\lsarpc and open policy handle */
6192 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6193 if (!NT_STATUS_IS_OK(nt_status)) {
6194 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6195 nt_errstr(nt_status)));
6196 talloc_destroy(mem_ctx);
6197 return -1;
6200 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6201 &pipe_hnd);
6202 if (!NT_STATUS_IS_OK(nt_status)) {
6203 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6204 nt_errstr(nt_status) ));
6205 cli_shutdown(cli);
6206 talloc_destroy(mem_ctx);
6207 return -1;
6210 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6211 &connect_hnd);
6212 if (NT_STATUS_IS_ERR(nt_status)) {
6213 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6214 nt_errstr(nt_status)));
6215 cli_shutdown(cli);
6216 talloc_destroy(mem_ctx);
6217 return -1;
6220 /* query info level 5 to obtain sid of a domain being queried */
6221 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6222 &connect_hnd,
6223 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6224 &info);
6226 if (NT_STATUS_IS_ERR(nt_status)) {
6227 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6228 nt_errstr(nt_status)));
6229 cli_shutdown(cli);
6230 talloc_destroy(mem_ctx);
6231 return -1;
6234 queried_dom_sid = info->account_domain.sid;
6237 * Keep calling LsaEnumTrustdom over opened pipe until
6238 * the end of enumeration is reached
6241 d_printf(_("Trusted domains list:\n\n"));
6243 found_domain = false;
6245 do {
6246 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6247 &connect_hnd,
6248 &enum_ctx,
6249 &dom_list,
6250 (uint32_t)-1);
6251 if (NT_STATUS_IS_ERR(nt_status)) {
6252 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6253 nt_errstr(nt_status)));
6254 cli_shutdown(cli);
6255 talloc_destroy(mem_ctx);
6256 return -1;
6259 for (i = 0; i < dom_list.count; i++) {
6260 print_trusted_domain(dom_list.domains[i].sid,
6261 dom_list.domains[i].name.string);
6262 found_domain = true;
6266 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6269 * in case of no trusted domains say something rather
6270 * than just display blank line
6272 if (!found_domain) {
6273 d_printf(_("none\n"));
6276 /* close this connection before doing next one */
6277 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6278 if (NT_STATUS_IS_ERR(nt_status)) {
6279 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6280 nt_errstr(nt_status)));
6281 cli_shutdown(cli);
6282 talloc_destroy(mem_ctx);
6283 return -1;
6286 TALLOC_FREE(pipe_hnd);
6289 * Listing trusting domains (stored in passdb backend, if local)
6292 d_printf(_("\nTrusting domains list:\n\n"));
6295 * Open \PIPE\samr and get needed policy handles
6297 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6298 &pipe_hnd);
6299 if (!NT_STATUS_IS_OK(nt_status)) {
6300 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6301 cli_shutdown(cli);
6302 talloc_destroy(mem_ctx);
6303 return -1;
6306 /* SamrConnect2 */
6307 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6308 pipe_hnd->desthost,
6309 SAMR_ACCESS_LOOKUP_DOMAIN,
6310 &connect_hnd);
6311 if (!NT_STATUS_IS_OK(nt_status)) {
6312 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6313 nt_errstr(nt_status)));
6314 cli_shutdown(cli);
6315 talloc_destroy(mem_ctx);
6316 return -1;
6319 /* SamrOpenDomain - we have to open domain policy handle in order to be
6320 able to enumerate accounts*/
6321 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6322 &connect_hnd,
6323 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6324 queried_dom_sid,
6325 &domain_hnd);
6326 if (!NT_STATUS_IS_OK(nt_status)) {
6327 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6328 nt_errstr(nt_status)));
6329 cli_shutdown(cli);
6330 talloc_destroy(mem_ctx);
6331 return -1;
6335 * perform actual enumeration
6338 found_domain = false;
6340 enum_ctx = 0; /* reset enumeration context from last enumeration */
6341 do {
6343 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6344 &domain_hnd,
6345 &enum_ctx,
6346 ACB_DOMTRUST,
6347 &trusts,
6348 0xffff,
6349 &num_domains);
6350 if (NT_STATUS_IS_ERR(nt_status)) {
6351 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6352 nt_errstr(nt_status)));
6353 cli_shutdown(cli);
6354 talloc_destroy(mem_ctx);
6355 return -1;
6358 for (i = 0; i < num_domains; i++) {
6360 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6362 found_domain = true;
6365 * get each single domain's sid (do we _really_ need this ?):
6366 * 1) connect to domain's pdc
6367 * 2) query the pdc for domain's sid
6370 /* get rid of '$' tail */
6371 ascii_dom_name_len = strlen(str);
6372 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6373 str[ascii_dom_name_len - 1] = '\0';
6375 /* set opt_* variables to remote domain */
6376 strupper_m(str);
6377 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6378 c->opt_target_workgroup = c->opt_workgroup;
6380 d_printf("%-20s", str);
6382 /* connect to remote domain controller */
6383 nt_status = net_make_ipc_connection(c,
6384 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6385 &remote_cli);
6386 if (NT_STATUS_IS_OK(nt_status)) {
6387 /* query for domain's sid */
6388 if (run_rpc_command(
6389 c, remote_cli,
6390 &ndr_table_lsarpc.syntax_id, 0,
6391 rpc_query_domain_sid, argc,
6392 argv))
6393 d_printf(_("strange - couldn't get domain's sid\n"));
6395 cli_shutdown(remote_cli);
6397 } else {
6398 d_fprintf(stderr, _("domain controller is not "
6399 "responding: %s\n"),
6400 nt_errstr(nt_status));
6401 d_printf(_("couldn't get domain's sid\n"));
6405 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6407 if (!found_domain) {
6408 d_printf("none\n");
6411 /* close opened samr and domain policy handles */
6412 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6413 if (!NT_STATUS_IS_OK(nt_status)) {
6414 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6417 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6418 if (!NT_STATUS_IS_OK(nt_status)) {
6419 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6422 /* close samr pipe and connection to IPC$ */
6423 cli_shutdown(cli);
6425 talloc_destroy(mem_ctx);
6426 return 0;
6430 * Entrypoint for 'net rpc trustdom' code.
6432 * @param argc Standard argc.
6433 * @param argv Standard argv without initial components.
6435 * @return Integer status (0 means success).
6438 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6440 struct functable func[] = {
6442 "add",
6443 rpc_trustdom_add,
6444 NET_TRANSPORT_RPC,
6445 N_("Add trusting domain's account"),
6446 N_("net rpc trustdom add\n"
6447 " Add trusting domain's account")
6450 "del",
6451 rpc_trustdom_del,
6452 NET_TRANSPORT_RPC,
6453 N_("Remove trusting domain's account"),
6454 N_("net rpc trustdom del\n"
6455 " Remove trusting domain's account")
6458 "establish",
6459 rpc_trustdom_establish,
6460 NET_TRANSPORT_RPC,
6461 N_("Establish outgoing trust relationship"),
6462 N_("net rpc trustdom establish\n"
6463 " Establish outgoing trust relationship")
6466 "revoke",
6467 rpc_trustdom_revoke,
6468 NET_TRANSPORT_RPC,
6469 N_("Revoke outgoing trust relationship"),
6470 N_("net rpc trustdom revoke\n"
6471 " Revoke outgoing trust relationship")
6474 "list",
6475 rpc_trustdom_list,
6476 NET_TRANSPORT_RPC,
6477 N_("List in- and outgoing domain trusts"),
6478 N_("net rpc trustdom list\n"
6479 " List in- and outgoing domain trusts")
6482 "vampire",
6483 rpc_trustdom_vampire,
6484 NET_TRANSPORT_RPC,
6485 N_("Vampire trusts from remote server"),
6486 N_("net rpc trustdom vampire\n"
6487 " Vampire trusts from remote server")
6489 {NULL, NULL, 0, NULL, NULL}
6492 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6496 * Check if a server will take rpc commands
6497 * @param flags Type of server to connect to (PDC, DMB, localhost)
6498 * if the host is not explicitly specified
6499 * @return bool (true means rpc supported)
6501 bool net_rpc_check(struct net_context *c, unsigned flags)
6503 struct cli_state *cli;
6504 bool ret = false;
6505 struct sockaddr_storage server_ss;
6506 char *server_name = NULL;
6507 NTSTATUS status;
6509 /* flags (i.e. server type) may depend on command */
6510 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6511 return false;
6513 if ((cli = cli_initialise()) == NULL) {
6514 return false;
6517 status = cli_connect(cli, server_name, &server_ss);
6518 if (!NT_STATUS_IS_OK(status))
6519 goto done;
6520 if (!attempt_netbios_session_request(&cli, global_myname(),
6521 server_name, &server_ss))
6522 goto done;
6523 status = cli_negprot(cli);
6524 if (!NT_STATUS_IS_OK(status))
6525 goto done;
6526 if (cli->protocol < PROTOCOL_NT1)
6527 goto done;
6529 ret = true;
6530 done:
6531 cli_shutdown(cli);
6532 return ret;
6535 /* dump sam database via samsync rpc calls */
6536 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6537 if (c->display_usage) {
6538 d_printf( "%s\n"
6539 "net rpc samdump\n"
6540 " %s\n",
6541 _("Usage:"),
6542 _("Dump remote SAM database"));
6543 return 0;
6546 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6547 NET_FLAGS_ANONYMOUS,
6548 rpc_samdump_internals, argc, argv);
6551 /* syncronise sam database via samsync rpc calls */
6552 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6554 struct functable func[] = {
6556 "ldif",
6557 rpc_vampire_ldif,
6558 NET_TRANSPORT_RPC,
6559 N_("Dump remote SAM database to ldif"),
6560 N_("net rpc vampire ldif\n"
6561 " Dump remote SAM database to LDIF file or "
6562 "stdout")
6565 "keytab",
6566 rpc_vampire_keytab,
6567 NET_TRANSPORT_RPC,
6568 N_("Dump remote SAM database to Kerberos Keytab"),
6569 N_("net rpc vampire keytab\n"
6570 " Dump remote SAM database to Kerberos keytab "
6571 "file")
6574 "passdb",
6575 rpc_vampire_passdb,
6576 NET_TRANSPORT_RPC,
6577 N_("Dump remote SAM database to passdb"),
6578 N_("net rpc vampire passdb\n"
6579 " Dump remote SAM database to passdb")
6582 {NULL, NULL, 0, NULL, NULL}
6585 if (argc == 0) {
6586 if (c->display_usage) {
6587 d_printf( "%s\n"
6588 "net rpc vampire\n"
6589 " %s\n",
6590 _("Usage:"),
6591 _("Vampire remote SAM database"));
6592 return 0;
6595 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6596 NET_FLAGS_ANONYMOUS,
6597 rpc_vampire_internals,
6598 argc, argv);
6601 return net_run_function(c, argc, argv, "net rpc vampire", func);
6605 * Migrate everything from a print server.
6607 * @param c A net_context structure.
6608 * @param argc Standard main() style argc.
6609 * @param argv Standard main() style argv. Initial components are already
6610 * stripped.
6612 * @return A shell status integer (0 for success).
6614 * The order is important !
6615 * To successfully add drivers the print queues have to exist !
6616 * Applying ACLs should be the last step, because you're easily locked out.
6619 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6620 const char **argv)
6622 int ret;
6624 if (c->display_usage) {
6625 d_printf( "%s\n"
6626 "net rpc printer migrate all\n"
6627 " %s\n",
6628 _("Usage:"),
6629 _("Migrate everything from a print server"));
6630 return 0;
6633 if (!c->opt_host) {
6634 d_printf(_("no server to migrate\n"));
6635 return -1;
6638 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6639 rpc_printer_migrate_printers_internals, argc,
6640 argv);
6641 if (ret)
6642 return ret;
6644 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6645 rpc_printer_migrate_drivers_internals, argc,
6646 argv);
6647 if (ret)
6648 return ret;
6650 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6651 rpc_printer_migrate_forms_internals, argc, argv);
6652 if (ret)
6653 return ret;
6655 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6656 rpc_printer_migrate_settings_internals, argc,
6657 argv);
6658 if (ret)
6659 return ret;
6661 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6662 rpc_printer_migrate_security_internals, argc,
6663 argv);
6668 * Migrate print drivers from a print server.
6670 * @param c A net_context structure.
6671 * @param argc Standard main() style argc.
6672 * @param argv Standard main() style argv. Initial components are already
6673 * stripped.
6675 * @return A shell status integer (0 for success).
6677 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6678 const char **argv)
6680 if (c->display_usage) {
6681 d_printf( "%s\n"
6682 "net rpc printer migrate drivers\n"
6683 " %s\n",
6684 _("Usage:"),
6685 _("Migrate print-drivers from a print-server"));
6686 return 0;
6689 if (!c->opt_host) {
6690 d_printf(_("no server to migrate\n"));
6691 return -1;
6694 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6695 rpc_printer_migrate_drivers_internals,
6696 argc, argv);
6700 * Migrate print-forms from a print-server.
6702 * @param c A net_context structure.
6703 * @param argc Standard main() style argc.
6704 * @param argv Standard main() style argv. Initial components are already
6705 * stripped.
6707 * @return A shell status integer (0 for success).
6709 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6710 const char **argv)
6712 if (c->display_usage) {
6713 d_printf( "%s\n"
6714 "net rpc printer migrate forms\n"
6715 " %s\n",
6716 _("Usage:"),
6717 _("Migrate print-forms from a print-server"));
6718 return 0;
6721 if (!c->opt_host) {
6722 d_printf(_("no server to migrate\n"));
6723 return -1;
6726 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6727 rpc_printer_migrate_forms_internals,
6728 argc, argv);
6732 * Migrate printers from a print-server.
6734 * @param c A net_context structure.
6735 * @param argc Standard main() style argc.
6736 * @param argv Standard main() style argv. Initial components are already
6737 * stripped.
6739 * @return A shell status integer (0 for success).
6741 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6742 const char **argv)
6744 if (c->display_usage) {
6745 d_printf( "%s\n"
6746 "net rpc printer migrate printers\n"
6747 " %s\n",
6748 _("Usage:"),
6749 _("Migrate printers from a print-server"));
6750 return 0;
6753 if (!c->opt_host) {
6754 d_printf(_("no server to migrate\n"));
6755 return -1;
6758 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6759 rpc_printer_migrate_printers_internals,
6760 argc, argv);
6764 * Migrate printer-ACLs from a print-server
6766 * @param c A net_context structure.
6767 * @param argc Standard main() style argc.
6768 * @param argv Standard main() style argv. Initial components are already
6769 * stripped.
6771 * @return A shell status integer (0 for success).
6773 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6774 const char **argv)
6776 if (c->display_usage) {
6777 d_printf( "%s\n"
6778 "net rpc printer migrate security\n"
6779 " %s\n",
6780 _("Usage:"),
6781 _("Migrate printer-ACLs from a print-server"));
6782 return 0;
6785 if (!c->opt_host) {
6786 d_printf(_("no server to migrate\n"));
6787 return -1;
6790 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6791 rpc_printer_migrate_security_internals,
6792 argc, argv);
6796 * Migrate printer-settings from a print-server.
6798 * @param c A net_context structure.
6799 * @param argc Standard main() style argc.
6800 * @param argv Standard main() style argv. Initial components are already
6801 * stripped.
6803 * @return A shell status integer (0 for success).
6805 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6806 const char **argv)
6808 if (c->display_usage) {
6809 d_printf( "%s\n"
6810 "net rpc printer migrate settings\n"
6811 " %s\n",
6812 _("Usage:"),
6813 _("Migrate printer-settings from a "
6814 "print-server"));
6815 return 0;
6818 if (!c->opt_host) {
6819 d_printf(_("no server to migrate\n"));
6820 return -1;
6823 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6824 rpc_printer_migrate_settings_internals,
6825 argc, argv);
6829 * 'net rpc printer' entrypoint.
6831 * @param c A net_context structure.
6832 * @param argc Standard main() style argc.
6833 * @param argv Standard main() style argv. Initial components are already
6834 * stripped.
6837 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6840 /* ouch: when addriver and setdriver are called from within
6841 rpc_printer_migrate_drivers_internals, the printer-queue already
6842 *has* to exist */
6844 struct functable func[] = {
6846 "all",
6847 rpc_printer_migrate_all,
6848 NET_TRANSPORT_RPC,
6849 N_("Migrate all from remote to local print server"),
6850 N_("net rpc printer migrate all\n"
6851 " Migrate all from remote to local print server")
6854 "drivers",
6855 rpc_printer_migrate_drivers,
6856 NET_TRANSPORT_RPC,
6857 N_("Migrate drivers to local server"),
6858 N_("net rpc printer migrate drivers\n"
6859 " Migrate drivers to local server")
6862 "forms",
6863 rpc_printer_migrate_forms,
6864 NET_TRANSPORT_RPC,
6865 N_("Migrate froms to local server"),
6866 N_("net rpc printer migrate forms\n"
6867 " Migrate froms to local server")
6870 "printers",
6871 rpc_printer_migrate_printers,
6872 NET_TRANSPORT_RPC,
6873 N_("Migrate printers to local server"),
6874 N_("net rpc printer migrate printers\n"
6875 " Migrate printers to local server")
6878 "security",
6879 rpc_printer_migrate_security,
6880 NET_TRANSPORT_RPC,
6881 N_("Mirgate printer ACLs to local server"),
6882 N_("net rpc printer migrate security\n"
6883 " Mirgate printer ACLs to local server")
6886 "settings",
6887 rpc_printer_migrate_settings,
6888 NET_TRANSPORT_RPC,
6889 N_("Migrate printer settings to local server"),
6890 N_("net rpc printer migrate settings\n"
6891 " Migrate printer settings to local server")
6893 {NULL, NULL, 0, NULL, NULL}
6896 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6901 * List printers on a remote RPC server.
6903 * @param c A net_context structure.
6904 * @param argc Standard main() style argc.
6905 * @param argv Standard main() style argv. Initial components are already
6906 * stripped.
6908 * @return A shell status integer (0 for success).
6910 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6912 if (c->display_usage) {
6913 d_printf( "%s\n"
6914 "net rpc printer list\n"
6915 " %s\n",
6916 _("Usage:"),
6917 _("List printers on a remote RPC server"));
6918 return 0;
6921 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6922 rpc_printer_list_internals,
6923 argc, argv);
6927 * List printer-drivers on a remote RPC server.
6929 * @param c A net_context structure.
6930 * @param argc Standard main() style argc.
6931 * @param argv Standard main() style argv. Initial components are already
6932 * stripped.
6934 * @return A shell status integer (0 for success).
6936 static int rpc_printer_driver_list(struct net_context *c, int argc,
6937 const char **argv)
6939 if (c->display_usage) {
6940 d_printf( "%s\n"
6941 "net rpc printer driver\n"
6942 " %s\n",
6943 _("Usage:"),
6944 _("List printer-drivers on a remote RPC server"));
6945 return 0;
6948 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6949 rpc_printer_driver_list_internals,
6950 argc, argv);
6954 * Publish printer in ADS via MSRPC.
6956 * @param c A net_context structure.
6957 * @param argc Standard main() style argc.
6958 * @param argv Standard main() style argv. Initial components are already
6959 * stripped.
6961 * @return A shell status integer (0 for success).
6963 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6964 const char **argv)
6966 if (c->display_usage) {
6967 d_printf( "%s\n"
6968 "net rpc printer publish publish\n"
6969 " %s\n",
6970 _("Usage:"),
6971 _("Publish printer in ADS via MSRPC"));
6972 return 0;
6975 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6976 rpc_printer_publish_publish_internals,
6977 argc, argv);
6981 * Update printer in ADS via MSRPC.
6983 * @param c A net_context structure.
6984 * @param argc Standard main() style argc.
6985 * @param argv Standard main() style argv. Initial components are already
6986 * stripped.
6988 * @return A shell status integer (0 for success).
6990 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6992 if (c->display_usage) {
6993 d_printf( "%s\n"
6994 "net rpc printer publish update\n"
6995 " %s\n",
6996 _("Usage:"),
6997 _("Update printer in ADS via MSRPC"));
6998 return 0;
7001 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7002 rpc_printer_publish_update_internals,
7003 argc, argv);
7007 * UnPublish printer in ADS via MSRPC.
7009 * @param c A net_context structure.
7010 * @param argc Standard main() style argc.
7011 * @param argv Standard main() style argv. Initial components are already
7012 * stripped.
7014 * @return A shell status integer (0 for success).
7016 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7017 const char **argv)
7019 if (c->display_usage) {
7020 d_printf( "%s\n"
7021 "net rpc printer publish unpublish\n"
7022 " %s\n",
7023 _("Usage:\n"),
7024 _("UnPublish printer in ADS via MSRPC"));
7025 return 0;
7028 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7029 rpc_printer_publish_unpublish_internals,
7030 argc, argv);
7034 * List published printers via MSRPC.
7036 * @param c A net_context structure.
7037 * @param argc Standard main() style argc.
7038 * @param argv Standard main() style argv. Initial components are already
7039 * stripped.
7041 * @return A shell status integer (0 for success).
7043 static int rpc_printer_publish_list(struct net_context *c, int argc,
7044 const char **argv)
7046 if (c->display_usage) {
7047 d_printf( "%s\n"
7048 "net rpc printer publish list\n"
7049 " %s\n",
7050 _("Usage:"),
7051 _("List published printers via MSRPC"));
7052 return 0;
7055 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7056 rpc_printer_publish_list_internals,
7057 argc, argv);
7062 * Publish printer in ADS.
7064 * @param c A net_context structure.
7065 * @param argc Standard main() style argc.
7066 * @param argv Standard main() style argv. Initial components are already
7067 * stripped.
7069 * @return A shell status integer (0 for success).
7071 static int rpc_printer_publish(struct net_context *c, int argc,
7072 const char **argv)
7075 struct functable func[] = {
7077 "publish",
7078 rpc_printer_publish_publish,
7079 NET_TRANSPORT_RPC,
7080 N_("Publish printer in AD"),
7081 N_("net rpc printer publish publish\n"
7082 " Publish printer in AD")
7085 "update",
7086 rpc_printer_publish_update,
7087 NET_TRANSPORT_RPC,
7088 N_("Update printer in AD"),
7089 N_("net rpc printer publish update\n"
7090 " Update printer in AD")
7093 "unpublish",
7094 rpc_printer_publish_unpublish,
7095 NET_TRANSPORT_RPC,
7096 N_("Unpublish printer"),
7097 N_("net rpc printer publish unpublish\n"
7098 " Unpublish printer")
7101 "list",
7102 rpc_printer_publish_list,
7103 NET_TRANSPORT_RPC,
7104 N_("List published printers"),
7105 N_("net rpc printer publish list\n"
7106 " List published printers")
7108 {NULL, NULL, 0, NULL, NULL}
7111 if (argc == 0) {
7112 if (c->display_usage) {
7113 d_printf(_("Usage:\n"));
7114 d_printf(_("net rpc printer publish\n"
7115 " List published printers\n"
7116 " Alias of net rpc printer publish "
7117 "list\n"));
7118 net_display_usage_from_functable(func);
7119 return 0;
7121 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7122 rpc_printer_publish_list_internals,
7123 argc, argv);
7126 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7132 * Display rpc printer help page.
7134 * @param c A net_context structure.
7135 * @param argc Standard main() style argc.
7136 * @param argv Standard main() style argv. Initial components are already
7137 * stripped.
7139 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7141 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7142 "\tlists all printers on print-server\n\n"));
7143 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7144 "\tlists all printer-drivers on print-server\n\n"));
7145 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7146 "\tpublishes printer settings in Active Directory\n"
7147 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7148 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7149 "\n\tmigrates printers from remote to local server\n\n"));
7150 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7151 "\n\tmigrates printer-settings from remote to local server\n\n"));
7152 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7153 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7154 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7155 "\n\tmigrates printer-forms from remote to local server\n\n"));
7156 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7157 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7158 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7159 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7160 "\tremote to local print-server\n\n"));
7161 net_common_methods_usage(c, argc, argv);
7162 net_common_flags_usage(c, argc, argv);
7163 d_printf(_(
7164 "\t-v or --verbose\t\t\tgive verbose output\n"
7165 "\t --destination\t\tmigration target server (default: localhost)\n"));
7167 return -1;
7171 * 'net rpc printer' entrypoint.
7173 * @param c A net_context structure.
7174 * @param argc Standard main() style argc.
7175 * @param argv Standard main() style argv. Initial components are already
7176 * stripped.
7178 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7180 struct functable func[] = {
7182 "list",
7183 rpc_printer_list,
7184 NET_TRANSPORT_RPC,
7185 N_("List all printers on print server"),
7186 N_("net rpc printer list\n"
7187 " List all printers on print server")
7190 "migrate",
7191 rpc_printer_migrate,
7192 NET_TRANSPORT_RPC,
7193 N_("Migrate printer to local server"),
7194 N_("net rpc printer migrate\n"
7195 " Migrate printer to local server")
7198 "driver",
7199 rpc_printer_driver_list,
7200 NET_TRANSPORT_RPC,
7201 N_("List printer drivers"),
7202 N_("net rpc printer driver\n"
7203 " List printer drivers")
7206 "publish",
7207 rpc_printer_publish,
7208 NET_TRANSPORT_RPC,
7209 N_("Publish printer in AD"),
7210 N_("net rpc printer publish\n"
7211 " Publish printer in AD")
7213 {NULL, NULL, 0, NULL, NULL}
7216 if (argc == 0) {
7217 if (c->display_usage) {
7218 d_printf(_("Usage:\n"));
7219 d_printf(_("net rpc printer\n"
7220 " List printers\n"));
7221 net_display_usage_from_functable(func);
7222 return 0;
7224 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7225 rpc_printer_list_internals,
7226 argc, argv);
7229 return net_run_function(c, argc, argv, "net rpc printer", func);
7233 * 'net rpc' entrypoint.
7235 * @param c A net_context structure.
7236 * @param argc Standard main() style argc.
7237 * @param argv Standard main() style argv. Initial components are already
7238 * stripped.
7241 int net_rpc(struct net_context *c, int argc, const char **argv)
7243 NET_API_STATUS status;
7245 struct functable func[] = {
7247 "audit",
7248 net_rpc_audit,
7249 NET_TRANSPORT_RPC,
7250 N_("Modify global audit settings"),
7251 N_("net rpc audit\n"
7252 " Modify global audit settings")
7255 "info",
7256 net_rpc_info,
7257 NET_TRANSPORT_RPC,
7258 N_("Show basic info about a domain"),
7259 N_("net rpc info\n"
7260 " Show basic info about a domain")
7263 "join",
7264 net_rpc_join,
7265 NET_TRANSPORT_RPC,
7266 N_("Join a domain"),
7267 N_("net rpc join\n"
7268 " Join a domain")
7271 "oldjoin",
7272 net_rpc_oldjoin,
7273 NET_TRANSPORT_RPC,
7274 N_("Join a domain created in server manager"),
7275 N_("net rpc oldjoin\n"
7276 " Join a domain created in server manager")
7279 "testjoin",
7280 net_rpc_testjoin,
7281 NET_TRANSPORT_RPC,
7282 N_("Test that a join is valid"),
7283 N_("net rpc testjoin\n"
7284 " Test that a join is valid")
7287 "user",
7288 net_rpc_user,
7289 NET_TRANSPORT_RPC,
7290 N_("List/modify users"),
7291 N_("net rpc user\n"
7292 " List/modify users")
7295 "password",
7296 rpc_user_password,
7297 NET_TRANSPORT_RPC,
7298 N_("Change a user password"),
7299 N_("net rpc password\n"
7300 " Change a user password\n"
7301 " Alias for net rpc user password")
7304 "group",
7305 net_rpc_group,
7306 NET_TRANSPORT_RPC,
7307 N_("List/modify groups"),
7308 N_("net rpc group\n"
7309 " List/modify groups")
7312 "share",
7313 net_rpc_share,
7314 NET_TRANSPORT_RPC,
7315 N_("List/modify shares"),
7316 N_("net rpc share\n"
7317 " List/modify shares")
7320 "file",
7321 net_rpc_file,
7322 NET_TRANSPORT_RPC,
7323 N_("List open files"),
7324 N_("net rpc file\n"
7325 " List open files")
7328 "printer",
7329 net_rpc_printer,
7330 NET_TRANSPORT_RPC,
7331 N_("List/modify printers"),
7332 N_("net rpc printer\n"
7333 " List/modify printers")
7336 "changetrustpw",
7337 net_rpc_changetrustpw,
7338 NET_TRANSPORT_RPC,
7339 N_("Change trust account password"),
7340 N_("net rpc changetrustpw\n"
7341 " Change trust account password")
7344 "trustdom",
7345 rpc_trustdom,
7346 NET_TRANSPORT_RPC,
7347 N_("Modify domain trusts"),
7348 N_("net rpc trustdom\n"
7349 " Modify domain trusts")
7352 "abortshutdown",
7353 rpc_shutdown_abort,
7354 NET_TRANSPORT_RPC,
7355 N_("Abort a remote shutdown"),
7356 N_("net rpc abortshutdown\n"
7357 " Abort a remote shutdown")
7360 "shutdown",
7361 rpc_shutdown,
7362 NET_TRANSPORT_RPC,
7363 N_("Shutdown a remote server"),
7364 N_("net rpc shutdown\n"
7365 " Shutdown a remote server")
7368 "samdump",
7369 rpc_samdump,
7370 NET_TRANSPORT_RPC,
7371 N_("Dump SAM data of remote NT PDC"),
7372 N_("net rpc samdump\n"
7373 " Dump SAM data of remote NT PDC")
7376 "vampire",
7377 rpc_vampire,
7378 NET_TRANSPORT_RPC,
7379 N_("Sync a remote NT PDC's data into local passdb"),
7380 N_("net rpc vampire\n"
7381 " Sync a remote NT PDC's data into local passdb")
7384 "getsid",
7385 net_rpc_getsid,
7386 NET_TRANSPORT_RPC,
7387 N_("Fetch the domain sid into local secrets.tdb"),
7388 N_("net rpc getsid\n"
7389 " Fetch the domain sid into local secrets.tdb")
7392 "rights",
7393 net_rpc_rights,
7394 NET_TRANSPORT_RPC,
7395 N_("Manage privileges assigned to SID"),
7396 N_("net rpc rights\n"
7397 " Manage privileges assigned to SID")
7400 "service",
7401 net_rpc_service,
7402 NET_TRANSPORT_RPC,
7403 N_("Start/stop/query remote services"),
7404 N_("net rpc service\n"
7405 " Start/stop/query remote services")
7408 "registry",
7409 net_rpc_registry,
7410 NET_TRANSPORT_RPC,
7411 N_("Manage registry hives"),
7412 N_("net rpc registry\n"
7413 " Manage registry hives")
7416 "shell",
7417 net_rpc_shell,
7418 NET_TRANSPORT_RPC,
7419 N_("Open interactive shell on remote server"),
7420 N_("net rpc shell\n"
7421 " Open interactive shell on remote server")
7423 {NULL, NULL, 0, NULL, NULL}
7426 status = libnetapi_init(&c->netapi_ctx);
7427 if (status != 0) {
7428 return -1;
7430 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
7431 libnetapi_set_password(c->netapi_ctx, c->opt_password);
7432 if (c->opt_kerberos) {
7433 libnetapi_set_use_kerberos(c->netapi_ctx);
7435 if (c->opt_ccache) {
7436 libnetapi_set_use_ccache(c->netapi_ctx);
7439 return net_run_function(c, argc, argv, "net rpc", func);