tevent: call epoll_panic() if EPOLL_CTL_DEL failed
[Samba/gebeck_regimport.git] / source3 / utils / net_rpc.c
blob870ad2287f2a9878a0d31b5cec92756f5913e615
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 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 SAFE_FREE(username);
5415 return status;
5419 * 'net rpc file' entrypoint.
5420 * @param argc Standard main() style argc.
5421 * @param argv Standard main() style argv. Initial components are already
5422 * stripped.
5425 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5427 NET_API_STATUS status;
5429 struct functable func[] = {
5431 "close",
5432 rpc_file_close,
5433 NET_TRANSPORT_RPC,
5434 N_("Close opened file"),
5435 N_("net rpc file close\n"
5436 " Close opened file")
5439 "user",
5440 rpc_file_user,
5441 NET_TRANSPORT_RPC,
5442 N_("List files opened by user"),
5443 N_("net rpc file user\n"
5444 " List files opened by user")
5446 #if 0
5448 "info",
5449 rpc_file_info,
5450 NET_TRANSPORT_RPC,
5451 N_("Display information about opened file"),
5452 N_("net rpc file info\n"
5453 " Display information about opened file")
5455 #endif
5456 {NULL, NULL, 0, NULL, NULL}
5459 status = libnetapi_net_init(&c->netapi_ctx);
5460 if (status != 0) {
5461 return -1;
5463 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5464 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5465 if (c->opt_kerberos) {
5466 libnetapi_set_use_kerberos(c->netapi_ctx);
5469 if (argc == 0) {
5470 if (c->display_usage) {
5471 d_printf(_("Usage:\n"));
5472 d_printf(_("net rpc file\n"
5473 " List opened files\n"));
5474 net_display_usage_from_functable(func);
5475 return 0;
5478 return rpc_file_user(c, argc, argv);
5481 return net_run_function(c, argc, argv, "net rpc file", func);
5485 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5487 * All parameters are provided by the run_rpc_command function, except for
5488 * argc, argv which are passed through.
5490 * @param c A net_context structure.
5491 * @param domain_sid The domain sid acquired from the remote server.
5492 * @param cli A cli_state connected to the server.
5493 * @param mem_ctx Talloc context, destroyed on completion of the function.
5494 * @param argc Standard main() style argc.
5495 * @param argv Standard main() style argv. Initial components are already
5496 * stripped.
5498 * @return Normal NTSTATUS return.
5501 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5502 const struct dom_sid *domain_sid,
5503 const char *domain_name,
5504 struct cli_state *cli,
5505 struct rpc_pipe_client *pipe_hnd,
5506 TALLOC_CTX *mem_ctx,
5507 int argc,
5508 const char **argv)
5510 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5511 WERROR result;
5512 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5514 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5515 if (!NT_STATUS_IS_OK(status)) {
5516 return status;
5518 if (W_ERROR_IS_OK(result)) {
5519 d_printf(_("\nShutdown successfully aborted\n"));
5520 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5521 } else
5522 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5524 return werror_to_ntstatus(result);
5528 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5530 * All parameters are provided by the run_rpc_command function, except for
5531 * argc, argv which are passed through.
5533 * @param c A net_context structure.
5534 * @param domain_sid The domain sid acquired from the remote server.
5535 * @param cli A cli_state connected to the server.
5536 * @param mem_ctx Talloc context, destroyed on completion of the function.
5537 * @param argc Standard main() style argc.
5538 * @param argv Standard main() style argv. Initial components are already
5539 * stripped.
5541 * @return Normal NTSTATUS return.
5544 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5545 const struct dom_sid *domain_sid,
5546 const char *domain_name,
5547 struct cli_state *cli,
5548 struct rpc_pipe_client *pipe_hnd,
5549 TALLOC_CTX *mem_ctx,
5550 int argc,
5551 const char **argv)
5553 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5554 WERROR werr;
5555 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5557 result = dcerpc_winreg_AbortSystemShutdown(b, mem_ctx, NULL, &werr);
5559 if (!NT_STATUS_IS_OK(result)) {
5560 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5561 return result;
5563 if (W_ERROR_IS_OK(werr)) {
5564 d_printf(_("\nShutdown successfully aborted\n"));
5565 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5566 } else
5567 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5569 return werror_to_ntstatus(werr);
5573 * ABORT the shutdown of a remote RPC server.
5575 * @param argc Standard main() style argc.
5576 * @param argv Standard main() style argv. Initial components are already
5577 * stripped.
5579 * @return A shell status integer (0 for success).
5582 static int rpc_shutdown_abort(struct net_context *c, int argc,
5583 const char **argv)
5585 int rc = -1;
5587 if (c->display_usage) {
5588 d_printf( "%s\n"
5589 "net rpc abortshutdown\n"
5590 " %s\n",
5591 _("Usage:"),
5592 _("Abort a scheduled shutdown"));
5593 return 0;
5596 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5597 rpc_shutdown_abort_internals, argc, argv);
5599 if (rc == 0)
5600 return rc;
5602 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5604 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5605 rpc_reg_shutdown_abort_internals,
5606 argc, argv);
5610 * Shut down a remote RPC Server via initshutdown pipe.
5612 * All parameters are provided by the run_rpc_command function, except for
5613 * argc, argv which are passed through.
5615 * @param c A net_context structure.
5616 * @param domain_sid The domain sid acquired from the remote server.
5617 * @param cli A cli_state connected to the server.
5618 * @param mem_ctx Talloc context, destroyed on completion of the function.
5619 * @param argc Standard main() style argc.
5620 * @param argv Standard main() style argv. Initial components are already
5621 * stripped.
5623 * @return Normal NTSTATUS return.
5626 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5627 const struct dom_sid *domain_sid,
5628 const char *domain_name,
5629 struct cli_state *cli,
5630 struct rpc_pipe_client *pipe_hnd,
5631 TALLOC_CTX *mem_ctx,
5632 int argc,
5633 const char **argv)
5635 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5636 WERROR result;
5637 const char *msg = N_("This machine will be shutdown shortly");
5638 uint32 timeout = 20;
5639 struct lsa_StringLarge msg_string;
5640 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5642 if (c->opt_comment) {
5643 msg = c->opt_comment;
5645 if (c->opt_timeout) {
5646 timeout = c->opt_timeout;
5649 msg_string.string = msg;
5651 /* create an entry */
5652 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5653 &msg_string, timeout, c->opt_force, c->opt_reboot,
5654 &result);
5655 if (!NT_STATUS_IS_OK(status)) {
5656 return status;
5658 if (W_ERROR_IS_OK(result)) {
5659 d_printf(_("\nShutdown of remote machine succeeded\n"));
5660 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5661 } else {
5662 DEBUG(1,("Shutdown of remote machine failed!\n"));
5664 return werror_to_ntstatus(result);
5668 * Shut down a remote RPC Server via winreg pipe.
5670 * All parameters are provided by the run_rpc_command function, except for
5671 * argc, argv which are passed through.
5673 * @param c A net_context structure.
5674 * @param domain_sid The domain sid acquired from the remote server.
5675 * @param cli A cli_state connected to the server.
5676 * @param mem_ctx Talloc context, destroyed on completion of the function.
5677 * @param argc Standard main() style argc.
5678 * @param argv Standard main() style argv. Initial components are already
5679 * stripped.
5681 * @return Normal NTSTATUS return.
5684 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5685 const struct dom_sid *domain_sid,
5686 const char *domain_name,
5687 struct cli_state *cli,
5688 struct rpc_pipe_client *pipe_hnd,
5689 TALLOC_CTX *mem_ctx,
5690 int argc,
5691 const char **argv)
5693 const char *msg = N_("This machine will be shutdown shortly");
5694 uint32 timeout = 20;
5695 struct lsa_StringLarge msg_string;
5696 NTSTATUS result;
5697 WERROR werr;
5698 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5700 if (c->opt_comment) {
5701 msg = c->opt_comment;
5703 msg_string.string = msg;
5705 if (c->opt_timeout) {
5706 timeout = c->opt_timeout;
5709 /* create an entry */
5710 result = dcerpc_winreg_InitiateSystemShutdown(b, mem_ctx, NULL,
5711 &msg_string, timeout, c->opt_force, c->opt_reboot,
5712 &werr);
5713 if (!NT_STATUS_IS_OK(result)) {
5714 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5715 return result;
5718 if (W_ERROR_IS_OK(werr)) {
5719 d_printf(_("\nShutdown of remote machine succeeded\n"));
5720 } else {
5721 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5722 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5723 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5724 else
5725 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5728 return werror_to_ntstatus(werr);
5732 * Shut down a remote RPC server.
5734 * @param argc Standard main() style argc.
5735 * @param argv Standard main() style argv. Initial components are already
5736 * stripped.
5738 * @return A shell status integer (0 for success).
5741 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5743 int rc = -1;
5745 if (c->display_usage) {
5746 d_printf( "%s\n"
5747 "net rpc shutdown\n"
5748 " %s\n",
5749 _("Usage:"),
5750 _("Shut down a remote RPC server"));
5751 return 0;
5754 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown, 0,
5755 rpc_init_shutdown_internals, argc, argv);
5757 if (rc) {
5758 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5759 rc = run_rpc_command(c, NULL, &ndr_table_winreg, 0,
5760 rpc_reg_shutdown_internals, argc, argv);
5763 return rc;
5766 /***************************************************************************
5767 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5768 ***************************************************************************/
5771 * Add interdomain trust account to the RPC server.
5772 * All parameters (except for argc and argv) are passed by run_rpc_command
5773 * function.
5775 * @param c A net_context structure.
5776 * @param domain_sid The domain sid acquired from the server.
5777 * @param cli A cli_state connected to the server.
5778 * @param mem_ctx Talloc context, destroyed on completion of the function.
5779 * @param argc Standard main() style argc.
5780 * @param argv Standard main() style argv. Initial components are already
5781 * stripped.
5783 * @return normal NTSTATUS return code.
5786 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5787 const struct dom_sid *domain_sid,
5788 const char *domain_name,
5789 struct cli_state *cli,
5790 struct rpc_pipe_client *pipe_hnd,
5791 TALLOC_CTX *mem_ctx,
5792 int argc,
5793 const char **argv)
5795 struct policy_handle connect_pol, domain_pol, user_pol;
5796 NTSTATUS status, result;
5797 char *acct_name;
5798 struct lsa_String lsa_acct_name;
5799 uint32 acb_info;
5800 uint32 acct_flags=0;
5801 uint32 user_rid;
5802 uint32_t access_granted = 0;
5803 union samr_UserInfo info;
5804 unsigned int orig_timeout;
5805 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5806 DATA_BLOB session_key = data_blob_null;
5808 if (argc != 2) {
5809 d_printf("%s\n%s",
5810 _("Usage:"),
5811 _(" net rpc trustdom add <domain_name> "
5812 "<trust password>\n"));
5813 return NT_STATUS_INVALID_PARAMETER;
5817 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5820 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5821 return NT_STATUS_NO_MEMORY;
5824 if (!strupper_m(acct_name)) {
5825 SAFE_FREE(acct_name);
5826 return NT_STATUS_INVALID_PARAMETER;
5829 init_lsa_String(&lsa_acct_name, acct_name);
5831 status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
5832 if (!NT_STATUS_IS_OK(status)) {
5833 DEBUG(0,("Error getting session_key of SAM pipe. Error was %s\n",
5834 nt_errstr(status)));
5835 goto done;
5838 /* Get samr policy handle */
5839 status = dcerpc_samr_Connect2(b, mem_ctx,
5840 pipe_hnd->desthost,
5841 MAXIMUM_ALLOWED_ACCESS,
5842 &connect_pol,
5843 &result);
5844 if (!NT_STATUS_IS_OK(status)) {
5845 goto done;
5847 if (!NT_STATUS_IS_OK(result)) {
5848 status = result;
5849 goto done;
5852 /* Get domain policy handle */
5853 status = dcerpc_samr_OpenDomain(b, mem_ctx,
5854 &connect_pol,
5855 MAXIMUM_ALLOWED_ACCESS,
5856 discard_const_p(struct dom_sid2, domain_sid),
5857 &domain_pol,
5858 &result);
5859 if (!NT_STATUS_IS_OK(status)) {
5860 goto done;
5862 if (!NT_STATUS_IS_OK(result)) {
5863 status = result;
5864 goto done;
5867 /* This call can take a long time - allow the server to time out.
5868 * 35 seconds should do it. */
5870 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5872 /* Create trusting domain's account */
5873 acb_info = ACB_NORMAL;
5874 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5875 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5876 SAMR_USER_ACCESS_SET_PASSWORD |
5877 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5878 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5880 status = dcerpc_samr_CreateUser2(b, mem_ctx,
5881 &domain_pol,
5882 &lsa_acct_name,
5883 acb_info,
5884 acct_flags,
5885 &user_pol,
5886 &access_granted,
5887 &user_rid,
5888 &result);
5889 if (!NT_STATUS_IS_OK(status)) {
5890 goto done;
5892 /* And restore our original timeout. */
5893 rpccli_set_timeout(pipe_hnd, orig_timeout);
5895 if (!NT_STATUS_IS_OK(result)) {
5896 status = result;
5897 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5898 acct_name, nt_errstr(result));
5899 goto done;
5903 struct samr_CryptPassword crypt_pwd;
5905 ZERO_STRUCT(info.info23);
5907 init_samr_CryptPassword(argv[1],
5908 &session_key,
5909 &crypt_pwd);
5911 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5912 SAMR_FIELD_NT_PASSWORD_PRESENT;
5913 info.info23.info.acct_flags = ACB_DOMTRUST;
5914 info.info23.password = crypt_pwd;
5916 status = dcerpc_samr_SetUserInfo2(b, mem_ctx,
5917 &user_pol,
5919 &info,
5920 &result);
5921 if (!NT_STATUS_IS_OK(status)) {
5922 goto done;
5925 if (!NT_STATUS_IS_OK(result)) {
5926 status = result;
5927 DEBUG(0,("Could not set trust account password: %s\n",
5928 nt_errstr(result)));
5929 goto done;
5933 done:
5934 SAFE_FREE(acct_name);
5935 data_blob_clear_free(&session_key);
5936 return status;
5940 * Create interdomain trust account for a remote domain.
5942 * @param argc Standard argc.
5943 * @param argv Standard argv without initial components.
5945 * @return Integer status (0 means success).
5948 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5950 if (argc > 0 && !c->display_usage) {
5951 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
5952 rpc_trustdom_add_internals, argc, argv);
5953 } else {
5954 d_printf("%s\n%s",
5955 _("Usage:"),
5956 _("net rpc trustdom add <domain_name> <trust "
5957 "password>\n"));
5958 return -1;
5964 * Remove interdomain trust account from the RPC server.
5965 * All parameters (except for argc and argv) are passed by run_rpc_command
5966 * function.
5968 * @param c A net_context structure.
5969 * @param domain_sid The domain sid acquired from the server.
5970 * @param cli A cli_state connected to the server.
5971 * @param mem_ctx Talloc context, destroyed on completion of the function.
5972 * @param argc Standard main() style argc.
5973 * @param argv Standard main() style argv. Initial components are already
5974 * stripped.
5976 * @return normal NTSTATUS return code.
5979 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5980 const struct dom_sid *domain_sid,
5981 const char *domain_name,
5982 struct cli_state *cli,
5983 struct rpc_pipe_client *pipe_hnd,
5984 TALLOC_CTX *mem_ctx,
5985 int argc,
5986 const char **argv)
5988 struct policy_handle connect_pol, domain_pol, user_pol;
5989 NTSTATUS status, result;
5990 char *acct_name;
5991 struct dom_sid trust_acct_sid;
5992 struct samr_Ids user_rids, name_types;
5993 struct lsa_String lsa_acct_name;
5994 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5996 if (argc != 1) {
5997 d_printf("%s\n%s",
5998 _("Usage:"),
5999 _(" net rpc trustdom del <domain_name>\n"));
6000 return NT_STATUS_INVALID_PARAMETER;
6004 * Make valid trusting domain account (ie. uppercased and with '$' appended)
6006 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
6008 if (acct_name == NULL)
6009 return NT_STATUS_NO_MEMORY;
6011 if (!strupper_m(acct_name)) {
6012 TALLOC_FREE(acct_name);
6013 return NT_STATUS_INVALID_PARAMETER;
6016 /* Get samr policy handle */
6017 status = dcerpc_samr_Connect2(b, mem_ctx,
6018 pipe_hnd->desthost,
6019 MAXIMUM_ALLOWED_ACCESS,
6020 &connect_pol,
6021 &result);
6022 if (!NT_STATUS_IS_OK(status)) {
6023 goto done;
6025 if (!NT_STATUS_IS_OK(result)) {
6026 status = result;
6027 goto done;
6030 /* Get domain policy handle */
6031 status = dcerpc_samr_OpenDomain(b, mem_ctx,
6032 &connect_pol,
6033 MAXIMUM_ALLOWED_ACCESS,
6034 discard_const_p(struct dom_sid2, domain_sid),
6035 &domain_pol,
6036 &result);
6037 if (!NT_STATUS_IS_OK(status)) {
6038 goto done;
6040 if (!NT_STATUS_IS_OK(result)) {
6041 status = result;
6042 goto done;
6045 init_lsa_String(&lsa_acct_name, acct_name);
6047 status = dcerpc_samr_LookupNames(b, mem_ctx,
6048 &domain_pol,
6050 &lsa_acct_name,
6051 &user_rids,
6052 &name_types,
6053 &result);
6054 if (!NT_STATUS_IS_OK(status)) {
6055 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6056 "failed %s\n"),
6057 acct_name, nt_errstr(status));
6058 goto done;
6060 if (!NT_STATUS_IS_OK(result)) {
6061 status = result;
6062 d_printf(_("net rpc trustdom del: LookupNames on user %s "
6063 "failed %s\n"),
6064 acct_name, nt_errstr(result) );
6065 goto done;
6068 status = dcerpc_samr_OpenUser(b, mem_ctx,
6069 &domain_pol,
6070 MAXIMUM_ALLOWED_ACCESS,
6071 user_rids.ids[0],
6072 &user_pol,
6073 &result);
6074 if (!NT_STATUS_IS_OK(status)) {
6075 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6076 "%s\n"),
6077 acct_name, nt_errstr(status) );
6078 goto done;
6081 if (!NT_STATUS_IS_OK(result)) {
6082 status = result;
6083 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
6084 "%s\n"),
6085 acct_name, nt_errstr(result) );
6086 goto done;
6089 /* append the rid to the domain sid */
6090 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
6091 goto done;
6094 /* remove the sid */
6096 status = dcerpc_samr_RemoveMemberFromForeignDomain(b, mem_ctx,
6097 &user_pol,
6098 &trust_acct_sid,
6099 &result);
6100 if (!NT_STATUS_IS_OK(status)) {
6101 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6102 " on user %s failed %s\n"),
6103 acct_name, nt_errstr(status));
6104 goto done;
6106 if (!NT_STATUS_IS_OK(result)) {
6107 status = result;
6108 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
6109 " on user %s failed %s\n"),
6110 acct_name, nt_errstr(result) );
6111 goto done;
6115 /* Delete user */
6117 status = dcerpc_samr_DeleteUser(b, mem_ctx,
6118 &user_pol,
6119 &result);
6120 if (!NT_STATUS_IS_OK(status)) {
6121 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6122 "%s\n"),
6123 acct_name, nt_errstr(status));
6124 goto done;
6127 if (!NT_STATUS_IS_OK(result)) {
6128 result = status;
6129 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
6130 "%s\n"),
6131 acct_name, nt_errstr(result) );
6132 goto done;
6135 if (!NT_STATUS_IS_OK(result)) {
6136 d_printf(_("Could not set trust account password: %s\n"),
6137 nt_errstr(result));
6138 goto done;
6141 done:
6142 return status;
6146 * Delete interdomain trust account for a remote domain.
6148 * @param argc Standard argc.
6149 * @param argv Standard argv without initial components.
6151 * @return Integer status (0 means success).
6154 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
6156 if (argc > 0 && !c->display_usage) {
6157 return run_rpc_command(c, NULL, &ndr_table_samr, 0,
6158 rpc_trustdom_del_internals, argc, argv);
6159 } else {
6160 d_printf("%s\n%s",
6161 _("Usage:"),
6162 _("net rpc trustdom del <domain>\n"));
6163 return -1;
6167 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
6168 struct cli_state *cli,
6169 TALLOC_CTX *mem_ctx,
6170 const char *domain_name)
6172 char *dc_name = NULL;
6173 const char *buffer = NULL;
6174 struct rpc_pipe_client *netr;
6175 NTSTATUS status;
6176 WERROR result;
6177 struct dcerpc_binding_handle *b;
6179 /* Use NetServerEnum2 */
6181 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
6182 SAFE_FREE(dc_name);
6183 return NT_STATUS_OK;
6186 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
6187 for domain %s\n", domain_name));
6189 /* Try netr_GetDcName */
6191 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
6192 &netr);
6193 if (!NT_STATUS_IS_OK(status)) {
6194 return status;
6197 b = netr->binding_handle;
6199 status = dcerpc_netr_GetDcName(b, mem_ctx,
6200 netr->desthost,
6201 domain_name,
6202 &buffer,
6203 &result);
6204 TALLOC_FREE(netr);
6206 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(result)) {
6207 return status;
6210 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
6211 for domain %s\n", domain_name));
6213 if (!NT_STATUS_IS_OK(status)) {
6214 return status;
6217 return werror_to_ntstatus(result);
6221 * Establish trust relationship to a trusting domain.
6222 * Interdomain account must already be created on remote PDC.
6224 * @param c A net_context structure.
6225 * @param argc Standard argc.
6226 * @param argv Standard argv without initial components.
6228 * @return Integer status (0 means success).
6231 static int rpc_trustdom_establish(struct net_context *c, int argc,
6232 const char **argv)
6234 struct cli_state *cli = NULL;
6235 struct sockaddr_storage server_ss;
6236 struct rpc_pipe_client *pipe_hnd = NULL;
6237 struct policy_handle connect_hnd;
6238 TALLOC_CTX *mem_ctx;
6239 NTSTATUS nt_status, result;
6240 struct dom_sid *domain_sid;
6242 char* domain_name;
6243 char* acct_name;
6244 fstring pdc_name;
6245 union lsa_PolicyInformation *info = NULL;
6246 struct dcerpc_binding_handle *b;
6249 * Connect to \\server\ipc$ as 'our domain' account with password
6252 if (argc != 1 || c->display_usage) {
6253 d_printf("%s\n%s",
6254 _("Usage:"),
6255 _("net rpc trustdom establish <domain_name>\n"));
6256 return -1;
6259 domain_name = smb_xstrdup(argv[0]);
6260 if (!strupper_m(domain_name)) {
6261 SAFE_FREE(domain_name);
6262 return -1;
6265 /* account name used at first is our domain's name with '$' */
6266 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
6267 return -1;
6269 if (!strupper_m(acct_name)) {
6270 SAFE_FREE(domain_name);
6271 SAFE_FREE(acct_name);
6272 return -1;
6276 * opt_workgroup will be used by connection functions further,
6277 * hence it should be set to remote domain name instead of ours
6279 if (c->opt_workgroup) {
6280 c->opt_workgroup = smb_xstrdup(domain_name);
6283 c->opt_user_name = acct_name;
6285 /* find the domain controller */
6286 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6287 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6288 return -1;
6291 /* connect to ipc$ as username/password */
6292 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6293 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6295 /* Is it trusting domain account for sure ? */
6296 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6297 nt_errstr(nt_status)));
6298 return -1;
6301 /* store who we connected to */
6303 saf_store( domain_name, pdc_name );
6306 * Connect to \\server\ipc$ again (this time anonymously)
6309 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6310 (char*)pdc_name);
6312 if (NT_STATUS_IS_ERR(nt_status)) {
6313 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6314 domain_name, nt_errstr(nt_status)));
6315 return -1;
6318 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6319 "domain %s", domain_name))) {
6320 DEBUG(0, ("talloc_init() failed\n"));
6321 cli_shutdown(cli);
6322 return -1;
6325 /* Make sure we're talking to a proper server */
6327 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6328 if (!NT_STATUS_IS_OK(nt_status)) {
6329 cli_shutdown(cli);
6330 talloc_destroy(mem_ctx);
6331 return -1;
6335 * Call LsaOpenPolicy and LsaQueryInfo
6338 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6339 &pipe_hnd);
6340 if (!NT_STATUS_IS_OK(nt_status)) {
6341 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6342 cli_shutdown(cli);
6343 talloc_destroy(mem_ctx);
6344 return -1;
6347 b = pipe_hnd->binding_handle;
6349 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
6350 &connect_hnd);
6351 if (NT_STATUS_IS_ERR(nt_status)) {
6352 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6353 nt_errstr(nt_status)));
6354 cli_shutdown(cli);
6355 talloc_destroy(mem_ctx);
6356 return -1;
6359 /* Querying info level 5 */
6361 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6362 &connect_hnd,
6363 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6364 &info,
6365 &result);
6366 if (NT_STATUS_IS_ERR(nt_status)) {
6367 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6368 nt_errstr(nt_status)));
6369 cli_shutdown(cli);
6370 talloc_destroy(mem_ctx);
6371 return -1;
6373 if (NT_STATUS_IS_ERR(result)) {
6374 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6375 nt_errstr(result)));
6376 cli_shutdown(cli);
6377 talloc_destroy(mem_ctx);
6378 return -1;
6381 domain_sid = info->account_domain.sid;
6383 /* There should be actually query info level 3 (following nt serv behaviour),
6384 but I still don't know if it's _really_ necessary */
6387 * Store the password in secrets db
6390 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6391 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6392 cli_shutdown(cli);
6393 talloc_destroy(mem_ctx);
6394 return -1;
6398 * Close the pipes and clean up
6401 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6402 if (NT_STATUS_IS_ERR(nt_status)) {
6403 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6404 nt_errstr(nt_status)));
6405 cli_shutdown(cli);
6406 talloc_destroy(mem_ctx);
6407 return -1;
6410 cli_shutdown(cli);
6412 talloc_destroy(mem_ctx);
6414 d_printf(_("Trust to domain %s established\n"), domain_name);
6415 return 0;
6419 * Revoke trust relationship to the remote domain.
6421 * @param c A net_context structure.
6422 * @param argc Standard argc.
6423 * @param argv Standard argv without initial components.
6425 * @return Integer status (0 means success).
6428 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6429 const char **argv)
6431 char* domain_name;
6432 int rc = -1;
6434 if (argc < 1 || c->display_usage) {
6435 d_printf("%s\n%s",
6436 _("Usage:"),
6437 _("net rpc trustdom revoke <domain_name>\n"
6438 " Revoke trust relationship\n"
6439 " domain_name\tName of domain to revoke trust\n"));
6440 return -1;
6443 /* generate upper cased domain name */
6444 domain_name = smb_xstrdup(argv[0]);
6445 if (!strupper_m(domain_name)) {
6446 SAFE_FREE(domain_name);
6447 return -1;
6450 /* delete password of the trust */
6451 if (!pdb_del_trusteddom_pw(domain_name)) {
6452 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6453 domain_name));
6454 goto done;
6457 rc = 0;
6458 done:
6459 SAFE_FREE(domain_name);
6460 return rc;
6463 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6464 const struct dom_sid *domain_sid,
6465 const char *domain_name,
6466 struct cli_state *cli,
6467 struct rpc_pipe_client *pipe_hnd,
6468 TALLOC_CTX *mem_ctx,
6469 int argc,
6470 const char **argv)
6472 fstring str_sid;
6473 if (!sid_to_fstring(str_sid, domain_sid)) {
6474 return NT_STATUS_UNSUCCESSFUL;
6476 d_printf("%s\n", str_sid);
6477 return NT_STATUS_OK;
6480 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
6482 fstring ascii_sid;
6484 /* convert sid into ascii string */
6485 sid_to_fstring(ascii_sid, dom_sid);
6487 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
6490 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6491 TALLOC_CTX *mem_ctx,
6492 struct policy_handle *pol,
6493 struct dom_sid dom_sid,
6494 const char *trusted_dom_name)
6496 NTSTATUS nt_status, result;
6497 union lsa_TrustedDomainInfo *info = NULL;
6498 char *cleartextpwd = NULL;
6499 DATA_BLOB session_key;
6500 DATA_BLOB data = data_blob_null;
6501 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
6503 nt_status = dcerpc_lsa_QueryTrustedDomainInfoBySid(b, mem_ctx,
6504 pol,
6505 &dom_sid,
6506 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6507 &info,
6508 &result);
6509 if (NT_STATUS_IS_ERR(nt_status)) {
6510 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6511 nt_errstr(nt_status)));
6512 goto done;
6514 if (NT_STATUS_IS_ERR(result)) {
6515 nt_status = result;
6516 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6517 nt_errstr(result)));
6518 goto done;
6521 data = data_blob(info->password.password->data,
6522 info->password.password->length);
6524 nt_status = cli_get_session_key(mem_ctx, pipe_hnd, &session_key);
6525 if (!NT_STATUS_IS_OK(nt_status)) {
6526 DEBUG(0, ("Could not retrieve session key: %s\n", nt_errstr(nt_status)));
6527 goto done;
6530 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key);
6531 data_blob_free(&session_key);
6533 if (cleartextpwd == NULL) {
6534 DEBUG(0,("retrieved NULL password\n"));
6535 nt_status = NT_STATUS_UNSUCCESSFUL;
6536 goto done;
6539 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6540 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6541 nt_status = NT_STATUS_UNSUCCESSFUL;
6542 goto done;
6545 #ifdef DEBUG_PASSWORD
6546 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6547 "password: [%s]\n", trusted_dom_name,
6548 sid_string_dbg(&dom_sid), cleartextpwd));
6549 #endif
6551 done:
6552 SAFE_FREE(cleartextpwd);
6553 data_blob_free(&data);
6555 return nt_status;
6558 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6559 const char **argv)
6561 /* common variables */
6562 TALLOC_CTX* mem_ctx;
6563 struct cli_state *cli = NULL;
6564 struct rpc_pipe_client *pipe_hnd = NULL;
6565 NTSTATUS nt_status, result;
6566 const char *domain_name = NULL;
6567 struct policy_handle connect_hnd;
6568 union lsa_PolicyInformation *info = NULL;
6570 /* trusted domains listing variables */
6571 unsigned int enum_ctx = 0;
6572 int i;
6573 struct lsa_DomainList dom_list;
6574 fstring pdc_name;
6575 struct dcerpc_binding_handle *b;
6577 if (c->display_usage) {
6578 d_printf( "%s\n"
6579 "net rpc trustdom vampire\n"
6580 " %s\n",
6581 _("Usage:"),
6582 _("Vampire trust relationship from remote server"));
6583 return 0;
6587 * Listing trusted domains (stored in secrets.tdb, if local)
6590 mem_ctx = talloc_init("trust relationships vampire");
6593 * set domain and pdc name to local samba server (default)
6594 * or to remote one given in command line
6597 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6598 domain_name = c->opt_workgroup;
6599 c->opt_target_workgroup = c->opt_workgroup;
6600 } else {
6601 fstrcpy(pdc_name, lp_netbios_name());
6602 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6603 c->opt_target_workgroup = domain_name;
6606 /* open \PIPE\lsarpc and open policy handle */
6607 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6608 if (!NT_STATUS_IS_OK(nt_status)) {
6609 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6610 nt_errstr(nt_status)));
6611 talloc_destroy(mem_ctx);
6612 return -1;
6615 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6616 &pipe_hnd);
6617 if (!NT_STATUS_IS_OK(nt_status)) {
6618 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6619 nt_errstr(nt_status) ));
6620 cli_shutdown(cli);
6621 talloc_destroy(mem_ctx);
6622 return -1;
6625 b = pipe_hnd->binding_handle;
6627 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6628 &connect_hnd);
6629 if (NT_STATUS_IS_ERR(nt_status)) {
6630 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6631 nt_errstr(nt_status)));
6632 cli_shutdown(cli);
6633 talloc_destroy(mem_ctx);
6634 return -1;
6637 /* query info level 5 to obtain sid of a domain being queried */
6638 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6639 &connect_hnd,
6640 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6641 &info,
6642 &result);
6644 if (NT_STATUS_IS_ERR(nt_status)) {
6645 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6646 nt_errstr(nt_status)));
6647 cli_shutdown(cli);
6648 talloc_destroy(mem_ctx);
6649 return -1;
6651 if (NT_STATUS_IS_ERR(result)) {
6652 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6653 nt_errstr(result)));
6654 cli_shutdown(cli);
6655 talloc_destroy(mem_ctx);
6656 return -1;
6660 * Keep calling LsaEnumTrustdom over opened pipe until
6661 * the end of enumeration is reached
6664 d_printf(_("Vampire trusted domains:\n\n"));
6666 do {
6667 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6668 &connect_hnd,
6669 &enum_ctx,
6670 &dom_list,
6671 (uint32_t)-1,
6672 &result);
6673 if (NT_STATUS_IS_ERR(nt_status)) {
6674 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6675 nt_errstr(nt_status)));
6676 cli_shutdown(cli);
6677 talloc_destroy(mem_ctx);
6678 return -1;
6680 if (NT_STATUS_IS_ERR(result)) {
6681 nt_status = result;
6682 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6683 nt_errstr(result)));
6684 cli_shutdown(cli);
6685 talloc_destroy(mem_ctx);
6686 return -1;
6690 for (i = 0; i < dom_list.count; i++) {
6692 print_trusted_domain(dom_list.domains[i].sid,
6693 dom_list.domains[i].name.string);
6695 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6696 *dom_list.domains[i].sid,
6697 dom_list.domains[i].name.string);
6698 if (!NT_STATUS_IS_OK(nt_status)) {
6699 cli_shutdown(cli);
6700 talloc_destroy(mem_ctx);
6701 return -1;
6706 * in case of no trusted domains say something rather
6707 * than just display blank line
6709 if (!dom_list.count) d_printf(_("none\n"));
6711 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6713 /* close this connection before doing next one */
6714 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6715 if (NT_STATUS_IS_ERR(nt_status)) {
6716 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6717 nt_errstr(nt_status)));
6718 cli_shutdown(cli);
6719 talloc_destroy(mem_ctx);
6720 return -1;
6723 /* close lsarpc pipe and connection to IPC$ */
6724 cli_shutdown(cli);
6726 talloc_destroy(mem_ctx);
6727 return 0;
6730 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6732 /* common variables */
6733 TALLOC_CTX* mem_ctx;
6734 struct cli_state *cli = NULL, *remote_cli = NULL;
6735 struct rpc_pipe_client *pipe_hnd = NULL;
6736 NTSTATUS nt_status, result;
6737 const char *domain_name = NULL;
6738 struct dom_sid *queried_dom_sid;
6739 int ascii_dom_name_len;
6740 struct policy_handle connect_hnd;
6741 union lsa_PolicyInformation *info = NULL;
6742 struct dcerpc_binding_handle *b = NULL;
6744 /* trusted domains listing variables */
6745 unsigned int num_domains, enum_ctx = 0;
6746 int i;
6747 struct lsa_DomainList dom_list;
6748 fstring pdc_name;
6749 bool found_domain;
6751 /* trusting domains listing variables */
6752 struct policy_handle domain_hnd;
6753 struct samr_SamArray *trusts = NULL;
6755 if (c->display_usage) {
6756 d_printf( "%s\n"
6757 "net rpc trustdom list\n"
6758 " %s\n",
6759 _("Usage:"),
6760 _("List incoming and outgoing trust relationships"));
6761 return 0;
6765 * Listing trusted domains (stored in secrets.tdb, if local)
6768 mem_ctx = talloc_init("trust relationships listing");
6771 * set domain and pdc name to local samba server (default)
6772 * or to remote one given in command line
6775 if (strcasecmp_m(c->opt_workgroup, lp_workgroup())) {
6776 domain_name = c->opt_workgroup;
6777 c->opt_target_workgroup = c->opt_workgroup;
6778 } else {
6779 fstrcpy(pdc_name, lp_netbios_name());
6780 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6781 c->opt_target_workgroup = domain_name;
6784 /* open \PIPE\lsarpc and open policy handle */
6785 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6786 if (!NT_STATUS_IS_OK(nt_status)) {
6787 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6788 nt_errstr(nt_status)));
6789 talloc_destroy(mem_ctx);
6790 return -1;
6793 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6794 &pipe_hnd);
6795 if (!NT_STATUS_IS_OK(nt_status)) {
6796 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6797 nt_errstr(nt_status) ));
6798 cli_shutdown(cli);
6799 talloc_destroy(mem_ctx);
6800 return -1;
6803 b = pipe_hnd->binding_handle;
6805 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6806 &connect_hnd);
6807 if (NT_STATUS_IS_ERR(nt_status)) {
6808 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6809 nt_errstr(nt_status)));
6810 cli_shutdown(cli);
6811 talloc_destroy(mem_ctx);
6812 return -1;
6815 /* query info level 5 to obtain sid of a domain being queried */
6816 nt_status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
6817 &connect_hnd,
6818 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6819 &info,
6820 &result);
6822 if (NT_STATUS_IS_ERR(nt_status)) {
6823 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6824 nt_errstr(nt_status)));
6825 cli_shutdown(cli);
6826 talloc_destroy(mem_ctx);
6827 return -1;
6829 if (NT_STATUS_IS_ERR(result)) {
6830 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6831 nt_errstr(result)));
6832 cli_shutdown(cli);
6833 talloc_destroy(mem_ctx);
6834 return -1;
6837 queried_dom_sid = info->account_domain.sid;
6840 * Keep calling LsaEnumTrustdom over opened pipe until
6841 * the end of enumeration is reached
6844 d_printf(_("Trusted domains list:\n\n"));
6846 found_domain = false;
6848 do {
6849 nt_status = dcerpc_lsa_EnumTrustDom(b, mem_ctx,
6850 &connect_hnd,
6851 &enum_ctx,
6852 &dom_list,
6853 (uint32_t)-1,
6854 &result);
6855 if (NT_STATUS_IS_ERR(nt_status)) {
6856 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6857 nt_errstr(nt_status)));
6858 cli_shutdown(cli);
6859 talloc_destroy(mem_ctx);
6860 return -1;
6862 if (NT_STATUS_IS_ERR(result)) {
6863 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6864 nt_errstr(result)));
6865 cli_shutdown(cli);
6866 talloc_destroy(mem_ctx);
6867 return -1;
6871 for (i = 0; i < dom_list.count; i++) {
6872 print_trusted_domain(dom_list.domains[i].sid,
6873 dom_list.domains[i].name.string);
6874 found_domain = true;
6878 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6881 * in case of no trusted domains say something rather
6882 * than just display blank line
6884 if (!found_domain) {
6885 d_printf(_("none\n"));
6888 /* close this connection before doing next one */
6889 nt_status = dcerpc_lsa_Close(b, mem_ctx, &connect_hnd, &result);
6890 if (NT_STATUS_IS_ERR(nt_status)) {
6891 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6892 nt_errstr(nt_status)));
6893 cli_shutdown(cli);
6894 talloc_destroy(mem_ctx);
6895 return -1;
6898 TALLOC_FREE(pipe_hnd);
6901 * Listing trusting domains (stored in passdb backend, if local)
6904 d_printf(_("\nTrusting domains list:\n\n"));
6907 * Open \PIPE\samr and get needed policy handles
6909 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6910 &pipe_hnd);
6911 if (!NT_STATUS_IS_OK(nt_status)) {
6912 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6913 cli_shutdown(cli);
6914 talloc_destroy(mem_ctx);
6915 return -1;
6918 b = pipe_hnd->binding_handle;
6920 /* SamrConnect2 */
6921 nt_status = dcerpc_samr_Connect2(b, mem_ctx,
6922 pipe_hnd->desthost,
6923 SAMR_ACCESS_LOOKUP_DOMAIN,
6924 &connect_hnd,
6925 &result);
6926 if (!NT_STATUS_IS_OK(nt_status)) {
6927 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6928 nt_errstr(nt_status)));
6929 cli_shutdown(cli);
6930 talloc_destroy(mem_ctx);
6931 return -1;
6933 if (!NT_STATUS_IS_OK(result)) {
6934 nt_status = result;
6935 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6936 nt_errstr(result)));
6937 cli_shutdown(cli);
6938 talloc_destroy(mem_ctx);
6939 return -1;
6942 /* SamrOpenDomain - we have to open domain policy handle in order to be
6943 able to enumerate accounts*/
6944 nt_status = dcerpc_samr_OpenDomain(b, mem_ctx,
6945 &connect_hnd,
6946 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6947 queried_dom_sid,
6948 &domain_hnd,
6949 &result);
6950 if (!NT_STATUS_IS_OK(nt_status)) {
6951 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6952 nt_errstr(nt_status)));
6953 cli_shutdown(cli);
6954 talloc_destroy(mem_ctx);
6955 return -1;
6957 if (!NT_STATUS_IS_OK(result)) {
6958 nt_status = result;
6959 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6960 nt_errstr(result)));
6961 cli_shutdown(cli);
6962 talloc_destroy(mem_ctx);
6963 return -1;
6967 * perform actual enumeration
6970 found_domain = false;
6972 enum_ctx = 0; /* reset enumeration context from last enumeration */
6973 do {
6975 nt_status = dcerpc_samr_EnumDomainUsers(b, mem_ctx,
6976 &domain_hnd,
6977 &enum_ctx,
6978 ACB_DOMTRUST,
6979 &trusts,
6980 0xffff,
6981 &num_domains,
6982 &result);
6983 if (NT_STATUS_IS_ERR(nt_status)) {
6984 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6985 nt_errstr(nt_status)));
6986 cli_shutdown(cli);
6987 talloc_destroy(mem_ctx);
6988 return -1;
6990 if (NT_STATUS_IS_ERR(result)) {
6991 nt_status = result;
6992 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6993 nt_errstr(result)));
6994 cli_shutdown(cli);
6995 talloc_destroy(mem_ctx);
6996 return -1;
6999 for (i = 0; i < num_domains; i++) {
7001 char *str = discard_const_p(char, trusts->entries[i].name.string);
7003 found_domain = true;
7006 * get each single domain's sid (do we _really_ need this ?):
7007 * 1) connect to domain's pdc
7008 * 2) query the pdc for domain's sid
7011 /* get rid of '$' tail */
7012 ascii_dom_name_len = strlen(str);
7013 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
7014 str[ascii_dom_name_len - 1] = '\0';
7016 /* set opt_* variables to remote domain */
7017 if (!strupper_m(str)) {
7018 cli_shutdown(cli);
7019 talloc_destroy(mem_ctx);
7020 return -1;
7022 c->opt_workgroup = talloc_strdup(mem_ctx, str);
7023 c->opt_target_workgroup = c->opt_workgroup;
7025 d_printf("%-20s", str);
7027 /* connect to remote domain controller */
7028 nt_status = net_make_ipc_connection(c,
7029 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
7030 &remote_cli);
7031 if (NT_STATUS_IS_OK(nt_status)) {
7032 /* query for domain's sid */
7033 if (run_rpc_command(
7034 c, remote_cli,
7035 &ndr_table_lsarpc, 0,
7036 rpc_query_domain_sid, argc,
7037 argv))
7038 d_printf(_("strange - couldn't get domain's sid\n"));
7040 cli_shutdown(remote_cli);
7042 } else {
7043 d_fprintf(stderr, _("domain controller is not "
7044 "responding: %s\n"),
7045 nt_errstr(nt_status));
7046 d_printf(_("couldn't get domain's sid\n"));
7050 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
7052 if (!found_domain) {
7053 d_printf("none\n");
7056 /* close opened samr and domain policy handles */
7057 nt_status = dcerpc_samr_Close(b, mem_ctx, &domain_hnd, &result);
7058 if (!NT_STATUS_IS_OK(nt_status)) {
7059 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
7062 nt_status = dcerpc_samr_Close(b, mem_ctx, &connect_hnd, &result);
7063 if (!NT_STATUS_IS_OK(nt_status)) {
7064 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
7067 /* close samr pipe and connection to IPC$ */
7068 cli_shutdown(cli);
7070 talloc_destroy(mem_ctx);
7071 return 0;
7075 * Entrypoint for 'net rpc trustdom' code.
7077 * @param argc Standard argc.
7078 * @param argv Standard argv without initial components.
7080 * @return Integer status (0 means success).
7083 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
7085 struct functable func[] = {
7087 "add",
7088 rpc_trustdom_add,
7089 NET_TRANSPORT_RPC,
7090 N_("Add trusting domain's account"),
7091 N_("net rpc trustdom add\n"
7092 " Add trusting domain's account")
7095 "del",
7096 rpc_trustdom_del,
7097 NET_TRANSPORT_RPC,
7098 N_("Remove trusting domain's account"),
7099 N_("net rpc trustdom del\n"
7100 " Remove trusting domain's account")
7103 "establish",
7104 rpc_trustdom_establish,
7105 NET_TRANSPORT_RPC,
7106 N_("Establish outgoing trust relationship"),
7107 N_("net rpc trustdom establish\n"
7108 " Establish outgoing trust relationship")
7111 "revoke",
7112 rpc_trustdom_revoke,
7113 NET_TRANSPORT_RPC,
7114 N_("Revoke outgoing trust relationship"),
7115 N_("net rpc trustdom revoke\n"
7116 " Revoke outgoing trust relationship")
7119 "list",
7120 rpc_trustdom_list,
7121 NET_TRANSPORT_RPC,
7122 N_("List in- and outgoing domain trusts"),
7123 N_("net rpc trustdom list\n"
7124 " List in- and outgoing domain trusts")
7127 "vampire",
7128 rpc_trustdom_vampire,
7129 NET_TRANSPORT_RPC,
7130 N_("Vampire trusts from remote server"),
7131 N_("net rpc trustdom vampire\n"
7132 " Vampire trusts from remote server")
7134 {NULL, NULL, 0, NULL, NULL}
7137 return net_run_function(c, argc, argv, "net rpc trustdom", func);
7141 * Check if a server will take rpc commands
7142 * @param flags Type of server to connect to (PDC, DMB, localhost)
7143 * if the host is not explicitly specified
7144 * @return bool (true means rpc supported)
7146 bool net_rpc_check(struct net_context *c, unsigned flags)
7148 struct cli_state *cli;
7149 bool ret = false;
7150 struct sockaddr_storage server_ss;
7151 char *server_name = NULL;
7152 NTSTATUS status;
7154 /* flags (i.e. server type) may depend on command */
7155 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
7156 return false;
7158 status = cli_connect_nb(server_name, &server_ss, 0, 0x20,
7159 lp_netbios_name(), SMB_SIGNING_DEFAULT,
7160 0, &cli);
7161 if (!NT_STATUS_IS_OK(status)) {
7162 return false;
7164 status = smbXcli_negprot(cli->conn, cli->timeout, PROTOCOL_CORE,
7165 PROTOCOL_NT1);
7166 if (!NT_STATUS_IS_OK(status))
7167 goto done;
7168 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_NT1)
7169 goto done;
7171 ret = true;
7172 done:
7173 cli_shutdown(cli);
7174 return ret;
7177 /* dump sam database via samsync rpc calls */
7178 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
7179 if (c->display_usage) {
7180 d_printf( "%s\n"
7181 "net rpc samdump\n"
7182 " %s\n",
7183 _("Usage:"),
7184 _("Dump remote SAM database"));
7185 return 0;
7188 return run_rpc_command(c, NULL, &ndr_table_netlogon,
7189 NET_FLAGS_ANONYMOUS,
7190 rpc_samdump_internals, argc, argv);
7193 /* syncronise sam database via samsync rpc calls */
7194 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
7196 struct functable func[] = {
7198 "ldif",
7199 rpc_vampire_ldif,
7200 NET_TRANSPORT_RPC,
7201 N_("Dump remote SAM database to ldif"),
7202 N_("net rpc vampire ldif\n"
7203 " Dump remote SAM database to LDIF file or "
7204 "stdout")
7207 "keytab",
7208 rpc_vampire_keytab,
7209 NET_TRANSPORT_RPC,
7210 N_("Dump remote SAM database to Kerberos Keytab"),
7211 N_("net rpc vampire keytab\n"
7212 " Dump remote SAM database to Kerberos keytab "
7213 "file")
7216 "passdb",
7217 rpc_vampire_passdb,
7218 NET_TRANSPORT_RPC,
7219 N_("Dump remote SAM database to passdb"),
7220 N_("net rpc vampire passdb\n"
7221 " Dump remote SAM database to passdb")
7224 {NULL, NULL, 0, NULL, NULL}
7227 if (argc == 0) {
7228 if (c->display_usage) {
7229 d_printf( "%s\n"
7230 "net rpc vampire\n"
7231 " %s\n",
7232 _("Usage:"),
7233 _("Vampire remote SAM database"));
7234 return 0;
7237 return rpc_vampire_passdb(c, argc, argv);
7240 return net_run_function(c, argc, argv, "net rpc vampire", func);
7244 * Migrate everything from a print server.
7246 * @param c A net_context structure.
7247 * @param argc Standard main() style argc.
7248 * @param argv Standard main() style argv. Initial components are already
7249 * stripped.
7251 * @return A shell status integer (0 for success).
7253 * The order is important !
7254 * To successfully add drivers the print queues have to exist !
7255 * Applying ACLs should be the last step, because you're easily locked out.
7258 static int rpc_printer_migrate_all(struct net_context *c, int argc,
7259 const char **argv)
7261 int ret;
7263 if (c->display_usage) {
7264 d_printf( "%s\n"
7265 "net rpc printer migrate all\n"
7266 " %s\n",
7267 _("Usage:"),
7268 _("Migrate everything from a print server"));
7269 return 0;
7272 if (!c->opt_host) {
7273 d_printf(_("no server to migrate\n"));
7274 return -1;
7277 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7278 rpc_printer_migrate_printers_internals, argc,
7279 argv);
7280 if (ret)
7281 return ret;
7283 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7284 rpc_printer_migrate_drivers_internals, argc,
7285 argv);
7286 if (ret)
7287 return ret;
7289 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7290 rpc_printer_migrate_forms_internals, argc, argv);
7291 if (ret)
7292 return ret;
7294 ret = run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7295 rpc_printer_migrate_settings_internals, argc,
7296 argv);
7297 if (ret)
7298 return ret;
7300 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7301 rpc_printer_migrate_security_internals, argc,
7302 argv);
7307 * Migrate print drivers from a print server.
7309 * @param c A net_context structure.
7310 * @param argc Standard main() style argc.
7311 * @param argv Standard main() style argv. Initial components are already
7312 * stripped.
7314 * @return A shell status integer (0 for success).
7316 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
7317 const char **argv)
7319 if (c->display_usage) {
7320 d_printf( "%s\n"
7321 "net rpc printer migrate drivers\n"
7322 " %s\n",
7323 _("Usage:"),
7324 _("Migrate print-drivers from a print-server"));
7325 return 0;
7328 if (!c->opt_host) {
7329 d_printf(_("no server to migrate\n"));
7330 return -1;
7333 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7334 rpc_printer_migrate_drivers_internals,
7335 argc, argv);
7339 * Migrate print-forms from a print-server.
7341 * @param c A net_context structure.
7342 * @param argc Standard main() style argc.
7343 * @param argv Standard main() style argv. Initial components are already
7344 * stripped.
7346 * @return A shell status integer (0 for success).
7348 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7349 const char **argv)
7351 if (c->display_usage) {
7352 d_printf( "%s\n"
7353 "net rpc printer migrate forms\n"
7354 " %s\n",
7355 _("Usage:"),
7356 _("Migrate print-forms from a print-server"));
7357 return 0;
7360 if (!c->opt_host) {
7361 d_printf(_("no server to migrate\n"));
7362 return -1;
7365 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7366 rpc_printer_migrate_forms_internals,
7367 argc, argv);
7371 * Migrate printers from a print-server.
7373 * @param c A net_context structure.
7374 * @param argc Standard main() style argc.
7375 * @param argv Standard main() style argv. Initial components are already
7376 * stripped.
7378 * @return A shell status integer (0 for success).
7380 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7381 const char **argv)
7383 if (c->display_usage) {
7384 d_printf( "%s\n"
7385 "net rpc printer migrate printers\n"
7386 " %s\n",
7387 _("Usage:"),
7388 _("Migrate printers from a print-server"));
7389 return 0;
7392 if (!c->opt_host) {
7393 d_printf(_("no server to migrate\n"));
7394 return -1;
7397 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7398 rpc_printer_migrate_printers_internals,
7399 argc, argv);
7403 * Migrate printer-ACLs from a print-server
7405 * @param c A net_context structure.
7406 * @param argc Standard main() style argc.
7407 * @param argv Standard main() style argv. Initial components are already
7408 * stripped.
7410 * @return A shell status integer (0 for success).
7412 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7413 const char **argv)
7415 if (c->display_usage) {
7416 d_printf( "%s\n"
7417 "net rpc printer migrate security\n"
7418 " %s\n",
7419 _("Usage:"),
7420 _("Migrate printer-ACLs from a print-server"));
7421 return 0;
7424 if (!c->opt_host) {
7425 d_printf(_("no server to migrate\n"));
7426 return -1;
7429 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7430 rpc_printer_migrate_security_internals,
7431 argc, argv);
7435 * Migrate printer-settings from a print-server.
7437 * @param c A net_context structure.
7438 * @param argc Standard main() style argc.
7439 * @param argv Standard main() style argv. Initial components are already
7440 * stripped.
7442 * @return A shell status integer (0 for success).
7444 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7445 const char **argv)
7447 if (c->display_usage) {
7448 d_printf( "%s\n"
7449 "net rpc printer migrate settings\n"
7450 " %s\n",
7451 _("Usage:"),
7452 _("Migrate printer-settings from a "
7453 "print-server"));
7454 return 0;
7457 if (!c->opt_host) {
7458 d_printf(_("no server to migrate\n"));
7459 return -1;
7462 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7463 rpc_printer_migrate_settings_internals,
7464 argc, argv);
7468 * 'net rpc printer' entrypoint.
7470 * @param c A net_context structure.
7471 * @param argc Standard main() style argc.
7472 * @param argv Standard main() style argv. Initial components are already
7473 * stripped.
7476 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7479 /* ouch: when addriver and setdriver are called from within
7480 rpc_printer_migrate_drivers_internals, the printer-queue already
7481 *has* to exist */
7483 struct functable func[] = {
7485 "all",
7486 rpc_printer_migrate_all,
7487 NET_TRANSPORT_RPC,
7488 N_("Migrate all from remote to local print server"),
7489 N_("net rpc printer migrate all\n"
7490 " Migrate all from remote to local print server")
7493 "drivers",
7494 rpc_printer_migrate_drivers,
7495 NET_TRANSPORT_RPC,
7496 N_("Migrate drivers to local server"),
7497 N_("net rpc printer migrate drivers\n"
7498 " Migrate drivers to local server")
7501 "forms",
7502 rpc_printer_migrate_forms,
7503 NET_TRANSPORT_RPC,
7504 N_("Migrate froms to local server"),
7505 N_("net rpc printer migrate forms\n"
7506 " Migrate froms to local server")
7509 "printers",
7510 rpc_printer_migrate_printers,
7511 NET_TRANSPORT_RPC,
7512 N_("Migrate printers to local server"),
7513 N_("net rpc printer migrate printers\n"
7514 " Migrate printers to local server")
7517 "security",
7518 rpc_printer_migrate_security,
7519 NET_TRANSPORT_RPC,
7520 N_("Mirgate printer ACLs to local server"),
7521 N_("net rpc printer migrate security\n"
7522 " Mirgate printer ACLs to local server")
7525 "settings",
7526 rpc_printer_migrate_settings,
7527 NET_TRANSPORT_RPC,
7528 N_("Migrate printer settings to local server"),
7529 N_("net rpc printer migrate settings\n"
7530 " Migrate printer settings to local server")
7532 {NULL, NULL, 0, NULL, NULL}
7535 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7540 * List printers on a remote RPC server.
7542 * @param c A net_context structure.
7543 * @param argc Standard main() style argc.
7544 * @param argv Standard main() style argv. Initial components are already
7545 * stripped.
7547 * @return A shell status integer (0 for success).
7549 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7551 if (c->display_usage) {
7552 d_printf( "%s\n"
7553 "net rpc printer list\n"
7554 " %s\n",
7555 _("Usage:"),
7556 _("List printers on a remote RPC server"));
7557 return 0;
7560 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7561 rpc_printer_list_internals,
7562 argc, argv);
7566 * List printer-drivers on a remote RPC server.
7568 * @param c A net_context structure.
7569 * @param argc Standard main() style argc.
7570 * @param argv Standard main() style argv. Initial components are already
7571 * stripped.
7573 * @return A shell status integer (0 for success).
7575 static int rpc_printer_driver_list(struct net_context *c, int argc,
7576 const char **argv)
7578 if (c->display_usage) {
7579 d_printf( "%s\n"
7580 "net rpc printer driver\n"
7581 " %s\n",
7582 _("Usage:"),
7583 _("List printer-drivers on a remote RPC server"));
7584 return 0;
7587 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7588 rpc_printer_driver_list_internals,
7589 argc, argv);
7593 * Publish printer in ADS via MSRPC.
7595 * @param c A net_context structure.
7596 * @param argc Standard main() style argc.
7597 * @param argv Standard main() style argv. Initial components are already
7598 * stripped.
7600 * @return A shell status integer (0 for success).
7602 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7603 const char **argv)
7605 if (c->display_usage) {
7606 d_printf( "%s\n"
7607 "net rpc printer publish publish\n"
7608 " %s\n",
7609 _("Usage:"),
7610 _("Publish printer in ADS via MSRPC"));
7611 return 0;
7614 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7615 rpc_printer_publish_publish_internals,
7616 argc, argv);
7620 * Update printer in ADS via MSRPC.
7622 * @param c A net_context structure.
7623 * @param argc Standard main() style argc.
7624 * @param argv Standard main() style argv. Initial components are already
7625 * stripped.
7627 * @return A shell status integer (0 for success).
7629 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7631 if (c->display_usage) {
7632 d_printf( "%s\n"
7633 "net rpc printer publish update\n"
7634 " %s\n",
7635 _("Usage:"),
7636 _("Update printer in ADS via MSRPC"));
7637 return 0;
7640 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7641 rpc_printer_publish_update_internals,
7642 argc, argv);
7646 * UnPublish printer in ADS via MSRPC.
7648 * @param c A net_context structure.
7649 * @param argc Standard main() style argc.
7650 * @param argv Standard main() style argv. Initial components are already
7651 * stripped.
7653 * @return A shell status integer (0 for success).
7655 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7656 const char **argv)
7658 if (c->display_usage) {
7659 d_printf( "%s\n"
7660 "net rpc printer publish unpublish\n"
7661 " %s\n",
7662 _("Usage:\n"),
7663 _("UnPublish printer in ADS via MSRPC"));
7664 return 0;
7667 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7668 rpc_printer_publish_unpublish_internals,
7669 argc, argv);
7673 * List published printers via MSRPC.
7675 * @param c A net_context structure.
7676 * @param argc Standard main() style argc.
7677 * @param argv Standard main() style argv. Initial components are already
7678 * stripped.
7680 * @return A shell status integer (0 for success).
7682 static int rpc_printer_publish_list(struct net_context *c, int argc,
7683 const char **argv)
7685 if (c->display_usage) {
7686 d_printf( "%s\n"
7687 "net rpc printer publish list\n"
7688 " %s\n",
7689 _("Usage:"),
7690 _("List published printers via MSRPC"));
7691 return 0;
7694 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7695 rpc_printer_publish_list_internals,
7696 argc, argv);
7701 * Publish printer in ADS.
7703 * @param c A net_context structure.
7704 * @param argc Standard main() style argc.
7705 * @param argv Standard main() style argv. Initial components are already
7706 * stripped.
7708 * @return A shell status integer (0 for success).
7710 static int rpc_printer_publish(struct net_context *c, int argc,
7711 const char **argv)
7714 struct functable func[] = {
7716 "publish",
7717 rpc_printer_publish_publish,
7718 NET_TRANSPORT_RPC,
7719 N_("Publish printer in AD"),
7720 N_("net rpc printer publish publish\n"
7721 " Publish printer in AD")
7724 "update",
7725 rpc_printer_publish_update,
7726 NET_TRANSPORT_RPC,
7727 N_("Update printer in AD"),
7728 N_("net rpc printer publish update\n"
7729 " Update printer in AD")
7732 "unpublish",
7733 rpc_printer_publish_unpublish,
7734 NET_TRANSPORT_RPC,
7735 N_("Unpublish printer"),
7736 N_("net rpc printer publish unpublish\n"
7737 " Unpublish printer")
7740 "list",
7741 rpc_printer_publish_list,
7742 NET_TRANSPORT_RPC,
7743 N_("List published printers"),
7744 N_("net rpc printer publish list\n"
7745 " List published printers")
7747 {NULL, NULL, 0, NULL, NULL}
7750 if (argc == 0) {
7751 if (c->display_usage) {
7752 d_printf(_("Usage:\n"));
7753 d_printf(_("net rpc printer publish\n"
7754 " List published printers\n"
7755 " Alias of net rpc printer publish "
7756 "list\n"));
7757 net_display_usage_from_functable(func);
7758 return 0;
7760 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7761 rpc_printer_publish_list_internals,
7762 argc, argv);
7765 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7771 * Display rpc printer help page.
7773 * @param c A net_context structure.
7774 * @param argc Standard main() style argc.
7775 * @param argv Standard main() style argv. Initial components are already
7776 * stripped.
7778 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7780 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7781 "\tlists all printers on print-server\n\n"));
7782 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7783 "\tlists all printer-drivers on print-server\n\n"));
7784 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7785 "\tpublishes printer settings in Active Directory\n"
7786 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7787 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7788 "\n\tmigrates printers from remote to local server\n\n"));
7789 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7790 "\n\tmigrates printer-settings from remote to local server\n\n"));
7791 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7792 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7793 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7794 "\n\tmigrates printer-forms from remote to local server\n\n"));
7795 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7796 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7797 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7798 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7799 "\tremote to local print-server\n\n"));
7800 net_common_methods_usage(c, argc, argv);
7801 net_common_flags_usage(c, argc, argv);
7802 d_printf(_(
7803 "\t-v or --verbose\t\t\tgive verbose output\n"
7804 "\t --destination\t\tmigration target server (default: localhost)\n"));
7806 return -1;
7810 * 'net rpc printer' entrypoint.
7812 * @param c A net_context structure.
7813 * @param argc Standard main() style argc.
7814 * @param argv Standard main() style argv. Initial components are already
7815 * stripped.
7817 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7819 struct functable func[] = {
7821 "list",
7822 rpc_printer_list,
7823 NET_TRANSPORT_RPC,
7824 N_("List all printers on print server"),
7825 N_("net rpc printer list\n"
7826 " List all printers on print server")
7829 "migrate",
7830 rpc_printer_migrate,
7831 NET_TRANSPORT_RPC,
7832 N_("Migrate printer to local server"),
7833 N_("net rpc printer migrate\n"
7834 " Migrate printer to local server")
7837 "driver",
7838 rpc_printer_driver_list,
7839 NET_TRANSPORT_RPC,
7840 N_("List printer drivers"),
7841 N_("net rpc printer driver\n"
7842 " List printer drivers")
7845 "publish",
7846 rpc_printer_publish,
7847 NET_TRANSPORT_RPC,
7848 N_("Publish printer in AD"),
7849 N_("net rpc printer publish\n"
7850 " Publish printer in AD")
7852 {NULL, NULL, 0, NULL, NULL}
7855 if (argc == 0) {
7856 if (c->display_usage) {
7857 d_printf(_("Usage:\n"));
7858 d_printf(_("net rpc printer\n"
7859 " List printers\n"));
7860 net_display_usage_from_functable(func);
7861 return 0;
7863 return run_rpc_command(c, NULL, &ndr_table_spoolss, 0,
7864 rpc_printer_list_internals,
7865 argc, argv);
7868 return net_run_function(c, argc, argv, "net rpc printer", func);
7872 * 'net rpc' entrypoint.
7874 * @param c A net_context structure.
7875 * @param argc Standard main() style argc.
7876 * @param argv Standard main() style argv. Initial components are already
7877 * stripped.
7880 int net_rpc(struct net_context *c, int argc, const char **argv)
7882 NET_API_STATUS status;
7884 struct functable func[] = {
7886 "audit",
7887 net_rpc_audit,
7888 NET_TRANSPORT_RPC,
7889 N_("Modify global audit settings"),
7890 N_("net rpc audit\n"
7891 " Modify global audit settings")
7894 "info",
7895 net_rpc_info,
7896 NET_TRANSPORT_RPC,
7897 N_("Show basic info about a domain"),
7898 N_("net rpc info\n"
7899 " Show basic info about a domain")
7902 "join",
7903 net_rpc_join,
7904 NET_TRANSPORT_RPC,
7905 N_("Join a domain"),
7906 N_("net rpc join\n"
7907 " Join a domain")
7910 "oldjoin",
7911 net_rpc_oldjoin,
7912 NET_TRANSPORT_RPC,
7913 N_("Join a domain created in server manager"),
7914 N_("net rpc oldjoin\n"
7915 " Join a domain created in server manager")
7918 "testjoin",
7919 net_rpc_testjoin,
7920 NET_TRANSPORT_RPC,
7921 N_("Test that a join is valid"),
7922 N_("net rpc testjoin\n"
7923 " Test that a join is valid")
7926 "user",
7927 net_rpc_user,
7928 NET_TRANSPORT_RPC,
7929 N_("List/modify users"),
7930 N_("net rpc user\n"
7931 " List/modify users")
7934 "password",
7935 rpc_user_password,
7936 NET_TRANSPORT_RPC,
7937 N_("Change a user password"),
7938 N_("net rpc password\n"
7939 " Change a user password\n"
7940 " Alias for net rpc user password")
7943 "group",
7944 net_rpc_group,
7945 NET_TRANSPORT_RPC,
7946 N_("List/modify groups"),
7947 N_("net rpc group\n"
7948 " List/modify groups")
7951 "share",
7952 net_rpc_share,
7953 NET_TRANSPORT_RPC,
7954 N_("List/modify shares"),
7955 N_("net rpc share\n"
7956 " List/modify shares")
7959 "file",
7960 net_rpc_file,
7961 NET_TRANSPORT_RPC,
7962 N_("List open files"),
7963 N_("net rpc file\n"
7964 " List open files")
7967 "printer",
7968 net_rpc_printer,
7969 NET_TRANSPORT_RPC,
7970 N_("List/modify printers"),
7971 N_("net rpc printer\n"
7972 " List/modify printers")
7975 "changetrustpw",
7976 net_rpc_changetrustpw,
7977 NET_TRANSPORT_RPC,
7978 N_("Change trust account password"),
7979 N_("net rpc changetrustpw\n"
7980 " Change trust account password")
7983 "trustdom",
7984 rpc_trustdom,
7985 NET_TRANSPORT_RPC,
7986 N_("Modify domain trusts"),
7987 N_("net rpc trustdom\n"
7988 " Modify domain trusts")
7991 "abortshutdown",
7992 rpc_shutdown_abort,
7993 NET_TRANSPORT_RPC,
7994 N_("Abort a remote shutdown"),
7995 N_("net rpc abortshutdown\n"
7996 " Abort a remote shutdown")
7999 "shutdown",
8000 rpc_shutdown,
8001 NET_TRANSPORT_RPC,
8002 N_("Shutdown a remote server"),
8003 N_("net rpc shutdown\n"
8004 " Shutdown a remote server")
8007 "samdump",
8008 rpc_samdump,
8009 NET_TRANSPORT_RPC,
8010 N_("Dump SAM data of remote NT PDC"),
8011 N_("net rpc samdump\n"
8012 " Dump SAM data of remote NT PDC")
8015 "vampire",
8016 rpc_vampire,
8017 NET_TRANSPORT_RPC,
8018 N_("Sync a remote NT PDC's data into local passdb"),
8019 N_("net rpc vampire\n"
8020 " Sync a remote NT PDC's data into local passdb")
8023 "getsid",
8024 net_rpc_getsid,
8025 NET_TRANSPORT_RPC,
8026 N_("Fetch the domain sid into local secrets.tdb"),
8027 N_("net rpc getsid\n"
8028 " Fetch the domain sid into local secrets.tdb")
8031 "rights",
8032 net_rpc_rights,
8033 NET_TRANSPORT_RPC,
8034 N_("Manage privileges assigned to SID"),
8035 N_("net rpc rights\n"
8036 " Manage privileges assigned to SID")
8039 "service",
8040 net_rpc_service,
8041 NET_TRANSPORT_RPC,
8042 N_("Start/stop/query remote services"),
8043 N_("net rpc service\n"
8044 " Start/stop/query remote services")
8047 "registry",
8048 net_rpc_registry,
8049 NET_TRANSPORT_RPC,
8050 N_("Manage registry hives"),
8051 N_("net rpc registry\n"
8052 " Manage registry hives")
8055 "shell",
8056 net_rpc_shell,
8057 NET_TRANSPORT_RPC,
8058 N_("Open interactive shell on remote server"),
8059 N_("net rpc shell\n"
8060 " Open interactive shell on remote server")
8063 "trust",
8064 net_rpc_trust,
8065 NET_TRANSPORT_RPC,
8066 N_("Manage trusts"),
8067 N_("net rpc trust\n"
8068 " Manage trusts")
8071 "conf",
8072 net_rpc_conf,
8073 NET_TRANSPORT_RPC,
8074 N_("Configure a remote samba server"),
8075 N_("net rpc conf\n"
8076 " Configure a remote samba server")
8078 {NULL, NULL, 0, NULL, NULL}
8081 status = libnetapi_net_init(&c->netapi_ctx);
8082 if (status != 0) {
8083 return -1;
8085 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
8086 libnetapi_set_password(c->netapi_ctx, c->opt_password);
8087 if (c->opt_kerberos) {
8088 libnetapi_set_use_kerberos(c->netapi_ctx);
8090 if (c->opt_ccache) {
8091 libnetapi_set_use_ccache(c->netapi_ctx);
8094 return net_run_function(c, argc, argv, "net rpc", func);