lib/dbwrap: talloc_strdup() name in db_open_file()
[Samba/gebeck_regimport.git] / source3 / utils / net_rpc.c
blobc112a15e7635c6df5d431a82fc26e19a5e7ffabf
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2004,2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "includes.h"
24 #include "utils/net.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_samr_c.h"
28 #include "rpc_client/cli_samr.h"
29 #include "rpc_client/init_samr.h"
30 #include "../librpc/gen_ndr/ndr_lsa_c.h"
31 #include "rpc_client/cli_lsarpc.h"
32 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
33 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
34 #include "../librpc/gen_ndr/ndr_spoolss.h"
35 #include "../librpc/gen_ndr/ndr_initshutdown_c.h"
36 #include "../librpc/gen_ndr/ndr_winreg_c.h"
37 #include "secrets.h"
38 #include "lib/netapi/netapi.h"
39 #include "lib/netapi/netapi_net.h"
40 #include "rpc_client/init_lsa.h"
41 #include "../libcli/security/security.h"
42 #include "libsmb/libsmb.h"
43 #include "libsmb/clirap.h"
44 #include "nsswitch/libwbclient/wbclient.h"
45 #include "passdb.h"
46 #include "../libcli/smb/smbXcli_base.h"
48 static int net_mode_share;
49 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
51 /**
52 * @file net_rpc.c
54 * @brief RPC based subcommands for the 'net' utility.
56 * This file should contain much of the functionality that used to
57 * be found in rpcclient, execpt that the commands should change
58 * less often, and the fucntionality should be sane (the user is not
59 * expected to know a rid/sid before they conduct an operation etc.)
61 * @todo Perhaps eventually these should be split out into a number
62 * of files, as this could get quite big.
63 **/
66 /**
67 * Many of the RPC functions need the domain sid. This function gets
68 * it at the start of every run
70 * @param cli A cli_state already connected to the remote machine
72 * @return The Domain SID of the remote machine.
73 **/
75 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
76 struct dom_sid **domain_sid,
77 const char **domain_name)
79 struct rpc_pipe_client *lsa_pipe = NULL;
80 struct policy_handle pol;
81 NTSTATUS status, result;
82 union lsa_PolicyInformation *info = NULL;
83 struct dcerpc_binding_handle *b;
85 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
86 &lsa_pipe);
87 if (!NT_STATUS_IS_OK(status)) {
88 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
89 return status;
92 b = lsa_pipe->binding_handle;
94 status = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
95 SEC_FLAG_MAXIMUM_ALLOWED,
96 &pol);
97 if (!NT_STATUS_IS_OK(status)) {
98 d_fprintf(stderr, "open_policy %s: %s\n",
99 _("failed"),
100 nt_errstr(status));
101 return status;
104 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
105 &pol,
106 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
107 &info,
108 &result);
109 if (!NT_STATUS_IS_OK(status)) {
110 d_fprintf(stderr, "lsaquery %s: %s\n",
111 _("failed"),
112 nt_errstr(status));
113 return status;
115 if (!NT_STATUS_IS_OK(result)) {
116 d_fprintf(stderr, "lsaquery %s: %s\n",
117 _("failed"),
118 nt_errstr(result));
119 return result;
122 *domain_name = info->account_domain.name.string;
123 *domain_sid = info->account_domain.sid;
125 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
126 TALLOC_FREE(lsa_pipe);
128 return NT_STATUS_OK;
132 * Run a single RPC command, from start to finish.
134 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
135 * @param conn_flag a NET_FLAG_ combination. Passed to
136 * net_make_ipc_connection.
137 * @param argc Standard main() style argc.
138 * @param argv Standard main() style argv. Initial components are already
139 * stripped.
140 * @return A shell status integer (0 for success).
143 int run_rpc_command(struct net_context *c,
144 struct cli_state *cli_arg,
145 const struct ndr_interface_table *table,
146 int conn_flags,
147 rpc_command_fn fn,
148 int argc,
149 const char **argv)
151 struct cli_state *cli = NULL;
152 struct rpc_pipe_client *pipe_hnd = NULL;
153 TALLOC_CTX *mem_ctx;
154 NTSTATUS nt_status;
155 struct dom_sid *domain_sid;
156 const char *domain_name;
157 int ret = -1;
159 /* make use of cli_state handed over as an argument, if possible */
160 if (!cli_arg) {
161 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
162 if (!NT_STATUS_IS_OK(nt_status)) {
163 DEBUG(1, ("failed to make ipc connection: %s\n",
164 nt_errstr(nt_status)));
165 return -1;
167 } else {
168 cli = cli_arg;
171 if (!cli) {
172 return -1;
175 /* Create mem_ctx */
177 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
178 DEBUG(0, ("talloc_init() failed\n"));
179 goto fail;
182 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
183 &domain_name);
184 if (!NT_STATUS_IS_OK(nt_status)) {
185 goto fail;
188 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
189 if (lp_client_schannel()
190 && (ndr_syntax_id_equal(&table->syntax_id,
191 &ndr_table_netlogon.syntax_id))) {
192 /* Always try and create an schannel netlogon pipe. */
193 nt_status = cli_rpc_pipe_open_schannel(
194 cli, &table->syntax_id, NCACN_NP,
195 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
196 &pipe_hnd);
197 if (!NT_STATUS_IS_OK(nt_status)) {
198 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
199 nt_errstr(nt_status) ));
200 goto fail;
202 } else {
203 if (conn_flags & NET_FLAGS_SEAL) {
204 nt_status = cli_rpc_pipe_open_generic_auth(
205 cli, table,
206 (conn_flags & NET_FLAGS_TCP) ?
207 NCACN_IP_TCP : NCACN_NP,
208 DCERPC_AUTH_TYPE_NTLMSSP,
209 DCERPC_AUTH_LEVEL_PRIVACY,
210 smbXcli_conn_remote_name(cli->conn),
211 lp_workgroup(), c->opt_user_name,
212 c->opt_password, &pipe_hnd);
213 } else {
214 nt_status = cli_rpc_pipe_open_noauth(
215 cli, &table->syntax_id,
216 &pipe_hnd);
218 if (!NT_STATUS_IS_OK(nt_status)) {
219 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
220 table->name,
221 nt_errstr(nt_status) ));
222 goto fail;
227 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
229 if (!NT_STATUS_IS_OK(nt_status)) {
230 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
231 } else {
232 ret = 0;
233 DEBUG(5, ("rpc command function succedded\n"));
236 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
237 if (pipe_hnd) {
238 TALLOC_FREE(pipe_hnd);
242 fail:
243 /* close the connection only if it was opened here */
244 if (!cli_arg) {
245 cli_shutdown(cli);
248 talloc_destroy(mem_ctx);
249 return ret;
253 * Force a change of the trust acccount password.
255 * All parameters are provided by the run_rpc_command function, except for
256 * argc, argv which are passed through.
258 * @param domain_sid The domain sid acquired from the remote server.
259 * @param cli A cli_state connected to the server.
260 * @param mem_ctx Talloc context, destroyed on completion of the function.
261 * @param argc Standard main() style argc.
262 * @param argv Standard main() style argv. Initial components are already
263 * stripped.
265 * @return Normal NTSTATUS return.
268 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
269 const struct dom_sid *domain_sid,
270 const char *domain_name,
271 struct cli_state *cli,
272 struct rpc_pipe_client *pipe_hnd,
273 TALLOC_CTX *mem_ctx,
274 int argc,
275 const char **argv)
277 NTSTATUS status;
279 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
280 if (!NT_STATUS_IS_OK(status)) {
281 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
282 nt_errstr(status));
283 return status;
286 return NT_STATUS_OK;
290 * Force a change of the trust acccount password.
292 * @param argc Standard main() style argc.
293 * @param argv Standard main() style argv. Initial components are already
294 * stripped.
296 * @return A shell status integer (0 for success).
299 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
301 if (c->display_usage) {
302 d_printf( "%s\n"
303 "net rpc changetrustpw\n"
304 " %s\n",
305 _("Usage:"),
306 _("Change the machine trust password"));
307 return 0;
310 return run_rpc_command(c, NULL, &ndr_table_netlogon,
311 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
312 rpc_changetrustpw_internals,
313 argc, argv);
317 * Join a domain, the old way.
319 * This uses 'machinename' as the inital password, and changes it.
321 * The password should be created with 'server manager' or equiv first.
323 * All parameters are provided by the run_rpc_command function, except for
324 * argc, argv which are passed through.
326 * @param domain_sid The domain sid acquired from the remote server.
327 * @param cli A cli_state connected to the server.
328 * @param mem_ctx Talloc context, destroyed on completion of the function.
329 * @param argc Standard main() style argc.
330 * @param argv Standard main() style argv. Initial components are already
331 * stripped.
333 * @return Normal NTSTATUS return.
336 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
337 const struct dom_sid *domain_sid,
338 const char *domain_name,
339 struct cli_state *cli,
340 struct rpc_pipe_client *pipe_hnd,
341 TALLOC_CTX *mem_ctx,
342 int argc,
343 const char **argv)
346 fstring trust_passwd;
347 unsigned char orig_trust_passwd_hash[16];
348 NTSTATUS result;
349 enum netr_SchannelType sec_channel_type;
351 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
352 &pipe_hnd);
353 if (!NT_STATUS_IS_OK(result)) {
354 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
355 "error was %s\n",
356 smbXcli_conn_remote_name(cli->conn),
357 nt_errstr(result) ));
358 return result;
362 check what type of join - if the user want's to join as
363 a BDC, the server must agree that we are a BDC.
365 if (argc >= 0) {
366 sec_channel_type = get_sec_channel_type(argv[0]);
367 } else {
368 sec_channel_type = get_sec_channel_type(NULL);
371 fstrcpy(trust_passwd, lp_netbios_name());
372 if (!strlower_m(trust_passwd)) {
373 return NT_STATUS_UNSUCCESSFUL;
377 * Machine names can be 15 characters, but the max length on
378 * a password is 14. --jerry
381 trust_passwd[14] = '\0';
383 E_md4hash(trust_passwd, orig_trust_passwd_hash);
385 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
386 lp_netbios_name(),
387 orig_trust_passwd_hash,
388 sec_channel_type);
390 if (NT_STATUS_IS_OK(result))
391 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
394 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
395 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
396 result = NT_STATUS_UNSUCCESSFUL;
399 return result;
403 * Join a domain, the old way.
405 * @param argc Standard main() style argc.
406 * @param argv Standard main() style argv. Initial components are already
407 * stripped.
409 * @return A shell status integer (0 for success).
412 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
414 return run_rpc_command(c, NULL, &ndr_table_netlogon,
415 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
416 rpc_oldjoin_internals,
417 argc, argv);
421 * Join a domain, the old way. This function exists to allow
422 * the message to be displayed when oldjoin was explicitly
423 * requested, but not when it was implied by "net rpc join".
425 * @param argc Standard main() style argc.
426 * @param argv Standard main() style argv. Initial components are already
427 * stripped.
429 * @return A shell status integer (0 for success).
432 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
434 int rc = -1;
436 if (c->display_usage) {
437 d_printf( "%s\n"
438 "net rpc oldjoin\n"
439 " %s\n",
440 _("Usage:"),
441 _("Join a domain the old way"));
442 return 0;
445 rc = net_rpc_perform_oldjoin(c, argc, argv);
447 if (rc) {
448 d_fprintf(stderr, _("Failed to join domain\n"));
451 return rc;
455 * 'net rpc join' entrypoint.
456 * @param argc Standard main() style argc.
457 * @param argv Standard main() style argv. Initial components are already
458 * stripped
460 * Main 'net_rpc_join()' (where the admin username/password is used) is
461 * in net_rpc_join.c.
462 * Try to just change the password, but if that doesn't work, use/prompt
463 * for a username/password.
466 int net_rpc_join(struct net_context *c, int argc, const char **argv)
468 if (c->display_usage) {
469 d_printf("%s\n%s",
470 _("Usage:"),
471 _("net rpc join -U <username>[%%password] <type>\n"
472 " Join a domain\n"
473 " username\tName of the admin user"
474 " password\tPassword of the admin user, will "
475 "prompt if not specified\n"
476 " type\tCan be one of the following:\n"
477 "\t\tMEMBER\tJoin as member server (default)\n"
478 "\t\tBDC\tJoin as BDC\n"
479 "\t\tPDC\tJoin as PDC\n"));
480 return 0;
483 if (lp_server_role() == ROLE_STANDALONE) {
484 d_printf(_("cannot join as standalone machine\n"));
485 return -1;
488 if (strlen(lp_netbios_name()) > 15) {
489 d_printf(_("Our netbios name can be at most 15 chars long, "
490 "\"%s\" is %u chars long\n"),
491 lp_netbios_name(), (unsigned int)strlen(lp_netbios_name()));
492 return -1;
495 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
496 return 0;
498 return net_rpc_join_newstyle(c, argc, argv);
502 * display info about a rpc domain
504 * All parameters are provided by the run_rpc_command function, except for
505 * argc, argv which are passed through.
507 * @param domain_sid The domain sid acquired from the remote server
508 * @param cli A cli_state connected to the server.
509 * @param mem_ctx Talloc context, destroyed on completion of the function.
510 * @param argc Standard main() style argc.
511 * @param argv Standard main() style argv. Initial components are already
512 * stripped.
514 * @return Normal NTSTATUS return.
517 NTSTATUS rpc_info_internals(struct net_context *c,
518 const struct dom_sid *domain_sid,
519 const char *domain_name,
520 struct cli_state *cli,
521 struct rpc_pipe_client *pipe_hnd,
522 TALLOC_CTX *mem_ctx,
523 int argc,
524 const char **argv)
526 struct policy_handle connect_pol, domain_pol;
527 NTSTATUS status, result;
528 union samr_DomainInfo *info = NULL;
529 fstring sid_str;
530 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
532 sid_to_fstring(sid_str, domain_sid);
534 /* Get sam policy handle */
535 status = dcerpc_samr_Connect2(b, mem_ctx,
536 pipe_hnd->desthost,
537 MAXIMUM_ALLOWED_ACCESS,
538 &connect_pol,
539 &result);
540 if (!NT_STATUS_IS_OK(status)) {
541 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
542 nt_errstr(status));
543 goto done;
546 if (!NT_STATUS_IS_OK(result)) {
547 status = result;
548 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
549 nt_errstr(result));
550 goto done;
553 /* Get domain policy handle */
554 status = dcerpc_samr_OpenDomain(b, mem_ctx,
555 &connect_pol,
556 MAXIMUM_ALLOWED_ACCESS,
557 discard_const_p(struct dom_sid2, domain_sid),
558 &domain_pol,
559 &result);
560 if (!NT_STATUS_IS_OK(status)) {
561 d_fprintf(stderr, _("Could not open domain: %s\n"),
562 nt_errstr(status));
563 goto done;
565 if (!NT_STATUS_IS_OK(result)) {
566 status = result;
567 d_fprintf(stderr, _("Could not open domain: %s\n"),
568 nt_errstr(result));
569 goto done;
572 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
573 &domain_pol,
575 &info,
576 &result);
577 if (!NT_STATUS_IS_OK(status)) {
578 goto done;
580 status = result;
581 if (NT_STATUS_IS_OK(result)) {
582 d_printf(_("Domain Name: %s\n"),
583 info->general.domain_name.string);
584 d_printf(_("Domain SID: %s\n"), sid_str);
585 d_printf(_("Sequence number: %llu\n"),
586 (unsigned long long)info->general.sequence_num);
587 d_printf(_("Num users: %u\n"), info->general.num_users);
588 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
589 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
592 done:
593 return status;
597 * 'net rpc info' entrypoint.
598 * @param argc Standard main() style argc.
599 * @param argv Standard main() style argv. Initial components are already
600 * stripped.
603 int net_rpc_info(struct net_context *c, int argc, const char **argv)
605 if (c->display_usage) {
606 d_printf( "%s\n"
607 "net rpc info\n"
608 " %s\n",
609 _("Usage:"),
610 _("Display information about the domain"));
611 return 0;
614 return run_rpc_command(c, NULL, &ndr_table_samr,
615 NET_FLAGS_PDC, rpc_info_internals,
616 argc, argv);
620 * Fetch domain SID into the local secrets.tdb.
622 * All parameters are provided by the run_rpc_command function, except for
623 * argc, argv which are passed through.
625 * @param domain_sid The domain sid acquired from the remote server.
626 * @param cli A cli_state connected to the server.
627 * @param mem_ctx Talloc context, destroyed on completion of the function.
628 * @param argc Standard main() style argc.
629 * @param argv Standard main() style argv. Initial components are already
630 * stripped.
632 * @return Normal NTSTATUS return.
635 static NTSTATUS rpc_getsid_internals(struct net_context *c,
636 const struct dom_sid *domain_sid,
637 const char *domain_name,
638 struct cli_state *cli,
639 struct rpc_pipe_client *pipe_hnd,
640 TALLOC_CTX *mem_ctx,
641 int argc,
642 const char **argv)
644 fstring sid_str;
646 sid_to_fstring(sid_str, domain_sid);
647 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
648 sid_str, domain_name);
650 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
651 DEBUG(0,("Can't store domain SID\n"));
652 return NT_STATUS_UNSUCCESSFUL;
655 return NT_STATUS_OK;
659 * 'net rpc getsid' entrypoint.
660 * @param argc Standard main() style argc.
661 * @param argv Standard main() style argv. Initial components are already
662 * stripped.
665 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
667 int conn_flags = NET_FLAGS_PDC;
669 if (!c->opt_user_specified) {
670 conn_flags |= NET_FLAGS_ANONYMOUS;
673 if (c->display_usage) {
674 d_printf( "%s\n"
675 "net rpc getsid\n"
676 " %s\n",
677 _("Usage:"),
678 _("Fetch domain SID into local secrets.tdb"));
679 return 0;
682 return run_rpc_command(c, NULL, &ndr_table_samr,
683 conn_flags,
684 rpc_getsid_internals,
685 argc, argv);
688 /****************************************************************************/
691 * Basic usage function for 'net rpc user'.
692 * @param argc Standard main() style argc.
693 * @param argv Standard main() style argv. Initial components are already
694 * stripped.
697 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
699 return net_user_usage(c, argc, argv);
703 * Add a new user to a remote RPC server.
705 * @param argc Standard main() style argc.
706 * @param argv Standard main() style argv. Initial components are already
707 * stripped.
709 * @return A shell status integer (0 for success).
712 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
714 NET_API_STATUS status;
715 struct USER_INFO_1 info1;
716 uint32_t parm_error = 0;
718 if (argc < 1 || c->display_usage) {
719 rpc_user_usage(c, argc, argv);
720 return 0;
723 ZERO_STRUCT(info1);
725 info1.usri1_name = argv[0];
726 if (argc == 2) {
727 info1.usri1_password = argv[1];
730 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
732 if (status != 0) {
733 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
734 argv[0], libnetapi_get_error_string(c->netapi_ctx,
735 status));
736 return -1;
737 } else {
738 d_printf(_("Added user '%s'.\n"), argv[0]);
741 return 0;
745 * Rename a user on a remote RPC server.
747 * @param argc Standard main() style argc.
748 * @param argv Standard main() style argv. Initial components are already
749 * stripped.
751 * @return A shell status integer (0 for success).
754 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
756 NET_API_STATUS status;
757 struct USER_INFO_0 u0;
758 uint32_t parm_err = 0;
760 if (argc != 2 || c->display_usage) {
761 rpc_user_usage(c, argc, argv);
762 return 0;
765 u0.usri0_name = argv[1];
767 status = NetUserSetInfo(c->opt_host, argv[0],
768 0, (uint8_t *)&u0, &parm_err);
769 if (status) {
770 d_fprintf(stderr,
771 _("Failed to rename user from %s to %s - %s\n"),
772 argv[0], argv[1],
773 libnetapi_get_error_string(c->netapi_ctx, status));
774 } else {
775 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
778 return status;
782 * Set a user's primary group
784 * @param argc Standard main() style argc.
785 * @param argv Standard main() style argv. Initial components are already
786 * stripped.
788 * @return A shell status integer (0 for success).
791 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
792 const char **argv)
794 NET_API_STATUS status;
795 uint8_t *buffer;
796 struct GROUP_INFO_2 *g2;
797 struct USER_INFO_1051 u1051;
798 uint32_t parm_err = 0;
800 if (argc != 2 || c->display_usage) {
801 rpc_user_usage(c, argc, argv);
802 return 0;
805 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
806 if (status) {
807 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
808 argv[1],
809 libnetapi_get_error_string(c->netapi_ctx, status));
810 return status;
812 g2 = (struct GROUP_INFO_2 *)buffer;
814 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
816 NetApiBufferFree(buffer);
818 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
819 (uint8_t *)&u1051, &parm_err);
820 if (status) {
821 d_fprintf(stderr,
822 _("Failed to set user's primary group %s to %s - "
823 "%s\n"), argv[0], argv[1],
824 libnetapi_get_error_string(c->netapi_ctx, status));
825 } else {
826 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
827 argv[1]);
829 return status;
833 * Delete a user from a remote RPC server.
835 * @param argc Standard main() style argc.
836 * @param argv Standard main() style argv. Initial components are already
837 * stripped.
839 * @return A shell status integer (0 for success).
842 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
844 NET_API_STATUS status;
846 if (argc < 1 || c->display_usage) {
847 rpc_user_usage(c, argc, argv);
848 return 0;
851 status = NetUserDel(c->opt_host, argv[0]);
853 if (status != 0) {
854 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
855 argv[0],
856 libnetapi_get_error_string(c->netapi_ctx, status));
857 return -1;
858 } else {
859 d_printf(_("Deleted user '%s'.\n"), argv[0]);
862 return 0;
866 * Set a user's password on a remote RPC server.
868 * @param argc Standard main() style argc.
869 * @param argv Standard main() style argv. Initial components are already
870 * stripped.
872 * @return A shell status integer (0 for success).
875 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
877 NET_API_STATUS status;
878 char *prompt = NULL;
879 struct USER_INFO_1003 u1003;
880 uint32_t parm_err = 0;
881 int ret;
883 if (argc < 1 || c->display_usage) {
884 rpc_user_usage(c, argc, argv);
885 return 0;
888 if (argv[1]) {
889 u1003.usri1003_password = argv[1];
890 } else {
891 char pwd[256] = {0};
892 ret = asprintf(&prompt, _("Enter new password for %s:"),
893 argv[0]);
894 if (ret == -1) {
895 return -1;
898 ret = samba_getpass(prompt, pwd, sizeof(pwd), false, false);
899 SAFE_FREE(prompt);
900 if (ret < 0) {
901 return -1;
904 u1003.usri1003_password = talloc_strdup(c, pwd);
905 if (u1003.usri1003_password == NULL) {
906 return -1;
910 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
912 /* Display results */
913 if (status != 0) {
914 d_fprintf(stderr,
915 _("Failed to set password for '%s' with error: %s.\n"),
916 argv[0], libnetapi_get_error_string(c->netapi_ctx,
917 status));
918 return -1;
921 return 0;
925 * List a user's groups from a remote RPC server.
927 * @param argc Standard main() style argc.
928 * @param argv Standard main() style argv. Initial components are already
929 * stripped.
931 * @return A shell status integer (0 for success)
934 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
937 NET_API_STATUS status;
938 struct GROUP_USERS_INFO_0 *u0 = NULL;
939 uint32_t entries_read = 0;
940 uint32_t total_entries = 0;
941 int i;
944 if (argc < 1 || c->display_usage) {
945 rpc_user_usage(c, argc, argv);
946 return 0;
949 status = NetUserGetGroups(c->opt_host,
950 argv[0],
952 (uint8_t **)(void *)&u0,
953 (uint32_t)-1,
954 &entries_read,
955 &total_entries);
956 if (status != 0) {
957 d_fprintf(stderr,
958 _("Failed to get groups for '%s' with error: %s.\n"),
959 argv[0], libnetapi_get_error_string(c->netapi_ctx,
960 status));
961 return -1;
964 for (i=0; i < entries_read; i++) {
965 printf("%s\n", u0->grui0_name);
966 u0++;
969 return 0;
973 * List users on a remote RPC server.
975 * All parameters are provided by the run_rpc_command function, except for
976 * argc, argv which are passed through.
978 * @param domain_sid The domain sid acquired from the remote server.
979 * @param cli A cli_state connected to the server.
980 * @param mem_ctx Talloc context, destroyed on completion of the function.
981 * @param argc Standard main() style argc.
982 * @param argv Standard main() style argv. Initial components are already
983 * stripped.
985 * @return Normal NTSTATUS return.
988 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
990 NET_API_STATUS status;
991 uint32_t start_idx=0, num_entries, i, loop_count = 0;
992 struct NET_DISPLAY_USER *info = NULL;
993 void *buffer = NULL;
995 /* Query domain users */
996 if (c->opt_long_list_entries)
997 d_printf(_("\nUser name Comment"
998 "\n-----------------------------\n"));
999 do {
1000 uint32_t max_entries, max_size;
1002 dcerpc_get_query_dispinfo_params(
1003 loop_count, &max_entries, &max_size);
1005 status = NetQueryDisplayInformation(c->opt_host,
1007 start_idx,
1008 max_entries,
1009 max_size,
1010 &num_entries,
1011 &buffer);
1012 if (status != 0 && status != ERROR_MORE_DATA) {
1013 return status;
1016 info = (struct NET_DISPLAY_USER *)buffer;
1018 for (i = 0; i < num_entries; i++) {
1020 if (c->opt_long_list_entries)
1021 printf("%-21.21s %s\n", info->usri1_name,
1022 info->usri1_comment);
1023 else
1024 printf("%s\n", info->usri1_name);
1025 info++;
1028 NetApiBufferFree(buffer);
1030 loop_count++;
1031 start_idx += num_entries;
1033 } while (status == ERROR_MORE_DATA);
1035 return status;
1039 * 'net rpc user' entrypoint.
1040 * @param argc Standard main() style argc.
1041 * @param argv Standard main() style argv. Initial components are already
1042 * stripped.
1045 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1047 NET_API_STATUS status;
1049 struct functable func[] = {
1051 "add",
1052 rpc_user_add,
1053 NET_TRANSPORT_RPC,
1054 N_("Add specified user"),
1055 N_("net rpc user add\n"
1056 " Add specified user")
1059 "info",
1060 rpc_user_info,
1061 NET_TRANSPORT_RPC,
1062 N_("List domain groups of user"),
1063 N_("net rpc user info\n"
1064 " List domain groups of user")
1067 "delete",
1068 rpc_user_delete,
1069 NET_TRANSPORT_RPC,
1070 N_("Remove specified user"),
1071 N_("net rpc user delete\n"
1072 " Remove specified user")
1075 "password",
1076 rpc_user_password,
1077 NET_TRANSPORT_RPC,
1078 N_("Change user password"),
1079 N_("net rpc user password\n"
1080 " Change user password")
1083 "rename",
1084 rpc_user_rename,
1085 NET_TRANSPORT_RPC,
1086 N_("Rename specified user"),
1087 N_("net rpc user rename\n"
1088 " Rename specified user")
1091 "setprimarygroup",
1092 rpc_user_setprimarygroup,
1093 NET_TRANSPORT_RPC,
1094 "Set a user's primary group",
1095 "net rpc user setprimarygroup\n"
1096 " Set a user's primary group"
1098 {NULL, NULL, 0, NULL, NULL}
1101 status = libnetapi_net_init(&c->netapi_ctx);
1102 if (status != 0) {
1103 return -1;
1105 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1106 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1107 if (c->opt_kerberos) {
1108 libnetapi_set_use_kerberos(c->netapi_ctx);
1111 if (argc == 0) {
1112 if (c->display_usage) {
1113 d_printf( "%s\n"
1114 "net rpc user\n"
1115 " %s\n",
1116 _("Usage:"),
1117 _("List all users"));
1118 net_display_usage_from_functable(func);
1119 return 0;
1122 return rpc_user_list(c, argc, argv);
1125 return net_run_function(c, argc, argv, "net rpc user", func);
1128 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1129 TALLOC_CTX *mem_ctx,
1130 struct rpc_sh_ctx *ctx,
1131 struct rpc_pipe_client *pipe_hnd,
1132 int argc, const char **argv)
1134 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1137 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1138 TALLOC_CTX *mem_ctx,
1139 struct rpc_sh_ctx *ctx,
1140 struct rpc_pipe_client *pipe_hnd,
1141 int argc, const char **argv)
1143 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1146 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1147 TALLOC_CTX *mem_ctx,
1148 struct rpc_sh_ctx *ctx,
1149 struct rpc_pipe_client *pipe_hnd,
1150 int argc, const char **argv,
1151 NTSTATUS (*fn)(
1152 struct net_context *c,
1153 TALLOC_CTX *mem_ctx,
1154 struct rpc_sh_ctx *ctx,
1155 struct rpc_pipe_client *pipe_hnd,
1156 struct policy_handle *user_hnd,
1157 int argc, const char **argv))
1159 struct policy_handle connect_pol, domain_pol, user_pol;
1160 NTSTATUS status, result;
1161 struct dom_sid sid;
1162 uint32 rid;
1163 enum lsa_SidType type;
1164 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1166 if (argc == 0) {
1167 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1168 ctx->whoami);
1169 return NT_STATUS_INVALID_PARAMETER;
1172 ZERO_STRUCT(connect_pol);
1173 ZERO_STRUCT(domain_pol);
1174 ZERO_STRUCT(user_pol);
1176 status = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1177 argv[0], NULL, NULL, &sid, &type);
1178 if (!NT_STATUS_IS_OK(status)) {
1179 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1180 nt_errstr(status));
1181 goto done;
1184 if (type != SID_NAME_USER) {
1185 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1186 sid_type_lookup(type));
1187 status = NT_STATUS_NO_SUCH_USER;
1188 goto done;
1191 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1192 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1193 status = NT_STATUS_NO_SUCH_USER;
1194 goto done;
1197 status = dcerpc_samr_Connect2(b, mem_ctx,
1198 pipe_hnd->desthost,
1199 MAXIMUM_ALLOWED_ACCESS,
1200 &connect_pol,
1201 &result);
1202 if (!NT_STATUS_IS_OK(status)) {
1203 goto done;
1205 if (!NT_STATUS_IS_OK(result)) {
1206 status = result;
1207 goto done;
1210 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1211 &connect_pol,
1212 MAXIMUM_ALLOWED_ACCESS,
1213 ctx->domain_sid,
1214 &domain_pol,
1215 &result);
1216 if (!NT_STATUS_IS_OK(status)) {
1217 goto done;
1219 if (!NT_STATUS_IS_OK(result)) {
1220 status = result;
1221 goto done;
1224 status = dcerpc_samr_OpenUser(b, mem_ctx,
1225 &domain_pol,
1226 MAXIMUM_ALLOWED_ACCESS,
1227 rid,
1228 &user_pol,
1229 &result);
1230 if (!NT_STATUS_IS_OK(status)) {
1231 goto done;
1233 if (!NT_STATUS_IS_OK(result)) {
1234 status = result;
1235 goto done;
1238 status = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1240 done:
1241 if (is_valid_policy_hnd(&user_pol)) {
1242 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1244 if (is_valid_policy_hnd(&domain_pol)) {
1245 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
1247 if (is_valid_policy_hnd(&connect_pol)) {
1248 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
1250 return status;
1253 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1254 TALLOC_CTX *mem_ctx,
1255 struct rpc_sh_ctx *ctx,
1256 struct rpc_pipe_client *pipe_hnd,
1257 struct policy_handle *user_hnd,
1258 int argc, const char **argv)
1260 NTSTATUS status, result;
1261 union samr_UserInfo *info = NULL;
1262 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1264 if (argc != 0) {
1265 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1266 ctx->whoami);
1267 return NT_STATUS_INVALID_PARAMETER;
1270 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1271 user_hnd,
1273 &info,
1274 &result);
1275 if (!NT_STATUS_IS_OK(status)) {
1276 return status;
1278 if (!NT_STATUS_IS_OK(result)) {
1279 return result;
1282 d_printf(_("user rid: %d, group rid: %d\n"),
1283 info->info21.rid,
1284 info->info21.primary_gid);
1286 return result;
1289 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1290 TALLOC_CTX *mem_ctx,
1291 struct rpc_sh_ctx *ctx,
1292 struct rpc_pipe_client *pipe_hnd,
1293 int argc, const char **argv)
1295 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1296 rpc_sh_user_show_internals);
1299 #define FETCHSTR(name, rec) \
1300 do { if (strequal(ctx->thiscmd, name)) { \
1301 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1302 } while (0);
1304 #define SETSTR(name, rec, flag) \
1305 do { if (strequal(ctx->thiscmd, name)) { \
1306 init_lsa_String(&(info->info21.rec), argv[0]); \
1307 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1308 } while (0);
1310 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1311 TALLOC_CTX *mem_ctx,
1312 struct rpc_sh_ctx *ctx,
1313 struct rpc_pipe_client *pipe_hnd,
1314 struct policy_handle *user_hnd,
1315 int argc, const char **argv)
1317 NTSTATUS status, result;
1318 const char *username;
1319 const char *oldval = "";
1320 union samr_UserInfo *info = NULL;
1321 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1323 if (argc > 1) {
1324 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1325 _("Usage:"), ctx->whoami);
1326 return NT_STATUS_INVALID_PARAMETER;
1329 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1330 user_hnd,
1332 &info,
1333 &result);
1334 if (!NT_STATUS_IS_OK(status)) {
1335 return status;
1337 if (!NT_STATUS_IS_OK(result)) {
1338 return result;
1341 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1343 FETCHSTR("fullname", full_name);
1344 FETCHSTR("homedir", home_directory);
1345 FETCHSTR("homedrive", home_drive);
1346 FETCHSTR("logonscript", logon_script);
1347 FETCHSTR("profilepath", profile_path);
1348 FETCHSTR("description", description);
1350 if (argc == 0) {
1351 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1352 goto done;
1355 if (strcmp(argv[0], "NULL") == 0) {
1356 argv[0] = "";
1359 ZERO_STRUCT(info->info21);
1361 SETSTR("fullname", full_name, FULL_NAME);
1362 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1363 SETSTR("homedrive", home_drive, HOME_DRIVE);
1364 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1365 SETSTR("profilepath", profile_path, PROFILE_PATH);
1366 SETSTR("description", description, DESCRIPTION);
1368 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1369 user_hnd,
1371 info,
1372 &result);
1373 if (!NT_STATUS_IS_OK(status)) {
1374 return status;
1377 status = result;
1379 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1380 ctx->thiscmd, oldval, argv[0]);
1382 done:
1384 return status;
1387 #define HANDLEFLG(name, rec) \
1388 do { if (strequal(ctx->thiscmd, name)) { \
1389 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1390 if (newval) { \
1391 newflags = oldflags | ACB_##rec; \
1392 } else { \
1393 newflags = oldflags & ~ACB_##rec; \
1394 } } } while (0);
1396 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1397 TALLOC_CTX *mem_ctx,
1398 struct rpc_sh_ctx *ctx,
1399 struct rpc_pipe_client *pipe_hnd,
1400 int argc, const char **argv)
1402 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1403 rpc_sh_user_str_edit_internals);
1406 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1407 TALLOC_CTX *mem_ctx,
1408 struct rpc_sh_ctx *ctx,
1409 struct rpc_pipe_client *pipe_hnd,
1410 struct policy_handle *user_hnd,
1411 int argc, const char **argv)
1413 NTSTATUS status, result;
1414 const char *username;
1415 const char *oldval = "unknown";
1416 uint32 oldflags, newflags;
1417 bool newval;
1418 union samr_UserInfo *info = NULL;
1419 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1421 if ((argc > 1) ||
1422 ((argc == 1) && !strequal(argv[0], "yes") &&
1423 !strequal(argv[0], "no"))) {
1424 /* TRANSATORS: The yes|no here are program keywords. Please do
1425 not translate. */
1426 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1427 ctx->whoami);
1428 return NT_STATUS_INVALID_PARAMETER;
1431 newval = strequal(argv[0], "yes");
1433 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1434 user_hnd,
1436 &info,
1437 &result);
1438 if (!NT_STATUS_IS_OK(status)) {
1439 return status;
1441 if (!NT_STATUS_IS_OK(result)) {
1442 return result;
1445 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1446 oldflags = info->info21.acct_flags;
1447 newflags = info->info21.acct_flags;
1449 HANDLEFLG("disabled", DISABLED);
1450 HANDLEFLG("pwnotreq", PWNOTREQ);
1451 HANDLEFLG("autolock", AUTOLOCK);
1452 HANDLEFLG("pwnoexp", PWNOEXP);
1454 if (argc == 0) {
1455 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1456 oldval);
1457 goto done;
1460 ZERO_STRUCT(info->info21);
1462 info->info21.acct_flags = newflags;
1463 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1465 status = dcerpc_samr_SetUserInfo(b, mem_ctx,
1466 user_hnd,
1468 info,
1469 &result);
1470 if (!NT_STATUS_IS_OK(status)) {
1471 goto done;
1473 status = result;
1474 if (NT_STATUS_IS_OK(result)) {
1475 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1476 ctx->thiscmd, oldval, argv[0]);
1479 done:
1481 return status;
1484 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1485 TALLOC_CTX *mem_ctx,
1486 struct rpc_sh_ctx *ctx,
1487 struct rpc_pipe_client *pipe_hnd,
1488 int argc, const char **argv)
1490 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1491 rpc_sh_user_flag_edit_internals);
1494 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1495 TALLOC_CTX *mem_ctx,
1496 struct rpc_sh_ctx *ctx)
1498 static struct rpc_sh_cmd cmds[] = {
1500 { "fullname", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1501 N_("Show/Set a user's full name") },
1503 { "homedir", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1504 N_("Show/Set a user's home directory") },
1506 { "homedrive", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1507 N_("Show/Set a user's home drive") },
1509 { "logonscript", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1510 N_("Show/Set a user's logon script") },
1512 { "profilepath", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1513 N_("Show/Set a user's profile path") },
1515 { "description", NULL, &ndr_table_samr, rpc_sh_user_str_edit,
1516 N_("Show/Set a user's description") },
1518 { "disabled", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1519 N_("Show/Set whether a user is disabled") },
1521 { "autolock", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1522 N_("Show/Set whether a user locked out") },
1524 { "pwnotreq", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1525 N_("Show/Set whether a user does not need a password") },
1527 { "pwnoexp", NULL, &ndr_table_samr, rpc_sh_user_flag_edit,
1528 N_("Show/Set whether a user's password does not expire") },
1530 { NULL, NULL, 0, NULL, NULL }
1533 return cmds;
1536 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1537 TALLOC_CTX *mem_ctx,
1538 struct rpc_sh_ctx *ctx)
1540 static struct rpc_sh_cmd cmds[] = {
1542 { "list", NULL, &ndr_table_samr, rpc_sh_user_list,
1543 N_("List available users") },
1545 { "info", NULL, &ndr_table_samr, rpc_sh_user_info,
1546 N_("List the domain groups a user is member of") },
1548 { "show", NULL, &ndr_table_samr, rpc_sh_user_show,
1549 N_("Show info about a user") },
1551 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1552 N_("Show/Modify a user's fields") },
1554 { NULL, NULL, 0, NULL, NULL }
1557 return cmds;
1560 /****************************************************************************/
1563 * Basic usage function for 'net rpc group'.
1564 * @param argc Standard main() style argc.
1565 * @param argv Standard main() style argv. Initial components are already
1566 * stripped.
1569 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1571 return net_group_usage(c, argc, argv);
1575 * Delete group on a remote RPC server.
1577 * All parameters are provided by the run_rpc_command function, except for
1578 * argc, argv which are passed through.
1580 * @param domain_sid The domain sid acquired from the remote server.
1581 * @param cli A cli_state connected to the server.
1582 * @param mem_ctx Talloc context, destroyed on completion of the function.
1583 * @param argc Standard main() style argc.
1584 * @param argv Standard main() style argv. Initial components are already
1585 * stripped.
1587 * @return Normal NTSTATUS return.
1590 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1591 const struct dom_sid *domain_sid,
1592 const char *domain_name,
1593 struct cli_state *cli,
1594 struct rpc_pipe_client *pipe_hnd,
1595 TALLOC_CTX *mem_ctx,
1596 int argc,
1597 const char **argv)
1599 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1600 bool group_is_primary = false;
1601 NTSTATUS status, result;
1602 uint32_t group_rid;
1603 struct samr_RidAttrArray *rids = NULL;
1604 /* char **names; */
1605 int i;
1606 /* struct samr_RidWithAttribute *user_gids; */
1607 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1609 struct samr_Ids group_rids, name_types;
1610 struct lsa_String lsa_acct_name;
1611 union samr_UserInfo *info = NULL;
1613 if (argc < 1 || c->display_usage) {
1614 rpc_group_usage(c, argc,argv);
1615 return NT_STATUS_OK; /* ok? */
1618 status = dcerpc_samr_Connect2(b, mem_ctx,
1619 pipe_hnd->desthost,
1620 MAXIMUM_ALLOWED_ACCESS,
1621 &connect_pol,
1622 &result);
1623 if (!NT_STATUS_IS_OK(status)) {
1624 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1625 goto done;
1628 if (!NT_STATUS_IS_OK(result)) {
1629 status = result;
1630 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1631 goto done;
1634 status = dcerpc_samr_OpenDomain(b, mem_ctx,
1635 &connect_pol,
1636 MAXIMUM_ALLOWED_ACCESS,
1637 discard_const_p(struct dom_sid2, domain_sid),
1638 &domain_pol,
1639 &result);
1640 if (!NT_STATUS_IS_OK(status)) {
1641 d_fprintf(stderr, _("Request open_domain failed\n"));
1642 goto done;
1645 if (!NT_STATUS_IS_OK(result)) {
1646 status = result;
1647 d_fprintf(stderr, _("Request open_domain failed\n"));
1648 goto done;
1651 init_lsa_String(&lsa_acct_name, argv[0]);
1653 status = dcerpc_samr_LookupNames(b, mem_ctx,
1654 &domain_pol,
1656 &lsa_acct_name,
1657 &group_rids,
1658 &name_types,
1659 &result);
1660 if (!NT_STATUS_IS_OK(status)) {
1661 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1662 goto done;
1665 if (!NT_STATUS_IS_OK(result)) {
1666 status = result;
1667 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1668 goto done;
1671 switch (name_types.ids[0])
1673 case SID_NAME_DOM_GRP:
1674 status = dcerpc_samr_OpenGroup(b, mem_ctx,
1675 &domain_pol,
1676 MAXIMUM_ALLOWED_ACCESS,
1677 group_rids.ids[0],
1678 &group_pol,
1679 &result);
1680 if (!NT_STATUS_IS_OK(status)) {
1681 d_fprintf(stderr, _("Request open_group failed"));
1682 goto done;
1685 if (!NT_STATUS_IS_OK(result)) {
1686 status = result;
1687 d_fprintf(stderr, _("Request open_group failed"));
1688 goto done;
1691 group_rid = group_rids.ids[0];
1693 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
1694 &group_pol,
1695 &rids,
1696 &result);
1697 if (!NT_STATUS_IS_OK(status)) {
1698 d_fprintf(stderr,
1699 _("Unable to query group members of %s"),
1700 argv[0]);
1701 goto done;
1704 if (!NT_STATUS_IS_OK(result)) {
1705 status = result;
1706 d_fprintf(stderr,
1707 _("Unable to query group members of %s"),
1708 argv[0]);
1709 goto done;
1712 if (c->opt_verbose) {
1713 d_printf(
1714 _("Domain Group %s (rid: %d) has %d members\n"),
1715 argv[0],group_rid, rids->count);
1718 /* Check if group is anyone's primary group */
1719 for (i = 0; i < rids->count; i++)
1721 status = dcerpc_samr_OpenUser(b, mem_ctx,
1722 &domain_pol,
1723 MAXIMUM_ALLOWED_ACCESS,
1724 rids->rids[i],
1725 &user_pol,
1726 &result);
1727 if (!NT_STATUS_IS_OK(status)) {
1728 d_fprintf(stderr,
1729 _("Unable to open group member %d\n"),
1730 rids->rids[i]);
1731 goto done;
1734 if (!NT_STATUS_IS_OK(result)) {
1735 status = result;
1736 d_fprintf(stderr,
1737 _("Unable to open group member %d\n"),
1738 rids->rids[i]);
1739 goto done;
1742 status = dcerpc_samr_QueryUserInfo(b, mem_ctx,
1743 &user_pol,
1745 &info,
1746 &result);
1747 if (!NT_STATUS_IS_OK(status)) {
1748 d_fprintf(stderr,
1749 _("Unable to lookup userinfo for group "
1750 "member %d\n"),
1751 rids->rids[i]);
1752 goto done;
1755 if (!NT_STATUS_IS_OK(result)) {
1756 status = result;
1757 d_fprintf(stderr,
1758 _("Unable to lookup userinfo for group "
1759 "member %d\n"),
1760 rids->rids[i]);
1761 goto done;
1764 if (info->info21.primary_gid == group_rid) {
1765 if (c->opt_verbose) {
1766 d_printf(_("Group is primary group "
1767 "of %s\n"),
1768 info->info21.account_name.string);
1770 group_is_primary = true;
1773 dcerpc_samr_Close(b, mem_ctx, &user_pol, &result);
1776 if (group_is_primary) {
1777 d_fprintf(stderr, _("Unable to delete group because "
1778 "some of it's members have it as primary "
1779 "group\n"));
1780 status = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1781 goto done;
1784 /* remove all group members */
1785 for (i = 0; i < rids->count; i++)
1787 if (c->opt_verbose)
1788 d_printf(_("Remove group member %d..."),
1789 rids->rids[i]);
1790 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
1791 &group_pol,
1792 rids->rids[i],
1793 &result);
1794 if (!NT_STATUS_IS_OK(status)) {
1795 goto done;
1797 status = result;
1798 if (NT_STATUS_IS_OK(result)) {
1799 if (c->opt_verbose)
1800 d_printf(_("ok\n"));
1801 } else {
1802 if (c->opt_verbose)
1803 d_printf("%s\n", _("failed"));
1804 goto done;
1808 status = dcerpc_samr_DeleteDomainGroup(b, mem_ctx,
1809 &group_pol,
1810 &result);
1811 if (!NT_STATUS_IS_OK(status)) {
1812 break;
1815 status = result;
1817 break;
1818 /* removing a local group is easier... */
1819 case SID_NAME_ALIAS:
1820 status = dcerpc_samr_OpenAlias(b, mem_ctx,
1821 &domain_pol,
1822 MAXIMUM_ALLOWED_ACCESS,
1823 group_rids.ids[0],
1824 &group_pol,
1825 &result);
1826 if (!NT_STATUS_IS_OK(status)) {
1827 d_fprintf(stderr, _("Request open_alias failed\n"));
1828 goto done;
1830 if (!NT_STATUS_IS_OK(result)) {
1831 status = result;
1832 d_fprintf(stderr, _("Request open_alias failed\n"));
1833 goto done;
1836 status = dcerpc_samr_DeleteDomAlias(b, mem_ctx,
1837 &group_pol,
1838 &result);
1839 if (!NT_STATUS_IS_OK(status)) {
1840 break;
1843 status = result;
1845 break;
1846 default:
1847 d_fprintf(stderr, _("%s is of type %s. This command is only "
1848 "for deleting local or global groups\n"),
1849 argv[0],sid_type_lookup(name_types.ids[0]));
1850 status = NT_STATUS_UNSUCCESSFUL;
1851 goto done;
1854 if (NT_STATUS_IS_OK(status)) {
1855 if (c->opt_verbose)
1856 d_printf(_("Deleted %s '%s'\n"),
1857 sid_type_lookup(name_types.ids[0]), argv[0]);
1858 } else {
1859 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1860 get_friendly_nt_error_msg(status));
1863 done:
1864 return status;
1868 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1870 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
1871 rpc_group_delete_internals, argc,argv);
1874 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1876 NET_API_STATUS status;
1877 struct GROUP_INFO_1 info1;
1878 uint32_t parm_error = 0;
1880 if (argc != 1 || c->display_usage) {
1881 rpc_group_usage(c, argc, argv);
1882 return 0;
1885 ZERO_STRUCT(info1);
1887 info1.grpi1_name = argv[0];
1888 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1889 info1.grpi1_comment = c->opt_comment;
1892 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1894 if (status != 0) {
1895 d_fprintf(stderr,
1896 _("Failed to add group '%s' with error: %s.\n"),
1897 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1898 status));
1899 return -1;
1900 } else {
1901 d_printf(_("Added group '%s'.\n"), argv[0]);
1904 return 0;
1907 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1909 NET_API_STATUS status;
1910 struct LOCALGROUP_INFO_1 info1;
1911 uint32_t parm_error = 0;
1913 if (argc != 1 || c->display_usage) {
1914 rpc_group_usage(c, argc, argv);
1915 return 0;
1918 ZERO_STRUCT(info1);
1920 info1.lgrpi1_name = argv[0];
1921 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1922 info1.lgrpi1_comment = c->opt_comment;
1925 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1927 if (status != 0) {
1928 d_fprintf(stderr,
1929 _("Failed to add alias '%s' with error: %s.\n"),
1930 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1931 status));
1932 return -1;
1933 } else {
1934 d_printf(_("Added alias '%s'.\n"), argv[0]);
1937 return 0;
1940 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1942 if (c->opt_localgroup)
1943 return rpc_alias_add_internals(c, argc, argv);
1945 return rpc_group_add_internals(c, argc, argv);
1948 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1949 TALLOC_CTX *mem_ctx,
1950 const char *name,
1951 struct dom_sid *sid,
1952 enum lsa_SidType *type)
1954 struct dom_sid *sids = NULL;
1955 enum lsa_SidType *types = NULL;
1956 struct rpc_pipe_client *pipe_hnd = NULL;
1957 struct policy_handle lsa_pol;
1958 NTSTATUS status, result;
1959 struct dcerpc_binding_handle *b;
1961 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1962 &pipe_hnd);
1963 if (!NT_STATUS_IS_OK(status)) {
1964 goto done;
1967 b = pipe_hnd->binding_handle;
1969 status = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1970 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1972 if (!NT_STATUS_IS_OK(status)) {
1973 goto done;
1976 status = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1977 &name, NULL, 1, &sids, &types);
1979 if (NT_STATUS_IS_OK(status)) {
1980 sid_copy(sid, &sids[0]);
1981 *type = types[0];
1984 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
1986 done:
1987 if (pipe_hnd) {
1988 TALLOC_FREE(pipe_hnd);
1991 if (!NT_STATUS_IS_OK(status) && (strncasecmp_m(name, "S-", 2) == 0)) {
1993 /* Try as S-1-5-whatever */
1995 struct dom_sid tmp_sid;
1997 if (string_to_sid(&tmp_sid, name)) {
1998 sid_copy(sid, &tmp_sid);
1999 *type = SID_NAME_UNKNOWN;
2000 status = NT_STATUS_OK;
2004 return status;
2007 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2008 TALLOC_CTX *mem_ctx,
2009 const struct dom_sid *group_sid,
2010 const char *member)
2012 struct policy_handle connect_pol, domain_pol;
2013 NTSTATUS status, result;
2014 uint32 group_rid;
2015 struct policy_handle group_pol;
2016 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2018 struct samr_Ids rids, rid_types;
2019 struct lsa_String lsa_acct_name;
2021 struct dom_sid sid;
2023 sid_copy(&sid, group_sid);
2025 if (!sid_split_rid(&sid, &group_rid)) {
2026 return NT_STATUS_UNSUCCESSFUL;
2029 /* Get sam policy handle */
2030 status = dcerpc_samr_Connect2(b, mem_ctx,
2031 pipe_hnd->desthost,
2032 MAXIMUM_ALLOWED_ACCESS,
2033 &connect_pol,
2034 &result);
2035 if (!NT_STATUS_IS_OK(status)) {
2036 return status;
2038 if (!NT_STATUS_IS_OK(result)) {
2039 return result;
2042 /* Get domain policy handle */
2043 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2044 &connect_pol,
2045 MAXIMUM_ALLOWED_ACCESS,
2046 &sid,
2047 &domain_pol,
2048 &result);
2049 if (!NT_STATUS_IS_OK(status)) {
2050 return status;
2052 if (!NT_STATUS_IS_OK(result)) {
2053 return result;
2056 init_lsa_String(&lsa_acct_name, member);
2058 status = dcerpc_samr_LookupNames(b, mem_ctx,
2059 &domain_pol,
2061 &lsa_acct_name,
2062 &rids,
2063 &rid_types,
2064 &result);
2065 if (!NT_STATUS_IS_OK(status)) {
2066 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2067 member);
2068 goto done;
2071 if (!NT_STATUS_IS_OK(result)) {
2072 status = result;
2073 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2074 member);
2075 goto done;
2078 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2079 &domain_pol,
2080 MAXIMUM_ALLOWED_ACCESS,
2081 group_rid,
2082 &group_pol,
2083 &result);
2084 if (!NT_STATUS_IS_OK(status)) {
2085 goto done;
2088 if (!NT_STATUS_IS_OK(result)) {
2089 status = result;
2090 goto done;
2093 status = dcerpc_samr_AddGroupMember(b, mem_ctx,
2094 &group_pol,
2095 rids.ids[0],
2096 0x0005, /* unknown flags */
2097 &result);
2098 if (!NT_STATUS_IS_OK(status)) {
2099 goto done;
2102 status = result;
2104 done:
2105 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2106 return status;
2109 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2110 TALLOC_CTX *mem_ctx,
2111 const struct dom_sid *alias_sid,
2112 const char *member)
2114 struct policy_handle connect_pol, domain_pol;
2115 NTSTATUS status, result;
2116 uint32 alias_rid;
2117 struct policy_handle alias_pol;
2118 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2120 struct dom_sid member_sid;
2121 enum lsa_SidType member_type;
2123 struct dom_sid sid;
2125 sid_copy(&sid, alias_sid);
2127 if (!sid_split_rid(&sid, &alias_rid)) {
2128 return NT_STATUS_UNSUCCESSFUL;
2131 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2132 member, &member_sid, &member_type);
2134 if (!NT_STATUS_IS_OK(result)) {
2135 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2136 member);
2137 return result;
2140 /* Get sam policy handle */
2141 status = dcerpc_samr_Connect2(b, mem_ctx,
2142 pipe_hnd->desthost,
2143 MAXIMUM_ALLOWED_ACCESS,
2144 &connect_pol,
2145 &result);
2146 if (!NT_STATUS_IS_OK(status)) {
2147 goto done;
2149 if (!NT_STATUS_IS_OK(result)) {
2150 status = result;
2151 goto done;
2154 /* Get domain policy handle */
2155 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2156 &connect_pol,
2157 MAXIMUM_ALLOWED_ACCESS,
2158 &sid,
2159 &domain_pol,
2160 &result);
2161 if (!NT_STATUS_IS_OK(status)) {
2162 goto done;
2164 if (!NT_STATUS_IS_OK(result)) {
2165 status = result;
2166 goto done;
2169 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2170 &domain_pol,
2171 MAXIMUM_ALLOWED_ACCESS,
2172 alias_rid,
2173 &alias_pol,
2174 &result);
2175 if (!NT_STATUS_IS_OK(status)) {
2176 return status;
2178 if (!NT_STATUS_IS_OK(result)) {
2179 return result;
2182 status = dcerpc_samr_AddAliasMember(b, mem_ctx,
2183 &alias_pol,
2184 &member_sid,
2185 &result);
2186 if (!NT_STATUS_IS_OK(status)) {
2187 return status;
2190 status = result;
2192 done:
2193 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2194 return status;
2197 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2198 const struct dom_sid *domain_sid,
2199 const char *domain_name,
2200 struct cli_state *cli,
2201 struct rpc_pipe_client *pipe_hnd,
2202 TALLOC_CTX *mem_ctx,
2203 int argc,
2204 const char **argv)
2206 struct dom_sid group_sid;
2207 enum lsa_SidType group_type;
2209 if (argc != 2 || c->display_usage) {
2210 d_printf("%s\n%s",
2211 _("Usage:"),
2212 _("net rpc group addmem <group> <member>\n"
2213 " Add a member to a group\n"
2214 " group\tGroup to add member to\n"
2215 " member\tMember to add to group\n"));
2216 return NT_STATUS_UNSUCCESSFUL;
2219 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2220 &group_sid, &group_type))) {
2221 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2222 argv[0]);
2223 return NT_STATUS_UNSUCCESSFUL;
2226 if (group_type == SID_NAME_DOM_GRP) {
2227 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2228 &group_sid, argv[1]);
2230 if (!NT_STATUS_IS_OK(result)) {
2231 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2232 argv[1], argv[0], nt_errstr(result));
2234 return result;
2237 if (group_type == SID_NAME_ALIAS) {
2238 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2239 &group_sid, argv[1]);
2241 if (!NT_STATUS_IS_OK(result)) {
2242 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2243 argv[1], argv[0], nt_errstr(result));
2245 return result;
2248 d_fprintf(stderr, _("Can only add members to global or local groups "
2249 "which %s is not\n"), argv[0]);
2251 return NT_STATUS_UNSUCCESSFUL;
2254 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2256 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2257 rpc_group_addmem_internals,
2258 argc, argv);
2261 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2262 struct rpc_pipe_client *pipe_hnd,
2263 TALLOC_CTX *mem_ctx,
2264 const struct dom_sid *group_sid,
2265 const char *member)
2267 struct policy_handle connect_pol, domain_pol;
2268 NTSTATUS status, result;
2269 uint32 group_rid;
2270 struct policy_handle group_pol;
2271 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2273 struct samr_Ids rids, rid_types;
2274 struct lsa_String lsa_acct_name;
2276 struct dom_sid sid;
2278 sid_copy(&sid, group_sid);
2280 if (!sid_split_rid(&sid, &group_rid))
2281 return NT_STATUS_UNSUCCESSFUL;
2283 /* Get sam policy handle */
2284 status = dcerpc_samr_Connect2(b, mem_ctx,
2285 pipe_hnd->desthost,
2286 MAXIMUM_ALLOWED_ACCESS,
2287 &connect_pol,
2288 &result);
2289 if (!NT_STATUS_IS_OK(status)) {
2290 return status;
2292 if (!NT_STATUS_IS_OK(result)) {
2293 return result;
2297 /* Get domain policy handle */
2298 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2299 &connect_pol,
2300 MAXIMUM_ALLOWED_ACCESS,
2301 &sid,
2302 &domain_pol,
2303 &result);
2304 if (!NT_STATUS_IS_OK(status)) {
2305 return status;
2307 if (!NT_STATUS_IS_OK(result)) {
2308 return result;
2311 init_lsa_String(&lsa_acct_name, member);
2313 status = dcerpc_samr_LookupNames(b, mem_ctx,
2314 &domain_pol,
2316 &lsa_acct_name,
2317 &rids,
2318 &rid_types,
2319 &result);
2320 if (!NT_STATUS_IS_OK(status)) {
2321 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2322 member);
2323 goto done;
2326 if (!NT_STATUS_IS_OK(result)) {
2327 status = result;
2328 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2329 member);
2330 goto done;
2333 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2334 &domain_pol,
2335 MAXIMUM_ALLOWED_ACCESS,
2336 group_rid,
2337 &group_pol,
2338 &result);
2339 if (!NT_STATUS_IS_OK(status)) {
2340 goto done;
2342 if (!NT_STATUS_IS_OK(result)) {
2343 status = result;
2344 goto done;
2347 status = dcerpc_samr_DeleteGroupMember(b, mem_ctx,
2348 &group_pol,
2349 rids.ids[0],
2350 &result);
2351 if (!NT_STATUS_IS_OK(status)) {
2352 goto done;
2355 status = result;
2356 done:
2357 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2358 return status;
2361 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2362 TALLOC_CTX *mem_ctx,
2363 const struct dom_sid *alias_sid,
2364 const char *member)
2366 struct policy_handle connect_pol, domain_pol;
2367 NTSTATUS status, result;
2368 uint32 alias_rid;
2369 struct policy_handle alias_pol;
2370 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2372 struct dom_sid member_sid;
2373 enum lsa_SidType member_type;
2375 struct dom_sid sid;
2377 sid_copy(&sid, alias_sid);
2379 if (!sid_split_rid(&sid, &alias_rid))
2380 return NT_STATUS_UNSUCCESSFUL;
2382 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2383 member, &member_sid, &member_type);
2385 if (!NT_STATUS_IS_OK(result)) {
2386 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2387 member);
2388 return result;
2391 /* Get sam policy handle */
2392 status = dcerpc_samr_Connect2(b, mem_ctx,
2393 pipe_hnd->desthost,
2394 MAXIMUM_ALLOWED_ACCESS,
2395 &connect_pol,
2396 &result);
2397 if (!NT_STATUS_IS_OK(status)) {
2398 goto done;
2400 if (!NT_STATUS_IS_OK(result)) {
2401 status = result;
2402 goto done;
2405 /* Get domain policy handle */
2406 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2407 &connect_pol,
2408 MAXIMUM_ALLOWED_ACCESS,
2409 &sid,
2410 &domain_pol,
2411 &result);
2412 if (!NT_STATUS_IS_OK(status)) {
2413 goto done;
2415 if (!NT_STATUS_IS_OK(result)) {
2416 status = result;
2417 goto done;
2420 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2421 &domain_pol,
2422 MAXIMUM_ALLOWED_ACCESS,
2423 alias_rid,
2424 &alias_pol,
2425 &result);
2426 if (!NT_STATUS_IS_OK(status)) {
2427 return status;
2430 if (!NT_STATUS_IS_OK(result)) {
2431 return result;
2434 status = dcerpc_samr_DeleteAliasMember(b, mem_ctx,
2435 &alias_pol,
2436 &member_sid,
2437 &result);
2439 if (!NT_STATUS_IS_OK(status)) {
2440 return status;
2443 status = result;
2445 done:
2446 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
2447 return status;
2450 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2451 const struct dom_sid *domain_sid,
2452 const char *domain_name,
2453 struct cli_state *cli,
2454 struct rpc_pipe_client *pipe_hnd,
2455 TALLOC_CTX *mem_ctx,
2456 int argc,
2457 const char **argv)
2459 struct dom_sid group_sid;
2460 enum lsa_SidType group_type;
2462 if (argc != 2 || c->display_usage) {
2463 d_printf("%s\n%s",
2464 _("Usage:"),
2465 _("net rpc group delmem <group> <member>\n"
2466 " Delete a member from a group\n"
2467 " group\tGroup to delete member from\n"
2468 " member\tMember to delete from group\n"));
2469 return NT_STATUS_UNSUCCESSFUL;
2472 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2473 &group_sid, &group_type))) {
2474 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2475 argv[0]);
2476 return NT_STATUS_UNSUCCESSFUL;
2479 if (group_type == SID_NAME_DOM_GRP) {
2480 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2481 &group_sid, argv[1]);
2483 if (!NT_STATUS_IS_OK(result)) {
2484 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2485 argv[1], argv[0], nt_errstr(result));
2487 return result;
2490 if (group_type == SID_NAME_ALIAS) {
2491 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2492 &group_sid, argv[1]);
2494 if (!NT_STATUS_IS_OK(result)) {
2495 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2496 argv[1], argv[0], nt_errstr(result));
2498 return result;
2501 d_fprintf(stderr, _("Can only delete members from global or local "
2502 "groups which %s is not\n"), argv[0]);
2504 return NT_STATUS_UNSUCCESSFUL;
2507 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2509 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2510 rpc_group_delmem_internals,
2511 argc, argv);
2515 * List groups on a remote RPC server.
2517 * All parameters are provided by the run_rpc_command function, except for
2518 * argc, argv which are passes through.
2520 * @param domain_sid The domain sid acquired from the remote server.
2521 * @param cli A cli_state connected to the server.
2522 * @param mem_ctx Talloc context, destroyed on completion of the function.
2523 * @param argc Standard main() style argc.
2524 * @param argv Standard main() style argv. Initial components are already
2525 * stripped.
2527 * @return Normal NTSTATUS return.
2530 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2531 const struct dom_sid *domain_sid,
2532 const char *domain_name,
2533 struct cli_state *cli,
2534 struct rpc_pipe_client *pipe_hnd,
2535 TALLOC_CTX *mem_ctx,
2536 int argc,
2537 const char **argv)
2539 struct policy_handle connect_pol, domain_pol;
2540 NTSTATUS status, result;
2541 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2542 struct samr_SamArray *groups = NULL;
2543 bool global = false;
2544 bool local = false;
2545 bool builtin = false;
2546 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2548 if (c->display_usage) {
2549 d_printf("%s\n%s",
2550 _("Usage:"),
2551 _("net rpc group list [global] [local] [builtin]\n"
2552 " List groups on RPC server\n"
2553 " global\tList global groups\n"
2554 " local\tList local groups\n"
2555 " builtin\tList builtin groups\n"
2556 " If none of global, local or builtin is "
2557 "specified, all three options are considered "
2558 "set\n"));
2559 return NT_STATUS_OK;
2562 if (argc == 0) {
2563 global = true;
2564 local = true;
2565 builtin = true;
2568 for (i=0; i<argc; i++) {
2569 if (strequal(argv[i], "global"))
2570 global = true;
2572 if (strequal(argv[i], "local"))
2573 local = true;
2575 if (strequal(argv[i], "builtin"))
2576 builtin = true;
2579 /* Get sam policy handle */
2581 status = dcerpc_samr_Connect2(b, mem_ctx,
2582 pipe_hnd->desthost,
2583 MAXIMUM_ALLOWED_ACCESS,
2584 &connect_pol,
2585 &result);
2586 if (!NT_STATUS_IS_OK(status)) {
2587 goto done;
2589 if (!NT_STATUS_IS_OK(result)) {
2590 status = result;
2591 goto done;
2594 /* Get domain policy handle */
2596 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2597 &connect_pol,
2598 MAXIMUM_ALLOWED_ACCESS,
2599 discard_const_p(struct dom_sid2, domain_sid),
2600 &domain_pol,
2601 &result);
2602 if (!NT_STATUS_IS_OK(status)) {
2603 goto done;
2605 if (!NT_STATUS_IS_OK(result)) {
2606 status = result;
2607 goto done;
2610 /* Query domain groups */
2611 if (c->opt_long_list_entries)
2612 d_printf(_("\nGroup name Comment"
2613 "\n-----------------------------\n"));
2614 do {
2615 uint32_t max_size, total_size, returned_size;
2616 union samr_DispInfo info;
2618 if (!global) break;
2620 dcerpc_get_query_dispinfo_params(
2621 loop_count, &max_entries, &max_size);
2623 status = dcerpc_samr_QueryDisplayInfo(b, mem_ctx,
2624 &domain_pol,
2626 start_idx,
2627 max_entries,
2628 max_size,
2629 &total_size,
2630 &returned_size,
2631 &info,
2632 &result);
2633 if (!NT_STATUS_IS_OK(status)) {
2634 goto done;
2636 num_entries = info.info3.count;
2637 start_idx += info.info3.count;
2639 if (!NT_STATUS_IS_OK(result) &&
2640 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2641 break;
2643 for (i = 0; i < num_entries; i++) {
2645 const char *group = NULL;
2646 const char *desc = NULL;
2648 group = info.info3.entries[i].account_name.string;
2649 desc = info.info3.entries[i].description.string;
2651 if (c->opt_long_list_entries)
2652 printf("%-21.21s %-50.50s\n",
2653 group, desc);
2654 else
2655 printf("%s\n", group);
2657 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2658 /* query domain aliases */
2659 start_idx = 0;
2660 do {
2661 if (!local) break;
2663 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2664 &domain_pol,
2665 &start_idx,
2666 &groups,
2667 0xffff,
2668 &num_entries,
2669 &result);
2670 if (!NT_STATUS_IS_OK(status)) {
2671 goto done;
2673 if (!NT_STATUS_IS_OK(result) &&
2674 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2675 break;
2677 for (i = 0; i < num_entries; i++) {
2679 const char *description = NULL;
2681 if (c->opt_long_list_entries) {
2683 struct policy_handle alias_pol;
2684 union samr_AliasInfo *info = NULL;
2685 NTSTATUS _result;
2687 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2688 &domain_pol,
2689 0x8,
2690 groups->entries[i].idx,
2691 &alias_pol,
2692 &_result);
2693 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2694 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2695 &alias_pol,
2697 &info,
2698 &_result);
2699 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2700 status = dcerpc_samr_Close(b, mem_ctx,
2701 &alias_pol,
2702 &_result);
2703 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2704 description = info->description.string;
2710 if (description != NULL) {
2711 printf("%-21.21s %-50.50s\n",
2712 groups->entries[i].name.string,
2713 description);
2714 } else {
2715 printf("%s\n", groups->entries[i].name.string);
2718 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2719 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
2720 /* Get builtin policy handle */
2722 status = dcerpc_samr_OpenDomain(b, mem_ctx,
2723 &connect_pol,
2724 MAXIMUM_ALLOWED_ACCESS,
2725 discard_const_p(struct dom_sid2, &global_sid_Builtin),
2726 &domain_pol,
2727 &result);
2728 if (!NT_STATUS_IS_OK(status)) {
2729 goto done;
2731 if (!NT_STATUS_IS_OK(result)) {
2732 status = result;
2733 goto done;
2736 /* query builtin aliases */
2737 start_idx = 0;
2738 do {
2739 if (!builtin) break;
2741 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
2742 &domain_pol,
2743 &start_idx,
2744 &groups,
2745 max_entries,
2746 &num_entries,
2747 &result);
2748 if (!NT_STATUS_IS_OK(status)) {
2749 break;
2751 if (!NT_STATUS_IS_OK(result) &&
2752 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
2753 status = result;
2754 break;
2757 for (i = 0; i < num_entries; i++) {
2759 const char *description = NULL;
2761 if (c->opt_long_list_entries) {
2763 struct policy_handle alias_pol;
2764 union samr_AliasInfo *info = NULL;
2765 NTSTATUS _result;
2767 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2768 &domain_pol,
2769 0x8,
2770 groups->entries[i].idx,
2771 &alias_pol,
2772 &_result);
2773 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2774 status = dcerpc_samr_QueryAliasInfo(b, mem_ctx,
2775 &alias_pol,
2777 &info,
2778 &_result);
2779 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2780 status = dcerpc_samr_Close(b, mem_ctx,
2781 &alias_pol,
2782 &_result);
2783 if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(_result)) {
2784 description = info->description.string;
2790 if (description != NULL) {
2791 printf("%-21.21s %-50.50s\n",
2792 groups->entries[i].name.string,
2793 description);
2794 } else {
2795 printf("%s\n", groups->entries[i].name.string);
2798 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2800 status = result;
2802 done:
2803 return status;
2806 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2808 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
2809 rpc_group_list_internals,
2810 argc, argv);
2813 static NTSTATUS rpc_list_group_members(struct net_context *c,
2814 struct rpc_pipe_client *pipe_hnd,
2815 TALLOC_CTX *mem_ctx,
2816 const char *domain_name,
2817 const struct dom_sid *domain_sid,
2818 struct policy_handle *domain_pol,
2819 uint32 rid)
2821 NTSTATUS result, status;
2822 struct policy_handle group_pol;
2823 uint32 num_members, *group_rids;
2824 int i;
2825 struct samr_RidAttrArray *rids = NULL;
2826 struct lsa_Strings names;
2827 struct samr_Ids types;
2828 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2830 fstring sid_str;
2831 sid_to_fstring(sid_str, domain_sid);
2833 status = dcerpc_samr_OpenGroup(b, mem_ctx,
2834 domain_pol,
2835 MAXIMUM_ALLOWED_ACCESS,
2836 rid,
2837 &group_pol,
2838 &result);
2839 if (!NT_STATUS_IS_OK(status)) {
2840 return status;
2842 if (!NT_STATUS_IS_OK(result)) {
2843 return result;
2846 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
2847 &group_pol,
2848 &rids,
2849 &result);
2850 if (!NT_STATUS_IS_OK(status)) {
2851 return status;
2853 if (!NT_STATUS_IS_OK(result)) {
2854 return result;
2857 num_members = rids->count;
2858 group_rids = rids->rids;
2860 while (num_members > 0) {
2861 int this_time = 512;
2863 if (num_members < this_time)
2864 this_time = num_members;
2866 status = dcerpc_samr_LookupRids(b, mem_ctx,
2867 domain_pol,
2868 this_time,
2869 group_rids,
2870 &names,
2871 &types,
2872 &result);
2873 if (!NT_STATUS_IS_OK(status)) {
2874 return status;
2876 if (!NT_STATUS_IS_OK(result)) {
2877 return result;
2880 /* We only have users as members, but make the output
2881 the same as the output of alias members */
2883 for (i = 0; i < this_time; i++) {
2885 if (c->opt_long_list_entries) {
2886 printf("%s-%d %s\\%s %d\n", sid_str,
2887 group_rids[i], domain_name,
2888 names.names[i].string,
2889 SID_NAME_USER);
2890 } else {
2891 printf("%s\\%s\n", domain_name,
2892 names.names[i].string);
2896 num_members -= this_time;
2897 group_rids += 512;
2900 return NT_STATUS_OK;
2903 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2904 struct rpc_pipe_client *pipe_hnd,
2905 TALLOC_CTX *mem_ctx,
2906 struct policy_handle *domain_pol,
2907 uint32 rid)
2909 NTSTATUS result, status;
2910 struct rpc_pipe_client *lsa_pipe;
2911 struct policy_handle alias_pol, lsa_pol;
2912 uint32 num_members;
2913 struct dom_sid *alias_sids;
2914 char **domains;
2915 char **names;
2916 enum lsa_SidType *types;
2917 int i;
2918 struct lsa_SidArray sid_array;
2919 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2921 status = dcerpc_samr_OpenAlias(b, mem_ctx,
2922 domain_pol,
2923 MAXIMUM_ALLOWED_ACCESS,
2924 rid,
2925 &alias_pol,
2926 &result);
2927 if (!NT_STATUS_IS_OK(status)) {
2928 return status;
2930 if (!NT_STATUS_IS_OK(result)) {
2931 return result;
2934 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
2935 &alias_pol,
2936 &sid_array,
2937 &result);
2938 if (!NT_STATUS_IS_OK(status)) {
2939 d_fprintf(stderr, _("Couldn't list alias members\n"));
2940 return status;
2942 if (!NT_STATUS_IS_OK(result)) {
2943 d_fprintf(stderr, _("Couldn't list alias members\n"));
2944 return result;
2947 num_members = sid_array.num_sids;
2949 if (num_members == 0) {
2950 return NT_STATUS_OK;
2953 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2954 &ndr_table_lsarpc.syntax_id,
2955 &lsa_pipe);
2956 if (!NT_STATUS_IS_OK(result)) {
2957 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2958 nt_errstr(result) );
2959 return result;
2962 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2963 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2965 if (!NT_STATUS_IS_OK(result)) {
2966 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2967 TALLOC_FREE(lsa_pipe);
2968 return result;
2971 alias_sids = talloc_zero_array(mem_ctx, struct dom_sid, num_members);
2972 if (!alias_sids) {
2973 d_fprintf(stderr, _("Out of memory\n"));
2974 TALLOC_FREE(lsa_pipe);
2975 return NT_STATUS_NO_MEMORY;
2978 for (i=0; i<num_members; i++) {
2979 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2982 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2983 num_members, alias_sids,
2984 &domains, &names, &types);
2986 if (!NT_STATUS_IS_OK(result) &&
2987 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2988 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2989 TALLOC_FREE(lsa_pipe);
2990 return result;
2993 for (i = 0; i < num_members; i++) {
2994 fstring sid_str;
2995 sid_to_fstring(sid_str, &alias_sids[i]);
2997 if (c->opt_long_list_entries) {
2998 printf("%s %s\\%s %d\n", sid_str,
2999 domains[i] ? domains[i] : _("*unknown*"),
3000 names[i] ? names[i] : _("*unknown*"), types[i]);
3001 } else {
3002 if (domains[i])
3003 printf("%s\\%s\n", domains[i], names[i]);
3004 else
3005 printf("%s\n", sid_str);
3009 TALLOC_FREE(lsa_pipe);
3010 return NT_STATUS_OK;
3013 static NTSTATUS rpc_group_members_internals(struct net_context *c,
3014 const struct dom_sid *domain_sid,
3015 const char *domain_name,
3016 struct cli_state *cli,
3017 struct rpc_pipe_client *pipe_hnd,
3018 TALLOC_CTX *mem_ctx,
3019 int argc,
3020 const char **argv)
3022 NTSTATUS result, status;
3023 struct policy_handle connect_pol, domain_pol;
3024 struct samr_Ids rids, rid_types;
3025 struct lsa_String lsa_acct_name;
3026 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3028 /* Get sam policy handle */
3030 status = dcerpc_samr_Connect2(b, mem_ctx,
3031 pipe_hnd->desthost,
3032 MAXIMUM_ALLOWED_ACCESS,
3033 &connect_pol,
3034 &result);
3035 if (!NT_STATUS_IS_OK(status)) {
3036 return status;
3038 if (!NT_STATUS_IS_OK(result)) {
3039 return result;
3042 /* Get domain policy handle */
3044 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3045 &connect_pol,
3046 MAXIMUM_ALLOWED_ACCESS,
3047 discard_const_p(struct dom_sid2, domain_sid),
3048 &domain_pol,
3049 &result);
3050 if (!NT_STATUS_IS_OK(status)) {
3051 return status;
3053 if (!NT_STATUS_IS_OK(result)) {
3054 return result;
3057 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
3059 status = dcerpc_samr_LookupNames(b, mem_ctx,
3060 &domain_pol,
3062 &lsa_acct_name,
3063 &rids,
3064 &rid_types,
3065 &result);
3066 if (!NT_STATUS_IS_OK(status)) {
3067 return status;
3070 if (!NT_STATUS_IS_OK(result)) {
3072 /* Ok, did not find it in the global sam, try with builtin */
3074 struct dom_sid sid_Builtin;
3076 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
3078 sid_copy(&sid_Builtin, &global_sid_Builtin);
3080 status = dcerpc_samr_OpenDomain(b, mem_ctx,
3081 &connect_pol,
3082 MAXIMUM_ALLOWED_ACCESS,
3083 &sid_Builtin,
3084 &domain_pol,
3085 &result);
3086 if (!NT_STATUS_IS_OK(status)) {
3087 return status;
3089 if (!NT_STATUS_IS_OK(result)) {
3090 d_fprintf(stderr, _("Couldn't find group %s\n"),
3091 argv[0]);
3092 return result;
3095 status = dcerpc_samr_LookupNames(b, mem_ctx,
3096 &domain_pol,
3098 &lsa_acct_name,
3099 &rids,
3100 &rid_types,
3101 &result);
3102 if (!NT_STATUS_IS_OK(status)) {
3103 return status;
3105 if (!NT_STATUS_IS_OK(result)) {
3106 d_fprintf(stderr, _("Couldn't find group %s\n"),
3107 argv[0]);
3108 return result;
3112 if (rids.count != 1) {
3113 d_fprintf(stderr, _("Couldn't find group %s\n"),
3114 argv[0]);
3115 return result;
3118 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
3119 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
3120 domain_sid, &domain_pol,
3121 rids.ids[0]);
3124 if (rid_types.ids[0] == SID_NAME_ALIAS) {
3125 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
3126 rids.ids[0]);
3129 return NT_STATUS_NO_SUCH_GROUP;
3132 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
3134 if (argc != 1 || c->display_usage) {
3135 return rpc_group_usage(c, argc, argv);
3138 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3139 rpc_group_members_internals,
3140 argc, argv);
3143 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
3145 NET_API_STATUS status;
3146 struct GROUP_INFO_0 g0;
3147 uint32_t parm_err;
3149 if (argc != 2) {
3150 d_printf(_("Usage:\n"));
3151 d_printf("net rpc group rename group newname\n");
3152 return -1;
3155 g0.grpi0_name = argv[1];
3157 status = NetGroupSetInfo(c->opt_host,
3158 argv[0],
3160 (uint8_t *)&g0,
3161 &parm_err);
3163 if (status != 0) {
3164 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
3165 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3166 status));
3167 return -1;
3170 return 0;
3173 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3175 if (argc != 2 || c->display_usage) {
3176 return rpc_group_usage(c, argc, argv);
3179 return rpc_group_rename_internals(c, argc, argv);
3183 * 'net rpc group' entrypoint.
3184 * @param argc Standard main() style argc.
3185 * @param argv Standard main() style argv. Initial components are already
3186 * stripped.
3189 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3191 NET_API_STATUS status;
3193 struct functable func[] = {
3195 "add",
3196 rpc_group_add,
3197 NET_TRANSPORT_RPC,
3198 N_("Create specified group"),
3199 N_("net rpc group add\n"
3200 " Create specified group")
3203 "delete",
3204 rpc_group_delete,
3205 NET_TRANSPORT_RPC,
3206 N_("Delete specified group"),
3207 N_("net rpc group delete\n"
3208 " Delete specified group")
3211 "addmem",
3212 rpc_group_addmem,
3213 NET_TRANSPORT_RPC,
3214 N_("Add member to group"),
3215 N_("net rpc group addmem\n"
3216 " Add member to group")
3219 "delmem",
3220 rpc_group_delmem,
3221 NET_TRANSPORT_RPC,
3222 N_("Remove member from group"),
3223 N_("net rpc group delmem\n"
3224 " Remove member from group")
3227 "list",
3228 rpc_group_list,
3229 NET_TRANSPORT_RPC,
3230 N_("List groups"),
3231 N_("net rpc group list\n"
3232 " List groups")
3235 "members",
3236 rpc_group_members,
3237 NET_TRANSPORT_RPC,
3238 N_("List group members"),
3239 N_("net rpc group members\n"
3240 " List group members")
3243 "rename",
3244 rpc_group_rename,
3245 NET_TRANSPORT_RPC,
3246 N_("Rename group"),
3247 N_("net rpc group rename\n"
3248 " Rename group")
3250 {NULL, NULL, 0, NULL, NULL}
3253 status = libnetapi_net_init(&c->netapi_ctx);
3254 if (status != 0) {
3255 return -1;
3257 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3258 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3259 if (c->opt_kerberos) {
3260 libnetapi_set_use_kerberos(c->netapi_ctx);
3263 if (argc == 0) {
3264 if (c->display_usage) {
3265 d_printf(_("Usage:\n"));
3266 d_printf(_("net rpc group\n"
3267 " Alias for net rpc group list global "
3268 "local builtin\n"));
3269 net_display_usage_from_functable(func);
3270 return 0;
3273 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
3274 rpc_group_list_internals,
3275 argc, argv);
3278 return net_run_function(c, argc, argv, "net rpc group", func);
3281 /****************************************************************************/
3283 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3285 return net_share_usage(c, argc, argv);
3289 * Add a share on a remote RPC server.
3291 * @param argc Standard main() style argc.
3292 * @param argv Standard main() style argv. Initial components are already
3293 * stripped.
3295 * @return A shell status integer (0 for success).
3298 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3300 NET_API_STATUS status;
3301 char *sharename;
3302 char *path;
3303 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3304 uint32 num_users=0, perms=0;
3305 char *password=NULL; /* don't allow a share password */
3306 struct SHARE_INFO_2 i2;
3307 uint32_t parm_error = 0;
3309 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3310 return rpc_share_usage(c, argc, argv);
3313 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
3314 return -1;
3317 path = strchr(sharename, '=');
3318 if (!path) {
3319 return -1;
3322 *path++ = '\0';
3324 i2.shi2_netname = sharename;
3325 i2.shi2_type = type;
3326 i2.shi2_remark = c->opt_comment;
3327 i2.shi2_permissions = perms;
3328 i2.shi2_max_uses = c->opt_maxusers;
3329 i2.shi2_current_uses = num_users;
3330 i2.shi2_path = path;
3331 i2.shi2_passwd = password;
3333 status = NetShareAdd(c->opt_host,
3335 (uint8_t *)&i2,
3336 &parm_error);
3337 if (status != 0) {
3338 printf(_("NetShareAdd failed with: %s\n"),
3339 libnetapi_get_error_string(c->netapi_ctx, status));
3342 return status;
3346 * Delete a share on a remote RPC server.
3348 * @param domain_sid The domain sid acquired from the remote server.
3349 * @param argc Standard main() style argc.
3350 * @param argv Standard main() style argv. Initial components are already
3351 * stripped.
3353 * @return A shell status integer (0 for success).
3355 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3357 if (argc < 1 || c->display_usage) {
3358 return rpc_share_usage(c, argc, argv);
3361 return NetShareDel(c->opt_host, argv[0], 0);
3365 * Formatted print of share info
3367 * @param r pointer to SHARE_INFO_1 to format
3370 static void display_share_info_1(struct net_context *c,
3371 struct SHARE_INFO_1 *r)
3373 if (c->opt_long_list_entries) {
3374 d_printf("%-12s %-8.8s %-50s\n",
3375 r->shi1_netname,
3376 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3377 r->shi1_remark);
3378 } else {
3379 d_printf("%s\n", r->shi1_netname);
3383 static WERROR get_share_info(struct net_context *c,
3384 struct rpc_pipe_client *pipe_hnd,
3385 TALLOC_CTX *mem_ctx,
3386 uint32 level,
3387 int argc,
3388 const char **argv,
3389 struct srvsvc_NetShareInfoCtr *info_ctr)
3391 WERROR result;
3392 NTSTATUS status;
3393 union srvsvc_NetShareInfo info;
3394 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
3396 /* no specific share requested, enumerate all */
3397 if (argc == 0) {
3399 uint32_t preferred_len = 0xffffffff;
3400 uint32_t total_entries = 0;
3401 uint32_t resume_handle = 0;
3403 info_ctr->level = level;
3405 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
3406 pipe_hnd->desthost,
3407 info_ctr,
3408 preferred_len,
3409 &total_entries,
3410 &resume_handle,
3411 &result);
3412 if (!NT_STATUS_IS_OK(status)) {
3413 return ntstatus_to_werror(status);
3415 return result;
3418 /* request just one share */
3419 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
3420 pipe_hnd->desthost,
3421 argv[0],
3422 level,
3423 &info,
3424 &result);
3426 if (!NT_STATUS_IS_OK(status)) {
3427 result = ntstatus_to_werror(status);
3428 goto done;
3431 if (!W_ERROR_IS_OK(result)) {
3432 goto done;
3435 /* construct ctr */
3436 ZERO_STRUCTP(info_ctr);
3438 info_ctr->level = level;
3440 switch (level) {
3441 case 1:
3443 struct srvsvc_NetShareCtr1 *ctr1;
3445 ctr1 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr1);
3446 W_ERROR_HAVE_NO_MEMORY(ctr1);
3448 ctr1->count = 1;
3449 ctr1->array = info.info1;
3451 info_ctr->ctr.ctr1 = ctr1;
3453 break;
3455 case 2:
3457 struct srvsvc_NetShareCtr2 *ctr2;
3459 ctr2 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr2);
3460 W_ERROR_HAVE_NO_MEMORY(ctr2);
3462 ctr2->count = 1;
3463 ctr2->array = info.info2;
3465 info_ctr->ctr.ctr2 = ctr2;
3467 break;
3469 case 502:
3471 struct srvsvc_NetShareCtr502 *ctr502;
3473 ctr502 = talloc_zero(mem_ctx, struct srvsvc_NetShareCtr502);
3474 W_ERROR_HAVE_NO_MEMORY(ctr502);
3476 ctr502->count = 1;
3477 ctr502->array = info.info502;
3479 info_ctr->ctr.ctr502 = ctr502;
3481 break;
3483 } /* switch */
3484 done:
3485 return result;
3488 /***
3489 * 'net rpc share list' entrypoint.
3490 * @param argc Standard main() style argc.
3491 * @param argv Standard main() style argv. Initial components are already
3492 * stripped.
3494 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3496 NET_API_STATUS status;
3497 struct SHARE_INFO_1 *i1 = NULL;
3498 uint32_t entries_read = 0;
3499 uint32_t total_entries = 0;
3500 uint32_t resume_handle = 0;
3501 uint32_t i, level = 1;
3503 if (c->display_usage) {
3504 d_printf( "%s\n"
3505 "net rpc share list\n"
3506 " %s\n",
3507 _("Usage:"),
3508 _("List shares on remote server"));
3509 return 0;
3512 status = NetShareEnum(c->opt_host,
3513 level,
3514 (uint8_t **)(void *)&i1,
3515 (uint32_t)-1,
3516 &entries_read,
3517 &total_entries,
3518 &resume_handle);
3519 if (status != 0) {
3520 goto done;
3523 /* Display results */
3525 if (c->opt_long_list_entries) {
3526 d_printf(_(
3527 "\nEnumerating shared resources (exports) on remote server:\n\n"
3528 "\nShare name Type Description\n"
3529 "---------- ---- -----------\n"));
3531 for (i = 0; i < entries_read; i++)
3532 display_share_info_1(c, &i1[i]);
3533 done:
3534 return status;
3537 static bool check_share_availability(struct cli_state *cli, const char *netname)
3539 NTSTATUS status;
3541 status = cli_tree_connect(cli, netname, "A:", "", 0);
3542 if (!NT_STATUS_IS_OK(status)) {
3543 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3544 return false;
3547 status = cli_tdis(cli);
3548 if (!NT_STATUS_IS_OK(status)) {
3549 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3550 return false;
3553 return true;
3556 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3557 const char *netname, uint32 type)
3559 /* only support disk shares */
3560 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3561 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3562 type);
3563 return false;
3566 /* skip builtin shares */
3567 /* FIXME: should print$ be added too ? */
3568 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3569 strequal(netname,"global"))
3570 return false;
3572 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3573 printf(_("excluding [%s]\n"), netname);
3574 return false;
3577 return check_share_availability(cli, netname);
3581 * Migrate shares from a remote RPC server to the local RPC server.
3583 * All parameters are provided by the run_rpc_command function, except for
3584 * argc, argv which are passed through.
3586 * @param domain_sid The domain sid acquired from the remote server.
3587 * @param cli A cli_state connected to the server.
3588 * @param mem_ctx Talloc context, destroyed on completion of the function.
3589 * @param argc Standard main() style argc.
3590 * @param argv Standard main() style argv. Initial components are already
3591 * stripped.
3593 * @return Normal NTSTATUS return.
3596 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3597 const struct dom_sid *domain_sid,
3598 const char *domain_name,
3599 struct cli_state *cli,
3600 struct rpc_pipe_client *pipe_hnd,
3601 TALLOC_CTX *mem_ctx,
3602 int argc,
3603 const char **argv)
3605 WERROR result;
3606 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3607 struct srvsvc_NetShareInfoCtr ctr_src;
3608 uint32 i;
3609 struct rpc_pipe_client *srvsvc_pipe = NULL;
3610 struct cli_state *cli_dst = NULL;
3611 uint32 level = 502; /* includes secdesc */
3612 uint32_t parm_error = 0;
3613 struct dcerpc_binding_handle *b;
3615 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3616 &ctr_src);
3617 if (!W_ERROR_IS_OK(result))
3618 goto done;
3620 /* connect destination PI_SRVSVC */
3621 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3622 &ndr_table_srvsvc.syntax_id);
3623 if (!NT_STATUS_IS_OK(nt_status))
3624 return nt_status;
3626 b = srvsvc_pipe->binding_handle;
3628 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3630 union srvsvc_NetShareInfo info;
3631 struct srvsvc_NetShareInfo502 info502 =
3632 ctr_src.ctr.ctr502->array[i];
3634 /* reset error-code */
3635 nt_status = NT_STATUS_UNSUCCESSFUL;
3637 if (!check_share_sanity(c, cli, info502.name, info502.type))
3638 continue;
3640 /* finally add the share on the dst server */
3642 printf(_("migrating: [%s], path: %s, comment: %s, without "
3643 "share-ACLs\n"),
3644 info502.name, info502.path, info502.comment);
3646 info.info502 = &info502;
3648 nt_status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
3649 srvsvc_pipe->desthost,
3650 502,
3651 &info,
3652 &parm_error,
3653 &result);
3654 if (!NT_STATUS_IS_OK(nt_status)) {
3655 printf(_("cannot add share: %s\n"),
3656 nt_errstr(nt_status));
3657 goto done;
3659 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3660 printf(_(" [%s] does already exist\n"),
3661 info502.name);
3662 continue;
3665 if (!W_ERROR_IS_OK(result)) {
3666 nt_status = werror_to_ntstatus(result);
3667 printf(_("cannot add share: %s\n"),
3668 win_errstr(result));
3669 goto done;
3674 nt_status = NT_STATUS_OK;
3676 done:
3677 if (cli_dst) {
3678 cli_shutdown(cli_dst);
3681 return nt_status;
3686 * Migrate shares from a RPC server to another.
3688 * @param argc Standard main() style argc.
3689 * @param argv Standard main() style argv. Initial components are already
3690 * stripped.
3692 * @return A shell status integer (0 for success).
3694 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3695 const char **argv)
3697 if (c->display_usage) {
3698 d_printf( "%s\n"
3699 "net rpc share migrate shares\n"
3700 " %s\n",
3701 _("Usage:"),
3702 _("Migrate shares to local server"));
3703 return 0;
3706 if (!c->opt_host) {
3707 printf(_("no server to migrate\n"));
3708 return -1;
3711 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
3712 rpc_share_migrate_shares_internals,
3713 argc, argv);
3717 * Copy a file/dir
3719 * @param f file_info
3720 * @param mask current search mask
3721 * @param state arg-pointer
3724 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3725 const char *mask, void *state)
3727 static NTSTATUS nt_status;
3728 static struct copy_clistate *local_state;
3729 static fstring filename, new_mask;
3730 fstring dir;
3731 char *old_dir;
3732 struct net_context *c;
3734 local_state = (struct copy_clistate *)state;
3735 nt_status = NT_STATUS_UNSUCCESSFUL;
3737 c = local_state->c;
3739 if (strequal(f->name, ".") || strequal(f->name, ".."))
3740 return NT_STATUS_OK;
3742 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3744 /* DIRECTORY */
3745 if (f->mode & FILE_ATTRIBUTE_DIRECTORY) {
3747 DEBUG(3,("got dir: %s\n", f->name));
3749 fstrcpy(dir, local_state->cwd);
3750 fstrcat(dir, "\\");
3751 fstrcat(dir, f->name);
3753 switch (net_mode_share)
3755 case NET_MODE_SHARE_MIGRATE:
3756 /* create that directory */
3757 nt_status = net_copy_file(c, local_state->mem_ctx,
3758 local_state->cli_share_src,
3759 local_state->cli_share_dst,
3760 dir, dir,
3761 c->opt_acls? true : false,
3762 c->opt_attrs? true : false,
3763 c->opt_timestamps? true:false,
3764 false);
3765 break;
3766 default:
3767 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3768 return NT_STATUS_INTERNAL_ERROR;
3771 if (!NT_STATUS_IS_OK(nt_status)) {
3772 printf(_("could not handle dir %s: %s\n"),
3773 dir, nt_errstr(nt_status));
3774 return nt_status;
3777 /* search below that directory */
3778 if (strlcpy(new_mask, dir, sizeof(new_mask)) >= sizeof(new_mask)) {
3779 return NT_STATUS_NO_MEMORY;
3781 if (strlcat(new_mask, "\\*", sizeof(new_mask)) >= sizeof(new_mask)) {
3782 return NT_STATUS_NO_MEMORY;
3785 old_dir = local_state->cwd;
3786 local_state->cwd = dir;
3787 nt_status = sync_files(local_state, new_mask);
3788 if (!NT_STATUS_IS_OK(nt_status)) {
3789 printf(_("could not handle files\n"));
3791 local_state->cwd = old_dir;
3793 return nt_status;
3797 /* FILE */
3798 fstrcpy(filename, local_state->cwd);
3799 fstrcat(filename, "\\");
3800 fstrcat(filename, f->name);
3802 DEBUG(3,("got file: %s\n", filename));
3804 switch (net_mode_share)
3806 case NET_MODE_SHARE_MIGRATE:
3807 nt_status = net_copy_file(c, local_state->mem_ctx,
3808 local_state->cli_share_src,
3809 local_state->cli_share_dst,
3810 filename, filename,
3811 c->opt_acls? true : false,
3812 c->opt_attrs? true : false,
3813 c->opt_timestamps? true: false,
3814 true);
3815 break;
3816 default:
3817 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3818 net_mode_share);
3819 return NT_STATUS_INTERNAL_ERROR;
3822 if (!NT_STATUS_IS_OK(nt_status))
3823 printf(_("could not handle file %s: %s\n"),
3824 filename, nt_errstr(nt_status));
3825 return nt_status;
3829 * sync files, can be called recursivly to list files
3830 * and then call copy_fn for each file
3832 * @param cp_clistate pointer to the copy_clistate we work with
3833 * @param mask the current search mask
3835 * @return Boolean result
3837 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
3839 struct cli_state *targetcli;
3840 char *targetpath = NULL;
3841 NTSTATUS status;
3843 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3845 status = cli_resolve_path(talloc_tos(), "", NULL,
3846 cp_clistate->cli_share_src,
3847 mask, &targetcli, &targetpath);
3848 if (!NT_STATUS_IS_OK(status)) {
3849 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3850 "%s\n"),
3851 mask, nt_errstr(status));
3852 return status;
3855 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
3856 copy_fn, cp_clistate);
3857 if (!NT_STATUS_IS_OK(status)) {
3858 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3859 mask, nt_errstr(status));
3862 return status;
3867 * Set the top level directory permissions before we do any further copies.
3868 * Should set up ACL inheritance.
3871 bool copy_top_level_perms(struct net_context *c,
3872 struct copy_clistate *cp_clistate,
3873 const char *sharename)
3875 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3877 switch (net_mode_share) {
3878 case NET_MODE_SHARE_MIGRATE:
3879 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3880 nt_status = net_copy_fileattr(c,
3881 cp_clistate->mem_ctx,
3882 cp_clistate->cli_share_src,
3883 cp_clistate->cli_share_dst,
3884 "\\", "\\",
3885 c->opt_acls? true : false,
3886 c->opt_attrs? true : false,
3887 c->opt_timestamps? true: false,
3888 false);
3889 break;
3890 default:
3891 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3892 break;
3895 if (!NT_STATUS_IS_OK(nt_status)) {
3896 printf(_("Could handle directory attributes for top level "
3897 "directory of share %s. Error %s\n"),
3898 sharename, nt_errstr(nt_status));
3899 return false;
3902 return true;
3906 * Sync all files inside a remote share to another share (over smb).
3908 * All parameters are provided by the run_rpc_command function, except for
3909 * argc, argv which are passed through.
3911 * @param domain_sid The domain sid acquired from the remote server.
3912 * @param cli A cli_state connected to the server.
3913 * @param mem_ctx Talloc context, destroyed on completion of the function.
3914 * @param argc Standard main() style argc.
3915 * @param argv Standard main() style argv. Initial components are already
3916 * stripped.
3918 * @return Normal NTSTATUS return.
3921 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3922 const struct dom_sid *domain_sid,
3923 const char *domain_name,
3924 struct cli_state *cli,
3925 struct rpc_pipe_client *pipe_hnd,
3926 TALLOC_CTX *mem_ctx,
3927 int argc,
3928 const char **argv)
3930 WERROR result;
3931 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3932 struct srvsvc_NetShareInfoCtr ctr_src;
3933 uint32 i;
3934 uint32 level = 502;
3935 struct copy_clistate cp_clistate;
3936 bool got_src_share = false;
3937 bool got_dst_share = false;
3938 const char *mask = "\\*";
3939 char *dst = NULL;
3941 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3942 if (dst == NULL) {
3943 nt_status = NT_STATUS_NO_MEMORY;
3944 goto done;
3947 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3948 &ctr_src);
3950 if (!W_ERROR_IS_OK(result))
3951 goto done;
3953 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3955 struct srvsvc_NetShareInfo502 info502 =
3956 ctr_src.ctr.ctr502->array[i];
3958 if (!check_share_sanity(c, cli, info502.name, info502.type))
3959 continue;
3961 /* one might not want to mirror whole discs :) */
3962 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3963 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3964 info502.name);
3965 continue;
3968 switch (net_mode_share)
3970 case NET_MODE_SHARE_MIGRATE:
3971 printf("syncing");
3972 break;
3973 default:
3974 d_fprintf(stderr, _("Unsupported mode %d\n"),
3975 net_mode_share);
3976 break;
3978 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3979 "Attributes %s\n"),
3980 info502.name,
3981 c->opt_acls ? _("including") : _("without"),
3982 c->opt_attrs ? _("including") : _("without"),
3983 c->opt_timestamps ? _("(preserving timestamps)") : "");
3985 cp_clistate.mem_ctx = mem_ctx;
3986 cp_clistate.cli_share_src = NULL;
3987 cp_clistate.cli_share_dst = NULL;
3988 cp_clistate.cwd = NULL;
3989 cp_clistate.attribute = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
3990 cp_clistate.c = c;
3992 /* open share source */
3993 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3994 smbXcli_conn_remote_sockaddr(cli->conn),
3995 smbXcli_conn_remote_name(cli->conn),
3996 info502.name, "A:");
3997 if (!NT_STATUS_IS_OK(nt_status))
3998 goto done;
4000 got_src_share = true;
4002 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
4003 /* open share destination */
4004 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
4005 NULL, dst, info502.name, "A:");
4006 if (!NT_STATUS_IS_OK(nt_status))
4007 goto done;
4009 got_dst_share = true;
4012 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
4013 d_fprintf(stderr, _("Could not handle the top level "
4014 "directory permissions for the "
4015 "share: %s\n"), info502.name);
4016 nt_status = NT_STATUS_UNSUCCESSFUL;
4017 goto done;
4020 nt_status = sync_files(&cp_clistate, mask);
4021 if (!NT_STATUS_IS_OK(nt_status)) {
4022 d_fprintf(stderr, _("could not handle files for share: "
4023 "%s\n"), info502.name);
4024 goto done;
4028 nt_status = NT_STATUS_OK;
4030 done:
4032 if (got_src_share)
4033 cli_shutdown(cp_clistate.cli_share_src);
4035 if (got_dst_share)
4036 cli_shutdown(cp_clistate.cli_share_dst);
4038 SAFE_FREE(dst);
4039 return nt_status;
4043 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
4045 if (c->display_usage) {
4046 d_printf( "%s\n"
4047 "net share migrate files\n"
4048 " %s\n",
4049 _("Usage:"),
4050 _("Migrate files to local server"));
4051 return 0;
4054 if (!c->opt_host) {
4055 d_printf(_("no server to migrate\n"));
4056 return -1;
4059 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4060 rpc_share_migrate_files_internals,
4061 argc, argv);
4065 * Migrate share-ACLs from a remote RPC server to the local RPC server.
4067 * All parameters are provided by the run_rpc_command function, except for
4068 * argc, argv which are passed through.
4070 * @param domain_sid The domain sid acquired from the remote server.
4071 * @param cli A cli_state connected to the server.
4072 * @param mem_ctx Talloc context, destroyed on completion of the function.
4073 * @param argc Standard main() style argc.
4074 * @param argv Standard main() style argv. Initial components are already
4075 * stripped.
4077 * @return Normal NTSTATUS return.
4080 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
4081 const struct dom_sid *domain_sid,
4082 const char *domain_name,
4083 struct cli_state *cli,
4084 struct rpc_pipe_client *pipe_hnd,
4085 TALLOC_CTX *mem_ctx,
4086 int argc,
4087 const char **argv)
4089 WERROR result;
4090 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
4091 struct srvsvc_NetShareInfoCtr ctr_src;
4092 union srvsvc_NetShareInfo info;
4093 uint32 i;
4094 struct rpc_pipe_client *srvsvc_pipe = NULL;
4095 struct cli_state *cli_dst = NULL;
4096 uint32 level = 502; /* includes secdesc */
4097 uint32_t parm_error = 0;
4098 struct dcerpc_binding_handle *b;
4100 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
4101 &ctr_src);
4103 if (!W_ERROR_IS_OK(result))
4104 goto done;
4106 /* connect destination PI_SRVSVC */
4107 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
4108 &ndr_table_srvsvc.syntax_id);
4109 if (!NT_STATUS_IS_OK(nt_status))
4110 return nt_status;
4112 b = srvsvc_pipe->binding_handle;
4114 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
4116 struct srvsvc_NetShareInfo502 info502 =
4117 ctr_src.ctr.ctr502->array[i];
4119 /* reset error-code */
4120 nt_status = NT_STATUS_UNSUCCESSFUL;
4122 if (!check_share_sanity(c, cli, info502.name, info502.type))
4123 continue;
4125 printf(_("migrating: [%s], path: %s, comment: %s, including "
4126 "share-ACLs\n"),
4127 info502.name, info502.path, info502.comment);
4129 if (c->opt_verbose)
4130 display_sec_desc(info502.sd_buf.sd);
4132 /* FIXME: shouldn't we be able to just set the security descriptor ? */
4133 info.info502 = &info502;
4135 /* finally modify the share on the dst server */
4136 nt_status = dcerpc_srvsvc_NetShareSetInfo(b, mem_ctx,
4137 srvsvc_pipe->desthost,
4138 info502.name,
4139 level,
4140 &info,
4141 &parm_error,
4142 &result);
4143 if (!NT_STATUS_IS_OK(nt_status)) {
4144 printf(_("cannot set share-acl: %s\n"),
4145 nt_errstr(nt_status));
4146 goto done;
4148 if (!W_ERROR_IS_OK(result)) {
4149 nt_status = werror_to_ntstatus(result);
4150 printf(_("cannot set share-acl: %s\n"),
4151 win_errstr(result));
4152 goto done;
4157 nt_status = NT_STATUS_OK;
4159 done:
4160 if (cli_dst) {
4161 cli_shutdown(cli_dst);
4164 return nt_status;
4169 * Migrate share-acls from a RPC server to another.
4171 * @param argc Standard main() style argc.
4172 * @param argv Standard main() style argv. Initial components are already
4173 * stripped.
4175 * @return A shell status integer (0 for success).
4177 static int rpc_share_migrate_security(struct net_context *c, int argc,
4178 const char **argv)
4180 if (c->display_usage) {
4181 d_printf( "%s\n"
4182 "net rpc share migrate security\n"
4183 " %s\n",
4184 _("Usage:"),
4185 _("Migrate share-acls to local server"));
4186 return 0;
4189 if (!c->opt_host) {
4190 d_printf(_("no server to migrate\n"));
4191 return -1;
4194 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4195 rpc_share_migrate_security_internals,
4196 argc, argv);
4200 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4201 * from one server to another.
4203 * @param argc Standard main() style argc.
4204 * @param argv Standard main() style argv. Initial components are already
4205 * stripped.
4207 * @return A shell status integer (0 for success).
4210 static int rpc_share_migrate_all(struct net_context *c, int argc,
4211 const char **argv)
4213 int ret;
4215 if (c->display_usage) {
4216 d_printf( "%s\n"
4217 "net rpc share migrate all\n"
4218 " %s\n",
4219 _("Usage:"),
4220 _("Migrates shares including all share settings"));
4221 return 0;
4224 if (!c->opt_host) {
4225 d_printf(_("no server to migrate\n"));
4226 return -1;
4229 /* order is important. we don't want to be locked out by the share-acl
4230 * before copying files - gd */
4232 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4233 rpc_share_migrate_shares_internals, argc, argv);
4234 if (ret)
4235 return ret;
4237 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4238 rpc_share_migrate_files_internals, argc, argv);
4239 if (ret)
4240 return ret;
4242 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
4243 rpc_share_migrate_security_internals, argc,
4244 argv);
4249 * 'net rpc share migrate' entrypoint.
4250 * @param argc Standard main() style argc.
4251 * @param argv Standard main() style argv. Initial components are already
4252 * stripped.
4254 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4257 struct functable func[] = {
4259 "all",
4260 rpc_share_migrate_all,
4261 NET_TRANSPORT_RPC,
4262 N_("Migrate shares from remote to local server"),
4263 N_("net rpc share migrate all\n"
4264 " Migrate shares from remote to local server")
4267 "files",
4268 rpc_share_migrate_files,
4269 NET_TRANSPORT_RPC,
4270 N_("Migrate files from remote to local server"),
4271 N_("net rpc share migrate files\n"
4272 " Migrate files from remote to local server")
4275 "security",
4276 rpc_share_migrate_security,
4277 NET_TRANSPORT_RPC,
4278 N_("Migrate share-ACLs from remote to local server"),
4279 N_("net rpc share migrate security\n"
4280 " Migrate share-ACLs from remote to local server")
4283 "shares",
4284 rpc_share_migrate_shares,
4285 NET_TRANSPORT_RPC,
4286 N_("Migrate shares from remote to local server"),
4287 N_("net rpc share migrate shares\n"
4288 " Migrate shares from remote to local server")
4290 {NULL, NULL, 0, NULL, NULL}
4293 net_mode_share = NET_MODE_SHARE_MIGRATE;
4295 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4298 struct full_alias {
4299 struct dom_sid sid;
4300 uint32 num_members;
4301 struct dom_sid *members;
4304 static int num_server_aliases;
4305 static struct full_alias *server_aliases;
4308 * Add an alias to the static list.
4310 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4312 if (server_aliases == NULL)
4313 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4315 server_aliases[num_server_aliases] = *alias;
4316 num_server_aliases += 1;
4320 * For a specific domain on the server, fetch all the aliases
4321 * and their members. Add all of them to the server_aliases.
4324 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4325 TALLOC_CTX *mem_ctx,
4326 struct policy_handle *connect_pol,
4327 const struct dom_sid *domain_sid)
4329 uint32 start_idx, max_entries, num_entries, i;
4330 struct samr_SamArray *groups = NULL;
4331 NTSTATUS result, status;
4332 struct policy_handle domain_pol;
4333 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4335 /* Get domain policy handle */
4337 status = dcerpc_samr_OpenDomain(b, mem_ctx,
4338 connect_pol,
4339 MAXIMUM_ALLOWED_ACCESS,
4340 discard_const_p(struct dom_sid2, domain_sid),
4341 &domain_pol,
4342 &result);
4343 if (!NT_STATUS_IS_OK(status)) {
4344 return status;
4346 if (!NT_STATUS_IS_OK(result)) {
4347 return result;
4350 start_idx = 0;
4351 max_entries = 250;
4353 do {
4354 status = dcerpc_samr_EnumDomainAliases(b, mem_ctx,
4355 &domain_pol,
4356 &start_idx,
4357 &groups,
4358 max_entries,
4359 &num_entries,
4360 &result);
4361 if (!NT_STATUS_IS_OK(status)) {
4362 goto done;
4364 for (i = 0; i < num_entries; i++) {
4366 struct policy_handle alias_pol;
4367 struct full_alias alias;
4368 struct lsa_SidArray sid_array;
4369 int j;
4370 NTSTATUS _result;
4372 status = dcerpc_samr_OpenAlias(b, mem_ctx,
4373 &domain_pol,
4374 MAXIMUM_ALLOWED_ACCESS,
4375 groups->entries[i].idx,
4376 &alias_pol,
4377 &_result);
4378 if (!NT_STATUS_IS_OK(status)) {
4379 goto done;
4381 if (!NT_STATUS_IS_OK(_result)) {
4382 status = _result;
4383 goto done;
4386 status = dcerpc_samr_GetMembersInAlias(b, mem_ctx,
4387 &alias_pol,
4388 &sid_array,
4389 &_result);
4390 if (!NT_STATUS_IS_OK(status)) {
4391 goto done;
4393 if (!NT_STATUS_IS_OK(_result)) {
4394 status = _result;
4395 goto done;
4398 alias.num_members = sid_array.num_sids;
4400 status = dcerpc_samr_Close(b, mem_ctx, &alias_pol, &_result);
4401 if (!NT_STATUS_IS_OK(status)) {
4402 goto done;
4404 if (!NT_STATUS_IS_OK(_result)) {
4405 status = _result;
4406 goto done;
4409 alias.members = NULL;
4411 if (alias.num_members > 0) {
4412 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4414 for (j = 0; j < alias.num_members; j++)
4415 sid_copy(&alias.members[j],
4416 sid_array.sids[j].sid);
4419 sid_compose(&alias.sid, domain_sid,
4420 groups->entries[i].idx);
4422 push_alias(mem_ctx, &alias);
4424 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4426 status = NT_STATUS_OK;
4428 done:
4429 dcerpc_samr_Close(b, mem_ctx, &domain_pol, &result);
4431 return status;
4435 * Dump server_aliases as names for debugging purposes.
4438 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4439 const struct dom_sid *domain_sid,
4440 const char *domain_name,
4441 struct cli_state *cli,
4442 struct rpc_pipe_client *pipe_hnd,
4443 TALLOC_CTX *mem_ctx,
4444 int argc,
4445 const char **argv)
4447 int i;
4448 NTSTATUS result;
4449 struct policy_handle lsa_pol;
4450 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4452 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4453 SEC_FLAG_MAXIMUM_ALLOWED,
4454 &lsa_pol);
4455 if (!NT_STATUS_IS_OK(result))
4456 return result;
4458 for (i=0; i<num_server_aliases; i++) {
4459 char **names;
4460 char **domains;
4461 enum lsa_SidType *types;
4462 int j;
4464 struct full_alias *alias = &server_aliases[i];
4466 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4467 &alias->sid,
4468 &domains, &names, &types);
4469 if (!NT_STATUS_IS_OK(result))
4470 continue;
4472 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4474 if (alias->num_members == 0) {
4475 DEBUG(1, ("\n"));
4476 continue;
4479 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4480 alias->num_members,
4481 alias->members,
4482 &domains, &names, &types);
4484 if (!NT_STATUS_IS_OK(result) &&
4485 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4486 continue;
4488 for (j=0; j<alias->num_members; j++)
4489 DEBUG(1, ("%s\\%s (%d); ",
4490 domains[j] ? domains[j] : "*unknown*",
4491 names[j] ? names[j] : "*unknown*",types[j]));
4492 DEBUG(1, ("\n"));
4495 dcerpc_lsa_Close(b, mem_ctx, &lsa_pol, &result);
4497 return NT_STATUS_OK;
4501 * Fetch a list of all server aliases and their members into
4502 * server_aliases.
4505 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4506 const struct dom_sid *domain_sid,
4507 const char *domain_name,
4508 struct cli_state *cli,
4509 struct rpc_pipe_client *pipe_hnd,
4510 TALLOC_CTX *mem_ctx,
4511 int argc,
4512 const char **argv)
4514 NTSTATUS result, status;
4515 struct policy_handle connect_pol;
4516 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4518 status = dcerpc_samr_Connect2(b, mem_ctx,
4519 pipe_hnd->desthost,
4520 MAXIMUM_ALLOWED_ACCESS,
4521 &connect_pol,
4522 &result);
4523 if (!NT_STATUS_IS_OK(status)) {
4524 goto done;
4526 if (!NT_STATUS_IS_OK(result)) {
4527 status = result;
4528 goto done;
4531 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4532 &global_sid_Builtin);
4533 if (!NT_STATUS_IS_OK(status)) {
4534 goto done;
4537 status = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4538 domain_sid);
4540 dcerpc_samr_Close(b, mem_ctx, &connect_pol, &result);
4541 done:
4542 return status;
4545 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4547 token->num_sids = 4;
4549 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4550 d_fprintf(stderr, "malloc %s\n",_("failed"));
4551 token->num_sids = 0;
4552 return;
4555 token->sids[0] = *user_sid;
4556 sid_copy(&token->sids[1], &global_sid_World);
4557 sid_copy(&token->sids[2], &global_sid_Network);
4558 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4561 static void free_user_token(struct security_token *token)
4563 SAFE_FREE(token->sids);
4566 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4568 if (security_token_has_sid(token, sid))
4569 return;
4571 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4572 if (!token->sids) {
4573 return;
4576 sid_copy(&token->sids[token->num_sids], sid);
4578 token->num_sids += 1;
4581 struct user_token {
4582 fstring name;
4583 struct security_token token;
4586 static void dump_user_token(struct user_token *token)
4588 int i;
4590 d_printf("%s\n", token->name);
4592 for (i=0; i<token->token.num_sids; i++) {
4593 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4597 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4599 int i;
4601 for (i=0; i<alias->num_members; i++) {
4602 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4603 return true;
4606 return false;
4609 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4611 int i;
4613 for (i=0; i<num_server_aliases; i++) {
4614 if (is_alias_member(&sid, &server_aliases[i]))
4615 add_sid_to_token(token, &server_aliases[i].sid);
4620 * We got a user token with all the SIDs we can know about without asking the
4621 * server directly. These are the user and domain group sids. All of these can
4622 * be members of aliases. So scan the list of aliases for each of the SIDs and
4623 * add them to the token.
4626 static void collect_alias_memberships(struct security_token *token)
4628 int num_global_sids = token->num_sids;
4629 int i;
4631 for (i=0; i<num_global_sids; i++) {
4632 collect_sid_memberships(token, token->sids[i]);
4636 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4638 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4639 enum wbcSidType type;
4640 fstring full_name;
4641 struct wbcDomainSid wsid;
4642 char sid_str[WBC_SID_STRING_BUFLEN];
4643 struct dom_sid user_sid;
4644 uint32_t num_groups;
4645 gid_t *groups = NULL;
4646 uint32_t i;
4648 fstr_sprintf(full_name, "%s%c%s",
4649 domain, *lp_winbind_separator(), user);
4651 /* First let's find out the user sid */
4653 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4655 if (!WBC_ERROR_IS_OK(wbc_status)) {
4656 DEBUG(1, ("winbind could not find %s: %s\n",
4657 full_name, wbcErrorString(wbc_status)));
4658 return false;
4661 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4663 if (type != WBC_SID_NAME_USER) {
4664 DEBUG(1, ("%s is not a user\n", full_name));
4665 return false;
4668 if (!string_to_sid(&user_sid, sid_str)) {
4669 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4670 return false;
4673 init_user_token(token, &user_sid);
4675 /* And now the groups winbind knows about */
4677 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4678 if (!WBC_ERROR_IS_OK(wbc_status)) {
4679 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4680 full_name, wbcErrorString(wbc_status)));
4681 return false;
4684 for (i = 0; i < num_groups; i++) {
4685 gid_t gid = groups[i];
4686 struct dom_sid sid;
4687 bool ok;
4689 wbc_status = wbcGidToSid(gid, &wsid);
4690 if (!WBC_ERROR_IS_OK(wbc_status)) {
4691 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4692 (unsigned int)gid, wbcErrorString(wbc_status)));
4693 wbcFreeMemory(groups);
4694 return false;
4697 wbcSidToStringBuf(&wsid, sid_str, sizeof(sid_str));
4699 DEBUG(3, (" %s\n", sid_str));
4701 ok = string_to_sid(&sid, sid_str);
4702 if (!ok) {
4703 DEBUG(1, ("Failed to convert string to SID\n"));
4704 wbcFreeMemory(groups);
4705 return false;
4707 add_sid_to_token(token, &sid);
4709 wbcFreeMemory(groups);
4711 return true;
4715 * Get a list of all user tokens we want to look at
4718 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4719 struct user_token **user_tokens)
4721 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4722 uint32_t i, num_users;
4723 const char **users;
4724 struct user_token *result;
4725 TALLOC_CTX *frame = NULL;
4727 if (lp_winbind_use_default_domain() &&
4728 (c->opt_target_workgroup == NULL)) {
4729 d_fprintf(stderr, _("winbind use default domain = yes set, "
4730 "please specify a workgroup\n"));
4731 return false;
4734 /* Send request to winbind daemon */
4736 wbc_status = wbcListUsers(NULL, &num_users, &users);
4737 if (!WBC_ERROR_IS_OK(wbc_status)) {
4738 DEBUG(1, (_("winbind could not list users: %s\n"),
4739 wbcErrorString(wbc_status)));
4740 return false;
4743 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4745 if (result == NULL) {
4746 DEBUG(1, ("Could not malloc sid array\n"));
4747 wbcFreeMemory(users);
4748 return false;
4751 frame = talloc_stackframe();
4752 for (i=0; i < num_users; i++) {
4753 fstring domain, user;
4754 char *p;
4756 fstrcpy(result[i].name, users[i]);
4758 p = strchr(users[i], *lp_winbind_separator());
4760 DEBUG(3, ("%s\n", users[i]));
4762 if (p == NULL) {
4763 fstrcpy(domain, c->opt_target_workgroup);
4764 fstrcpy(user, users[i]);
4765 } else {
4766 *p++ = '\0';
4767 fstrcpy(domain, users[i]);
4768 if (!strupper_m(domain)) {
4769 DEBUG(1, ("strupper_m %s failed\n", domain));
4770 wbcFreeMemory(users);
4771 return false;
4773 fstrcpy(user, p);
4776 get_user_sids(domain, user, &(result[i].token));
4778 TALLOC_FREE(frame);
4779 wbcFreeMemory(users);
4781 *num_tokens = num_users;
4782 *user_tokens = result;
4784 return true;
4787 static bool get_user_tokens_from_file(FILE *f,
4788 int *num_tokens,
4789 struct user_token **tokens)
4791 struct user_token *token = NULL;
4793 while (!feof(f)) {
4794 fstring line;
4796 if (fgets(line, sizeof(line)-1, f) == NULL) {
4797 return true;
4800 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4801 line[strlen(line)-1] = '\0';
4804 if (line[0] == ' ') {
4805 /* We have a SID */
4807 struct dom_sid sid;
4808 if(!string_to_sid(&sid, &line[1])) {
4809 DEBUG(1,("get_user_tokens_from_file: Could "
4810 "not convert sid %s \n",&line[1]));
4811 return false;
4814 if (token == NULL) {
4815 DEBUG(0, ("File does not begin with username"));
4816 return false;
4819 add_sid_to_token(&token->token, &sid);
4820 continue;
4823 /* And a new user... */
4825 *num_tokens += 1;
4826 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4827 if (*tokens == NULL) {
4828 DEBUG(0, ("Could not realloc tokens\n"));
4829 return false;
4832 token = &((*tokens)[*num_tokens-1]);
4834 if (strlcpy(token->name, line, sizeof(token->name)) >= sizeof(token->name)) {
4835 return false;
4837 token->token.num_sids = 0;
4838 token->token.sids = NULL;
4839 continue;
4842 return false;
4847 * Show the list of all users that have access to a share
4850 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4851 TALLOC_CTX *mem_ctx,
4852 const char *netname,
4853 int num_tokens,
4854 struct user_token *tokens)
4856 uint16_t fnum;
4857 struct security_descriptor *share_sd = NULL;
4858 struct security_descriptor *root_sd = NULL;
4859 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4860 int i;
4861 union srvsvc_NetShareInfo info;
4862 WERROR result;
4863 NTSTATUS status;
4864 uint16 cnum;
4865 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
4867 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
4868 pipe_hnd->desthost,
4869 netname,
4870 502,
4871 &info,
4872 &result);
4874 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4875 DEBUG(1, ("Coult not query secdesc for share %s\n",
4876 netname));
4877 return;
4880 share_sd = info.info502->sd_buf.sd;
4881 if (share_sd == NULL) {
4882 DEBUG(1, ("Got no secdesc for share %s\n",
4883 netname));
4886 cnum = cli_state_get_tid(cli);
4888 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, netname, "A:", "", 0))) {
4889 return;
4892 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4893 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4894 cli_query_secdesc(cli, fnum, mem_ctx, &root_sd);
4897 for (i=0; i<num_tokens; i++) {
4898 uint32 acc_granted;
4900 if (share_sd != NULL) {
4901 status = se_access_check(share_sd, &tokens[i].token,
4902 1, &acc_granted);
4904 if (!NT_STATUS_IS_OK(status)) {
4905 DEBUG(1, ("Could not check share_sd for "
4906 "user %s\n",
4907 tokens[i].name));
4908 continue;
4912 if (root_sd == NULL) {
4913 d_printf(" %s\n", tokens[i].name);
4914 continue;
4917 status = se_access_check(root_sd, &tokens[i].token,
4918 1, &acc_granted);
4919 if (!NT_STATUS_IS_OK(status)) {
4920 DEBUG(1, ("Could not check root_sd for user %s\n",
4921 tokens[i].name));
4922 continue;
4924 d_printf(" %s\n", tokens[i].name);
4927 if (fnum != (uint16_t)-1)
4928 cli_close(cli, fnum);
4929 cli_tdis(cli);
4930 cli_state_set_tid(cli, cnum);
4932 return;
4936 * List shares on a remote RPC server, including the security descriptors.
4938 * All parameters are provided by the run_rpc_command function, except for
4939 * argc, argv which are passed through.
4941 * @param domain_sid The domain sid acquired from the remote server.
4942 * @param cli A cli_state connected to the server.
4943 * @param mem_ctx Talloc context, destroyed on completion of the function.
4944 * @param argc Standard main() style argc.
4945 * @param argv Standard main() style argv. Initial components are already
4946 * stripped.
4948 * @return Normal NTSTATUS return.
4951 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4952 const struct dom_sid *domain_sid,
4953 const char *domain_name,
4954 struct cli_state *cli,
4955 struct rpc_pipe_client *pipe_hnd,
4956 TALLOC_CTX *mem_ctx,
4957 int argc,
4958 const char **argv)
4960 bool r;
4961 FILE *f;
4962 NTSTATUS nt_status = NT_STATUS_OK;
4963 uint32_t total_entries = 0;
4964 uint32_t resume_handle = 0;
4965 uint32_t preferred_len = 0xffffffff;
4966 uint32_t i;
4967 struct dcerpc_binding_handle *b = NULL;
4968 struct srvsvc_NetShareInfoCtr info_ctr;
4969 struct srvsvc_NetShareCtr1 ctr1;
4970 WERROR result;
4972 struct user_token *tokens = NULL;
4973 int num_tokens = 0;
4975 if (argc == 0) {
4976 f = stdin;
4977 } else {
4978 f = fopen(argv[0], "r");
4981 if (f == NULL) {
4982 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4983 return NT_STATUS_UNSUCCESSFUL;
4986 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4988 if (f != stdin)
4989 fclose(f);
4991 if (!r) {
4992 DEBUG(0, ("Could not read users from file\n"));
4993 return NT_STATUS_UNSUCCESSFUL;
4996 for (i=0; i<num_tokens; i++)
4997 collect_alias_memberships(&tokens[i].token);
4999 ZERO_STRUCT(info_ctr);
5000 ZERO_STRUCT(ctr1);
5002 info_ctr.level = 1;
5003 info_ctr.ctr.ctr1 = &ctr1;
5005 b = pipe_hnd->binding_handle;
5007 /* Issue the NetShareEnum RPC call and retrieve the response */
5008 nt_status = dcerpc_srvsvc_NetShareEnumAll(b,
5009 talloc_tos(),
5010 pipe_hnd->desthost,
5011 &info_ctr,
5012 preferred_len,
5013 &total_entries,
5014 &resume_handle,
5015 &result);
5017 /* Was it successful? */
5018 if (!NT_STATUS_IS_OK(nt_status)) {
5019 /* Nope. Go clean up. */
5020 goto done;
5023 if (!W_ERROR_IS_OK(result)) {
5024 /* Nope. Go clean up. */
5025 nt_status = werror_to_ntstatus(result);
5026 goto done;
5029 if (total_entries == 0) {
5030 goto done;
5033 /* For each returned entry... */
5034 for (i = 0; i < info_ctr.ctr.ctr1->count; i++) {
5035 const char *netname = info_ctr.ctr.ctr1->array[i].name;
5037 if (info_ctr.ctr.ctr1->array[i].type != STYPE_DISKTREE) {
5038 continue;
5041 d_printf("%s\n", netname);
5043 show_userlist(pipe_hnd, mem_ctx, netname,
5044 num_tokens, tokens);
5046 done:
5047 for (i=0; i<num_tokens; i++) {
5048 free_user_token(&tokens[i].token);
5050 SAFE_FREE(tokens);
5052 return nt_status;
5055 static int rpc_share_allowedusers(struct net_context *c, int argc,
5056 const char **argv)
5058 int result;
5060 if (c->display_usage) {
5061 d_printf( "%s\n"
5062 "net rpc share allowedusers\n"
5063 " %s\n",
5064 _("Usage:"),
5065 _("List allowed users"));
5066 return 0;
5069 result = run_rpc_command(c, NULL, &ndr_table_samr, 0,
5070 rpc_aliaslist_internals,
5071 argc, argv);
5072 if (result != 0)
5073 return result;
5075 result = run_rpc_command(c, NULL, &ndr_table_lsarpc, 0,
5076 rpc_aliaslist_dump,
5077 argc, argv);
5078 if (result != 0)
5079 return result;
5081 return run_rpc_command(c, NULL, &ndr_table_srvsvc, 0,
5082 rpc_share_allowedusers_internals,
5083 argc, argv);
5086 int net_usersidlist(struct net_context *c, int argc, const char **argv)
5088 int num_tokens = 0;
5089 struct user_token *tokens = NULL;
5090 int i;
5092 if (argc != 0) {
5093 net_usersidlist_usage(c, argc, argv);
5094 return 0;
5097 if (!get_user_tokens(c, &num_tokens, &tokens)) {
5098 DEBUG(0, ("Could not get the user/sid list\n"));
5099 return -1;
5102 for (i=0; i<num_tokens; i++) {
5103 dump_user_token(&tokens[i]);
5104 free_user_token(&tokens[i].token);
5107 SAFE_FREE(tokens);
5108 return 0;
5111 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
5113 d_printf(_("net usersidlist\n"
5114 "\tprints out a list of all users the running winbind knows\n"
5115 "\tabout, together with all their SIDs. This is used as\n"
5116 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
5118 net_common_flags_usage(c, argc, argv);
5119 return -1;
5123 * 'net rpc share' entrypoint.
5124 * @param argc Standard main() style argc.
5125 * @param argv Standard main() style argv. Initial components are already
5126 * stripped.
5129 int net_rpc_share(struct net_context *c, int argc, const char **argv)
5131 NET_API_STATUS status;
5133 struct functable func[] = {
5135 "add",
5136 rpc_share_add,
5137 NET_TRANSPORT_RPC,
5138 N_("Add share"),
5139 N_("net rpc share add\n"
5140 " Add share")
5143 "delete",
5144 rpc_share_delete,
5145 NET_TRANSPORT_RPC,
5146 N_("Remove share"),
5147 N_("net rpc share delete\n"
5148 " Remove share")
5151 "allowedusers",
5152 rpc_share_allowedusers,
5153 NET_TRANSPORT_RPC,
5154 N_("Modify allowed users"),
5155 N_("net rpc share allowedusers\n"
5156 " Modify allowed users")
5159 "migrate",
5160 rpc_share_migrate,
5161 NET_TRANSPORT_RPC,
5162 N_("Migrate share to local server"),
5163 N_("net rpc share migrate\n"
5164 " Migrate share to local server")
5167 "list",
5168 rpc_share_list,
5169 NET_TRANSPORT_RPC,
5170 N_("List shares"),
5171 N_("net rpc share list\n"
5172 " List shares")
5174 {NULL, NULL, 0, NULL, NULL}
5177 status = libnetapi_net_init(&c->netapi_ctx);
5178 if (status != 0) {
5179 return -1;
5181 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5182 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5183 if (c->opt_kerberos) {
5184 libnetapi_set_use_kerberos(c->netapi_ctx);
5187 if (argc == 0) {
5188 if (c->display_usage) {
5189 d_printf("%s\n%s",
5190 _("Usage:"),
5191 _("net rpc share\n"
5192 " List shares\n"
5193 " Alias for net rpc share list\n"));
5194 net_display_usage_from_functable(func);
5195 return 0;
5198 return rpc_share_list(c, argc, argv);
5201 return net_run_function(c, argc, argv, "net rpc share", func);
5204 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5205 TALLOC_CTX *mem_ctx,
5206 struct rpc_sh_ctx *ctx,
5207 struct rpc_pipe_client *pipe_hnd,
5208 int argc, const char **argv)
5211 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
5214 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5215 TALLOC_CTX *mem_ctx,
5216 struct rpc_sh_ctx *ctx,
5217 struct rpc_pipe_client *pipe_hnd,
5218 int argc, const char **argv)
5220 NET_API_STATUS status;
5221 uint32_t parm_err = 0;
5222 struct SHARE_INFO_2 i2;
5224 if ((argc < 2) || (argc > 3)) {
5225 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
5226 ctx->whoami);
5227 return NT_STATUS_INVALID_PARAMETER;
5230 i2.shi2_netname = argv[0];
5231 i2.shi2_type = STYPE_DISKTREE;
5232 i2.shi2_remark = (argc == 3) ? argv[2] : "";
5233 i2.shi2_permissions = 0;
5234 i2.shi2_max_uses = 0;
5235 i2.shi2_current_uses = 0;
5236 i2.shi2_path = argv[1];
5237 i2.shi2_passwd = NULL;
5239 status = NetShareAdd(pipe_hnd->desthost,
5241 (uint8_t *)&i2,
5242 &parm_err);
5244 return werror_to_ntstatus(W_ERROR(status));
5247 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5248 TALLOC_CTX *mem_ctx,
5249 struct rpc_sh_ctx *ctx,
5250 struct rpc_pipe_client *pipe_hnd,
5251 int argc, const char **argv)
5253 if (argc != 1) {
5254 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5255 return NT_STATUS_INVALID_PARAMETER;
5258 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
5261 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5262 TALLOC_CTX *mem_ctx,
5263 struct rpc_sh_ctx *ctx,
5264 struct rpc_pipe_client *pipe_hnd,
5265 int argc, const char **argv)
5267 union srvsvc_NetShareInfo info;
5268 WERROR result;
5269 NTSTATUS status;
5270 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5272 if (argc != 1) {
5273 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
5274 return NT_STATUS_INVALID_PARAMETER;
5277 status = dcerpc_srvsvc_NetShareGetInfo(b, mem_ctx,
5278 pipe_hnd->desthost,
5279 argv[0],
5281 &info,
5282 &result);
5283 if (!NT_STATUS_IS_OK(status)) {
5284 result = ntstatus_to_werror(status);
5285 goto done;
5287 if (!W_ERROR_IS_OK(result)) {
5288 goto done;
5291 d_printf(_("Name: %s\n"), info.info2->name);
5292 d_printf(_("Comment: %s\n"), info.info2->comment);
5293 d_printf(_("Path: %s\n"), info.info2->path);
5294 d_printf(_("Password: %s\n"), info.info2->password);
5296 done:
5297 return werror_to_ntstatus(result);
5300 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5301 struct rpc_sh_ctx *ctx)
5303 static struct rpc_sh_cmd cmds[] = {
5305 { "list", NULL, &ndr_table_srvsvc, rpc_sh_share_list,
5306 N_("List available shares") },
5308 { "add", NULL, &ndr_table_srvsvc, rpc_sh_share_add,
5309 N_("Add a share") },
5311 { "delete", NULL, &ndr_table_srvsvc, rpc_sh_share_delete,
5312 N_("Delete a share") },
5314 { "info", NULL, &ndr_table_srvsvc, rpc_sh_share_info,
5315 N_("Get information about a share") },
5317 { NULL, NULL, 0, NULL, NULL }
5320 return cmds;
5323 /****************************************************************************/
5325 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5327 return net_file_usage(c, argc, argv);
5331 * Close a file on a remote RPC server.
5333 * @param argc Standard main() style argc.
5334 * @param argv Standard main() style argv. Initial components are already
5335 * stripped.
5337 * @return A shell status integer (0 for success).
5339 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5341 if (argc < 1 || c->display_usage) {
5342 return rpc_file_usage(c, argc, argv);
5345 return NetFileClose(c->opt_host, atoi(argv[0]));
5349 * Formatted print of open file info
5351 * @param r struct FILE_INFO_3 contents
5354 static void display_file_info_3(struct FILE_INFO_3 *r)
5356 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5357 r->fi3_id, r->fi3_username, r->fi3_permissions,
5358 r->fi3_num_locks, r->fi3_pathname);
5362 * List files for a user on a remote RPC server.
5364 * @param argc Standard main() style argc.
5365 * @param argv Standard main() style argv. Initial components are already
5366 * stripped.
5368 * @return A shell status integer (0 for success)..
5371 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5373 NET_API_STATUS status;
5374 uint32 preferred_len = 0xffffffff, i;
5375 const char *username=NULL;
5376 uint32_t total_entries = 0;
5377 uint32_t entries_read = 0;
5378 uint32_t resume_handle = 0;
5379 struct FILE_INFO_3 *i3 = NULL;
5381 if (c->display_usage) {
5382 return rpc_file_usage(c, argc, argv);
5385 /* if argc > 0, must be user command */
5386 if (argc > 0) {
5387 username = smb_xstrdup(argv[0]);
5390 status = NetFileEnum(c->opt_host,
5391 NULL,
5392 username,
5394 (uint8_t **)(void *)&i3,
5395 preferred_len,
5396 &entries_read,
5397 &total_entries,
5398 &resume_handle);
5400 if (status != 0) {
5401 goto done;
5404 /* Display results */
5406 d_printf(_(
5407 "\nEnumerating open files on remote server:\n\n"
5408 "\nFileId Opened by Perms Locks Path"
5409 "\n------ --------- ----- ----- ---- \n"));
5410 for (i = 0; i < entries_read; i++) {
5411 display_file_info_3(&i3[i]);
5413 done:
5414 return status;
5418 * 'net rpc file' entrypoint.
5419 * @param argc Standard main() style argc.
5420 * @param argv Standard main() style argv. Initial components are already
5421 * stripped.
5424 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5426 NET_API_STATUS status;
5428 struct functable func[] = {
5430 "close",
5431 rpc_file_close,
5432 NET_TRANSPORT_RPC,
5433 N_("Close opened file"),
5434 N_("net rpc file close\n"
5435 " Close opened file")
5438 "user",
5439 rpc_file_user,
5440 NET_TRANSPORT_RPC,
5441 N_("List files opened by user"),
5442 N_("net rpc file user\n"
5443 " List files opened by user")
5445 #if 0
5447 "info",
5448 rpc_file_info,
5449 NET_TRANSPORT_RPC,
5450 N_("Display information about opened file"),
5451 N_("net rpc file info\n"
5452 " Display information about opened file")
5454 #endif
5455 {NULL, NULL, 0, NULL, NULL}
5458 status = libnetapi_net_init(&c->netapi_ctx);
5459 if (status != 0) {
5460 return -1;
5462 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5463 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5464 if (c->opt_kerberos) {
5465 libnetapi_set_use_kerberos(c->netapi_ctx);
5468 if (argc == 0) {
5469 if (c->display_usage) {
5470 d_printf(_("Usage:\n"));
5471 d_printf(_("net rpc file\n"
5472 " List opened files\n"));
5473 net_display_usage_from_functable(func);
5474 return 0;
5477 return rpc_file_user(c, argc, argv);
5480 return net_run_function(c, argc, argv, "net rpc file", func);
5484 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5486 * All parameters are provided by the run_rpc_command function, except for
5487 * argc, argv which are passed through.
5489 * @param c A net_context structure.
5490 * @param domain_sid The domain sid acquired from the remote server.
5491 * @param cli A cli_state connected to the server.
5492 * @param mem_ctx Talloc context, destroyed on completion of the function.
5493 * @param argc Standard main() style argc.
5494 * @param argv Standard main() style argv. Initial components are already
5495 * stripped.
5497 * @return Normal NTSTATUS return.
5500 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5501 const struct dom_sid *domain_sid,
5502 const char *domain_name,
5503 struct cli_state *cli,
5504 struct rpc_pipe_client *pipe_hnd,
5505 TALLOC_CTX *mem_ctx,
5506 int argc,
5507 const char **argv)
5509 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5510 WERROR result;
5511 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5513 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5514 if (!NT_STATUS_IS_OK(status)) {
5515 return status;
5517 if (W_ERROR_IS_OK(result)) {
5518 d_printf(_("\nShutdown successfully aborted\n"));
5519 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5520 } else
5521 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5523 return werror_to_ntstatus(result);
5527 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5529 * All parameters are provided by the run_rpc_command function, except for
5530 * argc, argv which are passed through.
5532 * @param c A net_context structure.
5533 * @param domain_sid The domain sid acquired from the remote server.
5534 * @param cli A cli_state connected to the server.
5535 * @param mem_ctx Talloc context, destroyed on completion of the function.
5536 * @param argc Standard main() style argc.
5537 * @param argv Standard main() style argv. Initial components are already
5538 * stripped.
5540 * @return Normal NTSTATUS return.
5543 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5544 const struct dom_sid *domain_sid,
5545 const char *domain_name,
5546 struct cli_state *cli,
5547 struct rpc_pipe_client *pipe_hnd,
5548 TALLOC_CTX *mem_ctx,
5549 int argc,
5550 const char **argv)
5552 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5553 WERROR werr;
5554 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5556 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5558 if (!NT_STATUS_IS_OK(result)) {
5559 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5560 return result;
5562 if (W_ERROR_IS_OK(werr)) {
5563 d_printf(_("\nShutdown successfully aborted\n"));
5564 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5565 } else
5566 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5568 return werror_to_ntstatus(werr);
5572 * ABORT the shutdown of a remote RPC server.
5574 * @param argc Standard main() style argc.
5575 * @param argv Standard main() style argv. Initial components are already
5576 * stripped.
5578 * @return A shell status integer (0 for success).
5581 static int rpc_shutdown_abort(struct net_context *c, int argc,
5582 const char **argv)
5584 int rc = -1;
5586 if (c->display_usage) {
5587 d_printf( "%s\n"
5588 "net rpc abortshutdown\n"
5589 " %s\n",
5590 _("Usage:"),
5591 _("Abort a scheduled shutdown"));
5592 return 0;
5595 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5596 rpc_shutdown_abort_internals, argc, argv);
5598 if (rc == 0)
5599 return rc;
5601 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5603 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5604 rpc_reg_shutdown_abort_internals,
5605 argc, argv);
5609 * Shut down a remote RPC Server via initshutdown pipe.
5611 * All parameters are provided by the run_rpc_command function, except for
5612 * argc, argv which are passed through.
5614 * @param c A net_context structure.
5615 * @param domain_sid The domain sid acquired from the remote server.
5616 * @param cli A cli_state connected to the server.
5617 * @param mem_ctx Talloc context, destroyed on completion of the function.
5618 * @param argc Standard main() style argc.
5619 * @param argv Standard main() style argv. Initial components are already
5620 * stripped.
5622 * @return Normal NTSTATUS return.
5625 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5626 const struct dom_sid *domain_sid,
5627 const char *domain_name,
5628 struct cli_state *cli,
5629 struct rpc_pipe_client *pipe_hnd,
5630 TALLOC_CTX *mem_ctx,
5631 int argc,
5632 const char **argv)
5634 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5635 WERROR result;
5636 const char *msg = N_("This machine will be shutdown shortly");
5637 uint32 timeout = 20;
5638 struct lsa_StringLarge msg_string;
5639 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5641 if (c->opt_comment) {
5642 msg = c->opt_comment;
5644 if (c->opt_timeout) {
5645 timeout = c->opt_timeout;
5648 msg_string.string = msg;
5650 /* create an entry */
5651 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5652 &msg_string, timeout, c->opt_force, c->opt_reboot,
5653 &result);
5654 if (!NT_STATUS_IS_OK(status)) {
5655 return status;
5657 if (W_ERROR_IS_OK(result)) {
5658 d_printf(_("\nShutdown of remote machine succeeded\n"));
5659 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5660 } else {
5661 DEBUG(1,("Shutdown of remote machine failed!\n"));
5663 return werror_to_ntstatus(result);
5667 * Shut down a remote RPC Server via winreg pipe.
5669 * All parameters are provided by the run_rpc_command function, except for
5670 * argc, argv which are passed through.
5672 * @param c A net_context structure.
5673 * @param domain_sid The domain sid acquired from the remote server.
5674 * @param cli A cli_state connected to the server.
5675 * @param mem_ctx Talloc context, destroyed on completion of the function.
5676 * @param argc Standard main() style argc.
5677 * @param argv Standard main() style argv. Initial components are already
5678 * stripped.
5680 * @return Normal NTSTATUS return.
5683 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5684 const struct dom_sid *domain_sid,
5685 const char *domain_name,
5686 struct cli_state *cli,
5687 struct rpc_pipe_client *pipe_hnd,
5688 TALLOC_CTX *mem_ctx,
5689 int argc,
5690 const char **argv)
5692 const char *msg = N_("This machine will be shutdown shortly");
5693 uint32 timeout = 20;
5694 struct lsa_StringLarge msg_string;
5695 NTSTATUS result;
5696 WERROR werr;
5697 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5699 if (c->opt_comment) {
5700 msg = c->opt_comment;
5702 msg_string.string = msg;
5704 if (c->opt_timeout) {
5705 timeout = c->opt_timeout;
5708 /* create an entry */
5709 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5710 &msg_string, timeout, c->opt_force, c->opt_reboot,
5711 &werr);
5712 if (!NT_STATUS_IS_OK(result)) {
5713 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5714 return result;
5717 if (W_ERROR_IS_OK(werr)) {
5718 d_printf(_("\nShutdown of remote machine succeeded\n"));
5719 } else {
5720 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5721 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5722 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5723 else
5724 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5727 return werror_to_ntstatus(werr);
5731 * Shut down a remote RPC server.
5733 * @param argc Standard main() style argc.
5734 * @param argv Standard main() style argv. Initial components are already
5735 * stripped.
5737 * @return A shell status integer (0 for success).
5740 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5742 int rc = -1;
5744 if (c->display_usage) {
5745 d_printf( "%s\n"
5746 "net rpc shutdown\n"
5747 " %s\n",
5748 _("Usage:"),
5749 _("Shut down a remote RPC server"));
5750 return 0;
5753 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5754 rpc_init_shutdown_internals, argc, argv);
5756 if (rc) {
5757 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5758 rc = run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5759 rpc_reg_shutdown_internals, argc, argv);
5762 return rc;
5765 /***************************************************************************
5766 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5767 ***************************************************************************/
5770 * Add interdomain trust account to the RPC server.
5771 * All parameters (except for argc and argv) are passed by run_rpc_command
5772 * function.
5774 * @param c A net_context structure.
5775 * @param domain_sid The domain sid acquired from the server.
5776 * @param cli A cli_state connected to the server.
5777 * @param mem_ctx Talloc context, destroyed on completion of the function.
5778 * @param argc Standard main() style argc.
5779 * @param argv Standard main() style argv. Initial components are already
5780 * stripped.
5782 * @return normal NTSTATUS return code.
5785 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5786 const struct dom_sid *domain_sid,
5787 const char *domain_name,
5788 struct cli_state *cli,
5789 struct rpc_pipe_client *pipe_hnd,
5790 TALLOC_CTX *mem_ctx,
5791 int argc,
5792 const char **argv)
5794 struct policy_handle connect_pol, domain_pol, user_pol;
5795 NTSTATUS status, result;
5796 char *acct_name;
5797 struct lsa_String lsa_acct_name;
5798 uint32 acb_info;
5799 uint32 acct_flags=0;
5800 uint32 user_rid;
5801 uint32_t access_granted = 0;
5802 union samr_UserInfo info;
5803 unsigned int orig_timeout;
5804 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5805 DATA_BLOB session_key = data_blob_null;
5807 if (argc != 2) {
5808 d_printf("%s\n%s",
5809 _("Usage:"),
5810 _(" net rpc trustdom add <domain_name> "
5811 "<trust password>\n"));
5812 return NT_STATUS_INVALID_PARAMETER;
5816 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5819 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5820 return NT_STATUS_NO_MEMORY;
5823 if (!strupper_m(acct_name)) {
5824 SAFE_FREE(acct_name);
5825 return NT_STATUS_INVALID_PARAMETER;
5828 init_lsa_String(&lsa_acct_name, acct_name);
5830 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
5831 if (!NT_STATUS_IS_OK(status)) {
5832 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
5833 nt_errstr(status)));
5834 goto done;
5837 /* Get samr policy handle */
5838 status = dcerpc_samr_Connect2(b, mem_ctx,
5839 pipe_hnd->desthost,
5840 MAXIMUM_ALLOWED_ACCESS,
5841 &connect_pol,
5842 &result);
5843 if (!NT_STATUS_IS_OK(status)) {
5844 goto done;
5846 if (!NT_STATUS_IS_OK(result)) {
5847 status = result;
5848 goto done;
5851 /* Get domain policy handle */
5852 status = dcerpc_samr_OpenDomain(b, mem_ctx,
5853 &connect_pol,
5854 MAXIMUM_ALLOWED_ACCESS,
5855 discard_const_p(struct dom_sid2, domain_sid),
5856 &domain_pol,
5857 &result);
5858 if (!NT_STATUS_IS_OK(status)) {
5859 goto done;
5861 if (!NT_STATUS_IS_OK(result)) {
5862 status = result;
5863 goto done;
5866 /* This call can take a long time - allow the server to time out.
5867 * 35 seconds should do it. */
5869 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5871 /* Create trusting domain's account */
5872 acb_info = ACB_NORMAL;
5873 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5874 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5875 SAMR_USER_ACCESS_SET_PASSWORD |
5876 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5877 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5879 status = dcerpc_samr_CreateUser2(b, mem_ctx,
5880 &domain_pol,
5881 &lsa_acct_name,
5882 acb_info,
5883 acct_flags,
5884 &user_pol,
5885 &access_granted,
5886 &user_rid,
5887 &result);
5888 if (!NT_STATUS_IS_OK(status)) {
5889 goto done;
5891 /* And restore our original timeout. */
5892 rpccli_set_timeout(pipe_hnd, orig_timeout);
5894 if (!NT_STATUS_IS_OK(result)) {
5895 status = result;
5896 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5897 acct_name, nt_errstr(result));
5898 goto done;
5902 struct samr_CryptPassword crypt_pwd;
5904 ZERO_STRUCT(info.info23);
5906 init_samr_CryptPassword(argv[1],
5907 &session_key,
5908 &crypt_pwd);
5910 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5911 SAMR_FIELD_NT_PASSWORD_PRESENT;
5912 info.info23.info.acct_flags = ACB_DOMTRUST;
5913 info.info23.password = crypt_pwd;
5915 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
5916 &user_pol,
5918 &info,
5919 &result);
5920 if (!NT_STATUS_IS_OK(status)) {
5921 goto done;
5924 if (!NT_STATUS_IS_OK(result)) {
5925 status = result;
5926 DEBUG(0,("Could not set trust account password: %s\n",
5927 nt_errstr(result)));
5928 goto done;
5932 done:
5933 SAFE_FREE(acct_name);
5934 data_blob_clear_free(&session_key);
5935 return status;
5939 * Create interdomain trust account for a remote domain.
5941 * @param argc Standard argc.
5942 * @param argv Standard argv without initial components.
5944 * @return Integer status (0 means success).
5947 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5949 if (argc > 0 && !c->display_usage) {
5950 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
5951 rpc_trustdom_add_internals, argc, argv);
5952 } else {
5953 d_printf("%s\n%s",
5954 _("Usage:"),
5955 _("net rpc trustdom add <domain_name> <trust "
5956 "password>\n"));
5957 return -1;
5963 * Remove interdomain trust account from the RPC server.
5964 * All parameters (except for argc and argv) are passed by run_rpc_command
5965 * function.
5967 * @param c A net_context structure.
5968 * @param domain_sid The domain sid acquired from the server.
5969 * @param cli A cli_state connected to the server.
5970 * @param mem_ctx Talloc context, destroyed on completion of the function.
5971 * @param argc Standard main() style argc.
5972 * @param argv Standard main() style argv. Initial components are already
5973 * stripped.
5975 * @return normal NTSTATUS return code.
5978 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5979 const struct dom_sid *domain_sid,
5980 const char *domain_name,
5981 struct cli_state *cli,
5982 struct rpc_pipe_client *pipe_hnd,
5983 TALLOC_CTX *mem_ctx,
5984 int argc,
5985 const char **argv)
5987 struct policy_handle connect_pol, domain_pol, user_pol;
5988 NTSTATUS status, result;
5989 char *acct_name;
5990 struct dom_sid trust_acct_sid;
5991 struct samr_Ids user_rids, name_types;
5992 struct lsa_String lsa_acct_name;
5993 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5995 if (argc != 1) {
5996 d_printf("%s\n%s",
5997 _("Usage:"),
5998 _(" net rpc trustdom del <domain_name>\n"));
5999 return NT_STATUS_INVALID_PARAMETER;
6003 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6005 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
6007 if (acct_name == NULL)
6008 return NT_STATUS_NO_MEMORY;
6010 if (!strupper_m(acct_name)) {
6011 TALLOC_FREE(acct_name);
6012 return NT_STATUS_INVALID_PARAMETER;
6015 /* Get samr policy handle */
6016 status = dcerpc_samr_Connect2(b, mem_ctx,
6017 pipe_hnd->desthost,
6018 MAXIMUM_ALLOWED_ACCESS,
6019 &connect_pol,
6020 &result);
6021 if (!NT_STATUS_IS_OK(status)) {
6022 goto done;
6024 if (!NT_STATUS_IS_OK(result)) {
6025 status = result;
6026 goto done;
6029 /* Get domain policy handle */
6030 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6031 &connect_pol,
6032 MAXIMUM_ALLOWED_ACCESS,
6033 discard_const_p(struct dom_sid2, domain_sid),
6034 &domain_pol,
6035 &result);
6036 if (!NT_STATUS_IS_OK(status)) {
6037 goto done;
6039 if (!NT_STATUS_IS_OK(result)) {
6040 status = result;
6041 goto done;
6044 init_lsa_String(&lsa_acct_name, acct_name);
6046 status = dcerpc_samr_LookupNames(b, mem_ctx,
6047 &domain_pol,
6049 &lsa_acct_name,
6050 &user_rids,
6051 &name_types,
6052 &result);
6053 if (!NT_STATUS_IS_OK(status)) {
6054 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6055 "failed %s\n"),
6056 acct_name, nt_errstr(status));
6057 goto done;
6059 if (!NT_STATUS_IS_OK(result)) {
6060 status = result;
6061 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6062 "failed %s\n"),
6063 acct_name, nt_errstr(result) );
6064 goto done;
6067 status = dcerpc_samr_OpenUser(b, mem_ctx,
6068 &domain_pol,
6069 MAXIMUM_ALLOWED_ACCESS,
6070 user_rids.ids[0],
6071 &user_pol,
6072 &result);
6073 if (!NT_STATUS_IS_OK(status)) {
6074 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6075 "%s\n"),
6076 acct_name, nt_errstr(status) );
6077 goto done;
6080 if (!NT_STATUS_IS_OK(result)) {
6081 status = result;
6082 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6083 "%s\n"),
6084 acct_name, nt_errstr(result) );
6085 goto done;
6088 /* append the rid to the domain sid */
6089 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6090 goto done;
6093 /* remove the sid */
6095 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6096 &user_pol,
6097 &trust_acct_sid,
6098 &result);
6099 if (!NT_STATUS_IS_OK(status)) {
6100 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6101 " on user %s failed %s\n"),
6102 acct_name, nt_errstr(status));
6103 goto done;
6105 if (!NT_STATUS_IS_OK(result)) {
6106 status = result;
6107 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6108 " on user %s failed %s\n"),
6109 acct_name, nt_errstr(result) );
6110 goto done;
6114 /* Delete user */
6116 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6117 &user_pol,
6118 &result);
6119 if (!NT_STATUS_IS_OK(status)) {
6120 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6121 "%s\n"),
6122 acct_name, nt_errstr(status));
6123 goto done;
6126 if (!NT_STATUS_IS_OK(result)) {
6127 result = status;
6128 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6129 "%s\n"),
6130 acct_name, nt_errstr(result) );
6131 goto done;
6134 if (!NT_STATUS_IS_OK(result)) {
6135 d_printf(_("Could not set trust account password: %s\n"),
6136 nt_errstr(result));
6137 goto done;
6140 done:
6141 return status;
6145 * Delete interdomain trust account for a remote domain.
6147 * @param argc Standard argc.
6148 * @param argv Standard argv without initial components.
6150 * @return Integer status (0 means success).
6153 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6155 if (argc > 0 && !c->display_usage) {
6156 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6157 rpc_trustdom_del_internals, argc, argv);
6158 } else {
6159 d_printf("%s\n%s",
6160 _("Usage:"),
6161 _("net rpc trustdom del <domain>\n"));
6162 return -1;
6166 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6167 struct cli_state *cli,
6168 TALLOC_CTX *mem_ctx,
6169 const char *domain_name)
6171 char *dc_name = NULL;
6172 const char *buffer = NULL;
6173 struct rpc_pipe_client *netr;
6174 NTSTATUS status;
6175 WERROR result;
6176 struct dcerpc_binding_handle *b;
6178 /* Use NetServerEnum2 */
6180 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6181 SAFE_FREE(dc_name);
6182 return NT_STATUS_OK;
6185 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6186 for domain %s\n", domain_name));
6188 /* Try netr_GetDcName */
6190 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
6191 &netr);
6192 if (!NT_STATUS_IS_OK(status)) {
6193 return status;
6196 b = netr->binding_handle;
6198 status = dcerpc_netr_GetDcName(b, mem_ctx,
6199 netr->desthost,
6200 domain_name,
6201 &buffer,
6202 &result);
6203 TALLOC_FREE(netr);
6205 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6206 return status;
6209 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6210 for domain %s\n", domain_name));
6212 if (!NT_STATUS_IS_OK(status)) {
6213 return status;
6216 return werror_to_ntstatus(result);
6220 * Establish trust relationship to a trusting domain.
6221 * Interdomain account must already be created on remote PDC.
6223 * @param c A net_context structure.
6224 * @param argc Standard argc.
6225 * @param argv Standard argv without initial components.
6227 * @return Integer status (0 means success).
6230 static int rpc_trustdom_establish(struct net_context *c, int argc,
6231 const char **argv)
6233 struct cli_state *cli = NULL;
6234 struct sockaddr_storage server_ss;
6235 struct rpc_pipe_client *pipe_hnd = NULL;
6236 struct policy_handle connect_hnd;
6237 TALLOC_CTX *mem_ctx;
6238 NTSTATUS nt_status, result;
6239 struct dom_sid *domain_sid;
6241 char* domain_name;
6242 char* acct_name;
6243 fstring pdc_name;
6244 union lsa_PolicyInformation *info = NULL;
6245 struct dcerpc_binding_handle *b;
6248 * Connect to \\server\ipc$ as 'our domain' account with password
6251 if (argc != 1 || c->display_usage) {
6252 d_printf("%s\n%s",
6253 _("Usage:"),
6254 _("net rpc trustdom establish <domain_name>\n"));
6255 return -1;
6258 domain_name = smb_xstrdup(argv[0]);
6259 if (!strupper_m(domain_name)) {
6260 SAFE_FREE(domain_name);
6261 return -1;
6264 /* account name used at first is our domain's name with '$' */
6265 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6266 return -1;
6268 if (!strupper_m(acct_name)) {
6269 SAFE_FREE(domain_name);
6270 SAFE_FREE(acct_name);
6271 return -1;
6275 * opt_workgroup will be used by connection functions further,
6276 * hence it should be set to remote domain name instead of ours
6278 if (c->opt_workgroup) {
6279 c->opt_workgroup = smb_xstrdup(domain_name);
6282 c->opt_user_name = acct_name;
6284 /* find the domain controller */
6285 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6286 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6287 return -1;
6290 /* connect to ipc$ as username/password */
6291 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6292 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6294 /* Is it trusting domain account for sure ? */
6295 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6296 nt_errstr(nt_status)));
6297 return -1;
6300 /* store who we connected to */
6302 saf_store( domain_name, pdc_name );
6305 * Connect to \\server\ipc$ again (this time anonymously)
6308 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6309 (char*)pdc_name);
6311 if (NT_STATUS_IS_ERR(nt_status)) {
6312 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6313 domain_name, nt_errstr(nt_status)));
6314 return -1;
6317 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6318 "domain %s", domain_name))) {
6319 DEBUG(0, ("talloc_init() failed\n"));
6320 cli_shutdown(cli);
6321 return -1;
6324 /* Make sure we're talking to a proper server */
6326 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6327 if (!NT_STATUS_IS_OK(nt_status)) {
6328 cli_shutdown(cli);
6329 talloc_destroy(mem_ctx);
6330 return -1;
6334 * Call LsaOpenPolicy and LsaQueryInfo
6337 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6338 &pipe_hnd);
6339 if (!NT_STATUS_IS_OK(nt_status)) {
6340 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6341 cli_shutdown(cli);
6342 talloc_destroy(mem_ctx);
6343 return -1;
6346 b = pipe_hnd->binding_handle;
6348 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6349 &connect_hnd);
6350 if (NT_STATUS_IS_ERR(nt_status)) {
6351 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6352 nt_errstr(nt_status)));
6353 cli_shutdown(cli);
6354 talloc_destroy(mem_ctx);
6355 return -1;
6358 /* Querying info level 5 */
6360 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6361 &connect_hnd,
6362 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6363 &info,
6364 &result);
6365 if (NT_STATUS_IS_ERR(nt_status)) {
6366 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6367 nt_errstr(nt_status)));
6368 cli_shutdown(cli);
6369 talloc_destroy(mem_ctx);
6370 return -1;
6372 if (NT_STATUS_IS_ERR(result)) {
6373 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6374 nt_errstr(result)));
6375 cli_shutdown(cli);
6376 talloc_destroy(mem_ctx);
6377 return -1;
6380 domain_sid = info->account_domain.sid;
6382 /* There should be actually query info level 3 (following nt serv behaviour),
6383 but I still don't know if it's _really_ necessary */
6386 * Store the password in secrets db
6389 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6390 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6391 cli_shutdown(cli);
6392 talloc_destroy(mem_ctx);
6393 return -1;
6397 * Close the pipes and clean up
6400 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6401 if (NT_STATUS_IS_ERR(nt_status)) {
6402 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6403 nt_errstr(nt_status)));
6404 cli_shutdown(cli);
6405 talloc_destroy(mem_ctx);
6406 return -1;
6409 cli_shutdown(cli);
6411 talloc_destroy(mem_ctx);
6413 d_printf(_("Trust to domain %s established\n"), domain_name);
6414 return 0;
6418 * Revoke trust relationship to the remote domain.
6420 * @param c A net_context structure.
6421 * @param argc Standard argc.
6422 * @param argv Standard argv without initial components.
6424 * @return Integer status (0 means success).
6427 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6428 const char **argv)
6430 char* domain_name;
6431 int rc = -1;
6433 if (argc < 1 || c->display_usage) {
6434 d_printf("%s\n%s",
6435 _("Usage:"),
6436 _("net rpc trustdom revoke <domain_name>\n"
6437 " Revoke trust relationship\n"
6438 " domain_name\tName of domain to revoke trust\n"));
6439 return -1;
6442 /* generate upper cased domain name */
6443 domain_name = smb_xstrdup(argv[0]);
6444 if (!strupper_m(domain_name)) {
6445 SAFE_FREE(domain_name);
6446 return -1;
6449 /* delete password of the trust */
6450 if (!pdb_del_trusteddom_pw(domain_name)) {
6451 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6452 domain_name));
6453 goto done;
6456 rc = 0;
6457 done:
6458 SAFE_FREE(domain_name);
6459 return rc;
6462 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6463 const struct dom_sid *domain_sid,
6464 const char *domain_name,
6465 struct cli_state *cli,
6466 struct rpc_pipe_client *pipe_hnd,
6467 TALLOC_CTX *mem_ctx,
6468 int argc,
6469 const char **argv)
6471 fstring str_sid;
6472 if (!sid_to_fstring(str_sid, domain_sid)) {
6473 return NT_STATUS_UNSUCCESSFUL;
6475 d_printf("%s\n", str_sid);
6476 return NT_STATUS_OK;
6479 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6481 fstring ascii_sid;
6483 /* convert sid into ascii string */
6484 sid_to_fstring(ascii_sid, dom_sid);
6486 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
6489 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6490 TALLOC_CTX *mem_ctx,
6491 struct policy_handle *pol,
6492 struct dom_sid dom_sid,
6493 const char *trusted_dom_name)
6495 NTSTATUS nt_status, result;
6496 union lsa_TrustedDomainInfo *info = NULL;
6497 char *cleartextpwd = NULL;
6498 DATA_BLOB session_key;
6499 DATA_BLOB data = data_blob_null;
6500 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6502 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6503 pol,
6504 &dom_sid,
6505 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6506 &info,
6507 &result);
6508 if (NT_STATUS_IS_ERR(nt_status)) {
6509 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6510 nt_errstr(nt_status)));
6511 goto done;
6513 if (NT_STATUS_IS_ERR(result)) {
6514 nt_status = result;
6515 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6516 nt_errstr(result)));
6517 goto done;
6520 data = data_blob(info->password.password->data,
6521 info->password.password->length);
6523 nt_status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6524 if (!NT_STATUS_IS_OK(nt_status)) {
6525 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status)));
6526 goto done;
6529 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key);
6530 data_blob_free(&session_key);
6532 if (cleartextpwd == NULL) {
6533 DEBUG(0,("retrieved NULL password\n"));
6534 nt_status = NT_STATUS_UNSUCCESSFUL;
6535 goto done;
6538 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6539 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6540 nt_status = NT_STATUS_UNSUCCESSFUL;
6541 goto done;
6544 #ifdef DEBUG_PASSWORD
6545 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6546 "password: [%s]\n", trusted_dom_name,
6547 sid_string_dbg(&dom_sid), cleartextpwd));
6548 #endif
6550 done:
6551 SAFE_FREE(cleartextpwd);
6552 data_blob_free(&data);
6554 return nt_status;
6557 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6558 const char **argv)
6560 /* common variables */
6561 TALLOC_CTX* mem_ctx;
6562 struct cli_state *cli = NULL;
6563 struct rpc_pipe_client *pipe_hnd = NULL;
6564 NTSTATUS nt_status, result;
6565 const char *domain_name = NULL;
6566 struct policy_handle connect_hnd;
6567 union lsa_PolicyInformation *info = NULL;
6569 /* trusted domains listing variables */
6570 unsigned int enum_ctx = 0;
6571 int i;
6572 struct lsa_DomainList dom_list;
6573 fstring pdc_name;
6574 struct dcerpc_binding_handle *b;
6576 if (c->display_usage) {
6577 d_printf( "%s\n"
6578 "net rpc trustdom vampire\n"
6579 " %s\n",
6580 _("Usage:"),
6581 _("Vampire trust relationship from remote server"));
6582 return 0;
6586 * Listing trusted domains (stored in secrets.tdb, if local)
6589 mem_ctx = talloc_init("trust relationships vampire");
6592 * set domain and pdc name to local samba server (default)
6593 * or to remote one given in command line
6596 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6597 domain_name = c->opt_workgroup;
6598 c->opt_target_workgroup = c->opt_workgroup;
6599 } else {
6600 fstrcpy(pdc_name, lp_netbios_name());
6601 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6602 c->opt_target_workgroup = domain_name;
6605 /* open \PIPE\lsarpc and open policy handle */
6606 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6607 if (!NT_STATUS_IS_OK(nt_status)) {
6608 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6609 nt_errstr(nt_status)));
6610 talloc_destroy(mem_ctx);
6611 return -1;
6614 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6615 &pipe_hnd);
6616 if (!NT_STATUS_IS_OK(nt_status)) {
6617 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6618 nt_errstr(nt_status) ));
6619 cli_shutdown(cli);
6620 talloc_destroy(mem_ctx);
6621 return -1;
6624 b = pipe_hnd->binding_handle;
6626 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6627 &connect_hnd);
6628 if (NT_STATUS_IS_ERR(nt_status)) {
6629 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6630 nt_errstr(nt_status)));
6631 cli_shutdown(cli);
6632 talloc_destroy(mem_ctx);
6633 return -1;
6636 /* query info level 5 to obtain sid of a domain being queried */
6637 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6638 &connect_hnd,
6639 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6640 &info,
6641 &result);
6643 if (NT_STATUS_IS_ERR(nt_status)) {
6644 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6645 nt_errstr(nt_status)));
6646 cli_shutdown(cli);
6647 talloc_destroy(mem_ctx);
6648 return -1;
6650 if (NT_STATUS_IS_ERR(result)) {
6651 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6652 nt_errstr(result)));
6653 cli_shutdown(cli);
6654 talloc_destroy(mem_ctx);
6655 return -1;
6659 * Keep calling LsaEnumTrustdom over opened pipe until
6660 * the end of enumeration is reached
6663 d_printf(_("Vampire trusted domains:\n\n"));
6665 do {
6666 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6667 &connect_hnd,
6668 &enum_ctx,
6669 &dom_list,
6670 (uint32_t)-1,
6671 &result);
6672 if (NT_STATUS_IS_ERR(nt_status)) {
6673 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6674 nt_errstr(nt_status)));
6675 cli_shutdown(cli);
6676 talloc_destroy(mem_ctx);
6677 return -1;
6679 if (NT_STATUS_IS_ERR(result)) {
6680 nt_status = result;
6681 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6682 nt_errstr(result)));
6683 cli_shutdown(cli);
6684 talloc_destroy(mem_ctx);
6685 return -1;
6689 for (i = 0; i < dom_list.count; i++) {
6691 print_trusted_domain(dom_list.domains[i].sid,
6692 dom_list.domains[i].name.string);
6694 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6695 *dom_list.domains[i].sid,
6696 dom_list.domains[i].name.string);
6697 if (!NT_STATUS_IS_OK(nt_status)) {
6698 cli_shutdown(cli);
6699 talloc_destroy(mem_ctx);
6700 return -1;
6705 * in case of no trusted domains say something rather
6706 * than just display blank line
6708 if (!dom_list.count) d_printf(_("none\n"));
6710 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6712 /* close this connection before doing next one */
6713 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6714 if (NT_STATUS_IS_ERR(nt_status)) {
6715 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6716 nt_errstr(nt_status)));
6717 cli_shutdown(cli);
6718 talloc_destroy(mem_ctx);
6719 return -1;
6722 /* close lsarpc pipe and connection to IPC$ */
6723 cli_shutdown(cli);
6725 talloc_destroy(mem_ctx);
6726 return 0;
6729 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6731 /* common variables */
6732 TALLOC_CTX* mem_ctx;
6733 struct cli_state *cli = NULL, *remote_cli = NULL;
6734 struct rpc_pipe_client *pipe_hnd = NULL;
6735 NTSTATUS nt_status, result;
6736 const char *domain_name = NULL;
6737 struct dom_sid *queried_dom_sid;
6738 int ascii_dom_name_len;
6739 struct policy_handle connect_hnd;
6740 union lsa_PolicyInformation *info = NULL;
6741 struct dcerpc_binding_handle *b = NULL;
6743 /* trusted domains listing variables */
6744 unsigned int num_domains, enum_ctx = 0;
6745 int i;
6746 struct lsa_DomainList dom_list;
6747 fstring pdc_name;
6748 bool found_domain;
6750 /* trusting domains listing variables */
6751 struct policy_handle domain_hnd;
6752 struct samr_SamArray *trusts = NULL;
6754 if (c->display_usage) {
6755 d_printf( "%s\n"
6756 "net rpc trustdom list\n"
6757 " %s\n",
6758 _("Usage:"),
6759 _("List incoming and outgoing trust relationships"));
6760 return 0;
6764 * Listing trusted domains (stored in secrets.tdb, if local)
6767 mem_ctx = talloc_init("trust relationships listing");
6770 * set domain and pdc name to local samba server (default)
6771 * or to remote one given in command line
6774 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6775 domain_name = c->opt_workgroup;
6776 c->opt_target_workgroup = c->opt_workgroup;
6777 } else {
6778 fstrcpy(pdc_name, lp_netbios_name());
6779 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6780 c->opt_target_workgroup = domain_name;
6783 /* open \PIPE\lsarpc and open policy handle */
6784 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6785 if (!NT_STATUS_IS_OK(nt_status)) {
6786 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6787 nt_errstr(nt_status)));
6788 talloc_destroy(mem_ctx);
6789 return -1;
6792 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6793 &pipe_hnd);
6794 if (!NT_STATUS_IS_OK(nt_status)) {
6795 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6796 nt_errstr(nt_status) ));
6797 cli_shutdown(cli);
6798 talloc_destroy(mem_ctx);
6799 return -1;
6802 b = pipe_hnd->binding_handle;
6804 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6805 &connect_hnd);
6806 if (NT_STATUS_IS_ERR(nt_status)) {
6807 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6808 nt_errstr(nt_status)));
6809 cli_shutdown(cli);
6810 talloc_destroy(mem_ctx);
6811 return -1;
6814 /* query info level 5 to obtain sid of a domain being queried */
6815 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6816 &connect_hnd,
6817 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6818 &info,
6819 &result);
6821 if (NT_STATUS_IS_ERR(nt_status)) {
6822 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6823 nt_errstr(nt_status)));
6824 cli_shutdown(cli);
6825 talloc_destroy(mem_ctx);
6826 return -1;
6828 if (NT_STATUS_IS_ERR(result)) {
6829 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6830 nt_errstr(result)));
6831 cli_shutdown(cli);
6832 talloc_destroy(mem_ctx);
6833 return -1;
6836 queried_dom_sid = info->account_domain.sid;
6839 * Keep calling LsaEnumTrustdom over opened pipe until
6840 * the end of enumeration is reached
6843 d_printf(_("Trusted domains list:\n\n"));
6845 found_domain = false;
6847 do {
6848 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6849 &connect_hnd,
6850 &enum_ctx,
6851 &dom_list,
6852 (uint32_t)-1,
6853 &result);
6854 if (NT_STATUS_IS_ERR(nt_status)) {
6855 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6856 nt_errstr(nt_status)));
6857 cli_shutdown(cli);
6858 talloc_destroy(mem_ctx);
6859 return -1;
6861 if (NT_STATUS_IS_ERR(result)) {
6862 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6863 nt_errstr(result)));
6864 cli_shutdown(cli);
6865 talloc_destroy(mem_ctx);
6866 return -1;
6870 for (i = 0; i < dom_list.count; i++) {
6871 print_trusted_domain(dom_list.domains[i].sid,
6872 dom_list.domains[i].name.string);
6873 found_domain = true;
6877 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6880 * in case of no trusted domains say something rather
6881 * than just display blank line
6883 if (!found_domain) {
6884 d_printf(_("none\n"));
6887 /* close this connection before doing next one */
6888 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6889 if (NT_STATUS_IS_ERR(nt_status)) {
6890 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6891 nt_errstr(nt_status)));
6892 cli_shutdown(cli);
6893 talloc_destroy(mem_ctx);
6894 return -1;
6897 TALLOC_FREE(pipe_hnd);
6900 * Listing trusting domains (stored in passdb backend, if local)
6903 d_printf(_("\nTrusting domains list:\n\n"));
6906 * Open \PIPE\samr and get needed policy handles
6908 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6909 &pipe_hnd);
6910 if (!NT_STATUS_IS_OK(nt_status)) {
6911 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6912 cli_shutdown(cli);
6913 talloc_destroy(mem_ctx);
6914 return -1;
6917 b = pipe_hnd->binding_handle;
6919 /* SamrConnect2 */
6920 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
6921 pipe_hnd->desthost,
6922 SAMR_ACCESS_LOOKUP_DOMAIN,
6923 &connect_hnd,
6924 &result);
6925 if (!NT_STATUS_IS_OK(nt_status)) {
6926 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6927 nt_errstr(nt_status)));
6928 cli_shutdown(cli);
6929 talloc_destroy(mem_ctx);
6930 return -1;
6932 if (!NT_STATUS_IS_OK(result)) {
6933 nt_status = result;
6934 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6935 nt_errstr(result)));
6936 cli_shutdown(cli);
6937 talloc_destroy(mem_ctx);
6938 return -1;
6941 /* SamrOpenDomain - we have to open domain policy handle in order to be
6942 able to enumerate accounts*/
6943 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
6944 &connect_hnd,
6945 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6946 queried_dom_sid,
6947 &domain_hnd,
6948 &result);
6949 if (!NT_STATUS_IS_OK(nt_status)) {
6950 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6951 nt_errstr(nt_status)));
6952 cli_shutdown(cli);
6953 talloc_destroy(mem_ctx);
6954 return -1;
6956 if (!NT_STATUS_IS_OK(result)) {
6957 nt_status = result;
6958 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6959 nt_errstr(result)));
6960 cli_shutdown(cli);
6961 talloc_destroy(mem_ctx);
6962 return -1;
6966 * perform actual enumeration
6969 found_domain = false;
6971 enum_ctx = 0; /* reset enumeration context from last enumeration */
6972 do {
6974 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
6975 &domain_hnd,
6976 &enum_ctx,
6977 ACB_DOMTRUST,
6978 &trusts,
6979 0xffff,
6980 &num_domains,
6981 &result);
6982 if (NT_STATUS_IS_ERR(nt_status)) {
6983 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6984 nt_errstr(nt_status)));
6985 cli_shutdown(cli);
6986 talloc_destroy(mem_ctx);
6987 return -1;
6989 if (NT_STATUS_IS_ERR(result)) {
6990 nt_status = result;
6991 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6992 nt_errstr(result)));
6993 cli_shutdown(cli);
6994 talloc_destroy(mem_ctx);
6995 return -1;
6998 for (i = 0; i < num_domains; i++) {
7000 char *str = discard_const_p(char, trusts->entries[i].name.string);
7002 found_domain = true;
7005 * get each single domain's sid (do we _really_ need this ?):
7006 * 1) connect to domain's pdc
7007 * 2) query the pdc for domain's sid
7010 /* get rid of '$' tail */
7011 ascii_dom_name_len = strlen(str);
7012 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
7013 str[ascii_dom_name_len - 1] = '\0';
7015 /* set opt_* variables to remote domain */
7016 if (!strupper_m(str)) {
7017 cli_shutdown(cli);
7018 talloc_destroy(mem_ctx);
7019 return -1;
7021 c->opt_workgroup = talloc_strdup(mem_ctx, str);
7022 c->opt_target_workgroup = c->opt_workgroup;
7024 d_printf("%-20s", str);
7026 /* connect to remote domain controller */
7027 nt_status = net_make_ipc_connection(c,
7028 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
7029 &remote_cli);
7030 if (NT_STATUS_IS_OK(nt_status)) {
7031 /* query for domain's sid */
7032 if (run_rpc_command(
7033 c, remote_cli,
7034 &ndr_table_lsarpc, 0,
7035 rpc_query_domain_sid, argc,
7036 argv))
7037 d_printf(_("strange - couldn't get domain's sid\n"));
7039 cli_shutdown(remote_cli);
7041 } else {
7042 d_fprintf(stderr, _("domain controller is not "
7043 "responding: %s\n"),
7044 nt_errstr(nt_status));
7045 d_printf(_("couldn't get domain's sid\n"));
7049 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
7051 if (!found_domain) {
7052 d_printf("none\n");
7055 /* close opened samr and domain policy handles */
7056 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
7057 if (!NT_STATUS_IS_OK(nt_status)) {
7058 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
7061 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
7062 if (!NT_STATUS_IS_OK(nt_status)) {
7063 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
7066 /* close samr pipe and connection to IPC$ */
7067 cli_shutdown(cli);
7069 talloc_destroy(mem_ctx);
7070 return 0;
7074 * Entrypoint for 'net rpc trustdom' code.
7076 * @param argc Standard argc.
7077 * @param argv Standard argv without initial components.
7079 * @return Integer status (0 means success).
7082 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7084 struct functable func[] = {
7086 "add",
7087 rpc_trustdom_add,
7088 NET_TRANSPORT_RPC,
7089 N_("Add trusting domain's account"),
7090 N_("net rpc trustdom add\n"
7091 " Add trusting domain's account")
7094 "del",
7095 rpc_trustdom_del,
7096 NET_TRANSPORT_RPC,
7097 N_("Remove trusting domain's account"),
7098 N_("net rpc trustdom del\n"
7099 " Remove trusting domain's account")
7102 "establish",
7103 rpc_trustdom_establish,
7104 NET_TRANSPORT_RPC,
7105 N_("Establish outgoing trust relationship"),
7106 N_("net rpc trustdom establish\n"
7107 " Establish outgoing trust relationship")
7110 "revoke",
7111 rpc_trustdom_revoke,
7112 NET_TRANSPORT_RPC,
7113 N_("Revoke outgoing trust relationship"),
7114 N_("net rpc trustdom revoke\n"
7115 " Revoke outgoing trust relationship")
7118 "list",
7119 rpc_trustdom_list,
7120 NET_TRANSPORT_RPC,
7121 N_("List in- and outgoing domain trusts"),
7122 N_("net rpc trustdom list\n"
7123 " List in- and outgoing domain trusts")
7126 "vampire",
7127 rpc_trustdom_vampire,
7128 NET_TRANSPORT_RPC,
7129 N_("Vampire trusts from remote server"),
7130 N_("net rpc trustdom vampire\n"
7131 " Vampire trusts from remote server")
7133 {NULL, NULL, 0, NULL, NULL}
7136 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7140 * Check if a server will take rpc commands
7141 * @param flags Type of server to connect to (PDC, DMB, localhost)
7142 * if the host is not explicitly specified
7143 * @return bool (true means rpc supported)
7145 bool net_rpc_check(struct net_context *c, unsigned flags)
7147 struct cli_state *cli;
7148 bool ret = false;
7149 struct sockaddr_storage server_ss;
7150 char *server_name = NULL;
7151 NTSTATUS status;
7153 /* flags (i.e. server type) may depend on command */
7154 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7155 return false;
7157 status = cli_connect_nb(server_name, &server_ss, 0, 0x20,
7158 lp_netbios_name(), SMB_SIGNING_DEFAULT,
7159 0, &cli);
7160 if (!NT_STATUS_IS_OK(status)) {
7161 return false;
7163 status = smbXcli_negprot(cli->conn, cli->timeout, PROTOCOL_CORE,
7164 PROTOCOL_NT1);
7165 if (!NT_STATUS_IS_OK(status))
7166 goto done;
7167 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_NT1)
7168 goto done;
7170 ret = true;
7171 done:
7172 cli_shutdown(cli);
7173 return ret;
7176 /* dump sam database via samsync rpc calls */
7177 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
7178 if (c->display_usage) {
7179 d_printf( "%s\n"
7180 "net rpc samdump\n"
7181 " %s\n",
7182 _("Usage:"),
7183 _("Dump remote SAM database"));
7184 return 0;
7187 return run_rpc_command(c, NULL, &ndr_table_netlogon,
7188 NET_FLAGS_ANONYMOUS,
7189 rpc_samdump_internals, argc, argv);
7192 /* syncronise sam database via samsync rpc calls */
7193 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7195 struct functable func[] = {
7197 "ldif",
7198 rpc_vampire_ldif,
7199 NET_TRANSPORT_RPC,
7200 N_("Dump remote SAM database to ldif"),
7201 N_("net rpc vampire ldif\n"
7202 " Dump remote SAM database to LDIF file or "
7203 "stdout")
7206 "keytab",
7207 rpc_vampire_keytab,
7208 NET_TRANSPORT_RPC,
7209 N_("Dump remote SAM database to Kerberos Keytab"),
7210 N_("net rpc vampire keytab\n"
7211 " Dump remote SAM database to Kerberos keytab "
7212 "file")
7215 "passdb",
7216 rpc_vampire_passdb,
7217 NET_TRANSPORT_RPC,
7218 N_("Dump remote SAM database to passdb"),
7219 N_("net rpc vampire passdb\n"
7220 " Dump remote SAM database to passdb")
7223 {NULL, NULL, 0, NULL, NULL}
7226 if (argc == 0) {
7227 if (c->display_usage) {
7228 d_printf( "%s\n"
7229 "net rpc vampire\n"
7230 " %s\n",
7231 _("Usage:"),
7232 _("Vampire remote SAM database"));
7233 return 0;
7236 return rpc_vampire_passdb(c, argc, argv);
7239 return net_run_function(c, argc, argv, "net rpc vampire", func);
7243 * Migrate everything from a print server.
7245 * @param c A net_context structure.
7246 * @param argc Standard main() style argc.
7247 * @param argv Standard main() style argv. Initial components are already
7248 * stripped.
7250 * @return A shell status integer (0 for success).
7252 * The order is important !
7253 * To successfully add drivers the print queues have to exist !
7254 * Applying ACLs should be the last step, because you're easily locked out.
7257 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7258 const char **argv)
7260 int ret;
7262 if (c->display_usage) {
7263 d_printf( "%s\n"
7264 "net rpc printer migrate all\n"
7265 " %s\n",
7266 _("Usage:"),
7267 _("Migrate everything from a print server"));
7268 return 0;
7271 if (!c->opt_host) {
7272 d_printf(_("no server to migrate\n"));
7273 return -1;
7276 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7277 rpc_printer_migrate_printers_internals, argc,
7278 argv);
7279 if (ret)
7280 return ret;
7282 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7283 rpc_printer_migrate_drivers_internals, argc,
7284 argv);
7285 if (ret)
7286 return ret;
7288 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7289 rpc_printer_migrate_forms_internals, argc, argv);
7290 if (ret)
7291 return ret;
7293 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7294 rpc_printer_migrate_settings_internals, argc,
7295 argv);
7296 if (ret)
7297 return ret;
7299 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7300 rpc_printer_migrate_security_internals, argc,
7301 argv);
7306 * Migrate print drivers from a print server.
7308 * @param c A net_context structure.
7309 * @param argc Standard main() style argc.
7310 * @param argv Standard main() style argv. Initial components are already
7311 * stripped.
7313 * @return A shell status integer (0 for success).
7315 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7316 const char **argv)
7318 if (c->display_usage) {
7319 d_printf( "%s\n"
7320 "net rpc printer migrate drivers\n"
7321 " %s\n",
7322 _("Usage:"),
7323 _("Migrate print-drivers from a print-server"));
7324 return 0;
7327 if (!c->opt_host) {
7328 d_printf(_("no server to migrate\n"));
7329 return -1;
7332 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7333 rpc_printer_migrate_drivers_internals,
7334 argc, argv);
7338 * Migrate print-forms from a print-server.
7340 * @param c A net_context structure.
7341 * @param argc Standard main() style argc.
7342 * @param argv Standard main() style argv. Initial components are already
7343 * stripped.
7345 * @return A shell status integer (0 for success).
7347 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7348 const char **argv)
7350 if (c->display_usage) {
7351 d_printf( "%s\n"
7352 "net rpc printer migrate forms\n"
7353 " %s\n",
7354 _("Usage:"),
7355 _("Migrate print-forms from a print-server"));
7356 return 0;
7359 if (!c->opt_host) {
7360 d_printf(_("no server to migrate\n"));
7361 return -1;
7364 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7365 rpc_printer_migrate_forms_internals,
7366 argc, argv);
7370 * Migrate printers from a print-server.
7372 * @param c A net_context structure.
7373 * @param argc Standard main() style argc.
7374 * @param argv Standard main() style argv. Initial components are already
7375 * stripped.
7377 * @return A shell status integer (0 for success).
7379 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7380 const char **argv)
7382 if (c->display_usage) {
7383 d_printf( "%s\n"
7384 "net rpc printer migrate printers\n"
7385 " %s\n",
7386 _("Usage:"),
7387 _("Migrate printers from a print-server"));
7388 return 0;
7391 if (!c->opt_host) {
7392 d_printf(_("no server to migrate\n"));
7393 return -1;
7396 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7397 rpc_printer_migrate_printers_internals,
7398 argc, argv);
7402 * Migrate printer-ACLs from a print-server
7404 * @param c A net_context structure.
7405 * @param argc Standard main() style argc.
7406 * @param argv Standard main() style argv. Initial components are already
7407 * stripped.
7409 * @return A shell status integer (0 for success).
7411 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7412 const char **argv)
7414 if (c->display_usage) {
7415 d_printf( "%s\n"
7416 "net rpc printer migrate security\n"
7417 " %s\n",
7418 _("Usage:"),
7419 _("Migrate printer-ACLs from a print-server"));
7420 return 0;
7423 if (!c->opt_host) {
7424 d_printf(_("no server to migrate\n"));
7425 return -1;
7428 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7429 rpc_printer_migrate_security_internals,
7430 argc, argv);
7434 * Migrate printer-settings from a print-server.
7436 * @param c A net_context structure.
7437 * @param argc Standard main() style argc.
7438 * @param argv Standard main() style argv. Initial components are already
7439 * stripped.
7441 * @return A shell status integer (0 for success).
7443 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7444 const char **argv)
7446 if (c->display_usage) {
7447 d_printf( "%s\n"
7448 "net rpc printer migrate settings\n"
7449 " %s\n",
7450 _("Usage:"),
7451 _("Migrate printer-settings from a "
7452 "print-server"));
7453 return 0;
7456 if (!c->opt_host) {
7457 d_printf(_("no server to migrate\n"));
7458 return -1;
7461 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7462 rpc_printer_migrate_settings_internals,
7463 argc, argv);
7467 * 'net rpc printer' entrypoint.
7469 * @param c A net_context structure.
7470 * @param argc Standard main() style argc.
7471 * @param argv Standard main() style argv. Initial components are already
7472 * stripped.
7475 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7478 /* ouch: when addriver and setdriver are called from within
7479 rpc_printer_migrate_drivers_internals, the printer-queue already
7480 *has* to exist */
7482 struct functable func[] = {
7484 "all",
7485 rpc_printer_migrate_all,
7486 NET_TRANSPORT_RPC,
7487 N_("Migrate all from remote to local print server"),
7488 N_("net rpc printer migrate all\n"
7489 " Migrate all from remote to local print server")
7492 "drivers",
7493 rpc_printer_migrate_drivers,
7494 NET_TRANSPORT_RPC,
7495 N_("Migrate drivers to local server"),
7496 N_("net rpc printer migrate drivers\n"
7497 " Migrate drivers to local server")
7500 "forms",
7501 rpc_printer_migrate_forms,
7502 NET_TRANSPORT_RPC,
7503 N_("Migrate froms to local server"),
7504 N_("net rpc printer migrate forms\n"
7505 " Migrate froms to local server")
7508 "printers",
7509 rpc_printer_migrate_printers,
7510 NET_TRANSPORT_RPC,
7511 N_("Migrate printers to local server"),
7512 N_("net rpc printer migrate printers\n"
7513 " Migrate printers to local server")
7516 "security",
7517 rpc_printer_migrate_security,
7518 NET_TRANSPORT_RPC,
7519 N_("Mirgate printer ACLs to local server"),
7520 N_("net rpc printer migrate security\n"
7521 " Mirgate printer ACLs to local server")
7524 "settings",
7525 rpc_printer_migrate_settings,
7526 NET_TRANSPORT_RPC,
7527 N_("Migrate printer settings to local server"),
7528 N_("net rpc printer migrate settings\n"
7529 " Migrate printer settings to local server")
7531 {NULL, NULL, 0, NULL, NULL}
7534 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7539 * List printers on a remote RPC server.
7541 * @param c A net_context structure.
7542 * @param argc Standard main() style argc.
7543 * @param argv Standard main() style argv. Initial components are already
7544 * stripped.
7546 * @return A shell status integer (0 for success).
7548 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7550 if (c->display_usage) {
7551 d_printf( "%s\n"
7552 "net rpc printer list\n"
7553 " %s\n",
7554 _("Usage:"),
7555 _("List printers on a remote RPC server"));
7556 return 0;
7559 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7560 rpc_printer_list_internals,
7561 argc, argv);
7565 * List printer-drivers on a remote RPC server.
7567 * @param c A net_context structure.
7568 * @param argc Standard main() style argc.
7569 * @param argv Standard main() style argv. Initial components are already
7570 * stripped.
7572 * @return A shell status integer (0 for success).
7574 static int rpc_printer_driver_list(struct net_context *c, int argc,
7575 const char **argv)
7577 if (c->display_usage) {
7578 d_printf( "%s\n"
7579 "net rpc printer driver\n"
7580 " %s\n",
7581 _("Usage:"),
7582 _("List printer-drivers on a remote RPC server"));
7583 return 0;
7586 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7587 rpc_printer_driver_list_internals,
7588 argc, argv);
7592 * Publish printer in ADS via MSRPC.
7594 * @param c A net_context structure.
7595 * @param argc Standard main() style argc.
7596 * @param argv Standard main() style argv. Initial components are already
7597 * stripped.
7599 * @return A shell status integer (0 for success).
7601 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7602 const char **argv)
7604 if (c->display_usage) {
7605 d_printf( "%s\n"
7606 "net rpc printer publish publish\n"
7607 " %s\n",
7608 _("Usage:"),
7609 _("Publish printer in ADS via MSRPC"));
7610 return 0;
7613 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7614 rpc_printer_publish_publish_internals,
7615 argc, argv);
7619 * Update printer in ADS via MSRPC.
7621 * @param c A net_context structure.
7622 * @param argc Standard main() style argc.
7623 * @param argv Standard main() style argv. Initial components are already
7624 * stripped.
7626 * @return A shell status integer (0 for success).
7628 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7630 if (c->display_usage) {
7631 d_printf( "%s\n"
7632 "net rpc printer publish update\n"
7633 " %s\n",
7634 _("Usage:"),
7635 _("Update printer in ADS via MSRPC"));
7636 return 0;
7639 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7640 rpc_printer_publish_update_internals,
7641 argc, argv);
7645 * UnPublish printer in ADS via MSRPC.
7647 * @param c A net_context structure.
7648 * @param argc Standard main() style argc.
7649 * @param argv Standard main() style argv. Initial components are already
7650 * stripped.
7652 * @return A shell status integer (0 for success).
7654 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7655 const char **argv)
7657 if (c->display_usage) {
7658 d_printf( "%s\n"
7659 "net rpc printer publish unpublish\n"
7660 " %s\n",
7661 _("Usage:\n"),
7662 _("UnPublish printer in ADS via MSRPC"));
7663 return 0;
7666 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7667 rpc_printer_publish_unpublish_internals,
7668 argc, argv);
7672 * List published printers via MSRPC.
7674 * @param c A net_context structure.
7675 * @param argc Standard main() style argc.
7676 * @param argv Standard main() style argv. Initial components are already
7677 * stripped.
7679 * @return A shell status integer (0 for success).
7681 static int rpc_printer_publish_list(struct net_context *c, int argc,
7682 const char **argv)
7684 if (c->display_usage) {
7685 d_printf( "%s\n"
7686 "net rpc printer publish list\n"
7687 " %s\n",
7688 _("Usage:"),
7689 _("List published printers via MSRPC"));
7690 return 0;
7693 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7694 rpc_printer_publish_list_internals,
7695 argc, argv);
7700 * Publish printer in ADS.
7702 * @param c A net_context structure.
7703 * @param argc Standard main() style argc.
7704 * @param argv Standard main() style argv. Initial components are already
7705 * stripped.
7707 * @return A shell status integer (0 for success).
7709 static int rpc_printer_publish(struct net_context *c, int argc,
7710 const char **argv)
7713 struct functable func[] = {
7715 "publish",
7716 rpc_printer_publish_publish,
7717 NET_TRANSPORT_RPC,
7718 N_("Publish printer in AD"),
7719 N_("net rpc printer publish publish\n"
7720 " Publish printer in AD")
7723 "update",
7724 rpc_printer_publish_update,
7725 NET_TRANSPORT_RPC,
7726 N_("Update printer in AD"),
7727 N_("net rpc printer publish update\n"
7728 " Update printer in AD")
7731 "unpublish",
7732 rpc_printer_publish_unpublish,
7733 NET_TRANSPORT_RPC,
7734 N_("Unpublish printer"),
7735 N_("net rpc printer publish unpublish\n"
7736 " Unpublish printer")
7739 "list",
7740 rpc_printer_publish_list,
7741 NET_TRANSPORT_RPC,
7742 N_("List published printers"),
7743 N_("net rpc printer publish list\n"
7744 " List published printers")
7746 {NULL, NULL, 0, NULL, NULL}
7749 if (argc == 0) {
7750 if (c->display_usage) {
7751 d_printf(_("Usage:\n"));
7752 d_printf(_("net rpc printer publish\n"
7753 " List published printers\n"
7754 " Alias of net rpc printer publish "
7755 "list\n"));
7756 net_display_usage_from_functable(func);
7757 return 0;
7759 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7760 rpc_printer_publish_list_internals,
7761 argc, argv);
7764 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7770 * Display rpc printer help page.
7772 * @param c A net_context structure.
7773 * @param argc Standard main() style argc.
7774 * @param argv Standard main() style argv. Initial components are already
7775 * stripped.
7777 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7779 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7780 "\tlists all printers on print-server\n\n"));
7781 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7782 "\tlists all printer-drivers on print-server\n\n"));
7783 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7784 "\tpublishes printer settings in Active Directory\n"
7785 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7786 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7787 "\n\tmigrates printers from remote to local server\n\n"));
7788 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7789 "\n\tmigrates printer-settings from remote to local server\n\n"));
7790 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7791 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7792 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7793 "\n\tmigrates printer-forms from remote to local server\n\n"));
7794 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7795 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7796 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7797 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7798 "\tremote to local print-server\n\n"));
7799 net_common_methods_usage(c, argc, argv);
7800 net_common_flags_usage(c, argc, argv);
7801 d_printf(_(
7802 "\t-v or --verbose\t\t\tgive verbose output\n"
7803 "\t --destination\t\tmigration target server (default: localhost)\n"));
7805 return -1;
7809 * 'net rpc printer' entrypoint.
7811 * @param c A net_context structure.
7812 * @param argc Standard main() style argc.
7813 * @param argv Standard main() style argv. Initial components are already
7814 * stripped.
7816 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7818 struct functable func[] = {
7820 "list",
7821 rpc_printer_list,
7822 NET_TRANSPORT_RPC,
7823 N_("List all printers on print server"),
7824 N_("net rpc printer list\n"
7825 " List all printers on print server")
7828 "migrate",
7829 rpc_printer_migrate,
7830 NET_TRANSPORT_RPC,
7831 N_("Migrate printer to local server"),
7832 N_("net rpc printer migrate\n"
7833 " Migrate printer to local server")
7836 "driver",
7837 rpc_printer_driver_list,
7838 NET_TRANSPORT_RPC,
7839 N_("List printer drivers"),
7840 N_("net rpc printer driver\n"
7841 " List printer drivers")
7844 "publish",
7845 rpc_printer_publish,
7846 NET_TRANSPORT_RPC,
7847 N_("Publish printer in AD"),
7848 N_("net rpc printer publish\n"
7849 " Publish printer in AD")
7851 {NULL, NULL, 0, NULL, NULL}
7854 if (argc == 0) {
7855 if (c->display_usage) {
7856 d_printf(_("Usage:\n"));
7857 d_printf(_("net rpc printer\n"
7858 " List printers\n"));
7859 net_display_usage_from_functable(func);
7860 return 0;
7862 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7863 rpc_printer_list_internals,
7864 argc, argv);
7867 return net_run_function(c, argc, argv, "net rpc printer", func);
7871 * 'net rpc' entrypoint.
7873 * @param c A net_context structure.
7874 * @param argc Standard main() style argc.
7875 * @param argv Standard main() style argv. Initial components are already
7876 * stripped.
7879 int net_rpc(struct net_context *c, int argc, const char **argv)
7881 NET_API_STATUS status;
7883 struct functable func[] = {
7885 "audit",
7886 net_rpc_audit,
7887 NET_TRANSPORT_RPC,
7888 N_("Modify global audit settings"),
7889 N_("net rpc audit\n"
7890 " Modify global audit settings")
7893 "info",
7894 net_rpc_info,
7895 NET_TRANSPORT_RPC,
7896 N_("Show basic info about a domain"),
7897 N_("net rpc info\n"
7898 " Show basic info about a domain")
7901 "join",
7902 net_rpc_join,
7903 NET_TRANSPORT_RPC,
7904 N_("Join a domain"),
7905 N_("net rpc join\n"
7906 " Join a domain")
7909 "oldjoin",
7910 net_rpc_oldjoin,
7911 NET_TRANSPORT_RPC,
7912 N_("Join a domain created in server manager"),
7913 N_("net rpc oldjoin\n"
7914 " Join a domain created in server manager")
7917 "testjoin",
7918 net_rpc_testjoin,
7919 NET_TRANSPORT_RPC,
7920 N_("Test that a join is valid"),
7921 N_("net rpc testjoin\n"
7922 " Test that a join is valid")
7925 "user",
7926 net_rpc_user,
7927 NET_TRANSPORT_RPC,
7928 N_("List/modify users"),
7929 N_("net rpc user\n"
7930 " List/modify users")
7933 "password",
7934 rpc_user_password,
7935 NET_TRANSPORT_RPC,
7936 N_("Change a user password"),
7937 N_("net rpc password\n"
7938 " Change a user password\n"
7939 " Alias for net rpc user password")
7942 "group",
7943 net_rpc_group,
7944 NET_TRANSPORT_RPC,
7945 N_("List/modify groups"),
7946 N_("net rpc group\n"
7947 " List/modify groups")
7950 "share",
7951 net_rpc_share,
7952 NET_TRANSPORT_RPC,
7953 N_("List/modify shares"),
7954 N_("net rpc share\n"
7955 " List/modify shares")
7958 "file",
7959 net_rpc_file,
7960 NET_TRANSPORT_RPC,
7961 N_("List open files"),
7962 N_("net rpc file\n"
7963 " List open files")
7966 "printer",
7967 net_rpc_printer,
7968 NET_TRANSPORT_RPC,
7969 N_("List/modify printers"),
7970 N_("net rpc printer\n"
7971 " List/modify printers")
7974 "changetrustpw",
7975 net_rpc_changetrustpw,
7976 NET_TRANSPORT_RPC,
7977 N_("Change trust account password"),
7978 N_("net rpc changetrustpw\n"
7979 " Change trust account password")
7982 "trustdom",
7983 rpc_trustdom,
7984 NET_TRANSPORT_RPC,
7985 N_("Modify domain trusts"),
7986 N_("net rpc trustdom\n"
7987 " Modify domain trusts")
7990 "abortshutdown",
7991 rpc_shutdown_abort,
7992 NET_TRANSPORT_RPC,
7993 N_("Abort a remote shutdown"),
7994 N_("net rpc abortshutdown\n"
7995 " Abort a remote shutdown")
7998 "shutdown",
7999 rpc_shutdown,
8000 NET_TRANSPORT_RPC,
8001 N_("Shutdown a remote server"),
8002 N_("net rpc shutdown\n"
8003 " Shutdown a remote server")
8006 "samdump",
8007 rpc_samdump,
8008 NET_TRANSPORT_RPC,
8009 N_("Dump SAM data of remote NT PDC"),
8010 N_("net rpc samdump\n"
8011 " Dump SAM data of remote NT PDC")
8014 "vampire",
8015 rpc_vampire,
8016 NET_TRANSPORT_RPC,
8017 N_("Sync a remote NT PDC's data into local passdb"),
8018 N_("net rpc vampire\n"
8019 " Sync a remote NT PDC's data into local passdb")
8022 "getsid",
8023 net_rpc_getsid,
8024 NET_TRANSPORT_RPC,
8025 N_("Fetch the domain sid into local secrets.tdb"),
8026 N_("net rpc getsid\n"
8027 " Fetch the domain sid into local secrets.tdb")
8030 "rights",
8031 net_rpc_rights,
8032 NET_TRANSPORT_RPC,
8033 N_("Manage privileges assigned to SID"),
8034 N_("net rpc rights\n"
8035 " Manage privileges assigned to SID")
8038 "service",
8039 net_rpc_service,
8040 NET_TRANSPORT_RPC,
8041 N_("Start/stop/query remote services"),
8042 N_("net rpc service\n"
8043 " Start/stop/query remote services")
8046 "registry",
8047 net_rpc_registry,
8048 NET_TRANSPORT_RPC,
8049 N_("Manage registry hives"),
8050 N_("net rpc registry\n"
8051 " Manage registry hives")
8054 "shell",
8055 net_rpc_shell,
8056 NET_TRANSPORT_RPC,
8057 N_("Open interactive shell on remote server"),
8058 N_("net rpc shell\n"
8059 " Open interactive shell on remote server")
8062 "trust",
8063 net_rpc_trust,
8064 NET_TRANSPORT_RPC,
8065 N_("Manage trusts"),
8066 N_("net rpc trust\n"
8067 " Manage trusts")
8070 "conf",
8071 net_rpc_conf,
8072 NET_TRANSPORT_RPC,
8073 N_("Configure a remote samba server"),
8074 N_("net rpc conf\n"
8075 " Configure a remote samba server")
8077 {NULL, NULL, 0, NULL, NULL}
8080 status = libnetapi_net_init(&c->netapi_ctx);
8081 if (status != 0) {
8082 return -1;
8084 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
8085 libnetapi_set_password(c->netapi_ctx, c->opt_password);
8086 if (c->opt_kerberos) {
8087 libnetapi_set_use_kerberos(c->netapi_ctx);
8089 if (c->opt_ccache) {
8090 libnetapi_set_use_ccache(c->netapi_ctx);
8093 return net_run_function(c, argc, argv, "net rpc", func);