s4-ldb: allow for unescaped '=' in a index DN
[Samba/cd1.git] / source3 / utils / net_rpc.c
blob455405a3ee899a37a691bbb3cb5227e349f31959
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2004,2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "includes.h"
24 #include "utils/net.h"
25 #include "../libcli/auth/libcli_auth.h"
27 static int net_mode_share;
28 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
30 /**
31 * @file net_rpc.c
33 * @brief RPC based subcommands for the 'net' utility.
35 * This file should contain much of the functionality that used to
36 * be found in rpcclient, execpt that the commands should change
37 * less often, and the fucntionality should be sane (the user is not
38 * expected to know a rid/sid before they conduct an operation etc.)
40 * @todo Perhaps eventually these should be split out into a number
41 * of files, as this could get quite big.
42 **/
45 /**
46 * Many of the RPC functions need the domain sid. This function gets
47 * it at the start of every run
49 * @param cli A cli_state already connected to the remote machine
51 * @return The Domain SID of the remote machine.
52 **/
54 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
55 DOM_SID **domain_sid,
56 const char **domain_name)
58 struct rpc_pipe_client *lsa_pipe = NULL;
59 struct policy_handle pol;
60 NTSTATUS result = NT_STATUS_OK;
61 union lsa_PolicyInformation *info = NULL;
63 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
64 &lsa_pipe);
65 if (!NT_STATUS_IS_OK(result)) {
66 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
67 return result;
70 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
71 SEC_FLAG_MAXIMUM_ALLOWED,
72 &pol);
73 if (!NT_STATUS_IS_OK(result)) {
74 d_fprintf(stderr, _("open_policy failed: %s\n"),
75 nt_errstr(result));
76 return result;
79 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
80 &pol,
81 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
82 &info);
83 if (!NT_STATUS_IS_OK(result)) {
84 d_fprintf(stderr, _("lsaquery failed: %s\n"),
85 nt_errstr(result));
86 return result;
89 *domain_name = info->account_domain.name.string;
90 *domain_sid = info->account_domain.sid;
92 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
93 TALLOC_FREE(lsa_pipe);
95 return NT_STATUS_OK;
98 /**
99 * Run a single RPC command, from start to finish.
101 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
102 * @param conn_flag a NET_FLAG_ combination. Passed to
103 * net_make_ipc_connection.
104 * @param argc Standard main() style argc.
105 * @param argv Standard main() style argv. Initial components are already
106 * stripped.
107 * @return A shell status integer (0 for success).
110 int run_rpc_command(struct net_context *c,
111 struct cli_state *cli_arg,
112 const struct ndr_syntax_id *interface,
113 int conn_flags,
114 rpc_command_fn fn,
115 int argc,
116 const char **argv)
118 struct cli_state *cli = NULL;
119 struct rpc_pipe_client *pipe_hnd = NULL;
120 TALLOC_CTX *mem_ctx;
121 NTSTATUS nt_status;
122 DOM_SID *domain_sid;
123 const char *domain_name;
124 int ret = -1;
126 /* make use of cli_state handed over as an argument, if possible */
127 if (!cli_arg) {
128 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
129 if (!NT_STATUS_IS_OK(nt_status)) {
130 DEBUG(1, ("failed to make ipc connection: %s\n",
131 nt_errstr(nt_status)));
132 return -1;
134 } else {
135 cli = cli_arg;
138 if (!cli) {
139 return -1;
142 /* Create mem_ctx */
144 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
145 DEBUG(0, ("talloc_init() failed\n"));
146 goto fail;
149 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
150 &domain_name);
151 if (!NT_STATUS_IS_OK(nt_status)) {
152 goto fail;
155 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
156 if (lp_client_schannel()
157 && (ndr_syntax_id_equal(interface,
158 &ndr_table_netlogon.syntax_id))) {
159 /* Always try and create an schannel netlogon pipe. */
160 nt_status = cli_rpc_pipe_open_schannel(
161 cli, interface, NCACN_NP,
162 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
163 &pipe_hnd);
164 if (!NT_STATUS_IS_OK(nt_status)) {
165 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
166 nt_errstr(nt_status) ));
167 goto fail;
169 } else {
170 if (conn_flags & NET_FLAGS_SEAL) {
171 nt_status = cli_rpc_pipe_open_ntlmssp(
172 cli, interface,
173 (conn_flags & NET_FLAGS_TCP) ?
174 NCACN_IP_TCP : NCACN_NP,
175 DCERPC_AUTH_LEVEL_PRIVACY,
176 lp_workgroup(), c->opt_user_name,
177 c->opt_password, &pipe_hnd);
178 } else {
179 nt_status = cli_rpc_pipe_open_noauth(
180 cli, interface,
181 &pipe_hnd);
183 if (!NT_STATUS_IS_OK(nt_status)) {
184 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
185 get_pipe_name_from_iface(interface),
186 nt_errstr(nt_status) ));
187 goto fail;
192 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
194 if (!NT_STATUS_IS_OK(nt_status)) {
195 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
196 } else {
197 ret = 0;
198 DEBUG(5, ("rpc command function succedded\n"));
201 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
202 if (pipe_hnd) {
203 TALLOC_FREE(pipe_hnd);
207 fail:
208 /* close the connection only if it was opened here */
209 if (!cli_arg) {
210 cli_shutdown(cli);
213 talloc_destroy(mem_ctx);
214 return ret;
218 * Force a change of the trust acccount password.
220 * All parameters are provided by the run_rpc_command function, except for
221 * argc, argv which are passed through.
223 * @param domain_sid The domain sid acquired from the remote server.
224 * @param cli A cli_state connected to the server.
225 * @param mem_ctx Talloc context, destroyed on completion of the function.
226 * @param argc Standard main() style argc.
227 * @param argv Standard main() style argv. Initial components are already
228 * stripped.
230 * @return Normal NTSTATUS return.
233 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
234 const DOM_SID *domain_sid,
235 const char *domain_name,
236 struct cli_state *cli,
237 struct rpc_pipe_client *pipe_hnd,
238 TALLOC_CTX *mem_ctx,
239 int argc,
240 const char **argv)
242 NTSTATUS status;
244 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
245 if (!NT_STATUS_IS_OK(status)) {
246 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
247 nt_errstr(status));
248 return status;
251 return NT_STATUS_OK;
255 * Force a change of the trust acccount password.
257 * @param argc Standard main() style argc.
258 * @param argv Standard main() style argv. Initial components are already
259 * stripped.
261 * @return A shell status integer (0 for success).
264 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
266 if (c->display_usage) {
267 d_printf(_("Usage:\n"
268 "net rpc changetrustpw\n"
269 " Change the machine trust password\n"));
270 return 0;
273 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
274 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
275 rpc_changetrustpw_internals,
276 argc, argv);
280 * Join a domain, the old way.
282 * This uses 'machinename' as the inital password, and changes it.
284 * The password should be created with 'server manager' or equiv first.
286 * All parameters are provided by the run_rpc_command function, except for
287 * argc, argv which are passed through.
289 * @param domain_sid The domain sid acquired from the remote server.
290 * @param cli A cli_state connected to the server.
291 * @param mem_ctx Talloc context, destroyed on completion of the function.
292 * @param argc Standard main() style argc.
293 * @param argv Standard main() style argv. Initial components are already
294 * stripped.
296 * @return Normal NTSTATUS return.
299 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
300 const DOM_SID *domain_sid,
301 const char *domain_name,
302 struct cli_state *cli,
303 struct rpc_pipe_client *pipe_hnd,
304 TALLOC_CTX *mem_ctx,
305 int argc,
306 const char **argv)
309 fstring trust_passwd;
310 unsigned char orig_trust_passwd_hash[16];
311 NTSTATUS result;
312 enum netr_SchannelType sec_channel_type;
314 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
315 &pipe_hnd);
316 if (!NT_STATUS_IS_OK(result)) {
317 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
318 "error was %s\n",
319 cli->desthost,
320 nt_errstr(result) ));
321 return result;
325 check what type of join - if the user want's to join as
326 a BDC, the server must agree that we are a BDC.
328 if (argc >= 0) {
329 sec_channel_type = get_sec_channel_type(argv[0]);
330 } else {
331 sec_channel_type = get_sec_channel_type(NULL);
334 fstrcpy(trust_passwd, global_myname());
335 strlower_m(trust_passwd);
338 * Machine names can be 15 characters, but the max length on
339 * a password is 14. --jerry
342 trust_passwd[14] = '\0';
344 E_md4hash(trust_passwd, orig_trust_passwd_hash);
346 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
347 global_myname(),
348 orig_trust_passwd_hash,
349 sec_channel_type);
351 if (NT_STATUS_IS_OK(result))
352 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
355 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
356 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
357 result = NT_STATUS_UNSUCCESSFUL;
360 return result;
364 * Join a domain, the old way.
366 * @param argc Standard main() style argc.
367 * @param argv Standard main() style argv. Initial components are already
368 * stripped.
370 * @return A shell status integer (0 for success).
373 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
375 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
376 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
377 rpc_oldjoin_internals,
378 argc, argv);
382 * Join a domain, the old way. This function exists to allow
383 * the message to be displayed when oldjoin was explicitly
384 * requested, but not when it was implied by "net rpc join".
386 * @param argc Standard main() style argc.
387 * @param argv Standard main() style argv. Initial components are already
388 * stripped.
390 * @return A shell status integer (0 for success).
393 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
395 int rc = -1;
397 if (c->display_usage) {
398 d_printf(_("Usage:\n"
399 "net rpc oldjoin\n"
400 " Join a domain the old way\n"));
401 return 0;
404 rc = net_rpc_perform_oldjoin(c, argc, argv);
406 if (rc) {
407 d_fprintf(stderr, _("Failed to join domain\n"));
410 return rc;
414 * 'net rpc join' entrypoint.
415 * @param argc Standard main() style argc.
416 * @param argv Standard main() style argv. Initial components are already
417 * stripped
419 * Main 'net_rpc_join()' (where the admin username/password is used) is
420 * in net_rpc_join.c.
421 * Try to just change the password, but if that doesn't work, use/prompt
422 * for a username/password.
425 int net_rpc_join(struct net_context *c, int argc, const char **argv)
427 if (c->display_usage) {
428 d_printf(_("Usage:\n"
429 "net rpc join -U <username>[%%password] <type>\n"
430 " Join a domain\n"
431 " username\tName of the admin user"
432 " password\tPassword of the admin user, will "
433 "prompt if not specified\n"
434 " type\tCan be one of the following:\n"
435 "\t\tMEMBER\tJoin as member server (default)\n"
436 "\t\tBDC\tJoin as BDC\n"
437 "\t\tPDC\tJoin as PDC\n"));
438 return 0;
441 if (lp_server_role() == ROLE_STANDALONE) {
442 d_printf(_("cannot join as standalone machine\n"));
443 return -1;
446 if (strlen(global_myname()) > 15) {
447 d_printf(_("Our netbios name can be at most 15 chars long, "
448 "\"%s\" is %u chars long\n"),
449 global_myname(), (unsigned int)strlen(global_myname()));
450 return -1;
453 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
454 return 0;
456 return net_rpc_join_newstyle(c, argc, argv);
460 * display info about a rpc domain
462 * All parameters are provided by the run_rpc_command function, except for
463 * argc, argv which are passed through.
465 * @param domain_sid The domain sid acquired from the remote server
466 * @param cli A cli_state connected to the server.
467 * @param mem_ctx Talloc context, destroyed on completion of the function.
468 * @param argc Standard main() style argc.
469 * @param argv Standard main() style argv. Initial components are already
470 * stripped.
472 * @return Normal NTSTATUS return.
475 NTSTATUS rpc_info_internals(struct net_context *c,
476 const DOM_SID *domain_sid,
477 const char *domain_name,
478 struct cli_state *cli,
479 struct rpc_pipe_client *pipe_hnd,
480 TALLOC_CTX *mem_ctx,
481 int argc,
482 const char **argv)
484 struct policy_handle connect_pol, domain_pol;
485 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
486 union samr_DomainInfo *info = NULL;
487 fstring sid_str;
489 sid_to_fstring(sid_str, domain_sid);
491 /* Get sam policy handle */
492 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
493 pipe_hnd->desthost,
494 MAXIMUM_ALLOWED_ACCESS,
495 &connect_pol);
496 if (!NT_STATUS_IS_OK(result)) {
497 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
498 nt_errstr(result));
499 goto done;
502 /* Get domain policy handle */
503 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
504 &connect_pol,
505 MAXIMUM_ALLOWED_ACCESS,
506 CONST_DISCARD(struct dom_sid2 *, domain_sid),
507 &domain_pol);
508 if (!NT_STATUS_IS_OK(result)) {
509 d_fprintf(stderr, _("Could not open domain: %s\n"),
510 nt_errstr(result));
511 goto done;
514 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
515 &domain_pol,
517 &info);
518 if (NT_STATUS_IS_OK(result)) {
519 d_printf(_("Domain Name: %s\n"),
520 info->general.domain_name.string);
521 d_printf(_("Domain SID: %s\n"), sid_str);
522 d_printf(_("Sequence number: %llu\n"),
523 (unsigned long long)info->general.sequence_num);
524 d_printf(_("Num users: %u\n"), info->general.num_users);
525 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
526 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
529 done:
530 return result;
534 * 'net rpc info' entrypoint.
535 * @param argc Standard main() style argc.
536 * @param argv Standard main() style argv. Initial components are already
537 * stripped.
540 int net_rpc_info(struct net_context *c, int argc, const char **argv)
542 if (c->display_usage) {
543 d_printf(_("Usage:\n"
544 "net rpc info\n"
545 " Display information about the domain\n"));
546 return 0;
549 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
550 NET_FLAGS_PDC, rpc_info_internals,
551 argc, argv);
555 * Fetch domain SID into the local secrets.tdb.
557 * All parameters are provided by the run_rpc_command function, except for
558 * argc, argv which are passed through.
560 * @param domain_sid The domain sid acquired from the remote server.
561 * @param cli A cli_state connected to the server.
562 * @param mem_ctx Talloc context, destroyed on completion of the function.
563 * @param argc Standard main() style argc.
564 * @param argv Standard main() style argv. Initial components are already
565 * stripped.
567 * @return Normal NTSTATUS return.
570 static NTSTATUS rpc_getsid_internals(struct net_context *c,
571 const DOM_SID *domain_sid,
572 const char *domain_name,
573 struct cli_state *cli,
574 struct rpc_pipe_client *pipe_hnd,
575 TALLOC_CTX *mem_ctx,
576 int argc,
577 const char **argv)
579 fstring sid_str;
581 sid_to_fstring(sid_str, domain_sid);
582 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
583 sid_str, domain_name);
585 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
586 DEBUG(0,("Can't store domain SID\n"));
587 return NT_STATUS_UNSUCCESSFUL;
590 return NT_STATUS_OK;
594 * 'net rpc getsid' entrypoint.
595 * @param argc Standard main() style argc.
596 * @param argv Standard main() style argv. Initial components are already
597 * stripped.
600 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
602 if (c->display_usage) {
603 d_printf(_("Usage:\n"
604 "net rpc getsid\n"
605 " Fetch domain SID into local secrets.tdb\n"));
606 return 0;
609 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
610 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
611 rpc_getsid_internals,
612 argc, argv);
615 /****************************************************************************/
618 * Basic usage function for 'net rpc user'.
619 * @param argc Standard main() style argc.
620 * @param argv Standard main() style argv. Initial components are already
621 * stripped.
624 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
626 return net_user_usage(c, argc, argv);
630 * Add a new user to a remote RPC server.
632 * @param argc Standard main() style argc.
633 * @param argv Standard main() style argv. Initial components are already
634 * stripped.
636 * @return A shell status integer (0 for success).
639 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
641 NET_API_STATUS status;
642 struct USER_INFO_1 info1;
643 uint32_t parm_error = 0;
645 if (argc < 1 || c->display_usage) {
646 rpc_user_usage(c, argc, argv);
647 return 0;
650 ZERO_STRUCT(info1);
652 info1.usri1_name = argv[0];
653 if (argc == 2) {
654 info1.usri1_password = argv[1];
657 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
659 if (status != 0) {
660 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
661 argv[0], libnetapi_get_error_string(c->netapi_ctx,
662 status));
663 return -1;
664 } else {
665 d_printf(_("Added user '%s'.\n"), argv[0]);
668 return 0;
672 * Rename a user on a remote RPC server.
674 * @param argc Standard main() style argc.
675 * @param argv Standard main() style argv. Initial components are already
676 * stripped.
678 * @return A shell status integer (0 for success).
681 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
683 NET_API_STATUS status;
684 struct USER_INFO_0 u0;
685 uint32_t parm_err = 0;
687 if (argc != 2 || c->display_usage) {
688 rpc_user_usage(c, argc, argv);
689 return 0;
692 u0.usri0_name = argv[1];
694 status = NetUserSetInfo(c->opt_host, argv[0],
695 0, (uint8_t *)&u0, &parm_err);
696 if (status) {
697 d_fprintf(stderr,
698 _("Failed to rename user from %s to %s - %s\n"),
699 argv[0], argv[1],
700 libnetapi_get_error_string(c->netapi_ctx, status));
701 } else {
702 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
705 return status;
709 * Delete a user from a remote RPC server.
711 * @param argc Standard main() style argc.
712 * @param argv Standard main() style argv. Initial components are already
713 * stripped.
715 * @return A shell status integer (0 for success).
718 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
720 NET_API_STATUS status;
722 if (argc < 1 || c->display_usage) {
723 rpc_user_usage(c, argc, argv);
724 return 0;
727 status = NetUserDel(c->opt_host, argv[0]);
729 if (status != 0) {
730 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
731 argv[0],
732 libnetapi_get_error_string(c->netapi_ctx, status));
733 return -1;
734 } else {
735 d_printf(_("Deleted user '%s'.\n"), argv[0]);
738 return 0;
742 * Set a user's password on a remote RPC server.
744 * @param argc Standard main() style argc.
745 * @param argv Standard main() style argv. Initial components are already
746 * stripped.
748 * @return A shell status integer (0 for success).
751 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
753 NET_API_STATUS status;
754 char *prompt = NULL;
755 struct USER_INFO_1003 u1003;
756 uint32_t parm_err = 0;
757 int ret;
759 if (argc < 1 || c->display_usage) {
760 rpc_user_usage(c, argc, argv);
761 return 0;
764 if (argv[1]) {
765 u1003.usri1003_password = argv[1];
766 } else {
767 ret = asprintf(&prompt, _("Enter new password for %s:"),
768 argv[0]);
769 if (ret == -1) {
770 return -1;
772 u1003.usri1003_password = getpass(prompt);
773 SAFE_FREE(prompt);
776 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
778 /* Display results */
779 if (status != 0) {
780 d_fprintf(stderr,
781 _("Failed to set password for '%s' with error: %s.\n"),
782 argv[0], libnetapi_get_error_string(c->netapi_ctx,
783 status));
784 return -1;
787 return 0;
791 * List a user's groups from a remote RPC server.
793 * @param argc Standard main() style argc.
794 * @param argv Standard main() style argv. Initial components are already
795 * stripped.
797 * @return A shell status integer (0 for success)
800 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
803 NET_API_STATUS status;
804 struct GROUP_USERS_INFO_0 *u0 = NULL;
805 uint32_t entries_read = 0;
806 uint32_t total_entries = 0;
807 int i;
810 if (argc < 1 || c->display_usage) {
811 rpc_user_usage(c, argc, argv);
812 return 0;
815 status = NetUserGetGroups(c->opt_host,
816 argv[0],
818 (uint8_t **)(void *)&u0,
819 (uint32_t)-1,
820 &entries_read,
821 &total_entries);
822 if (status != 0) {
823 d_fprintf(stderr,
824 _("Failed to get groups for '%s' with error: %s.\n"),
825 argv[0], libnetapi_get_error_string(c->netapi_ctx,
826 status));
827 return -1;
830 for (i=0; i < entries_read; i++) {
831 printf("%s\n", u0->grui0_name);
832 u0++;
835 return 0;
839 * List users on a remote RPC server.
841 * All parameters are provided by the run_rpc_command function, except for
842 * argc, argv which are passed through.
844 * @param domain_sid The domain sid acquired from the remote server.
845 * @param cli A cli_state connected to the server.
846 * @param mem_ctx Talloc context, destroyed on completion of the function.
847 * @param argc Standard main() style argc.
848 * @param argv Standard main() style argv. Initial components are already
849 * stripped.
851 * @return Normal NTSTATUS return.
854 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
856 NET_API_STATUS status;
857 uint32_t start_idx=0, num_entries, i, loop_count = 0;
858 struct NET_DISPLAY_USER *info = NULL;
859 void *buffer = NULL;
861 /* Query domain users */
862 if (c->opt_long_list_entries)
863 d_printf(_("\nUser name Comment"
864 "\n-----------------------------\n"));
865 do {
866 uint32_t max_entries, max_size;
868 get_query_dispinfo_params(
869 loop_count, &max_entries, &max_size);
871 status = NetQueryDisplayInformation(c->opt_host,
873 start_idx,
874 max_entries,
875 max_size,
876 &num_entries,
877 &buffer);
878 if (status != 0 && status != ERROR_MORE_DATA) {
879 return status;
882 info = (struct NET_DISPLAY_USER *)buffer;
884 for (i = 0; i < num_entries; i++) {
886 if (c->opt_long_list_entries)
887 printf("%-21.21s %s\n", info->usri1_name,
888 info->usri1_comment);
889 else
890 printf("%s\n", info->usri1_name);
891 info++;
894 NetApiBufferFree(buffer);
896 loop_count++;
897 start_idx += num_entries;
899 } while (status == ERROR_MORE_DATA);
901 return status;
905 * 'net rpc user' entrypoint.
906 * @param argc Standard main() style argc.
907 * @param argv Standard main() style argv. Initial components are already
908 * stripped.
911 int net_rpc_user(struct net_context *c, int argc, const char **argv)
913 NET_API_STATUS status;
915 struct functable func[] = {
917 "add",
918 rpc_user_add,
919 NET_TRANSPORT_RPC,
920 N_("Add specified user"),
921 N_("net rpc user add\n"
922 " Add specified user")
925 "info",
926 rpc_user_info,
927 NET_TRANSPORT_RPC,
928 N_("List domain groups of user"),
929 N_("net rpc user info\n"
930 " Lis domain groups of user")
933 "delete",
934 rpc_user_delete,
935 NET_TRANSPORT_RPC,
936 N_("Remove specified user"),
937 N_("net rpc user delete\n"
938 " Remove specified user")
941 "password",
942 rpc_user_password,
943 NET_TRANSPORT_RPC,
944 N_("Change user password"),
945 N_("net rpc user password\n"
946 " Change user password")
949 "rename",
950 rpc_user_rename,
951 NET_TRANSPORT_RPC,
952 N_("Rename specified user"),
953 N_("net rpc user rename\n"
954 " Rename specified user")
956 {NULL, NULL, 0, NULL, NULL}
959 status = libnetapi_init(&c->netapi_ctx);
960 if (status != 0) {
961 return -1;
963 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
964 libnetapi_set_password(c->netapi_ctx, c->opt_password);
965 if (c->opt_kerberos) {
966 libnetapi_set_use_kerberos(c->netapi_ctx);
969 if (argc == 0) {
970 if (c->display_usage) {
971 d_printf(_("Usage:\n"));
972 d_printf(_("net rpc user\n"
973 " List all users\n"));
974 net_display_usage_from_functable(func);
975 return 0;
978 return rpc_user_list(c, argc, argv);
981 return net_run_function(c, argc, argv, "net rpc user", func);
984 static NTSTATUS rpc_sh_user_list(struct net_context *c,
985 TALLOC_CTX *mem_ctx,
986 struct rpc_sh_ctx *ctx,
987 struct rpc_pipe_client *pipe_hnd,
988 int argc, const char **argv)
990 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
993 static NTSTATUS rpc_sh_user_info(struct net_context *c,
994 TALLOC_CTX *mem_ctx,
995 struct rpc_sh_ctx *ctx,
996 struct rpc_pipe_client *pipe_hnd,
997 int argc, const char **argv)
999 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1002 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1003 TALLOC_CTX *mem_ctx,
1004 struct rpc_sh_ctx *ctx,
1005 struct rpc_pipe_client *pipe_hnd,
1006 int argc, const char **argv,
1007 NTSTATUS (*fn)(
1008 struct net_context *c,
1009 TALLOC_CTX *mem_ctx,
1010 struct rpc_sh_ctx *ctx,
1011 struct rpc_pipe_client *pipe_hnd,
1012 struct policy_handle *user_hnd,
1013 int argc, const char **argv))
1015 struct policy_handle connect_pol, domain_pol, user_pol;
1016 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1017 DOM_SID sid;
1018 uint32 rid;
1019 enum lsa_SidType type;
1021 if (argc == 0) {
1022 d_fprintf(stderr, _("usage: %s <username>\n"), ctx->whoami);
1023 return NT_STATUS_INVALID_PARAMETER;
1026 ZERO_STRUCT(connect_pol);
1027 ZERO_STRUCT(domain_pol);
1028 ZERO_STRUCT(user_pol);
1030 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1031 argv[0], NULL, NULL, &sid, &type);
1032 if (!NT_STATUS_IS_OK(result)) {
1033 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1034 nt_errstr(result));
1035 goto done;
1038 if (type != SID_NAME_USER) {
1039 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1040 sid_type_lookup(type));
1041 result = NT_STATUS_NO_SUCH_USER;
1042 goto done;
1045 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1046 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1047 result = NT_STATUS_NO_SUCH_USER;
1048 goto done;
1051 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1052 pipe_hnd->desthost,
1053 MAXIMUM_ALLOWED_ACCESS,
1054 &connect_pol);
1055 if (!NT_STATUS_IS_OK(result)) {
1056 goto done;
1059 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1060 &connect_pol,
1061 MAXIMUM_ALLOWED_ACCESS,
1062 ctx->domain_sid,
1063 &domain_pol);
1064 if (!NT_STATUS_IS_OK(result)) {
1065 goto done;
1068 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1069 &domain_pol,
1070 MAXIMUM_ALLOWED_ACCESS,
1071 rid,
1072 &user_pol);
1073 if (!NT_STATUS_IS_OK(result)) {
1074 goto done;
1077 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1079 done:
1080 if (is_valid_policy_hnd(&user_pol)) {
1081 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1083 if (is_valid_policy_hnd(&domain_pol)) {
1084 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1086 if (is_valid_policy_hnd(&connect_pol)) {
1087 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1089 return result;
1092 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1093 TALLOC_CTX *mem_ctx,
1094 struct rpc_sh_ctx *ctx,
1095 struct rpc_pipe_client *pipe_hnd,
1096 struct policy_handle *user_hnd,
1097 int argc, const char **argv)
1099 NTSTATUS result;
1100 union samr_UserInfo *info = NULL;
1102 if (argc != 0) {
1103 d_fprintf(stderr, _("usage: %s show <username>\n"),ctx->whoami);
1104 return NT_STATUS_INVALID_PARAMETER;
1107 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1108 user_hnd,
1110 &info);
1111 if (!NT_STATUS_IS_OK(result)) {
1112 return result;
1115 d_printf(_("user rid: %d, group rid: %d\n"),
1116 info->info21.rid,
1117 info->info21.primary_gid);
1119 return result;
1122 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1123 TALLOC_CTX *mem_ctx,
1124 struct rpc_sh_ctx *ctx,
1125 struct rpc_pipe_client *pipe_hnd,
1126 int argc, const char **argv)
1128 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1129 rpc_sh_user_show_internals);
1132 #define FETCHSTR(name, rec) \
1133 do { if (strequal(ctx->thiscmd, name)) { \
1134 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1135 } while (0);
1137 #define SETSTR(name, rec, flag) \
1138 do { if (strequal(ctx->thiscmd, name)) { \
1139 init_lsa_String(&(info->info21.rec), argv[0]); \
1140 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1141 } while (0);
1143 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1144 TALLOC_CTX *mem_ctx,
1145 struct rpc_sh_ctx *ctx,
1146 struct rpc_pipe_client *pipe_hnd,
1147 struct policy_handle *user_hnd,
1148 int argc, const char **argv)
1150 NTSTATUS result;
1151 const char *username;
1152 const char *oldval = "";
1153 union samr_UserInfo *info = NULL;
1155 if (argc > 1) {
1156 d_fprintf(stderr, _("usage: %s <username> [new value|NULL]\n"),
1157 ctx->whoami);
1158 return NT_STATUS_INVALID_PARAMETER;
1161 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1162 user_hnd,
1164 &info);
1165 if (!NT_STATUS_IS_OK(result)) {
1166 return result;
1169 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1171 FETCHSTR("fullname", full_name);
1172 FETCHSTR("homedir", home_directory);
1173 FETCHSTR("homedrive", home_drive);
1174 FETCHSTR("logonscript", logon_script);
1175 FETCHSTR("profilepath", profile_path);
1176 FETCHSTR("description", description);
1178 if (argc == 0) {
1179 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1180 goto done;
1183 if (strcmp(argv[0], "NULL") == 0) {
1184 argv[0] = "";
1187 ZERO_STRUCT(info->info21);
1189 SETSTR("fullname", full_name, FULL_NAME);
1190 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1191 SETSTR("homedrive", home_drive, HOME_DRIVE);
1192 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1193 SETSTR("profilepath", profile_path, PROFILE_PATH);
1194 SETSTR("description", description, DESCRIPTION);
1196 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1197 user_hnd,
1199 info);
1201 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1202 ctx->thiscmd, oldval, argv[0]);
1204 done:
1206 return result;
1209 #define HANDLEFLG(name, rec) \
1210 do { if (strequal(ctx->thiscmd, name)) { \
1211 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1212 if (newval) { \
1213 newflags = oldflags | ACB_##rec; \
1214 } else { \
1215 newflags = oldflags & ~ACB_##rec; \
1216 } } } while (0);
1218 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1219 TALLOC_CTX *mem_ctx,
1220 struct rpc_sh_ctx *ctx,
1221 struct rpc_pipe_client *pipe_hnd,
1222 int argc, const char **argv)
1224 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1225 rpc_sh_user_str_edit_internals);
1228 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1229 TALLOC_CTX *mem_ctx,
1230 struct rpc_sh_ctx *ctx,
1231 struct rpc_pipe_client *pipe_hnd,
1232 struct policy_handle *user_hnd,
1233 int argc, const char **argv)
1235 NTSTATUS result;
1236 const char *username;
1237 const char *oldval = "unknown";
1238 uint32 oldflags, newflags;
1239 bool newval;
1240 union samr_UserInfo *info = NULL;
1242 if ((argc > 1) ||
1243 ((argc == 1) && !strequal(argv[0], "yes") &&
1244 !strequal(argv[0], "no"))) {
1245 /* TRANSATORS: The yes|no here are program keywords. Please do
1246 not translate. */
1247 d_fprintf(stderr, _("usage: %s <username> [yes|no]\n"),
1248 ctx->whoami);
1249 return NT_STATUS_INVALID_PARAMETER;
1252 newval = strequal(argv[0], "yes");
1254 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1255 user_hnd,
1257 &info);
1258 if (!NT_STATUS_IS_OK(result)) {
1259 return result;
1262 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1263 oldflags = info->info21.acct_flags;
1264 newflags = info->info21.acct_flags;
1266 HANDLEFLG("disabled", DISABLED);
1267 HANDLEFLG("pwnotreq", PWNOTREQ);
1268 HANDLEFLG("autolock", AUTOLOCK);
1269 HANDLEFLG("pwnoexp", PWNOEXP);
1271 if (argc == 0) {
1272 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1273 oldval);
1274 goto done;
1277 ZERO_STRUCT(info->info21);
1279 info->info21.acct_flags = newflags;
1280 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1282 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1283 user_hnd,
1285 info);
1287 if (NT_STATUS_IS_OK(result)) {
1288 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1289 ctx->thiscmd, oldval, argv[0]);
1292 done:
1294 return result;
1297 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1298 TALLOC_CTX *mem_ctx,
1299 struct rpc_sh_ctx *ctx,
1300 struct rpc_pipe_client *pipe_hnd,
1301 int argc, const char **argv)
1303 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1304 rpc_sh_user_flag_edit_internals);
1307 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1308 TALLOC_CTX *mem_ctx,
1309 struct rpc_sh_ctx *ctx)
1311 static struct rpc_sh_cmd cmds[] = {
1313 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1314 N_("Show/Set a user's full name") },
1316 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1317 N_("Show/Set a user's home directory") },
1319 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1320 N_("Show/Set a user's home drive") },
1322 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1323 N_("Show/Set a user's logon script") },
1325 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1326 N_("Show/Set a user's profile path") },
1328 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1329 N_("Show/Set a user's description") },
1331 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1332 N_("Show/Set whether a user is disabled") },
1334 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1335 N_("Show/Set whether a user locked out") },
1337 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1338 N_("Show/Set whether a user does not need a password") },
1340 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1341 N_("Show/Set whether a user's password does not expire") },
1343 { NULL, NULL, 0, NULL, NULL }
1346 return cmds;
1349 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1350 TALLOC_CTX *mem_ctx,
1351 struct rpc_sh_ctx *ctx)
1353 static struct rpc_sh_cmd cmds[] = {
1355 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1356 N_("List available users") },
1358 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1359 N_("List the domain groups a user is member of") },
1361 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1362 N_("Show info about a user") },
1364 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1365 N_("Show/Modify a user's fields") },
1367 { NULL, NULL, 0, NULL, NULL }
1370 return cmds;
1373 /****************************************************************************/
1376 * Basic usage function for 'net rpc group'.
1377 * @param argc Standard main() style argc.
1378 * @param argv Standard main() style argv. Initial components are already
1379 * stripped.
1382 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1384 return net_group_usage(c, argc, argv);
1388 * Delete group on a remote RPC server.
1390 * All parameters are provided by the run_rpc_command function, except for
1391 * argc, argv which are passed through.
1393 * @param domain_sid The domain sid acquired from the remote server.
1394 * @param cli A cli_state connected to the server.
1395 * @param mem_ctx Talloc context, destroyed on completion of the function.
1396 * @param argc Standard main() style argc.
1397 * @param argv Standard main() style argv. Initial components are already
1398 * stripped.
1400 * @return Normal NTSTATUS return.
1403 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1404 const DOM_SID *domain_sid,
1405 const char *domain_name,
1406 struct cli_state *cli,
1407 struct rpc_pipe_client *pipe_hnd,
1408 TALLOC_CTX *mem_ctx,
1409 int argc,
1410 const char **argv)
1412 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1413 bool group_is_primary = false;
1414 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1415 uint32_t group_rid;
1416 struct samr_RidTypeArray *rids = NULL;
1417 /* char **names; */
1418 int i;
1419 /* struct samr_RidWithAttribute *user_gids; */
1421 struct samr_Ids group_rids, name_types;
1422 struct lsa_String lsa_acct_name;
1423 union samr_UserInfo *info = NULL;
1425 if (argc < 1 || c->display_usage) {
1426 rpc_group_usage(c, argc,argv);
1427 return NT_STATUS_OK; /* ok? */
1430 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1431 pipe_hnd->desthost,
1432 MAXIMUM_ALLOWED_ACCESS,
1433 &connect_pol);
1435 if (!NT_STATUS_IS_OK(result)) {
1436 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1437 goto done;
1440 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1441 &connect_pol,
1442 MAXIMUM_ALLOWED_ACCESS,
1443 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1444 &domain_pol);
1446 if (!NT_STATUS_IS_OK(result)) {
1447 d_fprintf(stderr, _("Request open_domain failed\n"));
1448 goto done;
1451 init_lsa_String(&lsa_acct_name, argv[0]);
1453 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1454 &domain_pol,
1456 &lsa_acct_name,
1457 &group_rids,
1458 &name_types);
1459 if (!NT_STATUS_IS_OK(result)) {
1460 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1461 goto done;
1464 switch (name_types.ids[0])
1466 case SID_NAME_DOM_GRP:
1467 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1468 &domain_pol,
1469 MAXIMUM_ALLOWED_ACCESS,
1470 group_rids.ids[0],
1471 &group_pol);
1472 if (!NT_STATUS_IS_OK(result)) {
1473 d_fprintf(stderr, _("Request open_group failed"));
1474 goto done;
1477 group_rid = group_rids.ids[0];
1479 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1480 &group_pol,
1481 &rids);
1483 if (!NT_STATUS_IS_OK(result)) {
1484 d_fprintf(stderr,
1485 _("Unable to query group members of %s"),
1486 argv[0]);
1487 goto done;
1490 if (c->opt_verbose) {
1491 d_printf(
1492 _("Domain Group %s (rid: %d) has %d members\n"),
1493 argv[0],group_rid, rids->count);
1496 /* Check if group is anyone's primary group */
1497 for (i = 0; i < rids->count; i++)
1499 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1500 &domain_pol,
1501 MAXIMUM_ALLOWED_ACCESS,
1502 rids->rids[i],
1503 &user_pol);
1505 if (!NT_STATUS_IS_OK(result)) {
1506 d_fprintf(stderr,
1507 _("Unable to open group member %d\n"),
1508 rids->rids[i]);
1509 goto done;
1512 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1513 &user_pol,
1515 &info);
1517 if (!NT_STATUS_IS_OK(result)) {
1518 d_fprintf(stderr,
1519 _("Unable to lookup userinfo for group "
1520 "member %d\n"),
1521 rids->rids[i]);
1522 goto done;
1525 if (info->info21.primary_gid == group_rid) {
1526 if (c->opt_verbose) {
1527 d_printf(_("Group is primary group "
1528 "of %s\n"),
1529 info->info21.account_name.string);
1531 group_is_primary = true;
1534 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1537 if (group_is_primary) {
1538 d_fprintf(stderr, _("Unable to delete group because "
1539 "some of it's members have it as primary "
1540 "group\n"));
1541 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1542 goto done;
1545 /* remove all group members */
1546 for (i = 0; i < rids->count; i++)
1548 if (c->opt_verbose)
1549 d_printf(_("Remove group member %d..."),
1550 rids->rids[i]);
1551 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1552 &group_pol,
1553 rids->rids[i]);
1555 if (NT_STATUS_IS_OK(result)) {
1556 if (c->opt_verbose)
1557 d_printf(_("ok\n"));
1558 } else {
1559 if (c->opt_verbose)
1560 d_printf(_("failed\n"));
1561 goto done;
1565 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1566 &group_pol);
1568 break;
1569 /* removing a local group is easier... */
1570 case SID_NAME_ALIAS:
1571 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1572 &domain_pol,
1573 MAXIMUM_ALLOWED_ACCESS,
1574 group_rids.ids[0],
1575 &group_pol);
1577 if (!NT_STATUS_IS_OK(result)) {
1578 d_fprintf(stderr, _("Request open_alias failed\n"));
1579 goto done;
1582 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1583 &group_pol);
1584 break;
1585 default:
1586 d_fprintf(stderr, _("%s is of type %s. This command is only "
1587 "for deleting local or global groups\n"),
1588 argv[0],sid_type_lookup(name_types.ids[0]));
1589 result = NT_STATUS_UNSUCCESSFUL;
1590 goto done;
1593 if (NT_STATUS_IS_OK(result)) {
1594 if (c->opt_verbose)
1595 d_printf(_("Deleted %s '%s'\n"),
1596 sid_type_lookup(name_types.ids[0]), argv[0]);
1597 } else {
1598 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1599 get_friendly_nt_error_msg(result));
1602 done:
1603 return result;
1607 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1609 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1610 rpc_group_delete_internals, argc,argv);
1613 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1615 NET_API_STATUS status;
1616 struct GROUP_INFO_1 info1;
1617 uint32_t parm_error = 0;
1619 if (argc != 1 || c->display_usage) {
1620 rpc_group_usage(c, argc, argv);
1621 return 0;
1624 ZERO_STRUCT(info1);
1626 info1.grpi1_name = argv[0];
1627 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1628 info1.grpi1_comment = c->opt_comment;
1631 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1633 if (status != 0) {
1634 d_fprintf(stderr,
1635 _("Failed to add group '%s' with error: %s.\n"),
1636 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1637 status));
1638 return -1;
1639 } else {
1640 d_printf(_("Added group '%s'.\n"), argv[0]);
1643 return 0;
1646 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1648 NET_API_STATUS status;
1649 struct LOCALGROUP_INFO_1 info1;
1650 uint32_t parm_error = 0;
1652 if (argc != 1 || c->display_usage) {
1653 rpc_group_usage(c, argc, argv);
1654 return 0;
1657 ZERO_STRUCT(info1);
1659 info1.lgrpi1_name = argv[0];
1660 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1661 info1.lgrpi1_comment = c->opt_comment;
1664 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1666 if (status != 0) {
1667 d_fprintf(stderr,
1668 _("Failed to add alias '%s' with error: %s.\n"),
1669 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1670 status));
1671 return -1;
1672 } else {
1673 d_printf(_("Added alias '%s'.\n"), argv[0]);
1676 return 0;
1679 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1681 if (c->opt_localgroup)
1682 return rpc_alias_add_internals(c, argc, argv);
1684 return rpc_group_add_internals(c, argc, argv);
1687 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1688 TALLOC_CTX *mem_ctx,
1689 const char *name,
1690 DOM_SID *sid,
1691 enum lsa_SidType *type)
1693 DOM_SID *sids = NULL;
1694 enum lsa_SidType *types = NULL;
1695 struct rpc_pipe_client *pipe_hnd = NULL;
1696 struct policy_handle lsa_pol;
1697 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1699 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1700 &pipe_hnd);
1701 if (!NT_STATUS_IS_OK(result)) {
1702 goto done;
1705 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1706 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1708 if (!NT_STATUS_IS_OK(result)) {
1709 goto done;
1712 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1713 &name, NULL, 1, &sids, &types);
1715 if (NT_STATUS_IS_OK(result)) {
1716 sid_copy(sid, &sids[0]);
1717 *type = types[0];
1720 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1722 done:
1723 if (pipe_hnd) {
1724 TALLOC_FREE(pipe_hnd);
1727 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1729 /* Try as S-1-5-whatever */
1731 DOM_SID tmp_sid;
1733 if (string_to_sid(&tmp_sid, name)) {
1734 sid_copy(sid, &tmp_sid);
1735 *type = SID_NAME_UNKNOWN;
1736 result = NT_STATUS_OK;
1740 return result;
1743 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1744 TALLOC_CTX *mem_ctx,
1745 const DOM_SID *group_sid,
1746 const char *member)
1748 struct policy_handle connect_pol, domain_pol;
1749 NTSTATUS result;
1750 uint32 group_rid;
1751 struct policy_handle group_pol;
1753 struct samr_Ids rids, rid_types;
1754 struct lsa_String lsa_acct_name;
1756 DOM_SID sid;
1758 sid_copy(&sid, group_sid);
1760 if (!sid_split_rid(&sid, &group_rid)) {
1761 return NT_STATUS_UNSUCCESSFUL;
1764 /* Get sam policy handle */
1765 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1766 pipe_hnd->desthost,
1767 MAXIMUM_ALLOWED_ACCESS,
1768 &connect_pol);
1769 if (!NT_STATUS_IS_OK(result)) {
1770 return result;
1773 /* Get domain policy handle */
1774 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1775 &connect_pol,
1776 MAXIMUM_ALLOWED_ACCESS,
1777 &sid,
1778 &domain_pol);
1779 if (!NT_STATUS_IS_OK(result)) {
1780 return result;
1783 init_lsa_String(&lsa_acct_name, member);
1785 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1786 &domain_pol,
1788 &lsa_acct_name,
1789 &rids,
1790 &rid_types);
1792 if (!NT_STATUS_IS_OK(result)) {
1793 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1794 member);
1795 goto done;
1798 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1799 &domain_pol,
1800 MAXIMUM_ALLOWED_ACCESS,
1801 group_rid,
1802 &group_pol);
1804 if (!NT_STATUS_IS_OK(result)) {
1805 goto done;
1808 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1809 &group_pol,
1810 rids.ids[0],
1811 0x0005); /* unknown flags */
1813 done:
1814 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1815 return result;
1818 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1819 TALLOC_CTX *mem_ctx,
1820 const DOM_SID *alias_sid,
1821 const char *member)
1823 struct policy_handle connect_pol, domain_pol;
1824 NTSTATUS result;
1825 uint32 alias_rid;
1826 struct policy_handle alias_pol;
1828 DOM_SID member_sid;
1829 enum lsa_SidType member_type;
1831 DOM_SID sid;
1833 sid_copy(&sid, alias_sid);
1835 if (!sid_split_rid(&sid, &alias_rid)) {
1836 return NT_STATUS_UNSUCCESSFUL;
1839 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1840 member, &member_sid, &member_type);
1842 if (!NT_STATUS_IS_OK(result)) {
1843 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1844 member);
1845 return result;
1848 /* Get sam policy handle */
1849 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1850 pipe_hnd->desthost,
1851 MAXIMUM_ALLOWED_ACCESS,
1852 &connect_pol);
1853 if (!NT_STATUS_IS_OK(result)) {
1854 goto done;
1857 /* Get domain policy handle */
1858 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1859 &connect_pol,
1860 MAXIMUM_ALLOWED_ACCESS,
1861 &sid,
1862 &domain_pol);
1863 if (!NT_STATUS_IS_OK(result)) {
1864 goto done;
1867 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1868 &domain_pol,
1869 MAXIMUM_ALLOWED_ACCESS,
1870 alias_rid,
1871 &alias_pol);
1873 if (!NT_STATUS_IS_OK(result)) {
1874 return result;
1877 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1878 &alias_pol,
1879 &member_sid);
1881 if (!NT_STATUS_IS_OK(result)) {
1882 return result;
1885 done:
1886 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1887 return result;
1890 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1891 const DOM_SID *domain_sid,
1892 const char *domain_name,
1893 struct cli_state *cli,
1894 struct rpc_pipe_client *pipe_hnd,
1895 TALLOC_CTX *mem_ctx,
1896 int argc,
1897 const char **argv)
1899 DOM_SID group_sid;
1900 enum lsa_SidType group_type;
1902 if (argc != 2 || c->display_usage) {
1903 d_printf(_("Usage:\n"
1904 "net rpc group addmem <group> <member>\n"
1905 " Add a member to a group\n"
1906 " group\tGroup to add member to\n"
1907 " member\tMember to add to group\n"));
1908 return NT_STATUS_UNSUCCESSFUL;
1911 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1912 &group_sid, &group_type))) {
1913 d_fprintf(stderr, _("Could not lookup group name %s\n"),
1914 argv[0]);
1915 return NT_STATUS_UNSUCCESSFUL;
1918 if (group_type == SID_NAME_DOM_GRP) {
1919 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
1920 &group_sid, argv[1]);
1922 if (!NT_STATUS_IS_OK(result)) {
1923 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
1924 argv[1], argv[0], nt_errstr(result));
1926 return result;
1929 if (group_type == SID_NAME_ALIAS) {
1930 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
1931 &group_sid, argv[1]);
1933 if (!NT_STATUS_IS_OK(result)) {
1934 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
1935 argv[1], argv[0], nt_errstr(result));
1937 return result;
1940 d_fprintf(stderr, _("Can only add members to global or local groups "
1941 "which %s is not\n"), argv[0]);
1943 return NT_STATUS_UNSUCCESSFUL;
1946 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
1948 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1949 rpc_group_addmem_internals,
1950 argc, argv);
1953 static NTSTATUS rpc_del_groupmem(struct net_context *c,
1954 struct rpc_pipe_client *pipe_hnd,
1955 TALLOC_CTX *mem_ctx,
1956 const DOM_SID *group_sid,
1957 const char *member)
1959 struct policy_handle connect_pol, domain_pol;
1960 NTSTATUS result;
1961 uint32 group_rid;
1962 struct policy_handle group_pol;
1964 struct samr_Ids rids, rid_types;
1965 struct lsa_String lsa_acct_name;
1967 DOM_SID sid;
1969 sid_copy(&sid, group_sid);
1971 if (!sid_split_rid(&sid, &group_rid))
1972 return NT_STATUS_UNSUCCESSFUL;
1974 /* Get sam policy handle */
1975 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1976 pipe_hnd->desthost,
1977 MAXIMUM_ALLOWED_ACCESS,
1978 &connect_pol);
1979 if (!NT_STATUS_IS_OK(result))
1980 return result;
1982 /* Get domain policy handle */
1983 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1984 &connect_pol,
1985 MAXIMUM_ALLOWED_ACCESS,
1986 &sid,
1987 &domain_pol);
1988 if (!NT_STATUS_IS_OK(result))
1989 return result;
1991 init_lsa_String(&lsa_acct_name, member);
1993 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1994 &domain_pol,
1996 &lsa_acct_name,
1997 &rids,
1998 &rid_types);
1999 if (!NT_STATUS_IS_OK(result)) {
2000 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2001 member);
2002 goto done;
2005 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2006 &domain_pol,
2007 MAXIMUM_ALLOWED_ACCESS,
2008 group_rid,
2009 &group_pol);
2011 if (!NT_STATUS_IS_OK(result))
2012 goto done;
2014 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2015 &group_pol,
2016 rids.ids[0]);
2018 done:
2019 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2020 return result;
2023 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2024 TALLOC_CTX *mem_ctx,
2025 const DOM_SID *alias_sid,
2026 const char *member)
2028 struct policy_handle connect_pol, domain_pol;
2029 NTSTATUS result;
2030 uint32 alias_rid;
2031 struct policy_handle alias_pol;
2033 DOM_SID member_sid;
2034 enum lsa_SidType member_type;
2036 DOM_SID sid;
2038 sid_copy(&sid, alias_sid);
2040 if (!sid_split_rid(&sid, &alias_rid))
2041 return NT_STATUS_UNSUCCESSFUL;
2043 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2044 member, &member_sid, &member_type);
2046 if (!NT_STATUS_IS_OK(result)) {
2047 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2048 member);
2049 return result;
2052 /* Get sam policy handle */
2053 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2054 pipe_hnd->desthost,
2055 MAXIMUM_ALLOWED_ACCESS,
2056 &connect_pol);
2057 if (!NT_STATUS_IS_OK(result)) {
2058 goto done;
2061 /* Get domain policy handle */
2062 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2063 &connect_pol,
2064 MAXIMUM_ALLOWED_ACCESS,
2065 &sid,
2066 &domain_pol);
2067 if (!NT_STATUS_IS_OK(result)) {
2068 goto done;
2071 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2072 &domain_pol,
2073 MAXIMUM_ALLOWED_ACCESS,
2074 alias_rid,
2075 &alias_pol);
2077 if (!NT_STATUS_IS_OK(result))
2078 return result;
2080 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2081 &alias_pol,
2082 &member_sid);
2084 if (!NT_STATUS_IS_OK(result))
2085 return result;
2087 done:
2088 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2089 return result;
2092 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2093 const DOM_SID *domain_sid,
2094 const char *domain_name,
2095 struct cli_state *cli,
2096 struct rpc_pipe_client *pipe_hnd,
2097 TALLOC_CTX *mem_ctx,
2098 int argc,
2099 const char **argv)
2101 DOM_SID group_sid;
2102 enum lsa_SidType group_type;
2104 if (argc != 2 || c->display_usage) {
2105 d_printf(_("Usage:\n"
2106 "net rpc group delmem <group> <member>\n"
2107 " Delete a member from a group\n"
2108 " group\tGroup to delete member from\n"
2109 " member\tMember to delete from group\n"));
2110 return NT_STATUS_UNSUCCESSFUL;
2113 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2114 &group_sid, &group_type))) {
2115 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2116 argv[0]);
2117 return NT_STATUS_UNSUCCESSFUL;
2120 if (group_type == SID_NAME_DOM_GRP) {
2121 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2122 &group_sid, argv[1]);
2124 if (!NT_STATUS_IS_OK(result)) {
2125 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2126 argv[1], argv[0], nt_errstr(result));
2128 return result;
2131 if (group_type == SID_NAME_ALIAS) {
2132 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2133 &group_sid, argv[1]);
2135 if (!NT_STATUS_IS_OK(result)) {
2136 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2137 argv[1], argv[0], nt_errstr(result));
2139 return result;
2142 d_fprintf(stderr, _("Can only delete members from global or local "
2143 "groups which %s is not\n"), argv[0]);
2145 return NT_STATUS_UNSUCCESSFUL;
2148 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2150 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2151 rpc_group_delmem_internals,
2152 argc, argv);
2156 * List groups on a remote RPC server.
2158 * All parameters are provided by the run_rpc_command function, except for
2159 * argc, argv which are passes through.
2161 * @param domain_sid The domain sid acquired from the remote server.
2162 * @param cli A cli_state connected to the server.
2163 * @param mem_ctx Talloc context, destroyed on completion of the function.
2164 * @param argc Standard main() style argc.
2165 * @param argv Standard main() style argv. Initial components are already
2166 * stripped.
2168 * @return Normal NTSTATUS return.
2171 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2172 const DOM_SID *domain_sid,
2173 const char *domain_name,
2174 struct cli_state *cli,
2175 struct rpc_pipe_client *pipe_hnd,
2176 TALLOC_CTX *mem_ctx,
2177 int argc,
2178 const char **argv)
2180 struct policy_handle connect_pol, domain_pol;
2181 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2182 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2183 struct samr_SamArray *groups = NULL;
2184 bool global = false;
2185 bool local = false;
2186 bool builtin = false;
2188 if (c->display_usage) {
2189 d_printf(_("Usage:\n"
2190 "net rpc group list [global] [local] [builtin]\n"
2191 " List groups on RPC server\n"
2192 " global\tList global groups\n"
2193 " local\tList local groups\n"
2194 " builtin\tList builtin groups\n"
2195 " If none of global, local or builtin is "
2196 "specified, all three options are considered "
2197 "set\n"));
2198 return NT_STATUS_OK;
2201 if (argc == 0) {
2202 global = true;
2203 local = true;
2204 builtin = true;
2207 for (i=0; i<argc; i++) {
2208 if (strequal(argv[i], "global"))
2209 global = true;
2211 if (strequal(argv[i], "local"))
2212 local = true;
2214 if (strequal(argv[i], "builtin"))
2215 builtin = true;
2218 /* Get sam policy handle */
2220 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2221 pipe_hnd->desthost,
2222 MAXIMUM_ALLOWED_ACCESS,
2223 &connect_pol);
2224 if (!NT_STATUS_IS_OK(result)) {
2225 goto done;
2228 /* Get domain policy handle */
2230 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2231 &connect_pol,
2232 MAXIMUM_ALLOWED_ACCESS,
2233 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2234 &domain_pol);
2235 if (!NT_STATUS_IS_OK(result)) {
2236 goto done;
2239 /* Query domain groups */
2240 if (c->opt_long_list_entries)
2241 d_printf(_("\nGroup name Comment"
2242 "\n-----------------------------\n"));
2243 do {
2244 uint32_t max_size, total_size, returned_size;
2245 union samr_DispInfo info;
2247 if (!global) break;
2249 get_query_dispinfo_params(
2250 loop_count, &max_entries, &max_size);
2252 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2253 &domain_pol,
2255 start_idx,
2256 max_entries,
2257 max_size,
2258 &total_size,
2259 &returned_size,
2260 &info);
2261 num_entries = info.info3.count;
2262 start_idx += info.info3.count;
2264 if (!NT_STATUS_IS_OK(result) &&
2265 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2266 break;
2268 for (i = 0; i < num_entries; i++) {
2270 const char *group = NULL;
2271 const char *desc = NULL;
2273 group = info.info3.entries[i].account_name.string;
2274 desc = info.info3.entries[i].description.string;
2276 if (c->opt_long_list_entries)
2277 printf("%-21.21s %-50.50s\n",
2278 group, desc);
2279 else
2280 printf("%s\n", group);
2282 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2283 /* query domain aliases */
2284 start_idx = 0;
2285 do {
2286 if (!local) break;
2288 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2289 &domain_pol,
2290 &start_idx,
2291 &groups,
2292 0xffff,
2293 &num_entries);
2294 if (!NT_STATUS_IS_OK(result) &&
2295 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2296 break;
2298 for (i = 0; i < num_entries; i++) {
2300 const char *description = NULL;
2302 if (c->opt_long_list_entries) {
2304 struct policy_handle alias_pol;
2305 union samr_AliasInfo *info = NULL;
2307 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2308 &domain_pol,
2309 0x8,
2310 groups->entries[i].idx,
2311 &alias_pol))) &&
2312 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2313 &alias_pol,
2315 &info))) &&
2316 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2317 &alias_pol)))) {
2318 description = info->description.string;
2322 if (description != NULL) {
2323 printf("%-21.21s %-50.50s\n",
2324 groups->entries[i].name.string,
2325 description);
2326 } else {
2327 printf("%s\n", groups->entries[i].name.string);
2330 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2331 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2332 /* Get builtin policy handle */
2334 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2335 &connect_pol,
2336 MAXIMUM_ALLOWED_ACCESS,
2337 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2338 &domain_pol);
2339 if (!NT_STATUS_IS_OK(result)) {
2340 goto done;
2342 /* query builtin aliases */
2343 start_idx = 0;
2344 do {
2345 if (!builtin) break;
2347 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2348 &domain_pol,
2349 &start_idx,
2350 &groups,
2351 max_entries,
2352 &num_entries);
2353 if (!NT_STATUS_IS_OK(result) &&
2354 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2355 break;
2357 for (i = 0; i < num_entries; i++) {
2359 const char *description = NULL;
2361 if (c->opt_long_list_entries) {
2363 struct policy_handle alias_pol;
2364 union samr_AliasInfo *info = NULL;
2366 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2367 &domain_pol,
2368 0x8,
2369 groups->entries[i].idx,
2370 &alias_pol))) &&
2371 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2372 &alias_pol,
2374 &info))) &&
2375 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2376 &alias_pol)))) {
2377 description = info->description.string;
2381 if (description != NULL) {
2382 printf("%-21.21s %-50.50s\n",
2383 groups->entries[i].name.string,
2384 description);
2385 } else {
2386 printf("%s\n", groups->entries[i].name.string);
2389 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2391 done:
2392 return result;
2395 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2397 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2398 rpc_group_list_internals,
2399 argc, argv);
2402 static NTSTATUS rpc_list_group_members(struct net_context *c,
2403 struct rpc_pipe_client *pipe_hnd,
2404 TALLOC_CTX *mem_ctx,
2405 const char *domain_name,
2406 const DOM_SID *domain_sid,
2407 struct policy_handle *domain_pol,
2408 uint32 rid)
2410 NTSTATUS result;
2411 struct policy_handle group_pol;
2412 uint32 num_members, *group_rids;
2413 int i;
2414 struct samr_RidTypeArray *rids = NULL;
2415 struct lsa_Strings names;
2416 struct samr_Ids types;
2418 fstring sid_str;
2419 sid_to_fstring(sid_str, domain_sid);
2421 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2422 domain_pol,
2423 MAXIMUM_ALLOWED_ACCESS,
2424 rid,
2425 &group_pol);
2427 if (!NT_STATUS_IS_OK(result))
2428 return result;
2430 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2431 &group_pol,
2432 &rids);
2434 if (!NT_STATUS_IS_OK(result))
2435 return result;
2437 num_members = rids->count;
2438 group_rids = rids->rids;
2440 while (num_members > 0) {
2441 int this_time = 512;
2443 if (num_members < this_time)
2444 this_time = num_members;
2446 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2447 domain_pol,
2448 this_time,
2449 group_rids,
2450 &names,
2451 &types);
2453 if (!NT_STATUS_IS_OK(result))
2454 return result;
2456 /* We only have users as members, but make the output
2457 the same as the output of alias members */
2459 for (i = 0; i < this_time; i++) {
2461 if (c->opt_long_list_entries) {
2462 printf("%s-%d %s\\%s %d\n", sid_str,
2463 group_rids[i], domain_name,
2464 names.names[i].string,
2465 SID_NAME_USER);
2466 } else {
2467 printf("%s\\%s\n", domain_name,
2468 names.names[i].string);
2472 num_members -= this_time;
2473 group_rids += 512;
2476 return NT_STATUS_OK;
2479 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2480 struct rpc_pipe_client *pipe_hnd,
2481 TALLOC_CTX *mem_ctx,
2482 struct policy_handle *domain_pol,
2483 uint32 rid)
2485 NTSTATUS result;
2486 struct rpc_pipe_client *lsa_pipe;
2487 struct policy_handle alias_pol, lsa_pol;
2488 uint32 num_members;
2489 DOM_SID *alias_sids;
2490 char **domains;
2491 char **names;
2492 enum lsa_SidType *types;
2493 int i;
2494 struct lsa_SidArray sid_array;
2496 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2497 domain_pol,
2498 MAXIMUM_ALLOWED_ACCESS,
2499 rid,
2500 &alias_pol);
2502 if (!NT_STATUS_IS_OK(result))
2503 return result;
2505 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2506 &alias_pol,
2507 &sid_array);
2509 if (!NT_STATUS_IS_OK(result)) {
2510 d_fprintf(stderr, _("Couldn't list alias members\n"));
2511 return result;
2514 num_members = sid_array.num_sids;
2516 if (num_members == 0) {
2517 return NT_STATUS_OK;
2520 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2521 &ndr_table_lsarpc.syntax_id,
2522 &lsa_pipe);
2523 if (!NT_STATUS_IS_OK(result)) {
2524 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2525 nt_errstr(result) );
2526 return result;
2529 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2530 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2532 if (!NT_STATUS_IS_OK(result)) {
2533 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2534 TALLOC_FREE(lsa_pipe);
2535 return result;
2538 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2539 if (!alias_sids) {
2540 d_fprintf(stderr, _("Out of memory\n"));
2541 TALLOC_FREE(lsa_pipe);
2542 return NT_STATUS_NO_MEMORY;
2545 for (i=0; i<num_members; i++) {
2546 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2549 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2550 num_members, alias_sids,
2551 &domains, &names, &types);
2553 if (!NT_STATUS_IS_OK(result) &&
2554 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2555 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2556 TALLOC_FREE(lsa_pipe);
2557 return result;
2560 for (i = 0; i < num_members; i++) {
2561 fstring sid_str;
2562 sid_to_fstring(sid_str, &alias_sids[i]);
2564 if (c->opt_long_list_entries) {
2565 printf("%s %s\\%s %d\n", sid_str,
2566 domains[i] ? domains[i] : _("*unknown*"),
2567 names[i] ? names[i] : _("*unknown*"), types[i]);
2568 } else {
2569 if (domains[i])
2570 printf("%s\\%s\n", domains[i], names[i]);
2571 else
2572 printf("%s\n", sid_str);
2576 TALLOC_FREE(lsa_pipe);
2577 return NT_STATUS_OK;
2580 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2581 const DOM_SID *domain_sid,
2582 const char *domain_name,
2583 struct cli_state *cli,
2584 struct rpc_pipe_client *pipe_hnd,
2585 TALLOC_CTX *mem_ctx,
2586 int argc,
2587 const char **argv)
2589 NTSTATUS result;
2590 struct policy_handle connect_pol, domain_pol;
2591 struct samr_Ids rids, rid_types;
2592 struct lsa_String lsa_acct_name;
2594 /* Get sam policy handle */
2596 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2597 pipe_hnd->desthost,
2598 MAXIMUM_ALLOWED_ACCESS,
2599 &connect_pol);
2601 if (!NT_STATUS_IS_OK(result))
2602 return result;
2604 /* Get domain policy handle */
2606 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2607 &connect_pol,
2608 MAXIMUM_ALLOWED_ACCESS,
2609 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2610 &domain_pol);
2612 if (!NT_STATUS_IS_OK(result))
2613 return result;
2615 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2617 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2618 &domain_pol,
2620 &lsa_acct_name,
2621 &rids,
2622 &rid_types);
2624 if (!NT_STATUS_IS_OK(result)) {
2626 /* Ok, did not find it in the global sam, try with builtin */
2628 DOM_SID sid_Builtin;
2630 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2632 sid_copy(&sid_Builtin, &global_sid_Builtin);
2634 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2635 &connect_pol,
2636 MAXIMUM_ALLOWED_ACCESS,
2637 &sid_Builtin,
2638 &domain_pol);
2640 if (!NT_STATUS_IS_OK(result)) {
2641 d_fprintf(stderr, _("Couldn't find group %s\n"),
2642 argv[0]);
2643 return result;
2646 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2647 &domain_pol,
2649 &lsa_acct_name,
2650 &rids,
2651 &rid_types);
2653 if (!NT_STATUS_IS_OK(result)) {
2654 d_fprintf(stderr, _("Couldn't find group %s\n"),
2655 argv[0]);
2656 return result;
2660 if (rids.count != 1) {
2661 d_fprintf(stderr, _("Couldn't find group %s\n"),
2662 argv[0]);
2663 return result;
2666 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2667 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2668 domain_sid, &domain_pol,
2669 rids.ids[0]);
2672 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2673 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2674 rids.ids[0]);
2677 return NT_STATUS_NO_SUCH_GROUP;
2680 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2682 if (argc != 1 || c->display_usage) {
2683 return rpc_group_usage(c, argc, argv);
2686 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2687 rpc_group_members_internals,
2688 argc, argv);
2691 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2693 NET_API_STATUS status;
2694 struct GROUP_INFO_0 g0;
2695 uint32_t parm_err;
2697 if (argc != 2) {
2698 d_printf(_("Usage: 'net rpc group rename group newname'\n"));
2699 return -1;
2702 g0.grpi0_name = argv[1];
2704 status = NetGroupSetInfo(c->opt_host,
2705 argv[0],
2707 (uint8_t *)&g0,
2708 &parm_err);
2710 if (status != 0) {
2711 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
2712 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2713 status));
2714 return -1;
2717 return 0;
2720 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2722 if (argc != 2 || c->display_usage) {
2723 return rpc_group_usage(c, argc, argv);
2726 return rpc_group_rename_internals(c, argc, argv);
2730 * 'net rpc group' entrypoint.
2731 * @param argc Standard main() style argc.
2732 * @param argv Standard main() style argv. Initial components are already
2733 * stripped.
2736 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2738 NET_API_STATUS status;
2740 struct functable func[] = {
2742 "add",
2743 rpc_group_add,
2744 NET_TRANSPORT_RPC,
2745 N_("Create specified group"),
2746 N_("net rpc group add\n"
2747 " Create specified group")
2750 "delete",
2751 rpc_group_delete,
2752 NET_TRANSPORT_RPC,
2753 N_("Delete specified group"),
2754 N_("net rpc group delete\n"
2755 " Delete specified group")
2758 "addmem",
2759 rpc_group_addmem,
2760 NET_TRANSPORT_RPC,
2761 N_("Add member to group"),
2762 N_("net rpc group addmem\n"
2763 " Add member to group")
2766 "delmem",
2767 rpc_group_delmem,
2768 NET_TRANSPORT_RPC,
2769 N_("Remove member from group"),
2770 N_("net rpc group delmem\n"
2771 " Remove member from group")
2774 "list",
2775 rpc_group_list,
2776 NET_TRANSPORT_RPC,
2777 N_("List groups"),
2778 N_("net rpc group list\n"
2779 " List groups")
2782 "members",
2783 rpc_group_members,
2784 NET_TRANSPORT_RPC,
2785 N_("List group members"),
2786 N_("net rpc group members\n"
2787 " List group members")
2790 "rename",
2791 rpc_group_rename,
2792 NET_TRANSPORT_RPC,
2793 N_("Rename group"),
2794 N_("net rpc group rename\n"
2795 " Rename group")
2797 {NULL, NULL, 0, NULL, NULL}
2800 status = libnetapi_init(&c->netapi_ctx);
2801 if (status != 0) {
2802 return -1;
2804 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2805 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2806 if (c->opt_kerberos) {
2807 libnetapi_set_use_kerberos(c->netapi_ctx);
2810 if (argc == 0) {
2811 if (c->display_usage) {
2812 d_printf(_("Usage:\n"));
2813 d_printf(_("net rpc group\n"
2814 " Alias for net rpc group list global "
2815 "local builtin\n"));
2816 net_display_usage_from_functable(func);
2817 return 0;
2820 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2821 rpc_group_list_internals,
2822 argc, argv);
2825 return net_run_function(c, argc, argv, "net rpc group", func);
2828 /****************************************************************************/
2830 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2832 return net_share_usage(c, argc, argv);
2836 * Add a share on a remote RPC server.
2838 * @param argc Standard main() style argc.
2839 * @param argv Standard main() style argv. Initial components are already
2840 * stripped.
2842 * @return A shell status integer (0 for success).
2845 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2847 NET_API_STATUS status;
2848 char *sharename;
2849 char *path;
2850 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2851 uint32 num_users=0, perms=0;
2852 char *password=NULL; /* don't allow a share password */
2853 struct SHARE_INFO_2 i2;
2854 uint32_t parm_error = 0;
2856 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2857 return rpc_share_usage(c, argc, argv);
2860 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2861 return -1;
2864 path = strchr(sharename, '=');
2865 if (!path) {
2866 return -1;
2869 *path++ = '\0';
2871 i2.shi2_netname = sharename;
2872 i2.shi2_type = type;
2873 i2.shi2_remark = c->opt_comment;
2874 i2.shi2_permissions = perms;
2875 i2.shi2_max_uses = c->opt_maxusers;
2876 i2.shi2_current_uses = num_users;
2877 i2.shi2_path = path;
2878 i2.shi2_passwd = password;
2880 status = NetShareAdd(c->opt_host,
2882 (uint8_t *)&i2,
2883 &parm_error);
2884 if (status != 0) {
2885 printf(_("NetShareAdd failed with: %s\n"),
2886 libnetapi_get_error_string(c->netapi_ctx, status));
2889 return status;
2893 * Delete a share on a remote RPC server.
2895 * @param domain_sid The domain sid acquired from the remote server.
2896 * @param argc Standard main() style argc.
2897 * @param argv Standard main() style argv. Initial components are already
2898 * stripped.
2900 * @return A shell status integer (0 for success).
2902 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
2904 if (argc < 1 || c->display_usage) {
2905 return rpc_share_usage(c, argc, argv);
2908 return NetShareDel(c->opt_host, argv[0], 0);
2912 * Formatted print of share info
2914 * @param r pointer to SHARE_INFO_1 to format
2917 static void display_share_info_1(struct net_context *c,
2918 struct SHARE_INFO_1 *r)
2920 if (c->opt_long_list_entries) {
2921 d_printf("%-12s %-8.8s %-50s\n",
2922 r->shi1_netname,
2923 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
2924 r->shi1_remark);
2925 } else {
2926 d_printf("%s\n", r->shi1_netname);
2930 static WERROR get_share_info(struct net_context *c,
2931 struct rpc_pipe_client *pipe_hnd,
2932 TALLOC_CTX *mem_ctx,
2933 uint32 level,
2934 int argc,
2935 const char **argv,
2936 struct srvsvc_NetShareInfoCtr *info_ctr)
2938 WERROR result;
2939 NTSTATUS status;
2940 union srvsvc_NetShareInfo info;
2942 /* no specific share requested, enumerate all */
2943 if (argc == 0) {
2945 uint32_t preferred_len = 0xffffffff;
2946 uint32_t total_entries = 0;
2947 uint32_t resume_handle = 0;
2949 info_ctr->level = level;
2951 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
2952 pipe_hnd->desthost,
2953 info_ctr,
2954 preferred_len,
2955 &total_entries,
2956 &resume_handle,
2957 &result);
2958 return result;
2961 /* request just one share */
2962 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
2963 pipe_hnd->desthost,
2964 argv[0],
2965 level,
2966 &info,
2967 &result);
2969 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
2970 goto done;
2973 /* construct ctr */
2974 ZERO_STRUCTP(info_ctr);
2976 info_ctr->level = level;
2978 switch (level) {
2979 case 1:
2981 struct srvsvc_NetShareCtr1 *ctr1;
2983 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
2984 W_ERROR_HAVE_NO_MEMORY(ctr1);
2986 ctr1->count = 1;
2987 ctr1->array = info.info1;
2989 info_ctr->ctr.ctr1 = ctr1;
2991 case 2:
2993 struct srvsvc_NetShareCtr2 *ctr2;
2995 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
2996 W_ERROR_HAVE_NO_MEMORY(ctr2);
2998 ctr2->count = 1;
2999 ctr2->array = info.info2;
3001 info_ctr->ctr.ctr2 = ctr2;
3003 case 502:
3005 struct srvsvc_NetShareCtr502 *ctr502;
3007 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3008 W_ERROR_HAVE_NO_MEMORY(ctr502);
3010 ctr502->count = 1;
3011 ctr502->array = info.info502;
3013 info_ctr->ctr.ctr502 = ctr502;
3015 } /* switch */
3016 done:
3017 return result;
3020 /***
3021 * 'net rpc share list' entrypoint.
3022 * @param argc Standard main() style argc.
3023 * @param argv Standard main() style argv. Initial components are already
3024 * stripped.
3026 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3028 NET_API_STATUS status;
3029 struct SHARE_INFO_1 *i1 = NULL;
3030 uint32_t entries_read = 0;
3031 uint32_t total_entries = 0;
3032 uint32_t resume_handle = 0;
3033 uint32_t i, level = 1;
3035 if (c->display_usage) {
3036 d_printf(_("Usage\n"
3037 "net rpc share list\n"
3038 " List shares on remote server\n"));
3039 return 0;
3042 status = NetShareEnum(c->opt_host,
3043 level,
3044 (uint8_t **)(void *)&i1,
3045 (uint32_t)-1,
3046 &entries_read,
3047 &total_entries,
3048 &resume_handle);
3049 if (status != 0) {
3050 goto done;
3053 /* Display results */
3055 if (c->opt_long_list_entries) {
3056 d_printf(_(
3057 "\nEnumerating shared resources (exports) on remote server:\n\n"
3058 "\nShare name Type Description\n"
3059 "---------- ---- -----------\n"));
3061 for (i = 0; i < entries_read; i++)
3062 display_share_info_1(c, &i1[i]);
3063 done:
3064 return status;
3067 static bool check_share_availability(struct cli_state *cli, const char *netname)
3069 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
3070 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3071 return false;
3074 if (!cli_tdis(cli))
3075 return false;
3077 return true;
3080 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3081 const char *netname, uint32 type)
3083 /* only support disk shares */
3084 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3085 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3086 type);
3087 return false;
3090 /* skip builtin shares */
3091 /* FIXME: should print$ be added too ? */
3092 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3093 strequal(netname,"global"))
3094 return false;
3096 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3097 printf(_("excluding [%s]\n"), netname);
3098 return false;
3101 return check_share_availability(cli, netname);
3105 * Migrate shares from a remote RPC server to the local RPC server.
3107 * All parameters are provided by the run_rpc_command function, except for
3108 * argc, argv which are passed through.
3110 * @param domain_sid The domain sid acquired from the remote server.
3111 * @param cli A cli_state connected to the server.
3112 * @param mem_ctx Talloc context, destroyed on completion of the function.
3113 * @param argc Standard main() style argc.
3114 * @param argv Standard main() style argv. Initial components are already
3115 * stripped.
3117 * @return Normal NTSTATUS return.
3120 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3121 const DOM_SID *domain_sid,
3122 const char *domain_name,
3123 struct cli_state *cli,
3124 struct rpc_pipe_client *pipe_hnd,
3125 TALLOC_CTX *mem_ctx,
3126 int argc,
3127 const char **argv)
3129 WERROR result;
3130 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3131 struct srvsvc_NetShareInfoCtr ctr_src;
3132 uint32 i;
3133 struct rpc_pipe_client *srvsvc_pipe = NULL;
3134 struct cli_state *cli_dst = NULL;
3135 uint32 level = 502; /* includes secdesc */
3136 uint32_t parm_error = 0;
3138 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3139 &ctr_src);
3140 if (!W_ERROR_IS_OK(result))
3141 goto done;
3143 /* connect destination PI_SRVSVC */
3144 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3145 &ndr_table_srvsvc.syntax_id);
3146 if (!NT_STATUS_IS_OK(nt_status))
3147 return nt_status;
3150 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3152 union srvsvc_NetShareInfo info;
3153 struct srvsvc_NetShareInfo502 info502 =
3154 ctr_src.ctr.ctr502->array[i];
3156 /* reset error-code */
3157 nt_status = NT_STATUS_UNSUCCESSFUL;
3159 if (!check_share_sanity(c, cli, info502.name, info502.type))
3160 continue;
3162 /* finally add the share on the dst server */
3164 printf(_("migrating: [%s], path: %s, comment: %s, without "
3165 "share-ACLs\n"),
3166 info502.name, info502.path, info502.comment);
3168 info.info502 = &info502;
3170 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3171 srvsvc_pipe->desthost,
3172 502,
3173 &info,
3174 &parm_error,
3175 &result);
3177 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3178 printf(_(" [%s] does already exist\n"),
3179 info502.name);
3180 continue;
3183 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3184 printf(_("cannot add share: %s\n"), win_errstr(result));
3185 goto done;
3190 nt_status = NT_STATUS_OK;
3192 done:
3193 if (cli_dst) {
3194 cli_shutdown(cli_dst);
3197 return nt_status;
3202 * Migrate shares from a RPC server to another.
3204 * @param argc Standard main() style argc.
3205 * @param argv Standard main() style argv. Initial components are already
3206 * stripped.
3208 * @return A shell status integer (0 for success).
3210 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3211 const char **argv)
3213 if (c->display_usage) {
3214 d_printf(_("Usage:\n"
3215 "net rpc share migrate shares\n"
3216 " Migrate shares to local server\n"));
3217 return 0;
3220 if (!c->opt_host) {
3221 printf(_("no server to migrate\n"));
3222 return -1;
3225 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3226 rpc_share_migrate_shares_internals,
3227 argc, argv);
3231 * Copy a file/dir
3233 * @param f file_info
3234 * @param mask current search mask
3235 * @param state arg-pointer
3238 static void copy_fn(const char *mnt, file_info *f,
3239 const char *mask, void *state)
3241 static NTSTATUS nt_status;
3242 static struct copy_clistate *local_state;
3243 static fstring filename, new_mask;
3244 fstring dir;
3245 char *old_dir;
3246 struct net_context *c;
3248 local_state = (struct copy_clistate *)state;
3249 nt_status = NT_STATUS_UNSUCCESSFUL;
3251 c = local_state->c;
3253 if (strequal(f->name, ".") || strequal(f->name, ".."))
3254 return;
3256 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3258 /* DIRECTORY */
3259 if (f->mode & aDIR) {
3261 DEBUG(3,("got dir: %s\n", f->name));
3263 fstrcpy(dir, local_state->cwd);
3264 fstrcat(dir, "\\");
3265 fstrcat(dir, f->name);
3267 switch (net_mode_share)
3269 case NET_MODE_SHARE_MIGRATE:
3270 /* create that directory */
3271 nt_status = net_copy_file(c, local_state->mem_ctx,
3272 local_state->cli_share_src,
3273 local_state->cli_share_dst,
3274 dir, dir,
3275 c->opt_acls? true : false,
3276 c->opt_attrs? true : false,
3277 c->opt_timestamps? true:false,
3278 false);
3279 break;
3280 default:
3281 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3282 return;
3285 if (!NT_STATUS_IS_OK(nt_status))
3286 printf(_("could not handle dir %s: %s\n"),
3287 dir, nt_errstr(nt_status));
3289 /* search below that directory */
3290 fstrcpy(new_mask, dir);
3291 fstrcat(new_mask, "\\*");
3293 old_dir = local_state->cwd;
3294 local_state->cwd = dir;
3295 if (!sync_files(local_state, new_mask))
3296 printf(_("could not handle files\n"));
3297 local_state->cwd = old_dir;
3299 return;
3303 /* FILE */
3304 fstrcpy(filename, local_state->cwd);
3305 fstrcat(filename, "\\");
3306 fstrcat(filename, f->name);
3308 DEBUG(3,("got file: %s\n", filename));
3310 switch (net_mode_share)
3312 case NET_MODE_SHARE_MIGRATE:
3313 nt_status = net_copy_file(c, local_state->mem_ctx,
3314 local_state->cli_share_src,
3315 local_state->cli_share_dst,
3316 filename, filename,
3317 c->opt_acls? true : false,
3318 c->opt_attrs? true : false,
3319 c->opt_timestamps? true: false,
3320 true);
3321 break;
3322 default:
3323 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3324 net_mode_share);
3325 return;
3328 if (!NT_STATUS_IS_OK(nt_status))
3329 printf(_("could not handle file %s: %s\n"),
3330 filename, nt_errstr(nt_status));
3335 * sync files, can be called recursivly to list files
3336 * and then call copy_fn for each file
3338 * @param cp_clistate pointer to the copy_clistate we work with
3339 * @param mask the current search mask
3341 * @return Boolean result
3343 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3345 struct cli_state *targetcli;
3346 char *targetpath = NULL;
3348 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3350 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3351 mask, &targetcli, &targetpath ) ) {
3352 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3353 "%s\n"),
3354 mask, cli_errstr(cp_clistate->cli_share_src));
3355 return false;
3358 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3359 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3360 mask, cli_errstr(targetcli));
3361 return false;
3364 return true;
3369 * Set the top level directory permissions before we do any further copies.
3370 * Should set up ACL inheritance.
3373 bool copy_top_level_perms(struct net_context *c,
3374 struct copy_clistate *cp_clistate,
3375 const char *sharename)
3377 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3379 switch (net_mode_share) {
3380 case NET_MODE_SHARE_MIGRATE:
3381 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3382 nt_status = net_copy_fileattr(c,
3383 cp_clistate->mem_ctx,
3384 cp_clistate->cli_share_src,
3385 cp_clistate->cli_share_dst,
3386 "\\", "\\",
3387 c->opt_acls? true : false,
3388 c->opt_attrs? true : false,
3389 c->opt_timestamps? true: false,
3390 false);
3391 break;
3392 default:
3393 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3394 break;
3397 if (!NT_STATUS_IS_OK(nt_status)) {
3398 printf(_("Could handle directory attributes for top level "
3399 "directory of share %s. Error %s\n"),
3400 sharename, nt_errstr(nt_status));
3401 return false;
3404 return true;
3408 * Sync all files inside a remote share to another share (over smb).
3410 * All parameters are provided by the run_rpc_command function, except for
3411 * argc, argv which are passed through.
3413 * @param domain_sid The domain sid acquired from the remote server.
3414 * @param cli A cli_state connected to the server.
3415 * @param mem_ctx Talloc context, destroyed on completion of the function.
3416 * @param argc Standard main() style argc.
3417 * @param argv Standard main() style argv. Initial components are already
3418 * stripped.
3420 * @return Normal NTSTATUS return.
3423 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3424 const DOM_SID *domain_sid,
3425 const char *domain_name,
3426 struct cli_state *cli,
3427 struct rpc_pipe_client *pipe_hnd,
3428 TALLOC_CTX *mem_ctx,
3429 int argc,
3430 const char **argv)
3432 WERROR result;
3433 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3434 struct srvsvc_NetShareInfoCtr ctr_src;
3435 uint32 i;
3436 uint32 level = 502;
3437 struct copy_clistate cp_clistate;
3438 bool got_src_share = false;
3439 bool got_dst_share = false;
3440 const char *mask = "\\*";
3441 char *dst = NULL;
3443 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3444 if (dst == NULL) {
3445 nt_status = NT_STATUS_NO_MEMORY;
3446 goto done;
3449 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3450 &ctr_src);
3452 if (!W_ERROR_IS_OK(result))
3453 goto done;
3455 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3457 struct srvsvc_NetShareInfo502 info502 =
3458 ctr_src.ctr.ctr502->array[i];
3460 if (!check_share_sanity(c, cli, info502.name, info502.type))
3461 continue;
3463 /* one might not want to mirror whole discs :) */
3464 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3465 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3466 info502.name);
3467 continue;
3470 switch (net_mode_share)
3472 case NET_MODE_SHARE_MIGRATE:
3473 printf("syncing");
3474 break;
3475 default:
3476 d_fprintf(stderr, _("Unsupported mode %d\n"),
3477 net_mode_share);
3478 break;
3480 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3481 "Attributes %s\n"),
3482 info502.name,
3483 c->opt_acls ? _("including") : _("without"),
3484 c->opt_attrs ? _("including") : _("without"),
3485 c->opt_timestamps ? _("(preserving timestamps)") : "");
3487 cp_clistate.mem_ctx = mem_ctx;
3488 cp_clistate.cli_share_src = NULL;
3489 cp_clistate.cli_share_dst = NULL;
3490 cp_clistate.cwd = NULL;
3491 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3492 cp_clistate.c = c;
3494 /* open share source */
3495 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3496 &cli->dest_ss, cli->desthost,
3497 info502.name, "A:");
3498 if (!NT_STATUS_IS_OK(nt_status))
3499 goto done;
3501 got_src_share = true;
3503 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3504 /* open share destination */
3505 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3506 NULL, dst, info502.name, "A:");
3507 if (!NT_STATUS_IS_OK(nt_status))
3508 goto done;
3510 got_dst_share = true;
3513 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3514 d_fprintf(stderr, _("Could not handle the top level "
3515 "directory permissions for the "
3516 "share: %s\n"), info502.name);
3517 nt_status = NT_STATUS_UNSUCCESSFUL;
3518 goto done;
3521 if (!sync_files(&cp_clistate, mask)) {
3522 d_fprintf(stderr, _("could not handle files for share: "
3523 "%s\n"), info502.name);
3524 nt_status = NT_STATUS_UNSUCCESSFUL;
3525 goto done;
3529 nt_status = NT_STATUS_OK;
3531 done:
3533 if (got_src_share)
3534 cli_shutdown(cp_clistate.cli_share_src);
3536 if (got_dst_share)
3537 cli_shutdown(cp_clistate.cli_share_dst);
3539 SAFE_FREE(dst);
3540 return nt_status;
3544 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3546 if (c->display_usage) {
3547 d_printf(_("Usage:\n"
3548 "net share migrate files\n"
3549 " Migrate files to local server\n"));
3550 return 0;
3553 if (!c->opt_host) {
3554 d_printf(_("no server to migrate\n"));
3555 return -1;
3558 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3559 rpc_share_migrate_files_internals,
3560 argc, argv);
3564 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3566 * All parameters are provided by the run_rpc_command function, except for
3567 * argc, argv which are passed through.
3569 * @param domain_sid The domain sid acquired from the remote server.
3570 * @param cli A cli_state connected to the server.
3571 * @param mem_ctx Talloc context, destroyed on completion of the function.
3572 * @param argc Standard main() style argc.
3573 * @param argv Standard main() style argv. Initial components are already
3574 * stripped.
3576 * @return Normal NTSTATUS return.
3579 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3580 const DOM_SID *domain_sid,
3581 const char *domain_name,
3582 struct cli_state *cli,
3583 struct rpc_pipe_client *pipe_hnd,
3584 TALLOC_CTX *mem_ctx,
3585 int argc,
3586 const char **argv)
3588 WERROR result;
3589 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3590 struct srvsvc_NetShareInfoCtr ctr_src;
3591 union srvsvc_NetShareInfo info;
3592 uint32 i;
3593 struct rpc_pipe_client *srvsvc_pipe = NULL;
3594 struct cli_state *cli_dst = NULL;
3595 uint32 level = 502; /* includes secdesc */
3596 uint32_t parm_error = 0;
3598 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3599 &ctr_src);
3601 if (!W_ERROR_IS_OK(result))
3602 goto done;
3604 /* connect destination PI_SRVSVC */
3605 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3606 &ndr_table_srvsvc.syntax_id);
3607 if (!NT_STATUS_IS_OK(nt_status))
3608 return nt_status;
3611 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3613 struct srvsvc_NetShareInfo502 info502 =
3614 ctr_src.ctr.ctr502->array[i];
3616 /* reset error-code */
3617 nt_status = NT_STATUS_UNSUCCESSFUL;
3619 if (!check_share_sanity(c, cli, info502.name, info502.type))
3620 continue;
3622 printf(_("migrating: [%s], path: %s, comment: %s, including "
3623 "share-ACLs\n"),
3624 info502.name, info502.path, info502.comment);
3626 if (c->opt_verbose)
3627 display_sec_desc(info502.sd_buf.sd);
3629 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3630 info.info502 = &info502;
3632 /* finally modify the share on the dst server */
3633 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3634 srvsvc_pipe->desthost,
3635 info502.name,
3636 level,
3637 &info,
3638 &parm_error,
3639 &result);
3640 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3641 printf(_("cannot set share-acl: %s\n"),
3642 win_errstr(result));
3643 goto done;
3648 nt_status = NT_STATUS_OK;
3650 done:
3651 if (cli_dst) {
3652 cli_shutdown(cli_dst);
3655 return nt_status;
3660 * Migrate share-acls from a RPC server to another.
3662 * @param argc Standard main() style argc.
3663 * @param argv Standard main() style argv. Initial components are already
3664 * stripped.
3666 * @return A shell status integer (0 for success).
3668 static int rpc_share_migrate_security(struct net_context *c, int argc,
3669 const char **argv)
3671 if (c->display_usage) {
3672 d_printf(_("Usage:\n"
3673 "net rpc share migrate security\n"
3674 " Migrate share-acls to local server\n"));
3675 return 0;
3678 if (!c->opt_host) {
3679 d_printf(_("no server to migrate\n"));
3680 return -1;
3683 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3684 rpc_share_migrate_security_internals,
3685 argc, argv);
3689 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3690 * from one server to another.
3692 * @param argc Standard main() style argc.
3693 * @param argv Standard main() style argv. Initial components are already
3694 * stripped.
3696 * @return A shell status integer (0 for success).
3699 static int rpc_share_migrate_all(struct net_context *c, int argc,
3700 const char **argv)
3702 int ret;
3704 if (c->display_usage) {
3705 d_printf(_("Usage:\n"
3706 "net rpc share migrate all\n"
3707 " Migrates shares including all share settings\n"));
3708 return 0;
3711 if (!c->opt_host) {
3712 d_printf(_("no server to migrate\n"));
3713 return -1;
3716 /* order is important. we don't want to be locked out by the share-acl
3717 * before copying files - gd */
3719 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3720 rpc_share_migrate_shares_internals, argc, argv);
3721 if (ret)
3722 return ret;
3724 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3725 rpc_share_migrate_files_internals, argc, argv);
3726 if (ret)
3727 return ret;
3729 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3730 rpc_share_migrate_security_internals, argc,
3731 argv);
3736 * 'net rpc share migrate' entrypoint.
3737 * @param argc Standard main() style argc.
3738 * @param argv Standard main() style argv. Initial components are already
3739 * stripped.
3741 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3744 struct functable func[] = {
3746 "all",
3747 rpc_share_migrate_all,
3748 NET_TRANSPORT_RPC,
3749 N_("Migrate shares from remote to local server"),
3750 N_("net rpc share migrate all\n"
3751 " Migrate shares from remote to local server")
3754 "files",
3755 rpc_share_migrate_files,
3756 NET_TRANSPORT_RPC,
3757 N_("Migrate files from remote to local server"),
3758 N_("net rpc share migrate files\n"
3759 " Migrate files from remote to local server")
3762 "security",
3763 rpc_share_migrate_security,
3764 NET_TRANSPORT_RPC,
3765 N_("Migrate share-ACLs from remote to local server"),
3766 N_("net rpc share migrate security\n"
3767 " Migrate share-ACLs from remote to local server")
3770 "shares",
3771 rpc_share_migrate_shares,
3772 NET_TRANSPORT_RPC,
3773 N_("Migrate shares from remote to local server"),
3774 N_("net rpc share migrate shares\n"
3775 " Migrate shares from remote to local server")
3777 {NULL, NULL, 0, NULL, NULL}
3780 net_mode_share = NET_MODE_SHARE_MIGRATE;
3782 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3785 struct full_alias {
3786 DOM_SID sid;
3787 uint32 num_members;
3788 DOM_SID *members;
3791 static int num_server_aliases;
3792 static struct full_alias *server_aliases;
3795 * Add an alias to the static list.
3797 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3799 if (server_aliases == NULL)
3800 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3802 server_aliases[num_server_aliases] = *alias;
3803 num_server_aliases += 1;
3807 * For a specific domain on the server, fetch all the aliases
3808 * and their members. Add all of them to the server_aliases.
3811 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3812 TALLOC_CTX *mem_ctx,
3813 struct policy_handle *connect_pol,
3814 const DOM_SID *domain_sid)
3816 uint32 start_idx, max_entries, num_entries, i;
3817 struct samr_SamArray *groups = NULL;
3818 NTSTATUS result;
3819 struct policy_handle domain_pol;
3821 /* Get domain policy handle */
3823 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3824 connect_pol,
3825 MAXIMUM_ALLOWED_ACCESS,
3826 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3827 &domain_pol);
3828 if (!NT_STATUS_IS_OK(result))
3829 return result;
3831 start_idx = 0;
3832 max_entries = 250;
3834 do {
3835 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3836 &domain_pol,
3837 &start_idx,
3838 &groups,
3839 max_entries,
3840 &num_entries);
3841 for (i = 0; i < num_entries; i++) {
3843 struct policy_handle alias_pol;
3844 struct full_alias alias;
3845 struct lsa_SidArray sid_array;
3846 int j;
3848 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3849 &domain_pol,
3850 MAXIMUM_ALLOWED_ACCESS,
3851 groups->entries[i].idx,
3852 &alias_pol);
3853 if (!NT_STATUS_IS_OK(result))
3854 goto done;
3856 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3857 &alias_pol,
3858 &sid_array);
3859 if (!NT_STATUS_IS_OK(result))
3860 goto done;
3862 alias.num_members = sid_array.num_sids;
3864 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3865 if (!NT_STATUS_IS_OK(result))
3866 goto done;
3868 alias.members = NULL;
3870 if (alias.num_members > 0) {
3871 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3873 for (j = 0; j < alias.num_members; j++)
3874 sid_copy(&alias.members[j],
3875 sid_array.sids[j].sid);
3878 sid_copy(&alias.sid, domain_sid);
3879 sid_append_rid(&alias.sid, groups->entries[i].idx);
3881 push_alias(mem_ctx, &alias);
3883 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3885 result = NT_STATUS_OK;
3887 done:
3888 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3890 return result;
3894 * Dump server_aliases as names for debugging purposes.
3897 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
3898 const DOM_SID *domain_sid,
3899 const char *domain_name,
3900 struct cli_state *cli,
3901 struct rpc_pipe_client *pipe_hnd,
3902 TALLOC_CTX *mem_ctx,
3903 int argc,
3904 const char **argv)
3906 int i;
3907 NTSTATUS result;
3908 struct policy_handle lsa_pol;
3910 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
3911 SEC_FLAG_MAXIMUM_ALLOWED,
3912 &lsa_pol);
3913 if (!NT_STATUS_IS_OK(result))
3914 return result;
3916 for (i=0; i<num_server_aliases; i++) {
3917 char **names;
3918 char **domains;
3919 enum lsa_SidType *types;
3920 int j;
3922 struct full_alias *alias = &server_aliases[i];
3924 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3925 &alias->sid,
3926 &domains, &names, &types);
3927 if (!NT_STATUS_IS_OK(result))
3928 continue;
3930 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3932 if (alias->num_members == 0) {
3933 DEBUG(1, ("\n"));
3934 continue;
3937 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
3938 alias->num_members,
3939 alias->members,
3940 &domains, &names, &types);
3942 if (!NT_STATUS_IS_OK(result) &&
3943 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3944 continue;
3946 for (j=0; j<alias->num_members; j++)
3947 DEBUG(1, ("%s\\%s (%d); ",
3948 domains[j] ? domains[j] : "*unknown*",
3949 names[j] ? names[j] : "*unknown*",types[j]));
3950 DEBUG(1, ("\n"));
3953 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
3955 return NT_STATUS_OK;
3959 * Fetch a list of all server aliases and their members into
3960 * server_aliases.
3963 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
3964 const DOM_SID *domain_sid,
3965 const char *domain_name,
3966 struct cli_state *cli,
3967 struct rpc_pipe_client *pipe_hnd,
3968 TALLOC_CTX *mem_ctx,
3969 int argc,
3970 const char **argv)
3972 NTSTATUS result;
3973 struct policy_handle connect_pol;
3975 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3976 pipe_hnd->desthost,
3977 MAXIMUM_ALLOWED_ACCESS,
3978 &connect_pol);
3980 if (!NT_STATUS_IS_OK(result))
3981 goto done;
3983 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3984 &global_sid_Builtin);
3986 if (!NT_STATUS_IS_OK(result))
3987 goto done;
3989 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3990 domain_sid);
3992 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
3993 done:
3994 return result;
3997 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
3999 token->num_sids = 4;
4001 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4002 d_fprintf(stderr, _("malloc failed\n"));
4003 token->num_sids = 0;
4004 return;
4007 token->user_sids[0] = *user_sid;
4008 sid_copy(&token->user_sids[1], &global_sid_World);
4009 sid_copy(&token->user_sids[2], &global_sid_Network);
4010 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4013 static void free_user_token(NT_USER_TOKEN *token)
4015 SAFE_FREE(token->user_sids);
4018 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4020 if (is_sid_in_token(token, sid))
4021 return;
4023 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4024 if (!token->user_sids) {
4025 return;
4028 sid_copy(&token->user_sids[token->num_sids], sid);
4030 token->num_sids += 1;
4033 struct user_token {
4034 fstring name;
4035 NT_USER_TOKEN token;
4038 static void dump_user_token(struct user_token *token)
4040 int i;
4042 d_printf("%s\n", token->name);
4044 for (i=0; i<token->token.num_sids; i++) {
4045 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4049 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4051 int i;
4053 for (i=0; i<alias->num_members; i++) {
4054 if (sid_compare(sid, &alias->members[i]) == 0)
4055 return true;
4058 return false;
4061 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4063 int i;
4065 for (i=0; i<num_server_aliases; i++) {
4066 if (is_alias_member(&sid, &server_aliases[i]))
4067 add_sid_to_token(token, &server_aliases[i].sid);
4072 * We got a user token with all the SIDs we can know about without asking the
4073 * server directly. These are the user and domain group sids. All of these can
4074 * be members of aliases. So scan the list of aliases for each of the SIDs and
4075 * add them to the token.
4078 static void collect_alias_memberships(NT_USER_TOKEN *token)
4080 int num_global_sids = token->num_sids;
4081 int i;
4083 for (i=0; i<num_global_sids; i++) {
4084 collect_sid_memberships(token, token->user_sids[i]);
4088 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4090 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4091 enum wbcSidType type;
4092 fstring full_name;
4093 struct wbcDomainSid wsid;
4094 char *sid_str = NULL;
4095 DOM_SID user_sid;
4096 uint32_t num_groups;
4097 gid_t *groups = NULL;
4098 uint32_t i;
4100 fstr_sprintf(full_name, "%s%c%s",
4101 domain, *lp_winbind_separator(), user);
4103 /* First let's find out the user sid */
4105 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4107 if (!WBC_ERROR_IS_OK(wbc_status)) {
4108 DEBUG(1, ("winbind could not find %s: %s\n",
4109 full_name, wbcErrorString(wbc_status)));
4110 return false;
4113 wbc_status = wbcSidToString(&wsid, &sid_str);
4114 if (!WBC_ERROR_IS_OK(wbc_status)) {
4115 return false;
4118 if (type != SID_NAME_USER) {
4119 wbcFreeMemory(sid_str);
4120 DEBUG(1, ("%s is not a user\n", full_name));
4121 return false;
4124 if (!string_to_sid(&user_sid, sid_str)) {
4125 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4126 return false;
4129 wbcFreeMemory(sid_str);
4130 sid_str = NULL;
4132 init_user_token(token, &user_sid);
4134 /* And now the groups winbind knows about */
4136 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4137 if (!WBC_ERROR_IS_OK(wbc_status)) {
4138 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4139 full_name, wbcErrorString(wbc_status)));
4140 return false;
4143 for (i = 0; i < num_groups; i++) {
4144 gid_t gid = groups[i];
4145 DOM_SID sid;
4147 wbc_status = wbcGidToSid(gid, &wsid);
4148 if (!WBC_ERROR_IS_OK(wbc_status)) {
4149 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4150 (unsigned int)gid, wbcErrorString(wbc_status)));
4151 wbcFreeMemory(groups);
4152 return false;
4155 wbc_status = wbcSidToString(&wsid, &sid_str);
4156 if (!WBC_ERROR_IS_OK(wbc_status)) {
4157 wbcFreeMemory(groups);
4158 return false;
4161 DEBUG(3, (" %s\n", sid_str));
4163 string_to_sid(&sid, sid_str);
4164 wbcFreeMemory(sid_str);
4165 sid_str = NULL;
4167 add_sid_to_token(token, &sid);
4169 wbcFreeMemory(groups);
4171 return true;
4175 * Get a list of all user tokens we want to look at
4178 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4179 struct user_token **user_tokens)
4181 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4182 uint32_t i, num_users;
4183 const char **users;
4184 struct user_token *result;
4185 TALLOC_CTX *frame = NULL;
4187 if (lp_winbind_use_default_domain() &&
4188 (c->opt_target_workgroup == NULL)) {
4189 d_fprintf(stderr, _("winbind use default domain = yes set, "
4190 "please specify a workgroup\n"));
4191 return false;
4194 /* Send request to winbind daemon */
4196 wbc_status = wbcListUsers(NULL, &num_users, &users);
4197 if (!WBC_ERROR_IS_OK(wbc_status)) {
4198 DEBUG(1, (_("winbind could not list users: %s\n"),
4199 wbcErrorString(wbc_status)));
4200 return false;
4203 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4205 if (result == NULL) {
4206 DEBUG(1, ("Could not malloc sid array\n"));
4207 wbcFreeMemory(users);
4208 return false;
4211 frame = talloc_stackframe();
4212 for (i=0; i < num_users; i++) {
4213 fstring domain, user;
4214 char *p;
4216 fstrcpy(result[i].name, users[i]);
4218 p = strchr(users[i], *lp_winbind_separator());
4220 DEBUG(3, ("%s\n", users[i]));
4222 if (p == NULL) {
4223 fstrcpy(domain, c->opt_target_workgroup);
4224 fstrcpy(user, users[i]);
4225 } else {
4226 *p++ = '\0';
4227 fstrcpy(domain, users[i]);
4228 strupper_m(domain);
4229 fstrcpy(user, p);
4232 get_user_sids(domain, user, &(result[i].token));
4233 i+=1;
4235 TALLOC_FREE(frame);
4236 wbcFreeMemory(users);
4238 *num_tokens = num_users;
4239 *user_tokens = result;
4241 return true;
4244 static bool get_user_tokens_from_file(FILE *f,
4245 int *num_tokens,
4246 struct user_token **tokens)
4248 struct user_token *token = NULL;
4250 while (!feof(f)) {
4251 fstring line;
4253 if (fgets(line, sizeof(line)-1, f) == NULL) {
4254 return true;
4257 if (line[strlen(line)-1] == '\n')
4258 line[strlen(line)-1] = '\0';
4260 if (line[0] == ' ') {
4261 /* We have a SID */
4263 DOM_SID sid;
4264 if(!string_to_sid(&sid, &line[1])) {
4265 DEBUG(1,("get_user_tokens_from_file: Could "
4266 "not convert sid %s \n",&line[1]));
4267 return false;
4270 if (token == NULL) {
4271 DEBUG(0, ("File does not begin with username"));
4272 return false;
4275 add_sid_to_token(&token->token, &sid);
4276 continue;
4279 /* And a new user... */
4281 *num_tokens += 1;
4282 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4283 if (*tokens == NULL) {
4284 DEBUG(0, ("Could not realloc tokens\n"));
4285 return false;
4288 token = &((*tokens)[*num_tokens-1]);
4290 fstrcpy(token->name, line);
4291 token->token.num_sids = 0;
4292 token->token.user_sids = NULL;
4293 continue;
4296 return false;
4301 * Show the list of all users that have access to a share
4304 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4305 TALLOC_CTX *mem_ctx,
4306 const char *netname,
4307 int num_tokens,
4308 struct user_token *tokens)
4310 uint16_t fnum;
4311 SEC_DESC *share_sd = NULL;
4312 SEC_DESC *root_sd = NULL;
4313 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4314 int i;
4315 union srvsvc_NetShareInfo info;
4316 WERROR result;
4317 NTSTATUS status;
4318 uint16 cnum;
4320 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4321 pipe_hnd->desthost,
4322 netname,
4323 502,
4324 &info,
4325 &result);
4327 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4328 DEBUG(1, ("Coult not query secdesc for share %s\n",
4329 netname));
4330 return;
4333 share_sd = info.info502->sd_buf.sd;
4334 if (share_sd == NULL) {
4335 DEBUG(1, ("Got no secdesc for share %s\n",
4336 netname));
4339 cnum = cli->cnum;
4341 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4342 return;
4345 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4346 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4347 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4350 for (i=0; i<num_tokens; i++) {
4351 uint32 acc_granted;
4353 if (share_sd != NULL) {
4354 status = se_access_check(share_sd, &tokens[i].token,
4355 1, &acc_granted);
4357 if (!NT_STATUS_IS_OK(status)) {
4358 DEBUG(1, ("Could not check share_sd for "
4359 "user %s\n",
4360 tokens[i].name));
4361 continue;
4365 if (root_sd == NULL) {
4366 d_printf(" %s\n", tokens[i].name);
4367 continue;
4370 status = se_access_check(root_sd, &tokens[i].token,
4371 1, &acc_granted);
4372 if (!NT_STATUS_IS_OK(status)) {
4373 DEBUG(1, ("Could not check root_sd for user %s\n",
4374 tokens[i].name));
4375 continue;
4377 d_printf(" %s\n", tokens[i].name);
4380 if (fnum != (uint16_t)-1)
4381 cli_close(cli, fnum);
4382 cli_tdis(cli);
4383 cli->cnum = cnum;
4385 return;
4388 struct share_list {
4389 int num_shares;
4390 char **shares;
4393 static void collect_share(const char *name, uint32 m,
4394 const char *comment, void *state)
4396 struct share_list *share_list = (struct share_list *)state;
4398 if (m != STYPE_DISKTREE)
4399 return;
4401 share_list->num_shares += 1;
4402 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4403 if (!share_list->shares) {
4404 share_list->num_shares = 0;
4405 return;
4407 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4411 * List shares on a remote RPC server, including the security descriptors.
4413 * All parameters are provided by the run_rpc_command function, except for
4414 * argc, argv which are passed through.
4416 * @param domain_sid The domain sid acquired from the remote server.
4417 * @param cli A cli_state connected to the server.
4418 * @param mem_ctx Talloc context, destroyed on completion of the function.
4419 * @param argc Standard main() style argc.
4420 * @param argv Standard main() style argv. Initial components are already
4421 * stripped.
4423 * @return Normal NTSTATUS return.
4426 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4427 const DOM_SID *domain_sid,
4428 const char *domain_name,
4429 struct cli_state *cli,
4430 struct rpc_pipe_client *pipe_hnd,
4431 TALLOC_CTX *mem_ctx,
4432 int argc,
4433 const char **argv)
4435 int ret;
4436 bool r;
4437 uint32 i;
4438 FILE *f;
4440 struct user_token *tokens = NULL;
4441 int num_tokens = 0;
4443 struct share_list share_list;
4445 if (argc == 0) {
4446 f = stdin;
4447 } else {
4448 f = fopen(argv[0], "r");
4451 if (f == NULL) {
4452 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4453 return NT_STATUS_UNSUCCESSFUL;
4456 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4458 if (f != stdin)
4459 fclose(f);
4461 if (!r) {
4462 DEBUG(0, ("Could not read users from file\n"));
4463 return NT_STATUS_UNSUCCESSFUL;
4466 for (i=0; i<num_tokens; i++)
4467 collect_alias_memberships(&tokens[i].token);
4469 share_list.num_shares = 0;
4470 share_list.shares = NULL;
4472 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4474 if (ret == -1) {
4475 DEBUG(0, ("Error returning browse list: %s\n",
4476 cli_errstr(cli)));
4477 goto done;
4480 for (i = 0; i < share_list.num_shares; i++) {
4481 char *netname = share_list.shares[i];
4483 if (netname[strlen(netname)-1] == '$')
4484 continue;
4486 d_printf("%s\n", netname);
4488 show_userlist(pipe_hnd, mem_ctx, netname,
4489 num_tokens, tokens);
4491 done:
4492 for (i=0; i<num_tokens; i++) {
4493 free_user_token(&tokens[i].token);
4495 SAFE_FREE(tokens);
4496 SAFE_FREE(share_list.shares);
4498 return NT_STATUS_OK;
4501 static int rpc_share_allowedusers(struct net_context *c, int argc,
4502 const char **argv)
4504 int result;
4506 if (c->display_usage) {
4507 d_printf(_("Usage:\n"
4508 "net rpc share allowedusers\n"
4509 " List allowed users\n"));
4510 return 0;
4513 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4514 rpc_aliaslist_internals,
4515 argc, argv);
4516 if (result != 0)
4517 return result;
4519 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4520 rpc_aliaslist_dump,
4521 argc, argv);
4522 if (result != 0)
4523 return result;
4525 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4526 rpc_share_allowedusers_internals,
4527 argc, argv);
4530 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4532 int num_tokens = 0;
4533 struct user_token *tokens = NULL;
4534 int i;
4536 if (argc != 0) {
4537 net_usersidlist_usage(c, argc, argv);
4538 return 0;
4541 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4542 DEBUG(0, ("Could not get the user/sid list\n"));
4543 return 0;
4546 for (i=0; i<num_tokens; i++) {
4547 dump_user_token(&tokens[i]);
4548 free_user_token(&tokens[i].token);
4551 SAFE_FREE(tokens);
4552 return 1;
4555 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4557 d_printf(_("net usersidlist\n"
4558 "\tprints out a list of all users the running winbind knows\n"
4559 "\tabout, together with all their SIDs. This is used as\n"
4560 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
4562 net_common_flags_usage(c, argc, argv);
4563 return -1;
4567 * 'net rpc share' entrypoint.
4568 * @param argc Standard main() style argc.
4569 * @param argv Standard main() style argv. Initial components are already
4570 * stripped.
4573 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4575 NET_API_STATUS status;
4577 struct functable func[] = {
4579 "add",
4580 rpc_share_add,
4581 NET_TRANSPORT_RPC,
4582 N_("Add share"),
4583 N_("net rpc share add\n"
4584 " Add share")
4587 "delete",
4588 rpc_share_delete,
4589 NET_TRANSPORT_RPC,
4590 N_("Remove share"),
4591 N_("net rpc share delete\n"
4592 " Remove share")
4595 "allowedusers",
4596 rpc_share_allowedusers,
4597 NET_TRANSPORT_RPC,
4598 N_("Modify allowed users"),
4599 N_("net rpc share allowedusers\n"
4600 " Modify allowed users")
4603 "migrate",
4604 rpc_share_migrate,
4605 NET_TRANSPORT_RPC,
4606 N_("Migrate share to local server"),
4607 N_("net rpc share migrate\n"
4608 " Migrate share to local server")
4611 "list",
4612 rpc_share_list,
4613 NET_TRANSPORT_RPC,
4614 N_("List shares"),
4615 N_("net rpc share list\n"
4616 " List shares")
4618 {NULL, NULL, 0, NULL, NULL}
4621 status = libnetapi_init(&c->netapi_ctx);
4622 if (status != 0) {
4623 return -1;
4625 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4626 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4627 if (c->opt_kerberos) {
4628 libnetapi_set_use_kerberos(c->netapi_ctx);
4631 if (argc == 0) {
4632 if (c->display_usage) {
4633 d_printf(_("Usage:\n"
4634 "net rpc share\n"
4635 " List shares\n"
4636 " Alias for net rpc share list\n"));
4637 net_display_usage_from_functable(func);
4638 return 0;
4641 return rpc_share_list(c, argc, argv);
4644 return net_run_function(c, argc, argv, "net rpc share", func);
4647 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4648 TALLOC_CTX *mem_ctx,
4649 struct rpc_sh_ctx *ctx,
4650 struct rpc_pipe_client *pipe_hnd,
4651 int argc, const char **argv)
4654 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4657 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4658 TALLOC_CTX *mem_ctx,
4659 struct rpc_sh_ctx *ctx,
4660 struct rpc_pipe_client *pipe_hnd,
4661 int argc, const char **argv)
4663 NET_API_STATUS status;
4664 uint32_t parm_err = 0;
4665 struct SHARE_INFO_2 i2;
4667 if ((argc < 2) || (argc > 3)) {
4668 d_fprintf(stderr, _("usage: %s <share> <path> [comment]\n"),
4669 ctx->whoami);
4670 return NT_STATUS_INVALID_PARAMETER;
4673 i2.shi2_netname = argv[0];
4674 i2.shi2_type = STYPE_DISKTREE;
4675 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4676 i2.shi2_permissions = 0;
4677 i2.shi2_max_uses = 0;
4678 i2.shi2_current_uses = 0;
4679 i2.shi2_path = argv[1];
4680 i2.shi2_passwd = NULL;
4682 status = NetShareAdd(pipe_hnd->desthost,
4684 (uint8_t *)&i2,
4685 &parm_err);
4687 return werror_to_ntstatus(W_ERROR(status));
4690 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4691 TALLOC_CTX *mem_ctx,
4692 struct rpc_sh_ctx *ctx,
4693 struct rpc_pipe_client *pipe_hnd,
4694 int argc, const char **argv)
4696 if (argc != 1) {
4697 d_fprintf(stderr, _("usage: %s <share>\n"), ctx->whoami);
4698 return NT_STATUS_INVALID_PARAMETER;
4701 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4704 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4705 TALLOC_CTX *mem_ctx,
4706 struct rpc_sh_ctx *ctx,
4707 struct rpc_pipe_client *pipe_hnd,
4708 int argc, const char **argv)
4710 union srvsvc_NetShareInfo info;
4711 WERROR result;
4712 NTSTATUS status;
4714 if (argc != 1) {
4715 d_fprintf(stderr, _("usage: %s <share>\n"), ctx->whoami);
4716 return NT_STATUS_INVALID_PARAMETER;
4719 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4720 pipe_hnd->desthost,
4721 argv[0],
4723 &info,
4724 &result);
4725 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4726 goto done;
4729 d_printf(_("Name: %s\n"), info.info2->name);
4730 d_printf(_("Comment: %s\n"), info.info2->comment);
4731 d_printf(_("Path: %s\n"), info.info2->path);
4732 d_printf(_("Password: %s\n"), info.info2->password);
4734 done:
4735 return werror_to_ntstatus(result);
4738 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4739 struct rpc_sh_ctx *ctx)
4741 static struct rpc_sh_cmd cmds[] = {
4743 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4744 N_("List available shares") },
4746 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4747 N_("Add a share") },
4749 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4750 N_("Delete a share") },
4752 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4753 N_("Get information about a share") },
4755 { NULL, NULL, 0, NULL, NULL }
4758 return cmds;
4761 /****************************************************************************/
4763 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4765 return net_file_usage(c, argc, argv);
4769 * Close a file on a remote RPC server.
4771 * @param argc Standard main() style argc.
4772 * @param argv Standard main() style argv. Initial components are already
4773 * stripped.
4775 * @return A shell status integer (0 for success).
4777 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4779 if (argc < 1 || c->display_usage) {
4780 return rpc_file_usage(c, argc, argv);
4783 return NetFileClose(c->opt_host, atoi(argv[0]));
4787 * Formatted print of open file info
4789 * @param r struct FILE_INFO_3 contents
4792 static void display_file_info_3(struct FILE_INFO_3 *r)
4794 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4795 r->fi3_id, r->fi3_username, r->fi3_permissions,
4796 r->fi3_num_locks, r->fi3_pathname);
4800 * List files for a user on a remote RPC server.
4802 * @param argc Standard main() style argc.
4803 * @param argv Standard main() style argv. Initial components are already
4804 * stripped.
4806 * @return A shell status integer (0 for success)..
4809 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4811 NET_API_STATUS status;
4812 uint32 preferred_len = 0xffffffff, i;
4813 const char *username=NULL;
4814 uint32_t total_entries = 0;
4815 uint32_t entries_read = 0;
4816 uint32_t resume_handle = 0;
4817 struct FILE_INFO_3 *i3 = NULL;
4819 if (c->display_usage) {
4820 return rpc_file_usage(c, argc, argv);
4823 /* if argc > 0, must be user command */
4824 if (argc > 0) {
4825 username = smb_xstrdup(argv[0]);
4828 status = NetFileEnum(c->opt_host,
4829 NULL,
4830 username,
4832 (uint8_t **)(void *)&i3,
4833 preferred_len,
4834 &entries_read,
4835 &total_entries,
4836 &resume_handle);
4838 if (status != 0) {
4839 goto done;
4842 /* Display results */
4844 d_printf(_(
4845 "\nEnumerating open files on remote server:\n\n"
4846 "\nFileId Opened by Perms Locks Path"
4847 "\n------ --------- ----- ----- ---- \n"));
4848 for (i = 0; i < entries_read; i++) {
4849 display_file_info_3(&i3[i]);
4851 done:
4852 return status;
4856 * 'net rpc file' entrypoint.
4857 * @param argc Standard main() style argc.
4858 * @param argv Standard main() style argv. Initial components are already
4859 * stripped.
4862 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4864 NET_API_STATUS status;
4866 struct functable func[] = {
4868 "close",
4869 rpc_file_close,
4870 NET_TRANSPORT_RPC,
4871 N_("Close opened file"),
4872 N_("net rpc file close\n"
4873 " Close opened file")
4876 "user",
4877 rpc_file_user,
4878 NET_TRANSPORT_RPC,
4879 N_("List files opened by user"),
4880 N_("net rpc file user\n"
4881 " List files opened by user")
4883 #if 0
4885 "info",
4886 rpc_file_info,
4887 NET_TRANSPORT_RPC,
4888 N_("Display information about opened file"),
4889 N_("net rpc file info\n"
4890 " Display information about opened file")
4892 #endif
4893 {NULL, NULL, 0, NULL, NULL}
4896 status = libnetapi_init(&c->netapi_ctx);
4897 if (status != 0) {
4898 return -1;
4900 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4901 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4902 if (c->opt_kerberos) {
4903 libnetapi_set_use_kerberos(c->netapi_ctx);
4906 if (argc == 0) {
4907 if (c->display_usage) {
4908 d_printf(_("Usage:\n"));
4909 d_printf(_("net rpc file\n"
4910 " List opened files\n"));
4911 net_display_usage_from_functable(func);
4912 return 0;
4915 return rpc_file_user(c, argc, argv);
4918 return net_run_function(c, argc, argv, "net rpc file", func);
4922 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
4924 * All parameters are provided by the run_rpc_command function, except for
4925 * argc, argv which are passed through.
4927 * @param c A net_context structure.
4928 * @param domain_sid The domain sid acquired from the remote server.
4929 * @param cli A cli_state connected to the server.
4930 * @param mem_ctx Talloc context, destroyed on completion of the function.
4931 * @param argc Standard main() style argc.
4932 * @param argv Standard main() style argv. Initial components are already
4933 * stripped.
4935 * @return Normal NTSTATUS return.
4938 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
4939 const DOM_SID *domain_sid,
4940 const char *domain_name,
4941 struct cli_state *cli,
4942 struct rpc_pipe_client *pipe_hnd,
4943 TALLOC_CTX *mem_ctx,
4944 int argc,
4945 const char **argv)
4947 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4949 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
4951 if (NT_STATUS_IS_OK(result)) {
4952 d_printf(_("\nShutdown successfully aborted\n"));
4953 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4954 } else
4955 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4957 return result;
4961 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
4963 * All parameters are provided by the run_rpc_command function, except for
4964 * argc, argv which are passed through.
4966 * @param c A net_context structure.
4967 * @param domain_sid The domain sid acquired from the remote server.
4968 * @param cli A cli_state connected to the server.
4969 * @param mem_ctx Talloc context, destroyed on completion of the function.
4970 * @param argc Standard main() style argc.
4971 * @param argv Standard main() style argv. Initial components are already
4972 * stripped.
4974 * @return Normal NTSTATUS return.
4977 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
4978 const DOM_SID *domain_sid,
4979 const char *domain_name,
4980 struct cli_state *cli,
4981 struct rpc_pipe_client *pipe_hnd,
4982 TALLOC_CTX *mem_ctx,
4983 int argc,
4984 const char **argv)
4986 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4988 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
4990 if (NT_STATUS_IS_OK(result)) {
4991 d_printf(_("\nShutdown successfully aborted\n"));
4992 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
4993 } else
4994 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
4996 return result;
5000 * ABORT the shutdown of a remote RPC server.
5002 * @param argc Standard main() style argc.
5003 * @param argv Standard main() style argv. Initial components are already
5004 * stripped.
5006 * @return A shell status integer (0 for success).
5009 static int rpc_shutdown_abort(struct net_context *c, int argc,
5010 const char **argv)
5012 int rc = -1;
5014 if (c->display_usage) {
5015 d_printf(_("Usage:\n"
5016 "net rpc abortshutdown\n"
5017 " Abort a scheduled shutdown\n"));
5018 return 0;
5021 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5022 rpc_shutdown_abort_internals, argc, argv);
5024 if (rc == 0)
5025 return rc;
5027 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5029 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5030 rpc_reg_shutdown_abort_internals,
5031 argc, argv);
5035 * Shut down a remote RPC Server via initshutdown pipe.
5037 * All parameters are provided by the run_rpc_command function, except for
5038 * argc, argv which are passed through.
5040 * @param c A net_context structure.
5041 * @param domain_sid The domain sid acquired from the remote server.
5042 * @param cli A cli_state connected to the server.
5043 * @param mem_ctx Talloc context, destroyed on completion of the function.
5044 * @param argc Standard main() style argc.
5045 * @param argv Standard main() style argv. Initial components are already
5046 * stripped.
5048 * @return Normal NTSTATUS return.
5051 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5052 const DOM_SID *domain_sid,
5053 const char *domain_name,
5054 struct cli_state *cli,
5055 struct rpc_pipe_client *pipe_hnd,
5056 TALLOC_CTX *mem_ctx,
5057 int argc,
5058 const char **argv)
5060 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5061 const char *msg = N_("This machine will be shutdown shortly");
5062 uint32 timeout = 20;
5063 struct lsa_StringLarge msg_string;
5065 if (c->opt_comment) {
5066 msg = c->opt_comment;
5068 if (c->opt_timeout) {
5069 timeout = c->opt_timeout;
5072 msg_string.string = msg;
5074 /* create an entry */
5075 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5076 &msg_string, timeout, c->opt_force, c->opt_reboot,
5077 NULL);
5079 if (NT_STATUS_IS_OK(result)) {
5080 d_printf(_("\nShutdown of remote machine succeeded\n"));
5081 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5082 } else {
5083 DEBUG(1,("Shutdown of remote machine failed!\n"));
5085 return result;
5089 * Shut down a remote RPC Server via winreg pipe.
5091 * All parameters are provided by the run_rpc_command function, except for
5092 * argc, argv which are passed through.
5094 * @param c A net_context structure.
5095 * @param domain_sid The domain sid acquired from the remote server.
5096 * @param cli A cli_state connected to the server.
5097 * @param mem_ctx Talloc context, destroyed on completion of the function.
5098 * @param argc Standard main() style argc.
5099 * @param argv Standard main() style argv. Initial components are already
5100 * stripped.
5102 * @return Normal NTSTATUS return.
5105 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5106 const DOM_SID *domain_sid,
5107 const char *domain_name,
5108 struct cli_state *cli,
5109 struct rpc_pipe_client *pipe_hnd,
5110 TALLOC_CTX *mem_ctx,
5111 int argc,
5112 const char **argv)
5114 const char *msg = N_("This machine will be shutdown shortly");
5115 uint32 timeout = 20;
5116 struct lsa_StringLarge msg_string;
5117 NTSTATUS result;
5118 WERROR werr;
5120 if (c->opt_comment) {
5121 msg = c->opt_comment;
5123 msg_string.string = msg;
5125 if (c->opt_timeout) {
5126 timeout = c->opt_timeout;
5129 /* create an entry */
5130 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5131 &msg_string, timeout, c->opt_force, c->opt_reboot,
5132 &werr);
5134 if (NT_STATUS_IS_OK(result)) {
5135 d_printf(_("\nShutdown of remote machine succeeded\n"));
5136 } else {
5137 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5138 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5139 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5140 else
5141 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5144 return result;
5148 * Shut down a remote RPC server.
5150 * @param argc Standard main() style argc.
5151 * @param argv Standard main() style argv. Initial components are already
5152 * stripped.
5154 * @return A shell status integer (0 for success).
5157 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5159 int rc = -1;
5161 if (c->display_usage) {
5162 d_printf(_("Usage:\n"
5163 "net rpc shutdown\n"
5164 " Shut down a remote RPC server\n"));
5165 return 0;
5168 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5169 rpc_init_shutdown_internals, argc, argv);
5171 if (rc) {
5172 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5173 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5174 rpc_reg_shutdown_internals, argc, argv);
5177 return rc;
5180 /***************************************************************************
5181 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5182 ***************************************************************************/
5185 * Add interdomain trust account to the RPC server.
5186 * All parameters (except for argc and argv) are passed by run_rpc_command
5187 * function.
5189 * @param c A net_context structure.
5190 * @param domain_sid The domain sid acquired from the server.
5191 * @param cli A cli_state connected to the server.
5192 * @param mem_ctx Talloc context, destroyed on completion of the function.
5193 * @param argc Standard main() style argc.
5194 * @param argv Standard main() style argv. Initial components are already
5195 * stripped.
5197 * @return normal NTSTATUS return code.
5200 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5201 const DOM_SID *domain_sid,
5202 const char *domain_name,
5203 struct cli_state *cli,
5204 struct rpc_pipe_client *pipe_hnd,
5205 TALLOC_CTX *mem_ctx,
5206 int argc,
5207 const char **argv)
5209 struct policy_handle connect_pol, domain_pol, user_pol;
5210 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5211 char *acct_name;
5212 struct lsa_String lsa_acct_name;
5213 uint32 acb_info;
5214 uint32 acct_flags=0;
5215 uint32 user_rid;
5216 uint32_t access_granted = 0;
5217 union samr_UserInfo info;
5218 unsigned int orig_timeout;
5220 if (argc != 2) {
5221 d_printf(_("Usage: net rpc trustdom add <domain_name> "
5222 "<trust password>\n"));
5223 return NT_STATUS_INVALID_PARAMETER;
5227 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5230 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5231 return NT_STATUS_NO_MEMORY;
5234 strupper_m(acct_name);
5236 init_lsa_String(&lsa_acct_name, acct_name);
5238 /* Get samr policy handle */
5239 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5240 pipe_hnd->desthost,
5241 MAXIMUM_ALLOWED_ACCESS,
5242 &connect_pol);
5243 if (!NT_STATUS_IS_OK(result)) {
5244 goto done;
5247 /* Get domain policy handle */
5248 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5249 &connect_pol,
5250 MAXIMUM_ALLOWED_ACCESS,
5251 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5252 &domain_pol);
5253 if (!NT_STATUS_IS_OK(result)) {
5254 goto done;
5257 /* This call can take a long time - allow the server to time out.
5258 * 35 seconds should do it. */
5260 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5262 /* Create trusting domain's account */
5263 acb_info = ACB_NORMAL;
5264 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5265 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5266 SAMR_USER_ACCESS_SET_PASSWORD |
5267 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5268 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5270 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5271 &domain_pol,
5272 &lsa_acct_name,
5273 acb_info,
5274 acct_flags,
5275 &user_pol,
5276 &access_granted,
5277 &user_rid);
5279 /* And restore our original timeout. */
5280 rpccli_set_timeout(pipe_hnd, orig_timeout);
5282 if (!NT_STATUS_IS_OK(result)) {
5283 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5284 acct_name, nt_errstr(result));
5285 goto done;
5289 struct samr_CryptPassword crypt_pwd;
5291 ZERO_STRUCT(info.info23);
5293 init_samr_CryptPassword(argv[1],
5294 &cli->user_session_key,
5295 &crypt_pwd);
5297 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5298 SAMR_FIELD_NT_PASSWORD_PRESENT;
5299 info.info23.info.acct_flags = ACB_DOMTRUST;
5300 info.info23.password = crypt_pwd;
5302 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5303 &user_pol,
5305 &info);
5307 if (!NT_STATUS_IS_OK(result)) {
5308 DEBUG(0,("Could not set trust account password: %s\n",
5309 nt_errstr(result)));
5310 goto done;
5314 done:
5315 SAFE_FREE(acct_name);
5316 return result;
5320 * Create interdomain trust account for a remote domain.
5322 * @param argc Standard argc.
5323 * @param argv Standard argv without initial components.
5325 * @return Integer status (0 means success).
5328 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5330 if (argc > 0 && !c->display_usage) {
5331 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5332 rpc_trustdom_add_internals, argc, argv);
5333 } else {
5334 d_printf(_("Usage:\n"
5335 "net rpc trustdom add <domain_name> <trust "
5336 "password>\n"));
5337 return -1;
5343 * Remove interdomain trust account from the RPC server.
5344 * All parameters (except for argc and argv) are passed by run_rpc_command
5345 * function.
5347 * @param c A net_context structure.
5348 * @param domain_sid The domain sid acquired from the server.
5349 * @param cli A cli_state connected to the server.
5350 * @param mem_ctx Talloc context, destroyed on completion of the function.
5351 * @param argc Standard main() style argc.
5352 * @param argv Standard main() style argv. Initial components are already
5353 * stripped.
5355 * @return normal NTSTATUS return code.
5358 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5359 const DOM_SID *domain_sid,
5360 const char *domain_name,
5361 struct cli_state *cli,
5362 struct rpc_pipe_client *pipe_hnd,
5363 TALLOC_CTX *mem_ctx,
5364 int argc,
5365 const char **argv)
5367 struct policy_handle connect_pol, domain_pol, user_pol;
5368 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5369 char *acct_name;
5370 DOM_SID trust_acct_sid;
5371 struct samr_Ids user_rids, name_types;
5372 struct lsa_String lsa_acct_name;
5374 if (argc != 1) {
5375 d_printf(_("Usage: net rpc trustdom del <domain_name>\n"));
5376 return NT_STATUS_INVALID_PARAMETER;
5380 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5382 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5384 if (acct_name == NULL)
5385 return NT_STATUS_NO_MEMORY;
5387 strupper_m(acct_name);
5389 /* Get samr policy handle */
5390 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5391 pipe_hnd->desthost,
5392 MAXIMUM_ALLOWED_ACCESS,
5393 &connect_pol);
5394 if (!NT_STATUS_IS_OK(result)) {
5395 goto done;
5398 /* Get domain policy handle */
5399 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5400 &connect_pol,
5401 MAXIMUM_ALLOWED_ACCESS,
5402 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5403 &domain_pol);
5404 if (!NT_STATUS_IS_OK(result)) {
5405 goto done;
5408 init_lsa_String(&lsa_acct_name, acct_name);
5410 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5411 &domain_pol,
5413 &lsa_acct_name,
5414 &user_rids,
5415 &name_types);
5417 if (!NT_STATUS_IS_OK(result)) {
5418 d_printf(_("net rpc trustdom del: LookupNames on user %s "
5419 "failed %s\n"),
5420 acct_name, nt_errstr(result) );
5421 goto done;
5424 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5425 &domain_pol,
5426 MAXIMUM_ALLOWED_ACCESS,
5427 user_rids.ids[0],
5428 &user_pol);
5430 if (!NT_STATUS_IS_OK(result)) {
5431 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
5432 "%s\n"),
5433 acct_name, nt_errstr(result) );
5434 goto done;
5437 /* append the rid to the domain sid */
5438 sid_copy(&trust_acct_sid, domain_sid);
5439 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5440 goto done;
5443 /* remove the sid */
5445 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5446 &user_pol,
5447 &trust_acct_sid);
5448 if (!NT_STATUS_IS_OK(result)) {
5449 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
5450 " on user %s failed %s\n"),
5451 acct_name, nt_errstr(result) );
5452 goto done;
5455 /* Delete user */
5457 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5458 &user_pol);
5460 if (!NT_STATUS_IS_OK(result)) {
5461 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
5462 "%s\n"),
5463 acct_name, nt_errstr(result) );
5464 goto done;
5467 if (!NT_STATUS_IS_OK(result)) {
5468 d_printf(_("Could not set trust account password: %s\n"),
5469 nt_errstr(result));
5470 goto done;
5473 done:
5474 return result;
5478 * Delete interdomain trust account for a remote domain.
5480 * @param argc Standard argc.
5481 * @param argv Standard argv without initial components.
5483 * @return Integer status (0 means success).
5486 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5488 if (argc > 0 && !c->display_usage) {
5489 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5490 rpc_trustdom_del_internals, argc, argv);
5491 } else {
5492 d_printf(_("Usage:\n"
5493 "net rpc trustdom del <domain>\n"));
5494 return -1;
5498 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5499 struct cli_state *cli,
5500 TALLOC_CTX *mem_ctx,
5501 const char *domain_name)
5503 char *dc_name = NULL;
5504 const char *buffer = NULL;
5505 struct rpc_pipe_client *netr;
5506 NTSTATUS status;
5508 /* Use NetServerEnum2 */
5510 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5511 SAFE_FREE(dc_name);
5512 return NT_STATUS_OK;
5515 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5516 for domain %s\n", domain_name));
5518 /* Try netr_GetDcName */
5520 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5521 &netr);
5522 if (!NT_STATUS_IS_OK(status)) {
5523 return status;
5526 status = rpccli_netr_GetDcName(netr, mem_ctx,
5527 cli->desthost,
5528 domain_name,
5529 &buffer,
5530 NULL);
5531 TALLOC_FREE(netr);
5533 if (NT_STATUS_IS_OK(status)) {
5534 return status;
5537 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5538 for domain %s\n", domain_name));
5540 return status;
5544 * Establish trust relationship to a trusting domain.
5545 * Interdomain account must already be created on remote PDC.
5547 * @param c A net_context structure.
5548 * @param argc Standard argc.
5549 * @param argv Standard argv without initial components.
5551 * @return Integer status (0 means success).
5554 static int rpc_trustdom_establish(struct net_context *c, int argc,
5555 const char **argv)
5557 struct cli_state *cli = NULL;
5558 struct sockaddr_storage server_ss;
5559 struct rpc_pipe_client *pipe_hnd = NULL;
5560 struct policy_handle connect_hnd;
5561 TALLOC_CTX *mem_ctx;
5562 NTSTATUS nt_status;
5563 DOM_SID *domain_sid;
5565 char* domain_name;
5566 char* acct_name;
5567 fstring pdc_name;
5568 union lsa_PolicyInformation *info = NULL;
5571 * Connect to \\server\ipc$ as 'our domain' account with password
5574 if (argc != 1 || c->display_usage) {
5575 d_printf(_("Usage:\n"
5576 "net rpc trustdom establish <domain_name>\n"));
5577 return -1;
5580 domain_name = smb_xstrdup(argv[0]);
5581 strupper_m(domain_name);
5583 /* account name used at first is our domain's name with '$' */
5584 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
5585 return -1;
5587 strupper_m(acct_name);
5590 * opt_workgroup will be used by connection functions further,
5591 * hence it should be set to remote domain name instead of ours
5593 if (c->opt_workgroup) {
5594 c->opt_workgroup = smb_xstrdup(domain_name);
5597 c->opt_user_name = acct_name;
5599 /* find the domain controller */
5600 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5601 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5602 return -1;
5605 /* connect to ipc$ as username/password */
5606 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5607 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5609 /* Is it trusting domain account for sure ? */
5610 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5611 nt_errstr(nt_status)));
5612 return -1;
5615 /* store who we connected to */
5617 saf_store( domain_name, pdc_name );
5620 * Connect to \\server\ipc$ again (this time anonymously)
5623 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5624 (char*)pdc_name);
5626 if (NT_STATUS_IS_ERR(nt_status)) {
5627 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5628 domain_name, nt_errstr(nt_status)));
5629 return -1;
5632 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5633 "domain %s", domain_name))) {
5634 DEBUG(0, ("talloc_init() failed\n"));
5635 cli_shutdown(cli);
5636 return -1;
5639 /* Make sure we're talking to a proper server */
5641 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5642 if (!NT_STATUS_IS_OK(nt_status)) {
5643 cli_shutdown(cli);
5644 talloc_destroy(mem_ctx);
5645 return -1;
5649 * Call LsaOpenPolicy and LsaQueryInfo
5652 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5653 &pipe_hnd);
5654 if (!NT_STATUS_IS_OK(nt_status)) {
5655 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5656 cli_shutdown(cli);
5657 talloc_destroy(mem_ctx);
5658 return -1;
5661 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
5662 &connect_hnd);
5663 if (NT_STATUS_IS_ERR(nt_status)) {
5664 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5665 nt_errstr(nt_status)));
5666 cli_shutdown(cli);
5667 talloc_destroy(mem_ctx);
5668 return -1;
5671 /* Querying info level 5 */
5673 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5674 &connect_hnd,
5675 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5676 &info);
5677 if (NT_STATUS_IS_ERR(nt_status)) {
5678 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5679 nt_errstr(nt_status)));
5680 cli_shutdown(cli);
5681 talloc_destroy(mem_ctx);
5682 return -1;
5685 domain_sid = info->account_domain.sid;
5687 /* There should be actually query info level 3 (following nt serv behaviour),
5688 but I still don't know if it's _really_ necessary */
5691 * Store the password in secrets db
5694 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5695 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5696 cli_shutdown(cli);
5697 talloc_destroy(mem_ctx);
5698 return -1;
5702 * Close the pipes and clean up
5705 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5706 if (NT_STATUS_IS_ERR(nt_status)) {
5707 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5708 nt_errstr(nt_status)));
5709 cli_shutdown(cli);
5710 talloc_destroy(mem_ctx);
5711 return -1;
5714 cli_shutdown(cli);
5716 talloc_destroy(mem_ctx);
5718 d_printf(_("Trust to domain %s established\n"), domain_name);
5719 return 0;
5723 * Revoke trust relationship to the remote domain.
5725 * @param c A net_context structure.
5726 * @param argc Standard argc.
5727 * @param argv Standard argv without initial components.
5729 * @return Integer status (0 means success).
5732 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5733 const char **argv)
5735 char* domain_name;
5736 int rc = -1;
5738 if (argc < 1 || c->display_usage) {
5739 d_printf(_("Usage:\n"
5740 "net rpc trustdom revoke <domain_name>\n"
5741 " Revoke trust relationship\n"
5742 " domain_name\tName of domain to revoke trust\n"));
5743 return -1;
5746 /* generate upper cased domain name */
5747 domain_name = smb_xstrdup(argv[0]);
5748 strupper_m(domain_name);
5750 /* delete password of the trust */
5751 if (!pdb_del_trusteddom_pw(domain_name)) {
5752 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5753 domain_name));
5754 goto done;
5757 rc = 0;
5758 done:
5759 SAFE_FREE(domain_name);
5760 return rc;
5763 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5764 const DOM_SID *domain_sid,
5765 const char *domain_name,
5766 struct cli_state *cli,
5767 struct rpc_pipe_client *pipe_hnd,
5768 TALLOC_CTX *mem_ctx,
5769 int argc,
5770 const char **argv)
5772 fstring str_sid;
5773 sid_to_fstring(str_sid, domain_sid);
5774 d_printf("%s\n", str_sid);
5775 return NT_STATUS_OK;
5778 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5780 fstring ascii_sid;
5782 /* convert sid into ascii string */
5783 sid_to_fstring(ascii_sid, dom_sid);
5785 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
5788 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5789 TALLOC_CTX *mem_ctx,
5790 struct policy_handle *pol,
5791 DOM_SID dom_sid,
5792 const char *trusted_dom_name)
5794 NTSTATUS nt_status;
5795 union lsa_TrustedDomainInfo *info = NULL;
5796 char *cleartextpwd = NULL;
5797 uint8_t session_key[16];
5798 DATA_BLOB session_key_blob;
5799 DATA_BLOB data = data_blob_null;
5801 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5802 pol,
5803 &dom_sid,
5804 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5805 &info);
5806 if (NT_STATUS_IS_ERR(nt_status)) {
5807 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5808 nt_errstr(nt_status)));
5809 goto done;
5812 data = data_blob(info->password.password->data,
5813 info->password.password->length);
5815 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
5816 DEBUG(0, ("Could not retrieve password hash\n"));
5817 goto done;
5820 session_key_blob = data_blob_const(session_key, sizeof(session_key));
5821 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
5823 if (cleartextpwd == NULL) {
5824 DEBUG(0,("retrieved NULL password\n"));
5825 nt_status = NT_STATUS_UNSUCCESSFUL;
5826 goto done;
5829 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5830 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5831 nt_status = NT_STATUS_UNSUCCESSFUL;
5832 goto done;
5835 #ifdef DEBUG_PASSWORD
5836 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5837 "password: [%s]\n", trusted_dom_name,
5838 sid_string_dbg(&dom_sid), cleartextpwd));
5839 #endif
5841 done:
5842 SAFE_FREE(cleartextpwd);
5843 data_blob_free(&data);
5845 return nt_status;
5848 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5849 const char **argv)
5851 /* common variables */
5852 TALLOC_CTX* mem_ctx;
5853 struct cli_state *cli = NULL;
5854 struct rpc_pipe_client *pipe_hnd = NULL;
5855 NTSTATUS nt_status;
5856 const char *domain_name = NULL;
5857 DOM_SID *queried_dom_sid;
5858 struct policy_handle connect_hnd;
5859 union lsa_PolicyInformation *info = NULL;
5861 /* trusted domains listing variables */
5862 unsigned int enum_ctx = 0;
5863 int i;
5864 struct lsa_DomainList dom_list;
5865 fstring pdc_name;
5867 if (c->display_usage) {
5868 d_printf(_("Usage:\n"
5869 "net rpc trustdom vampire\n"
5870 " Vampire trust relationship from remote server\n"));
5871 return 0;
5875 * Listing trusted domains (stored in secrets.tdb, if local)
5878 mem_ctx = talloc_init("trust relationships vampire");
5881 * set domain and pdc name to local samba server (default)
5882 * or to remote one given in command line
5885 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
5886 domain_name = c->opt_workgroup;
5887 c->opt_target_workgroup = c->opt_workgroup;
5888 } else {
5889 fstrcpy(pdc_name, global_myname());
5890 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5891 c->opt_target_workgroup = domain_name;
5894 /* open \PIPE\lsarpc and open policy handle */
5895 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
5896 if (!NT_STATUS_IS_OK(nt_status)) {
5897 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
5898 nt_errstr(nt_status)));
5899 talloc_destroy(mem_ctx);
5900 return -1;
5903 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5904 &pipe_hnd);
5905 if (!NT_STATUS_IS_OK(nt_status)) {
5906 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5907 nt_errstr(nt_status) ));
5908 cli_shutdown(cli);
5909 talloc_destroy(mem_ctx);
5910 return -1;
5913 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
5914 &connect_hnd);
5915 if (NT_STATUS_IS_ERR(nt_status)) {
5916 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5917 nt_errstr(nt_status)));
5918 cli_shutdown(cli);
5919 talloc_destroy(mem_ctx);
5920 return -1;
5923 /* query info level 5 to obtain sid of a domain being queried */
5924 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5925 &connect_hnd,
5926 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5927 &info);
5929 if (NT_STATUS_IS_ERR(nt_status)) {
5930 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5931 nt_errstr(nt_status)));
5932 cli_shutdown(cli);
5933 talloc_destroy(mem_ctx);
5934 return -1;
5937 queried_dom_sid = info->account_domain.sid;
5940 * Keep calling LsaEnumTrustdom over opened pipe until
5941 * the end of enumeration is reached
5944 d_printf(_("Vampire trusted domains:\n\n"));
5946 do {
5947 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
5948 &connect_hnd,
5949 &enum_ctx,
5950 &dom_list,
5951 (uint32_t)-1);
5952 if (NT_STATUS_IS_ERR(nt_status)) {
5953 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5954 nt_errstr(nt_status)));
5955 cli_shutdown(cli);
5956 talloc_destroy(mem_ctx);
5957 return -1;
5960 for (i = 0; i < dom_list.count; i++) {
5962 print_trusted_domain(dom_list.domains[i].sid,
5963 dom_list.domains[i].name.string);
5965 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5966 *dom_list.domains[i].sid,
5967 dom_list.domains[i].name.string);
5968 if (!NT_STATUS_IS_OK(nt_status)) {
5969 cli_shutdown(cli);
5970 talloc_destroy(mem_ctx);
5971 return -1;
5976 * in case of no trusted domains say something rather
5977 * than just display blank line
5979 if (!dom_list.count) d_printf(_("none\n"));
5981 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5983 /* close this connection before doing next one */
5984 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5985 if (NT_STATUS_IS_ERR(nt_status)) {
5986 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5987 nt_errstr(nt_status)));
5988 cli_shutdown(cli);
5989 talloc_destroy(mem_ctx);
5990 return -1;
5993 /* close lsarpc pipe and connection to IPC$ */
5994 cli_shutdown(cli);
5996 talloc_destroy(mem_ctx);
5997 return 0;
6000 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6002 /* common variables */
6003 TALLOC_CTX* mem_ctx;
6004 struct cli_state *cli = NULL, *remote_cli = NULL;
6005 struct rpc_pipe_client *pipe_hnd = NULL;
6006 NTSTATUS nt_status;
6007 const char *domain_name = NULL;
6008 DOM_SID *queried_dom_sid;
6009 int ascii_dom_name_len;
6010 struct policy_handle connect_hnd;
6011 union lsa_PolicyInformation *info = NULL;
6013 /* trusted domains listing variables */
6014 unsigned int num_domains, enum_ctx = 0;
6015 int i;
6016 struct lsa_DomainList dom_list;
6017 fstring pdc_name;
6019 /* trusting domains listing variables */
6020 struct policy_handle domain_hnd;
6021 struct samr_SamArray *trusts = NULL;
6023 if (c->display_usage) {
6024 d_printf(_("Usage:\n"
6025 "net rpc trustdom list\n"
6026 " List in- and outgoing trust relationships\n"));
6027 return 0;
6031 * Listing trusted domains (stored in secrets.tdb, if local)
6034 mem_ctx = talloc_init("trust relationships listing");
6037 * set domain and pdc name to local samba server (default)
6038 * or to remote one given in command line
6041 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6042 domain_name = c->opt_workgroup;
6043 c->opt_target_workgroup = c->opt_workgroup;
6044 } else {
6045 fstrcpy(pdc_name, global_myname());
6046 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6047 c->opt_target_workgroup = domain_name;
6050 /* open \PIPE\lsarpc and open policy handle */
6051 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6052 if (!NT_STATUS_IS_OK(nt_status)) {
6053 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6054 nt_errstr(nt_status)));
6055 talloc_destroy(mem_ctx);
6056 return -1;
6059 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6060 &pipe_hnd);
6061 if (!NT_STATUS_IS_OK(nt_status)) {
6062 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6063 nt_errstr(nt_status) ));
6064 cli_shutdown(cli);
6065 talloc_destroy(mem_ctx);
6066 return -1;
6069 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6070 &connect_hnd);
6071 if (NT_STATUS_IS_ERR(nt_status)) {
6072 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6073 nt_errstr(nt_status)));
6074 cli_shutdown(cli);
6075 talloc_destroy(mem_ctx);
6076 return -1;
6079 /* query info level 5 to obtain sid of a domain being queried */
6080 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6081 &connect_hnd,
6082 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6083 &info);
6085 if (NT_STATUS_IS_ERR(nt_status)) {
6086 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6087 nt_errstr(nt_status)));
6088 cli_shutdown(cli);
6089 talloc_destroy(mem_ctx);
6090 return -1;
6093 queried_dom_sid = info->account_domain.sid;
6096 * Keep calling LsaEnumTrustdom over opened pipe until
6097 * the end of enumeration is reached
6100 d_printf(_("Trusted domains list:\n\n"));
6102 do {
6103 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6104 &connect_hnd,
6105 &enum_ctx,
6106 &dom_list,
6107 (uint32_t)-1);
6108 if (NT_STATUS_IS_ERR(nt_status)) {
6109 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6110 nt_errstr(nt_status)));
6111 cli_shutdown(cli);
6112 talloc_destroy(mem_ctx);
6113 return -1;
6116 for (i = 0; i < dom_list.count; i++) {
6117 print_trusted_domain(dom_list.domains[i].sid,
6118 dom_list.domains[i].name.string);
6122 * in case of no trusted domains say something rather
6123 * than just display blank line
6125 if (!dom_list.count) d_printf(_("none\n"));
6127 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6129 /* close this connection before doing next one */
6130 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6131 if (NT_STATUS_IS_ERR(nt_status)) {
6132 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6133 nt_errstr(nt_status)));
6134 cli_shutdown(cli);
6135 talloc_destroy(mem_ctx);
6136 return -1;
6139 TALLOC_FREE(pipe_hnd);
6142 * Listing trusting domains (stored in passdb backend, if local)
6145 d_printf(_("\nTrusting domains list:\n\n"));
6148 * Open \PIPE\samr and get needed policy handles
6150 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6151 &pipe_hnd);
6152 if (!NT_STATUS_IS_OK(nt_status)) {
6153 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6154 cli_shutdown(cli);
6155 talloc_destroy(mem_ctx);
6156 return -1;
6159 /* SamrConnect2 */
6160 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6161 pipe_hnd->desthost,
6162 SAMR_ACCESS_LOOKUP_DOMAIN,
6163 &connect_hnd);
6164 if (!NT_STATUS_IS_OK(nt_status)) {
6165 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6166 nt_errstr(nt_status)));
6167 cli_shutdown(cli);
6168 talloc_destroy(mem_ctx);
6169 return -1;
6172 /* SamrOpenDomain - we have to open domain policy handle in order to be
6173 able to enumerate accounts*/
6174 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6175 &connect_hnd,
6176 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6177 queried_dom_sid,
6178 &domain_hnd);
6179 if (!NT_STATUS_IS_OK(nt_status)) {
6180 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6181 nt_errstr(nt_status)));
6182 cli_shutdown(cli);
6183 talloc_destroy(mem_ctx);
6184 return -1;
6188 * perform actual enumeration
6191 enum_ctx = 0; /* reset enumeration context from last enumeration */
6192 do {
6194 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6195 &domain_hnd,
6196 &enum_ctx,
6197 ACB_DOMTRUST,
6198 &trusts,
6199 0xffff,
6200 &num_domains);
6201 if (NT_STATUS_IS_ERR(nt_status)) {
6202 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6203 nt_errstr(nt_status)));
6204 cli_shutdown(cli);
6205 talloc_destroy(mem_ctx);
6206 return -1;
6209 for (i = 0; i < num_domains; i++) {
6211 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6214 * get each single domain's sid (do we _really_ need this ?):
6215 * 1) connect to domain's pdc
6216 * 2) query the pdc for domain's sid
6219 /* get rid of '$' tail */
6220 ascii_dom_name_len = strlen(str);
6221 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6222 str[ascii_dom_name_len - 1] = '\0';
6224 /* set opt_* variables to remote domain */
6225 strupper_m(str);
6226 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6227 c->opt_target_workgroup = c->opt_workgroup;
6229 d_printf("%-20s", str);
6231 /* connect to remote domain controller */
6232 nt_status = net_make_ipc_connection(c,
6233 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6234 &remote_cli);
6235 if (NT_STATUS_IS_OK(nt_status)) {
6236 /* query for domain's sid */
6237 if (run_rpc_command(
6238 c, remote_cli,
6239 &ndr_table_lsarpc.syntax_id, 0,
6240 rpc_query_domain_sid, argc,
6241 argv))
6242 d_fprintf(stderr,
6243 _("couldn't get domain's sid\n"));
6245 cli_shutdown(remote_cli);
6247 } else {
6248 d_fprintf(stderr, _("domain controller is not "
6249 "responding: %s\n"),
6250 nt_errstr(nt_status));
6254 if (!num_domains) d_printf("none\n");
6256 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6258 /* close opened samr and domain policy handles */
6259 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6260 if (!NT_STATUS_IS_OK(nt_status)) {
6261 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6264 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6265 if (!NT_STATUS_IS_OK(nt_status)) {
6266 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6269 /* close samr pipe and connection to IPC$ */
6270 cli_shutdown(cli);
6272 talloc_destroy(mem_ctx);
6273 return 0;
6277 * Entrypoint for 'net rpc trustdom' code.
6279 * @param argc Standard argc.
6280 * @param argv Standard argv without initial components.
6282 * @return Integer status (0 means success).
6285 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6287 struct functable func[] = {
6289 "add",
6290 rpc_trustdom_add,
6291 NET_TRANSPORT_RPC,
6292 N_("Add trusting domain's account"),
6293 N_("net rpc trustdom add\n"
6294 " Add trusting domain's account")
6297 "del",
6298 rpc_trustdom_del,
6299 NET_TRANSPORT_RPC,
6300 N_("Remove trusting domain's account"),
6301 N_("net rpc trustdom del\n"
6302 " Remove trusting domain's account")
6305 "establish",
6306 rpc_trustdom_establish,
6307 NET_TRANSPORT_RPC,
6308 N_("Establish outgoing trust relationship"),
6309 N_("net rpc trustdom establish\n"
6310 " Establish outgoing trust relationship")
6313 "revoke",
6314 rpc_trustdom_revoke,
6315 NET_TRANSPORT_RPC,
6316 N_("Revoke outgoing trust relationship"),
6317 N_("net rpc trustdom revoke\n"
6318 " Revoke outgoing trust relationship")
6321 "list",
6322 rpc_trustdom_list,
6323 NET_TRANSPORT_RPC,
6324 N_("List in- and outgoing domain trusts"),
6325 N_("net rpc trustdom list\n"
6326 " List in- and outgoing domain trusts")
6329 "vampire",
6330 rpc_trustdom_vampire,
6331 NET_TRANSPORT_RPC,
6332 N_("Vampire trusts from remote server"),
6333 N_("net rpc trustdom vampire\n"
6334 " Vampire trusts from remote server")
6336 {NULL, NULL, 0, NULL, NULL}
6339 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6343 * Check if a server will take rpc commands
6344 * @param flags Type of server to connect to (PDC, DMB, localhost)
6345 * if the host is not explicitly specified
6346 * @return bool (true means rpc supported)
6348 bool net_rpc_check(struct net_context *c, unsigned flags)
6350 struct cli_state *cli;
6351 bool ret = false;
6352 struct sockaddr_storage server_ss;
6353 char *server_name = NULL;
6354 NTSTATUS status;
6356 /* flags (i.e. server type) may depend on command */
6357 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6358 return false;
6360 if ((cli = cli_initialise()) == NULL) {
6361 return false;
6364 status = cli_connect(cli, server_name, &server_ss);
6365 if (!NT_STATUS_IS_OK(status))
6366 goto done;
6367 if (!attempt_netbios_session_request(&cli, global_myname(),
6368 server_name, &server_ss))
6369 goto done;
6370 status = cli_negprot(cli);
6371 if (!NT_STATUS_IS_OK(status))
6372 goto done;
6373 if (cli->protocol < PROTOCOL_NT1)
6374 goto done;
6376 ret = true;
6377 done:
6378 cli_shutdown(cli);
6379 return ret;
6382 /* dump sam database via samsync rpc calls */
6383 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6384 if (c->display_usage) {
6385 d_printf(_("Usage:\n"
6386 "net rpc samdump\n"
6387 " Dump remote SAM database\n"));
6388 return 0;
6391 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6392 NET_FLAGS_ANONYMOUS,
6393 rpc_samdump_internals, argc, argv);
6396 /* syncronise sam database via samsync rpc calls */
6397 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6399 struct functable func[] = {
6401 "ldif",
6402 rpc_vampire_ldif,
6403 NET_TRANSPORT_RPC,
6404 N_("Dump remote SAM database to ldif"),
6405 N_("net rpc vampire ldif\n"
6406 " Dump remote SAM database to LDIF file or "
6407 "stdout")
6410 "keytab",
6411 rpc_vampire_keytab,
6412 NET_TRANSPORT_RPC,
6413 N_("Dump remote SAM database to Kerberos Keytab"),
6414 N_("net rpc vampire keytab\n"
6415 " Dump remote SAM database to Kerberos keytab "
6416 "file")
6419 "passdb",
6420 rpc_vampire_passdb,
6421 NET_TRANSPORT_RPC,
6422 N_("Dump remote SAM database to passdb"),
6423 N_("net rpc vampire passdb\n"
6424 " Dump remote SAM database to passdb")
6427 {NULL, NULL, 0, NULL, NULL}
6430 if (argc == 0) {
6431 if (c->display_usage) {
6432 d_printf(_("Usage:\n"
6433 "net rpc vampire\n"
6434 " Vampire remote SAM database\n"));
6435 return 0;
6438 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6439 NET_FLAGS_ANONYMOUS,
6440 rpc_vampire_internals,
6441 argc, argv);
6444 return net_run_function(c, argc, argv, "net rpc vampire", func);
6448 * Migrate everything from a print server.
6450 * @param c A net_context structure.
6451 * @param argc Standard main() style argc.
6452 * @param argv Standard main() style argv. Initial components are already
6453 * stripped.
6455 * @return A shell status integer (0 for success).
6457 * The order is important !
6458 * To successfully add drivers the print queues have to exist !
6459 * Applying ACLs should be the last step, because you're easily locked out.
6462 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6463 const char **argv)
6465 int ret;
6467 if (c->display_usage) {
6468 d_printf(_("Usage:\n"
6469 "net rpc printer migrate all\n"
6470 " Migrate everything from a print server\n"));
6471 return 0;
6474 if (!c->opt_host) {
6475 d_printf(_("no server to migrate\n"));
6476 return -1;
6479 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6480 rpc_printer_migrate_printers_internals, argc,
6481 argv);
6482 if (ret)
6483 return ret;
6485 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6486 rpc_printer_migrate_drivers_internals, argc,
6487 argv);
6488 if (ret)
6489 return ret;
6491 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6492 rpc_printer_migrate_forms_internals, argc, argv);
6493 if (ret)
6494 return ret;
6496 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6497 rpc_printer_migrate_settings_internals, argc,
6498 argv);
6499 if (ret)
6500 return ret;
6502 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6503 rpc_printer_migrate_security_internals, argc,
6504 argv);
6509 * Migrate print drivers from a print server.
6511 * @param c A net_context structure.
6512 * @param argc Standard main() style argc.
6513 * @param argv Standard main() style argv. Initial components are already
6514 * stripped.
6516 * @return A shell status integer (0 for success).
6518 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6519 const char **argv)
6521 if (c->display_usage) {
6522 d_printf(_("Usage:\n"
6523 "net rpc printer migrate drivers\n"
6524 " Migrate print-drivers from a print-server\n"));
6525 return 0;
6528 if (!c->opt_host) {
6529 d_printf(_("no server to migrate\n"));
6530 return -1;
6533 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6534 rpc_printer_migrate_drivers_internals,
6535 argc, argv);
6539 * Migrate print-forms from a print-server.
6541 * @param c A net_context structure.
6542 * @param argc Standard main() style argc.
6543 * @param argv Standard main() style argv. Initial components are already
6544 * stripped.
6546 * @return A shell status integer (0 for success).
6548 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6549 const char **argv)
6551 if (c->display_usage) {
6552 d_printf(_("Usage:\n"
6553 "net rpc printer migrate forms\n"
6554 " Migrate print-forms from a print-server\n"));
6555 return 0;
6558 if (!c->opt_host) {
6559 d_printf(_("no server to migrate\n"));
6560 return -1;
6563 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6564 rpc_printer_migrate_forms_internals,
6565 argc, argv);
6569 * Migrate printers from a print-server.
6571 * @param c A net_context structure.
6572 * @param argc Standard main() style argc.
6573 * @param argv Standard main() style argv. Initial components are already
6574 * stripped.
6576 * @return A shell status integer (0 for success).
6578 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6579 const char **argv)
6581 if (c->display_usage) {
6582 d_printf(_("Usage:\n"
6583 "net rpc printer migrate printers\n"
6584 " Migrate printers from a print-server\n"));
6585 return 0;
6588 if (!c->opt_host) {
6589 d_printf(_("no server to migrate\n"));
6590 return -1;
6593 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6594 rpc_printer_migrate_printers_internals,
6595 argc, argv);
6599 * Migrate printer-ACLs from a print-server
6601 * @param c A net_context structure.
6602 * @param argc Standard main() style argc.
6603 * @param argv Standard main() style argv. Initial components are already
6604 * stripped.
6606 * @return A shell status integer (0 for success).
6608 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6609 const char **argv)
6611 if (c->display_usage) {
6612 d_printf(_("Usage:\n"
6613 "net rpc printer migrate security\n"
6614 " Migrate printer-ACLs from a print-server\n"));
6615 return 0;
6618 if (!c->opt_host) {
6619 d_printf(_("no server to migrate\n"));
6620 return -1;
6623 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6624 rpc_printer_migrate_security_internals,
6625 argc, argv);
6629 * Migrate printer-settings from a print-server.
6631 * @param c A net_context structure.
6632 * @param argc Standard main() style argc.
6633 * @param argv Standard main() style argv. Initial components are already
6634 * stripped.
6636 * @return A shell status integer (0 for success).
6638 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6639 const char **argv)
6641 if (c->display_usage) {
6642 d_printf(_("Usage:\n"
6643 "net rpc printer migrate settings\n"
6644 " Migrate printer-settings from a "
6645 "print-server\n"));
6646 return 0;
6649 if (!c->opt_host) {
6650 d_printf(_("no server to migrate\n"));
6651 return -1;
6654 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6655 rpc_printer_migrate_settings_internals,
6656 argc, argv);
6660 * 'net rpc printer' entrypoint.
6662 * @param c A net_context structure.
6663 * @param argc Standard main() style argc.
6664 * @param argv Standard main() style argv. Initial components are already
6665 * stripped.
6668 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6671 /* ouch: when addriver and setdriver are called from within
6672 rpc_printer_migrate_drivers_internals, the printer-queue already
6673 *has* to exist */
6675 struct functable func[] = {
6677 "all",
6678 rpc_printer_migrate_all,
6679 NET_TRANSPORT_RPC,
6680 N_("Migrate all from remote to local print server"),
6681 N_("net rpc printer migrate all\n"
6682 " Migrate all from remote to local print server")
6685 "drivers",
6686 rpc_printer_migrate_drivers,
6687 NET_TRANSPORT_RPC,
6688 N_("Migrate drivers to local server"),
6689 N_("net rpc printer migrate drivers\n"
6690 " Migrate drivers to local server")
6693 "forms",
6694 rpc_printer_migrate_forms,
6695 NET_TRANSPORT_RPC,
6696 N_("Migrate froms to local server"),
6697 N_("net rpc printer migrate forms\n"
6698 " Migrate froms to local server")
6701 "printers",
6702 rpc_printer_migrate_printers,
6703 NET_TRANSPORT_RPC,
6704 N_("Migrate printers to local server"),
6705 N_("net rpc printer migrate printers\n"
6706 " Migrate printers to local server")
6709 "security",
6710 rpc_printer_migrate_security,
6711 NET_TRANSPORT_RPC,
6712 N_("Mirgate printer ACLs to local server"),
6713 N_("net rpc printer migrate security\n"
6714 " Mirgate printer ACLs to local server")
6717 "settings",
6718 rpc_printer_migrate_settings,
6719 NET_TRANSPORT_RPC,
6720 N_("Migrate printer settings to local server"),
6721 N_("net rpc printer migrate settings\n"
6722 " Migrate printer settings to local server")
6724 {NULL, NULL, 0, NULL, NULL}
6727 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6732 * List printers on a remote RPC server.
6734 * @param c A net_context structure.
6735 * @param argc Standard main() style argc.
6736 * @param argv Standard main() style argv. Initial components are already
6737 * stripped.
6739 * @return A shell status integer (0 for success).
6741 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6743 if (c->display_usage) {
6744 d_printf(_("Usage:\n"
6745 "net rpc printer list\n"
6746 " List printers on a remote RPC server\n"));
6747 return 0;
6750 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6751 rpc_printer_list_internals,
6752 argc, argv);
6756 * List printer-drivers on a remote RPC server.
6758 * @param c A net_context structure.
6759 * @param argc Standard main() style argc.
6760 * @param argv Standard main() style argv. Initial components are already
6761 * stripped.
6763 * @return A shell status integer (0 for success).
6765 static int rpc_printer_driver_list(struct net_context *c, int argc,
6766 const char **argv)
6768 if (c->display_usage) {
6769 d_printf(_("Usage:\n"
6770 "net rpc printer driver\n"
6771 " List printer-drivers on a remote RPC server\n"));
6772 return 0;
6775 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6776 rpc_printer_driver_list_internals,
6777 argc, argv);
6781 * Publish printer in ADS via MSRPC.
6783 * @param c A net_context structure.
6784 * @param argc Standard main() style argc.
6785 * @param argv Standard main() style argv. Initial components are already
6786 * stripped.
6788 * @return A shell status integer (0 for success).
6790 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6791 const char **argv)
6793 if (c->display_usage) {
6794 d_printf(_("Usage:\n"
6795 "net rpc printer publish publish\n"
6796 " Publish printer in ADS via MSRPC\n"));
6797 return 0;
6800 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6801 rpc_printer_publish_publish_internals,
6802 argc, argv);
6806 * Update printer in ADS via MSRPC.
6808 * @param c A net_context structure.
6809 * @param argc Standard main() style argc.
6810 * @param argv Standard main() style argv. Initial components are already
6811 * stripped.
6813 * @return A shell status integer (0 for success).
6815 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6817 if (c->display_usage) {
6818 d_printf(_("Usage:\n"
6819 "net rpc printer publish update\n"
6820 " Update printer in ADS via MSRPC\n"));
6821 return 0;
6824 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6825 rpc_printer_publish_update_internals,
6826 argc, argv);
6830 * UnPublish printer in ADS via MSRPC.
6832 * @param c A net_context structure.
6833 * @param argc Standard main() style argc.
6834 * @param argv Standard main() style argv. Initial components are already
6835 * stripped.
6837 * @return A shell status integer (0 for success).
6839 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
6840 const char **argv)
6842 if (c->display_usage) {
6843 d_printf(_("Usage:\n"
6844 "net rpc printer publish unpublish\n"
6845 " UnPublish printer in ADS via MSRPC\n"));
6846 return 0;
6849 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6850 rpc_printer_publish_unpublish_internals,
6851 argc, argv);
6855 * List published printers via MSRPC.
6857 * @param c A net_context structure.
6858 * @param argc Standard main() style argc.
6859 * @param argv Standard main() style argv. Initial components are already
6860 * stripped.
6862 * @return A shell status integer (0 for success).
6864 static int rpc_printer_publish_list(struct net_context *c, int argc,
6865 const char **argv)
6867 if (c->display_usage) {
6868 d_printf(_("Usage:\n"
6869 "net rpc printer publish list\n"
6870 " List published printers via MSRPC\n"));
6871 return 0;
6874 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6875 rpc_printer_publish_list_internals,
6876 argc, argv);
6881 * Publish printer in ADS.
6883 * @param c A net_context structure.
6884 * @param argc Standard main() style argc.
6885 * @param argv Standard main() style argv. Initial components are already
6886 * stripped.
6888 * @return A shell status integer (0 for success).
6890 static int rpc_printer_publish(struct net_context *c, int argc,
6891 const char **argv)
6894 struct functable func[] = {
6896 "publish",
6897 rpc_printer_publish_publish,
6898 NET_TRANSPORT_RPC,
6899 N_("Publish printer in AD"),
6900 N_("net rpc printer publish publish\n"
6901 " Publish printer in AD")
6904 "update",
6905 rpc_printer_publish_update,
6906 NET_TRANSPORT_RPC,
6907 N_("Update printer in AD"),
6908 N_("net rpc printer publish update\n"
6909 " Update printer in AD")
6912 "unpublish",
6913 rpc_printer_publish_unpublish,
6914 NET_TRANSPORT_RPC,
6915 N_("Unpublish printer"),
6916 N_("net rpc printer publish unpublish\n"
6917 " Unpublish printer")
6920 "list",
6921 rpc_printer_publish_list,
6922 NET_TRANSPORT_RPC,
6923 N_("List published printers"),
6924 N_("net rpc printer publish list\n"
6925 " List published printers")
6927 {NULL, NULL, 0, NULL, NULL}
6930 if (argc == 0) {
6931 if (c->display_usage) {
6932 d_printf(_("Usage:\n"));
6933 d_printf(_("net rpc printer publish\n"
6934 " List published printers\n"
6935 " Alias of net rpc printer publish "
6936 "list\n"));
6937 net_display_usage_from_functable(func);
6938 return 0;
6940 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6941 rpc_printer_publish_list_internals,
6942 argc, argv);
6945 return net_run_function(c, argc, argv, "net rpc printer publish",func);
6951 * Display rpc printer help page.
6953 * @param c A net_context structure.
6954 * @param argc Standard main() style argc.
6955 * @param argv Standard main() style argv. Initial components are already
6956 * stripped.
6958 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
6960 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
6961 "\tlists all printers on print-server\n\n"));
6962 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
6963 "\tlists all printer-drivers on print-server\n\n"));
6964 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
6965 "\tpublishes printer settings in Active Directory\n"
6966 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
6967 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
6968 "\n\tmigrates printers from remote to local server\n\n"));
6969 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
6970 "\n\tmigrates printer-settings from remote to local server\n\n"));
6971 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
6972 "\n\tmigrates printer-drivers from remote to local server\n\n"));
6973 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
6974 "\n\tmigrates printer-forms from remote to local server\n\n"));
6975 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
6976 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
6977 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
6978 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
6979 "\tremote to local print-server\n\n"));
6980 net_common_methods_usage(c, argc, argv);
6981 net_common_flags_usage(c, argc, argv);
6982 d_printf(_(
6983 "\t-v or --verbose\t\t\tgive verbose output\n"
6984 "\t --destination\t\tmigration target server (default: localhost)\n"));
6986 return -1;
6990 * 'net rpc printer' entrypoint.
6992 * @param c A net_context structure.
6993 * @param argc Standard main() style argc.
6994 * @param argv Standard main() style argv. Initial components are already
6995 * stripped.
6997 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
6999 struct functable func[] = {
7001 "list",
7002 rpc_printer_list,
7003 NET_TRANSPORT_RPC,
7004 N_("List all printers on print server"),
7005 N_("net rpc printer list\n"
7006 " List all printers on print server")
7009 "migrate",
7010 rpc_printer_migrate,
7011 NET_TRANSPORT_RPC,
7012 N_("Migrate printer to local server"),
7013 N_("net rpc printer migrate\n"
7014 " Migrate printer to local server")
7017 "driver",
7018 rpc_printer_driver_list,
7019 NET_TRANSPORT_RPC,
7020 N_("List printer drivers"),
7021 N_("net rpc printer driver\n"
7022 " List printer drivers")
7025 "publish",
7026 rpc_printer_publish,
7027 NET_TRANSPORT_RPC,
7028 N_("Publish printer in AD"),
7029 N_("net rpc printer publish\n"
7030 " Publish printer in AD")
7032 {NULL, NULL, 0, NULL, NULL}
7035 if (argc == 0) {
7036 if (c->display_usage) {
7037 d_printf(_("Usage:\n"));
7038 d_printf(_("net rpc printer\n"
7039 " List printers\n"));
7040 net_display_usage_from_functable(func);
7041 return 0;
7043 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7044 rpc_printer_list_internals,
7045 argc, argv);
7048 return net_run_function(c, argc, argv, "net rpc printer", func);
7052 * 'net rpc' entrypoint.
7054 * @param c A net_context structure.
7055 * @param argc Standard main() style argc.
7056 * @param argv Standard main() style argv. Initial components are already
7057 * stripped.
7060 int net_rpc(struct net_context *c, int argc, const char **argv)
7062 NET_API_STATUS status;
7064 struct functable func[] = {
7066 "audit",
7067 net_rpc_audit,
7068 NET_TRANSPORT_RPC,
7069 N_("Modify global audit settings"),
7070 N_("net rpc audit\n"
7071 " Modify global audit settings")
7074 "info",
7075 net_rpc_info,
7076 NET_TRANSPORT_RPC,
7077 N_("Show basic info about a domain"),
7078 N_("net rpc info\n"
7079 " Show basic info about a domain")
7082 "join",
7083 net_rpc_join,
7084 NET_TRANSPORT_RPC,
7085 N_("Join a domain"),
7086 N_("net rpc join\n"
7087 " Join a domain")
7090 "oldjoin",
7091 net_rpc_oldjoin,
7092 NET_TRANSPORT_RPC,
7093 N_("Join a domain created in server manager"),
7094 N_("net rpc oldjoin\n"
7095 " Join a domain created in server manager")
7098 "testjoin",
7099 net_rpc_testjoin,
7100 NET_TRANSPORT_RPC,
7101 N_("Test that a join is valid"),
7102 N_("net rpc testjoin\n"
7103 " Test that a join is valid")
7106 "user",
7107 net_rpc_user,
7108 NET_TRANSPORT_RPC,
7109 N_("List/modify users"),
7110 N_("net rpc user\n"
7111 " List/modify users")
7114 "password",
7115 rpc_user_password,
7116 NET_TRANSPORT_RPC,
7117 N_("Change a user password"),
7118 N_("net rpc password\n"
7119 " Change a user password\n"
7120 " Alias for net rpc user password")
7123 "group",
7124 net_rpc_group,
7125 NET_TRANSPORT_RPC,
7126 N_("List/modify groups"),
7127 N_("net rpc group\n"
7128 " List/modify groups")
7131 "share",
7132 net_rpc_share,
7133 NET_TRANSPORT_RPC,
7134 N_("List/modify shares"),
7135 N_("net rpc share\n"
7136 " List/modify shares")
7139 "file",
7140 net_rpc_file,
7141 NET_TRANSPORT_RPC,
7142 N_("List open files"),
7143 N_("net rpc file\n"
7144 " List open files")
7147 "printer",
7148 net_rpc_printer,
7149 NET_TRANSPORT_RPC,
7150 N_("List/modify printers"),
7151 N_("net rpc printer\n"
7152 " List/modify printers")
7155 "changetrustpw",
7156 net_rpc_changetrustpw,
7157 NET_TRANSPORT_RPC,
7158 N_("Change trust account password"),
7159 N_("net rpc changetrustpw\n"
7160 " Change trust account password")
7163 "trustdom",
7164 rpc_trustdom,
7165 NET_TRANSPORT_RPC,
7166 N_("Modify domain trusts"),
7167 N_("net rpc trustdom\n"
7168 " Modify domain trusts")
7171 "abortshutdown",
7172 rpc_shutdown_abort,
7173 NET_TRANSPORT_RPC,
7174 N_("Abort a remote shutdown"),
7175 N_("net rpc abortshutdown\n"
7176 " Abort a remote shutdown")
7179 "shutdown",
7180 rpc_shutdown,
7181 NET_TRANSPORT_RPC,
7182 N_("Shutdown a remote server"),
7183 N_("net rpc shutdown\n"
7184 " Shutdown a remote server")
7187 "samdump",
7188 rpc_samdump,
7189 NET_TRANSPORT_RPC,
7190 N_("Dump SAM data of remote NT PDC"),
7191 N_("net rpc samdump\n"
7192 " Dump SAM data of remote NT PDC")
7195 "vampire",
7196 rpc_vampire,
7197 NET_TRANSPORT_RPC,
7198 N_("Sync a remote NT PDC's data into local passdb"),
7199 N_("net rpc vampire\n"
7200 " Sync a remote NT PDC's data into local passdb")
7203 "getsid",
7204 net_rpc_getsid,
7205 NET_TRANSPORT_RPC,
7206 N_("Fetch the domain sid into local secrets.tdb"),
7207 N_("net rpc getsid\n"
7208 " Fetch the domain sid into local secrets.tdb")
7211 "rights",
7212 net_rpc_rights,
7213 NET_TRANSPORT_RPC,
7214 N_("Manage privileges assigned to SID"),
7215 N_("net rpc rights\n"
7216 " Manage privileges assigned to SID")
7219 "service",
7220 net_rpc_service,
7221 NET_TRANSPORT_RPC,
7222 N_("Start/stop/query remote services"),
7223 N_("net rpc service\n"
7224 " Start/stop/query remote services")
7227 "registry",
7228 net_rpc_registry,
7229 NET_TRANSPORT_RPC,
7230 N_("Manage registry hives"),
7231 N_("net rpc registry\n"
7232 " Manage registry hives")
7235 "shell",
7236 net_rpc_shell,
7237 NET_TRANSPORT_RPC,
7238 N_("Open interactive shell on remote server"),
7239 N_("net rpc shell\n"
7240 " Open interactive shell on remote server")
7242 {NULL, NULL, 0, NULL, NULL}
7245 status = libnetapi_init(&c->netapi_ctx);
7246 if (status != 0) {
7247 return -1;
7249 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
7250 libnetapi_set_password(c->netapi_ctx, c->opt_password);
7251 if (c->opt_kerberos) {
7252 libnetapi_set_use_kerberos(c->netapi_ctx);
7255 return net_run_function(c, argc, argv, "net rpc", func);