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/>. */
24 #include "utils/net.h"
25 #include "../libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/cli_samr.h"
27 #include "../librpc/gen_ndr/cli_lsa.h"
28 #include "../librpc/gen_ndr/cli_netlogon.h"
29 #include "../librpc/gen_ndr/cli_srvsvc.h"
30 #include "../librpc/gen_ndr/cli_spoolss.h"
31 #include "../librpc/gen_ndr/cli_initshutdown.h"
32 #include "../librpc/gen_ndr/cli_winreg.h"
34 static int net_mode_share;
35 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
40 * @brief RPC based subcommands for the 'net' utility.
42 * This file should contain much of the functionality that used to
43 * be found in rpcclient, execpt that the commands should change
44 * less often, and the fucntionality should be sane (the user is not
45 * expected to know a rid/sid before they conduct an operation etc.)
47 * @todo Perhaps eventually these should be split out into a number
48 * of files, as this could get quite big.
53 * Many of the RPC functions need the domain sid. This function gets
54 * it at the start of every run
56 * @param cli A cli_state already connected to the remote machine
58 * @return The Domain SID of the remote machine.
61 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
63 const char **domain_name)
65 struct rpc_pipe_client *lsa_pipe = NULL;
66 struct policy_handle pol;
67 NTSTATUS result = NT_STATUS_OK;
68 union lsa_PolicyInformation *info = NULL;
70 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
72 if (!NT_STATUS_IS_OK(result)) {
73 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
77 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
78 SEC_FLAG_MAXIMUM_ALLOWED,
80 if (!NT_STATUS_IS_OK(result)) {
81 d_fprintf(stderr, "open_policy ",_("failed"),": %s\n"),
86 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
88 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
90 if (!NT_STATUS_IS_OK(result)) {
91 d_fprintf(stderr, "lsaquery ",_("failed"),": %s\n"),
96 *domain_name = info->account_domain.name.string;
97 *domain_sid = info->account_domain.sid;
99 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
100 TALLOC_FREE(lsa_pipe);
106 * Run a single RPC command, from start to finish.
108 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
109 * @param conn_flag a NET_FLAG_ combination. Passed to
110 * net_make_ipc_connection.
111 * @param argc Standard main() style argc.
112 * @param argv Standard main() style argv. Initial components are already
114 * @return A shell status integer (0 for success).
117 int run_rpc_command(struct net_context *c,
118 struct cli_state *cli_arg,
119 const struct ndr_syntax_id *interface,
125 struct cli_state *cli = NULL;
126 struct rpc_pipe_client *pipe_hnd = NULL;
130 const char *domain_name;
133 /* make use of cli_state handed over as an argument, if possible */
135 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
136 if (!NT_STATUS_IS_OK(nt_status)) {
137 DEBUG(1, ("failed to make ipc connection: %s\n",
138 nt_errstr(nt_status)));
151 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
152 DEBUG(0, ("talloc_init() failed\n"));
156 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
158 if (!NT_STATUS_IS_OK(nt_status)) {
162 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
163 if (lp_client_schannel()
164 && (ndr_syntax_id_equal(interface,
165 &ndr_table_netlogon.syntax_id))) {
166 /* Always try and create an schannel netlogon pipe. */
167 nt_status = cli_rpc_pipe_open_schannel(
168 cli, interface, NCACN_NP,
169 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
171 if (!NT_STATUS_IS_OK(nt_status)) {
172 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
173 nt_errstr(nt_status) ));
177 if (conn_flags & NET_FLAGS_SEAL) {
178 nt_status = cli_rpc_pipe_open_ntlmssp(
180 (conn_flags & NET_FLAGS_TCP) ?
181 NCACN_IP_TCP : NCACN_NP,
182 DCERPC_AUTH_LEVEL_PRIVACY,
183 lp_workgroup(), c->opt_user_name,
184 c->opt_password, &pipe_hnd);
186 nt_status = cli_rpc_pipe_open_noauth(
190 if (!NT_STATUS_IS_OK(nt_status)) {
191 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
192 get_pipe_name_from_syntax(
193 talloc_tos(), interface),
194 nt_errstr(nt_status) ));
200 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
202 if (!NT_STATUS_IS_OK(nt_status)) {
203 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
206 DEBUG(5, ("rpc command function succedded\n"));
209 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
211 TALLOC_FREE(pipe_hnd);
216 /* close the connection only if it was opened here */
221 talloc_destroy(mem_ctx);
226 * Force a change of the trust acccount password.
228 * All parameters are provided by the run_rpc_command function, except for
229 * argc, argv which are passed through.
231 * @param domain_sid The domain sid acquired from the remote server.
232 * @param cli A cli_state connected to the server.
233 * @param mem_ctx Talloc context, destroyed on completion of the function.
234 * @param argc Standard main() style argc.
235 * @param argv Standard main() style argv. Initial components are already
238 * @return Normal NTSTATUS return.
241 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
242 const DOM_SID *domain_sid,
243 const char *domain_name,
244 struct cli_state *cli,
245 struct rpc_pipe_client *pipe_hnd,
252 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
253 if (!NT_STATUS_IS_OK(status)) {
254 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
263 * Force a change of the trust acccount password.
265 * @param argc Standard main() style argc.
266 * @param argv Standard main() style argv. Initial components are already
269 * @return A shell status integer (0 for success).
272 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
274 if (c->display_usage) {
275 d_printf(_("Usage:\n"),
276 "net rpc changetrustpw\n"
277 " ",_("Change the machine trust password\n"));
281 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
282 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
283 rpc_changetrustpw_internals,
288 * Join a domain, the old way.
290 * This uses 'machinename' as the inital password, and changes it.
292 * The password should be created with 'server manager' or equiv first.
294 * All parameters are provided by the run_rpc_command function, except for
295 * argc, argv which are passed through.
297 * @param domain_sid The domain sid acquired from the remote server.
298 * @param cli A cli_state connected to the server.
299 * @param mem_ctx Talloc context, destroyed on completion of the function.
300 * @param argc Standard main() style argc.
301 * @param argv Standard main() style argv. Initial components are already
304 * @return Normal NTSTATUS return.
307 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
308 const DOM_SID *domain_sid,
309 const char *domain_name,
310 struct cli_state *cli,
311 struct rpc_pipe_client *pipe_hnd,
317 fstring trust_passwd;
318 unsigned char orig_trust_passwd_hash[16];
320 enum netr_SchannelType sec_channel_type;
322 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
324 if (!NT_STATUS_IS_OK(result)) {
325 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
328 nt_errstr(result) ));
333 check what type of join - if the user want's to join as
334 a BDC, the server must agree that we are a BDC.
337 sec_channel_type = get_sec_channel_type(argv[0]);
339 sec_channel_type = get_sec_channel_type(NULL);
342 fstrcpy(trust_passwd, global_myname());
343 strlower_m(trust_passwd);
346 * Machine names can be 15 characters, but the max length on
347 * a password is 14. --jerry
350 trust_passwd[14] = '\0';
352 E_md4hash(trust_passwd, orig_trust_passwd_hash);
354 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
356 orig_trust_passwd_hash,
359 if (NT_STATUS_IS_OK(result))
360 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
363 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
364 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
365 result = NT_STATUS_UNSUCCESSFUL;
372 * Join a domain, the old way.
374 * @param argc Standard main() style argc.
375 * @param argv Standard main() style argv. Initial components are already
378 * @return A shell status integer (0 for success).
381 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
383 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
384 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
385 rpc_oldjoin_internals,
390 * Join a domain, the old way. This function exists to allow
391 * the message to be displayed when oldjoin was explicitly
392 * requested, but not when it was implied by "net rpc join".
394 * @param argc Standard main() style argc.
395 * @param argv Standard main() style argv. Initial components are already
398 * @return A shell status integer (0 for success).
401 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
405 if (c->display_usage) {
406 d_printf(_("Usage:\n"),
408 " ",_("Join a domain the old way\n"));
412 rc = net_rpc_perform_oldjoin(c, argc, argv);
415 d_fprintf(stderr, _("Failed to join domain\n"));
422 * 'net rpc join' entrypoint.
423 * @param argc Standard main() style argc.
424 * @param argv Standard main() style argv. Initial components are already
427 * Main 'net_rpc_join()' (where the admin username/password is used) is
429 * Try to just change the password, but if that doesn't work, use/prompt
430 * for a username/password.
433 int net_rpc_join(struct net_context *c, int argc, const char **argv)
435 if (c->display_usage) {
436 d_printf(_("Usage:\n"),
437 _("net rpc join -U <username>[%%password] <type>\n"
439 " username\tName of the admin user"
440 " password\tPassword of the admin user, will "
441 "prompt if not specified\n"
442 " type\tCan be one of the following:\n"
443 "\t\tMEMBER\tJoin as member server (default)\n"
444 "\t\tBDC\tJoin as BDC\n"
445 "\t\tPDC\tJoin as PDC\n"));
449 if (lp_server_role() == ROLE_STANDALONE) {
450 d_printf(_("cannot join as standalone machine\n"));
454 if (strlen(global_myname()) > 15) {
455 d_printf(_("Our netbios name can be at most 15 chars long, "
456 "\"%s\" is %u chars long\n"),
457 global_myname(), (unsigned int)strlen(global_myname()));
461 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
464 return net_rpc_join_newstyle(c, argc, argv);
468 * display info about a rpc domain
470 * All parameters are provided by the run_rpc_command function, except for
471 * argc, argv which are passed through.
473 * @param domain_sid The domain sid acquired from the remote server
474 * @param cli A cli_state connected to the server.
475 * @param mem_ctx Talloc context, destroyed on completion of the function.
476 * @param argc Standard main() style argc.
477 * @param argv Standard main() style argv. Initial components are already
480 * @return Normal NTSTATUS return.
483 NTSTATUS rpc_info_internals(struct net_context *c,
484 const DOM_SID *domain_sid,
485 const char *domain_name,
486 struct cli_state *cli,
487 struct rpc_pipe_client *pipe_hnd,
492 struct policy_handle connect_pol, domain_pol;
493 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
494 union samr_DomainInfo *info = NULL;
497 sid_to_fstring(sid_str, domain_sid);
499 /* Get sam policy handle */
500 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
502 MAXIMUM_ALLOWED_ACCESS,
504 if (!NT_STATUS_IS_OK(result)) {
505 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
510 /* Get domain policy handle */
511 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
513 MAXIMUM_ALLOWED_ACCESS,
514 CONST_DISCARD(struct dom_sid2 *, domain_sid),
516 if (!NT_STATUS_IS_OK(result)) {
517 d_fprintf(stderr, _("Could not open domain: %s\n"),
522 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
526 if (NT_STATUS_IS_OK(result)) {
527 d_printf(_("Domain Name: %s\n"),
528 info->general.domain_name.string);
529 d_printf(_("Domain SID: %s\n"), sid_str);
530 d_printf(_("Sequence number: %llu\n"),
531 (unsigned long long)info->general.sequence_num);
532 d_printf(_("Num users: %u\n"), info->general.num_users);
533 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
534 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
542 * 'net rpc info' entrypoint.
543 * @param argc Standard main() style argc.
544 * @param argv Standard main() style argv. Initial components are already
548 int net_rpc_info(struct net_context *c, int argc, const char **argv)
550 if (c->display_usage) {
551 d_printf(_("Usage:\n"),
553 " ",_("Display information about the domain\n"));
557 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
558 NET_FLAGS_PDC, rpc_info_internals,
563 * Fetch domain SID into the local secrets.tdb.
565 * All parameters are provided by the run_rpc_command function, except for
566 * argc, argv which are passed through.
568 * @param domain_sid The domain sid acquired from the remote server.
569 * @param cli A cli_state connected to the server.
570 * @param mem_ctx Talloc context, destroyed on completion of the function.
571 * @param argc Standard main() style argc.
572 * @param argv Standard main() style argv. Initial components are already
575 * @return Normal NTSTATUS return.
578 static NTSTATUS rpc_getsid_internals(struct net_context *c,
579 const DOM_SID *domain_sid,
580 const char *domain_name,
581 struct cli_state *cli,
582 struct rpc_pipe_client *pipe_hnd,
589 sid_to_fstring(sid_str, domain_sid);
590 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
591 sid_str, domain_name);
593 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
594 DEBUG(0,("Can't store domain SID\n"));
595 return NT_STATUS_UNSUCCESSFUL;
602 * 'net rpc getsid' entrypoint.
603 * @param argc Standard main() style argc.
604 * @param argv Standard main() style argv. Initial components are already
608 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
610 int conn_flags = NET_FLAGS_PDC;
612 if (!c->opt_user_specified) {
613 conn_flags |= NET_FLAGS_ANONYMOUS;
616 if (c->display_usage) {
617 d_printf(_("Usage:\n"),
619 " ",_("Fetch domain SID into local secrets.tdb\n"));
623 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
625 rpc_getsid_internals,
629 /****************************************************************************/
632 * Basic usage function for 'net rpc user'.
633 * @param argc Standard main() style argc.
634 * @param argv Standard main() style argv. Initial components are already
638 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
640 return net_user_usage(c, argc, argv);
644 * Add a new user to a remote RPC server.
646 * @param argc Standard main() style argc.
647 * @param argv Standard main() style argv. Initial components are already
650 * @return A shell status integer (0 for success).
653 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
655 NET_API_STATUS status;
656 struct USER_INFO_1 info1;
657 uint32_t parm_error = 0;
659 if (argc < 1 || c->display_usage) {
660 rpc_user_usage(c, argc, argv);
666 info1.usri1_name = argv[0];
668 info1.usri1_password = argv[1];
671 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
674 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
675 argv[0], libnetapi_get_error_string(c->netapi_ctx,
679 d_printf(_("Added user '%s'.\n"), argv[0]);
686 * Rename a user on a remote RPC server.
688 * @param argc Standard main() style argc.
689 * @param argv Standard main() style argv. Initial components are already
692 * @return A shell status integer (0 for success).
695 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
697 NET_API_STATUS status;
698 struct USER_INFO_0 u0;
699 uint32_t parm_err = 0;
701 if (argc != 2 || c->display_usage) {
702 rpc_user_usage(c, argc, argv);
706 u0.usri0_name = argv[1];
708 status = NetUserSetInfo(c->opt_host, argv[0],
709 0, (uint8_t *)&u0, &parm_err);
712 _("Failed to rename user from %s to %s - %s\n"),
714 libnetapi_get_error_string(c->netapi_ctx, status));
716 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
723 * Delete a user from a remote RPC server.
725 * @param argc Standard main() style argc.
726 * @param argv Standard main() style argv. Initial components are already
729 * @return A shell status integer (0 for success).
732 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
734 NET_API_STATUS status;
736 if (argc < 1 || c->display_usage) {
737 rpc_user_usage(c, argc, argv);
741 status = NetUserDel(c->opt_host, argv[0]);
744 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
746 libnetapi_get_error_string(c->netapi_ctx, status));
749 d_printf(_("Deleted user '%s'.\n"), argv[0]);
756 * Set a user's password on a remote RPC server.
758 * @param argc Standard main() style argc.
759 * @param argv Standard main() style argv. Initial components are already
762 * @return A shell status integer (0 for success).
765 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
767 NET_API_STATUS status;
769 struct USER_INFO_1003 u1003;
770 uint32_t parm_err = 0;
773 if (argc < 1 || c->display_usage) {
774 rpc_user_usage(c, argc, argv);
779 u1003.usri1003_password = argv[1];
781 ret = asprintf(&prompt, _("Enter new password for %s:"),
786 u1003.usri1003_password = getpass(prompt);
790 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
792 /* Display results */
795 _("Failed to set password for '%s' with error: %s.\n"),
796 argv[0], libnetapi_get_error_string(c->netapi_ctx,
805 * List a user's groups from a remote RPC server.
807 * @param argc Standard main() style argc.
808 * @param argv Standard main() style argv. Initial components are already
811 * @return A shell status integer (0 for success)
814 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
817 NET_API_STATUS status;
818 struct GROUP_USERS_INFO_0 *u0 = NULL;
819 uint32_t entries_read = 0;
820 uint32_t total_entries = 0;
824 if (argc < 1 || c->display_usage) {
825 rpc_user_usage(c, argc, argv);
829 status = NetUserGetGroups(c->opt_host,
832 (uint8_t **)(void *)&u0,
838 _("Failed to get groups for '%s' with error: %s.\n"),
839 argv[0], libnetapi_get_error_string(c->netapi_ctx,
844 for (i=0; i < entries_read; i++) {
845 printf("%s\n", u0->grui0_name);
853 * List users on a remote RPC server.
855 * All parameters are provided by the run_rpc_command function, except for
856 * argc, argv which are passed through.
858 * @param domain_sid The domain sid acquired from the remote server.
859 * @param cli A cli_state connected to the server.
860 * @param mem_ctx Talloc context, destroyed on completion of the function.
861 * @param argc Standard main() style argc.
862 * @param argv Standard main() style argv. Initial components are already
865 * @return Normal NTSTATUS return.
868 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
870 NET_API_STATUS status;
871 uint32_t start_idx=0, num_entries, i, loop_count = 0;
872 struct NET_DISPLAY_USER *info = NULL;
875 /* Query domain users */
876 if (c->opt_long_list_entries)
877 d_printf(_("\nUser name Comment"
878 "\n-----------------------------\n"));
880 uint32_t max_entries, max_size;
882 get_query_dispinfo_params(
883 loop_count, &max_entries, &max_size);
885 status = NetQueryDisplayInformation(c->opt_host,
892 if (status != 0 && status != ERROR_MORE_DATA) {
896 info = (struct NET_DISPLAY_USER *)buffer;
898 for (i = 0; i < num_entries; i++) {
900 if (c->opt_long_list_entries)
901 printf("%-21.21s %s\n", info->usri1_name,
902 info->usri1_comment);
904 printf("%s\n", info->usri1_name);
908 NetApiBufferFree(buffer);
911 start_idx += num_entries;
913 } while (status == ERROR_MORE_DATA);
919 * 'net rpc user' entrypoint.
920 * @param argc Standard main() style argc.
921 * @param argv Standard main() style argv. Initial components are already
925 int net_rpc_user(struct net_context *c, int argc, const char **argv)
927 NET_API_STATUS status;
929 struct functable func[] = {
934 N_("Add specified user"),
935 N_("net rpc user add\n"
936 " Add specified user")
942 N_("List domain groups of user"),
943 N_("net rpc user info\n"
944 " Lis domain groups of user")
950 N_("Remove specified user"),
951 N_("net rpc user delete\n"
952 " Remove specified user")
958 N_("Change user password"),
959 N_("net rpc user password\n"
960 " Change user password")
966 N_("Rename specified user"),
967 N_("net rpc user rename\n"
968 " Rename specified user")
970 {NULL, NULL, 0, NULL, NULL}
973 status = libnetapi_init(&c->netapi_ctx);
977 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
978 libnetapi_set_password(c->netapi_ctx, c->opt_password);
979 if (c->opt_kerberos) {
980 libnetapi_set_use_kerberos(c->netapi_ctx);
984 if (c->display_usage) {
985 d_printf(_("Usage:\n"),
987 " ",_("List all users\n"));
988 net_display_usage_from_functable(func);
992 return rpc_user_list(c, argc, argv);
995 return net_run_function(c, argc, argv, "net rpc user", func);
998 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1000 struct rpc_sh_ctx *ctx,
1001 struct rpc_pipe_client *pipe_hnd,
1002 int argc, const char **argv)
1004 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1007 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1008 TALLOC_CTX *mem_ctx,
1009 struct rpc_sh_ctx *ctx,
1010 struct rpc_pipe_client *pipe_hnd,
1011 int argc, const char **argv)
1013 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1016 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1017 TALLOC_CTX *mem_ctx,
1018 struct rpc_sh_ctx *ctx,
1019 struct rpc_pipe_client *pipe_hnd,
1020 int argc, const char **argv,
1022 struct net_context *c,
1023 TALLOC_CTX *mem_ctx,
1024 struct rpc_sh_ctx *ctx,
1025 struct rpc_pipe_client *pipe_hnd,
1026 struct policy_handle *user_hnd,
1027 int argc, const char **argv))
1029 struct policy_handle connect_pol, domain_pol, user_pol;
1030 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1033 enum lsa_SidType type;
1036 d_fprintf(stderr, _("Usage:")," %s <username>\n"), ctx->whoami;
1037 return NT_STATUS_INVALID_PARAMETER;
1040 ZERO_STRUCT(connect_pol);
1041 ZERO_STRUCT(domain_pol);
1042 ZERO_STRUCT(user_pol);
1044 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1045 argv[0], NULL, NULL, &sid, &type);
1046 if (!NT_STATUS_IS_OK(result)) {
1047 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1052 if (type != SID_NAME_USER) {
1053 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1054 sid_type_lookup(type));
1055 result = NT_STATUS_NO_SUCH_USER;
1059 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1060 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1061 result = NT_STATUS_NO_SUCH_USER;
1065 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1067 MAXIMUM_ALLOWED_ACCESS,
1069 if (!NT_STATUS_IS_OK(result)) {
1073 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1075 MAXIMUM_ALLOWED_ACCESS,
1078 if (!NT_STATUS_IS_OK(result)) {
1082 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1084 MAXIMUM_ALLOWED_ACCESS,
1087 if (!NT_STATUS_IS_OK(result)) {
1091 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1094 if (is_valid_policy_hnd(&user_pol)) {
1095 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1097 if (is_valid_policy_hnd(&domain_pol)) {
1098 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1100 if (is_valid_policy_hnd(&connect_pol)) {
1101 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1106 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1107 TALLOC_CTX *mem_ctx,
1108 struct rpc_sh_ctx *ctx,
1109 struct rpc_pipe_client *pipe_hnd,
1110 struct policy_handle *user_hnd,
1111 int argc, const char **argv)
1114 union samr_UserInfo *info = NULL;
1117 d_fprintf(stderr, _("Usage:")," %s show <username>\n"),ctx->whoami;
1118 return NT_STATUS_INVALID_PARAMETER;
1121 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1125 if (!NT_STATUS_IS_OK(result)) {
1129 d_printf(_("user rid: %d, group rid: %d\n"),
1131 info->info21.primary_gid);
1136 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1137 TALLOC_CTX *mem_ctx,
1138 struct rpc_sh_ctx *ctx,
1139 struct rpc_pipe_client *pipe_hnd,
1140 int argc, const char **argv)
1142 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1143 rpc_sh_user_show_internals);
1146 #define FETCHSTR(name, rec) \
1147 do { if (strequal(ctx->thiscmd, name)) { \
1148 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1151 #define SETSTR(name, rec, flag) \
1152 do { if (strequal(ctx->thiscmd, name)) { \
1153 init_lsa_String(&(info->info21.rec), argv[0]); \
1154 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1157 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1158 TALLOC_CTX *mem_ctx,
1159 struct rpc_sh_ctx *ctx,
1160 struct rpc_pipe_client *pipe_hnd,
1161 struct policy_handle *user_hnd,
1162 int argc, const char **argv)
1165 const char *username;
1166 const char *oldval = "";
1167 union samr_UserInfo *info = NULL;
1170 d_fprintf(stderr, _("Usage:")," %s <username> [new value|NULL]\n",
1172 return NT_STATUS_INVALID_PARAMETER;
1175 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1179 if (!NT_STATUS_IS_OK(result)) {
1183 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1185 FETCHSTR("fullname", full_name);
1186 FETCHSTR("homedir", home_directory);
1187 FETCHSTR("homedrive", home_drive);
1188 FETCHSTR("logonscript", logon_script);
1189 FETCHSTR("profilepath", profile_path);
1190 FETCHSTR("description", description);
1193 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1197 if (strcmp(argv[0], "NULL") == 0) {
1201 ZERO_STRUCT(info->info21);
1203 SETSTR("fullname", full_name, FULL_NAME);
1204 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1205 SETSTR("homedrive", home_drive, HOME_DRIVE);
1206 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1207 SETSTR("profilepath", profile_path, PROFILE_PATH);
1208 SETSTR("description", description, DESCRIPTION);
1210 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1215 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1216 ctx->thiscmd, oldval, argv[0]);
1223 #define HANDLEFLG(name, rec) \
1224 do { if (strequal(ctx->thiscmd, name)) { \
1225 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1227 newflags = oldflags | ACB_##rec; \
1229 newflags = oldflags & ~ACB_##rec; \
1232 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1233 TALLOC_CTX *mem_ctx,
1234 struct rpc_sh_ctx *ctx,
1235 struct rpc_pipe_client *pipe_hnd,
1236 int argc, const char **argv)
1238 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1239 rpc_sh_user_str_edit_internals);
1242 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1243 TALLOC_CTX *mem_ctx,
1244 struct rpc_sh_ctx *ctx,
1245 struct rpc_pipe_client *pipe_hnd,
1246 struct policy_handle *user_hnd,
1247 int argc, const char **argv)
1250 const char *username;
1251 const char *oldval = "unknown";
1252 uint32 oldflags, newflags;
1254 union samr_UserInfo *info = NULL;
1257 ((argc == 1) && !strequal(argv[0], "yes") &&
1258 !strequal(argv[0], "no"))) {
1259 /* TRANSATORS: The yes|no here are program keywords. Please do
1261 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1263 return NT_STATUS_INVALID_PARAMETER;
1266 newval = strequal(argv[0], "yes");
1268 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1272 if (!NT_STATUS_IS_OK(result)) {
1276 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1277 oldflags = info->info21.acct_flags;
1278 newflags = info->info21.acct_flags;
1280 HANDLEFLG("disabled", DISABLED);
1281 HANDLEFLG("pwnotreq", PWNOTREQ);
1282 HANDLEFLG("autolock", AUTOLOCK);
1283 HANDLEFLG("pwnoexp", PWNOEXP);
1286 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1291 ZERO_STRUCT(info->info21);
1293 info->info21.acct_flags = newflags;
1294 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1296 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1301 if (NT_STATUS_IS_OK(result)) {
1302 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1303 ctx->thiscmd, oldval, argv[0]);
1311 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1312 TALLOC_CTX *mem_ctx,
1313 struct rpc_sh_ctx *ctx,
1314 struct rpc_pipe_client *pipe_hnd,
1315 int argc, const char **argv)
1317 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1318 rpc_sh_user_flag_edit_internals);
1321 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1322 TALLOC_CTX *mem_ctx,
1323 struct rpc_sh_ctx *ctx)
1325 static struct rpc_sh_cmd cmds[] = {
1327 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1328 N_("Show/Set a user's full name") },
1330 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1331 N_("Show/Set a user's home directory") },
1333 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1334 N_("Show/Set a user's home drive") },
1336 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1337 N_("Show/Set a user's logon script") },
1339 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1340 N_("Show/Set a user's profile path") },
1342 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1343 N_("Show/Set a user's description") },
1345 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1346 N_("Show/Set whether a user is disabled") },
1348 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1349 N_("Show/Set whether a user locked out") },
1351 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1352 N_("Show/Set whether a user does not need a password") },
1354 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1355 N_("Show/Set whether a user's password does not expire") },
1357 { NULL, NULL, 0, NULL, NULL }
1363 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1364 TALLOC_CTX *mem_ctx,
1365 struct rpc_sh_ctx *ctx)
1367 static struct rpc_sh_cmd cmds[] = {
1369 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1370 N_("List available users") },
1372 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1373 N_("List the domain groups a user is member of") },
1375 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1376 N_("Show info about a user") },
1378 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1379 N_("Show/Modify a user's fields") },
1381 { NULL, NULL, 0, NULL, NULL }
1387 /****************************************************************************/
1390 * Basic usage function for 'net rpc group'.
1391 * @param argc Standard main() style argc.
1392 * @param argv Standard main() style argv. Initial components are already
1396 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1398 return net_group_usage(c, argc, argv);
1402 * Delete group on a remote RPC server.
1404 * All parameters are provided by the run_rpc_command function, except for
1405 * argc, argv which are passed through.
1407 * @param domain_sid The domain sid acquired from the remote server.
1408 * @param cli A cli_state connected to the server.
1409 * @param mem_ctx Talloc context, destroyed on completion of the function.
1410 * @param argc Standard main() style argc.
1411 * @param argv Standard main() style argv. Initial components are already
1414 * @return Normal NTSTATUS return.
1417 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1418 const DOM_SID *domain_sid,
1419 const char *domain_name,
1420 struct cli_state *cli,
1421 struct rpc_pipe_client *pipe_hnd,
1422 TALLOC_CTX *mem_ctx,
1426 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1427 bool group_is_primary = false;
1428 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1430 struct samr_RidTypeArray *rids = NULL;
1433 /* struct samr_RidWithAttribute *user_gids; */
1435 struct samr_Ids group_rids, name_types;
1436 struct lsa_String lsa_acct_name;
1437 union samr_UserInfo *info = NULL;
1439 if (argc < 1 || c->display_usage) {
1440 rpc_group_usage(c, argc,argv);
1441 return NT_STATUS_OK; /* ok? */
1444 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1446 MAXIMUM_ALLOWED_ACCESS,
1449 if (!NT_STATUS_IS_OK(result)) {
1450 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1454 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1456 MAXIMUM_ALLOWED_ACCESS,
1457 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1460 if (!NT_STATUS_IS_OK(result)) {
1461 d_fprintf(stderr, _("Request open_domain failed\n"));
1465 init_lsa_String(&lsa_acct_name, argv[0]);
1467 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1473 if (!NT_STATUS_IS_OK(result)) {
1474 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1478 switch (name_types.ids[0])
1480 case SID_NAME_DOM_GRP:
1481 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1483 MAXIMUM_ALLOWED_ACCESS,
1486 if (!NT_STATUS_IS_OK(result)) {
1487 d_fprintf(stderr, _("Request open_group failed"));
1491 group_rid = group_rids.ids[0];
1493 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1497 if (!NT_STATUS_IS_OK(result)) {
1499 _("Unable to query group members of %s"),
1504 if (c->opt_verbose) {
1506 _("Domain Group %s (rid: %d) has %d members\n"),
1507 argv[0],group_rid, rids->count);
1510 /* Check if group is anyone's primary group */
1511 for (i = 0; i < rids->count; i++)
1513 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1515 MAXIMUM_ALLOWED_ACCESS,
1519 if (!NT_STATUS_IS_OK(result)) {
1521 _("Unable to open group member %d\n"),
1526 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1531 if (!NT_STATUS_IS_OK(result)) {
1533 _("Unable to lookup userinfo for group "
1539 if (info->info21.primary_gid == group_rid) {
1540 if (c->opt_verbose) {
1541 d_printf(_("Group is primary group "
1543 info->info21.account_name.string);
1545 group_is_primary = true;
1548 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1551 if (group_is_primary) {
1552 d_fprintf(stderr, _("Unable to delete group because "
1553 "some of it's members have it as primary "
1555 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1559 /* remove all group members */
1560 for (i = 0; i < rids->count; i++)
1563 d_printf(_("Remove group member %d..."),
1565 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1569 if (NT_STATUS_IS_OK(result)) {
1571 d_printf(_("ok\n"));
1574 d_printf(_("failed"),"\n");
1579 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1583 /* removing a local group is easier... */
1584 case SID_NAME_ALIAS:
1585 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1587 MAXIMUM_ALLOWED_ACCESS,
1591 if (!NT_STATUS_IS_OK(result)) {
1592 d_fprintf(stderr, _("Request open_alias failed\n"));
1596 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1600 d_fprintf(stderr, _("%s is of type %s. This command is only "
1601 "for deleting local or global groups\n"),
1602 argv[0],sid_type_lookup(name_types.ids[0]));
1603 result = NT_STATUS_UNSUCCESSFUL;
1607 if (NT_STATUS_IS_OK(result)) {
1609 d_printf(_("Deleted %s '%s'\n"),
1610 sid_type_lookup(name_types.ids[0]), argv[0]);
1612 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1613 get_friendly_nt_error_msg(result));
1621 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1623 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1624 rpc_group_delete_internals, argc,argv);
1627 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1629 NET_API_STATUS status;
1630 struct GROUP_INFO_1 info1;
1631 uint32_t parm_error = 0;
1633 if (argc != 1 || c->display_usage) {
1634 rpc_group_usage(c, argc, argv);
1640 info1.grpi1_name = argv[0];
1641 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1642 info1.grpi1_comment = c->opt_comment;
1645 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1649 _("Failed to add group '%s' with error: %s.\n"),
1650 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1654 d_printf(_("Added group '%s'.\n"), argv[0]);
1660 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1662 NET_API_STATUS status;
1663 struct LOCALGROUP_INFO_1 info1;
1664 uint32_t parm_error = 0;
1666 if (argc != 1 || c->display_usage) {
1667 rpc_group_usage(c, argc, argv);
1673 info1.lgrpi1_name = argv[0];
1674 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1675 info1.lgrpi1_comment = c->opt_comment;
1678 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1682 _("Failed to add alias '%s' with error: %s.\n"),
1683 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1687 d_printf(_("Added alias '%s'.\n"), argv[0]);
1693 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1695 if (c->opt_localgroup)
1696 return rpc_alias_add_internals(c, argc, argv);
1698 return rpc_group_add_internals(c, argc, argv);
1701 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1702 TALLOC_CTX *mem_ctx,
1705 enum lsa_SidType *type)
1707 DOM_SID *sids = NULL;
1708 enum lsa_SidType *types = NULL;
1709 struct rpc_pipe_client *pipe_hnd = NULL;
1710 struct policy_handle lsa_pol;
1711 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1713 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1715 if (!NT_STATUS_IS_OK(result)) {
1719 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1720 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1722 if (!NT_STATUS_IS_OK(result)) {
1726 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1727 &name, NULL, 1, &sids, &types);
1729 if (NT_STATUS_IS_OK(result)) {
1730 sid_copy(sid, &sids[0]);
1734 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1738 TALLOC_FREE(pipe_hnd);
1741 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1743 /* Try as S-1-5-whatever */
1747 if (string_to_sid(&tmp_sid, name)) {
1748 sid_copy(sid, &tmp_sid);
1749 *type = SID_NAME_UNKNOWN;
1750 result = NT_STATUS_OK;
1757 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1758 TALLOC_CTX *mem_ctx,
1759 const DOM_SID *group_sid,
1762 struct policy_handle connect_pol, domain_pol;
1765 struct policy_handle group_pol;
1767 struct samr_Ids rids, rid_types;
1768 struct lsa_String lsa_acct_name;
1772 sid_copy(&sid, group_sid);
1774 if (!sid_split_rid(&sid, &group_rid)) {
1775 return NT_STATUS_UNSUCCESSFUL;
1778 /* Get sam policy handle */
1779 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1781 MAXIMUM_ALLOWED_ACCESS,
1783 if (!NT_STATUS_IS_OK(result)) {
1787 /* Get domain policy handle */
1788 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1790 MAXIMUM_ALLOWED_ACCESS,
1793 if (!NT_STATUS_IS_OK(result)) {
1797 init_lsa_String(&lsa_acct_name, member);
1799 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1806 if (!NT_STATUS_IS_OK(result)) {
1807 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1812 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1814 MAXIMUM_ALLOWED_ACCESS,
1818 if (!NT_STATUS_IS_OK(result)) {
1822 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1825 0x0005); /* unknown flags */
1828 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1832 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1833 TALLOC_CTX *mem_ctx,
1834 const DOM_SID *alias_sid,
1837 struct policy_handle connect_pol, domain_pol;
1840 struct policy_handle alias_pol;
1843 enum lsa_SidType member_type;
1847 sid_copy(&sid, alias_sid);
1849 if (!sid_split_rid(&sid, &alias_rid)) {
1850 return NT_STATUS_UNSUCCESSFUL;
1853 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1854 member, &member_sid, &member_type);
1856 if (!NT_STATUS_IS_OK(result)) {
1857 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1862 /* Get sam policy handle */
1863 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1865 MAXIMUM_ALLOWED_ACCESS,
1867 if (!NT_STATUS_IS_OK(result)) {
1871 /* Get domain policy handle */
1872 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1874 MAXIMUM_ALLOWED_ACCESS,
1877 if (!NT_STATUS_IS_OK(result)) {
1881 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1883 MAXIMUM_ALLOWED_ACCESS,
1887 if (!NT_STATUS_IS_OK(result)) {
1891 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1895 if (!NT_STATUS_IS_OK(result)) {
1900 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1904 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1905 const DOM_SID *domain_sid,
1906 const char *domain_name,
1907 struct cli_state *cli,
1908 struct rpc_pipe_client *pipe_hnd,
1909 TALLOC_CTX *mem_ctx,
1914 enum lsa_SidType group_type;
1916 if (argc != 2 || c->display_usage) {
1917 d_printf(_("Usage:\n"),
1918 _("net rpc group addmem <group> <member>\n"
1919 " Add a member to a group\n"
1920 " group\tGroup to add member to\n"
1921 " member\tMember to add to group\n"));
1922 return NT_STATUS_UNSUCCESSFUL;
1925 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1926 &group_sid, &group_type))) {
1927 d_fprintf(stderr, _("Could not lookup group name %s\n"),
1929 return NT_STATUS_UNSUCCESSFUL;
1932 if (group_type == SID_NAME_DOM_GRP) {
1933 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
1934 &group_sid, argv[1]);
1936 if (!NT_STATUS_IS_OK(result)) {
1937 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
1938 argv[1], argv[0], nt_errstr(result));
1943 if (group_type == SID_NAME_ALIAS) {
1944 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
1945 &group_sid, argv[1]);
1947 if (!NT_STATUS_IS_OK(result)) {
1948 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
1949 argv[1], argv[0], nt_errstr(result));
1954 d_fprintf(stderr, _("Can only add members to global or local groups "
1955 "which %s is not\n"), argv[0]);
1957 return NT_STATUS_UNSUCCESSFUL;
1960 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
1962 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1963 rpc_group_addmem_internals,
1967 static NTSTATUS rpc_del_groupmem(struct net_context *c,
1968 struct rpc_pipe_client *pipe_hnd,
1969 TALLOC_CTX *mem_ctx,
1970 const DOM_SID *group_sid,
1973 struct policy_handle connect_pol, domain_pol;
1976 struct policy_handle group_pol;
1978 struct samr_Ids rids, rid_types;
1979 struct lsa_String lsa_acct_name;
1983 sid_copy(&sid, group_sid);
1985 if (!sid_split_rid(&sid, &group_rid))
1986 return NT_STATUS_UNSUCCESSFUL;
1988 /* Get sam policy handle */
1989 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1991 MAXIMUM_ALLOWED_ACCESS,
1993 if (!NT_STATUS_IS_OK(result))
1996 /* Get domain policy handle */
1997 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1999 MAXIMUM_ALLOWED_ACCESS,
2002 if (!NT_STATUS_IS_OK(result))
2005 init_lsa_String(&lsa_acct_name, member);
2007 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2013 if (!NT_STATUS_IS_OK(result)) {
2014 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2019 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2021 MAXIMUM_ALLOWED_ACCESS,
2025 if (!NT_STATUS_IS_OK(result))
2028 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2033 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2037 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2038 TALLOC_CTX *mem_ctx,
2039 const DOM_SID *alias_sid,
2042 struct policy_handle connect_pol, domain_pol;
2045 struct policy_handle alias_pol;
2048 enum lsa_SidType member_type;
2052 sid_copy(&sid, alias_sid);
2054 if (!sid_split_rid(&sid, &alias_rid))
2055 return NT_STATUS_UNSUCCESSFUL;
2057 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2058 member, &member_sid, &member_type);
2060 if (!NT_STATUS_IS_OK(result)) {
2061 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2066 /* Get sam policy handle */
2067 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2069 MAXIMUM_ALLOWED_ACCESS,
2071 if (!NT_STATUS_IS_OK(result)) {
2075 /* Get domain policy handle */
2076 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2078 MAXIMUM_ALLOWED_ACCESS,
2081 if (!NT_STATUS_IS_OK(result)) {
2085 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2087 MAXIMUM_ALLOWED_ACCESS,
2091 if (!NT_STATUS_IS_OK(result))
2094 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2098 if (!NT_STATUS_IS_OK(result))
2102 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2106 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2107 const DOM_SID *domain_sid,
2108 const char *domain_name,
2109 struct cli_state *cli,
2110 struct rpc_pipe_client *pipe_hnd,
2111 TALLOC_CTX *mem_ctx,
2116 enum lsa_SidType group_type;
2118 if (argc != 2 || c->display_usage) {
2119 d_printf(_("Usage:\n"),
2120 _("net rpc group delmem <group> <member>\n"
2121 " Delete a member from a group\n"
2122 " group\tGroup to delete member from\n"
2123 " member\tMember to delete from group\n"));
2124 return NT_STATUS_UNSUCCESSFUL;
2127 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2128 &group_sid, &group_type))) {
2129 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2131 return NT_STATUS_UNSUCCESSFUL;
2134 if (group_type == SID_NAME_DOM_GRP) {
2135 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2136 &group_sid, argv[1]);
2138 if (!NT_STATUS_IS_OK(result)) {
2139 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2140 argv[1], argv[0], nt_errstr(result));
2145 if (group_type == SID_NAME_ALIAS) {
2146 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2147 &group_sid, argv[1]);
2149 if (!NT_STATUS_IS_OK(result)) {
2150 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2151 argv[1], argv[0], nt_errstr(result));
2156 d_fprintf(stderr, _("Can only delete members from global or local "
2157 "groups which %s is not\n"), argv[0]);
2159 return NT_STATUS_UNSUCCESSFUL;
2162 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2164 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2165 rpc_group_delmem_internals,
2170 * List groups on a remote RPC server.
2172 * All parameters are provided by the run_rpc_command function, except for
2173 * argc, argv which are passes through.
2175 * @param domain_sid The domain sid acquired from the remote server.
2176 * @param cli A cli_state connected to the server.
2177 * @param mem_ctx Talloc context, destroyed on completion of the function.
2178 * @param argc Standard main() style argc.
2179 * @param argv Standard main() style argv. Initial components are already
2182 * @return Normal NTSTATUS return.
2185 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2186 const DOM_SID *domain_sid,
2187 const char *domain_name,
2188 struct cli_state *cli,
2189 struct rpc_pipe_client *pipe_hnd,
2190 TALLOC_CTX *mem_ctx,
2194 struct policy_handle connect_pol, domain_pol;
2195 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2196 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2197 struct samr_SamArray *groups = NULL;
2198 bool global = false;
2200 bool builtin = false;
2202 if (c->display_usage) {
2203 d_printf(_("Usage:\n"),
2204 _("net rpc group list [global] [local] [builtin]\n"
2205 " List groups on RPC server\n"
2206 " global\tList global groups\n"
2207 " local\tList local groups\n"
2208 " builtin\tList builtin groups\n"
2209 " If none of global, local or builtin is "
2210 "specified, all three options are considered "
2212 return NT_STATUS_OK;
2221 for (i=0; i<argc; i++) {
2222 if (strequal(argv[i], "global"))
2225 if (strequal(argv[i], "local"))
2228 if (strequal(argv[i], "builtin"))
2232 /* Get sam policy handle */
2234 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2236 MAXIMUM_ALLOWED_ACCESS,
2238 if (!NT_STATUS_IS_OK(result)) {
2242 /* Get domain policy handle */
2244 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2246 MAXIMUM_ALLOWED_ACCESS,
2247 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2249 if (!NT_STATUS_IS_OK(result)) {
2253 /* Query domain groups */
2254 if (c->opt_long_list_entries)
2255 d_printf(_("\nGroup name Comment"
2256 "\n-----------------------------\n"));
2258 uint32_t max_size, total_size, returned_size;
2259 union samr_DispInfo info;
2263 get_query_dispinfo_params(
2264 loop_count, &max_entries, &max_size);
2266 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2275 num_entries = info.info3.count;
2276 start_idx += info.info3.count;
2278 if (!NT_STATUS_IS_OK(result) &&
2279 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2282 for (i = 0; i < num_entries; i++) {
2284 const char *group = NULL;
2285 const char *desc = NULL;
2287 group = info.info3.entries[i].account_name.string;
2288 desc = info.info3.entries[i].description.string;
2290 if (c->opt_long_list_entries)
2291 printf("%-21.21s %-50.50s\n",
2294 printf("%s\n", group);
2296 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2297 /* query domain aliases */
2302 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2308 if (!NT_STATUS_IS_OK(result) &&
2309 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2312 for (i = 0; i < num_entries; i++) {
2314 const char *description = NULL;
2316 if (c->opt_long_list_entries) {
2318 struct policy_handle alias_pol;
2319 union samr_AliasInfo *info = NULL;
2321 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2324 groups->entries[i].idx,
2326 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2330 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2332 description = info->description.string;
2336 if (description != NULL) {
2337 printf("%-21.21s %-50.50s\n",
2338 groups->entries[i].name.string,
2341 printf("%s\n", groups->entries[i].name.string);
2344 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2345 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2346 /* Get builtin policy handle */
2348 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2350 MAXIMUM_ALLOWED_ACCESS,
2351 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2353 if (!NT_STATUS_IS_OK(result)) {
2356 /* query builtin aliases */
2359 if (!builtin) break;
2361 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2367 if (!NT_STATUS_IS_OK(result) &&
2368 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2371 for (i = 0; i < num_entries; i++) {
2373 const char *description = NULL;
2375 if (c->opt_long_list_entries) {
2377 struct policy_handle alias_pol;
2378 union samr_AliasInfo *info = NULL;
2380 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2383 groups->entries[i].idx,
2385 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2389 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2391 description = info->description.string;
2395 if (description != NULL) {
2396 printf("%-21.21s %-50.50s\n",
2397 groups->entries[i].name.string,
2400 printf("%s\n", groups->entries[i].name.string);
2403 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2409 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2411 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2412 rpc_group_list_internals,
2416 static NTSTATUS rpc_list_group_members(struct net_context *c,
2417 struct rpc_pipe_client *pipe_hnd,
2418 TALLOC_CTX *mem_ctx,
2419 const char *domain_name,
2420 const DOM_SID *domain_sid,
2421 struct policy_handle *domain_pol,
2425 struct policy_handle group_pol;
2426 uint32 num_members, *group_rids;
2428 struct samr_RidTypeArray *rids = NULL;
2429 struct lsa_Strings names;
2430 struct samr_Ids types;
2433 sid_to_fstring(sid_str, domain_sid);
2435 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2437 MAXIMUM_ALLOWED_ACCESS,
2441 if (!NT_STATUS_IS_OK(result))
2444 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2448 if (!NT_STATUS_IS_OK(result))
2451 num_members = rids->count;
2452 group_rids = rids->rids;
2454 while (num_members > 0) {
2455 int this_time = 512;
2457 if (num_members < this_time)
2458 this_time = num_members;
2460 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2467 if (!NT_STATUS_IS_OK(result))
2470 /* We only have users as members, but make the output
2471 the same as the output of alias members */
2473 for (i = 0; i < this_time; i++) {
2475 if (c->opt_long_list_entries) {
2476 printf("%s-%d %s\\%s %d\n", sid_str,
2477 group_rids[i], domain_name,
2478 names.names[i].string,
2481 printf("%s\\%s\n", domain_name,
2482 names.names[i].string);
2486 num_members -= this_time;
2490 return NT_STATUS_OK;
2493 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2494 struct rpc_pipe_client *pipe_hnd,
2495 TALLOC_CTX *mem_ctx,
2496 struct policy_handle *domain_pol,
2500 struct rpc_pipe_client *lsa_pipe;
2501 struct policy_handle alias_pol, lsa_pol;
2503 DOM_SID *alias_sids;
2506 enum lsa_SidType *types;
2508 struct lsa_SidArray sid_array;
2510 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2512 MAXIMUM_ALLOWED_ACCESS,
2516 if (!NT_STATUS_IS_OK(result))
2519 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2523 if (!NT_STATUS_IS_OK(result)) {
2524 d_fprintf(stderr, _("Couldn't list alias members\n"));
2528 num_members = sid_array.num_sids;
2530 if (num_members == 0) {
2531 return NT_STATUS_OK;
2534 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2535 &ndr_table_lsarpc.syntax_id,
2537 if (!NT_STATUS_IS_OK(result)) {
2538 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2539 nt_errstr(result) );
2543 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2544 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2546 if (!NT_STATUS_IS_OK(result)) {
2547 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2548 TALLOC_FREE(lsa_pipe);
2552 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2554 d_fprintf(stderr, _("Out of memory\n"));
2555 TALLOC_FREE(lsa_pipe);
2556 return NT_STATUS_NO_MEMORY;
2559 for (i=0; i<num_members; i++) {
2560 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2563 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2564 num_members, alias_sids,
2565 &domains, &names, &types);
2567 if (!NT_STATUS_IS_OK(result) &&
2568 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2569 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2570 TALLOC_FREE(lsa_pipe);
2574 for (i = 0; i < num_members; i++) {
2576 sid_to_fstring(sid_str, &alias_sids[i]);
2578 if (c->opt_long_list_entries) {
2579 printf("%s %s\\%s %d\n", sid_str,
2580 domains[i] ? domains[i] : _("*unknown*"),
2581 names[i] ? names[i] : _("*unknown*"), types[i]);
2584 printf("%s\\%s\n", domains[i], names[i]);
2586 printf("%s\n", sid_str);
2590 TALLOC_FREE(lsa_pipe);
2591 return NT_STATUS_OK;
2594 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2595 const DOM_SID *domain_sid,
2596 const char *domain_name,
2597 struct cli_state *cli,
2598 struct rpc_pipe_client *pipe_hnd,
2599 TALLOC_CTX *mem_ctx,
2604 struct policy_handle connect_pol, domain_pol;
2605 struct samr_Ids rids, rid_types;
2606 struct lsa_String lsa_acct_name;
2608 /* Get sam policy handle */
2610 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2612 MAXIMUM_ALLOWED_ACCESS,
2615 if (!NT_STATUS_IS_OK(result))
2618 /* Get domain policy handle */
2620 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2622 MAXIMUM_ALLOWED_ACCESS,
2623 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2626 if (!NT_STATUS_IS_OK(result))
2629 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2631 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2638 if (!NT_STATUS_IS_OK(result)) {
2640 /* Ok, did not find it in the global sam, try with builtin */
2642 DOM_SID sid_Builtin;
2644 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2646 sid_copy(&sid_Builtin, &global_sid_Builtin);
2648 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2650 MAXIMUM_ALLOWED_ACCESS,
2654 if (!NT_STATUS_IS_OK(result)) {
2655 d_fprintf(stderr, _("Couldn't find group %s\n"),
2660 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2667 if (!NT_STATUS_IS_OK(result)) {
2668 d_fprintf(stderr, _("Couldn't find group %s\n"),
2674 if (rids.count != 1) {
2675 d_fprintf(stderr, _("Couldn't find group %s\n"),
2680 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2681 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2682 domain_sid, &domain_pol,
2686 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2687 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2691 return NT_STATUS_NO_SUCH_GROUP;
2694 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2696 if (argc != 1 || c->display_usage) {
2697 return rpc_group_usage(c, argc, argv);
2700 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2701 rpc_group_members_internals,
2705 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2707 NET_API_STATUS status;
2708 struct GROUP_INFO_0 g0;
2712 d_printf(_("Usage:\n"), "net rpc group rename group newname\n");
2716 g0.grpi0_name = argv[1];
2718 status = NetGroupSetInfo(c->opt_host,
2725 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
2726 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2734 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2736 if (argc != 2 || c->display_usage) {
2737 return rpc_group_usage(c, argc, argv);
2740 return rpc_group_rename_internals(c, argc, argv);
2744 * 'net rpc group' entrypoint.
2745 * @param argc Standard main() style argc.
2746 * @param argv Standard main() style argv. Initial components are already
2750 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2752 NET_API_STATUS status;
2754 struct functable func[] = {
2759 N_("Create specified group"),
2760 N_("net rpc group add\n"
2761 " Create specified group")
2767 N_("Delete specified group"),
2768 N_("net rpc group delete\n"
2769 " Delete specified group")
2775 N_("Add member to group"),
2776 N_("net rpc group addmem\n"
2777 " Add member to group")
2783 N_("Remove member from group"),
2784 N_("net rpc group delmem\n"
2785 " Remove member from group")
2792 N_("net rpc group list\n"
2799 N_("List group members"),
2800 N_("net rpc group members\n"
2801 " List group members")
2808 N_("net rpc group rename\n"
2811 {NULL, NULL, 0, NULL, NULL}
2814 status = libnetapi_init(&c->netapi_ctx);
2818 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2819 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2820 if (c->opt_kerberos) {
2821 libnetapi_set_use_kerberos(c->netapi_ctx);
2825 if (c->display_usage) {
2826 d_printf(_("Usage:\n"));
2827 d_printf(_("net rpc group\n"
2828 " Alias for net rpc group list global "
2829 "local builtin\n"));
2830 net_display_usage_from_functable(func);
2834 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2835 rpc_group_list_internals,
2839 return net_run_function(c, argc, argv, "net rpc group", func);
2842 /****************************************************************************/
2844 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2846 return net_share_usage(c, argc, argv);
2850 * Add a share on a remote RPC server.
2852 * @param argc Standard main() style argc.
2853 * @param argv Standard main() style argv. Initial components are already
2856 * @return A shell status integer (0 for success).
2859 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2861 NET_API_STATUS status;
2864 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2865 uint32 num_users=0, perms=0;
2866 char *password=NULL; /* don't allow a share password */
2867 struct SHARE_INFO_2 i2;
2868 uint32_t parm_error = 0;
2870 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2871 return rpc_share_usage(c, argc, argv);
2874 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2878 path = strchr(sharename, '=');
2885 i2.shi2_netname = sharename;
2886 i2.shi2_type = type;
2887 i2.shi2_remark = c->opt_comment;
2888 i2.shi2_permissions = perms;
2889 i2.shi2_max_uses = c->opt_maxusers;
2890 i2.shi2_current_uses = num_users;
2891 i2.shi2_path = path;
2892 i2.shi2_passwd = password;
2894 status = NetShareAdd(c->opt_host,
2899 printf(_("NetShareAdd failed with: %s\n"),
2900 libnetapi_get_error_string(c->netapi_ctx, status));
2907 * Delete a share on a remote RPC server.
2909 * @param domain_sid The domain sid acquired from the remote server.
2910 * @param argc Standard main() style argc.
2911 * @param argv Standard main() style argv. Initial components are already
2914 * @return A shell status integer (0 for success).
2916 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
2918 if (argc < 1 || c->display_usage) {
2919 return rpc_share_usage(c, argc, argv);
2922 return NetShareDel(c->opt_host, argv[0], 0);
2926 * Formatted print of share info
2928 * @param r pointer to SHARE_INFO_1 to format
2931 static void display_share_info_1(struct net_context *c,
2932 struct SHARE_INFO_1 *r)
2934 if (c->opt_long_list_entries) {
2935 d_printf("%-12s %-8.8s %-50s\n",
2937 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
2940 d_printf("%s\n", r->shi1_netname);
2944 static WERROR get_share_info(struct net_context *c,
2945 struct rpc_pipe_client *pipe_hnd,
2946 TALLOC_CTX *mem_ctx,
2950 struct srvsvc_NetShareInfoCtr *info_ctr)
2954 union srvsvc_NetShareInfo info;
2956 /* no specific share requested, enumerate all */
2959 uint32_t preferred_len = 0xffffffff;
2960 uint32_t total_entries = 0;
2961 uint32_t resume_handle = 0;
2963 info_ctr->level = level;
2965 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
2975 /* request just one share */
2976 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
2983 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
2988 ZERO_STRUCTP(info_ctr);
2990 info_ctr->level = level;
2995 struct srvsvc_NetShareCtr1 *ctr1;
2997 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
2998 W_ERROR_HAVE_NO_MEMORY(ctr1);
3001 ctr1->array = info.info1;
3003 info_ctr->ctr.ctr1 = ctr1;
3007 struct srvsvc_NetShareCtr2 *ctr2;
3009 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3010 W_ERROR_HAVE_NO_MEMORY(ctr2);
3013 ctr2->array = info.info2;
3015 info_ctr->ctr.ctr2 = ctr2;
3019 struct srvsvc_NetShareCtr502 *ctr502;
3021 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3022 W_ERROR_HAVE_NO_MEMORY(ctr502);
3025 ctr502->array = info.info502;
3027 info_ctr->ctr.ctr502 = ctr502;
3035 * 'net rpc share list' entrypoint.
3036 * @param argc Standard main() style argc.
3037 * @param argv Standard main() style argv. Initial components are already
3040 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3042 NET_API_STATUS status;
3043 struct SHARE_INFO_1 *i1 = NULL;
3044 uint32_t entries_read = 0;
3045 uint32_t total_entries = 0;
3046 uint32_t resume_handle = 0;
3047 uint32_t i, level = 1;
3049 if (c->display_usage) {
3050 d_printf(_("Usage:\n"),
3051 "net rpc share list\n"
3052 " ",_("List shares on remote server\n"));
3056 status = NetShareEnum(c->opt_host,
3058 (uint8_t **)(void *)&i1,
3067 /* Display results */
3069 if (c->opt_long_list_entries) {
3071 "\nEnumerating shared resources (exports) on remote server:\n\n"
3072 "\nShare name Type Description\n"
3073 "---------- ---- -----------\n"));
3075 for (i = 0; i < entries_read; i++)
3076 display_share_info_1(c, &i1[i]);
3081 static bool check_share_availability(struct cli_state *cli, const char *netname)
3085 status = cli_tcon_andx(cli, netname, "A:", "", 0);
3086 if (!NT_STATUS_IS_OK(status)) {
3087 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3091 status = cli_tdis(cli);
3092 if (!NT_STATUS_IS_OK(status)) {
3093 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3100 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3101 const char *netname, uint32 type)
3103 /* only support disk shares */
3104 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3105 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3110 /* skip builtin shares */
3111 /* FIXME: should print$ be added too ? */
3112 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3113 strequal(netname,"global"))
3116 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3117 printf(_("excluding [%s]\n"), netname);
3121 return check_share_availability(cli, netname);
3125 * Migrate shares from a remote RPC server to the local RPC server.
3127 * All parameters are provided by the run_rpc_command function, except for
3128 * argc, argv which are passed through.
3130 * @param domain_sid The domain sid acquired from the remote server.
3131 * @param cli A cli_state connected to the server.
3132 * @param mem_ctx Talloc context, destroyed on completion of the function.
3133 * @param argc Standard main() style argc.
3134 * @param argv Standard main() style argv. Initial components are already
3137 * @return Normal NTSTATUS return.
3140 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3141 const DOM_SID *domain_sid,
3142 const char *domain_name,
3143 struct cli_state *cli,
3144 struct rpc_pipe_client *pipe_hnd,
3145 TALLOC_CTX *mem_ctx,
3150 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3151 struct srvsvc_NetShareInfoCtr ctr_src;
3153 struct rpc_pipe_client *srvsvc_pipe = NULL;
3154 struct cli_state *cli_dst = NULL;
3155 uint32 level = 502; /* includes secdesc */
3156 uint32_t parm_error = 0;
3158 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3160 if (!W_ERROR_IS_OK(result))
3163 /* connect destination PI_SRVSVC */
3164 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3165 &ndr_table_srvsvc.syntax_id);
3166 if (!NT_STATUS_IS_OK(nt_status))
3170 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3172 union srvsvc_NetShareInfo info;
3173 struct srvsvc_NetShareInfo502 info502 =
3174 ctr_src.ctr.ctr502->array[i];
3176 /* reset error-code */
3177 nt_status = NT_STATUS_UNSUCCESSFUL;
3179 if (!check_share_sanity(c, cli, info502.name, info502.type))
3182 /* finally add the share on the dst server */
3184 printf(_("migrating: [%s], path: %s, comment: %s, without "
3186 info502.name, info502.path, info502.comment);
3188 info.info502 = &info502;
3190 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3191 srvsvc_pipe->desthost,
3197 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3198 printf(_(" [%s] does already exist\n"),
3203 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3204 printf(_("cannot add share: %s\n"), win_errstr(result));
3210 nt_status = NT_STATUS_OK;
3214 cli_shutdown(cli_dst);
3222 * Migrate shares from a RPC server to another.
3224 * @param argc Standard main() style argc.
3225 * @param argv Standard main() style argv. Initial components are already
3228 * @return A shell status integer (0 for success).
3230 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3233 if (c->display_usage) {
3234 d_printf(_("Usage:\n"),
3235 "net rpc share migrate shares\n"
3236 " ",_("Migrate shares to local server\n"));
3241 printf(_("no server to migrate\n"));
3245 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3246 rpc_share_migrate_shares_internals,
3253 * @param f file_info
3254 * @param mask current search mask
3255 * @param state arg-pointer
3258 static void copy_fn(const char *mnt, file_info *f,
3259 const char *mask, void *state)
3261 static NTSTATUS nt_status;
3262 static struct copy_clistate *local_state;
3263 static fstring filename, new_mask;
3266 struct net_context *c;
3268 local_state = (struct copy_clistate *)state;
3269 nt_status = NT_STATUS_UNSUCCESSFUL;
3273 if (strequal(f->name, ".") || strequal(f->name, ".."))
3276 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3279 if (f->mode & aDIR) {
3281 DEBUG(3,("got dir: %s\n", f->name));
3283 fstrcpy(dir, local_state->cwd);
3285 fstrcat(dir, f->name);
3287 switch (net_mode_share)
3289 case NET_MODE_SHARE_MIGRATE:
3290 /* create that directory */
3291 nt_status = net_copy_file(c, local_state->mem_ctx,
3292 local_state->cli_share_src,
3293 local_state->cli_share_dst,
3295 c->opt_acls? true : false,
3296 c->opt_attrs? true : false,
3297 c->opt_timestamps? true:false,
3301 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3305 if (!NT_STATUS_IS_OK(nt_status))
3306 printf(_("could not handle dir %s: %s\n"),
3307 dir, nt_errstr(nt_status));
3309 /* search below that directory */
3310 fstrcpy(new_mask, dir);
3311 fstrcat(new_mask, "\\*");
3313 old_dir = local_state->cwd;
3314 local_state->cwd = dir;
3315 if (!sync_files(local_state, new_mask))
3316 printf(_("could not handle files\n"));
3317 local_state->cwd = old_dir;
3324 fstrcpy(filename, local_state->cwd);
3325 fstrcat(filename, "\\");
3326 fstrcat(filename, f->name);
3328 DEBUG(3,("got file: %s\n", filename));
3330 switch (net_mode_share)
3332 case NET_MODE_SHARE_MIGRATE:
3333 nt_status = net_copy_file(c, local_state->mem_ctx,
3334 local_state->cli_share_src,
3335 local_state->cli_share_dst,
3337 c->opt_acls? true : false,
3338 c->opt_attrs? true : false,
3339 c->opt_timestamps? true: false,
3343 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3348 if (!NT_STATUS_IS_OK(nt_status))
3349 printf(_("could not handle file %s: %s\n"),
3350 filename, nt_errstr(nt_status));
3355 * sync files, can be called recursivly to list files
3356 * and then call copy_fn for each file
3358 * @param cp_clistate pointer to the copy_clistate we work with
3359 * @param mask the current search mask
3361 * @return Boolean result
3363 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3365 struct cli_state *targetcli;
3366 char *targetpath = NULL;
3368 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3370 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3371 mask, &targetcli, &targetpath ) ) {
3372 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3374 mask, cli_errstr(cp_clistate->cli_share_src));
3378 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3379 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3380 mask, cli_errstr(targetcli));
3389 * Set the top level directory permissions before we do any further copies.
3390 * Should set up ACL inheritance.
3393 bool copy_top_level_perms(struct net_context *c,
3394 struct copy_clistate *cp_clistate,
3395 const char *sharename)
3397 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3399 switch (net_mode_share) {
3400 case NET_MODE_SHARE_MIGRATE:
3401 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3402 nt_status = net_copy_fileattr(c,
3403 cp_clistate->mem_ctx,
3404 cp_clistate->cli_share_src,
3405 cp_clistate->cli_share_dst,
3407 c->opt_acls? true : false,
3408 c->opt_attrs? true : false,
3409 c->opt_timestamps? true: false,
3413 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3417 if (!NT_STATUS_IS_OK(nt_status)) {
3418 printf(_("Could handle directory attributes for top level "
3419 "directory of share %s. Error %s\n"),
3420 sharename, nt_errstr(nt_status));
3428 * Sync all files inside a remote share to another share (over smb).
3430 * All parameters are provided by the run_rpc_command function, except for
3431 * argc, argv which are passed through.
3433 * @param domain_sid The domain sid acquired from the remote server.
3434 * @param cli A cli_state connected to the server.
3435 * @param mem_ctx Talloc context, destroyed on completion of the function.
3436 * @param argc Standard main() style argc.
3437 * @param argv Standard main() style argv. Initial components are already
3440 * @return Normal NTSTATUS return.
3443 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3444 const DOM_SID *domain_sid,
3445 const char *domain_name,
3446 struct cli_state *cli,
3447 struct rpc_pipe_client *pipe_hnd,
3448 TALLOC_CTX *mem_ctx,
3453 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3454 struct srvsvc_NetShareInfoCtr ctr_src;
3457 struct copy_clistate cp_clistate;
3458 bool got_src_share = false;
3459 bool got_dst_share = false;
3460 const char *mask = "\\*";
3463 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3465 nt_status = NT_STATUS_NO_MEMORY;
3469 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3472 if (!W_ERROR_IS_OK(result))
3475 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3477 struct srvsvc_NetShareInfo502 info502 =
3478 ctr_src.ctr.ctr502->array[i];
3480 if (!check_share_sanity(c, cli, info502.name, info502.type))
3483 /* one might not want to mirror whole discs :) */
3484 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3485 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3490 switch (net_mode_share)
3492 case NET_MODE_SHARE_MIGRATE:
3496 d_fprintf(stderr, _("Unsupported mode %d\n"),
3500 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3503 c->opt_acls ? _("including") : _("without"),
3504 c->opt_attrs ? _("including") : _("without"),
3505 c->opt_timestamps ? _("(preserving timestamps)") : "");
3507 cp_clistate.mem_ctx = mem_ctx;
3508 cp_clistate.cli_share_src = NULL;
3509 cp_clistate.cli_share_dst = NULL;
3510 cp_clistate.cwd = NULL;
3511 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3514 /* open share source */
3515 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3516 &cli->dest_ss, cli->desthost,
3517 info502.name, "A:");
3518 if (!NT_STATUS_IS_OK(nt_status))
3521 got_src_share = true;
3523 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3524 /* open share destination */
3525 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3526 NULL, dst, info502.name, "A:");
3527 if (!NT_STATUS_IS_OK(nt_status))
3530 got_dst_share = true;
3533 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3534 d_fprintf(stderr, _("Could not handle the top level "
3535 "directory permissions for the "
3536 "share: %s\n"), info502.name);
3537 nt_status = NT_STATUS_UNSUCCESSFUL;
3541 if (!sync_files(&cp_clistate, mask)) {
3542 d_fprintf(stderr, _("could not handle files for share: "
3543 "%s\n"), info502.name);
3544 nt_status = NT_STATUS_UNSUCCESSFUL;
3549 nt_status = NT_STATUS_OK;
3554 cli_shutdown(cp_clistate.cli_share_src);
3557 cli_shutdown(cp_clistate.cli_share_dst);
3564 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3566 if (c->display_usage) {
3567 d_printf(_("Usage:\n"),
3568 "net share migrate files\n"
3569 " ",_("Migrate files to local server\n"));
3574 d_printf(_("no server to migrate\n"));
3578 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3579 rpc_share_migrate_files_internals,
3584 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3586 * All parameters are provided by the run_rpc_command function, except for
3587 * argc, argv which are passed through.
3589 * @param domain_sid The domain sid acquired from the remote server.
3590 * @param cli A cli_state connected to the server.
3591 * @param mem_ctx Talloc context, destroyed on completion of the function.
3592 * @param argc Standard main() style argc.
3593 * @param argv Standard main() style argv. Initial components are already
3596 * @return Normal NTSTATUS return.
3599 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3600 const DOM_SID *domain_sid,
3601 const char *domain_name,
3602 struct cli_state *cli,
3603 struct rpc_pipe_client *pipe_hnd,
3604 TALLOC_CTX *mem_ctx,
3609 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3610 struct srvsvc_NetShareInfoCtr ctr_src;
3611 union srvsvc_NetShareInfo info;
3613 struct rpc_pipe_client *srvsvc_pipe = NULL;
3614 struct cli_state *cli_dst = NULL;
3615 uint32 level = 502; /* includes secdesc */
3616 uint32_t parm_error = 0;
3618 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3621 if (!W_ERROR_IS_OK(result))
3624 /* connect destination PI_SRVSVC */
3625 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3626 &ndr_table_srvsvc.syntax_id);
3627 if (!NT_STATUS_IS_OK(nt_status))
3631 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3633 struct srvsvc_NetShareInfo502 info502 =
3634 ctr_src.ctr.ctr502->array[i];
3636 /* reset error-code */
3637 nt_status = NT_STATUS_UNSUCCESSFUL;
3639 if (!check_share_sanity(c, cli, info502.name, info502.type))
3642 printf(_("migrating: [%s], path: %s, comment: %s, including "
3644 info502.name, info502.path, info502.comment);
3647 display_sec_desc(info502.sd_buf.sd);
3649 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3650 info.info502 = &info502;
3652 /* finally modify the share on the dst server */
3653 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3654 srvsvc_pipe->desthost,
3660 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3661 printf(_("cannot set share-acl: %s\n"),
3662 win_errstr(result));
3668 nt_status = NT_STATUS_OK;
3672 cli_shutdown(cli_dst);
3680 * Migrate share-acls from a RPC server to another.
3682 * @param argc Standard main() style argc.
3683 * @param argv Standard main() style argv. Initial components are already
3686 * @return A shell status integer (0 for success).
3688 static int rpc_share_migrate_security(struct net_context *c, int argc,
3691 if (c->display_usage) {
3692 d_printf(_("Usage:\n"),
3693 "net rpc share migrate security\n"
3694 " ",_("Migrate share-acls to local server\n"));
3699 d_printf(_("no server to migrate\n"));
3703 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3704 rpc_share_migrate_security_internals,
3709 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3710 * from one server to another.
3712 * @param argc Standard main() style argc.
3713 * @param argv Standard main() style argv. Initial components are already
3716 * @return A shell status integer (0 for success).
3719 static int rpc_share_migrate_all(struct net_context *c, int argc,
3724 if (c->display_usage) {
3725 d_printf(_("Usage:\n"),
3726 "net rpc share migrate all\n"
3727 " ",_("Migrates shares including all share settings\n"));
3732 d_printf(_("no server to migrate\n"));
3736 /* order is important. we don't want to be locked out by the share-acl
3737 * before copying files - gd */
3739 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3740 rpc_share_migrate_shares_internals, argc, argv);
3744 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3745 rpc_share_migrate_files_internals, argc, argv);
3749 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3750 rpc_share_migrate_security_internals, argc,
3756 * 'net rpc share migrate' entrypoint.
3757 * @param argc Standard main() style argc.
3758 * @param argv Standard main() style argv. Initial components are already
3761 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3764 struct functable func[] = {
3767 rpc_share_migrate_all,
3769 N_("Migrate shares from remote to local server"),
3770 N_("net rpc share migrate all\n"
3771 " Migrate shares from remote to local server")
3775 rpc_share_migrate_files,
3777 N_("Migrate files from remote to local server"),
3778 N_("net rpc share migrate files\n"
3779 " Migrate files from remote to local server")
3783 rpc_share_migrate_security,
3785 N_("Migrate share-ACLs from remote to local server"),
3786 N_("net rpc share migrate security\n"
3787 " Migrate share-ACLs from remote to local server")
3791 rpc_share_migrate_shares,
3793 N_("Migrate shares from remote to local server"),
3794 N_("net rpc share migrate shares\n"
3795 " Migrate shares from remote to local server")
3797 {NULL, NULL, 0, NULL, NULL}
3800 net_mode_share = NET_MODE_SHARE_MIGRATE;
3802 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3811 static int num_server_aliases;
3812 static struct full_alias *server_aliases;
3815 * Add an alias to the static list.
3817 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3819 if (server_aliases == NULL)
3820 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3822 server_aliases[num_server_aliases] = *alias;
3823 num_server_aliases += 1;
3827 * For a specific domain on the server, fetch all the aliases
3828 * and their members. Add all of them to the server_aliases.
3831 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3832 TALLOC_CTX *mem_ctx,
3833 struct policy_handle *connect_pol,
3834 const DOM_SID *domain_sid)
3836 uint32 start_idx, max_entries, num_entries, i;
3837 struct samr_SamArray *groups = NULL;
3839 struct policy_handle domain_pol;
3841 /* Get domain policy handle */
3843 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3845 MAXIMUM_ALLOWED_ACCESS,
3846 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3848 if (!NT_STATUS_IS_OK(result))
3855 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3861 for (i = 0; i < num_entries; i++) {
3863 struct policy_handle alias_pol;
3864 struct full_alias alias;
3865 struct lsa_SidArray sid_array;
3868 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3870 MAXIMUM_ALLOWED_ACCESS,
3871 groups->entries[i].idx,
3873 if (!NT_STATUS_IS_OK(result))
3876 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3879 if (!NT_STATUS_IS_OK(result))
3882 alias.num_members = sid_array.num_sids;
3884 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3885 if (!NT_STATUS_IS_OK(result))
3888 alias.members = NULL;
3890 if (alias.num_members > 0) {
3891 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3893 for (j = 0; j < alias.num_members; j++)
3894 sid_copy(&alias.members[j],
3895 sid_array.sids[j].sid);
3898 sid_compose(&alias.sid, domain_sid,
3899 groups->entries[i].idx);
3901 push_alias(mem_ctx, &alias);
3903 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3905 result = NT_STATUS_OK;
3908 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3914 * Dump server_aliases as names for debugging purposes.
3917 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
3918 const DOM_SID *domain_sid,
3919 const char *domain_name,
3920 struct cli_state *cli,
3921 struct rpc_pipe_client *pipe_hnd,
3922 TALLOC_CTX *mem_ctx,
3928 struct policy_handle lsa_pol;
3930 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
3931 SEC_FLAG_MAXIMUM_ALLOWED,
3933 if (!NT_STATUS_IS_OK(result))
3936 for (i=0; i<num_server_aliases; i++) {
3939 enum lsa_SidType *types;
3942 struct full_alias *alias = &server_aliases[i];
3944 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3946 &domains, &names, &types);
3947 if (!NT_STATUS_IS_OK(result))
3950 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3952 if (alias->num_members == 0) {
3957 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
3960 &domains, &names, &types);
3962 if (!NT_STATUS_IS_OK(result) &&
3963 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3966 for (j=0; j<alias->num_members; j++)
3967 DEBUG(1, ("%s\\%s (%d); ",
3968 domains[j] ? domains[j] : "*unknown*",
3969 names[j] ? names[j] : "*unknown*",types[j]));
3973 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
3975 return NT_STATUS_OK;
3979 * Fetch a list of all server aliases and their members into
3983 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
3984 const DOM_SID *domain_sid,
3985 const char *domain_name,
3986 struct cli_state *cli,
3987 struct rpc_pipe_client *pipe_hnd,
3988 TALLOC_CTX *mem_ctx,
3993 struct policy_handle connect_pol;
3995 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3997 MAXIMUM_ALLOWED_ACCESS,
4000 if (!NT_STATUS_IS_OK(result))
4003 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4004 &global_sid_Builtin);
4006 if (!NT_STATUS_IS_OK(result))
4009 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4012 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4017 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4019 token->num_sids = 4;
4021 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4022 d_fprintf(stderr, "malloc ",_("failed"),"\n");
4023 token->num_sids = 0;
4027 token->user_sids[0] = *user_sid;
4028 sid_copy(&token->user_sids[1], &global_sid_World);
4029 sid_copy(&token->user_sids[2], &global_sid_Network);
4030 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4033 static void free_user_token(NT_USER_TOKEN *token)
4035 SAFE_FREE(token->user_sids);
4038 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4040 if (is_sid_in_token(token, sid))
4043 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4044 if (!token->user_sids) {
4048 sid_copy(&token->user_sids[token->num_sids], sid);
4050 token->num_sids += 1;
4055 NT_USER_TOKEN token;
4058 static void dump_user_token(struct user_token *token)
4062 d_printf("%s\n", token->name);
4064 for (i=0; i<token->token.num_sids; i++) {
4065 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4069 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4073 for (i=0; i<alias->num_members; i++) {
4074 if (sid_compare(sid, &alias->members[i]) == 0)
4081 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4085 for (i=0; i<num_server_aliases; i++) {
4086 if (is_alias_member(&sid, &server_aliases[i]))
4087 add_sid_to_token(token, &server_aliases[i].sid);
4092 * We got a user token with all the SIDs we can know about without asking the
4093 * server directly. These are the user and domain group sids. All of these can
4094 * be members of aliases. So scan the list of aliases for each of the SIDs and
4095 * add them to the token.
4098 static void collect_alias_memberships(NT_USER_TOKEN *token)
4100 int num_global_sids = token->num_sids;
4103 for (i=0; i<num_global_sids; i++) {
4104 collect_sid_memberships(token, token->user_sids[i]);
4108 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4110 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4111 enum wbcSidType type;
4113 struct wbcDomainSid wsid;
4114 char *sid_str = NULL;
4116 uint32_t num_groups;
4117 gid_t *groups = NULL;
4120 fstr_sprintf(full_name, "%s%c%s",
4121 domain, *lp_winbind_separator(), user);
4123 /* First let's find out the user sid */
4125 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4127 if (!WBC_ERROR_IS_OK(wbc_status)) {
4128 DEBUG(1, ("winbind could not find %s: %s\n",
4129 full_name, wbcErrorString(wbc_status)));
4133 wbc_status = wbcSidToString(&wsid, &sid_str);
4134 if (!WBC_ERROR_IS_OK(wbc_status)) {
4138 if (type != SID_NAME_USER) {
4139 wbcFreeMemory(sid_str);
4140 DEBUG(1, ("%s is not a user\n", full_name));
4144 if (!string_to_sid(&user_sid, sid_str)) {
4145 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4149 wbcFreeMemory(sid_str);
4152 init_user_token(token, &user_sid);
4154 /* And now the groups winbind knows about */
4156 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4157 if (!WBC_ERROR_IS_OK(wbc_status)) {
4158 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4159 full_name, wbcErrorString(wbc_status)));
4163 for (i = 0; i < num_groups; i++) {
4164 gid_t gid = groups[i];
4167 wbc_status = wbcGidToSid(gid, &wsid);
4168 if (!WBC_ERROR_IS_OK(wbc_status)) {
4169 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4170 (unsigned int)gid, wbcErrorString(wbc_status)));
4171 wbcFreeMemory(groups);
4175 wbc_status = wbcSidToString(&wsid, &sid_str);
4176 if (!WBC_ERROR_IS_OK(wbc_status)) {
4177 wbcFreeMemory(groups);
4181 DEBUG(3, (" %s\n", sid_str));
4183 string_to_sid(&sid, sid_str);
4184 wbcFreeMemory(sid_str);
4187 add_sid_to_token(token, &sid);
4189 wbcFreeMemory(groups);
4195 * Get a list of all user tokens we want to look at
4198 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4199 struct user_token **user_tokens)
4201 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4202 uint32_t i, num_users;
4204 struct user_token *result;
4205 TALLOC_CTX *frame = NULL;
4207 if (lp_winbind_use_default_domain() &&
4208 (c->opt_target_workgroup == NULL)) {
4209 d_fprintf(stderr, _("winbind use default domain = yes set, "
4210 "please specify a workgroup\n"));
4214 /* Send request to winbind daemon */
4216 wbc_status = wbcListUsers(NULL, &num_users, &users);
4217 if (!WBC_ERROR_IS_OK(wbc_status)) {
4218 DEBUG(1, (_("winbind could not list users: %s\n"),
4219 wbcErrorString(wbc_status)));
4223 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4225 if (result == NULL) {
4226 DEBUG(1, ("Could not malloc sid array\n"));
4227 wbcFreeMemory(users);
4231 frame = talloc_stackframe();
4232 for (i=0; i < num_users; i++) {
4233 fstring domain, user;
4236 fstrcpy(result[i].name, users[i]);
4238 p = strchr(users[i], *lp_winbind_separator());
4240 DEBUG(3, ("%s\n", users[i]));
4243 fstrcpy(domain, c->opt_target_workgroup);
4244 fstrcpy(user, users[i]);
4247 fstrcpy(domain, users[i]);
4252 get_user_sids(domain, user, &(result[i].token));
4256 wbcFreeMemory(users);
4258 *num_tokens = num_users;
4259 *user_tokens = result;
4264 static bool get_user_tokens_from_file(FILE *f,
4266 struct user_token **tokens)
4268 struct user_token *token = NULL;
4273 if (fgets(line, sizeof(line)-1, f) == NULL) {
4277 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4278 line[strlen(line)-1] = '\0';
4281 if (line[0] == ' ') {
4285 if(!string_to_sid(&sid, &line[1])) {
4286 DEBUG(1,("get_user_tokens_from_file: Could "
4287 "not convert sid %s \n",&line[1]));
4291 if (token == NULL) {
4292 DEBUG(0, ("File does not begin with username"));
4296 add_sid_to_token(&token->token, &sid);
4300 /* And a new user... */
4303 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4304 if (*tokens == NULL) {
4305 DEBUG(0, ("Could not realloc tokens\n"));
4309 token = &((*tokens)[*num_tokens-1]);
4311 fstrcpy(token->name, line);
4312 token->token.num_sids = 0;
4313 token->token.user_sids = NULL;
4322 * Show the list of all users that have access to a share
4325 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4326 TALLOC_CTX *mem_ctx,
4327 const char *netname,
4329 struct user_token *tokens)
4332 SEC_DESC *share_sd = NULL;
4333 SEC_DESC *root_sd = NULL;
4334 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4336 union srvsvc_NetShareInfo info;
4341 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4348 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4349 DEBUG(1, ("Coult not query secdesc for share %s\n",
4354 share_sd = info.info502->sd_buf.sd;
4355 if (share_sd == NULL) {
4356 DEBUG(1, ("Got no secdesc for share %s\n",
4362 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4366 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4367 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4368 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4371 for (i=0; i<num_tokens; i++) {
4374 if (share_sd != NULL) {
4375 status = se_access_check(share_sd, &tokens[i].token,
4378 if (!NT_STATUS_IS_OK(status)) {
4379 DEBUG(1, ("Could not check share_sd for "
4386 if (root_sd == NULL) {
4387 d_printf(" %s\n", tokens[i].name);
4391 status = se_access_check(root_sd, &tokens[i].token,
4393 if (!NT_STATUS_IS_OK(status)) {
4394 DEBUG(1, ("Could not check root_sd for user %s\n",
4398 d_printf(" %s\n", tokens[i].name);
4401 if (fnum != (uint16_t)-1)
4402 cli_close(cli, fnum);
4414 static void collect_share(const char *name, uint32 m,
4415 const char *comment, void *state)
4417 struct share_list *share_list = (struct share_list *)state;
4419 if (m != STYPE_DISKTREE)
4422 share_list->num_shares += 1;
4423 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4424 if (!share_list->shares) {
4425 share_list->num_shares = 0;
4428 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4432 * List shares on a remote RPC server, including the security descriptors.
4434 * All parameters are provided by the run_rpc_command function, except for
4435 * argc, argv which are passed through.
4437 * @param domain_sid The domain sid acquired from the remote server.
4438 * @param cli A cli_state connected to the server.
4439 * @param mem_ctx Talloc context, destroyed on completion of the function.
4440 * @param argc Standard main() style argc.
4441 * @param argv Standard main() style argv. Initial components are already
4444 * @return Normal NTSTATUS return.
4447 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4448 const DOM_SID *domain_sid,
4449 const char *domain_name,
4450 struct cli_state *cli,
4451 struct rpc_pipe_client *pipe_hnd,
4452 TALLOC_CTX *mem_ctx,
4461 struct user_token *tokens = NULL;
4464 struct share_list share_list;
4469 f = fopen(argv[0], "r");
4473 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4474 return NT_STATUS_UNSUCCESSFUL;
4477 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4483 DEBUG(0, ("Could not read users from file\n"));
4484 return NT_STATUS_UNSUCCESSFUL;
4487 for (i=0; i<num_tokens; i++)
4488 collect_alias_memberships(&tokens[i].token);
4490 share_list.num_shares = 0;
4491 share_list.shares = NULL;
4493 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4496 DEBUG(0, ("Error returning browse list: %s\n",
4501 for (i = 0; i < share_list.num_shares; i++) {
4502 char *netname = share_list.shares[i];
4504 if (netname[strlen(netname)-1] == '$')
4507 d_printf("%s\n", netname);
4509 show_userlist(pipe_hnd, mem_ctx, netname,
4510 num_tokens, tokens);
4513 for (i=0; i<num_tokens; i++) {
4514 free_user_token(&tokens[i].token);
4517 SAFE_FREE(share_list.shares);
4519 return NT_STATUS_OK;
4522 static int rpc_share_allowedusers(struct net_context *c, int argc,
4527 if (c->display_usage) {
4528 d_printf(_("Usage:\n"),
4529 "net rpc share allowedusers\n"
4530 " ",_("List allowed users\n"));
4534 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4535 rpc_aliaslist_internals,
4540 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4546 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4547 rpc_share_allowedusers_internals,
4551 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4554 struct user_token *tokens = NULL;
4558 net_usersidlist_usage(c, argc, argv);
4562 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4563 DEBUG(0, ("Could not get the user/sid list\n"));
4567 for (i=0; i<num_tokens; i++) {
4568 dump_user_token(&tokens[i]);
4569 free_user_token(&tokens[i].token);
4576 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4578 d_printf(_("net usersidlist\n"
4579 "\tprints out a list of all users the running winbind knows\n"
4580 "\tabout, together with all their SIDs. This is used as\n"
4581 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
4583 net_common_flags_usage(c, argc, argv);
4588 * 'net rpc share' entrypoint.
4589 * @param argc Standard main() style argc.
4590 * @param argv Standard main() style argv. Initial components are already
4594 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4596 NET_API_STATUS status;
4598 struct functable func[] = {
4604 N_("net rpc share add\n"
4612 N_("net rpc share delete\n"
4617 rpc_share_allowedusers,
4619 N_("Modify allowed users"),
4620 N_("net rpc share allowedusers\n"
4621 " Modify allowed users")
4627 N_("Migrate share to local server"),
4628 N_("net rpc share migrate\n"
4629 " Migrate share to local server")
4636 N_("net rpc share list\n"
4639 {NULL, NULL, 0, NULL, NULL}
4642 status = libnetapi_init(&c->netapi_ctx);
4646 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4647 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4648 if (c->opt_kerberos) {
4649 libnetapi_set_use_kerberos(c->netapi_ctx);
4653 if (c->display_usage) {
4654 d_printf(_("Usage:\n"),
4657 " Alias for net rpc share list\n"));
4658 net_display_usage_from_functable(func);
4662 return rpc_share_list(c, argc, argv);
4665 return net_run_function(c, argc, argv, "net rpc share", func);
4668 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4669 TALLOC_CTX *mem_ctx,
4670 struct rpc_sh_ctx *ctx,
4671 struct rpc_pipe_client *pipe_hnd,
4672 int argc, const char **argv)
4675 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4678 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4679 TALLOC_CTX *mem_ctx,
4680 struct rpc_sh_ctx *ctx,
4681 struct rpc_pipe_client *pipe_hnd,
4682 int argc, const char **argv)
4684 NET_API_STATUS status;
4685 uint32_t parm_err = 0;
4686 struct SHARE_INFO_2 i2;
4688 if ((argc < 2) || (argc > 3)) {
4689 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
4691 return NT_STATUS_INVALID_PARAMETER;
4694 i2.shi2_netname = argv[0];
4695 i2.shi2_type = STYPE_DISKTREE;
4696 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4697 i2.shi2_permissions = 0;
4698 i2.shi2_max_uses = 0;
4699 i2.shi2_current_uses = 0;
4700 i2.shi2_path = argv[1];
4701 i2.shi2_passwd = NULL;
4703 status = NetShareAdd(pipe_hnd->desthost,
4708 return werror_to_ntstatus(W_ERROR(status));
4711 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4712 TALLOC_CTX *mem_ctx,
4713 struct rpc_sh_ctx *ctx,
4714 struct rpc_pipe_client *pipe_hnd,
4715 int argc, const char **argv)
4718 d_fprintf(stderr, _("Usage:")," %s <share>\n", ctx->whoami);
4719 return NT_STATUS_INVALID_PARAMETER;
4722 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4725 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4726 TALLOC_CTX *mem_ctx,
4727 struct rpc_sh_ctx *ctx,
4728 struct rpc_pipe_client *pipe_hnd,
4729 int argc, const char **argv)
4731 union srvsvc_NetShareInfo info;
4736 d_fprintf(stderr, _("Usage: %s <share>\n"), ctx->whoami);
4737 return NT_STATUS_INVALID_PARAMETER;
4740 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4746 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4750 d_printf(_("Name: %s\n"), info.info2->name);
4751 d_printf(_("Comment: %s\n"), info.info2->comment);
4752 d_printf(_("Path: %s\n"), info.info2->path);
4753 d_printf(_("Password: %s\n"), info.info2->password);
4756 return werror_to_ntstatus(result);
4759 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4760 struct rpc_sh_ctx *ctx)
4762 static struct rpc_sh_cmd cmds[] = {
4764 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4765 N_("List available shares") },
4767 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4768 N_("Add a share") },
4770 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4771 N_("Delete a share") },
4773 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4774 N_("Get information about a share") },
4776 { NULL, NULL, 0, NULL, NULL }
4782 /****************************************************************************/
4784 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4786 return net_file_usage(c, argc, argv);
4790 * Close a file on a remote RPC server.
4792 * @param argc Standard main() style argc.
4793 * @param argv Standard main() style argv. Initial components are already
4796 * @return A shell status integer (0 for success).
4798 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4800 if (argc < 1 || c->display_usage) {
4801 return rpc_file_usage(c, argc, argv);
4804 return NetFileClose(c->opt_host, atoi(argv[0]));
4808 * Formatted print of open file info
4810 * @param r struct FILE_INFO_3 contents
4813 static void display_file_info_3(struct FILE_INFO_3 *r)
4815 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4816 r->fi3_id, r->fi3_username, r->fi3_permissions,
4817 r->fi3_num_locks, r->fi3_pathname);
4821 * List files for a user on a remote RPC server.
4823 * @param argc Standard main() style argc.
4824 * @param argv Standard main() style argv. Initial components are already
4827 * @return A shell status integer (0 for success)..
4830 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4832 NET_API_STATUS status;
4833 uint32 preferred_len = 0xffffffff, i;
4834 const char *username=NULL;
4835 uint32_t total_entries = 0;
4836 uint32_t entries_read = 0;
4837 uint32_t resume_handle = 0;
4838 struct FILE_INFO_3 *i3 = NULL;
4840 if (c->display_usage) {
4841 return rpc_file_usage(c, argc, argv);
4844 /* if argc > 0, must be user command */
4846 username = smb_xstrdup(argv[0]);
4849 status = NetFileEnum(c->opt_host,
4853 (uint8_t **)(void *)&i3,
4863 /* Display results */
4866 "\nEnumerating open files on remote server:\n\n"
4867 "\nFileId Opened by Perms Locks Path"
4868 "\n------ --------- ----- ----- ---- \n"));
4869 for (i = 0; i < entries_read; i++) {
4870 display_file_info_3(&i3[i]);
4877 * 'net rpc file' entrypoint.
4878 * @param argc Standard main() style argc.
4879 * @param argv Standard main() style argv. Initial components are already
4883 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4885 NET_API_STATUS status;
4887 struct functable func[] = {
4892 N_("Close opened file"),
4893 N_("net rpc file close\n"
4894 " Close opened file")
4900 N_("List files opened by user"),
4901 N_("net rpc file user\n"
4902 " List files opened by user")
4909 N_("Display information about opened file"),
4910 N_("net rpc file info\n"
4911 " Display information about opened file")
4914 {NULL, NULL, 0, NULL, NULL}
4917 status = libnetapi_init(&c->netapi_ctx);
4921 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4922 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4923 if (c->opt_kerberos) {
4924 libnetapi_set_use_kerberos(c->netapi_ctx);
4928 if (c->display_usage) {
4929 d_printf(_("Usage:\n"));
4930 d_printf(_("net rpc file\n"
4931 " List opened files\n"));
4932 net_display_usage_from_functable(func);
4936 return rpc_file_user(c, argc, argv);
4939 return net_run_function(c, argc, argv, "net rpc file", func);
4943 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
4945 * All parameters are provided by the run_rpc_command function, except for
4946 * argc, argv which are passed through.
4948 * @param c A net_context structure.
4949 * @param domain_sid The domain sid acquired from the remote server.
4950 * @param cli A cli_state connected to the server.
4951 * @param mem_ctx Talloc context, destroyed on completion of the function.
4952 * @param argc Standard main() style argc.
4953 * @param argv Standard main() style argv. Initial components are already
4956 * @return Normal NTSTATUS return.
4959 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
4960 const DOM_SID *domain_sid,
4961 const char *domain_name,
4962 struct cli_state *cli,
4963 struct rpc_pipe_client *pipe_hnd,
4964 TALLOC_CTX *mem_ctx,
4968 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4970 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
4972 if (NT_STATUS_IS_OK(result)) {
4973 d_printf(_("\nShutdown successfully aborted\n"));
4974 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4976 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4982 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
4984 * All parameters are provided by the run_rpc_command function, except for
4985 * argc, argv which are passed through.
4987 * @param c A net_context structure.
4988 * @param domain_sid The domain sid acquired from the remote server.
4989 * @param cli A cli_state connected to the server.
4990 * @param mem_ctx Talloc context, destroyed on completion of the function.
4991 * @param argc Standard main() style argc.
4992 * @param argv Standard main() style argv. Initial components are already
4995 * @return Normal NTSTATUS return.
4998 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
4999 const DOM_SID *domain_sid,
5000 const char *domain_name,
5001 struct cli_state *cli,
5002 struct rpc_pipe_client *pipe_hnd,
5003 TALLOC_CTX *mem_ctx,
5007 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5009 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5011 if (NT_STATUS_IS_OK(result)) {
5012 d_printf(_("\nShutdown successfully aborted\n"));
5013 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5015 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5021 * ABORT the shutdown of a remote RPC server.
5023 * @param argc Standard main() style argc.
5024 * @param argv Standard main() style argv. Initial components are already
5027 * @return A shell status integer (0 for success).
5030 static int rpc_shutdown_abort(struct net_context *c, int argc,
5035 if (c->display_usage) {
5036 d_printf(_("Usage:\n"),
5037 "net rpc abortshutdown\n"
5038 " ",_("Abort a scheduled shutdown\n"));
5042 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5043 rpc_shutdown_abort_internals, argc, argv);
5048 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5050 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5051 rpc_reg_shutdown_abort_internals,
5056 * Shut down a remote RPC Server via initshutdown pipe.
5058 * All parameters are provided by the run_rpc_command function, except for
5059 * argc, argv which are passed through.
5061 * @param c A net_context structure.
5062 * @param domain_sid The domain sid acquired from the remote server.
5063 * @param cli A cli_state connected to the server.
5064 * @param mem_ctx Talloc context, destroyed on completion of the function.
5065 * @param argc Standard main() style argc.
5066 * @param argv Standard main() style argv. Initial components are already
5069 * @return Normal NTSTATUS return.
5072 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5073 const DOM_SID *domain_sid,
5074 const char *domain_name,
5075 struct cli_state *cli,
5076 struct rpc_pipe_client *pipe_hnd,
5077 TALLOC_CTX *mem_ctx,
5081 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5082 const char *msg = N_("This machine will be shutdown shortly");
5083 uint32 timeout = 20;
5084 struct lsa_StringLarge msg_string;
5086 if (c->opt_comment) {
5087 msg = c->opt_comment;
5089 if (c->opt_timeout) {
5090 timeout = c->opt_timeout;
5093 msg_string.string = msg;
5095 /* create an entry */
5096 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5097 &msg_string, timeout, c->opt_force, c->opt_reboot,
5100 if (NT_STATUS_IS_OK(result)) {
5101 d_printf(_("\nShutdown of remote machine succeeded\n"));
5102 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5104 DEBUG(1,("Shutdown of remote machine failed!\n"));
5110 * Shut down a remote RPC Server via winreg pipe.
5112 * All parameters are provided by the run_rpc_command function, except for
5113 * argc, argv which are passed through.
5115 * @param c A net_context structure.
5116 * @param domain_sid The domain sid acquired from the remote server.
5117 * @param cli A cli_state connected to the server.
5118 * @param mem_ctx Talloc context, destroyed on completion of the function.
5119 * @param argc Standard main() style argc.
5120 * @param argv Standard main() style argv. Initial components are already
5123 * @return Normal NTSTATUS return.
5126 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5127 const DOM_SID *domain_sid,
5128 const char *domain_name,
5129 struct cli_state *cli,
5130 struct rpc_pipe_client *pipe_hnd,
5131 TALLOC_CTX *mem_ctx,
5135 const char *msg = N_("This machine will be shutdown shortly");
5136 uint32 timeout = 20;
5137 struct lsa_StringLarge msg_string;
5141 if (c->opt_comment) {
5142 msg = c->opt_comment;
5144 msg_string.string = msg;
5146 if (c->opt_timeout) {
5147 timeout = c->opt_timeout;
5150 /* create an entry */
5151 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5152 &msg_string, timeout, c->opt_force, c->opt_reboot,
5155 if (NT_STATUS_IS_OK(result)) {
5156 d_printf(_("\nShutdown of remote machine succeeded\n"));
5158 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5159 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5160 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5162 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5169 * Shut down a remote RPC server.
5171 * @param argc Standard main() style argc.
5172 * @param argv Standard main() style argv. Initial components are already
5175 * @return A shell status integer (0 for success).
5178 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5182 if (c->display_usage) {
5183 d_printf(_("Usage:\n"),
5184 "net rpc shutdown\n"
5185 " ",_("Shut down a remote RPC server\n"));
5189 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5190 rpc_init_shutdown_internals, argc, argv);
5193 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5194 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5195 rpc_reg_shutdown_internals, argc, argv);
5201 /***************************************************************************
5202 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5203 ***************************************************************************/
5206 * Add interdomain trust account to the RPC server.
5207 * All parameters (except for argc and argv) are passed by run_rpc_command
5210 * @param c A net_context structure.
5211 * @param domain_sid The domain sid acquired from the server.
5212 * @param cli A cli_state connected to the server.
5213 * @param mem_ctx Talloc context, destroyed on completion of the function.
5214 * @param argc Standard main() style argc.
5215 * @param argv Standard main() style argv. Initial components are already
5218 * @return normal NTSTATUS return code.
5221 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5222 const DOM_SID *domain_sid,
5223 const char *domain_name,
5224 struct cli_state *cli,
5225 struct rpc_pipe_client *pipe_hnd,
5226 TALLOC_CTX *mem_ctx,
5230 struct policy_handle connect_pol, domain_pol, user_pol;
5231 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5233 struct lsa_String lsa_acct_name;
5235 uint32 acct_flags=0;
5237 uint32_t access_granted = 0;
5238 union samr_UserInfo info;
5239 unsigned int orig_timeout;
5242 d_printf(_("Usage:"),_(" net rpc trustdom add <domain_name> "
5243 "<trust password>\n"));
5244 return NT_STATUS_INVALID_PARAMETER;
5248 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5251 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5252 return NT_STATUS_NO_MEMORY;
5255 strupper_m(acct_name);
5257 init_lsa_String(&lsa_acct_name, acct_name);
5259 /* Get samr policy handle */
5260 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5262 MAXIMUM_ALLOWED_ACCESS,
5264 if (!NT_STATUS_IS_OK(result)) {
5268 /* Get domain policy handle */
5269 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5271 MAXIMUM_ALLOWED_ACCESS,
5272 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5274 if (!NT_STATUS_IS_OK(result)) {
5278 /* This call can take a long time - allow the server to time out.
5279 * 35 seconds should do it. */
5281 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5283 /* Create trusting domain's account */
5284 acb_info = ACB_NORMAL;
5285 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5286 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5287 SAMR_USER_ACCESS_SET_PASSWORD |
5288 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5289 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5291 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5300 /* And restore our original timeout. */
5301 rpccli_set_timeout(pipe_hnd, orig_timeout);
5303 if (!NT_STATUS_IS_OK(result)) {
5304 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5305 acct_name, nt_errstr(result));
5310 struct samr_CryptPassword crypt_pwd;
5312 ZERO_STRUCT(info.info23);
5314 init_samr_CryptPassword(argv[1],
5315 &cli->user_session_key,
5318 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5319 SAMR_FIELD_NT_PASSWORD_PRESENT;
5320 info.info23.info.acct_flags = ACB_DOMTRUST;
5321 info.info23.password = crypt_pwd;
5323 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5328 if (!NT_STATUS_IS_OK(result)) {
5329 DEBUG(0,("Could not set trust account password: %s\n",
5330 nt_errstr(result)));
5336 SAFE_FREE(acct_name);
5341 * Create interdomain trust account for a remote domain.
5343 * @param argc Standard argc.
5344 * @param argv Standard argv without initial components.
5346 * @return Integer status (0 means success).
5349 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5351 if (argc > 0 && !c->display_usage) {
5352 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5353 rpc_trustdom_add_internals, argc, argv);
5355 d_printf(_("Usage:\n"),
5356 _("net rpc trustdom add <domain_name> <trust "
5364 * Remove interdomain trust account from the RPC server.
5365 * All parameters (except for argc and argv) are passed by run_rpc_command
5368 * @param c A net_context structure.
5369 * @param domain_sid The domain sid acquired from the server.
5370 * @param cli A cli_state connected to the server.
5371 * @param mem_ctx Talloc context, destroyed on completion of the function.
5372 * @param argc Standard main() style argc.
5373 * @param argv Standard main() style argv. Initial components are already
5376 * @return normal NTSTATUS return code.
5379 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5380 const DOM_SID *domain_sid,
5381 const char *domain_name,
5382 struct cli_state *cli,
5383 struct rpc_pipe_client *pipe_hnd,
5384 TALLOC_CTX *mem_ctx,
5388 struct policy_handle connect_pol, domain_pol, user_pol;
5389 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5391 DOM_SID trust_acct_sid;
5392 struct samr_Ids user_rids, name_types;
5393 struct lsa_String lsa_acct_name;
5396 d_printf(_("Usage:"),_(" net rpc trustdom del <domain_name>\n"));
5397 return NT_STATUS_INVALID_PARAMETER;
5401 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5403 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5405 if (acct_name == NULL)
5406 return NT_STATUS_NO_MEMORY;
5408 strupper_m(acct_name);
5410 /* Get samr policy handle */
5411 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5413 MAXIMUM_ALLOWED_ACCESS,
5415 if (!NT_STATUS_IS_OK(result)) {
5419 /* Get domain policy handle */
5420 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5422 MAXIMUM_ALLOWED_ACCESS,
5423 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5425 if (!NT_STATUS_IS_OK(result)) {
5429 init_lsa_String(&lsa_acct_name, acct_name);
5431 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5438 if (!NT_STATUS_IS_OK(result)) {
5439 d_printf(_("net rpc trustdom del: LookupNames on user %s "
5441 acct_name, nt_errstr(result) );
5445 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5447 MAXIMUM_ALLOWED_ACCESS,
5451 if (!NT_STATUS_IS_OK(result)) {
5452 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
5454 acct_name, nt_errstr(result) );
5458 /* append the rid to the domain sid */
5459 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
5463 /* remove the sid */
5465 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5468 if (!NT_STATUS_IS_OK(result)) {
5469 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
5470 " on user %s failed %s\n"),
5471 acct_name, nt_errstr(result) );
5477 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5480 if (!NT_STATUS_IS_OK(result)) {
5481 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
5483 acct_name, nt_errstr(result) );
5487 if (!NT_STATUS_IS_OK(result)) {
5488 d_printf(_("Could not set trust account password: %s\n"),
5498 * Delete interdomain trust account for a remote domain.
5500 * @param argc Standard argc.
5501 * @param argv Standard argv without initial components.
5503 * @return Integer status (0 means success).
5506 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5508 if (argc > 0 && !c->display_usage) {
5509 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5510 rpc_trustdom_del_internals, argc, argv);
5512 d_printf(_("Usage:\n"),
5513 _("net rpc trustdom del <domain>\n"));
5518 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5519 struct cli_state *cli,
5520 TALLOC_CTX *mem_ctx,
5521 const char *domain_name)
5523 char *dc_name = NULL;
5524 const char *buffer = NULL;
5525 struct rpc_pipe_client *netr;
5528 /* Use NetServerEnum2 */
5530 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5532 return NT_STATUS_OK;
5535 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5536 for domain %s\n", domain_name));
5538 /* Try netr_GetDcName */
5540 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5542 if (!NT_STATUS_IS_OK(status)) {
5546 status = rpccli_netr_GetDcName(netr, mem_ctx,
5553 if (NT_STATUS_IS_OK(status)) {
5557 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5558 for domain %s\n", domain_name));
5564 * Establish trust relationship to a trusting domain.
5565 * Interdomain account must already be created on remote PDC.
5567 * @param c A net_context structure.
5568 * @param argc Standard argc.
5569 * @param argv Standard argv without initial components.
5571 * @return Integer status (0 means success).
5574 static int rpc_trustdom_establish(struct net_context *c, int argc,
5577 struct cli_state *cli = NULL;
5578 struct sockaddr_storage server_ss;
5579 struct rpc_pipe_client *pipe_hnd = NULL;
5580 struct policy_handle connect_hnd;
5581 TALLOC_CTX *mem_ctx;
5583 DOM_SID *domain_sid;
5588 union lsa_PolicyInformation *info = NULL;
5591 * Connect to \\server\ipc$ as 'our domain' account with password
5594 if (argc != 1 || c->display_usage) {
5595 d_printf(_("Usage:\n"),
5596 _("net rpc trustdom establish <domain_name>\n"));
5600 domain_name = smb_xstrdup(argv[0]);
5601 strupper_m(domain_name);
5603 /* account name used at first is our domain's name with '$' */
5604 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
5607 strupper_m(acct_name);
5610 * opt_workgroup will be used by connection functions further,
5611 * hence it should be set to remote domain name instead of ours
5613 if (c->opt_workgroup) {
5614 c->opt_workgroup = smb_xstrdup(domain_name);
5617 c->opt_user_name = acct_name;
5619 /* find the domain controller */
5620 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5621 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5625 /* connect to ipc$ as username/password */
5626 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5627 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5629 /* Is it trusting domain account for sure ? */
5630 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5631 nt_errstr(nt_status)));
5635 /* store who we connected to */
5637 saf_store( domain_name, pdc_name );
5640 * Connect to \\server\ipc$ again (this time anonymously)
5643 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5646 if (NT_STATUS_IS_ERR(nt_status)) {
5647 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5648 domain_name, nt_errstr(nt_status)));
5652 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5653 "domain %s", domain_name))) {
5654 DEBUG(0, ("talloc_init() failed\n"));
5659 /* Make sure we're talking to a proper server */
5661 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5662 if (!NT_STATUS_IS_OK(nt_status)) {
5664 talloc_destroy(mem_ctx);
5669 * Call LsaOpenPolicy and LsaQueryInfo
5672 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5674 if (!NT_STATUS_IS_OK(nt_status)) {
5675 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5677 talloc_destroy(mem_ctx);
5681 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
5683 if (NT_STATUS_IS_ERR(nt_status)) {
5684 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5685 nt_errstr(nt_status)));
5687 talloc_destroy(mem_ctx);
5691 /* Querying info level 5 */
5693 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5695 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5697 if (NT_STATUS_IS_ERR(nt_status)) {
5698 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5699 nt_errstr(nt_status)));
5701 talloc_destroy(mem_ctx);
5705 domain_sid = info->account_domain.sid;
5707 /* There should be actually query info level 3 (following nt serv behaviour),
5708 but I still don't know if it's _really_ necessary */
5711 * Store the password in secrets db
5714 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5715 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5717 talloc_destroy(mem_ctx);
5722 * Close the pipes and clean up
5725 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5726 if (NT_STATUS_IS_ERR(nt_status)) {
5727 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5728 nt_errstr(nt_status)));
5730 talloc_destroy(mem_ctx);
5736 talloc_destroy(mem_ctx);
5738 d_printf(_("Trust to domain %s established\n"), domain_name);
5743 * Revoke trust relationship to the remote domain.
5745 * @param c A net_context structure.
5746 * @param argc Standard argc.
5747 * @param argv Standard argv without initial components.
5749 * @return Integer status (0 means success).
5752 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5758 if (argc < 1 || c->display_usage) {
5759 d_printf(_("Usage:\n"),
5760 _("net rpc trustdom revoke <domain_name>\n"
5761 " Revoke trust relationship\n"
5762 " domain_name\tName of domain to revoke trust\n"));
5766 /* generate upper cased domain name */
5767 domain_name = smb_xstrdup(argv[0]);
5768 strupper_m(domain_name);
5770 /* delete password of the trust */
5771 if (!pdb_del_trusteddom_pw(domain_name)) {
5772 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5779 SAFE_FREE(domain_name);
5783 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5784 const DOM_SID *domain_sid,
5785 const char *domain_name,
5786 struct cli_state *cli,
5787 struct rpc_pipe_client *pipe_hnd,
5788 TALLOC_CTX *mem_ctx,
5793 if (!sid_to_fstring(str_sid, domain_sid)) {
5794 return NT_STATUS_UNSUCCESSFUL;
5796 d_printf("%s\n", str_sid);
5797 return NT_STATUS_OK;
5800 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5804 /* convert sid into ascii string */
5805 sid_to_fstring(ascii_sid, dom_sid);
5807 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
5810 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5811 TALLOC_CTX *mem_ctx,
5812 struct policy_handle *pol,
5814 const char *trusted_dom_name)
5817 union lsa_TrustedDomainInfo *info = NULL;
5818 char *cleartextpwd = NULL;
5819 uint8_t session_key[16];
5820 DATA_BLOB session_key_blob;
5821 DATA_BLOB data = data_blob_null;
5823 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5826 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5828 if (NT_STATUS_IS_ERR(nt_status)) {
5829 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5830 nt_errstr(nt_status)));
5834 data = data_blob(info->password.password->data,
5835 info->password.password->length);
5837 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
5838 DEBUG(0, ("Could not retrieve password hash\n"));
5842 session_key_blob = data_blob_const(session_key, sizeof(session_key));
5843 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
5845 if (cleartextpwd == NULL) {
5846 DEBUG(0,("retrieved NULL password\n"));
5847 nt_status = NT_STATUS_UNSUCCESSFUL;
5851 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5852 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5853 nt_status = NT_STATUS_UNSUCCESSFUL;
5857 #ifdef DEBUG_PASSWORD
5858 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5859 "password: [%s]\n", trusted_dom_name,
5860 sid_string_dbg(&dom_sid), cleartextpwd));
5864 SAFE_FREE(cleartextpwd);
5865 data_blob_free(&data);
5870 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5873 /* common variables */
5874 TALLOC_CTX* mem_ctx;
5875 struct cli_state *cli = NULL;
5876 struct rpc_pipe_client *pipe_hnd = NULL;
5878 const char *domain_name = NULL;
5879 DOM_SID *queried_dom_sid;
5880 struct policy_handle connect_hnd;
5881 union lsa_PolicyInformation *info = NULL;
5883 /* trusted domains listing variables */
5884 unsigned int enum_ctx = 0;
5886 struct lsa_DomainList dom_list;
5889 if (c->display_usage) {
5890 d_printf(_("Usage:\n"),
5891 "net rpc trustdom vampire\n"
5892 " ",_("Vampire trust relationship from remote server\n"));
5897 * Listing trusted domains (stored in secrets.tdb, if local)
5900 mem_ctx = talloc_init("trust relationships vampire");
5903 * set domain and pdc name to local samba server (default)
5904 * or to remote one given in command line
5907 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
5908 domain_name = c->opt_workgroup;
5909 c->opt_target_workgroup = c->opt_workgroup;
5911 fstrcpy(pdc_name, global_myname());
5912 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5913 c->opt_target_workgroup = domain_name;
5916 /* open \PIPE\lsarpc and open policy handle */
5917 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
5918 if (!NT_STATUS_IS_OK(nt_status)) {
5919 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
5920 nt_errstr(nt_status)));
5921 talloc_destroy(mem_ctx);
5925 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5927 if (!NT_STATUS_IS_OK(nt_status)) {
5928 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5929 nt_errstr(nt_status) ));
5931 talloc_destroy(mem_ctx);
5935 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
5937 if (NT_STATUS_IS_ERR(nt_status)) {
5938 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5939 nt_errstr(nt_status)));
5941 talloc_destroy(mem_ctx);
5945 /* query info level 5 to obtain sid of a domain being queried */
5946 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5948 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5951 if (NT_STATUS_IS_ERR(nt_status)) {
5952 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5953 nt_errstr(nt_status)));
5955 talloc_destroy(mem_ctx);
5959 queried_dom_sid = info->account_domain.sid;
5962 * Keep calling LsaEnumTrustdom over opened pipe until
5963 * the end of enumeration is reached
5966 d_printf(_("Vampire trusted domains:\n\n"));
5969 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
5974 if (NT_STATUS_IS_ERR(nt_status)) {
5975 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5976 nt_errstr(nt_status)));
5978 talloc_destroy(mem_ctx);
5982 for (i = 0; i < dom_list.count; i++) {
5984 print_trusted_domain(dom_list.domains[i].sid,
5985 dom_list.domains[i].name.string);
5987 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5988 *dom_list.domains[i].sid,
5989 dom_list.domains[i].name.string);
5990 if (!NT_STATUS_IS_OK(nt_status)) {
5992 talloc_destroy(mem_ctx);
5998 * in case of no trusted domains say something rather
5999 * than just display blank line
6001 if (!dom_list.count) d_printf(_("none\n"));
6003 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6005 /* close this connection before doing next one */
6006 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6007 if (NT_STATUS_IS_ERR(nt_status)) {
6008 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6009 nt_errstr(nt_status)));
6011 talloc_destroy(mem_ctx);
6015 /* close lsarpc pipe and connection to IPC$ */
6018 talloc_destroy(mem_ctx);
6022 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6024 /* common variables */
6025 TALLOC_CTX* mem_ctx;
6026 struct cli_state *cli = NULL, *remote_cli = NULL;
6027 struct rpc_pipe_client *pipe_hnd = NULL;
6029 const char *domain_name = NULL;
6030 DOM_SID *queried_dom_sid;
6031 int ascii_dom_name_len;
6032 struct policy_handle connect_hnd;
6033 union lsa_PolicyInformation *info = NULL;
6035 /* trusted domains listing variables */
6036 unsigned int num_domains, enum_ctx = 0;
6038 struct lsa_DomainList dom_list;
6042 /* trusting domains listing variables */
6043 struct policy_handle domain_hnd;
6044 struct samr_SamArray *trusts = NULL;
6046 if (c->display_usage) {
6047 d_printf(_("Usage:\n"),
6048 "net rpc trustdom list\n"
6049 " ",_("List in- and outgoing trust relationships\n"));
6054 * Listing trusted domains (stored in secrets.tdb, if local)
6057 mem_ctx = talloc_init("trust relationships listing");
6060 * set domain and pdc name to local samba server (default)
6061 * or to remote one given in command line
6064 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6065 domain_name = c->opt_workgroup;
6066 c->opt_target_workgroup = c->opt_workgroup;
6068 fstrcpy(pdc_name, global_myname());
6069 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6070 c->opt_target_workgroup = domain_name;
6073 /* open \PIPE\lsarpc and open policy handle */
6074 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6075 if (!NT_STATUS_IS_OK(nt_status)) {
6076 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6077 nt_errstr(nt_status)));
6078 talloc_destroy(mem_ctx);
6082 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6084 if (!NT_STATUS_IS_OK(nt_status)) {
6085 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6086 nt_errstr(nt_status) ));
6088 talloc_destroy(mem_ctx);
6092 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6094 if (NT_STATUS_IS_ERR(nt_status)) {
6095 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6096 nt_errstr(nt_status)));
6098 talloc_destroy(mem_ctx);
6102 /* query info level 5 to obtain sid of a domain being queried */
6103 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6105 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6108 if (NT_STATUS_IS_ERR(nt_status)) {
6109 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6110 nt_errstr(nt_status)));
6112 talloc_destroy(mem_ctx);
6116 queried_dom_sid = info->account_domain.sid;
6119 * Keep calling LsaEnumTrustdom over opened pipe until
6120 * the end of enumeration is reached
6123 d_printf(_("Trusted domains list:\n\n"));
6125 found_domain = false;
6128 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6133 if (NT_STATUS_IS_ERR(nt_status)) {
6134 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6135 nt_errstr(nt_status)));
6137 talloc_destroy(mem_ctx);
6141 for (i = 0; i < dom_list.count; i++) {
6142 print_trusted_domain(dom_list.domains[i].sid,
6143 dom_list.domains[i].name.string);
6144 found_domain = true;
6148 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6151 * in case of no trusted domains say something rather
6152 * than just display blank line
6154 if (!found_domain) {
6155 d_printf(_("none\n"));
6158 /* close this connection before doing next one */
6159 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6160 if (NT_STATUS_IS_ERR(nt_status)) {
6161 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6162 nt_errstr(nt_status)));
6164 talloc_destroy(mem_ctx);
6168 TALLOC_FREE(pipe_hnd);
6171 * Listing trusting domains (stored in passdb backend, if local)
6174 d_printf(_("\nTrusting domains list:\n\n"));
6177 * Open \PIPE\samr and get needed policy handles
6179 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6181 if (!NT_STATUS_IS_OK(nt_status)) {
6182 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6184 talloc_destroy(mem_ctx);
6189 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6191 SAMR_ACCESS_LOOKUP_DOMAIN,
6193 if (!NT_STATUS_IS_OK(nt_status)) {
6194 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6195 nt_errstr(nt_status)));
6197 talloc_destroy(mem_ctx);
6201 /* SamrOpenDomain - we have to open domain policy handle in order to be
6202 able to enumerate accounts*/
6203 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6205 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6208 if (!NT_STATUS_IS_OK(nt_status)) {
6209 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6210 nt_errstr(nt_status)));
6212 talloc_destroy(mem_ctx);
6217 * perform actual enumeration
6220 found_domain = false;
6222 enum_ctx = 0; /* reset enumeration context from last enumeration */
6225 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6232 if (NT_STATUS_IS_ERR(nt_status)) {
6233 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6234 nt_errstr(nt_status)));
6236 talloc_destroy(mem_ctx);
6240 for (i = 0; i < num_domains; i++) {
6242 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6244 found_domain = true;
6247 * get each single domain's sid (do we _really_ need this ?):
6248 * 1) connect to domain's pdc
6249 * 2) query the pdc for domain's sid
6252 /* get rid of '$' tail */
6253 ascii_dom_name_len = strlen(str);
6254 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6255 str[ascii_dom_name_len - 1] = '\0';
6257 /* set opt_* variables to remote domain */
6259 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6260 c->opt_target_workgroup = c->opt_workgroup;
6262 d_printf("%-20s", str);
6264 /* connect to remote domain controller */
6265 nt_status = net_make_ipc_connection(c,
6266 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6268 if (NT_STATUS_IS_OK(nt_status)) {
6269 /* query for domain's sid */
6270 if (run_rpc_command(
6272 &ndr_table_lsarpc.syntax_id, 0,
6273 rpc_query_domain_sid, argc,
6275 d_printf(_("strange - couldn't get domain's sid\n"));
6277 cli_shutdown(remote_cli);
6280 d_fprintf(stderr, _("domain controller is not "
6281 "responding: %s\n"),
6282 nt_errstr(nt_status));
6283 d_printf(_("couldn't get domain's sid\n"));
6287 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6289 if (!found_domain) {
6293 /* close opened samr and domain policy handles */
6294 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6295 if (!NT_STATUS_IS_OK(nt_status)) {
6296 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6299 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6300 if (!NT_STATUS_IS_OK(nt_status)) {
6301 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6304 /* close samr pipe and connection to IPC$ */
6307 talloc_destroy(mem_ctx);
6312 * Entrypoint for 'net rpc trustdom' code.
6314 * @param argc Standard argc.
6315 * @param argv Standard argv without initial components.
6317 * @return Integer status (0 means success).
6320 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6322 struct functable func[] = {
6327 N_("Add trusting domain's account"),
6328 N_("net rpc trustdom add\n"
6329 " Add trusting domain's account")
6335 N_("Remove trusting domain's account"),
6336 N_("net rpc trustdom del\n"
6337 " Remove trusting domain's account")
6341 rpc_trustdom_establish,
6343 N_("Establish outgoing trust relationship"),
6344 N_("net rpc trustdom establish\n"
6345 " Establish outgoing trust relationship")
6349 rpc_trustdom_revoke,
6351 N_("Revoke outgoing trust relationship"),
6352 N_("net rpc trustdom revoke\n"
6353 " Revoke outgoing trust relationship")
6359 N_("List in- and outgoing domain trusts"),
6360 N_("net rpc trustdom list\n"
6361 " List in- and outgoing domain trusts")
6365 rpc_trustdom_vampire,
6367 N_("Vampire trusts from remote server"),
6368 N_("net rpc trustdom vampire\n"
6369 " Vampire trusts from remote server")
6371 {NULL, NULL, 0, NULL, NULL}
6374 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6378 * Check if a server will take rpc commands
6379 * @param flags Type of server to connect to (PDC, DMB, localhost)
6380 * if the host is not explicitly specified
6381 * @return bool (true means rpc supported)
6383 bool net_rpc_check(struct net_context *c, unsigned flags)
6385 struct cli_state *cli;
6387 struct sockaddr_storage server_ss;
6388 char *server_name = NULL;
6391 /* flags (i.e. server type) may depend on command */
6392 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6395 if ((cli = cli_initialise()) == NULL) {
6399 status = cli_connect(cli, server_name, &server_ss);
6400 if (!NT_STATUS_IS_OK(status))
6402 if (!attempt_netbios_session_request(&cli, global_myname(),
6403 server_name, &server_ss))
6405 status = cli_negprot(cli);
6406 if (!NT_STATUS_IS_OK(status))
6408 if (cli->protocol < PROTOCOL_NT1)
6417 /* dump sam database via samsync rpc calls */
6418 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6419 if (c->display_usage) {
6420 d_printf(_("Usage:\n"),
6422 " ",_("Dump remote SAM database\n"));
6426 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6427 NET_FLAGS_ANONYMOUS,
6428 rpc_samdump_internals, argc, argv);
6431 /* syncronise sam database via samsync rpc calls */
6432 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6434 struct functable func[] = {
6439 N_("Dump remote SAM database to ldif"),
6440 N_("net rpc vampire ldif\n"
6441 " Dump remote SAM database to LDIF file or "
6448 N_("Dump remote SAM database to Kerberos Keytab"),
6449 N_("net rpc vampire keytab\n"
6450 " Dump remote SAM database to Kerberos keytab "
6457 N_("Dump remote SAM database to passdb"),
6458 N_("net rpc vampire passdb\n"
6459 " Dump remote SAM database to passdb")
6462 {NULL, NULL, 0, NULL, NULL}
6466 if (c->display_usage) {
6467 d_printf(_("Usage:\n"),
6469 " ",_("Vampire remote SAM database\n"));
6473 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6474 NET_FLAGS_ANONYMOUS,
6475 rpc_vampire_internals,
6479 return net_run_function(c, argc, argv, "net rpc vampire", func);
6483 * Migrate everything from a print server.
6485 * @param c A net_context structure.
6486 * @param argc Standard main() style argc.
6487 * @param argv Standard main() style argv. Initial components are already
6490 * @return A shell status integer (0 for success).
6492 * The order is important !
6493 * To successfully add drivers the print queues have to exist !
6494 * Applying ACLs should be the last step, because you're easily locked out.
6497 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6502 if (c->display_usage) {
6503 d_printf(_("Usage:\n"),
6504 "net rpc printer migrate all\n"
6505 " ",_("Migrate everything from a print server\n"));
6510 d_printf(_("no server to migrate\n"));
6514 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6515 rpc_printer_migrate_printers_internals, argc,
6520 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6521 rpc_printer_migrate_drivers_internals, argc,
6526 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6527 rpc_printer_migrate_forms_internals, argc, argv);
6531 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6532 rpc_printer_migrate_settings_internals, argc,
6537 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6538 rpc_printer_migrate_security_internals, argc,
6544 * Migrate print drivers from a print server.
6546 * @param c A net_context structure.
6547 * @param argc Standard main() style argc.
6548 * @param argv Standard main() style argv. Initial components are already
6551 * @return A shell status integer (0 for success).
6553 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6556 if (c->display_usage) {
6557 d_printf(_("Usage:\n"),
6558 "net rpc printer migrate drivers\n"
6559 " ",_("Migrate print-drivers from a print-server\n"));
6564 d_printf(_("no server to migrate\n"));
6568 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6569 rpc_printer_migrate_drivers_internals,
6574 * Migrate print-forms from a print-server.
6576 * @param c A net_context structure.
6577 * @param argc Standard main() style argc.
6578 * @param argv Standard main() style argv. Initial components are already
6581 * @return A shell status integer (0 for success).
6583 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6586 if (c->display_usage) {
6587 d_printf(_("Usage:\n"),
6588 "net rpc printer migrate forms\n"
6589 " ",_("Migrate print-forms from a print-server\n"));
6594 d_printf(_("no server to migrate\n"));
6598 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6599 rpc_printer_migrate_forms_internals,
6604 * Migrate printers from a print-server.
6606 * @param c A net_context structure.
6607 * @param argc Standard main() style argc.
6608 * @param argv Standard main() style argv. Initial components are already
6611 * @return A shell status integer (0 for success).
6613 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6616 if (c->display_usage) {
6617 d_printf(_("Usage:\n"),
6618 "net rpc printer migrate printers\n"
6619 " ",_("Migrate printers from a print-server\n"));
6624 d_printf(_("no server to migrate\n"));
6628 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6629 rpc_printer_migrate_printers_internals,
6634 * Migrate printer-ACLs from a print-server
6636 * @param c A net_context structure.
6637 * @param argc Standard main() style argc.
6638 * @param argv Standard main() style argv. Initial components are already
6641 * @return A shell status integer (0 for success).
6643 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6646 if (c->display_usage) {
6647 d_printf(_("Usage:\n"),
6648 "net rpc printer migrate security\n"
6649 " ",_("Migrate printer-ACLs from a print-server\n"));
6654 d_printf(_("no server to migrate\n"));
6658 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6659 rpc_printer_migrate_security_internals,
6664 * Migrate printer-settings from a print-server.
6666 * @param c A net_context structure.
6667 * @param argc Standard main() style argc.
6668 * @param argv Standard main() style argv. Initial components are already
6671 * @return A shell status integer (0 for success).
6673 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6676 if (c->display_usage) {
6677 d_printf(_("Usage:\n"),
6678 "net rpc printer migrate settings\n"
6679 " ",_("Migrate printer-settings from a "
6685 d_printf(_("no server to migrate\n"));
6689 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6690 rpc_printer_migrate_settings_internals,
6695 * 'net rpc printer' entrypoint.
6697 * @param c A net_context structure.
6698 * @param argc Standard main() style argc.
6699 * @param argv Standard main() style argv. Initial components are already
6703 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6706 /* ouch: when addriver and setdriver are called from within
6707 rpc_printer_migrate_drivers_internals, the printer-queue already
6710 struct functable func[] = {
6713 rpc_printer_migrate_all,
6715 N_("Migrate all from remote to local print server"),
6716 N_("net rpc printer migrate all\n"
6717 " Migrate all from remote to local print server")
6721 rpc_printer_migrate_drivers,
6723 N_("Migrate drivers to local server"),
6724 N_("net rpc printer migrate drivers\n"
6725 " Migrate drivers to local server")
6729 rpc_printer_migrate_forms,
6731 N_("Migrate froms to local server"),
6732 N_("net rpc printer migrate forms\n"
6733 " Migrate froms to local server")
6737 rpc_printer_migrate_printers,
6739 N_("Migrate printers to local server"),
6740 N_("net rpc printer migrate printers\n"
6741 " Migrate printers to local server")
6745 rpc_printer_migrate_security,
6747 N_("Mirgate printer ACLs to local server"),
6748 N_("net rpc printer migrate security\n"
6749 " Mirgate printer ACLs to local server")
6753 rpc_printer_migrate_settings,
6755 N_("Migrate printer settings to local server"),
6756 N_("net rpc printer migrate settings\n"
6757 " Migrate printer settings to local server")
6759 {NULL, NULL, 0, NULL, NULL}
6762 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6767 * List printers on a remote RPC server.
6769 * @param c A net_context structure.
6770 * @param argc Standard main() style argc.
6771 * @param argv Standard main() style argv. Initial components are already
6774 * @return A shell status integer (0 for success).
6776 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6778 if (c->display_usage) {
6779 d_printf(_("Usage:\n"),
6780 "net rpc printer list\n"
6781 " ",_("List printers on a remote RPC server\n"));
6785 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6786 rpc_printer_list_internals,
6791 * List printer-drivers on a remote RPC server.
6793 * @param c A net_context structure.
6794 * @param argc Standard main() style argc.
6795 * @param argv Standard main() style argv. Initial components are already
6798 * @return A shell status integer (0 for success).
6800 static int rpc_printer_driver_list(struct net_context *c, int argc,
6803 if (c->display_usage) {
6804 d_printf(_("Usage:\n"),
6805 "net rpc printer driver\n"
6806 " ",_("List printer-drivers on a remote RPC server\n"));
6810 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6811 rpc_printer_driver_list_internals,
6816 * Publish printer in ADS via MSRPC.
6818 * @param c A net_context structure.
6819 * @param argc Standard main() style argc.
6820 * @param argv Standard main() style argv. Initial components are already
6823 * @return A shell status integer (0 for success).
6825 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6828 if (c->display_usage) {
6829 d_printf(_("Usage:\n"),
6830 "net rpc printer publish publish\n"
6831 " ",_("Publish printer in ADS via MSRPC\n"));
6835 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6836 rpc_printer_publish_publish_internals,
6841 * Update printer in ADS via MSRPC.
6843 * @param c A net_context structure.
6844 * @param argc Standard main() style argc.
6845 * @param argv Standard main() style argv. Initial components are already
6848 * @return A shell status integer (0 for success).
6850 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6852 if (c->display_usage) {
6853 d_printf(_("Usage:\n"),
6854 "net rpc printer publish update\n"
6855 " ",_("Update printer in ADS via MSRPC\n"));
6859 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6860 rpc_printer_publish_update_internals,
6865 * UnPublish printer in ADS via MSRPC.
6867 * @param c A net_context structure.
6868 * @param argc Standard main() style argc.
6869 * @param argv Standard main() style argv. Initial components are already
6872 * @return A shell status integer (0 for success).
6874 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
6877 if (c->display_usage) {
6878 d_printf(_("Usage:\n"),
6879 "net rpc printer publish unpublish\n"
6880 " ",_("UnPublish printer in ADS via MSRPC\n"));
6884 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6885 rpc_printer_publish_unpublish_internals,
6890 * List published printers via MSRPC.
6892 * @param c A net_context structure.
6893 * @param argc Standard main() style argc.
6894 * @param argv Standard main() style argv. Initial components are already
6897 * @return A shell status integer (0 for success).
6899 static int rpc_printer_publish_list(struct net_context *c, int argc,
6902 if (c->display_usage) {
6903 d_printf(_("Usage:\n"),
6904 "net rpc printer publish list\n"
6905 " ",_("List published printers via MSRPC\n"));
6909 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6910 rpc_printer_publish_list_internals,
6916 * Publish printer in ADS.
6918 * @param c A net_context structure.
6919 * @param argc Standard main() style argc.
6920 * @param argv Standard main() style argv. Initial components are already
6923 * @return A shell status integer (0 for success).
6925 static int rpc_printer_publish(struct net_context *c, int argc,
6929 struct functable func[] = {
6932 rpc_printer_publish_publish,
6934 N_("Publish printer in AD"),
6935 N_("net rpc printer publish publish\n"
6936 " Publish printer in AD")
6940 rpc_printer_publish_update,
6942 N_("Update printer in AD"),
6943 N_("net rpc printer publish update\n"
6944 " Update printer in AD")
6948 rpc_printer_publish_unpublish,
6950 N_("Unpublish printer"),
6951 N_("net rpc printer publish unpublish\n"
6952 " Unpublish printer")
6956 rpc_printer_publish_list,
6958 N_("List published printers"),
6959 N_("net rpc printer publish list\n"
6960 " List published printers")
6962 {NULL, NULL, 0, NULL, NULL}
6966 if (c->display_usage) {
6967 d_printf(_("Usage:\n"));
6968 d_printf(_("net rpc printer publish\n"
6969 " List published printers\n"
6970 " Alias of net rpc printer publish "
6972 net_display_usage_from_functable(func);
6975 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6976 rpc_printer_publish_list_internals,
6980 return net_run_function(c, argc, argv, "net rpc printer publish",func);
6986 * Display rpc printer help page.
6988 * @param c A net_context structure.
6989 * @param argc Standard main() style argc.
6990 * @param argv Standard main() style argv. Initial components are already
6993 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
6995 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
6996 "\tlists all printers on print-server\n\n"));
6997 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
6998 "\tlists all printer-drivers on print-server\n\n"));
6999 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7000 "\tpublishes printer settings in Active Directory\n"
7001 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7002 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7003 "\n\tmigrates printers from remote to local server\n\n"));
7004 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7005 "\n\tmigrates printer-settings from remote to local server\n\n"));
7006 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7007 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7008 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7009 "\n\tmigrates printer-forms from remote to local server\n\n"));
7010 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7011 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7012 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7013 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7014 "\tremote to local print-server\n\n"));
7015 net_common_methods_usage(c, argc, argv);
7016 net_common_flags_usage(c, argc, argv);
7018 "\t-v or --verbose\t\t\tgive verbose output\n"
7019 "\t --destination\t\tmigration target server (default: localhost)\n"));
7025 * 'net rpc printer' entrypoint.
7027 * @param c A net_context structure.
7028 * @param argc Standard main() style argc.
7029 * @param argv Standard main() style argv. Initial components are already
7032 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7034 struct functable func[] = {
7039 N_("List all printers on print server"),
7040 N_("net rpc printer list\n"
7041 " List all printers on print server")
7045 rpc_printer_migrate,
7047 N_("Migrate printer to local server"),
7048 N_("net rpc printer migrate\n"
7049 " Migrate printer to local server")
7053 rpc_printer_driver_list,
7055 N_("List printer drivers"),
7056 N_("net rpc printer driver\n"
7057 " List printer drivers")
7061 rpc_printer_publish,
7063 N_("Publish printer in AD"),
7064 N_("net rpc printer publish\n"
7065 " Publish printer in AD")
7067 {NULL, NULL, 0, NULL, NULL}
7071 if (c->display_usage) {
7072 d_printf(_("Usage:\n"));
7073 d_printf(_("net rpc printer\n"
7074 " List printers\n"));
7075 net_display_usage_from_functable(func);
7078 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7079 rpc_printer_list_internals,
7083 return net_run_function(c, argc, argv, "net rpc printer", func);
7087 * 'net rpc' entrypoint.
7089 * @param c A net_context structure.
7090 * @param argc Standard main() style argc.
7091 * @param argv Standard main() style argv. Initial components are already
7095 int net_rpc(struct net_context *c, int argc, const char **argv)
7097 NET_API_STATUS status;
7099 struct functable func[] = {
7104 N_("Modify global audit settings"),
7105 N_("net rpc audit\n"
7106 " Modify global audit settings")
7112 N_("Show basic info about a domain"),
7114 " Show basic info about a domain")
7120 N_("Join a domain"),
7128 N_("Join a domain created in server manager"),
7129 N_("net rpc oldjoin\n"
7130 " Join a domain created in server manager")
7136 N_("Test that a join is valid"),
7137 N_("net rpc testjoin\n"
7138 " Test that a join is valid")
7144 N_("List/modify users"),
7146 " List/modify users")
7152 N_("Change a user password"),
7153 N_("net rpc password\n"
7154 " Change a user password\n"
7155 " Alias for net rpc user password")
7161 N_("List/modify groups"),
7162 N_("net rpc group\n"
7163 " List/modify groups")
7169 N_("List/modify shares"),
7170 N_("net rpc share\n"
7171 " List/modify shares")
7177 N_("List open files"),
7185 N_("List/modify printers"),
7186 N_("net rpc printer\n"
7187 " List/modify printers")
7191 net_rpc_changetrustpw,
7193 N_("Change trust account password"),
7194 N_("net rpc changetrustpw\n"
7195 " Change trust account password")
7201 N_("Modify domain trusts"),
7202 N_("net rpc trustdom\n"
7203 " Modify domain trusts")
7209 N_("Abort a remote shutdown"),
7210 N_("net rpc abortshutdown\n"
7211 " Abort a remote shutdown")
7217 N_("Shutdown a remote server"),
7218 N_("net rpc shutdown\n"
7219 " Shutdown a remote server")
7225 N_("Dump SAM data of remote NT PDC"),
7226 N_("net rpc samdump\n"
7227 " Dump SAM data of remote NT PDC")
7233 N_("Sync a remote NT PDC's data into local passdb"),
7234 N_("net rpc vampire\n"
7235 " Sync a remote NT PDC's data into local passdb")
7241 N_("Fetch the domain sid into local secrets.tdb"),
7242 N_("net rpc getsid\n"
7243 " Fetch the domain sid into local secrets.tdb")
7249 N_("Manage privileges assigned to SID"),
7250 N_("net rpc rights\n"
7251 " Manage privileges assigned to SID")
7257 N_("Start/stop/query remote services"),
7258 N_("net rpc service\n"
7259 " Start/stop/query remote services")
7265 N_("Manage registry hives"),
7266 N_("net rpc registry\n"
7267 " Manage registry hives")
7273 N_("Open interactive shell on remote server"),
7274 N_("net rpc shell\n"
7275 " Open interactive shell on remote server")
7277 {NULL, NULL, 0, NULL, NULL}
7280 status = libnetapi_init(&c->netapi_ctx);
7284 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
7285 libnetapi_set_password(c->netapi_ctx, c->opt_password);
7286 if (c->opt_kerberos) {
7287 libnetapi_set_use_kerberos(c->netapi_ctx);
7290 return net_run_function(c, argc, argv, "net rpc", func);