s3: Add --use-ccache to net
[Samba.git] / source3 / utils / net_rpc.c
blob120f634a7a7130c64a7b3189809c15bae602b524
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 "../librpc/gen_ndr/cli_lsa.h"
28 #include "../librpc/gen_ndr/cli_netlogon.h"
29 #include "../librpc/gen_ndr/cli_srvsvc.h"
30 #include "../librpc/gen_ndr/cli_spoolss.h"
31 #include "../librpc/gen_ndr/cli_initshutdown.h"
32 #include "../librpc/gen_ndr/cli_winreg.h"
34 static int net_mode_share;
35 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
37 /**
38 * @file net_rpc.c
40 * @brief RPC based subcommands for the 'net' utility.
42 * This file should contain much of the functionality that used to
43 * be found in rpcclient, execpt that the commands should change
44 * less often, and the fucntionality should be sane (the user is not
45 * expected to know a rid/sid before they conduct an operation etc.)
47 * @todo Perhaps eventually these should be split out into a number
48 * of files, as this could get quite big.
49 **/
52 /**
53 * Many of the RPC functions need the domain sid. This function gets
54 * it at the start of every run
56 * @param cli A cli_state already connected to the remote machine
58 * @return The Domain SID of the remote machine.
59 **/
61 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
62 DOM_SID **domain_sid,
63 const char **domain_name)
65 struct rpc_pipe_client *lsa_pipe = NULL;
66 struct policy_handle pol;
67 NTSTATUS result = NT_STATUS_OK;
68 union lsa_PolicyInformation *info = NULL;
70 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
71 &lsa_pipe);
72 if (!NT_STATUS_IS_OK(result)) {
73 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
74 return result;
77 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
78 SEC_FLAG_MAXIMUM_ALLOWED,
79 &pol);
80 if (!NT_STATUS_IS_OK(result)) {
81 d_fprintf(stderr, "open_policy %s: %s\n",
82 _("failed"),
83 nt_errstr(result));
84 return result;
87 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
88 &pol,
89 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
90 &info);
91 if (!NT_STATUS_IS_OK(result)) {
92 d_fprintf(stderr, "lsaquery %s: %s\n",
93 _("failed"),
94 nt_errstr(result));
95 return result;
98 *domain_name = info->account_domain.name.string;
99 *domain_sid = info->account_domain.sid;
101 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
102 TALLOC_FREE(lsa_pipe);
104 return NT_STATUS_OK;
108 * Run a single RPC command, from start to finish.
110 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
111 * @param conn_flag a NET_FLAG_ combination. Passed to
112 * net_make_ipc_connection.
113 * @param argc Standard main() style argc.
114 * @param argv Standard main() style argv. Initial components are already
115 * stripped.
116 * @return A shell status integer (0 for success).
119 int run_rpc_command(struct net_context *c,
120 struct cli_state *cli_arg,
121 const struct ndr_syntax_id *interface,
122 int conn_flags,
123 rpc_command_fn fn,
124 int argc,
125 const char **argv)
127 struct cli_state *cli = NULL;
128 struct rpc_pipe_client *pipe_hnd = NULL;
129 TALLOC_CTX *mem_ctx;
130 NTSTATUS nt_status;
131 DOM_SID *domain_sid;
132 const char *domain_name;
133 int ret = -1;
135 /* make use of cli_state handed over as an argument, if possible */
136 if (!cli_arg) {
137 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
138 if (!NT_STATUS_IS_OK(nt_status)) {
139 DEBUG(1, ("failed to make ipc connection: %s\n",
140 nt_errstr(nt_status)));
141 return -1;
143 } else {
144 cli = cli_arg;
147 if (!cli) {
148 return -1;
151 /* Create mem_ctx */
153 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
154 DEBUG(0, ("talloc_init() failed\n"));
155 goto fail;
158 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
159 &domain_name);
160 if (!NT_STATUS_IS_OK(nt_status)) {
161 goto fail;
164 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
165 if (lp_client_schannel()
166 && (ndr_syntax_id_equal(interface,
167 &ndr_table_netlogon.syntax_id))) {
168 /* Always try and create an schannel netlogon pipe. */
169 nt_status = cli_rpc_pipe_open_schannel(
170 cli, interface, NCACN_NP,
171 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
172 &pipe_hnd);
173 if (!NT_STATUS_IS_OK(nt_status)) {
174 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
175 nt_errstr(nt_status) ));
176 goto fail;
178 } else {
179 if (conn_flags & NET_FLAGS_SEAL) {
180 nt_status = cli_rpc_pipe_open_ntlmssp(
181 cli, interface,
182 (conn_flags & NET_FLAGS_TCP) ?
183 NCACN_IP_TCP : NCACN_NP,
184 DCERPC_AUTH_LEVEL_PRIVACY,
185 lp_workgroup(), c->opt_user_name,
186 c->opt_password, &pipe_hnd);
187 } else {
188 nt_status = cli_rpc_pipe_open_noauth(
189 cli, interface,
190 &pipe_hnd);
192 if (!NT_STATUS_IS_OK(nt_status)) {
193 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
194 get_pipe_name_from_syntax(
195 talloc_tos(), interface),
196 nt_errstr(nt_status) ));
197 goto fail;
202 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
204 if (!NT_STATUS_IS_OK(nt_status)) {
205 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
206 } else {
207 ret = 0;
208 DEBUG(5, ("rpc command function succedded\n"));
211 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
212 if (pipe_hnd) {
213 TALLOC_FREE(pipe_hnd);
217 fail:
218 /* close the connection only if it was opened here */
219 if (!cli_arg) {
220 cli_shutdown(cli);
223 talloc_destroy(mem_ctx);
224 return ret;
228 * Force a change of the trust acccount password.
230 * All parameters are provided by the run_rpc_command function, except for
231 * argc, argv which are passed through.
233 * @param domain_sid The domain sid acquired from the remote server.
234 * @param cli A cli_state connected to the server.
235 * @param mem_ctx Talloc context, destroyed on completion of the function.
236 * @param argc Standard main() style argc.
237 * @param argv Standard main() style argv. Initial components are already
238 * stripped.
240 * @return Normal NTSTATUS return.
243 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
244 const DOM_SID *domain_sid,
245 const char *domain_name,
246 struct cli_state *cli,
247 struct rpc_pipe_client *pipe_hnd,
248 TALLOC_CTX *mem_ctx,
249 int argc,
250 const char **argv)
252 NTSTATUS status;
254 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
255 if (!NT_STATUS_IS_OK(status)) {
256 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
257 nt_errstr(status));
258 return status;
261 return NT_STATUS_OK;
265 * Force a change of the trust acccount password.
267 * @param argc Standard main() style argc.
268 * @param argv Standard main() style argv. Initial components are already
269 * stripped.
271 * @return A shell status integer (0 for success).
274 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
276 if (c->display_usage) {
277 d_printf( "%s\n"
278 "net rpc changetrustpw\n"
279 " %s\n",
280 _("Usage:"),
281 _("Change the machine trust password"));
282 return 0;
285 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
286 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
287 rpc_changetrustpw_internals,
288 argc, argv);
292 * Join a domain, the old way.
294 * This uses 'machinename' as the inital password, and changes it.
296 * The password should be created with 'server manager' or equiv first.
298 * All parameters are provided by the run_rpc_command function, except for
299 * argc, argv which are passed through.
301 * @param domain_sid The domain sid acquired from the remote server.
302 * @param cli A cli_state connected to the server.
303 * @param mem_ctx Talloc context, destroyed on completion of the function.
304 * @param argc Standard main() style argc.
305 * @param argv Standard main() style argv. Initial components are already
306 * stripped.
308 * @return Normal NTSTATUS return.
311 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
312 const DOM_SID *domain_sid,
313 const char *domain_name,
314 struct cli_state *cli,
315 struct rpc_pipe_client *pipe_hnd,
316 TALLOC_CTX *mem_ctx,
317 int argc,
318 const char **argv)
321 fstring trust_passwd;
322 unsigned char orig_trust_passwd_hash[16];
323 NTSTATUS result;
324 enum netr_SchannelType sec_channel_type;
326 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
327 &pipe_hnd);
328 if (!NT_STATUS_IS_OK(result)) {
329 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
330 "error was %s\n",
331 cli->desthost,
332 nt_errstr(result) ));
333 return result;
337 check what type of join - if the user want's to join as
338 a BDC, the server must agree that we are a BDC.
340 if (argc >= 0) {
341 sec_channel_type = get_sec_channel_type(argv[0]);
342 } else {
343 sec_channel_type = get_sec_channel_type(NULL);
346 fstrcpy(trust_passwd, global_myname());
347 strlower_m(trust_passwd);
350 * Machine names can be 15 characters, but the max length on
351 * a password is 14. --jerry
354 trust_passwd[14] = '\0';
356 E_md4hash(trust_passwd, orig_trust_passwd_hash);
358 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
359 global_myname(),
360 orig_trust_passwd_hash,
361 sec_channel_type);
363 if (NT_STATUS_IS_OK(result))
364 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
367 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
368 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
369 result = NT_STATUS_UNSUCCESSFUL;
372 return result;
376 * Join a domain, the old way.
378 * @param argc Standard main() style argc.
379 * @param argv Standard main() style argv. Initial components are already
380 * stripped.
382 * @return A shell status integer (0 for success).
385 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
387 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
388 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
389 rpc_oldjoin_internals,
390 argc, argv);
394 * Join a domain, the old way. This function exists to allow
395 * the message to be displayed when oldjoin was explicitly
396 * requested, but not when it was implied by "net rpc join".
398 * @param argc Standard main() style argc.
399 * @param argv Standard main() style argv. Initial components are already
400 * stripped.
402 * @return A shell status integer (0 for success).
405 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
407 int rc = -1;
409 if (c->display_usage) {
410 d_printf( "%s\n"
411 "net rpc oldjoin\n"
412 " %s\n",
413 _("Usage:"),
414 _("Join a domain the old way"));
415 return 0;
418 rc = net_rpc_perform_oldjoin(c, argc, argv);
420 if (rc) {
421 d_fprintf(stderr, _("Failed to join domain\n"));
424 return rc;
428 * 'net rpc join' entrypoint.
429 * @param argc Standard main() style argc.
430 * @param argv Standard main() style argv. Initial components are already
431 * stripped
433 * Main 'net_rpc_join()' (where the admin username/password is used) is
434 * in net_rpc_join.c.
435 * Try to just change the password, but if that doesn't work, use/prompt
436 * for a username/password.
439 int net_rpc_join(struct net_context *c, int argc, const char **argv)
441 if (c->display_usage) {
442 d_printf("%s\n%s",
443 _("Usage:"),
444 _("net rpc join -U <username>[%%password] <type>\n"
445 " Join a domain\n"
446 " username\tName of the admin user"
447 " password\tPassword of the admin user, will "
448 "prompt if not specified\n"
449 " type\tCan be one of the following:\n"
450 "\t\tMEMBER\tJoin as member server (default)\n"
451 "\t\tBDC\tJoin as BDC\n"
452 "\t\tPDC\tJoin as PDC\n"));
453 return 0;
456 if (lp_server_role() == ROLE_STANDALONE) {
457 d_printf(_("cannot join as standalone machine\n"));
458 return -1;
461 if (strlen(global_myname()) > 15) {
462 d_printf(_("Our netbios name can be at most 15 chars long, "
463 "\"%s\" is %u chars long\n"),
464 global_myname(), (unsigned int)strlen(global_myname()));
465 return -1;
468 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
469 return 0;
471 return net_rpc_join_newstyle(c, argc, argv);
475 * display info about a rpc domain
477 * All parameters are provided by the run_rpc_command function, except for
478 * argc, argv which are passed through.
480 * @param domain_sid The domain sid acquired from the remote server
481 * @param cli A cli_state connected to the server.
482 * @param mem_ctx Talloc context, destroyed on completion of the function.
483 * @param argc Standard main() style argc.
484 * @param argv Standard main() style argv. Initial components are already
485 * stripped.
487 * @return Normal NTSTATUS return.
490 NTSTATUS rpc_info_internals(struct net_context *c,
491 const DOM_SID *domain_sid,
492 const char *domain_name,
493 struct cli_state *cli,
494 struct rpc_pipe_client *pipe_hnd,
495 TALLOC_CTX *mem_ctx,
496 int argc,
497 const char **argv)
499 struct policy_handle connect_pol, domain_pol;
500 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
501 union samr_DomainInfo *info = NULL;
502 fstring sid_str;
504 sid_to_fstring(sid_str, domain_sid);
506 /* Get sam policy handle */
507 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
508 pipe_hnd->desthost,
509 MAXIMUM_ALLOWED_ACCESS,
510 &connect_pol);
511 if (!NT_STATUS_IS_OK(result)) {
512 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
513 nt_errstr(result));
514 goto done;
517 /* Get domain policy handle */
518 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
519 &connect_pol,
520 MAXIMUM_ALLOWED_ACCESS,
521 CONST_DISCARD(struct dom_sid2 *, domain_sid),
522 &domain_pol);
523 if (!NT_STATUS_IS_OK(result)) {
524 d_fprintf(stderr, _("Could not open domain: %s\n"),
525 nt_errstr(result));
526 goto done;
529 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
530 &domain_pol,
532 &info);
533 if (NT_STATUS_IS_OK(result)) {
534 d_printf(_("Domain Name: %s\n"),
535 info->general.domain_name.string);
536 d_printf(_("Domain SID: %s\n"), sid_str);
537 d_printf(_("Sequence number: %llu\n"),
538 (unsigned long long)info->general.sequence_num);
539 d_printf(_("Num users: %u\n"), info->general.num_users);
540 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
541 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
544 done:
545 return result;
549 * 'net rpc info' entrypoint.
550 * @param argc Standard main() style argc.
551 * @param argv Standard main() style argv. Initial components are already
552 * stripped.
555 int net_rpc_info(struct net_context *c, int argc, const char **argv)
557 if (c->display_usage) {
558 d_printf( "%s\n"
559 "net rpc info\n"
560 " %s\n",
561 _("Usage:"),
562 _("Display information about the domain"));
563 return 0;
566 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
567 NET_FLAGS_PDC, rpc_info_internals,
568 argc, argv);
572 * Fetch domain SID into the local secrets.tdb.
574 * All parameters are provided by the run_rpc_command function, except for
575 * argc, argv which are passed through.
577 * @param domain_sid The domain sid acquired from the remote server.
578 * @param cli A cli_state connected to the server.
579 * @param mem_ctx Talloc context, destroyed on completion of the function.
580 * @param argc Standard main() style argc.
581 * @param argv Standard main() style argv. Initial components are already
582 * stripped.
584 * @return Normal NTSTATUS return.
587 static NTSTATUS rpc_getsid_internals(struct net_context *c,
588 const DOM_SID *domain_sid,
589 const char *domain_name,
590 struct cli_state *cli,
591 struct rpc_pipe_client *pipe_hnd,
592 TALLOC_CTX *mem_ctx,
593 int argc,
594 const char **argv)
596 fstring sid_str;
598 sid_to_fstring(sid_str, domain_sid);
599 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
600 sid_str, domain_name);
602 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
603 DEBUG(0,("Can't store domain SID\n"));
604 return NT_STATUS_UNSUCCESSFUL;
607 return NT_STATUS_OK;
611 * 'net rpc getsid' entrypoint.
612 * @param argc Standard main() style argc.
613 * @param argv Standard main() style argv. Initial components are already
614 * stripped.
617 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
619 int conn_flags = NET_FLAGS_PDC;
621 if (!c->opt_user_specified) {
622 conn_flags |= NET_FLAGS_ANONYMOUS;
625 if (c->display_usage) {
626 d_printf( "%s\n"
627 "net rpc getsid\n"
628 " %s\n",
629 _("Usage:"),
630 _("Fetch domain SID into local secrets.tdb"));
631 return 0;
634 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
635 conn_flags,
636 rpc_getsid_internals,
637 argc, argv);
640 /****************************************************************************/
643 * Basic usage function for 'net rpc user'.
644 * @param argc Standard main() style argc.
645 * @param argv Standard main() style argv. Initial components are already
646 * stripped.
649 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
651 return net_user_usage(c, argc, argv);
655 * Add a new user to a remote RPC server.
657 * @param argc Standard main() style argc.
658 * @param argv Standard main() style argv. Initial components are already
659 * stripped.
661 * @return A shell status integer (0 for success).
664 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
666 NET_API_STATUS status;
667 struct USER_INFO_1 info1;
668 uint32_t parm_error = 0;
670 if (argc < 1 || c->display_usage) {
671 rpc_user_usage(c, argc, argv);
672 return 0;
675 ZERO_STRUCT(info1);
677 info1.usri1_name = argv[0];
678 if (argc == 2) {
679 info1.usri1_password = argv[1];
682 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
684 if (status != 0) {
685 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
686 argv[0], libnetapi_get_error_string(c->netapi_ctx,
687 status));
688 return -1;
689 } else {
690 d_printf(_("Added user '%s'.\n"), argv[0]);
693 return 0;
697 * Rename a user on a remote RPC server.
699 * @param argc Standard main() style argc.
700 * @param argv Standard main() style argv. Initial components are already
701 * stripped.
703 * @return A shell status integer (0 for success).
706 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
708 NET_API_STATUS status;
709 struct USER_INFO_0 u0;
710 uint32_t parm_err = 0;
712 if (argc != 2 || c->display_usage) {
713 rpc_user_usage(c, argc, argv);
714 return 0;
717 u0.usri0_name = argv[1];
719 status = NetUserSetInfo(c->opt_host, argv[0],
720 0, (uint8_t *)&u0, &parm_err);
721 if (status) {
722 d_fprintf(stderr,
723 _("Failed to rename user from %s to %s - %s\n"),
724 argv[0], argv[1],
725 libnetapi_get_error_string(c->netapi_ctx, status));
726 } else {
727 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
730 return status;
734 * Delete a user from a remote RPC server.
736 * @param argc Standard main() style argc.
737 * @param argv Standard main() style argv. Initial components are already
738 * stripped.
740 * @return A shell status integer (0 for success).
743 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
745 NET_API_STATUS status;
747 if (argc < 1 || c->display_usage) {
748 rpc_user_usage(c, argc, argv);
749 return 0;
752 status = NetUserDel(c->opt_host, argv[0]);
754 if (status != 0) {
755 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
756 argv[0],
757 libnetapi_get_error_string(c->netapi_ctx, status));
758 return -1;
759 } else {
760 d_printf(_("Deleted user '%s'.\n"), argv[0]);
763 return 0;
767 * Set a user's password on a remote RPC server.
769 * @param argc Standard main() style argc.
770 * @param argv Standard main() style argv. Initial components are already
771 * stripped.
773 * @return A shell status integer (0 for success).
776 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
778 NET_API_STATUS status;
779 char *prompt = NULL;
780 struct USER_INFO_1003 u1003;
781 uint32_t parm_err = 0;
782 int ret;
784 if (argc < 1 || c->display_usage) {
785 rpc_user_usage(c, argc, argv);
786 return 0;
789 if (argv[1]) {
790 u1003.usri1003_password = argv[1];
791 } else {
792 ret = asprintf(&prompt, _("Enter new password for %s:"),
793 argv[0]);
794 if (ret == -1) {
795 return -1;
797 u1003.usri1003_password = getpass(prompt);
798 SAFE_FREE(prompt);
801 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
803 /* Display results */
804 if (status != 0) {
805 d_fprintf(stderr,
806 _("Failed to set password for '%s' with error: %s.\n"),
807 argv[0], libnetapi_get_error_string(c->netapi_ctx,
808 status));
809 return -1;
812 return 0;
816 * List a user's groups from a remote RPC server.
818 * @param argc Standard main() style argc.
819 * @param argv Standard main() style argv. Initial components are already
820 * stripped.
822 * @return A shell status integer (0 for success)
825 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
828 NET_API_STATUS status;
829 struct GROUP_USERS_INFO_0 *u0 = NULL;
830 uint32_t entries_read = 0;
831 uint32_t total_entries = 0;
832 int i;
835 if (argc < 1 || c->display_usage) {
836 rpc_user_usage(c, argc, argv);
837 return 0;
840 status = NetUserGetGroups(c->opt_host,
841 argv[0],
843 (uint8_t **)(void *)&u0,
844 (uint32_t)-1,
845 &entries_read,
846 &total_entries);
847 if (status != 0) {
848 d_fprintf(stderr,
849 _("Failed to get groups for '%s' with error: %s.\n"),
850 argv[0], libnetapi_get_error_string(c->netapi_ctx,
851 status));
852 return -1;
855 for (i=0; i < entries_read; i++) {
856 printf("%s\n", u0->grui0_name);
857 u0++;
860 return 0;
864 * List users on a remote RPC server.
866 * All parameters are provided by the run_rpc_command function, except for
867 * argc, argv which are passed through.
869 * @param domain_sid The domain sid acquired from the remote server.
870 * @param cli A cli_state connected to the server.
871 * @param mem_ctx Talloc context, destroyed on completion of the function.
872 * @param argc Standard main() style argc.
873 * @param argv Standard main() style argv. Initial components are already
874 * stripped.
876 * @return Normal NTSTATUS return.
879 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
881 NET_API_STATUS status;
882 uint32_t start_idx=0, num_entries, i, loop_count = 0;
883 struct NET_DISPLAY_USER *info = NULL;
884 void *buffer = NULL;
886 /* Query domain users */
887 if (c->opt_long_list_entries)
888 d_printf(_("\nUser name Comment"
889 "\n-----------------------------\n"));
890 do {
891 uint32_t max_entries, max_size;
893 get_query_dispinfo_params(
894 loop_count, &max_entries, &max_size);
896 status = NetQueryDisplayInformation(c->opt_host,
898 start_idx,
899 max_entries,
900 max_size,
901 &num_entries,
902 &buffer);
903 if (status != 0 && status != ERROR_MORE_DATA) {
904 return status;
907 info = (struct NET_DISPLAY_USER *)buffer;
909 for (i = 0; i < num_entries; i++) {
911 if (c->opt_long_list_entries)
912 printf("%-21.21s %s\n", info->usri1_name,
913 info->usri1_comment);
914 else
915 printf("%s\n", info->usri1_name);
916 info++;
919 NetApiBufferFree(buffer);
921 loop_count++;
922 start_idx += num_entries;
924 } while (status == ERROR_MORE_DATA);
926 return status;
930 * 'net rpc user' entrypoint.
931 * @param argc Standard main() style argc.
932 * @param argv Standard main() style argv. Initial components are already
933 * stripped.
936 int net_rpc_user(struct net_context *c, int argc, const char **argv)
938 NET_API_STATUS status;
940 struct functable func[] = {
942 "add",
943 rpc_user_add,
944 NET_TRANSPORT_RPC,
945 N_("Add specified user"),
946 N_("net rpc user add\n"
947 " Add specified user")
950 "info",
951 rpc_user_info,
952 NET_TRANSPORT_RPC,
953 N_("List domain groups of user"),
954 N_("net rpc user info\n"
955 " Lis domain groups of user")
958 "delete",
959 rpc_user_delete,
960 NET_TRANSPORT_RPC,
961 N_("Remove specified user"),
962 N_("net rpc user delete\n"
963 " Remove specified user")
966 "password",
967 rpc_user_password,
968 NET_TRANSPORT_RPC,
969 N_("Change user password"),
970 N_("net rpc user password\n"
971 " Change user password")
974 "rename",
975 rpc_user_rename,
976 NET_TRANSPORT_RPC,
977 N_("Rename specified user"),
978 N_("net rpc user rename\n"
979 " Rename specified user")
981 {NULL, NULL, 0, NULL, NULL}
984 status = libnetapi_init(&c->netapi_ctx);
985 if (status != 0) {
986 return -1;
988 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
989 libnetapi_set_password(c->netapi_ctx, c->opt_password);
990 if (c->opt_kerberos) {
991 libnetapi_set_use_kerberos(c->netapi_ctx);
994 if (argc == 0) {
995 if (c->display_usage) {
996 d_printf( "%s\n"
997 "net rpc user\n"
998 " %s\n",
999 _("Usage:"),
1000 _("List all users"));
1001 net_display_usage_from_functable(func);
1002 return 0;
1005 return rpc_user_list(c, argc, argv);
1008 return net_run_function(c, argc, argv, "net rpc user", func);
1011 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1012 TALLOC_CTX *mem_ctx,
1013 struct rpc_sh_ctx *ctx,
1014 struct rpc_pipe_client *pipe_hnd,
1015 int argc, const char **argv)
1017 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1020 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1021 TALLOC_CTX *mem_ctx,
1022 struct rpc_sh_ctx *ctx,
1023 struct rpc_pipe_client *pipe_hnd,
1024 int argc, const char **argv)
1026 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1029 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1030 TALLOC_CTX *mem_ctx,
1031 struct rpc_sh_ctx *ctx,
1032 struct rpc_pipe_client *pipe_hnd,
1033 int argc, const char **argv,
1034 NTSTATUS (*fn)(
1035 struct net_context *c,
1036 TALLOC_CTX *mem_ctx,
1037 struct rpc_sh_ctx *ctx,
1038 struct rpc_pipe_client *pipe_hnd,
1039 struct policy_handle *user_hnd,
1040 int argc, const char **argv))
1042 struct policy_handle connect_pol, domain_pol, user_pol;
1043 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1044 DOM_SID sid;
1045 uint32 rid;
1046 enum lsa_SidType type;
1048 if (argc == 0) {
1049 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1050 ctx->whoami);
1051 return NT_STATUS_INVALID_PARAMETER;
1054 ZERO_STRUCT(connect_pol);
1055 ZERO_STRUCT(domain_pol);
1056 ZERO_STRUCT(user_pol);
1058 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1059 argv[0], NULL, NULL, &sid, &type);
1060 if (!NT_STATUS_IS_OK(result)) {
1061 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1062 nt_errstr(result));
1063 goto done;
1066 if (type != SID_NAME_USER) {
1067 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1068 sid_type_lookup(type));
1069 result = NT_STATUS_NO_SUCH_USER;
1070 goto done;
1073 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1074 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1075 result = NT_STATUS_NO_SUCH_USER;
1076 goto done;
1079 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1080 pipe_hnd->desthost,
1081 MAXIMUM_ALLOWED_ACCESS,
1082 &connect_pol);
1083 if (!NT_STATUS_IS_OK(result)) {
1084 goto done;
1087 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1088 &connect_pol,
1089 MAXIMUM_ALLOWED_ACCESS,
1090 ctx->domain_sid,
1091 &domain_pol);
1092 if (!NT_STATUS_IS_OK(result)) {
1093 goto done;
1096 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1097 &domain_pol,
1098 MAXIMUM_ALLOWED_ACCESS,
1099 rid,
1100 &user_pol);
1101 if (!NT_STATUS_IS_OK(result)) {
1102 goto done;
1105 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1107 done:
1108 if (is_valid_policy_hnd(&user_pol)) {
1109 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1111 if (is_valid_policy_hnd(&domain_pol)) {
1112 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1114 if (is_valid_policy_hnd(&connect_pol)) {
1115 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1117 return result;
1120 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1121 TALLOC_CTX *mem_ctx,
1122 struct rpc_sh_ctx *ctx,
1123 struct rpc_pipe_client *pipe_hnd,
1124 struct policy_handle *user_hnd,
1125 int argc, const char **argv)
1127 NTSTATUS result;
1128 union samr_UserInfo *info = NULL;
1130 if (argc != 0) {
1131 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1132 ctx->whoami);
1133 return NT_STATUS_INVALID_PARAMETER;
1136 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1137 user_hnd,
1139 &info);
1140 if (!NT_STATUS_IS_OK(result)) {
1141 return result;
1144 d_printf(_("user rid: %d, group rid: %d\n"),
1145 info->info21.rid,
1146 info->info21.primary_gid);
1148 return result;
1151 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1152 TALLOC_CTX *mem_ctx,
1153 struct rpc_sh_ctx *ctx,
1154 struct rpc_pipe_client *pipe_hnd,
1155 int argc, const char **argv)
1157 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1158 rpc_sh_user_show_internals);
1161 #define FETCHSTR(name, rec) \
1162 do { if (strequal(ctx->thiscmd, name)) { \
1163 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1164 } while (0);
1166 #define SETSTR(name, rec, flag) \
1167 do { if (strequal(ctx->thiscmd, name)) { \
1168 init_lsa_String(&(info->info21.rec), argv[0]); \
1169 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1170 } while (0);
1172 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1173 TALLOC_CTX *mem_ctx,
1174 struct rpc_sh_ctx *ctx,
1175 struct rpc_pipe_client *pipe_hnd,
1176 struct policy_handle *user_hnd,
1177 int argc, const char **argv)
1179 NTSTATUS result;
1180 const char *username;
1181 const char *oldval = "";
1182 union samr_UserInfo *info = NULL;
1184 if (argc > 1) {
1185 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1186 _("Usage:"), ctx->whoami);
1187 return NT_STATUS_INVALID_PARAMETER;
1190 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1191 user_hnd,
1193 &info);
1194 if (!NT_STATUS_IS_OK(result)) {
1195 return result;
1198 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1200 FETCHSTR("fullname", full_name);
1201 FETCHSTR("homedir", home_directory);
1202 FETCHSTR("homedrive", home_drive);
1203 FETCHSTR("logonscript", logon_script);
1204 FETCHSTR("profilepath", profile_path);
1205 FETCHSTR("description", description);
1207 if (argc == 0) {
1208 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1209 goto done;
1212 if (strcmp(argv[0], "NULL") == 0) {
1213 argv[0] = "";
1216 ZERO_STRUCT(info->info21);
1218 SETSTR("fullname", full_name, FULL_NAME);
1219 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1220 SETSTR("homedrive", home_drive, HOME_DRIVE);
1221 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1222 SETSTR("profilepath", profile_path, PROFILE_PATH);
1223 SETSTR("description", description, DESCRIPTION);
1225 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1226 user_hnd,
1228 info);
1230 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1231 ctx->thiscmd, oldval, argv[0]);
1233 done:
1235 return result;
1238 #define HANDLEFLG(name, rec) \
1239 do { if (strequal(ctx->thiscmd, name)) { \
1240 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1241 if (newval) { \
1242 newflags = oldflags | ACB_##rec; \
1243 } else { \
1244 newflags = oldflags & ~ACB_##rec; \
1245 } } } while (0);
1247 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1248 TALLOC_CTX *mem_ctx,
1249 struct rpc_sh_ctx *ctx,
1250 struct rpc_pipe_client *pipe_hnd,
1251 int argc, const char **argv)
1253 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1254 rpc_sh_user_str_edit_internals);
1257 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1258 TALLOC_CTX *mem_ctx,
1259 struct rpc_sh_ctx *ctx,
1260 struct rpc_pipe_client *pipe_hnd,
1261 struct policy_handle *user_hnd,
1262 int argc, const char **argv)
1264 NTSTATUS result;
1265 const char *username;
1266 const char *oldval = "unknown";
1267 uint32 oldflags, newflags;
1268 bool newval;
1269 union samr_UserInfo *info = NULL;
1271 if ((argc > 1) ||
1272 ((argc == 1) && !strequal(argv[0], "yes") &&
1273 !strequal(argv[0], "no"))) {
1274 /* TRANSATORS: The yes|no here are program keywords. Please do
1275 not translate. */
1276 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1277 ctx->whoami);
1278 return NT_STATUS_INVALID_PARAMETER;
1281 newval = strequal(argv[0], "yes");
1283 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1284 user_hnd,
1286 &info);
1287 if (!NT_STATUS_IS_OK(result)) {
1288 return result;
1291 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1292 oldflags = info->info21.acct_flags;
1293 newflags = info->info21.acct_flags;
1295 HANDLEFLG("disabled", DISABLED);
1296 HANDLEFLG("pwnotreq", PWNOTREQ);
1297 HANDLEFLG("autolock", AUTOLOCK);
1298 HANDLEFLG("pwnoexp", PWNOEXP);
1300 if (argc == 0) {
1301 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1302 oldval);
1303 goto done;
1306 ZERO_STRUCT(info->info21);
1308 info->info21.acct_flags = newflags;
1309 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1311 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1312 user_hnd,
1314 info);
1316 if (NT_STATUS_IS_OK(result)) {
1317 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1318 ctx->thiscmd, oldval, argv[0]);
1321 done:
1323 return result;
1326 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1327 TALLOC_CTX *mem_ctx,
1328 struct rpc_sh_ctx *ctx,
1329 struct rpc_pipe_client *pipe_hnd,
1330 int argc, const char **argv)
1332 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1333 rpc_sh_user_flag_edit_internals);
1336 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1337 TALLOC_CTX *mem_ctx,
1338 struct rpc_sh_ctx *ctx)
1340 static struct rpc_sh_cmd cmds[] = {
1342 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1343 N_("Show/Set a user's full name") },
1345 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1346 N_("Show/Set a user's home directory") },
1348 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1349 N_("Show/Set a user's home drive") },
1351 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1352 N_("Show/Set a user's logon script") },
1354 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1355 N_("Show/Set a user's profile path") },
1357 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1358 N_("Show/Set a user's description") },
1360 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1361 N_("Show/Set whether a user is disabled") },
1363 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1364 N_("Show/Set whether a user locked out") },
1366 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1367 N_("Show/Set whether a user does not need a password") },
1369 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1370 N_("Show/Set whether a user's password does not expire") },
1372 { NULL, NULL, 0, NULL, NULL }
1375 return cmds;
1378 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1379 TALLOC_CTX *mem_ctx,
1380 struct rpc_sh_ctx *ctx)
1382 static struct rpc_sh_cmd cmds[] = {
1384 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1385 N_("List available users") },
1387 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1388 N_("List the domain groups a user is member of") },
1390 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1391 N_("Show info about a user") },
1393 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1394 N_("Show/Modify a user's fields") },
1396 { NULL, NULL, 0, NULL, NULL }
1399 return cmds;
1402 /****************************************************************************/
1405 * Basic usage function for 'net rpc group'.
1406 * @param argc Standard main() style argc.
1407 * @param argv Standard main() style argv. Initial components are already
1408 * stripped.
1411 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1413 return net_group_usage(c, argc, argv);
1417 * Delete group on a remote RPC server.
1419 * All parameters are provided by the run_rpc_command function, except for
1420 * argc, argv which are passed through.
1422 * @param domain_sid The domain sid acquired from the remote server.
1423 * @param cli A cli_state connected to the server.
1424 * @param mem_ctx Talloc context, destroyed on completion of the function.
1425 * @param argc Standard main() style argc.
1426 * @param argv Standard main() style argv. Initial components are already
1427 * stripped.
1429 * @return Normal NTSTATUS return.
1432 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1433 const DOM_SID *domain_sid,
1434 const char *domain_name,
1435 struct cli_state *cli,
1436 struct rpc_pipe_client *pipe_hnd,
1437 TALLOC_CTX *mem_ctx,
1438 int argc,
1439 const char **argv)
1441 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1442 bool group_is_primary = false;
1443 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1444 uint32_t group_rid;
1445 struct samr_RidTypeArray *rids = NULL;
1446 /* char **names; */
1447 int i;
1448 /* struct samr_RidWithAttribute *user_gids; */
1450 struct samr_Ids group_rids, name_types;
1451 struct lsa_String lsa_acct_name;
1452 union samr_UserInfo *info = NULL;
1454 if (argc < 1 || c->display_usage) {
1455 rpc_group_usage(c, argc,argv);
1456 return NT_STATUS_OK; /* ok? */
1459 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1460 pipe_hnd->desthost,
1461 MAXIMUM_ALLOWED_ACCESS,
1462 &connect_pol);
1464 if (!NT_STATUS_IS_OK(result)) {
1465 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1466 goto done;
1469 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1470 &connect_pol,
1471 MAXIMUM_ALLOWED_ACCESS,
1472 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1473 &domain_pol);
1475 if (!NT_STATUS_IS_OK(result)) {
1476 d_fprintf(stderr, _("Request open_domain failed\n"));
1477 goto done;
1480 init_lsa_String(&lsa_acct_name, argv[0]);
1482 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1483 &domain_pol,
1485 &lsa_acct_name,
1486 &group_rids,
1487 &name_types);
1488 if (!NT_STATUS_IS_OK(result)) {
1489 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1490 goto done;
1493 switch (name_types.ids[0])
1495 case SID_NAME_DOM_GRP:
1496 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1497 &domain_pol,
1498 MAXIMUM_ALLOWED_ACCESS,
1499 group_rids.ids[0],
1500 &group_pol);
1501 if (!NT_STATUS_IS_OK(result)) {
1502 d_fprintf(stderr, _("Request open_group failed"));
1503 goto done;
1506 group_rid = group_rids.ids[0];
1508 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1509 &group_pol,
1510 &rids);
1512 if (!NT_STATUS_IS_OK(result)) {
1513 d_fprintf(stderr,
1514 _("Unable to query group members of %s"),
1515 argv[0]);
1516 goto done;
1519 if (c->opt_verbose) {
1520 d_printf(
1521 _("Domain Group %s (rid: %d) has %d members\n"),
1522 argv[0],group_rid, rids->count);
1525 /* Check if group is anyone's primary group */
1526 for (i = 0; i < rids->count; i++)
1528 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1529 &domain_pol,
1530 MAXIMUM_ALLOWED_ACCESS,
1531 rids->rids[i],
1532 &user_pol);
1534 if (!NT_STATUS_IS_OK(result)) {
1535 d_fprintf(stderr,
1536 _("Unable to open group member %d\n"),
1537 rids->rids[i]);
1538 goto done;
1541 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1542 &user_pol,
1544 &info);
1546 if (!NT_STATUS_IS_OK(result)) {
1547 d_fprintf(stderr,
1548 _("Unable to lookup userinfo for group "
1549 "member %d\n"),
1550 rids->rids[i]);
1551 goto done;
1554 if (info->info21.primary_gid == group_rid) {
1555 if (c->opt_verbose) {
1556 d_printf(_("Group is primary group "
1557 "of %s\n"),
1558 info->info21.account_name.string);
1560 group_is_primary = true;
1563 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1566 if (group_is_primary) {
1567 d_fprintf(stderr, _("Unable to delete group because "
1568 "some of it's members have it as primary "
1569 "group\n"));
1570 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1571 goto done;
1574 /* remove all group members */
1575 for (i = 0; i < rids->count; i++)
1577 if (c->opt_verbose)
1578 d_printf(_("Remove group member %d..."),
1579 rids->rids[i]);
1580 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1581 &group_pol,
1582 rids->rids[i]);
1584 if (NT_STATUS_IS_OK(result)) {
1585 if (c->opt_verbose)
1586 d_printf(_("ok\n"));
1587 } else {
1588 if (c->opt_verbose)
1589 d_printf("%s\n", _("failed"));
1590 goto done;
1594 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1595 &group_pol);
1597 break;
1598 /* removing a local group is easier... */
1599 case SID_NAME_ALIAS:
1600 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1601 &domain_pol,
1602 MAXIMUM_ALLOWED_ACCESS,
1603 group_rids.ids[0],
1604 &group_pol);
1606 if (!NT_STATUS_IS_OK(result)) {
1607 d_fprintf(stderr, _("Request open_alias failed\n"));
1608 goto done;
1611 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1612 &group_pol);
1613 break;
1614 default:
1615 d_fprintf(stderr, _("%s is of type %s. This command is only "
1616 "for deleting local or global groups\n"),
1617 argv[0],sid_type_lookup(name_types.ids[0]));
1618 result = NT_STATUS_UNSUCCESSFUL;
1619 goto done;
1622 if (NT_STATUS_IS_OK(result)) {
1623 if (c->opt_verbose)
1624 d_printf(_("Deleted %s '%s'\n"),
1625 sid_type_lookup(name_types.ids[0]), argv[0]);
1626 } else {
1627 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1628 get_friendly_nt_error_msg(result));
1631 done:
1632 return result;
1636 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1638 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1639 rpc_group_delete_internals, argc,argv);
1642 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1644 NET_API_STATUS status;
1645 struct GROUP_INFO_1 info1;
1646 uint32_t parm_error = 0;
1648 if (argc != 1 || c->display_usage) {
1649 rpc_group_usage(c, argc, argv);
1650 return 0;
1653 ZERO_STRUCT(info1);
1655 info1.grpi1_name = argv[0];
1656 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1657 info1.grpi1_comment = c->opt_comment;
1660 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1662 if (status != 0) {
1663 d_fprintf(stderr,
1664 _("Failed to add group '%s' with error: %s.\n"),
1665 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1666 status));
1667 return -1;
1668 } else {
1669 d_printf(_("Added group '%s'.\n"), argv[0]);
1672 return 0;
1675 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1677 NET_API_STATUS status;
1678 struct LOCALGROUP_INFO_1 info1;
1679 uint32_t parm_error = 0;
1681 if (argc != 1 || c->display_usage) {
1682 rpc_group_usage(c, argc, argv);
1683 return 0;
1686 ZERO_STRUCT(info1);
1688 info1.lgrpi1_name = argv[0];
1689 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1690 info1.lgrpi1_comment = c->opt_comment;
1693 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1695 if (status != 0) {
1696 d_fprintf(stderr,
1697 _("Failed to add alias '%s' with error: %s.\n"),
1698 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1699 status));
1700 return -1;
1701 } else {
1702 d_printf(_("Added alias '%s'.\n"), argv[0]);
1705 return 0;
1708 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1710 if (c->opt_localgroup)
1711 return rpc_alias_add_internals(c, argc, argv);
1713 return rpc_group_add_internals(c, argc, argv);
1716 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1717 TALLOC_CTX *mem_ctx,
1718 const char *name,
1719 DOM_SID *sid,
1720 enum lsa_SidType *type)
1722 DOM_SID *sids = NULL;
1723 enum lsa_SidType *types = NULL;
1724 struct rpc_pipe_client *pipe_hnd = NULL;
1725 struct policy_handle lsa_pol;
1726 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1728 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1729 &pipe_hnd);
1730 if (!NT_STATUS_IS_OK(result)) {
1731 goto done;
1734 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1735 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1737 if (!NT_STATUS_IS_OK(result)) {
1738 goto done;
1741 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1742 &name, NULL, 1, &sids, &types);
1744 if (NT_STATUS_IS_OK(result)) {
1745 sid_copy(sid, &sids[0]);
1746 *type = types[0];
1749 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1751 done:
1752 if (pipe_hnd) {
1753 TALLOC_FREE(pipe_hnd);
1756 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1758 /* Try as S-1-5-whatever */
1760 DOM_SID tmp_sid;
1762 if (string_to_sid(&tmp_sid, name)) {
1763 sid_copy(sid, &tmp_sid);
1764 *type = SID_NAME_UNKNOWN;
1765 result = NT_STATUS_OK;
1769 return result;
1772 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1773 TALLOC_CTX *mem_ctx,
1774 const DOM_SID *group_sid,
1775 const char *member)
1777 struct policy_handle connect_pol, domain_pol;
1778 NTSTATUS result;
1779 uint32 group_rid;
1780 struct policy_handle group_pol;
1782 struct samr_Ids rids, rid_types;
1783 struct lsa_String lsa_acct_name;
1785 DOM_SID sid;
1787 sid_copy(&sid, group_sid);
1789 if (!sid_split_rid(&sid, &group_rid)) {
1790 return NT_STATUS_UNSUCCESSFUL;
1793 /* Get sam policy handle */
1794 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1795 pipe_hnd->desthost,
1796 MAXIMUM_ALLOWED_ACCESS,
1797 &connect_pol);
1798 if (!NT_STATUS_IS_OK(result)) {
1799 return result;
1802 /* Get domain policy handle */
1803 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1804 &connect_pol,
1805 MAXIMUM_ALLOWED_ACCESS,
1806 &sid,
1807 &domain_pol);
1808 if (!NT_STATUS_IS_OK(result)) {
1809 return result;
1812 init_lsa_String(&lsa_acct_name, member);
1814 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1815 &domain_pol,
1817 &lsa_acct_name,
1818 &rids,
1819 &rid_types);
1821 if (!NT_STATUS_IS_OK(result)) {
1822 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1823 member);
1824 goto done;
1827 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1828 &domain_pol,
1829 MAXIMUM_ALLOWED_ACCESS,
1830 group_rid,
1831 &group_pol);
1833 if (!NT_STATUS_IS_OK(result)) {
1834 goto done;
1837 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1838 &group_pol,
1839 rids.ids[0],
1840 0x0005); /* unknown flags */
1842 done:
1843 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1844 return result;
1847 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1848 TALLOC_CTX *mem_ctx,
1849 const DOM_SID *alias_sid,
1850 const char *member)
1852 struct policy_handle connect_pol, domain_pol;
1853 NTSTATUS result;
1854 uint32 alias_rid;
1855 struct policy_handle alias_pol;
1857 DOM_SID member_sid;
1858 enum lsa_SidType member_type;
1860 DOM_SID sid;
1862 sid_copy(&sid, alias_sid);
1864 if (!sid_split_rid(&sid, &alias_rid)) {
1865 return NT_STATUS_UNSUCCESSFUL;
1868 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1869 member, &member_sid, &member_type);
1871 if (!NT_STATUS_IS_OK(result)) {
1872 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1873 member);
1874 return result;
1877 /* Get sam policy handle */
1878 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1879 pipe_hnd->desthost,
1880 MAXIMUM_ALLOWED_ACCESS,
1881 &connect_pol);
1882 if (!NT_STATUS_IS_OK(result)) {
1883 goto done;
1886 /* Get domain policy handle */
1887 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1888 &connect_pol,
1889 MAXIMUM_ALLOWED_ACCESS,
1890 &sid,
1891 &domain_pol);
1892 if (!NT_STATUS_IS_OK(result)) {
1893 goto done;
1896 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1897 &domain_pol,
1898 MAXIMUM_ALLOWED_ACCESS,
1899 alias_rid,
1900 &alias_pol);
1902 if (!NT_STATUS_IS_OK(result)) {
1903 return result;
1906 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1907 &alias_pol,
1908 &member_sid);
1910 if (!NT_STATUS_IS_OK(result)) {
1911 return result;
1914 done:
1915 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1916 return result;
1919 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1920 const DOM_SID *domain_sid,
1921 const char *domain_name,
1922 struct cli_state *cli,
1923 struct rpc_pipe_client *pipe_hnd,
1924 TALLOC_CTX *mem_ctx,
1925 int argc,
1926 const char **argv)
1928 DOM_SID group_sid;
1929 enum lsa_SidType group_type;
1931 if (argc != 2 || c->display_usage) {
1932 d_printf("%s\n%s",
1933 _("Usage:"),
1934 _("net rpc group addmem <group> <member>\n"
1935 " Add a member to a group\n"
1936 " group\tGroup to add member to\n"
1937 " member\tMember to add to group\n"));
1938 return NT_STATUS_UNSUCCESSFUL;
1941 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1942 &group_sid, &group_type))) {
1943 d_fprintf(stderr, _("Could not lookup group name %s\n"),
1944 argv[0]);
1945 return NT_STATUS_UNSUCCESSFUL;
1948 if (group_type == SID_NAME_DOM_GRP) {
1949 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
1950 &group_sid, argv[1]);
1952 if (!NT_STATUS_IS_OK(result)) {
1953 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
1954 argv[1], argv[0], nt_errstr(result));
1956 return result;
1959 if (group_type == SID_NAME_ALIAS) {
1960 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
1961 &group_sid, argv[1]);
1963 if (!NT_STATUS_IS_OK(result)) {
1964 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
1965 argv[1], argv[0], nt_errstr(result));
1967 return result;
1970 d_fprintf(stderr, _("Can only add members to global or local groups "
1971 "which %s is not\n"), argv[0]);
1973 return NT_STATUS_UNSUCCESSFUL;
1976 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
1978 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1979 rpc_group_addmem_internals,
1980 argc, argv);
1983 static NTSTATUS rpc_del_groupmem(struct net_context *c,
1984 struct rpc_pipe_client *pipe_hnd,
1985 TALLOC_CTX *mem_ctx,
1986 const DOM_SID *group_sid,
1987 const char *member)
1989 struct policy_handle connect_pol, domain_pol;
1990 NTSTATUS result;
1991 uint32 group_rid;
1992 struct policy_handle group_pol;
1994 struct samr_Ids rids, rid_types;
1995 struct lsa_String lsa_acct_name;
1997 DOM_SID sid;
1999 sid_copy(&sid, group_sid);
2001 if (!sid_split_rid(&sid, &group_rid))
2002 return NT_STATUS_UNSUCCESSFUL;
2004 /* Get sam policy handle */
2005 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2006 pipe_hnd->desthost,
2007 MAXIMUM_ALLOWED_ACCESS,
2008 &connect_pol);
2009 if (!NT_STATUS_IS_OK(result))
2010 return result;
2012 /* Get domain policy handle */
2013 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2014 &connect_pol,
2015 MAXIMUM_ALLOWED_ACCESS,
2016 &sid,
2017 &domain_pol);
2018 if (!NT_STATUS_IS_OK(result))
2019 return result;
2021 init_lsa_String(&lsa_acct_name, member);
2023 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2024 &domain_pol,
2026 &lsa_acct_name,
2027 &rids,
2028 &rid_types);
2029 if (!NT_STATUS_IS_OK(result)) {
2030 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2031 member);
2032 goto done;
2035 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2036 &domain_pol,
2037 MAXIMUM_ALLOWED_ACCESS,
2038 group_rid,
2039 &group_pol);
2041 if (!NT_STATUS_IS_OK(result))
2042 goto done;
2044 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2045 &group_pol,
2046 rids.ids[0]);
2048 done:
2049 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2050 return result;
2053 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2054 TALLOC_CTX *mem_ctx,
2055 const DOM_SID *alias_sid,
2056 const char *member)
2058 struct policy_handle connect_pol, domain_pol;
2059 NTSTATUS result;
2060 uint32 alias_rid;
2061 struct policy_handle alias_pol;
2063 DOM_SID member_sid;
2064 enum lsa_SidType member_type;
2066 DOM_SID sid;
2068 sid_copy(&sid, alias_sid);
2070 if (!sid_split_rid(&sid, &alias_rid))
2071 return NT_STATUS_UNSUCCESSFUL;
2073 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2074 member, &member_sid, &member_type);
2076 if (!NT_STATUS_IS_OK(result)) {
2077 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2078 member);
2079 return result;
2082 /* Get sam policy handle */
2083 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2084 pipe_hnd->desthost,
2085 MAXIMUM_ALLOWED_ACCESS,
2086 &connect_pol);
2087 if (!NT_STATUS_IS_OK(result)) {
2088 goto done;
2091 /* Get domain policy handle */
2092 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2093 &connect_pol,
2094 MAXIMUM_ALLOWED_ACCESS,
2095 &sid,
2096 &domain_pol);
2097 if (!NT_STATUS_IS_OK(result)) {
2098 goto done;
2101 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2102 &domain_pol,
2103 MAXIMUM_ALLOWED_ACCESS,
2104 alias_rid,
2105 &alias_pol);
2107 if (!NT_STATUS_IS_OK(result))
2108 return result;
2110 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2111 &alias_pol,
2112 &member_sid);
2114 if (!NT_STATUS_IS_OK(result))
2115 return result;
2117 done:
2118 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2119 return result;
2122 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2123 const DOM_SID *domain_sid,
2124 const char *domain_name,
2125 struct cli_state *cli,
2126 struct rpc_pipe_client *pipe_hnd,
2127 TALLOC_CTX *mem_ctx,
2128 int argc,
2129 const char **argv)
2131 DOM_SID group_sid;
2132 enum lsa_SidType group_type;
2134 if (argc != 2 || c->display_usage) {
2135 d_printf("%s\n%s",
2136 _("Usage:"),
2137 _("net rpc group delmem <group> <member>\n"
2138 " Delete a member from a group\n"
2139 " group\tGroup to delete member from\n"
2140 " member\tMember to delete from group\n"));
2141 return NT_STATUS_UNSUCCESSFUL;
2144 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2145 &group_sid, &group_type))) {
2146 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2147 argv[0]);
2148 return NT_STATUS_UNSUCCESSFUL;
2151 if (group_type == SID_NAME_DOM_GRP) {
2152 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2153 &group_sid, argv[1]);
2155 if (!NT_STATUS_IS_OK(result)) {
2156 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2157 argv[1], argv[0], nt_errstr(result));
2159 return result;
2162 if (group_type == SID_NAME_ALIAS) {
2163 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2164 &group_sid, argv[1]);
2166 if (!NT_STATUS_IS_OK(result)) {
2167 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2168 argv[1], argv[0], nt_errstr(result));
2170 return result;
2173 d_fprintf(stderr, _("Can only delete members from global or local "
2174 "groups which %s is not\n"), argv[0]);
2176 return NT_STATUS_UNSUCCESSFUL;
2179 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2181 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2182 rpc_group_delmem_internals,
2183 argc, argv);
2187 * List groups on a remote RPC server.
2189 * All parameters are provided by the run_rpc_command function, except for
2190 * argc, argv which are passes through.
2192 * @param domain_sid The domain sid acquired from the remote server.
2193 * @param cli A cli_state connected to the server.
2194 * @param mem_ctx Talloc context, destroyed on completion of the function.
2195 * @param argc Standard main() style argc.
2196 * @param argv Standard main() style argv. Initial components are already
2197 * stripped.
2199 * @return Normal NTSTATUS return.
2202 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2203 const DOM_SID *domain_sid,
2204 const char *domain_name,
2205 struct cli_state *cli,
2206 struct rpc_pipe_client *pipe_hnd,
2207 TALLOC_CTX *mem_ctx,
2208 int argc,
2209 const char **argv)
2211 struct policy_handle connect_pol, domain_pol;
2212 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2213 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2214 struct samr_SamArray *groups = NULL;
2215 bool global = false;
2216 bool local = false;
2217 bool builtin = false;
2219 if (c->display_usage) {
2220 d_printf("%s\n%s",
2221 _("Usage:"),
2222 _("net rpc group list [global] [local] [builtin]\n"
2223 " List groups on RPC server\n"
2224 " global\tList global groups\n"
2225 " local\tList local groups\n"
2226 " builtin\tList builtin groups\n"
2227 " If none of global, local or builtin is "
2228 "specified, all three options are considered "
2229 "set\n"));
2230 return NT_STATUS_OK;
2233 if (argc == 0) {
2234 global = true;
2235 local = true;
2236 builtin = true;
2239 for (i=0; i<argc; i++) {
2240 if (strequal(argv[i], "global"))
2241 global = true;
2243 if (strequal(argv[i], "local"))
2244 local = true;
2246 if (strequal(argv[i], "builtin"))
2247 builtin = true;
2250 /* Get sam policy handle */
2252 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2253 pipe_hnd->desthost,
2254 MAXIMUM_ALLOWED_ACCESS,
2255 &connect_pol);
2256 if (!NT_STATUS_IS_OK(result)) {
2257 goto done;
2260 /* Get domain policy handle */
2262 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2263 &connect_pol,
2264 MAXIMUM_ALLOWED_ACCESS,
2265 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2266 &domain_pol);
2267 if (!NT_STATUS_IS_OK(result)) {
2268 goto done;
2271 /* Query domain groups */
2272 if (c->opt_long_list_entries)
2273 d_printf(_("\nGroup name Comment"
2274 "\n-----------------------------\n"));
2275 do {
2276 uint32_t max_size, total_size, returned_size;
2277 union samr_DispInfo info;
2279 if (!global) break;
2281 get_query_dispinfo_params(
2282 loop_count, &max_entries, &max_size);
2284 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2285 &domain_pol,
2287 start_idx,
2288 max_entries,
2289 max_size,
2290 &total_size,
2291 &returned_size,
2292 &info);
2293 num_entries = info.info3.count;
2294 start_idx += info.info3.count;
2296 if (!NT_STATUS_IS_OK(result) &&
2297 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2298 break;
2300 for (i = 0; i < num_entries; i++) {
2302 const char *group = NULL;
2303 const char *desc = NULL;
2305 group = info.info3.entries[i].account_name.string;
2306 desc = info.info3.entries[i].description.string;
2308 if (c->opt_long_list_entries)
2309 printf("%-21.21s %-50.50s\n",
2310 group, desc);
2311 else
2312 printf("%s\n", group);
2314 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2315 /* query domain aliases */
2316 start_idx = 0;
2317 do {
2318 if (!local) break;
2320 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2321 &domain_pol,
2322 &start_idx,
2323 &groups,
2324 0xffff,
2325 &num_entries);
2326 if (!NT_STATUS_IS_OK(result) &&
2327 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2328 break;
2330 for (i = 0; i < num_entries; i++) {
2332 const char *description = NULL;
2334 if (c->opt_long_list_entries) {
2336 struct policy_handle alias_pol;
2337 union samr_AliasInfo *info = NULL;
2339 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2340 &domain_pol,
2341 0x8,
2342 groups->entries[i].idx,
2343 &alias_pol))) &&
2344 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2345 &alias_pol,
2347 &info))) &&
2348 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2349 &alias_pol)))) {
2350 description = info->description.string;
2354 if (description != NULL) {
2355 printf("%-21.21s %-50.50s\n",
2356 groups->entries[i].name.string,
2357 description);
2358 } else {
2359 printf("%s\n", groups->entries[i].name.string);
2362 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2363 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2364 /* Get builtin policy handle */
2366 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2367 &connect_pol,
2368 MAXIMUM_ALLOWED_ACCESS,
2369 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2370 &domain_pol);
2371 if (!NT_STATUS_IS_OK(result)) {
2372 goto done;
2374 /* query builtin aliases */
2375 start_idx = 0;
2376 do {
2377 if (!builtin) break;
2379 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2380 &domain_pol,
2381 &start_idx,
2382 &groups,
2383 max_entries,
2384 &num_entries);
2385 if (!NT_STATUS_IS_OK(result) &&
2386 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2387 break;
2389 for (i = 0; i < num_entries; i++) {
2391 const char *description = NULL;
2393 if (c->opt_long_list_entries) {
2395 struct policy_handle alias_pol;
2396 union samr_AliasInfo *info = NULL;
2398 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2399 &domain_pol,
2400 0x8,
2401 groups->entries[i].idx,
2402 &alias_pol))) &&
2403 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2404 &alias_pol,
2406 &info))) &&
2407 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2408 &alias_pol)))) {
2409 description = info->description.string;
2413 if (description != NULL) {
2414 printf("%-21.21s %-50.50s\n",
2415 groups->entries[i].name.string,
2416 description);
2417 } else {
2418 printf("%s\n", groups->entries[i].name.string);
2421 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2423 done:
2424 return result;
2427 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2429 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2430 rpc_group_list_internals,
2431 argc, argv);
2434 static NTSTATUS rpc_list_group_members(struct net_context *c,
2435 struct rpc_pipe_client *pipe_hnd,
2436 TALLOC_CTX *mem_ctx,
2437 const char *domain_name,
2438 const DOM_SID *domain_sid,
2439 struct policy_handle *domain_pol,
2440 uint32 rid)
2442 NTSTATUS result;
2443 struct policy_handle group_pol;
2444 uint32 num_members, *group_rids;
2445 int i;
2446 struct samr_RidTypeArray *rids = NULL;
2447 struct lsa_Strings names;
2448 struct samr_Ids types;
2450 fstring sid_str;
2451 sid_to_fstring(sid_str, domain_sid);
2453 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2454 domain_pol,
2455 MAXIMUM_ALLOWED_ACCESS,
2456 rid,
2457 &group_pol);
2459 if (!NT_STATUS_IS_OK(result))
2460 return result;
2462 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2463 &group_pol,
2464 &rids);
2466 if (!NT_STATUS_IS_OK(result))
2467 return result;
2469 num_members = rids->count;
2470 group_rids = rids->rids;
2472 while (num_members > 0) {
2473 int this_time = 512;
2475 if (num_members < this_time)
2476 this_time = num_members;
2478 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2479 domain_pol,
2480 this_time,
2481 group_rids,
2482 &names,
2483 &types);
2485 if (!NT_STATUS_IS_OK(result))
2486 return result;
2488 /* We only have users as members, but make the output
2489 the same as the output of alias members */
2491 for (i = 0; i < this_time; i++) {
2493 if (c->opt_long_list_entries) {
2494 printf("%s-%d %s\\%s %d\n", sid_str,
2495 group_rids[i], domain_name,
2496 names.names[i].string,
2497 SID_NAME_USER);
2498 } else {
2499 printf("%s\\%s\n", domain_name,
2500 names.names[i].string);
2504 num_members -= this_time;
2505 group_rids += 512;
2508 return NT_STATUS_OK;
2511 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2512 struct rpc_pipe_client *pipe_hnd,
2513 TALLOC_CTX *mem_ctx,
2514 struct policy_handle *domain_pol,
2515 uint32 rid)
2517 NTSTATUS result;
2518 struct rpc_pipe_client *lsa_pipe;
2519 struct policy_handle alias_pol, lsa_pol;
2520 uint32 num_members;
2521 DOM_SID *alias_sids;
2522 char **domains;
2523 char **names;
2524 enum lsa_SidType *types;
2525 int i;
2526 struct lsa_SidArray sid_array;
2528 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2529 domain_pol,
2530 MAXIMUM_ALLOWED_ACCESS,
2531 rid,
2532 &alias_pol);
2534 if (!NT_STATUS_IS_OK(result))
2535 return result;
2537 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2538 &alias_pol,
2539 &sid_array);
2541 if (!NT_STATUS_IS_OK(result)) {
2542 d_fprintf(stderr, _("Couldn't list alias members\n"));
2543 return result;
2546 num_members = sid_array.num_sids;
2548 if (num_members == 0) {
2549 return NT_STATUS_OK;
2552 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2553 &ndr_table_lsarpc.syntax_id,
2554 &lsa_pipe);
2555 if (!NT_STATUS_IS_OK(result)) {
2556 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2557 nt_errstr(result) );
2558 return result;
2561 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2562 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2564 if (!NT_STATUS_IS_OK(result)) {
2565 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2566 TALLOC_FREE(lsa_pipe);
2567 return result;
2570 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2571 if (!alias_sids) {
2572 d_fprintf(stderr, _("Out of memory\n"));
2573 TALLOC_FREE(lsa_pipe);
2574 return NT_STATUS_NO_MEMORY;
2577 for (i=0; i<num_members; i++) {
2578 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2581 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2582 num_members, alias_sids,
2583 &domains, &names, &types);
2585 if (!NT_STATUS_IS_OK(result) &&
2586 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2587 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2588 TALLOC_FREE(lsa_pipe);
2589 return result;
2592 for (i = 0; i < num_members; i++) {
2593 fstring sid_str;
2594 sid_to_fstring(sid_str, &alias_sids[i]);
2596 if (c->opt_long_list_entries) {
2597 printf("%s %s\\%s %d\n", sid_str,
2598 domains[i] ? domains[i] : _("*unknown*"),
2599 names[i] ? names[i] : _("*unknown*"), types[i]);
2600 } else {
2601 if (domains[i])
2602 printf("%s\\%s\n", domains[i], names[i]);
2603 else
2604 printf("%s\n", sid_str);
2608 TALLOC_FREE(lsa_pipe);
2609 return NT_STATUS_OK;
2612 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2613 const DOM_SID *domain_sid,
2614 const char *domain_name,
2615 struct cli_state *cli,
2616 struct rpc_pipe_client *pipe_hnd,
2617 TALLOC_CTX *mem_ctx,
2618 int argc,
2619 const char **argv)
2621 NTSTATUS result;
2622 struct policy_handle connect_pol, domain_pol;
2623 struct samr_Ids rids, rid_types;
2624 struct lsa_String lsa_acct_name;
2626 /* Get sam policy handle */
2628 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2629 pipe_hnd->desthost,
2630 MAXIMUM_ALLOWED_ACCESS,
2631 &connect_pol);
2633 if (!NT_STATUS_IS_OK(result))
2634 return result;
2636 /* Get domain policy handle */
2638 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2639 &connect_pol,
2640 MAXIMUM_ALLOWED_ACCESS,
2641 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2642 &domain_pol);
2644 if (!NT_STATUS_IS_OK(result))
2645 return result;
2647 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2649 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2650 &domain_pol,
2652 &lsa_acct_name,
2653 &rids,
2654 &rid_types);
2656 if (!NT_STATUS_IS_OK(result)) {
2658 /* Ok, did not find it in the global sam, try with builtin */
2660 DOM_SID sid_Builtin;
2662 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2664 sid_copy(&sid_Builtin, &global_sid_Builtin);
2666 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2667 &connect_pol,
2668 MAXIMUM_ALLOWED_ACCESS,
2669 &sid_Builtin,
2670 &domain_pol);
2672 if (!NT_STATUS_IS_OK(result)) {
2673 d_fprintf(stderr, _("Couldn't find group %s\n"),
2674 argv[0]);
2675 return result;
2678 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2679 &domain_pol,
2681 &lsa_acct_name,
2682 &rids,
2683 &rid_types);
2685 if (!NT_STATUS_IS_OK(result)) {
2686 d_fprintf(stderr, _("Couldn't find group %s\n"),
2687 argv[0]);
2688 return result;
2692 if (rids.count != 1) {
2693 d_fprintf(stderr, _("Couldn't find group %s\n"),
2694 argv[0]);
2695 return result;
2698 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2699 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2700 domain_sid, &domain_pol,
2701 rids.ids[0]);
2704 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2705 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2706 rids.ids[0]);
2709 return NT_STATUS_NO_SUCH_GROUP;
2712 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2714 if (argc != 1 || c->display_usage) {
2715 return rpc_group_usage(c, argc, argv);
2718 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2719 rpc_group_members_internals,
2720 argc, argv);
2723 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2725 NET_API_STATUS status;
2726 struct GROUP_INFO_0 g0;
2727 uint32_t parm_err;
2729 if (argc != 2) {
2730 d_printf(_("Usage:\n"));
2731 d_printf("net rpc group rename group newname\n");
2732 return -1;
2735 g0.grpi0_name = argv[1];
2737 status = NetGroupSetInfo(c->opt_host,
2738 argv[0],
2740 (uint8_t *)&g0,
2741 &parm_err);
2743 if (status != 0) {
2744 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
2745 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2746 status));
2747 return -1;
2750 return 0;
2753 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2755 if (argc != 2 || c->display_usage) {
2756 return rpc_group_usage(c, argc, argv);
2759 return rpc_group_rename_internals(c, argc, argv);
2763 * 'net rpc group' entrypoint.
2764 * @param argc Standard main() style argc.
2765 * @param argv Standard main() style argv. Initial components are already
2766 * stripped.
2769 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2771 NET_API_STATUS status;
2773 struct functable func[] = {
2775 "add",
2776 rpc_group_add,
2777 NET_TRANSPORT_RPC,
2778 N_("Create specified group"),
2779 N_("net rpc group add\n"
2780 " Create specified group")
2783 "delete",
2784 rpc_group_delete,
2785 NET_TRANSPORT_RPC,
2786 N_("Delete specified group"),
2787 N_("net rpc group delete\n"
2788 " Delete specified group")
2791 "addmem",
2792 rpc_group_addmem,
2793 NET_TRANSPORT_RPC,
2794 N_("Add member to group"),
2795 N_("net rpc group addmem\n"
2796 " Add member to group")
2799 "delmem",
2800 rpc_group_delmem,
2801 NET_TRANSPORT_RPC,
2802 N_("Remove member from group"),
2803 N_("net rpc group delmem\n"
2804 " Remove member from group")
2807 "list",
2808 rpc_group_list,
2809 NET_TRANSPORT_RPC,
2810 N_("List groups"),
2811 N_("net rpc group list\n"
2812 " List groups")
2815 "members",
2816 rpc_group_members,
2817 NET_TRANSPORT_RPC,
2818 N_("List group members"),
2819 N_("net rpc group members\n"
2820 " List group members")
2823 "rename",
2824 rpc_group_rename,
2825 NET_TRANSPORT_RPC,
2826 N_("Rename group"),
2827 N_("net rpc group rename\n"
2828 " Rename group")
2830 {NULL, NULL, 0, NULL, NULL}
2833 status = libnetapi_init(&c->netapi_ctx);
2834 if (status != 0) {
2835 return -1;
2837 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2838 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2839 if (c->opt_kerberos) {
2840 libnetapi_set_use_kerberos(c->netapi_ctx);
2843 if (argc == 0) {
2844 if (c->display_usage) {
2845 d_printf(_("Usage:\n"));
2846 d_printf(_("net rpc group\n"
2847 " Alias for net rpc group list global "
2848 "local builtin\n"));
2849 net_display_usage_from_functable(func);
2850 return 0;
2853 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2854 rpc_group_list_internals,
2855 argc, argv);
2858 return net_run_function(c, argc, argv, "net rpc group", func);
2861 /****************************************************************************/
2863 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2865 return net_share_usage(c, argc, argv);
2869 * Add a share on a remote RPC server.
2871 * @param argc Standard main() style argc.
2872 * @param argv Standard main() style argv. Initial components are already
2873 * stripped.
2875 * @return A shell status integer (0 for success).
2878 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2880 NET_API_STATUS status;
2881 char *sharename;
2882 char *path;
2883 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2884 uint32 num_users=0, perms=0;
2885 char *password=NULL; /* don't allow a share password */
2886 struct SHARE_INFO_2 i2;
2887 uint32_t parm_error = 0;
2889 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2890 return rpc_share_usage(c, argc, argv);
2893 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2894 return -1;
2897 path = strchr(sharename, '=');
2898 if (!path) {
2899 return -1;
2902 *path++ = '\0';
2904 i2.shi2_netname = sharename;
2905 i2.shi2_type = type;
2906 i2.shi2_remark = c->opt_comment;
2907 i2.shi2_permissions = perms;
2908 i2.shi2_max_uses = c->opt_maxusers;
2909 i2.shi2_current_uses = num_users;
2910 i2.shi2_path = path;
2911 i2.shi2_passwd = password;
2913 status = NetShareAdd(c->opt_host,
2915 (uint8_t *)&i2,
2916 &parm_error);
2917 if (status != 0) {
2918 printf(_("NetShareAdd failed with: %s\n"),
2919 libnetapi_get_error_string(c->netapi_ctx, status));
2922 return status;
2926 * Delete a share on a remote RPC server.
2928 * @param domain_sid The domain sid acquired from the remote server.
2929 * @param argc Standard main() style argc.
2930 * @param argv Standard main() style argv. Initial components are already
2931 * stripped.
2933 * @return A shell status integer (0 for success).
2935 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
2937 if (argc < 1 || c->display_usage) {
2938 return rpc_share_usage(c, argc, argv);
2941 return NetShareDel(c->opt_host, argv[0], 0);
2945 * Formatted print of share info
2947 * @param r pointer to SHARE_INFO_1 to format
2950 static void display_share_info_1(struct net_context *c,
2951 struct SHARE_INFO_1 *r)
2953 if (c->opt_long_list_entries) {
2954 d_printf("%-12s %-8.8s %-50s\n",
2955 r->shi1_netname,
2956 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
2957 r->shi1_remark);
2958 } else {
2959 d_printf("%s\n", r->shi1_netname);
2963 static WERROR get_share_info(struct net_context *c,
2964 struct rpc_pipe_client *pipe_hnd,
2965 TALLOC_CTX *mem_ctx,
2966 uint32 level,
2967 int argc,
2968 const char **argv,
2969 struct srvsvc_NetShareInfoCtr *info_ctr)
2971 WERROR result;
2972 NTSTATUS status;
2973 union srvsvc_NetShareInfo info;
2975 /* no specific share requested, enumerate all */
2976 if (argc == 0) {
2978 uint32_t preferred_len = 0xffffffff;
2979 uint32_t total_entries = 0;
2980 uint32_t resume_handle = 0;
2982 info_ctr->level = level;
2984 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
2985 pipe_hnd->desthost,
2986 info_ctr,
2987 preferred_len,
2988 &total_entries,
2989 &resume_handle,
2990 &result);
2991 return result;
2994 /* request just one share */
2995 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
2996 pipe_hnd->desthost,
2997 argv[0],
2998 level,
2999 &info,
3000 &result);
3002 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
3003 goto done;
3006 /* construct ctr */
3007 ZERO_STRUCTP(info_ctr);
3009 info_ctr->level = level;
3011 switch (level) {
3012 case 1:
3014 struct srvsvc_NetShareCtr1 *ctr1;
3016 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3017 W_ERROR_HAVE_NO_MEMORY(ctr1);
3019 ctr1->count = 1;
3020 ctr1->array = info.info1;
3022 info_ctr->ctr.ctr1 = ctr1;
3024 case 2:
3026 struct srvsvc_NetShareCtr2 *ctr2;
3028 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3029 W_ERROR_HAVE_NO_MEMORY(ctr2);
3031 ctr2->count = 1;
3032 ctr2->array = info.info2;
3034 info_ctr->ctr.ctr2 = ctr2;
3036 case 502:
3038 struct srvsvc_NetShareCtr502 *ctr502;
3040 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3041 W_ERROR_HAVE_NO_MEMORY(ctr502);
3043 ctr502->count = 1;
3044 ctr502->array = info.info502;
3046 info_ctr->ctr.ctr502 = ctr502;
3048 } /* switch */
3049 done:
3050 return result;
3053 /***
3054 * 'net rpc share list' entrypoint.
3055 * @param argc Standard main() style argc.
3056 * @param argv Standard main() style argv. Initial components are already
3057 * stripped.
3059 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3061 NET_API_STATUS status;
3062 struct SHARE_INFO_1 *i1 = NULL;
3063 uint32_t entries_read = 0;
3064 uint32_t total_entries = 0;
3065 uint32_t resume_handle = 0;
3066 uint32_t i, level = 1;
3068 if (c->display_usage) {
3069 d_printf( "%s\n"
3070 "net rpc share list\n"
3071 " %s\n",
3072 _("Usage:"),
3073 _("List shares on remote server"));
3074 return 0;
3077 status = NetShareEnum(c->opt_host,
3078 level,
3079 (uint8_t **)(void *)&i1,
3080 (uint32_t)-1,
3081 &entries_read,
3082 &total_entries,
3083 &resume_handle);
3084 if (status != 0) {
3085 goto done;
3088 /* Display results */
3090 if (c->opt_long_list_entries) {
3091 d_printf(_(
3092 "\nEnumerating shared resources (exports) on remote server:\n\n"
3093 "\nShare name Type Description\n"
3094 "---------- ---- -----------\n"));
3096 for (i = 0; i < entries_read; i++)
3097 display_share_info_1(c, &i1[i]);
3098 done:
3099 return status;
3102 static bool check_share_availability(struct cli_state *cli, const char *netname)
3104 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
3105 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3106 return false;
3109 if (!cli_tdis(cli))
3110 return false;
3112 return true;
3115 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3116 const char *netname, uint32 type)
3118 /* only support disk shares */
3119 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3120 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3121 type);
3122 return false;
3125 /* skip builtin shares */
3126 /* FIXME: should print$ be added too ? */
3127 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3128 strequal(netname,"global"))
3129 return false;
3131 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3132 printf(_("excluding [%s]\n"), netname);
3133 return false;
3136 return check_share_availability(cli, netname);
3140 * Migrate shares from a remote RPC server to the local RPC server.
3142 * All parameters are provided by the run_rpc_command function, except for
3143 * argc, argv which are passed through.
3145 * @param domain_sid The domain sid acquired from the remote server.
3146 * @param cli A cli_state connected to the server.
3147 * @param mem_ctx Talloc context, destroyed on completion of the function.
3148 * @param argc Standard main() style argc.
3149 * @param argv Standard main() style argv. Initial components are already
3150 * stripped.
3152 * @return Normal NTSTATUS return.
3155 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3156 const DOM_SID *domain_sid,
3157 const char *domain_name,
3158 struct cli_state *cli,
3159 struct rpc_pipe_client *pipe_hnd,
3160 TALLOC_CTX *mem_ctx,
3161 int argc,
3162 const char **argv)
3164 WERROR result;
3165 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3166 struct srvsvc_NetShareInfoCtr ctr_src;
3167 uint32 i;
3168 struct rpc_pipe_client *srvsvc_pipe = NULL;
3169 struct cli_state *cli_dst = NULL;
3170 uint32 level = 502; /* includes secdesc */
3171 uint32_t parm_error = 0;
3173 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3174 &ctr_src);
3175 if (!W_ERROR_IS_OK(result))
3176 goto done;
3178 /* connect destination PI_SRVSVC */
3179 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3180 &ndr_table_srvsvc.syntax_id);
3181 if (!NT_STATUS_IS_OK(nt_status))
3182 return nt_status;
3185 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3187 union srvsvc_NetShareInfo info;
3188 struct srvsvc_NetShareInfo502 info502 =
3189 ctr_src.ctr.ctr502->array[i];
3191 /* reset error-code */
3192 nt_status = NT_STATUS_UNSUCCESSFUL;
3194 if (!check_share_sanity(c, cli, info502.name, info502.type))
3195 continue;
3197 /* finally add the share on the dst server */
3199 printf(_("migrating: [%s], path: %s, comment: %s, without "
3200 "share-ACLs\n"),
3201 info502.name, info502.path, info502.comment);
3203 info.info502 = &info502;
3205 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3206 srvsvc_pipe->desthost,
3207 502,
3208 &info,
3209 &parm_error,
3210 &result);
3212 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3213 printf(_(" [%s] does already exist\n"),
3214 info502.name);
3215 continue;
3218 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3219 printf(_("cannot add share: %s\n"), win_errstr(result));
3220 goto done;
3225 nt_status = NT_STATUS_OK;
3227 done:
3228 if (cli_dst) {
3229 cli_shutdown(cli_dst);
3232 return nt_status;
3237 * Migrate shares from a RPC server to another.
3239 * @param argc Standard main() style argc.
3240 * @param argv Standard main() style argv. Initial components are already
3241 * stripped.
3243 * @return A shell status integer (0 for success).
3245 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3246 const char **argv)
3248 if (c->display_usage) {
3249 d_printf( "%s\n"
3250 "net rpc share migrate shares\n"
3251 " %s\n",
3252 _("Usage:"),
3253 _("Migrate shares to local server"));
3254 return 0;
3257 if (!c->opt_host) {
3258 printf(_("no server to migrate\n"));
3259 return -1;
3262 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3263 rpc_share_migrate_shares_internals,
3264 argc, argv);
3268 * Copy a file/dir
3270 * @param f file_info
3271 * @param mask current search mask
3272 * @param state arg-pointer
3275 static void copy_fn(const char *mnt, file_info *f,
3276 const char *mask, void *state)
3278 static NTSTATUS nt_status;
3279 static struct copy_clistate *local_state;
3280 static fstring filename, new_mask;
3281 fstring dir;
3282 char *old_dir;
3283 struct net_context *c;
3285 local_state = (struct copy_clistate *)state;
3286 nt_status = NT_STATUS_UNSUCCESSFUL;
3288 c = local_state->c;
3290 if (strequal(f->name, ".") || strequal(f->name, ".."))
3291 return;
3293 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3295 /* DIRECTORY */
3296 if (f->mode & aDIR) {
3298 DEBUG(3,("got dir: %s\n", f->name));
3300 fstrcpy(dir, local_state->cwd);
3301 fstrcat(dir, "\\");
3302 fstrcat(dir, f->name);
3304 switch (net_mode_share)
3306 case NET_MODE_SHARE_MIGRATE:
3307 /* create that directory */
3308 nt_status = net_copy_file(c, local_state->mem_ctx,
3309 local_state->cli_share_src,
3310 local_state->cli_share_dst,
3311 dir, dir,
3312 c->opt_acls? true : false,
3313 c->opt_attrs? true : false,
3314 c->opt_timestamps? true:false,
3315 false);
3316 break;
3317 default:
3318 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3319 return;
3322 if (!NT_STATUS_IS_OK(nt_status))
3323 printf(_("could not handle dir %s: %s\n"),
3324 dir, nt_errstr(nt_status));
3326 /* search below that directory */
3327 fstrcpy(new_mask, dir);
3328 fstrcat(new_mask, "\\*");
3330 old_dir = local_state->cwd;
3331 local_state->cwd = dir;
3332 if (!sync_files(local_state, new_mask))
3333 printf(_("could not handle files\n"));
3334 local_state->cwd = old_dir;
3336 return;
3340 /* FILE */
3341 fstrcpy(filename, local_state->cwd);
3342 fstrcat(filename, "\\");
3343 fstrcat(filename, f->name);
3345 DEBUG(3,("got file: %s\n", filename));
3347 switch (net_mode_share)
3349 case NET_MODE_SHARE_MIGRATE:
3350 nt_status = net_copy_file(c, local_state->mem_ctx,
3351 local_state->cli_share_src,
3352 local_state->cli_share_dst,
3353 filename, filename,
3354 c->opt_acls? true : false,
3355 c->opt_attrs? true : false,
3356 c->opt_timestamps? true: false,
3357 true);
3358 break;
3359 default:
3360 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3361 net_mode_share);
3362 return;
3365 if (!NT_STATUS_IS_OK(nt_status))
3366 printf(_("could not handle file %s: %s\n"),
3367 filename, nt_errstr(nt_status));
3372 * sync files, can be called recursivly to list files
3373 * and then call copy_fn for each file
3375 * @param cp_clistate pointer to the copy_clistate we work with
3376 * @param mask the current search mask
3378 * @return Boolean result
3380 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3382 struct cli_state *targetcli;
3383 char *targetpath = NULL;
3385 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3387 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3388 mask, &targetcli, &targetpath ) ) {
3389 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3390 "%s\n"),
3391 mask, cli_errstr(cp_clistate->cli_share_src));
3392 return false;
3395 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3396 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3397 mask, cli_errstr(targetcli));
3398 return false;
3401 return true;
3406 * Set the top level directory permissions before we do any further copies.
3407 * Should set up ACL inheritance.
3410 bool copy_top_level_perms(struct net_context *c,
3411 struct copy_clistate *cp_clistate,
3412 const char *sharename)
3414 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3416 switch (net_mode_share) {
3417 case NET_MODE_SHARE_MIGRATE:
3418 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3419 nt_status = net_copy_fileattr(c,
3420 cp_clistate->mem_ctx,
3421 cp_clistate->cli_share_src,
3422 cp_clistate->cli_share_dst,
3423 "\\", "\\",
3424 c->opt_acls? true : false,
3425 c->opt_attrs? true : false,
3426 c->opt_timestamps? true: false,
3427 false);
3428 break;
3429 default:
3430 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3431 break;
3434 if (!NT_STATUS_IS_OK(nt_status)) {
3435 printf(_("Could handle directory attributes for top level "
3436 "directory of share %s. Error %s\n"),
3437 sharename, nt_errstr(nt_status));
3438 return false;
3441 return true;
3445 * Sync all files inside a remote share to another share (over smb).
3447 * All parameters are provided by the run_rpc_command function, except for
3448 * argc, argv which are passed through.
3450 * @param domain_sid The domain sid acquired from the remote server.
3451 * @param cli A cli_state connected to the server.
3452 * @param mem_ctx Talloc context, destroyed on completion of the function.
3453 * @param argc Standard main() style argc.
3454 * @param argv Standard main() style argv. Initial components are already
3455 * stripped.
3457 * @return Normal NTSTATUS return.
3460 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3461 const DOM_SID *domain_sid,
3462 const char *domain_name,
3463 struct cli_state *cli,
3464 struct rpc_pipe_client *pipe_hnd,
3465 TALLOC_CTX *mem_ctx,
3466 int argc,
3467 const char **argv)
3469 WERROR result;
3470 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3471 struct srvsvc_NetShareInfoCtr ctr_src;
3472 uint32 i;
3473 uint32 level = 502;
3474 struct copy_clistate cp_clistate;
3475 bool got_src_share = false;
3476 bool got_dst_share = false;
3477 const char *mask = "\\*";
3478 char *dst = NULL;
3480 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3481 if (dst == NULL) {
3482 nt_status = NT_STATUS_NO_MEMORY;
3483 goto done;
3486 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3487 &ctr_src);
3489 if (!W_ERROR_IS_OK(result))
3490 goto done;
3492 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3494 struct srvsvc_NetShareInfo502 info502 =
3495 ctr_src.ctr.ctr502->array[i];
3497 if (!check_share_sanity(c, cli, info502.name, info502.type))
3498 continue;
3500 /* one might not want to mirror whole discs :) */
3501 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3502 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3503 info502.name);
3504 continue;
3507 switch (net_mode_share)
3509 case NET_MODE_SHARE_MIGRATE:
3510 printf("syncing");
3511 break;
3512 default:
3513 d_fprintf(stderr, _("Unsupported mode %d\n"),
3514 net_mode_share);
3515 break;
3517 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3518 "Attributes %s\n"),
3519 info502.name,
3520 c->opt_acls ? _("including") : _("without"),
3521 c->opt_attrs ? _("including") : _("without"),
3522 c->opt_timestamps ? _("(preserving timestamps)") : "");
3524 cp_clistate.mem_ctx = mem_ctx;
3525 cp_clistate.cli_share_src = NULL;
3526 cp_clistate.cli_share_dst = NULL;
3527 cp_clistate.cwd = NULL;
3528 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3529 cp_clistate.c = c;
3531 /* open share source */
3532 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3533 &cli->dest_ss, cli->desthost,
3534 info502.name, "A:");
3535 if (!NT_STATUS_IS_OK(nt_status))
3536 goto done;
3538 got_src_share = true;
3540 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3541 /* open share destination */
3542 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3543 NULL, dst, info502.name, "A:");
3544 if (!NT_STATUS_IS_OK(nt_status))
3545 goto done;
3547 got_dst_share = true;
3550 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3551 d_fprintf(stderr, _("Could not handle the top level "
3552 "directory permissions for the "
3553 "share: %s\n"), info502.name);
3554 nt_status = NT_STATUS_UNSUCCESSFUL;
3555 goto done;
3558 if (!sync_files(&cp_clistate, mask)) {
3559 d_fprintf(stderr, _("could not handle files for share: "
3560 "%s\n"), info502.name);
3561 nt_status = NT_STATUS_UNSUCCESSFUL;
3562 goto done;
3566 nt_status = NT_STATUS_OK;
3568 done:
3570 if (got_src_share)
3571 cli_shutdown(cp_clistate.cli_share_src);
3573 if (got_dst_share)
3574 cli_shutdown(cp_clistate.cli_share_dst);
3576 SAFE_FREE(dst);
3577 return nt_status;
3581 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3583 if (c->display_usage) {
3584 d_printf( "%s\n"
3585 "net share migrate files\n"
3586 " %s\n",
3587 _("Usage:"),
3588 _("Migrate files to local server"));
3589 return 0;
3592 if (!c->opt_host) {
3593 d_printf(_("no server to migrate\n"));
3594 return -1;
3597 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3598 rpc_share_migrate_files_internals,
3599 argc, argv);
3603 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3605 * All parameters are provided by the run_rpc_command function, except for
3606 * argc, argv which are passed through.
3608 * @param domain_sid The domain sid acquired from the remote server.
3609 * @param cli A cli_state connected to the server.
3610 * @param mem_ctx Talloc context, destroyed on completion of the function.
3611 * @param argc Standard main() style argc.
3612 * @param argv Standard main() style argv. Initial components are already
3613 * stripped.
3615 * @return Normal NTSTATUS return.
3618 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3619 const DOM_SID *domain_sid,
3620 const char *domain_name,
3621 struct cli_state *cli,
3622 struct rpc_pipe_client *pipe_hnd,
3623 TALLOC_CTX *mem_ctx,
3624 int argc,
3625 const char **argv)
3627 WERROR result;
3628 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3629 struct srvsvc_NetShareInfoCtr ctr_src;
3630 union srvsvc_NetShareInfo info;
3631 uint32 i;
3632 struct rpc_pipe_client *srvsvc_pipe = NULL;
3633 struct cli_state *cli_dst = NULL;
3634 uint32 level = 502; /* includes secdesc */
3635 uint32_t parm_error = 0;
3637 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3638 &ctr_src);
3640 if (!W_ERROR_IS_OK(result))
3641 goto done;
3643 /* connect destination PI_SRVSVC */
3644 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3645 &ndr_table_srvsvc.syntax_id);
3646 if (!NT_STATUS_IS_OK(nt_status))
3647 return nt_status;
3650 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3652 struct srvsvc_NetShareInfo502 info502 =
3653 ctr_src.ctr.ctr502->array[i];
3655 /* reset error-code */
3656 nt_status = NT_STATUS_UNSUCCESSFUL;
3658 if (!check_share_sanity(c, cli, info502.name, info502.type))
3659 continue;
3661 printf(_("migrating: [%s], path: %s, comment: %s, including "
3662 "share-ACLs\n"),
3663 info502.name, info502.path, info502.comment);
3665 if (c->opt_verbose)
3666 display_sec_desc(info502.sd_buf.sd);
3668 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3669 info.info502 = &info502;
3671 /* finally modify the share on the dst server */
3672 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3673 srvsvc_pipe->desthost,
3674 info502.name,
3675 level,
3676 &info,
3677 &parm_error,
3678 &result);
3679 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3680 printf(_("cannot set share-acl: %s\n"),
3681 win_errstr(result));
3682 goto done;
3687 nt_status = NT_STATUS_OK;
3689 done:
3690 if (cli_dst) {
3691 cli_shutdown(cli_dst);
3694 return nt_status;
3699 * Migrate share-acls from a RPC server to another.
3701 * @param argc Standard main() style argc.
3702 * @param argv Standard main() style argv. Initial components are already
3703 * stripped.
3705 * @return A shell status integer (0 for success).
3707 static int rpc_share_migrate_security(struct net_context *c, int argc,
3708 const char **argv)
3710 if (c->display_usage) {
3711 d_printf( "%s\n"
3712 "net rpc share migrate security\n"
3713 " %s\n",
3714 _("Usage:"),
3715 _("Migrate share-acls to local server"));
3716 return 0;
3719 if (!c->opt_host) {
3720 d_printf(_("no server to migrate\n"));
3721 return -1;
3724 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3725 rpc_share_migrate_security_internals,
3726 argc, argv);
3730 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3731 * from one server to another.
3733 * @param argc Standard main() style argc.
3734 * @param argv Standard main() style argv. Initial components are already
3735 * stripped.
3737 * @return A shell status integer (0 for success).
3740 static int rpc_share_migrate_all(struct net_context *c, int argc,
3741 const char **argv)
3743 int ret;
3745 if (c->display_usage) {
3746 d_printf( "%s\n"
3747 "net rpc share migrate all\n"
3748 " %s\n",
3749 _("Usage:"),
3750 _("Migrates shares including all share settings"));
3751 return 0;
3754 if (!c->opt_host) {
3755 d_printf(_("no server to migrate\n"));
3756 return -1;
3759 /* order is important. we don't want to be locked out by the share-acl
3760 * before copying files - gd */
3762 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3763 rpc_share_migrate_shares_internals, argc, argv);
3764 if (ret)
3765 return ret;
3767 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3768 rpc_share_migrate_files_internals, argc, argv);
3769 if (ret)
3770 return ret;
3772 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3773 rpc_share_migrate_security_internals, argc,
3774 argv);
3779 * 'net rpc share migrate' entrypoint.
3780 * @param argc Standard main() style argc.
3781 * @param argv Standard main() style argv. Initial components are already
3782 * stripped.
3784 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3787 struct functable func[] = {
3789 "all",
3790 rpc_share_migrate_all,
3791 NET_TRANSPORT_RPC,
3792 N_("Migrate shares from remote to local server"),
3793 N_("net rpc share migrate all\n"
3794 " Migrate shares from remote to local server")
3797 "files",
3798 rpc_share_migrate_files,
3799 NET_TRANSPORT_RPC,
3800 N_("Migrate files from remote to local server"),
3801 N_("net rpc share migrate files\n"
3802 " Migrate files from remote to local server")
3805 "security",
3806 rpc_share_migrate_security,
3807 NET_TRANSPORT_RPC,
3808 N_("Migrate share-ACLs from remote to local server"),
3809 N_("net rpc share migrate security\n"
3810 " Migrate share-ACLs from remote to local server")
3813 "shares",
3814 rpc_share_migrate_shares,
3815 NET_TRANSPORT_RPC,
3816 N_("Migrate shares from remote to local server"),
3817 N_("net rpc share migrate shares\n"
3818 " Migrate shares from remote to local server")
3820 {NULL, NULL, 0, NULL, NULL}
3823 net_mode_share = NET_MODE_SHARE_MIGRATE;
3825 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3828 struct full_alias {
3829 DOM_SID sid;
3830 uint32 num_members;
3831 DOM_SID *members;
3834 static int num_server_aliases;
3835 static struct full_alias *server_aliases;
3838 * Add an alias to the static list.
3840 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3842 if (server_aliases == NULL)
3843 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3845 server_aliases[num_server_aliases] = *alias;
3846 num_server_aliases += 1;
3850 * For a specific domain on the server, fetch all the aliases
3851 * and their members. Add all of them to the server_aliases.
3854 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3855 TALLOC_CTX *mem_ctx,
3856 struct policy_handle *connect_pol,
3857 const DOM_SID *domain_sid)
3859 uint32 start_idx, max_entries, num_entries, i;
3860 struct samr_SamArray *groups = NULL;
3861 NTSTATUS result;
3862 struct policy_handle domain_pol;
3864 /* Get domain policy handle */
3866 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3867 connect_pol,
3868 MAXIMUM_ALLOWED_ACCESS,
3869 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3870 &domain_pol);
3871 if (!NT_STATUS_IS_OK(result))
3872 return result;
3874 start_idx = 0;
3875 max_entries = 250;
3877 do {
3878 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3879 &domain_pol,
3880 &start_idx,
3881 &groups,
3882 max_entries,
3883 &num_entries);
3884 for (i = 0; i < num_entries; i++) {
3886 struct policy_handle alias_pol;
3887 struct full_alias alias;
3888 struct lsa_SidArray sid_array;
3889 int j;
3891 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3892 &domain_pol,
3893 MAXIMUM_ALLOWED_ACCESS,
3894 groups->entries[i].idx,
3895 &alias_pol);
3896 if (!NT_STATUS_IS_OK(result))
3897 goto done;
3899 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3900 &alias_pol,
3901 &sid_array);
3902 if (!NT_STATUS_IS_OK(result))
3903 goto done;
3905 alias.num_members = sid_array.num_sids;
3907 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3908 if (!NT_STATUS_IS_OK(result))
3909 goto done;
3911 alias.members = NULL;
3913 if (alias.num_members > 0) {
3914 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3916 for (j = 0; j < alias.num_members; j++)
3917 sid_copy(&alias.members[j],
3918 sid_array.sids[j].sid);
3921 sid_copy(&alias.sid, domain_sid);
3922 sid_append_rid(&alias.sid, groups->entries[i].idx);
3924 push_alias(mem_ctx, &alias);
3926 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3928 result = NT_STATUS_OK;
3930 done:
3931 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3933 return result;
3937 * Dump server_aliases as names for debugging purposes.
3940 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
3941 const DOM_SID *domain_sid,
3942 const char *domain_name,
3943 struct cli_state *cli,
3944 struct rpc_pipe_client *pipe_hnd,
3945 TALLOC_CTX *mem_ctx,
3946 int argc,
3947 const char **argv)
3949 int i;
3950 NTSTATUS result;
3951 struct policy_handle lsa_pol;
3953 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
3954 SEC_FLAG_MAXIMUM_ALLOWED,
3955 &lsa_pol);
3956 if (!NT_STATUS_IS_OK(result))
3957 return result;
3959 for (i=0; i<num_server_aliases; i++) {
3960 char **names;
3961 char **domains;
3962 enum lsa_SidType *types;
3963 int j;
3965 struct full_alias *alias = &server_aliases[i];
3967 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3968 &alias->sid,
3969 &domains, &names, &types);
3970 if (!NT_STATUS_IS_OK(result))
3971 continue;
3973 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3975 if (alias->num_members == 0) {
3976 DEBUG(1, ("\n"));
3977 continue;
3980 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
3981 alias->num_members,
3982 alias->members,
3983 &domains, &names, &types);
3985 if (!NT_STATUS_IS_OK(result) &&
3986 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3987 continue;
3989 for (j=0; j<alias->num_members; j++)
3990 DEBUG(1, ("%s\\%s (%d); ",
3991 domains[j] ? domains[j] : "*unknown*",
3992 names[j] ? names[j] : "*unknown*",types[j]));
3993 DEBUG(1, ("\n"));
3996 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
3998 return NT_STATUS_OK;
4002 * Fetch a list of all server aliases and their members into
4003 * server_aliases.
4006 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4007 const DOM_SID *domain_sid,
4008 const char *domain_name,
4009 struct cli_state *cli,
4010 struct rpc_pipe_client *pipe_hnd,
4011 TALLOC_CTX *mem_ctx,
4012 int argc,
4013 const char **argv)
4015 NTSTATUS result;
4016 struct policy_handle connect_pol;
4018 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4019 pipe_hnd->desthost,
4020 MAXIMUM_ALLOWED_ACCESS,
4021 &connect_pol);
4023 if (!NT_STATUS_IS_OK(result))
4024 goto done;
4026 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4027 &global_sid_Builtin);
4029 if (!NT_STATUS_IS_OK(result))
4030 goto done;
4032 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4033 domain_sid);
4035 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4036 done:
4037 return result;
4040 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4042 token->num_sids = 4;
4044 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4045 d_fprintf(stderr, "malloc %s\n",_("failed"));
4046 token->num_sids = 0;
4047 return;
4050 token->user_sids[0] = *user_sid;
4051 sid_copy(&token->user_sids[1], &global_sid_World);
4052 sid_copy(&token->user_sids[2], &global_sid_Network);
4053 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4056 static void free_user_token(NT_USER_TOKEN *token)
4058 SAFE_FREE(token->user_sids);
4061 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4063 if (is_sid_in_token(token, sid))
4064 return;
4066 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4067 if (!token->user_sids) {
4068 return;
4071 sid_copy(&token->user_sids[token->num_sids], sid);
4073 token->num_sids += 1;
4076 struct user_token {
4077 fstring name;
4078 NT_USER_TOKEN token;
4081 static void dump_user_token(struct user_token *token)
4083 int i;
4085 d_printf("%s\n", token->name);
4087 for (i=0; i<token->token.num_sids; i++) {
4088 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4092 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4094 int i;
4096 for (i=0; i<alias->num_members; i++) {
4097 if (sid_compare(sid, &alias->members[i]) == 0)
4098 return true;
4101 return false;
4104 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4106 int i;
4108 for (i=0; i<num_server_aliases; i++) {
4109 if (is_alias_member(&sid, &server_aliases[i]))
4110 add_sid_to_token(token, &server_aliases[i].sid);
4115 * We got a user token with all the SIDs we can know about without asking the
4116 * server directly. These are the user and domain group sids. All of these can
4117 * be members of aliases. So scan the list of aliases for each of the SIDs and
4118 * add them to the token.
4121 static void collect_alias_memberships(NT_USER_TOKEN *token)
4123 int num_global_sids = token->num_sids;
4124 int i;
4126 for (i=0; i<num_global_sids; i++) {
4127 collect_sid_memberships(token, token->user_sids[i]);
4131 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4133 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4134 enum wbcSidType type;
4135 fstring full_name;
4136 struct wbcDomainSid wsid;
4137 char *sid_str = NULL;
4138 DOM_SID user_sid;
4139 uint32_t num_groups;
4140 gid_t *groups = NULL;
4141 uint32_t i;
4143 fstr_sprintf(full_name, "%s%c%s",
4144 domain, *lp_winbind_separator(), user);
4146 /* First let's find out the user sid */
4148 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4150 if (!WBC_ERROR_IS_OK(wbc_status)) {
4151 DEBUG(1, ("winbind could not find %s: %s\n",
4152 full_name, wbcErrorString(wbc_status)));
4153 return false;
4156 wbc_status = wbcSidToString(&wsid, &sid_str);
4157 if (!WBC_ERROR_IS_OK(wbc_status)) {
4158 return false;
4161 if (type != SID_NAME_USER) {
4162 wbcFreeMemory(sid_str);
4163 DEBUG(1, ("%s is not a user\n", full_name));
4164 return false;
4167 if (!string_to_sid(&user_sid, sid_str)) {
4168 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4169 return false;
4172 wbcFreeMemory(sid_str);
4173 sid_str = NULL;
4175 init_user_token(token, &user_sid);
4177 /* And now the groups winbind knows about */
4179 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4180 if (!WBC_ERROR_IS_OK(wbc_status)) {
4181 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4182 full_name, wbcErrorString(wbc_status)));
4183 return false;
4186 for (i = 0; i < num_groups; i++) {
4187 gid_t gid = groups[i];
4188 DOM_SID sid;
4190 wbc_status = wbcGidToSid(gid, &wsid);
4191 if (!WBC_ERROR_IS_OK(wbc_status)) {
4192 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4193 (unsigned int)gid, wbcErrorString(wbc_status)));
4194 wbcFreeMemory(groups);
4195 return false;
4198 wbc_status = wbcSidToString(&wsid, &sid_str);
4199 if (!WBC_ERROR_IS_OK(wbc_status)) {
4200 wbcFreeMemory(groups);
4201 return false;
4204 DEBUG(3, (" %s\n", sid_str));
4206 string_to_sid(&sid, sid_str);
4207 wbcFreeMemory(sid_str);
4208 sid_str = NULL;
4210 add_sid_to_token(token, &sid);
4212 wbcFreeMemory(groups);
4214 return true;
4218 * Get a list of all user tokens we want to look at
4221 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4222 struct user_token **user_tokens)
4224 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4225 uint32_t i, num_users;
4226 const char **users;
4227 struct user_token *result;
4228 TALLOC_CTX *frame = NULL;
4230 if (lp_winbind_use_default_domain() &&
4231 (c->opt_target_workgroup == NULL)) {
4232 d_fprintf(stderr, _("winbind use default domain = yes set, "
4233 "please specify a workgroup\n"));
4234 return false;
4237 /* Send request to winbind daemon */
4239 wbc_status = wbcListUsers(NULL, &num_users, &users);
4240 if (!WBC_ERROR_IS_OK(wbc_status)) {
4241 DEBUG(1, (_("winbind could not list users: %s\n"),
4242 wbcErrorString(wbc_status)));
4243 return false;
4246 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4248 if (result == NULL) {
4249 DEBUG(1, ("Could not malloc sid array\n"));
4250 wbcFreeMemory(users);
4251 return false;
4254 frame = talloc_stackframe();
4255 for (i=0; i < num_users; i++) {
4256 fstring domain, user;
4257 char *p;
4259 fstrcpy(result[i].name, users[i]);
4261 p = strchr(users[i], *lp_winbind_separator());
4263 DEBUG(3, ("%s\n", users[i]));
4265 if (p == NULL) {
4266 fstrcpy(domain, c->opt_target_workgroup);
4267 fstrcpy(user, users[i]);
4268 } else {
4269 *p++ = '\0';
4270 fstrcpy(domain, users[i]);
4271 strupper_m(domain);
4272 fstrcpy(user, p);
4275 get_user_sids(domain, user, &(result[i].token));
4276 i+=1;
4278 TALLOC_FREE(frame);
4279 wbcFreeMemory(users);
4281 *num_tokens = num_users;
4282 *user_tokens = result;
4284 return true;
4287 static bool get_user_tokens_from_file(FILE *f,
4288 int *num_tokens,
4289 struct user_token **tokens)
4291 struct user_token *token = NULL;
4293 while (!feof(f)) {
4294 fstring line;
4296 if (fgets(line, sizeof(line)-1, f) == NULL) {
4297 return true;
4300 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4301 line[strlen(line)-1] = '\0';
4304 if (line[0] == ' ') {
4305 /* We have a SID */
4307 DOM_SID sid;
4308 if(!string_to_sid(&sid, &line[1])) {
4309 DEBUG(1,("get_user_tokens_from_file: Could "
4310 "not convert sid %s \n",&line[1]));
4311 return false;
4314 if (token == NULL) {
4315 DEBUG(0, ("File does not begin with username"));
4316 return false;
4319 add_sid_to_token(&token->token, &sid);
4320 continue;
4323 /* And a new user... */
4325 *num_tokens += 1;
4326 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4327 if (*tokens == NULL) {
4328 DEBUG(0, ("Could not realloc tokens\n"));
4329 return false;
4332 token = &((*tokens)[*num_tokens-1]);
4334 fstrcpy(token->name, line);
4335 token->token.num_sids = 0;
4336 token->token.user_sids = NULL;
4337 continue;
4340 return false;
4345 * Show the list of all users that have access to a share
4348 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4349 TALLOC_CTX *mem_ctx,
4350 const char *netname,
4351 int num_tokens,
4352 struct user_token *tokens)
4354 uint16_t fnum;
4355 SEC_DESC *share_sd = NULL;
4356 SEC_DESC *root_sd = NULL;
4357 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4358 int i;
4359 union srvsvc_NetShareInfo info;
4360 WERROR result;
4361 NTSTATUS status;
4362 uint16 cnum;
4364 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4365 pipe_hnd->desthost,
4366 netname,
4367 502,
4368 &info,
4369 &result);
4371 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4372 DEBUG(1, ("Coult not query secdesc for share %s\n",
4373 netname));
4374 return;
4377 share_sd = info.info502->sd_buf.sd;
4378 if (share_sd == NULL) {
4379 DEBUG(1, ("Got no secdesc for share %s\n",
4380 netname));
4383 cnum = cli->cnum;
4385 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4386 return;
4389 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4390 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4391 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4394 for (i=0; i<num_tokens; i++) {
4395 uint32 acc_granted;
4397 if (share_sd != NULL) {
4398 status = se_access_check(share_sd, &tokens[i].token,
4399 1, &acc_granted);
4401 if (!NT_STATUS_IS_OK(status)) {
4402 DEBUG(1, ("Could not check share_sd for "
4403 "user %s\n",
4404 tokens[i].name));
4405 continue;
4409 if (root_sd == NULL) {
4410 d_printf(" %s\n", tokens[i].name);
4411 continue;
4414 status = se_access_check(root_sd, &tokens[i].token,
4415 1, &acc_granted);
4416 if (!NT_STATUS_IS_OK(status)) {
4417 DEBUG(1, ("Could not check root_sd for user %s\n",
4418 tokens[i].name));
4419 continue;
4421 d_printf(" %s\n", tokens[i].name);
4424 if (fnum != (uint16_t)-1)
4425 cli_close(cli, fnum);
4426 cli_tdis(cli);
4427 cli->cnum = cnum;
4429 return;
4432 struct share_list {
4433 int num_shares;
4434 char **shares;
4437 static void collect_share(const char *name, uint32 m,
4438 const char *comment, void *state)
4440 struct share_list *share_list = (struct share_list *)state;
4442 if (m != STYPE_DISKTREE)
4443 return;
4445 share_list->num_shares += 1;
4446 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4447 if (!share_list->shares) {
4448 share_list->num_shares = 0;
4449 return;
4451 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4455 * List shares on a remote RPC server, including the security descriptors.
4457 * All parameters are provided by the run_rpc_command function, except for
4458 * argc, argv which are passed through.
4460 * @param domain_sid The domain sid acquired from the remote server.
4461 * @param cli A cli_state connected to the server.
4462 * @param mem_ctx Talloc context, destroyed on completion of the function.
4463 * @param argc Standard main() style argc.
4464 * @param argv Standard main() style argv. Initial components are already
4465 * stripped.
4467 * @return Normal NTSTATUS return.
4470 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4471 const DOM_SID *domain_sid,
4472 const char *domain_name,
4473 struct cli_state *cli,
4474 struct rpc_pipe_client *pipe_hnd,
4475 TALLOC_CTX *mem_ctx,
4476 int argc,
4477 const char **argv)
4479 int ret;
4480 bool r;
4481 uint32 i;
4482 FILE *f;
4484 struct user_token *tokens = NULL;
4485 int num_tokens = 0;
4487 struct share_list share_list;
4489 if (argc == 0) {
4490 f = stdin;
4491 } else {
4492 f = fopen(argv[0], "r");
4495 if (f == NULL) {
4496 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4497 return NT_STATUS_UNSUCCESSFUL;
4500 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4502 if (f != stdin)
4503 fclose(f);
4505 if (!r) {
4506 DEBUG(0, ("Could not read users from file\n"));
4507 return NT_STATUS_UNSUCCESSFUL;
4510 for (i=0; i<num_tokens; i++)
4511 collect_alias_memberships(&tokens[i].token);
4513 share_list.num_shares = 0;
4514 share_list.shares = NULL;
4516 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4518 if (ret == -1) {
4519 DEBUG(0, ("Error returning browse list: %s\n",
4520 cli_errstr(cli)));
4521 goto done;
4524 for (i = 0; i < share_list.num_shares; i++) {
4525 char *netname = share_list.shares[i];
4527 if (netname[strlen(netname)-1] == '$')
4528 continue;
4530 d_printf("%s\n", netname);
4532 show_userlist(pipe_hnd, mem_ctx, netname,
4533 num_tokens, tokens);
4535 done:
4536 for (i=0; i<num_tokens; i++) {
4537 free_user_token(&tokens[i].token);
4539 SAFE_FREE(tokens);
4540 SAFE_FREE(share_list.shares);
4542 return NT_STATUS_OK;
4545 static int rpc_share_allowedusers(struct net_context *c, int argc,
4546 const char **argv)
4548 int result;
4550 if (c->display_usage) {
4551 d_printf( "%s\n"
4552 "net rpc share allowedusers\n"
4553 " %s\n",
4554 _("Usage:"),
4555 _("List allowed users"));
4556 return 0;
4559 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4560 rpc_aliaslist_internals,
4561 argc, argv);
4562 if (result != 0)
4563 return result;
4565 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4566 rpc_aliaslist_dump,
4567 argc, argv);
4568 if (result != 0)
4569 return result;
4571 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4572 rpc_share_allowedusers_internals,
4573 argc, argv);
4576 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4578 int num_tokens = 0;
4579 struct user_token *tokens = NULL;
4580 int i;
4582 if (argc != 0) {
4583 net_usersidlist_usage(c, argc, argv);
4584 return 0;
4587 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4588 DEBUG(0, ("Could not get the user/sid list\n"));
4589 return 0;
4592 for (i=0; i<num_tokens; i++) {
4593 dump_user_token(&tokens[i]);
4594 free_user_token(&tokens[i].token);
4597 SAFE_FREE(tokens);
4598 return 1;
4601 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4603 d_printf(_("net usersidlist\n"
4604 "\tprints out a list of all users the running winbind knows\n"
4605 "\tabout, together with all their SIDs. This is used as\n"
4606 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
4608 net_common_flags_usage(c, argc, argv);
4609 return -1;
4613 * 'net rpc share' entrypoint.
4614 * @param argc Standard main() style argc.
4615 * @param argv Standard main() style argv. Initial components are already
4616 * stripped.
4619 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4621 NET_API_STATUS status;
4623 struct functable func[] = {
4625 "add",
4626 rpc_share_add,
4627 NET_TRANSPORT_RPC,
4628 N_("Add share"),
4629 N_("net rpc share add\n"
4630 " Add share")
4633 "delete",
4634 rpc_share_delete,
4635 NET_TRANSPORT_RPC,
4636 N_("Remove share"),
4637 N_("net rpc share delete\n"
4638 " Remove share")
4641 "allowedusers",
4642 rpc_share_allowedusers,
4643 NET_TRANSPORT_RPC,
4644 N_("Modify allowed users"),
4645 N_("net rpc share allowedusers\n"
4646 " Modify allowed users")
4649 "migrate",
4650 rpc_share_migrate,
4651 NET_TRANSPORT_RPC,
4652 N_("Migrate share to local server"),
4653 N_("net rpc share migrate\n"
4654 " Migrate share to local server")
4657 "list",
4658 rpc_share_list,
4659 NET_TRANSPORT_RPC,
4660 N_("List shares"),
4661 N_("net rpc share list\n"
4662 " List shares")
4664 {NULL, NULL, 0, NULL, NULL}
4667 status = libnetapi_init(&c->netapi_ctx);
4668 if (status != 0) {
4669 return -1;
4671 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4672 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4673 if (c->opt_kerberos) {
4674 libnetapi_set_use_kerberos(c->netapi_ctx);
4677 if (argc == 0) {
4678 if (c->display_usage) {
4679 d_printf("%s\n%s",
4680 _("Usage:"),
4681 _("net rpc share\n"
4682 " List shares\n"
4683 " Alias for net rpc share list\n"));
4684 net_display_usage_from_functable(func);
4685 return 0;
4688 return rpc_share_list(c, argc, argv);
4691 return net_run_function(c, argc, argv, "net rpc share", func);
4694 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4695 TALLOC_CTX *mem_ctx,
4696 struct rpc_sh_ctx *ctx,
4697 struct rpc_pipe_client *pipe_hnd,
4698 int argc, const char **argv)
4701 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4704 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4705 TALLOC_CTX *mem_ctx,
4706 struct rpc_sh_ctx *ctx,
4707 struct rpc_pipe_client *pipe_hnd,
4708 int argc, const char **argv)
4710 NET_API_STATUS status;
4711 uint32_t parm_err = 0;
4712 struct SHARE_INFO_2 i2;
4714 if ((argc < 2) || (argc > 3)) {
4715 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
4716 ctx->whoami);
4717 return NT_STATUS_INVALID_PARAMETER;
4720 i2.shi2_netname = argv[0];
4721 i2.shi2_type = STYPE_DISKTREE;
4722 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4723 i2.shi2_permissions = 0;
4724 i2.shi2_max_uses = 0;
4725 i2.shi2_current_uses = 0;
4726 i2.shi2_path = argv[1];
4727 i2.shi2_passwd = NULL;
4729 status = NetShareAdd(pipe_hnd->desthost,
4731 (uint8_t *)&i2,
4732 &parm_err);
4734 return werror_to_ntstatus(W_ERROR(status));
4737 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4738 TALLOC_CTX *mem_ctx,
4739 struct rpc_sh_ctx *ctx,
4740 struct rpc_pipe_client *pipe_hnd,
4741 int argc, const char **argv)
4743 if (argc != 1) {
4744 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
4745 return NT_STATUS_INVALID_PARAMETER;
4748 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4751 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4752 TALLOC_CTX *mem_ctx,
4753 struct rpc_sh_ctx *ctx,
4754 struct rpc_pipe_client *pipe_hnd,
4755 int argc, const char **argv)
4757 union srvsvc_NetShareInfo info;
4758 WERROR result;
4759 NTSTATUS status;
4761 if (argc != 1) {
4762 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
4763 return NT_STATUS_INVALID_PARAMETER;
4766 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4767 pipe_hnd->desthost,
4768 argv[0],
4770 &info,
4771 &result);
4772 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4773 goto done;
4776 d_printf(_("Name: %s\n"), info.info2->name);
4777 d_printf(_("Comment: %s\n"), info.info2->comment);
4778 d_printf(_("Path: %s\n"), info.info2->path);
4779 d_printf(_("Password: %s\n"), info.info2->password);
4781 done:
4782 return werror_to_ntstatus(result);
4785 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4786 struct rpc_sh_ctx *ctx)
4788 static struct rpc_sh_cmd cmds[] = {
4790 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4791 N_("List available shares") },
4793 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4794 N_("Add a share") },
4796 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4797 N_("Delete a share") },
4799 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4800 N_("Get information about a share") },
4802 { NULL, NULL, 0, NULL, NULL }
4805 return cmds;
4808 /****************************************************************************/
4810 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4812 return net_file_usage(c, argc, argv);
4816 * Close a file on a remote RPC server.
4818 * @param argc Standard main() style argc.
4819 * @param argv Standard main() style argv. Initial components are already
4820 * stripped.
4822 * @return A shell status integer (0 for success).
4824 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4826 if (argc < 1 || c->display_usage) {
4827 return rpc_file_usage(c, argc, argv);
4830 return NetFileClose(c->opt_host, atoi(argv[0]));
4834 * Formatted print of open file info
4836 * @param r struct FILE_INFO_3 contents
4839 static void display_file_info_3(struct FILE_INFO_3 *r)
4841 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4842 r->fi3_id, r->fi3_username, r->fi3_permissions,
4843 r->fi3_num_locks, r->fi3_pathname);
4847 * List files for a user on a remote RPC server.
4849 * @param argc Standard main() style argc.
4850 * @param argv Standard main() style argv. Initial components are already
4851 * stripped.
4853 * @return A shell status integer (0 for success)..
4856 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4858 NET_API_STATUS status;
4859 uint32 preferred_len = 0xffffffff, i;
4860 const char *username=NULL;
4861 uint32_t total_entries = 0;
4862 uint32_t entries_read = 0;
4863 uint32_t resume_handle = 0;
4864 struct FILE_INFO_3 *i3 = NULL;
4866 if (c->display_usage) {
4867 return rpc_file_usage(c, argc, argv);
4870 /* if argc > 0, must be user command */
4871 if (argc > 0) {
4872 username = smb_xstrdup(argv[0]);
4875 status = NetFileEnum(c->opt_host,
4876 NULL,
4877 username,
4879 (uint8_t **)(void *)&i3,
4880 preferred_len,
4881 &entries_read,
4882 &total_entries,
4883 &resume_handle);
4885 if (status != 0) {
4886 goto done;
4889 /* Display results */
4891 d_printf(_(
4892 "\nEnumerating open files on remote server:\n\n"
4893 "\nFileId Opened by Perms Locks Path"
4894 "\n------ --------- ----- ----- ---- \n"));
4895 for (i = 0; i < entries_read; i++) {
4896 display_file_info_3(&i3[i]);
4898 done:
4899 return status;
4903 * 'net rpc file' entrypoint.
4904 * @param argc Standard main() style argc.
4905 * @param argv Standard main() style argv. Initial components are already
4906 * stripped.
4909 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4911 NET_API_STATUS status;
4913 struct functable func[] = {
4915 "close",
4916 rpc_file_close,
4917 NET_TRANSPORT_RPC,
4918 N_("Close opened file"),
4919 N_("net rpc file close\n"
4920 " Close opened file")
4923 "user",
4924 rpc_file_user,
4925 NET_TRANSPORT_RPC,
4926 N_("List files opened by user"),
4927 N_("net rpc file user\n"
4928 " List files opened by user")
4930 #if 0
4932 "info",
4933 rpc_file_info,
4934 NET_TRANSPORT_RPC,
4935 N_("Display information about opened file"),
4936 N_("net rpc file info\n"
4937 " Display information about opened file")
4939 #endif
4940 {NULL, NULL, 0, NULL, NULL}
4943 status = libnetapi_init(&c->netapi_ctx);
4944 if (status != 0) {
4945 return -1;
4947 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4948 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4949 if (c->opt_kerberos) {
4950 libnetapi_set_use_kerberos(c->netapi_ctx);
4953 if (argc == 0) {
4954 if (c->display_usage) {
4955 d_printf(_("Usage:\n"));
4956 d_printf(_("net rpc file\n"
4957 " List opened files\n"));
4958 net_display_usage_from_functable(func);
4959 return 0;
4962 return rpc_file_user(c, argc, argv);
4965 return net_run_function(c, argc, argv, "net rpc file", func);
4969 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
4971 * All parameters are provided by the run_rpc_command function, except for
4972 * argc, argv which are passed through.
4974 * @param c A net_context structure.
4975 * @param domain_sid The domain sid acquired from the remote server.
4976 * @param cli A cli_state connected to the server.
4977 * @param mem_ctx Talloc context, destroyed on completion of the function.
4978 * @param argc Standard main() style argc.
4979 * @param argv Standard main() style argv. Initial components are already
4980 * stripped.
4982 * @return Normal NTSTATUS return.
4985 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
4986 const DOM_SID *domain_sid,
4987 const char *domain_name,
4988 struct cli_state *cli,
4989 struct rpc_pipe_client *pipe_hnd,
4990 TALLOC_CTX *mem_ctx,
4991 int argc,
4992 const char **argv)
4994 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4996 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
4998 if (NT_STATUS_IS_OK(result)) {
4999 d_printf(_("\nShutdown successfully aborted\n"));
5000 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5001 } else
5002 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5004 return result;
5008 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5010 * All parameters are provided by the run_rpc_command function, except for
5011 * argc, argv which are passed through.
5013 * @param c A net_context structure.
5014 * @param domain_sid The domain sid acquired from the remote server.
5015 * @param cli A cli_state connected to the server.
5016 * @param mem_ctx Talloc context, destroyed on completion of the function.
5017 * @param argc Standard main() style argc.
5018 * @param argv Standard main() style argv. Initial components are already
5019 * stripped.
5021 * @return Normal NTSTATUS return.
5024 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5025 const DOM_SID *domain_sid,
5026 const char *domain_name,
5027 struct cli_state *cli,
5028 struct rpc_pipe_client *pipe_hnd,
5029 TALLOC_CTX *mem_ctx,
5030 int argc,
5031 const char **argv)
5033 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5035 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5037 if (NT_STATUS_IS_OK(result)) {
5038 d_printf(_("\nShutdown successfully aborted\n"));
5039 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5040 } else
5041 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5043 return result;
5047 * ABORT the shutdown of a remote RPC server.
5049 * @param argc Standard main() style argc.
5050 * @param argv Standard main() style argv. Initial components are already
5051 * stripped.
5053 * @return A shell status integer (0 for success).
5056 static int rpc_shutdown_abort(struct net_context *c, int argc,
5057 const char **argv)
5059 int rc = -1;
5061 if (c->display_usage) {
5062 d_printf( "%s\n"
5063 "net rpc abortshutdown\n"
5064 " %s\n",
5065 _("Usage:"),
5066 _("Abort a scheduled shutdown"));
5067 return 0;
5070 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5071 rpc_shutdown_abort_internals, argc, argv);
5073 if (rc == 0)
5074 return rc;
5076 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5078 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5079 rpc_reg_shutdown_abort_internals,
5080 argc, argv);
5084 * Shut down a remote RPC Server via initshutdown 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 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5101 const 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;
5110 const char *msg = N_("This machine will be shutdown shortly");
5111 uint32 timeout = 20;
5112 struct lsa_StringLarge msg_string;
5114 if (c->opt_comment) {
5115 msg = c->opt_comment;
5117 if (c->opt_timeout) {
5118 timeout = c->opt_timeout;
5121 msg_string.string = msg;
5123 /* create an entry */
5124 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5125 &msg_string, timeout, c->opt_force, c->opt_reboot,
5126 NULL);
5128 if (NT_STATUS_IS_OK(result)) {
5129 d_printf(_("\nShutdown of remote machine succeeded\n"));
5130 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5131 } else {
5132 DEBUG(1,("Shutdown of remote machine failed!\n"));
5134 return result;
5138 * Shut down a remote RPC Server via winreg pipe.
5140 * All parameters are provided by the run_rpc_command function, except for
5141 * argc, argv which are passed through.
5143 * @param c A net_context structure.
5144 * @param domain_sid The domain sid acquired from the remote server.
5145 * @param cli A cli_state connected to the server.
5146 * @param mem_ctx Talloc context, destroyed on completion of the function.
5147 * @param argc Standard main() style argc.
5148 * @param argv Standard main() style argv. Initial components are already
5149 * stripped.
5151 * @return Normal NTSTATUS return.
5154 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5155 const DOM_SID *domain_sid,
5156 const char *domain_name,
5157 struct cli_state *cli,
5158 struct rpc_pipe_client *pipe_hnd,
5159 TALLOC_CTX *mem_ctx,
5160 int argc,
5161 const char **argv)
5163 const char *msg = N_("This machine will be shutdown shortly");
5164 uint32 timeout = 20;
5165 struct lsa_StringLarge msg_string;
5166 NTSTATUS result;
5167 WERROR werr;
5169 if (c->opt_comment) {
5170 msg = c->opt_comment;
5172 msg_string.string = msg;
5174 if (c->opt_timeout) {
5175 timeout = c->opt_timeout;
5178 /* create an entry */
5179 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5180 &msg_string, timeout, c->opt_force, c->opt_reboot,
5181 &werr);
5183 if (NT_STATUS_IS_OK(result)) {
5184 d_printf(_("\nShutdown of remote machine succeeded\n"));
5185 } else {
5186 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5187 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5188 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5189 else
5190 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5193 return result;
5197 * Shut down a remote RPC server.
5199 * @param argc Standard main() style argc.
5200 * @param argv Standard main() style argv. Initial components are already
5201 * stripped.
5203 * @return A shell status integer (0 for success).
5206 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5208 int rc = -1;
5210 if (c->display_usage) {
5211 d_printf( "%s\n"
5212 "net rpc shutdown\n"
5213 " %s\n",
5214 _("Usage:"),
5215 _("Shut down a remote RPC server"));
5216 return 0;
5219 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5220 rpc_init_shutdown_internals, argc, argv);
5222 if (rc) {
5223 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5224 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5225 rpc_reg_shutdown_internals, argc, argv);
5228 return rc;
5231 /***************************************************************************
5232 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5233 ***************************************************************************/
5236 * Add interdomain trust account to the RPC server.
5237 * All parameters (except for argc and argv) are passed by run_rpc_command
5238 * function.
5240 * @param c A net_context structure.
5241 * @param domain_sid The domain sid acquired from the server.
5242 * @param cli A cli_state connected to the server.
5243 * @param mem_ctx Talloc context, destroyed on completion of the function.
5244 * @param argc Standard main() style argc.
5245 * @param argv Standard main() style argv. Initial components are already
5246 * stripped.
5248 * @return normal NTSTATUS return code.
5251 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5252 const DOM_SID *domain_sid,
5253 const char *domain_name,
5254 struct cli_state *cli,
5255 struct rpc_pipe_client *pipe_hnd,
5256 TALLOC_CTX *mem_ctx,
5257 int argc,
5258 const char **argv)
5260 struct policy_handle connect_pol, domain_pol, user_pol;
5261 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5262 char *acct_name;
5263 struct lsa_String lsa_acct_name;
5264 uint32 acb_info;
5265 uint32 acct_flags=0;
5266 uint32 user_rid;
5267 uint32_t access_granted = 0;
5268 union samr_UserInfo info;
5269 unsigned int orig_timeout;
5271 if (argc != 2) {
5272 d_printf("%s\n%s",
5273 _("Usage:"),
5274 _(" net rpc trustdom add <domain_name> "
5275 "<trust password>\n"));
5276 return NT_STATUS_INVALID_PARAMETER;
5280 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5283 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5284 return NT_STATUS_NO_MEMORY;
5287 strupper_m(acct_name);
5289 init_lsa_String(&lsa_acct_name, acct_name);
5291 /* Get samr policy handle */
5292 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5293 pipe_hnd->desthost,
5294 MAXIMUM_ALLOWED_ACCESS,
5295 &connect_pol);
5296 if (!NT_STATUS_IS_OK(result)) {
5297 goto done;
5300 /* Get domain policy handle */
5301 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5302 &connect_pol,
5303 MAXIMUM_ALLOWED_ACCESS,
5304 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5305 &domain_pol);
5306 if (!NT_STATUS_IS_OK(result)) {
5307 goto done;
5310 /* This call can take a long time - allow the server to time out.
5311 * 35 seconds should do it. */
5313 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5315 /* Create trusting domain's account */
5316 acb_info = ACB_NORMAL;
5317 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5318 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5319 SAMR_USER_ACCESS_SET_PASSWORD |
5320 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5321 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5323 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5324 &domain_pol,
5325 &lsa_acct_name,
5326 acb_info,
5327 acct_flags,
5328 &user_pol,
5329 &access_granted,
5330 &user_rid);
5332 /* And restore our original timeout. */
5333 rpccli_set_timeout(pipe_hnd, orig_timeout);
5335 if (!NT_STATUS_IS_OK(result)) {
5336 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5337 acct_name, nt_errstr(result));
5338 goto done;
5342 struct samr_CryptPassword crypt_pwd;
5344 ZERO_STRUCT(info.info23);
5346 init_samr_CryptPassword(argv[1],
5347 &cli->user_session_key,
5348 &crypt_pwd);
5350 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5351 SAMR_FIELD_NT_PASSWORD_PRESENT;
5352 info.info23.info.acct_flags = ACB_DOMTRUST;
5353 info.info23.password = crypt_pwd;
5355 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5356 &user_pol,
5358 &info);
5360 if (!NT_STATUS_IS_OK(result)) {
5361 DEBUG(0,("Could not set trust account password: %s\n",
5362 nt_errstr(result)));
5363 goto done;
5367 done:
5368 SAFE_FREE(acct_name);
5369 return result;
5373 * Create interdomain trust account for a remote domain.
5375 * @param argc Standard argc.
5376 * @param argv Standard argv without initial components.
5378 * @return Integer status (0 means success).
5381 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5383 if (argc > 0 && !c->display_usage) {
5384 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5385 rpc_trustdom_add_internals, argc, argv);
5386 } else {
5387 d_printf("%s\n%s",
5388 _("Usage:"),
5389 _("net rpc trustdom add <domain_name> <trust "
5390 "password>\n"));
5391 return -1;
5397 * Remove interdomain trust account from the RPC server.
5398 * All parameters (except for argc and argv) are passed by run_rpc_command
5399 * function.
5401 * @param c A net_context structure.
5402 * @param domain_sid The domain sid acquired from the server.
5403 * @param cli A cli_state connected to the server.
5404 * @param mem_ctx Talloc context, destroyed on completion of the function.
5405 * @param argc Standard main() style argc.
5406 * @param argv Standard main() style argv. Initial components are already
5407 * stripped.
5409 * @return normal NTSTATUS return code.
5412 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5413 const DOM_SID *domain_sid,
5414 const char *domain_name,
5415 struct cli_state *cli,
5416 struct rpc_pipe_client *pipe_hnd,
5417 TALLOC_CTX *mem_ctx,
5418 int argc,
5419 const char **argv)
5421 struct policy_handle connect_pol, domain_pol, user_pol;
5422 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5423 char *acct_name;
5424 DOM_SID trust_acct_sid;
5425 struct samr_Ids user_rids, name_types;
5426 struct lsa_String lsa_acct_name;
5428 if (argc != 1) {
5429 d_printf("%s\n%s",
5430 _("Usage:"),
5431 _(" net rpc trustdom del <domain_name>\n"));
5432 return NT_STATUS_INVALID_PARAMETER;
5436 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5438 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5440 if (acct_name == NULL)
5441 return NT_STATUS_NO_MEMORY;
5443 strupper_m(acct_name);
5445 /* Get samr policy handle */
5446 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5447 pipe_hnd->desthost,
5448 MAXIMUM_ALLOWED_ACCESS,
5449 &connect_pol);
5450 if (!NT_STATUS_IS_OK(result)) {
5451 goto done;
5454 /* Get domain policy handle */
5455 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5456 &connect_pol,
5457 MAXIMUM_ALLOWED_ACCESS,
5458 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5459 &domain_pol);
5460 if (!NT_STATUS_IS_OK(result)) {
5461 goto done;
5464 init_lsa_String(&lsa_acct_name, acct_name);
5466 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5467 &domain_pol,
5469 &lsa_acct_name,
5470 &user_rids,
5471 &name_types);
5473 if (!NT_STATUS_IS_OK(result)) {
5474 d_printf(_("net rpc trustdom del: LookupNames on user %s "
5475 "failed %s\n"),
5476 acct_name, nt_errstr(result) );
5477 goto done;
5480 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5481 &domain_pol,
5482 MAXIMUM_ALLOWED_ACCESS,
5483 user_rids.ids[0],
5484 &user_pol);
5486 if (!NT_STATUS_IS_OK(result)) {
5487 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
5488 "%s\n"),
5489 acct_name, nt_errstr(result) );
5490 goto done;
5493 /* append the rid to the domain sid */
5494 sid_copy(&trust_acct_sid, domain_sid);
5495 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5496 goto done;
5499 /* remove the sid */
5501 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5502 &user_pol,
5503 &trust_acct_sid);
5504 if (!NT_STATUS_IS_OK(result)) {
5505 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
5506 " on user %s failed %s\n"),
5507 acct_name, nt_errstr(result) );
5508 goto done;
5511 /* Delete user */
5513 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5514 &user_pol);
5516 if (!NT_STATUS_IS_OK(result)) {
5517 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
5518 "%s\n"),
5519 acct_name, nt_errstr(result) );
5520 goto done;
5523 if (!NT_STATUS_IS_OK(result)) {
5524 d_printf(_("Could not set trust account password: %s\n"),
5525 nt_errstr(result));
5526 goto done;
5529 done:
5530 return result;
5534 * Delete interdomain trust account for a remote domain.
5536 * @param argc Standard argc.
5537 * @param argv Standard argv without initial components.
5539 * @return Integer status (0 means success).
5542 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5544 if (argc > 0 && !c->display_usage) {
5545 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5546 rpc_trustdom_del_internals, argc, argv);
5547 } else {
5548 d_printf("%s\n%s",
5549 _("Usage:"),
5550 _("net rpc trustdom del <domain>\n"));
5551 return -1;
5555 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5556 struct cli_state *cli,
5557 TALLOC_CTX *mem_ctx,
5558 const char *domain_name)
5560 char *dc_name = NULL;
5561 const char *buffer = NULL;
5562 struct rpc_pipe_client *netr;
5563 NTSTATUS status;
5565 /* Use NetServerEnum2 */
5567 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5568 SAFE_FREE(dc_name);
5569 return NT_STATUS_OK;
5572 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5573 for domain %s\n", domain_name));
5575 /* Try netr_GetDcName */
5577 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5578 &netr);
5579 if (!NT_STATUS_IS_OK(status)) {
5580 return status;
5583 status = rpccli_netr_GetDcName(netr, mem_ctx,
5584 cli->desthost,
5585 domain_name,
5586 &buffer,
5587 NULL);
5588 TALLOC_FREE(netr);
5590 if (NT_STATUS_IS_OK(status)) {
5591 return status;
5594 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5595 for domain %s\n", domain_name));
5597 return status;
5601 * Establish trust relationship to a trusting domain.
5602 * Interdomain account must already be created on remote PDC.
5604 * @param c A net_context structure.
5605 * @param argc Standard argc.
5606 * @param argv Standard argv without initial components.
5608 * @return Integer status (0 means success).
5611 static int rpc_trustdom_establish(struct net_context *c, int argc,
5612 const char **argv)
5614 struct cli_state *cli = NULL;
5615 struct sockaddr_storage server_ss;
5616 struct rpc_pipe_client *pipe_hnd = NULL;
5617 struct policy_handle connect_hnd;
5618 TALLOC_CTX *mem_ctx;
5619 NTSTATUS nt_status;
5620 DOM_SID *domain_sid;
5622 char* domain_name;
5623 char* acct_name;
5624 fstring pdc_name;
5625 union lsa_PolicyInformation *info = NULL;
5628 * Connect to \\server\ipc$ as 'our domain' account with password
5631 if (argc != 1 || c->display_usage) {
5632 d_printf("%s\n%s",
5633 _("Usage:"),
5634 _("net rpc trustdom establish <domain_name>\n"));
5635 return -1;
5638 domain_name = smb_xstrdup(argv[0]);
5639 strupper_m(domain_name);
5641 /* account name used at first is our domain's name with '$' */
5642 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
5643 return -1;
5645 strupper_m(acct_name);
5648 * opt_workgroup will be used by connection functions further,
5649 * hence it should be set to remote domain name instead of ours
5651 if (c->opt_workgroup) {
5652 c->opt_workgroup = smb_xstrdup(domain_name);
5655 c->opt_user_name = acct_name;
5657 /* find the domain controller */
5658 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5659 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5660 return -1;
5663 /* connect to ipc$ as username/password */
5664 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5665 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5667 /* Is it trusting domain account for sure ? */
5668 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5669 nt_errstr(nt_status)));
5670 return -1;
5673 /* store who we connected to */
5675 saf_store( domain_name, pdc_name );
5678 * Connect to \\server\ipc$ again (this time anonymously)
5681 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5682 (char*)pdc_name);
5684 if (NT_STATUS_IS_ERR(nt_status)) {
5685 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5686 domain_name, nt_errstr(nt_status)));
5687 return -1;
5690 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5691 "domain %s", domain_name))) {
5692 DEBUG(0, ("talloc_init() failed\n"));
5693 cli_shutdown(cli);
5694 return -1;
5697 /* Make sure we're talking to a proper server */
5699 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5700 if (!NT_STATUS_IS_OK(nt_status)) {
5701 cli_shutdown(cli);
5702 talloc_destroy(mem_ctx);
5703 return -1;
5707 * Call LsaOpenPolicy and LsaQueryInfo
5710 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5711 &pipe_hnd);
5712 if (!NT_STATUS_IS_OK(nt_status)) {
5713 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5714 cli_shutdown(cli);
5715 talloc_destroy(mem_ctx);
5716 return -1;
5719 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
5720 &connect_hnd);
5721 if (NT_STATUS_IS_ERR(nt_status)) {
5722 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5723 nt_errstr(nt_status)));
5724 cli_shutdown(cli);
5725 talloc_destroy(mem_ctx);
5726 return -1;
5729 /* Querying info level 5 */
5731 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5732 &connect_hnd,
5733 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5734 &info);
5735 if (NT_STATUS_IS_ERR(nt_status)) {
5736 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5737 nt_errstr(nt_status)));
5738 cli_shutdown(cli);
5739 talloc_destroy(mem_ctx);
5740 return -1;
5743 domain_sid = info->account_domain.sid;
5745 /* There should be actually query info level 3 (following nt serv behaviour),
5746 but I still don't know if it's _really_ necessary */
5749 * Store the password in secrets db
5752 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5753 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5754 cli_shutdown(cli);
5755 talloc_destroy(mem_ctx);
5756 return -1;
5760 * Close the pipes and clean up
5763 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5764 if (NT_STATUS_IS_ERR(nt_status)) {
5765 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5766 nt_errstr(nt_status)));
5767 cli_shutdown(cli);
5768 talloc_destroy(mem_ctx);
5769 return -1;
5772 cli_shutdown(cli);
5774 talloc_destroy(mem_ctx);
5776 d_printf(_("Trust to domain %s established\n"), domain_name);
5777 return 0;
5781 * Revoke trust relationship to the remote domain.
5783 * @param c A net_context structure.
5784 * @param argc Standard argc.
5785 * @param argv Standard argv without initial components.
5787 * @return Integer status (0 means success).
5790 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5791 const char **argv)
5793 char* domain_name;
5794 int rc = -1;
5796 if (argc < 1 || c->display_usage) {
5797 d_printf("%s\n%s",
5798 _("Usage:"),
5799 _("net rpc trustdom revoke <domain_name>\n"
5800 " Revoke trust relationship\n"
5801 " domain_name\tName of domain to revoke trust\n"));
5802 return -1;
5805 /* generate upper cased domain name */
5806 domain_name = smb_xstrdup(argv[0]);
5807 strupper_m(domain_name);
5809 /* delete password of the trust */
5810 if (!pdb_del_trusteddom_pw(domain_name)) {
5811 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5812 domain_name));
5813 goto done;
5816 rc = 0;
5817 done:
5818 SAFE_FREE(domain_name);
5819 return rc;
5822 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5823 const DOM_SID *domain_sid,
5824 const char *domain_name,
5825 struct cli_state *cli,
5826 struct rpc_pipe_client *pipe_hnd,
5827 TALLOC_CTX *mem_ctx,
5828 int argc,
5829 const char **argv)
5831 fstring str_sid;
5832 if (!sid_to_fstring(str_sid, domain_sid)) {
5833 return NT_STATUS_UNSUCCESSFUL;
5835 d_printf("%s\n", str_sid);
5836 return NT_STATUS_OK;
5839 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5841 fstring ascii_sid;
5843 /* convert sid into ascii string */
5844 sid_to_fstring(ascii_sid, dom_sid);
5846 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
5849 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5850 TALLOC_CTX *mem_ctx,
5851 struct policy_handle *pol,
5852 DOM_SID dom_sid,
5853 const char *trusted_dom_name)
5855 NTSTATUS nt_status;
5856 union lsa_TrustedDomainInfo *info = NULL;
5857 char *cleartextpwd = NULL;
5858 uint8_t session_key[16];
5859 DATA_BLOB session_key_blob;
5860 DATA_BLOB data = data_blob_null;
5862 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5863 pol,
5864 &dom_sid,
5865 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5866 &info);
5867 if (NT_STATUS_IS_ERR(nt_status)) {
5868 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5869 nt_errstr(nt_status)));
5870 goto done;
5873 data = data_blob(info->password.password->data,
5874 info->password.password->length);
5876 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
5877 DEBUG(0, ("Could not retrieve password hash\n"));
5878 goto done;
5881 session_key_blob = data_blob_const(session_key, sizeof(session_key));
5882 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
5884 if (cleartextpwd == NULL) {
5885 DEBUG(0,("retrieved NULL password\n"));
5886 nt_status = NT_STATUS_UNSUCCESSFUL;
5887 goto done;
5890 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5891 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5892 nt_status = NT_STATUS_UNSUCCESSFUL;
5893 goto done;
5896 #ifdef DEBUG_PASSWORD
5897 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5898 "password: [%s]\n", trusted_dom_name,
5899 sid_string_dbg(&dom_sid), cleartextpwd));
5900 #endif
5902 done:
5903 SAFE_FREE(cleartextpwd);
5904 data_blob_free(&data);
5906 return nt_status;
5909 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5910 const char **argv)
5912 /* common variables */
5913 TALLOC_CTX* mem_ctx;
5914 struct cli_state *cli = NULL;
5915 struct rpc_pipe_client *pipe_hnd = NULL;
5916 NTSTATUS nt_status;
5917 const char *domain_name = NULL;
5918 DOM_SID *queried_dom_sid;
5919 struct policy_handle connect_hnd;
5920 union lsa_PolicyInformation *info = NULL;
5922 /* trusted domains listing variables */
5923 unsigned int enum_ctx = 0;
5924 int i;
5925 struct lsa_DomainList dom_list;
5926 fstring pdc_name;
5928 if (c->display_usage) {
5929 d_printf( "%s\n"
5930 "net rpc trustdom vampire\n"
5931 " %s\n",
5932 _("Usage:"),
5933 _("Vampire trust relationship from remote server"));
5934 return 0;
5938 * Listing trusted domains (stored in secrets.tdb, if local)
5941 mem_ctx = talloc_init("trust relationships vampire");
5944 * set domain and pdc name to local samba server (default)
5945 * or to remote one given in command line
5948 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
5949 domain_name = c->opt_workgroup;
5950 c->opt_target_workgroup = c->opt_workgroup;
5951 } else {
5952 fstrcpy(pdc_name, global_myname());
5953 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5954 c->opt_target_workgroup = domain_name;
5957 /* open \PIPE\lsarpc and open policy handle */
5958 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
5959 if (!NT_STATUS_IS_OK(nt_status)) {
5960 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
5961 nt_errstr(nt_status)));
5962 talloc_destroy(mem_ctx);
5963 return -1;
5966 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5967 &pipe_hnd);
5968 if (!NT_STATUS_IS_OK(nt_status)) {
5969 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5970 nt_errstr(nt_status) ));
5971 cli_shutdown(cli);
5972 talloc_destroy(mem_ctx);
5973 return -1;
5976 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
5977 &connect_hnd);
5978 if (NT_STATUS_IS_ERR(nt_status)) {
5979 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5980 nt_errstr(nt_status)));
5981 cli_shutdown(cli);
5982 talloc_destroy(mem_ctx);
5983 return -1;
5986 /* query info level 5 to obtain sid of a domain being queried */
5987 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5988 &connect_hnd,
5989 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5990 &info);
5992 if (NT_STATUS_IS_ERR(nt_status)) {
5993 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5994 nt_errstr(nt_status)));
5995 cli_shutdown(cli);
5996 talloc_destroy(mem_ctx);
5997 return -1;
6000 queried_dom_sid = info->account_domain.sid;
6003 * Keep calling LsaEnumTrustdom over opened pipe until
6004 * the end of enumeration is reached
6007 d_printf(_("Vampire trusted domains:\n\n"));
6009 do {
6010 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6011 &connect_hnd,
6012 &enum_ctx,
6013 &dom_list,
6014 (uint32_t)-1);
6015 if (NT_STATUS_IS_ERR(nt_status)) {
6016 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6017 nt_errstr(nt_status)));
6018 cli_shutdown(cli);
6019 talloc_destroy(mem_ctx);
6020 return -1;
6023 for (i = 0; i < dom_list.count; i++) {
6025 print_trusted_domain(dom_list.domains[i].sid,
6026 dom_list.domains[i].name.string);
6028 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6029 *dom_list.domains[i].sid,
6030 dom_list.domains[i].name.string);
6031 if (!NT_STATUS_IS_OK(nt_status)) {
6032 cli_shutdown(cli);
6033 talloc_destroy(mem_ctx);
6034 return -1;
6039 * in case of no trusted domains say something rather
6040 * than just display blank line
6042 if (!dom_list.count) d_printf(_("none\n"));
6044 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6046 /* close this connection before doing next one */
6047 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6048 if (NT_STATUS_IS_ERR(nt_status)) {
6049 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6050 nt_errstr(nt_status)));
6051 cli_shutdown(cli);
6052 talloc_destroy(mem_ctx);
6053 return -1;
6056 /* close lsarpc pipe and connection to IPC$ */
6057 cli_shutdown(cli);
6059 talloc_destroy(mem_ctx);
6060 return 0;
6063 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6065 /* common variables */
6066 TALLOC_CTX* mem_ctx;
6067 struct cli_state *cli = NULL, *remote_cli = NULL;
6068 struct rpc_pipe_client *pipe_hnd = NULL;
6069 NTSTATUS nt_status;
6070 const char *domain_name = NULL;
6071 DOM_SID *queried_dom_sid;
6072 int ascii_dom_name_len;
6073 struct policy_handle connect_hnd;
6074 union lsa_PolicyInformation *info = NULL;
6076 /* trusted domains listing variables */
6077 unsigned int num_domains, enum_ctx = 0;
6078 int i;
6079 struct lsa_DomainList dom_list;
6080 fstring pdc_name;
6081 bool found_domain;
6083 /* trusting domains listing variables */
6084 struct policy_handle domain_hnd;
6085 struct samr_SamArray *trusts = NULL;
6087 if (c->display_usage) {
6088 d_printf( "%s\n"
6089 "net rpc trustdom list\n"
6090 " %s\n",
6091 _("Usage:"),
6092 _("List incoming and outgoing trust relationships"));
6093 return 0;
6097 * Listing trusted domains (stored in secrets.tdb, if local)
6100 mem_ctx = talloc_init("trust relationships listing");
6103 * set domain and pdc name to local samba server (default)
6104 * or to remote one given in command line
6107 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6108 domain_name = c->opt_workgroup;
6109 c->opt_target_workgroup = c->opt_workgroup;
6110 } else {
6111 fstrcpy(pdc_name, global_myname());
6112 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6113 c->opt_target_workgroup = domain_name;
6116 /* open \PIPE\lsarpc and open policy handle */
6117 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6118 if (!NT_STATUS_IS_OK(nt_status)) {
6119 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6120 nt_errstr(nt_status)));
6121 talloc_destroy(mem_ctx);
6122 return -1;
6125 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6126 &pipe_hnd);
6127 if (!NT_STATUS_IS_OK(nt_status)) {
6128 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6129 nt_errstr(nt_status) ));
6130 cli_shutdown(cli);
6131 talloc_destroy(mem_ctx);
6132 return -1;
6135 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6136 &connect_hnd);
6137 if (NT_STATUS_IS_ERR(nt_status)) {
6138 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6139 nt_errstr(nt_status)));
6140 cli_shutdown(cli);
6141 talloc_destroy(mem_ctx);
6142 return -1;
6145 /* query info level 5 to obtain sid of a domain being queried */
6146 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6147 &connect_hnd,
6148 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6149 &info);
6151 if (NT_STATUS_IS_ERR(nt_status)) {
6152 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6153 nt_errstr(nt_status)));
6154 cli_shutdown(cli);
6155 talloc_destroy(mem_ctx);
6156 return -1;
6159 queried_dom_sid = info->account_domain.sid;
6162 * Keep calling LsaEnumTrustdom over opened pipe until
6163 * the end of enumeration is reached
6166 d_printf(_("Trusted domains list:\n\n"));
6168 found_domain = false;
6170 do {
6171 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6172 &connect_hnd,
6173 &enum_ctx,
6174 &dom_list,
6175 (uint32_t)-1);
6176 if (NT_STATUS_IS_ERR(nt_status)) {
6177 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6178 nt_errstr(nt_status)));
6179 cli_shutdown(cli);
6180 talloc_destroy(mem_ctx);
6181 return -1;
6184 for (i = 0; i < dom_list.count; i++) {
6185 print_trusted_domain(dom_list.domains[i].sid,
6186 dom_list.domains[i].name.string);
6187 found_domain = true;
6191 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6194 * in case of no trusted domains say something rather
6195 * than just display blank line
6197 if (!found_domain) {
6198 d_printf(_("none\n"));
6201 /* close this connection before doing next one */
6202 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6203 if (NT_STATUS_IS_ERR(nt_status)) {
6204 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6205 nt_errstr(nt_status)));
6206 cli_shutdown(cli);
6207 talloc_destroy(mem_ctx);
6208 return -1;
6211 TALLOC_FREE(pipe_hnd);
6214 * Listing trusting domains (stored in passdb backend, if local)
6217 d_printf(_("\nTrusting domains list:\n\n"));
6220 * Open \PIPE\samr and get needed policy handles
6222 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6223 &pipe_hnd);
6224 if (!NT_STATUS_IS_OK(nt_status)) {
6225 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6226 cli_shutdown(cli);
6227 talloc_destroy(mem_ctx);
6228 return -1;
6231 /* SamrConnect2 */
6232 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6233 pipe_hnd->desthost,
6234 SAMR_ACCESS_LOOKUP_DOMAIN,
6235 &connect_hnd);
6236 if (!NT_STATUS_IS_OK(nt_status)) {
6237 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6238 nt_errstr(nt_status)));
6239 cli_shutdown(cli);
6240 talloc_destroy(mem_ctx);
6241 return -1;
6244 /* SamrOpenDomain - we have to open domain policy handle in order to be
6245 able to enumerate accounts*/
6246 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6247 &connect_hnd,
6248 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6249 queried_dom_sid,
6250 &domain_hnd);
6251 if (!NT_STATUS_IS_OK(nt_status)) {
6252 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6253 nt_errstr(nt_status)));
6254 cli_shutdown(cli);
6255 talloc_destroy(mem_ctx);
6256 return -1;
6260 * perform actual enumeration
6263 found_domain = false;
6265 enum_ctx = 0; /* reset enumeration context from last enumeration */
6266 do {
6268 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6269 &domain_hnd,
6270 &enum_ctx,
6271 ACB_DOMTRUST,
6272 &trusts,
6273 0xffff,
6274 &num_domains);
6275 if (NT_STATUS_IS_ERR(nt_status)) {
6276 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6277 nt_errstr(nt_status)));
6278 cli_shutdown(cli);
6279 talloc_destroy(mem_ctx);
6280 return -1;
6283 for (i = 0; i < num_domains; i++) {
6285 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6287 found_domain = true;
6290 * get each single domain's sid (do we _really_ need this ?):
6291 * 1) connect to domain's pdc
6292 * 2) query the pdc for domain's sid
6295 /* get rid of '$' tail */
6296 ascii_dom_name_len = strlen(str);
6297 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6298 str[ascii_dom_name_len - 1] = '\0';
6300 /* set opt_* variables to remote domain */
6301 strupper_m(str);
6302 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6303 c->opt_target_workgroup = c->opt_workgroup;
6305 d_printf("%-20s", str);
6307 /* connect to remote domain controller */
6308 nt_status = net_make_ipc_connection(c,
6309 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6310 &remote_cli);
6311 if (NT_STATUS_IS_OK(nt_status)) {
6312 /* query for domain's sid */
6313 if (run_rpc_command(
6314 c, remote_cli,
6315 &ndr_table_lsarpc.syntax_id, 0,
6316 rpc_query_domain_sid, argc,
6317 argv))
6318 d_printf(_("strange - couldn't get domain's sid\n"));
6320 cli_shutdown(remote_cli);
6322 } else {
6323 d_fprintf(stderr, _("domain controller is not "
6324 "responding: %s\n"),
6325 nt_errstr(nt_status));
6326 d_printf(_("couldn't get domain's sid\n"));
6330 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6332 if (!found_domain) {
6333 d_printf("none\n");
6336 /* close opened samr and domain policy handles */
6337 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6338 if (!NT_STATUS_IS_OK(nt_status)) {
6339 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6342 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6343 if (!NT_STATUS_IS_OK(nt_status)) {
6344 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6347 /* close samr pipe and connection to IPC$ */
6348 cli_shutdown(cli);
6350 talloc_destroy(mem_ctx);
6351 return 0;
6355 * Entrypoint for 'net rpc trustdom' code.
6357 * @param argc Standard argc.
6358 * @param argv Standard argv without initial components.
6360 * @return Integer status (0 means success).
6363 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6365 struct functable func[] = {
6367 "add",
6368 rpc_trustdom_add,
6369 NET_TRANSPORT_RPC,
6370 N_("Add trusting domain's account"),
6371 N_("net rpc trustdom add\n"
6372 " Add trusting domain's account")
6375 "del",
6376 rpc_trustdom_del,
6377 NET_TRANSPORT_RPC,
6378 N_("Remove trusting domain's account"),
6379 N_("net rpc trustdom del\n"
6380 " Remove trusting domain's account")
6383 "establish",
6384 rpc_trustdom_establish,
6385 NET_TRANSPORT_RPC,
6386 N_("Establish outgoing trust relationship"),
6387 N_("net rpc trustdom establish\n"
6388 " Establish outgoing trust relationship")
6391 "revoke",
6392 rpc_trustdom_revoke,
6393 NET_TRANSPORT_RPC,
6394 N_("Revoke outgoing trust relationship"),
6395 N_("net rpc trustdom revoke\n"
6396 " Revoke outgoing trust relationship")
6399 "list",
6400 rpc_trustdom_list,
6401 NET_TRANSPORT_RPC,
6402 N_("List in- and outgoing domain trusts"),
6403 N_("net rpc trustdom list\n"
6404 " List in- and outgoing domain trusts")
6407 "vampire",
6408 rpc_trustdom_vampire,
6409 NET_TRANSPORT_RPC,
6410 N_("Vampire trusts from remote server"),
6411 N_("net rpc trustdom vampire\n"
6412 " Vampire trusts from remote server")
6414 {NULL, NULL, 0, NULL, NULL}
6417 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6421 * Check if a server will take rpc commands
6422 * @param flags Type of server to connect to (PDC, DMB, localhost)
6423 * if the host is not explicitly specified
6424 * @return bool (true means rpc supported)
6426 bool net_rpc_check(struct net_context *c, unsigned flags)
6428 struct cli_state *cli;
6429 bool ret = false;
6430 struct sockaddr_storage server_ss;
6431 char *server_name = NULL;
6432 NTSTATUS status;
6434 /* flags (i.e. server type) may depend on command */
6435 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6436 return false;
6438 if ((cli = cli_initialise()) == NULL) {
6439 return false;
6442 status = cli_connect(cli, server_name, &server_ss);
6443 if (!NT_STATUS_IS_OK(status))
6444 goto done;
6445 if (!attempt_netbios_session_request(&cli, global_myname(),
6446 server_name, &server_ss))
6447 goto done;
6448 status = cli_negprot(cli);
6449 if (!NT_STATUS_IS_OK(status))
6450 goto done;
6451 if (cli->protocol < PROTOCOL_NT1)
6452 goto done;
6454 ret = true;
6455 done:
6456 cli_shutdown(cli);
6457 return ret;
6460 /* dump sam database via samsync rpc calls */
6461 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6462 if (c->display_usage) {
6463 d_printf( "%s\n"
6464 "net rpc samdump\n"
6465 " %s\n",
6466 _("Usage:"),
6467 _("Dump remote SAM database"));
6468 return 0;
6471 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6472 NET_FLAGS_ANONYMOUS,
6473 rpc_samdump_internals, argc, argv);
6476 /* syncronise sam database via samsync rpc calls */
6477 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6479 struct functable func[] = {
6481 "ldif",
6482 rpc_vampire_ldif,
6483 NET_TRANSPORT_RPC,
6484 N_("Dump remote SAM database to ldif"),
6485 N_("net rpc vampire ldif\n"
6486 " Dump remote SAM database to LDIF file or "
6487 "stdout")
6490 "keytab",
6491 rpc_vampire_keytab,
6492 NET_TRANSPORT_RPC,
6493 N_("Dump remote SAM database to Kerberos Keytab"),
6494 N_("net rpc vampire keytab\n"
6495 " Dump remote SAM database to Kerberos keytab "
6496 "file")
6499 "passdb",
6500 rpc_vampire_passdb,
6501 NET_TRANSPORT_RPC,
6502 N_("Dump remote SAM database to passdb"),
6503 N_("net rpc vampire passdb\n"
6504 " Dump remote SAM database to passdb")
6507 {NULL, NULL, 0, NULL, NULL}
6510 if (argc == 0) {
6511 if (c->display_usage) {
6512 d_printf( "%s\n"
6513 "net rpc vampire\n"
6514 " %s\n",
6515 _("Usage:"),
6516 _("Vampire remote SAM database"));
6517 return 0;
6520 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6521 NET_FLAGS_ANONYMOUS,
6522 rpc_vampire_internals,
6523 argc, argv);
6526 return net_run_function(c, argc, argv, "net rpc vampire", func);
6530 * Migrate everything from a print server.
6532 * @param c A net_context structure.
6533 * @param argc Standard main() style argc.
6534 * @param argv Standard main() style argv. Initial components are already
6535 * stripped.
6537 * @return A shell status integer (0 for success).
6539 * The order is important !
6540 * To successfully add drivers the print queues have to exist !
6541 * Applying ACLs should be the last step, because you're easily locked out.
6544 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6545 const char **argv)
6547 int ret;
6549 if (c->display_usage) {
6550 d_printf( "%s\n"
6551 "net rpc printer migrate all\n"
6552 " %s\n",
6553 _("Usage:"),
6554 _("Migrate everything from a print server"));
6555 return 0;
6558 if (!c->opt_host) {
6559 d_printf(_("no server to migrate\n"));
6560 return -1;
6563 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6564 rpc_printer_migrate_printers_internals, argc,
6565 argv);
6566 if (ret)
6567 return ret;
6569 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6570 rpc_printer_migrate_drivers_internals, argc,
6571 argv);
6572 if (ret)
6573 return ret;
6575 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6576 rpc_printer_migrate_forms_internals, argc, argv);
6577 if (ret)
6578 return ret;
6580 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6581 rpc_printer_migrate_settings_internals, argc,
6582 argv);
6583 if (ret)
6584 return ret;
6586 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6587 rpc_printer_migrate_security_internals, argc,
6588 argv);
6593 * Migrate print drivers from a print server.
6595 * @param c A net_context structure.
6596 * @param argc Standard main() style argc.
6597 * @param argv Standard main() style argv. Initial components are already
6598 * stripped.
6600 * @return A shell status integer (0 for success).
6602 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6603 const char **argv)
6605 if (c->display_usage) {
6606 d_printf( "%s\n"
6607 "net rpc printer migrate drivers\n"
6608 " %s\n",
6609 _("Usage:"),
6610 _("Migrate print-drivers from a print-server"));
6611 return 0;
6614 if (!c->opt_host) {
6615 d_printf(_("no server to migrate\n"));
6616 return -1;
6619 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6620 rpc_printer_migrate_drivers_internals,
6621 argc, argv);
6625 * Migrate print-forms from a print-server.
6627 * @param c A net_context structure.
6628 * @param argc Standard main() style argc.
6629 * @param argv Standard main() style argv. Initial components are already
6630 * stripped.
6632 * @return A shell status integer (0 for success).
6634 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6635 const char **argv)
6637 if (c->display_usage) {
6638 d_printf( "%s\n"
6639 "net rpc printer migrate forms\n"
6640 " %s\n",
6641 _("Usage:"),
6642 _("Migrate print-forms from a print-server"));
6643 return 0;
6646 if (!c->opt_host) {
6647 d_printf(_("no server to migrate\n"));
6648 return -1;
6651 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6652 rpc_printer_migrate_forms_internals,
6653 argc, argv);
6657 * Migrate printers from a print-server.
6659 * @param c A net_context structure.
6660 * @param argc Standard main() style argc.
6661 * @param argv Standard main() style argv. Initial components are already
6662 * stripped.
6664 * @return A shell status integer (0 for success).
6666 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6667 const char **argv)
6669 if (c->display_usage) {
6670 d_printf( "%s\n"
6671 "net rpc printer migrate printers\n"
6672 " %s\n",
6673 _("Usage:"),
6674 _("Migrate printers from a print-server"));
6675 return 0;
6678 if (!c->opt_host) {
6679 d_printf(_("no server to migrate\n"));
6680 return -1;
6683 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6684 rpc_printer_migrate_printers_internals,
6685 argc, argv);
6689 * Migrate printer-ACLs from a print-server
6691 * @param c A net_context structure.
6692 * @param argc Standard main() style argc.
6693 * @param argv Standard main() style argv. Initial components are already
6694 * stripped.
6696 * @return A shell status integer (0 for success).
6698 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6699 const char **argv)
6701 if (c->display_usage) {
6702 d_printf( "%s\n"
6703 "net rpc printer migrate security\n"
6704 " %s\n",
6705 _("Usage:"),
6706 _("Migrate printer-ACLs from a print-server"));
6707 return 0;
6710 if (!c->opt_host) {
6711 d_printf(_("no server to migrate\n"));
6712 return -1;
6715 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6716 rpc_printer_migrate_security_internals,
6717 argc, argv);
6721 * Migrate printer-settings from a print-server.
6723 * @param c A net_context structure.
6724 * @param argc Standard main() style argc.
6725 * @param argv Standard main() style argv. Initial components are already
6726 * stripped.
6728 * @return A shell status integer (0 for success).
6730 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6731 const char **argv)
6733 if (c->display_usage) {
6734 d_printf( "%s\n"
6735 "net rpc printer migrate settings\n"
6736 " %s\n",
6737 _("Usage:"),
6738 _("Migrate printer-settings from a "
6739 "print-server"));
6740 return 0;
6743 if (!c->opt_host) {
6744 d_printf(_("no server to migrate\n"));
6745 return -1;
6748 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6749 rpc_printer_migrate_settings_internals,
6750 argc, argv);
6754 * 'net rpc printer' entrypoint.
6756 * @param c A net_context structure.
6757 * @param argc Standard main() style argc.
6758 * @param argv Standard main() style argv. Initial components are already
6759 * stripped.
6762 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6765 /* ouch: when addriver and setdriver are called from within
6766 rpc_printer_migrate_drivers_internals, the printer-queue already
6767 *has* to exist */
6769 struct functable func[] = {
6771 "all",
6772 rpc_printer_migrate_all,
6773 NET_TRANSPORT_RPC,
6774 N_("Migrate all from remote to local print server"),
6775 N_("net rpc printer migrate all\n"
6776 " Migrate all from remote to local print server")
6779 "drivers",
6780 rpc_printer_migrate_drivers,
6781 NET_TRANSPORT_RPC,
6782 N_("Migrate drivers to local server"),
6783 N_("net rpc printer migrate drivers\n"
6784 " Migrate drivers to local server")
6787 "forms",
6788 rpc_printer_migrate_forms,
6789 NET_TRANSPORT_RPC,
6790 N_("Migrate froms to local server"),
6791 N_("net rpc printer migrate forms\n"
6792 " Migrate froms to local server")
6795 "printers",
6796 rpc_printer_migrate_printers,
6797 NET_TRANSPORT_RPC,
6798 N_("Migrate printers to local server"),
6799 N_("net rpc printer migrate printers\n"
6800 " Migrate printers to local server")
6803 "security",
6804 rpc_printer_migrate_security,
6805 NET_TRANSPORT_RPC,
6806 N_("Mirgate printer ACLs to local server"),
6807 N_("net rpc printer migrate security\n"
6808 " Mirgate printer ACLs to local server")
6811 "settings",
6812 rpc_printer_migrate_settings,
6813 NET_TRANSPORT_RPC,
6814 N_("Migrate printer settings to local server"),
6815 N_("net rpc printer migrate settings\n"
6816 " Migrate printer settings to local server")
6818 {NULL, NULL, 0, NULL, NULL}
6821 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6826 * List printers on a remote RPC server.
6828 * @param c A net_context structure.
6829 * @param argc Standard main() style argc.
6830 * @param argv Standard main() style argv. Initial components are already
6831 * stripped.
6833 * @return A shell status integer (0 for success).
6835 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6837 if (c->display_usage) {
6838 d_printf( "%s\n"
6839 "net rpc printer list\n"
6840 " %s\n",
6841 _("Usage:"),
6842 _("List printers on a remote RPC server"));
6843 return 0;
6846 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6847 rpc_printer_list_internals,
6848 argc, argv);
6852 * List printer-drivers on a remote RPC server.
6854 * @param c A net_context structure.
6855 * @param argc Standard main() style argc.
6856 * @param argv Standard main() style argv. Initial components are already
6857 * stripped.
6859 * @return A shell status integer (0 for success).
6861 static int rpc_printer_driver_list(struct net_context *c, int argc,
6862 const char **argv)
6864 if (c->display_usage) {
6865 d_printf( "%s\n"
6866 "net rpc printer driver\n"
6867 " %s\n",
6868 _("Usage:"),
6869 _("List printer-drivers on a remote RPC server"));
6870 return 0;
6873 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6874 rpc_printer_driver_list_internals,
6875 argc, argv);
6879 * Publish printer in ADS via MSRPC.
6881 * @param c A net_context structure.
6882 * @param argc Standard main() style argc.
6883 * @param argv Standard main() style argv. Initial components are already
6884 * stripped.
6886 * @return A shell status integer (0 for success).
6888 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6889 const char **argv)
6891 if (c->display_usage) {
6892 d_printf( "%s\n"
6893 "net rpc printer publish publish\n"
6894 " %s\n",
6895 _("Usage:"),
6896 _("Publish printer in ADS via MSRPC"));
6897 return 0;
6900 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6901 rpc_printer_publish_publish_internals,
6902 argc, argv);
6906 * Update printer in ADS via MSRPC.
6908 * @param c A net_context structure.
6909 * @param argc Standard main() style argc.
6910 * @param argv Standard main() style argv. Initial components are already
6911 * stripped.
6913 * @return A shell status integer (0 for success).
6915 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6917 if (c->display_usage) {
6918 d_printf( "%s\n"
6919 "net rpc printer publish update\n"
6920 " %s\n",
6921 _("Usage:"),
6922 _("Update printer in ADS via MSRPC"));
6923 return 0;
6926 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6927 rpc_printer_publish_update_internals,
6928 argc, argv);
6932 * UnPublish printer in ADS via MSRPC.
6934 * @param c A net_context structure.
6935 * @param argc Standard main() style argc.
6936 * @param argv Standard main() style argv. Initial components are already
6937 * stripped.
6939 * @return A shell status integer (0 for success).
6941 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
6942 const char **argv)
6944 if (c->display_usage) {
6945 d_printf( "%s\n"
6946 "net rpc printer publish unpublish\n"
6947 " %s\n",
6948 _("Usage:\n"),
6949 _("UnPublish printer in ADS via MSRPC"));
6950 return 0;
6953 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6954 rpc_printer_publish_unpublish_internals,
6955 argc, argv);
6959 * List published printers via MSRPC.
6961 * @param c A net_context structure.
6962 * @param argc Standard main() style argc.
6963 * @param argv Standard main() style argv. Initial components are already
6964 * stripped.
6966 * @return A shell status integer (0 for success).
6968 static int rpc_printer_publish_list(struct net_context *c, int argc,
6969 const char **argv)
6971 if (c->display_usage) {
6972 d_printf( "%s\n"
6973 "net rpc printer publish list\n"
6974 " %s\n",
6975 _("Usage:"),
6976 _("List published printers via MSRPC"));
6977 return 0;
6980 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6981 rpc_printer_publish_list_internals,
6982 argc, argv);
6987 * Publish printer in ADS.
6989 * @param c A net_context structure.
6990 * @param argc Standard main() style argc.
6991 * @param argv Standard main() style argv. Initial components are already
6992 * stripped.
6994 * @return A shell status integer (0 for success).
6996 static int rpc_printer_publish(struct net_context *c, int argc,
6997 const char **argv)
7000 struct functable func[] = {
7002 "publish",
7003 rpc_printer_publish_publish,
7004 NET_TRANSPORT_RPC,
7005 N_("Publish printer in AD"),
7006 N_("net rpc printer publish publish\n"
7007 " Publish printer in AD")
7010 "update",
7011 rpc_printer_publish_update,
7012 NET_TRANSPORT_RPC,
7013 N_("Update printer in AD"),
7014 N_("net rpc printer publish update\n"
7015 " Update printer in AD")
7018 "unpublish",
7019 rpc_printer_publish_unpublish,
7020 NET_TRANSPORT_RPC,
7021 N_("Unpublish printer"),
7022 N_("net rpc printer publish unpublish\n"
7023 " Unpublish printer")
7026 "list",
7027 rpc_printer_publish_list,
7028 NET_TRANSPORT_RPC,
7029 N_("List published printers"),
7030 N_("net rpc printer publish list\n"
7031 " List published printers")
7033 {NULL, NULL, 0, NULL, NULL}
7036 if (argc == 0) {
7037 if (c->display_usage) {
7038 d_printf(_("Usage:\n"));
7039 d_printf(_("net rpc printer publish\n"
7040 " List published printers\n"
7041 " Alias of net rpc printer publish "
7042 "list\n"));
7043 net_display_usage_from_functable(func);
7044 return 0;
7046 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7047 rpc_printer_publish_list_internals,
7048 argc, argv);
7051 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7057 * Display rpc printer help page.
7059 * @param c A net_context structure.
7060 * @param argc Standard main() style argc.
7061 * @param argv Standard main() style argv. Initial components are already
7062 * stripped.
7064 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7066 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7067 "\tlists all printers on print-server\n\n"));
7068 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7069 "\tlists all printer-drivers on print-server\n\n"));
7070 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7071 "\tpublishes printer settings in Active Directory\n"
7072 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7073 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7074 "\n\tmigrates printers from remote to local server\n\n"));
7075 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7076 "\n\tmigrates printer-settings from remote to local server\n\n"));
7077 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7078 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7079 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7080 "\n\tmigrates printer-forms from remote to local server\n\n"));
7081 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7082 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7083 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7084 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7085 "\tremote to local print-server\n\n"));
7086 net_common_methods_usage(c, argc, argv);
7087 net_common_flags_usage(c, argc, argv);
7088 d_printf(_(
7089 "\t-v or --verbose\t\t\tgive verbose output\n"
7090 "\t --destination\t\tmigration target server (default: localhost)\n"));
7092 return -1;
7096 * 'net rpc printer' entrypoint.
7098 * @param c A net_context structure.
7099 * @param argc Standard main() style argc.
7100 * @param argv Standard main() style argv. Initial components are already
7101 * stripped.
7103 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7105 struct functable func[] = {
7107 "list",
7108 rpc_printer_list,
7109 NET_TRANSPORT_RPC,
7110 N_("List all printers on print server"),
7111 N_("net rpc printer list\n"
7112 " List all printers on print server")
7115 "migrate",
7116 rpc_printer_migrate,
7117 NET_TRANSPORT_RPC,
7118 N_("Migrate printer to local server"),
7119 N_("net rpc printer migrate\n"
7120 " Migrate printer to local server")
7123 "driver",
7124 rpc_printer_driver_list,
7125 NET_TRANSPORT_RPC,
7126 N_("List printer drivers"),
7127 N_("net rpc printer driver\n"
7128 " List printer drivers")
7131 "publish",
7132 rpc_printer_publish,
7133 NET_TRANSPORT_RPC,
7134 N_("Publish printer in AD"),
7135 N_("net rpc printer publish\n"
7136 " Publish printer in AD")
7138 {NULL, NULL, 0, NULL, NULL}
7141 if (argc == 0) {
7142 if (c->display_usage) {
7143 d_printf(_("Usage:\n"));
7144 d_printf(_("net rpc printer\n"
7145 " List printers\n"));
7146 net_display_usage_from_functable(func);
7147 return 0;
7149 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7150 rpc_printer_list_internals,
7151 argc, argv);
7154 return net_run_function(c, argc, argv, "net rpc printer", func);
7158 * 'net rpc' entrypoint.
7160 * @param c A net_context structure.
7161 * @param argc Standard main() style argc.
7162 * @param argv Standard main() style argv. Initial components are already
7163 * stripped.
7166 int net_rpc(struct net_context *c, int argc, const char **argv)
7168 NET_API_STATUS status;
7170 struct functable func[] = {
7172 "audit",
7173 net_rpc_audit,
7174 NET_TRANSPORT_RPC,
7175 N_("Modify global audit settings"),
7176 N_("net rpc audit\n"
7177 " Modify global audit settings")
7180 "info",
7181 net_rpc_info,
7182 NET_TRANSPORT_RPC,
7183 N_("Show basic info about a domain"),
7184 N_("net rpc info\n"
7185 " Show basic info about a domain")
7188 "join",
7189 net_rpc_join,
7190 NET_TRANSPORT_RPC,
7191 N_("Join a domain"),
7192 N_("net rpc join\n"
7193 " Join a domain")
7196 "oldjoin",
7197 net_rpc_oldjoin,
7198 NET_TRANSPORT_RPC,
7199 N_("Join a domain created in server manager"),
7200 N_("net rpc oldjoin\n"
7201 " Join a domain created in server manager")
7204 "testjoin",
7205 net_rpc_testjoin,
7206 NET_TRANSPORT_RPC,
7207 N_("Test that a join is valid"),
7208 N_("net rpc testjoin\n"
7209 " Test that a join is valid")
7212 "user",
7213 net_rpc_user,
7214 NET_TRANSPORT_RPC,
7215 N_("List/modify users"),
7216 N_("net rpc user\n"
7217 " List/modify users")
7220 "password",
7221 rpc_user_password,
7222 NET_TRANSPORT_RPC,
7223 N_("Change a user password"),
7224 N_("net rpc password\n"
7225 " Change a user password\n"
7226 " Alias for net rpc user password")
7229 "group",
7230 net_rpc_group,
7231 NET_TRANSPORT_RPC,
7232 N_("List/modify groups"),
7233 N_("net rpc group\n"
7234 " List/modify groups")
7237 "share",
7238 net_rpc_share,
7239 NET_TRANSPORT_RPC,
7240 N_("List/modify shares"),
7241 N_("net rpc share\n"
7242 " List/modify shares")
7245 "file",
7246 net_rpc_file,
7247 NET_TRANSPORT_RPC,
7248 N_("List open files"),
7249 N_("net rpc file\n"
7250 " List open files")
7253 "printer",
7254 net_rpc_printer,
7255 NET_TRANSPORT_RPC,
7256 N_("List/modify printers"),
7257 N_("net rpc printer\n"
7258 " List/modify printers")
7261 "changetrustpw",
7262 net_rpc_changetrustpw,
7263 NET_TRANSPORT_RPC,
7264 N_("Change trust account password"),
7265 N_("net rpc changetrustpw\n"
7266 " Change trust account password")
7269 "trustdom",
7270 rpc_trustdom,
7271 NET_TRANSPORT_RPC,
7272 N_("Modify domain trusts"),
7273 N_("net rpc trustdom\n"
7274 " Modify domain trusts")
7277 "abortshutdown",
7278 rpc_shutdown_abort,
7279 NET_TRANSPORT_RPC,
7280 N_("Abort a remote shutdown"),
7281 N_("net rpc abortshutdown\n"
7282 " Abort a remote shutdown")
7285 "shutdown",
7286 rpc_shutdown,
7287 NET_TRANSPORT_RPC,
7288 N_("Shutdown a remote server"),
7289 N_("net rpc shutdown\n"
7290 " Shutdown a remote server")
7293 "samdump",
7294 rpc_samdump,
7295 NET_TRANSPORT_RPC,
7296 N_("Dump SAM data of remote NT PDC"),
7297 N_("net rpc samdump\n"
7298 " Dump SAM data of remote NT PDC")
7301 "vampire",
7302 rpc_vampire,
7303 NET_TRANSPORT_RPC,
7304 N_("Sync a remote NT PDC's data into local passdb"),
7305 N_("net rpc vampire\n"
7306 " Sync a remote NT PDC's data into local passdb")
7309 "getsid",
7310 net_rpc_getsid,
7311 NET_TRANSPORT_RPC,
7312 N_("Fetch the domain sid into local secrets.tdb"),
7313 N_("net rpc getsid\n"
7314 " Fetch the domain sid into local secrets.tdb")
7317 "rights",
7318 net_rpc_rights,
7319 NET_TRANSPORT_RPC,
7320 N_("Manage privileges assigned to SID"),
7321 N_("net rpc rights\n"
7322 " Manage privileges assigned to SID")
7325 "service",
7326 net_rpc_service,
7327 NET_TRANSPORT_RPC,
7328 N_("Start/stop/query remote services"),
7329 N_("net rpc service\n"
7330 " Start/stop/query remote services")
7333 "registry",
7334 net_rpc_registry,
7335 NET_TRANSPORT_RPC,
7336 N_("Manage registry hives"),
7337 N_("net rpc registry\n"
7338 " Manage registry hives")
7341 "shell",
7342 net_rpc_shell,
7343 NET_TRANSPORT_RPC,
7344 N_("Open interactive shell on remote server"),
7345 N_("net rpc shell\n"
7346 " Open interactive shell on remote server")
7348 {NULL, NULL, 0, NULL, NULL}
7351 status = libnetapi_init(&c->netapi_ctx);
7352 if (status != 0) {
7353 return -1;
7355 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
7356 libnetapi_set_password(c->netapi_ctx, c->opt_password);
7357 if (c->opt_kerberos) {
7358 libnetapi_set_use_kerberos(c->netapi_ctx);
7360 if (c->opt_ccache) {
7361 libnetapi_set_use_ccache(c->netapi_ctx);
7364 return net_run_function(c, argc, argv, "net rpc", func);