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 "rpc_client/cli_samr.h"
28 #include "rpc_client/init_samr.h"
29 #include "../librpc/gen_ndr/cli_lsa.h"
30 #include "rpc_client/cli_lsarpc.h"
31 #include "../librpc/gen_ndr/cli_netlogon.h"
32 #include "../librpc/gen_ndr/cli_srvsvc.h"
33 #include "../librpc/gen_ndr/cli_spoolss.h"
34 #include "../librpc/gen_ndr/ndr_initshutdown_c.h"
35 #include "../librpc/gen_ndr/cli_winreg.h"
37 #include "lib/netapi/netapi.h"
38 #include "lib/netapi/netapi_net.h"
39 #include "rpc_client/init_lsa.h"
40 #include "../libcli/security/security.h"
42 static int net_mode_share;
43 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask);
48 * @brief RPC based subcommands for the 'net' utility.
50 * This file should contain much of the functionality that used to
51 * be found in rpcclient, execpt that the commands should change
52 * less often, and the fucntionality should be sane (the user is not
53 * expected to know a rid/sid before they conduct an operation etc.)
55 * @todo Perhaps eventually these should be split out into a number
56 * of files, as this could get quite big.
61 * Many of the RPC functions need the domain sid. This function gets
62 * it at the start of every run
64 * @param cli A cli_state already connected to the remote machine
66 * @return The Domain SID of the remote machine.
69 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
70 struct dom_sid **domain_sid,
71 const char **domain_name)
73 struct rpc_pipe_client *lsa_pipe = NULL;
74 struct policy_handle pol;
75 NTSTATUS result = NT_STATUS_OK;
76 union lsa_PolicyInformation *info = NULL;
78 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
80 if (!NT_STATUS_IS_OK(result)) {
81 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
85 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
86 SEC_FLAG_MAXIMUM_ALLOWED,
88 if (!NT_STATUS_IS_OK(result)) {
89 d_fprintf(stderr, "open_policy %s: %s\n",
95 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
97 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
99 if (!NT_STATUS_IS_OK(result)) {
100 d_fprintf(stderr, "lsaquery %s: %s\n",
106 *domain_name = info->account_domain.name.string;
107 *domain_sid = info->account_domain.sid;
109 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
110 TALLOC_FREE(lsa_pipe);
116 * Run a single RPC command, from start to finish.
118 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
119 * @param conn_flag a NET_FLAG_ combination. Passed to
120 * net_make_ipc_connection.
121 * @param argc Standard main() style argc.
122 * @param argv Standard main() style argv. Initial components are already
124 * @return A shell status integer (0 for success).
127 int run_rpc_command(struct net_context *c,
128 struct cli_state *cli_arg,
129 const struct ndr_syntax_id *interface,
135 struct cli_state *cli = NULL;
136 struct rpc_pipe_client *pipe_hnd = NULL;
139 struct dom_sid *domain_sid;
140 const char *domain_name;
143 /* make use of cli_state handed over as an argument, if possible */
145 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
146 if (!NT_STATUS_IS_OK(nt_status)) {
147 DEBUG(1, ("failed to make ipc connection: %s\n",
148 nt_errstr(nt_status)));
161 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
162 DEBUG(0, ("talloc_init() failed\n"));
166 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
168 if (!NT_STATUS_IS_OK(nt_status)) {
172 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
173 if (lp_client_schannel()
174 && (ndr_syntax_id_equal(interface,
175 &ndr_table_netlogon.syntax_id))) {
176 /* Always try and create an schannel netlogon pipe. */
177 nt_status = cli_rpc_pipe_open_schannel(
178 cli, interface, NCACN_NP,
179 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
181 if (!NT_STATUS_IS_OK(nt_status)) {
182 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
183 nt_errstr(nt_status) ));
187 if (conn_flags & NET_FLAGS_SEAL) {
188 nt_status = cli_rpc_pipe_open_ntlmssp(
190 (conn_flags & NET_FLAGS_TCP) ?
191 NCACN_IP_TCP : NCACN_NP,
192 DCERPC_AUTH_LEVEL_PRIVACY,
193 lp_workgroup(), c->opt_user_name,
194 c->opt_password, &pipe_hnd);
196 nt_status = cli_rpc_pipe_open_noauth(
200 if (!NT_STATUS_IS_OK(nt_status)) {
201 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
202 get_pipe_name_from_syntax(
203 talloc_tos(), interface),
204 nt_errstr(nt_status) ));
210 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
212 if (!NT_STATUS_IS_OK(nt_status)) {
213 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
216 DEBUG(5, ("rpc command function succedded\n"));
219 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
221 TALLOC_FREE(pipe_hnd);
226 /* close the connection only if it was opened here */
231 talloc_destroy(mem_ctx);
236 * Force a change of the trust acccount password.
238 * All parameters are provided by the run_rpc_command function, except for
239 * argc, argv which are passed through.
241 * @param domain_sid The domain sid acquired from the remote server.
242 * @param cli A cli_state connected to the server.
243 * @param mem_ctx Talloc context, destroyed on completion of the function.
244 * @param argc Standard main() style argc.
245 * @param argv Standard main() style argv. Initial components are already
248 * @return Normal NTSTATUS return.
251 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
252 const struct dom_sid *domain_sid,
253 const char *domain_name,
254 struct cli_state *cli,
255 struct rpc_pipe_client *pipe_hnd,
262 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
263 if (!NT_STATUS_IS_OK(status)) {
264 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
273 * Force a change of the trust acccount password.
275 * @param argc Standard main() style argc.
276 * @param argv Standard main() style argv. Initial components are already
279 * @return A shell status integer (0 for success).
282 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
284 if (c->display_usage) {
286 "net rpc changetrustpw\n"
289 _("Change the machine trust password"));
293 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
294 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
295 rpc_changetrustpw_internals,
300 * Join a domain, the old way.
302 * This uses 'machinename' as the inital password, and changes it.
304 * The password should be created with 'server manager' or equiv first.
306 * All parameters are provided by the run_rpc_command function, except for
307 * argc, argv which are passed through.
309 * @param domain_sid The domain sid acquired from the remote server.
310 * @param cli A cli_state connected to the server.
311 * @param mem_ctx Talloc context, destroyed on completion of the function.
312 * @param argc Standard main() style argc.
313 * @param argv Standard main() style argv. Initial components are already
316 * @return Normal NTSTATUS return.
319 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
320 const struct dom_sid *domain_sid,
321 const char *domain_name,
322 struct cli_state *cli,
323 struct rpc_pipe_client *pipe_hnd,
329 fstring trust_passwd;
330 unsigned char orig_trust_passwd_hash[16];
332 enum netr_SchannelType sec_channel_type;
334 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
336 if (!NT_STATUS_IS_OK(result)) {
337 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
340 nt_errstr(result) ));
345 check what type of join - if the user want's to join as
346 a BDC, the server must agree that we are a BDC.
349 sec_channel_type = get_sec_channel_type(argv[0]);
351 sec_channel_type = get_sec_channel_type(NULL);
354 fstrcpy(trust_passwd, global_myname());
355 strlower_m(trust_passwd);
358 * Machine names can be 15 characters, but the max length on
359 * a password is 14. --jerry
362 trust_passwd[14] = '\0';
364 E_md4hash(trust_passwd, orig_trust_passwd_hash);
366 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
368 orig_trust_passwd_hash,
371 if (NT_STATUS_IS_OK(result))
372 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
375 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
376 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
377 result = NT_STATUS_UNSUCCESSFUL;
384 * Join a domain, the old way.
386 * @param argc Standard main() style argc.
387 * @param argv Standard main() style argv. Initial components are already
390 * @return A shell status integer (0 for success).
393 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
395 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
396 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
397 rpc_oldjoin_internals,
402 * Join a domain, the old way. This function exists to allow
403 * the message to be displayed when oldjoin was explicitly
404 * requested, but not when it was implied by "net rpc join".
406 * @param argc Standard main() style argc.
407 * @param argv Standard main() style argv. Initial components are already
410 * @return A shell status integer (0 for success).
413 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
417 if (c->display_usage) {
422 _("Join a domain the old way"));
426 rc = net_rpc_perform_oldjoin(c, argc, argv);
429 d_fprintf(stderr, _("Failed to join domain\n"));
436 * 'net rpc join' entrypoint.
437 * @param argc Standard main() style argc.
438 * @param argv Standard main() style argv. Initial components are already
441 * Main 'net_rpc_join()' (where the admin username/password is used) is
443 * Try to just change the password, but if that doesn't work, use/prompt
444 * for a username/password.
447 int net_rpc_join(struct net_context *c, int argc, const char **argv)
449 if (c->display_usage) {
452 _("net rpc join -U <username>[%%password] <type>\n"
454 " username\tName of the admin user"
455 " password\tPassword of the admin user, will "
456 "prompt if not specified\n"
457 " type\tCan be one of the following:\n"
458 "\t\tMEMBER\tJoin as member server (default)\n"
459 "\t\tBDC\tJoin as BDC\n"
460 "\t\tPDC\tJoin as PDC\n"));
464 if (lp_server_role() == ROLE_STANDALONE) {
465 d_printf(_("cannot join as standalone machine\n"));
469 if (strlen(global_myname()) > 15) {
470 d_printf(_("Our netbios name can be at most 15 chars long, "
471 "\"%s\" is %u chars long\n"),
472 global_myname(), (unsigned int)strlen(global_myname()));
476 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
479 return net_rpc_join_newstyle(c, argc, argv);
483 * display info about a rpc domain
485 * All parameters are provided by the run_rpc_command function, except for
486 * argc, argv which are passed through.
488 * @param domain_sid The domain sid acquired from the remote server
489 * @param cli A cli_state connected to the server.
490 * @param mem_ctx Talloc context, destroyed on completion of the function.
491 * @param argc Standard main() style argc.
492 * @param argv Standard main() style argv. Initial components are already
495 * @return Normal NTSTATUS return.
498 NTSTATUS rpc_info_internals(struct net_context *c,
499 const struct dom_sid *domain_sid,
500 const char *domain_name,
501 struct cli_state *cli,
502 struct rpc_pipe_client *pipe_hnd,
507 struct policy_handle connect_pol, domain_pol;
508 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
509 union samr_DomainInfo *info = NULL;
512 sid_to_fstring(sid_str, domain_sid);
514 /* Get sam policy handle */
515 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
517 MAXIMUM_ALLOWED_ACCESS,
519 if (!NT_STATUS_IS_OK(result)) {
520 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
525 /* Get domain policy handle */
526 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
528 MAXIMUM_ALLOWED_ACCESS,
529 CONST_DISCARD(struct dom_sid2 *, domain_sid),
531 if (!NT_STATUS_IS_OK(result)) {
532 d_fprintf(stderr, _("Could not open domain: %s\n"),
537 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
541 if (NT_STATUS_IS_OK(result)) {
542 d_printf(_("Domain Name: %s\n"),
543 info->general.domain_name.string);
544 d_printf(_("Domain SID: %s\n"), sid_str);
545 d_printf(_("Sequence number: %llu\n"),
546 (unsigned long long)info->general.sequence_num);
547 d_printf(_("Num users: %u\n"), info->general.num_users);
548 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
549 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
557 * 'net rpc info' entrypoint.
558 * @param argc Standard main() style argc.
559 * @param argv Standard main() style argv. Initial components are already
563 int net_rpc_info(struct net_context *c, int argc, const char **argv)
565 if (c->display_usage) {
570 _("Display information about the domain"));
574 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
575 NET_FLAGS_PDC, rpc_info_internals,
580 * Fetch domain SID into the local secrets.tdb.
582 * All parameters are provided by the run_rpc_command function, except for
583 * argc, argv which are passed through.
585 * @param domain_sid The domain sid acquired from the remote server.
586 * @param cli A cli_state connected to the server.
587 * @param mem_ctx Talloc context, destroyed on completion of the function.
588 * @param argc Standard main() style argc.
589 * @param argv Standard main() style argv. Initial components are already
592 * @return Normal NTSTATUS return.
595 static NTSTATUS rpc_getsid_internals(struct net_context *c,
596 const struct dom_sid *domain_sid,
597 const char *domain_name,
598 struct cli_state *cli,
599 struct rpc_pipe_client *pipe_hnd,
606 sid_to_fstring(sid_str, domain_sid);
607 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
608 sid_str, domain_name);
610 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
611 DEBUG(0,("Can't store domain SID\n"));
612 return NT_STATUS_UNSUCCESSFUL;
619 * 'net rpc getsid' entrypoint.
620 * @param argc Standard main() style argc.
621 * @param argv Standard main() style argv. Initial components are already
625 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
627 int conn_flags = NET_FLAGS_PDC;
629 if (!c->opt_user_specified) {
630 conn_flags |= NET_FLAGS_ANONYMOUS;
633 if (c->display_usage) {
638 _("Fetch domain SID into local secrets.tdb"));
642 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
644 rpc_getsid_internals,
648 /****************************************************************************/
651 * Basic usage function for 'net rpc user'.
652 * @param argc Standard main() style argc.
653 * @param argv Standard main() style argv. Initial components are already
657 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
659 return net_user_usage(c, argc, argv);
663 * Add a new user to a remote RPC server.
665 * @param argc Standard main() style argc.
666 * @param argv Standard main() style argv. Initial components are already
669 * @return A shell status integer (0 for success).
672 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
674 NET_API_STATUS status;
675 struct USER_INFO_1 info1;
676 uint32_t parm_error = 0;
678 if (argc < 1 || c->display_usage) {
679 rpc_user_usage(c, argc, argv);
685 info1.usri1_name = argv[0];
687 info1.usri1_password = argv[1];
690 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
693 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
694 argv[0], libnetapi_get_error_string(c->netapi_ctx,
698 d_printf(_("Added user '%s'.\n"), argv[0]);
705 * Rename a user on a remote RPC server.
707 * @param argc Standard main() style argc.
708 * @param argv Standard main() style argv. Initial components are already
711 * @return A shell status integer (0 for success).
714 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
716 NET_API_STATUS status;
717 struct USER_INFO_0 u0;
718 uint32_t parm_err = 0;
720 if (argc != 2 || c->display_usage) {
721 rpc_user_usage(c, argc, argv);
725 u0.usri0_name = argv[1];
727 status = NetUserSetInfo(c->opt_host, argv[0],
728 0, (uint8_t *)&u0, &parm_err);
731 _("Failed to rename user from %s to %s - %s\n"),
733 libnetapi_get_error_string(c->netapi_ctx, status));
735 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
742 * Set a user's primary group
744 * @param argc Standard main() style argc.
745 * @param argv Standard main() style argv. Initial components are already
748 * @return A shell status integer (0 for success).
751 static int rpc_user_setprimarygroup(struct net_context *c, int argc,
754 NET_API_STATUS status;
756 struct GROUP_INFO_2 *g2;
757 struct USER_INFO_1051 u1051;
758 uint32_t parm_err = 0;
760 if (argc != 2 || c->display_usage) {
761 rpc_user_usage(c, argc, argv);
765 status = NetGroupGetInfo(c->opt_host, argv[1], 2, &buffer);
767 d_fprintf(stderr, _("Failed to find group name %s -- %s\n"),
769 libnetapi_get_error_string(c->netapi_ctx, status));
772 g2 = (struct GROUP_INFO_2 *)buffer;
774 u1051.usri1051_primary_group_id = g2->grpi2_group_id;
776 NetApiBufferFree(buffer);
778 status = NetUserSetInfo(c->opt_host, argv[0], 1051,
779 (uint8_t *)&u1051, &parm_err);
782 _("Failed to set user's primary group %s to %s - "
783 "%s\n"), argv[0], argv[1],
784 libnetapi_get_error_string(c->netapi_ctx, status));
786 d_printf(_("Set primary group of user %s to %s\n"), argv[0],
793 * Delete a user from a remote RPC server.
795 * @param argc Standard main() style argc.
796 * @param argv Standard main() style argv. Initial components are already
799 * @return A shell status integer (0 for success).
802 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
804 NET_API_STATUS status;
806 if (argc < 1 || c->display_usage) {
807 rpc_user_usage(c, argc, argv);
811 status = NetUserDel(c->opt_host, argv[0]);
814 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
816 libnetapi_get_error_string(c->netapi_ctx, status));
819 d_printf(_("Deleted user '%s'.\n"), argv[0]);
826 * Set a user's password on a remote RPC server.
828 * @param argc Standard main() style argc.
829 * @param argv Standard main() style argv. Initial components are already
832 * @return A shell status integer (0 for success).
835 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
837 NET_API_STATUS status;
839 struct USER_INFO_1003 u1003;
840 uint32_t parm_err = 0;
843 if (argc < 1 || c->display_usage) {
844 rpc_user_usage(c, argc, argv);
849 u1003.usri1003_password = argv[1];
851 ret = asprintf(&prompt, _("Enter new password for %s:"),
856 u1003.usri1003_password = talloc_strdup(c, getpass(prompt));
858 if (u1003.usri1003_password == NULL) {
863 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
865 /* Display results */
868 _("Failed to set password for '%s' with error: %s.\n"),
869 argv[0], libnetapi_get_error_string(c->netapi_ctx,
878 * List a user's groups from a remote RPC server.
880 * @param argc Standard main() style argc.
881 * @param argv Standard main() style argv. Initial components are already
884 * @return A shell status integer (0 for success)
887 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
890 NET_API_STATUS status;
891 struct GROUP_USERS_INFO_0 *u0 = NULL;
892 uint32_t entries_read = 0;
893 uint32_t total_entries = 0;
897 if (argc < 1 || c->display_usage) {
898 rpc_user_usage(c, argc, argv);
902 status = NetUserGetGroups(c->opt_host,
905 (uint8_t **)(void *)&u0,
911 _("Failed to get groups for '%s' with error: %s.\n"),
912 argv[0], libnetapi_get_error_string(c->netapi_ctx,
917 for (i=0; i < entries_read; i++) {
918 printf("%s\n", u0->grui0_name);
926 * List users on a remote RPC server.
928 * All parameters are provided by the run_rpc_command function, except for
929 * argc, argv which are passed through.
931 * @param domain_sid The domain sid acquired from the remote server.
932 * @param cli A cli_state connected to the server.
933 * @param mem_ctx Talloc context, destroyed on completion of the function.
934 * @param argc Standard main() style argc.
935 * @param argv Standard main() style argv. Initial components are already
938 * @return Normal NTSTATUS return.
941 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
943 NET_API_STATUS status;
944 uint32_t start_idx=0, num_entries, i, loop_count = 0;
945 struct NET_DISPLAY_USER *info = NULL;
948 /* Query domain users */
949 if (c->opt_long_list_entries)
950 d_printf(_("\nUser name Comment"
951 "\n-----------------------------\n"));
953 uint32_t max_entries, max_size;
955 get_query_dispinfo_params(
956 loop_count, &max_entries, &max_size);
958 status = NetQueryDisplayInformation(c->opt_host,
965 if (status != 0 && status != ERROR_MORE_DATA) {
969 info = (struct NET_DISPLAY_USER *)buffer;
971 for (i = 0; i < num_entries; i++) {
973 if (c->opt_long_list_entries)
974 printf("%-21.21s %s\n", info->usri1_name,
975 info->usri1_comment);
977 printf("%s\n", info->usri1_name);
981 NetApiBufferFree(buffer);
984 start_idx += num_entries;
986 } while (status == ERROR_MORE_DATA);
992 * 'net rpc user' entrypoint.
993 * @param argc Standard main() style argc.
994 * @param argv Standard main() style argv. Initial components are already
998 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1000 NET_API_STATUS status;
1002 struct functable func[] = {
1007 N_("Add specified user"),
1008 N_("net rpc user add\n"
1009 " Add specified user")
1015 N_("List domain groups of user"),
1016 N_("net rpc user info\n"
1017 " List domain groups of user")
1023 N_("Remove specified user"),
1024 N_("net rpc user delete\n"
1025 " Remove specified user")
1031 N_("Change user password"),
1032 N_("net rpc user password\n"
1033 " Change user password")
1039 N_("Rename specified user"),
1040 N_("net rpc user rename\n"
1041 " Rename specified user")
1045 rpc_user_setprimarygroup,
1047 "Set a user's primary group",
1048 "net rpc user setprimarygroup\n"
1049 " Set a user's primary group"
1051 {NULL, NULL, 0, NULL, NULL}
1054 status = libnetapi_net_init(&c->netapi_ctx);
1058 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1059 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1060 if (c->opt_kerberos) {
1061 libnetapi_set_use_kerberos(c->netapi_ctx);
1065 if (c->display_usage) {
1070 _("List all users"));
1071 net_display_usage_from_functable(func);
1075 return rpc_user_list(c, argc, argv);
1078 return net_run_function(c, argc, argv, "net rpc user", func);
1081 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1082 TALLOC_CTX *mem_ctx,
1083 struct rpc_sh_ctx *ctx,
1084 struct rpc_pipe_client *pipe_hnd,
1085 int argc, const char **argv)
1087 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1090 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1091 TALLOC_CTX *mem_ctx,
1092 struct rpc_sh_ctx *ctx,
1093 struct rpc_pipe_client *pipe_hnd,
1094 int argc, const char **argv)
1096 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1099 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1100 TALLOC_CTX *mem_ctx,
1101 struct rpc_sh_ctx *ctx,
1102 struct rpc_pipe_client *pipe_hnd,
1103 int argc, const char **argv,
1105 struct net_context *c,
1106 TALLOC_CTX *mem_ctx,
1107 struct rpc_sh_ctx *ctx,
1108 struct rpc_pipe_client *pipe_hnd,
1109 struct policy_handle *user_hnd,
1110 int argc, const char **argv))
1112 struct policy_handle connect_pol, domain_pol, user_pol;
1113 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1116 enum lsa_SidType type;
1119 d_fprintf(stderr, "%s %s <username>\n", _("Usage:"),
1121 return NT_STATUS_INVALID_PARAMETER;
1124 ZERO_STRUCT(connect_pol);
1125 ZERO_STRUCT(domain_pol);
1126 ZERO_STRUCT(user_pol);
1128 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1129 argv[0], NULL, NULL, &sid, &type);
1130 if (!NT_STATUS_IS_OK(result)) {
1131 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1136 if (type != SID_NAME_USER) {
1137 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1138 sid_type_lookup(type));
1139 result = NT_STATUS_NO_SUCH_USER;
1143 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1144 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1145 result = NT_STATUS_NO_SUCH_USER;
1149 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1151 MAXIMUM_ALLOWED_ACCESS,
1153 if (!NT_STATUS_IS_OK(result)) {
1157 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1159 MAXIMUM_ALLOWED_ACCESS,
1162 if (!NT_STATUS_IS_OK(result)) {
1166 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1168 MAXIMUM_ALLOWED_ACCESS,
1171 if (!NT_STATUS_IS_OK(result)) {
1175 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1178 if (is_valid_policy_hnd(&user_pol)) {
1179 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1181 if (is_valid_policy_hnd(&domain_pol)) {
1182 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1184 if (is_valid_policy_hnd(&connect_pol)) {
1185 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1190 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1191 TALLOC_CTX *mem_ctx,
1192 struct rpc_sh_ctx *ctx,
1193 struct rpc_pipe_client *pipe_hnd,
1194 struct policy_handle *user_hnd,
1195 int argc, const char **argv)
1198 union samr_UserInfo *info = NULL;
1201 d_fprintf(stderr, "%s %s show <username>\n", _("Usage:"),
1203 return NT_STATUS_INVALID_PARAMETER;
1206 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1210 if (!NT_STATUS_IS_OK(result)) {
1214 d_printf(_("user rid: %d, group rid: %d\n"),
1216 info->info21.primary_gid);
1221 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1222 TALLOC_CTX *mem_ctx,
1223 struct rpc_sh_ctx *ctx,
1224 struct rpc_pipe_client *pipe_hnd,
1225 int argc, const char **argv)
1227 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1228 rpc_sh_user_show_internals);
1231 #define FETCHSTR(name, rec) \
1232 do { if (strequal(ctx->thiscmd, name)) { \
1233 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1236 #define SETSTR(name, rec, flag) \
1237 do { if (strequal(ctx->thiscmd, name)) { \
1238 init_lsa_String(&(info->info21.rec), argv[0]); \
1239 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1242 static NTSTATUS rpc_sh_user_str_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 = "";
1252 union samr_UserInfo *info = NULL;
1255 d_fprintf(stderr, "%s %s <username> [new value|NULL]\n",
1256 _("Usage:"), ctx->whoami);
1257 return NT_STATUS_INVALID_PARAMETER;
1260 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1264 if (!NT_STATUS_IS_OK(result)) {
1268 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1270 FETCHSTR("fullname", full_name);
1271 FETCHSTR("homedir", home_directory);
1272 FETCHSTR("homedrive", home_drive);
1273 FETCHSTR("logonscript", logon_script);
1274 FETCHSTR("profilepath", profile_path);
1275 FETCHSTR("description", description);
1278 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1282 if (strcmp(argv[0], "NULL") == 0) {
1286 ZERO_STRUCT(info->info21);
1288 SETSTR("fullname", full_name, FULL_NAME);
1289 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1290 SETSTR("homedrive", home_drive, HOME_DRIVE);
1291 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1292 SETSTR("profilepath", profile_path, PROFILE_PATH);
1293 SETSTR("description", description, DESCRIPTION);
1295 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1300 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1301 ctx->thiscmd, oldval, argv[0]);
1308 #define HANDLEFLG(name, rec) \
1309 do { if (strequal(ctx->thiscmd, name)) { \
1310 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1312 newflags = oldflags | ACB_##rec; \
1314 newflags = oldflags & ~ACB_##rec; \
1317 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1318 TALLOC_CTX *mem_ctx,
1319 struct rpc_sh_ctx *ctx,
1320 struct rpc_pipe_client *pipe_hnd,
1321 int argc, const char **argv)
1323 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1324 rpc_sh_user_str_edit_internals);
1327 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1328 TALLOC_CTX *mem_ctx,
1329 struct rpc_sh_ctx *ctx,
1330 struct rpc_pipe_client *pipe_hnd,
1331 struct policy_handle *user_hnd,
1332 int argc, const char **argv)
1335 const char *username;
1336 const char *oldval = "unknown";
1337 uint32 oldflags, newflags;
1339 union samr_UserInfo *info = NULL;
1342 ((argc == 1) && !strequal(argv[0], "yes") &&
1343 !strequal(argv[0], "no"))) {
1344 /* TRANSATORS: The yes|no here are program keywords. Please do
1346 d_fprintf(stderr, _("Usage: %s <username> [yes|no]\n"),
1348 return NT_STATUS_INVALID_PARAMETER;
1351 newval = strequal(argv[0], "yes");
1353 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1357 if (!NT_STATUS_IS_OK(result)) {
1361 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1362 oldflags = info->info21.acct_flags;
1363 newflags = info->info21.acct_flags;
1365 HANDLEFLG("disabled", DISABLED);
1366 HANDLEFLG("pwnotreq", PWNOTREQ);
1367 HANDLEFLG("autolock", AUTOLOCK);
1368 HANDLEFLG("pwnoexp", PWNOEXP);
1371 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1376 ZERO_STRUCT(info->info21);
1378 info->info21.acct_flags = newflags;
1379 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1381 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1386 if (NT_STATUS_IS_OK(result)) {
1387 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1388 ctx->thiscmd, oldval, argv[0]);
1396 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1397 TALLOC_CTX *mem_ctx,
1398 struct rpc_sh_ctx *ctx,
1399 struct rpc_pipe_client *pipe_hnd,
1400 int argc, const char **argv)
1402 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1403 rpc_sh_user_flag_edit_internals);
1406 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1407 TALLOC_CTX *mem_ctx,
1408 struct rpc_sh_ctx *ctx)
1410 static struct rpc_sh_cmd cmds[] = {
1412 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1413 N_("Show/Set a user's full name") },
1415 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1416 N_("Show/Set a user's home directory") },
1418 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1419 N_("Show/Set a user's home drive") },
1421 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1422 N_("Show/Set a user's logon script") },
1424 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1425 N_("Show/Set a user's profile path") },
1427 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1428 N_("Show/Set a user's description") },
1430 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1431 N_("Show/Set whether a user is disabled") },
1433 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1434 N_("Show/Set whether a user locked out") },
1436 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1437 N_("Show/Set whether a user does not need a password") },
1439 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1440 N_("Show/Set whether a user's password does not expire") },
1442 { NULL, NULL, 0, NULL, NULL }
1448 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1449 TALLOC_CTX *mem_ctx,
1450 struct rpc_sh_ctx *ctx)
1452 static struct rpc_sh_cmd cmds[] = {
1454 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1455 N_("List available users") },
1457 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1458 N_("List the domain groups a user is member of") },
1460 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1461 N_("Show info about a user") },
1463 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1464 N_("Show/Modify a user's fields") },
1466 { NULL, NULL, 0, NULL, NULL }
1472 /****************************************************************************/
1475 * Basic usage function for 'net rpc group'.
1476 * @param argc Standard main() style argc.
1477 * @param argv Standard main() style argv. Initial components are already
1481 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1483 return net_group_usage(c, argc, argv);
1487 * Delete group on a remote RPC server.
1489 * All parameters are provided by the run_rpc_command function, except for
1490 * argc, argv which are passed through.
1492 * @param domain_sid The domain sid acquired from the remote server.
1493 * @param cli A cli_state connected to the server.
1494 * @param mem_ctx Talloc context, destroyed on completion of the function.
1495 * @param argc Standard main() style argc.
1496 * @param argv Standard main() style argv. Initial components are already
1499 * @return Normal NTSTATUS return.
1502 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1503 const struct dom_sid *domain_sid,
1504 const char *domain_name,
1505 struct cli_state *cli,
1506 struct rpc_pipe_client *pipe_hnd,
1507 TALLOC_CTX *mem_ctx,
1511 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1512 bool group_is_primary = false;
1513 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1515 struct samr_RidAttrArray *rids = NULL;
1518 /* struct samr_RidWithAttribute *user_gids; */
1520 struct samr_Ids group_rids, name_types;
1521 struct lsa_String lsa_acct_name;
1522 union samr_UserInfo *info = NULL;
1524 if (argc < 1 || c->display_usage) {
1525 rpc_group_usage(c, argc,argv);
1526 return NT_STATUS_OK; /* ok? */
1529 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1531 MAXIMUM_ALLOWED_ACCESS,
1534 if (!NT_STATUS_IS_OK(result)) {
1535 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1539 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1541 MAXIMUM_ALLOWED_ACCESS,
1542 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1545 if (!NT_STATUS_IS_OK(result)) {
1546 d_fprintf(stderr, _("Request open_domain failed\n"));
1550 init_lsa_String(&lsa_acct_name, argv[0]);
1552 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1558 if (!NT_STATUS_IS_OK(result)) {
1559 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1563 switch (name_types.ids[0])
1565 case SID_NAME_DOM_GRP:
1566 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1568 MAXIMUM_ALLOWED_ACCESS,
1571 if (!NT_STATUS_IS_OK(result)) {
1572 d_fprintf(stderr, _("Request open_group failed"));
1576 group_rid = group_rids.ids[0];
1578 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1582 if (!NT_STATUS_IS_OK(result)) {
1584 _("Unable to query group members of %s"),
1589 if (c->opt_verbose) {
1591 _("Domain Group %s (rid: %d) has %d members\n"),
1592 argv[0],group_rid, rids->count);
1595 /* Check if group is anyone's primary group */
1596 for (i = 0; i < rids->count; i++)
1598 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1600 MAXIMUM_ALLOWED_ACCESS,
1604 if (!NT_STATUS_IS_OK(result)) {
1606 _("Unable to open group member %d\n"),
1611 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1616 if (!NT_STATUS_IS_OK(result)) {
1618 _("Unable to lookup userinfo for group "
1624 if (info->info21.primary_gid == group_rid) {
1625 if (c->opt_verbose) {
1626 d_printf(_("Group is primary group "
1628 info->info21.account_name.string);
1630 group_is_primary = true;
1633 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1636 if (group_is_primary) {
1637 d_fprintf(stderr, _("Unable to delete group because "
1638 "some of it's members have it as primary "
1640 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1644 /* remove all group members */
1645 for (i = 0; i < rids->count; i++)
1648 d_printf(_("Remove group member %d..."),
1650 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1654 if (NT_STATUS_IS_OK(result)) {
1656 d_printf(_("ok\n"));
1659 d_printf("%s\n", _("failed"));
1664 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1668 /* removing a local group is easier... */
1669 case SID_NAME_ALIAS:
1670 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1672 MAXIMUM_ALLOWED_ACCESS,
1676 if (!NT_STATUS_IS_OK(result)) {
1677 d_fprintf(stderr, _("Request open_alias failed\n"));
1681 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1685 d_fprintf(stderr, _("%s is of type %s. This command is only "
1686 "for deleting local or global groups\n"),
1687 argv[0],sid_type_lookup(name_types.ids[0]));
1688 result = NT_STATUS_UNSUCCESSFUL;
1692 if (NT_STATUS_IS_OK(result)) {
1694 d_printf(_("Deleted %s '%s'\n"),
1695 sid_type_lookup(name_types.ids[0]), argv[0]);
1697 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1698 get_friendly_nt_error_msg(result));
1706 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1708 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1709 rpc_group_delete_internals, argc,argv);
1712 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1714 NET_API_STATUS status;
1715 struct GROUP_INFO_1 info1;
1716 uint32_t parm_error = 0;
1718 if (argc != 1 || c->display_usage) {
1719 rpc_group_usage(c, argc, argv);
1725 info1.grpi1_name = argv[0];
1726 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1727 info1.grpi1_comment = c->opt_comment;
1730 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1734 _("Failed to add group '%s' with error: %s.\n"),
1735 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1739 d_printf(_("Added group '%s'.\n"), argv[0]);
1745 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1747 NET_API_STATUS status;
1748 struct LOCALGROUP_INFO_1 info1;
1749 uint32_t parm_error = 0;
1751 if (argc != 1 || c->display_usage) {
1752 rpc_group_usage(c, argc, argv);
1758 info1.lgrpi1_name = argv[0];
1759 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1760 info1.lgrpi1_comment = c->opt_comment;
1763 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1767 _("Failed to add alias '%s' with error: %s.\n"),
1768 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1772 d_printf(_("Added alias '%s'.\n"), argv[0]);
1778 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1780 if (c->opt_localgroup)
1781 return rpc_alias_add_internals(c, argc, argv);
1783 return rpc_group_add_internals(c, argc, argv);
1786 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1787 TALLOC_CTX *mem_ctx,
1789 struct dom_sid *sid,
1790 enum lsa_SidType *type)
1792 struct dom_sid *sids = NULL;
1793 enum lsa_SidType *types = NULL;
1794 struct rpc_pipe_client *pipe_hnd = NULL;
1795 struct policy_handle lsa_pol;
1796 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1798 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1800 if (!NT_STATUS_IS_OK(result)) {
1804 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1805 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1807 if (!NT_STATUS_IS_OK(result)) {
1811 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1812 &name, NULL, 1, &sids, &types);
1814 if (NT_STATUS_IS_OK(result)) {
1815 sid_copy(sid, &sids[0]);
1819 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1823 TALLOC_FREE(pipe_hnd);
1826 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1828 /* Try as S-1-5-whatever */
1830 struct dom_sid tmp_sid;
1832 if (string_to_sid(&tmp_sid, name)) {
1833 sid_copy(sid, &tmp_sid);
1834 *type = SID_NAME_UNKNOWN;
1835 result = NT_STATUS_OK;
1842 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1843 TALLOC_CTX *mem_ctx,
1844 const struct dom_sid *group_sid,
1847 struct policy_handle connect_pol, domain_pol;
1850 struct policy_handle group_pol;
1852 struct samr_Ids rids, rid_types;
1853 struct lsa_String lsa_acct_name;
1857 sid_copy(&sid, group_sid);
1859 if (!sid_split_rid(&sid, &group_rid)) {
1860 return NT_STATUS_UNSUCCESSFUL;
1863 /* Get sam policy handle */
1864 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1866 MAXIMUM_ALLOWED_ACCESS,
1868 if (!NT_STATUS_IS_OK(result)) {
1872 /* Get domain policy handle */
1873 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1875 MAXIMUM_ALLOWED_ACCESS,
1878 if (!NT_STATUS_IS_OK(result)) {
1882 init_lsa_String(&lsa_acct_name, member);
1884 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1891 if (!NT_STATUS_IS_OK(result)) {
1892 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1897 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1899 MAXIMUM_ALLOWED_ACCESS,
1903 if (!NT_STATUS_IS_OK(result)) {
1907 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1910 0x0005); /* unknown flags */
1913 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1917 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1918 TALLOC_CTX *mem_ctx,
1919 const struct dom_sid *alias_sid,
1922 struct policy_handle connect_pol, domain_pol;
1925 struct policy_handle alias_pol;
1927 struct dom_sid member_sid;
1928 enum lsa_SidType member_type;
1932 sid_copy(&sid, alias_sid);
1934 if (!sid_split_rid(&sid, &alias_rid)) {
1935 return NT_STATUS_UNSUCCESSFUL;
1938 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1939 member, &member_sid, &member_type);
1941 if (!NT_STATUS_IS_OK(result)) {
1942 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1947 /* Get sam policy handle */
1948 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1950 MAXIMUM_ALLOWED_ACCESS,
1952 if (!NT_STATUS_IS_OK(result)) {
1956 /* Get domain policy handle */
1957 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1959 MAXIMUM_ALLOWED_ACCESS,
1962 if (!NT_STATUS_IS_OK(result)) {
1966 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1968 MAXIMUM_ALLOWED_ACCESS,
1972 if (!NT_STATUS_IS_OK(result)) {
1976 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1980 if (!NT_STATUS_IS_OK(result)) {
1985 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1989 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1990 const struct dom_sid *domain_sid,
1991 const char *domain_name,
1992 struct cli_state *cli,
1993 struct rpc_pipe_client *pipe_hnd,
1994 TALLOC_CTX *mem_ctx,
1998 struct dom_sid group_sid;
1999 enum lsa_SidType group_type;
2001 if (argc != 2 || c->display_usage) {
2004 _("net rpc group addmem <group> <member>\n"
2005 " Add a member to a group\n"
2006 " group\tGroup to add member to\n"
2007 " member\tMember to add to group\n"));
2008 return NT_STATUS_UNSUCCESSFUL;
2011 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2012 &group_sid, &group_type))) {
2013 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2015 return NT_STATUS_UNSUCCESSFUL;
2018 if (group_type == SID_NAME_DOM_GRP) {
2019 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2020 &group_sid, argv[1]);
2022 if (!NT_STATUS_IS_OK(result)) {
2023 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2024 argv[1], argv[0], nt_errstr(result));
2029 if (group_type == SID_NAME_ALIAS) {
2030 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2031 &group_sid, argv[1]);
2033 if (!NT_STATUS_IS_OK(result)) {
2034 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
2035 argv[1], argv[0], nt_errstr(result));
2040 d_fprintf(stderr, _("Can only add members to global or local groups "
2041 "which %s is not\n"), argv[0]);
2043 return NT_STATUS_UNSUCCESSFUL;
2046 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2048 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2049 rpc_group_addmem_internals,
2053 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2054 struct rpc_pipe_client *pipe_hnd,
2055 TALLOC_CTX *mem_ctx,
2056 const struct dom_sid *group_sid,
2059 struct policy_handle connect_pol, domain_pol;
2062 struct policy_handle group_pol;
2064 struct samr_Ids rids, rid_types;
2065 struct lsa_String lsa_acct_name;
2069 sid_copy(&sid, group_sid);
2071 if (!sid_split_rid(&sid, &group_rid))
2072 return NT_STATUS_UNSUCCESSFUL;
2074 /* Get sam policy handle */
2075 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2077 MAXIMUM_ALLOWED_ACCESS,
2079 if (!NT_STATUS_IS_OK(result))
2082 /* Get domain policy handle */
2083 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2085 MAXIMUM_ALLOWED_ACCESS,
2088 if (!NT_STATUS_IS_OK(result))
2091 init_lsa_String(&lsa_acct_name, member);
2093 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2099 if (!NT_STATUS_IS_OK(result)) {
2100 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2105 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2107 MAXIMUM_ALLOWED_ACCESS,
2111 if (!NT_STATUS_IS_OK(result))
2114 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2119 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2123 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2124 TALLOC_CTX *mem_ctx,
2125 const struct dom_sid *alias_sid,
2128 struct policy_handle connect_pol, domain_pol;
2131 struct policy_handle alias_pol;
2133 struct dom_sid member_sid;
2134 enum lsa_SidType member_type;
2138 sid_copy(&sid, alias_sid);
2140 if (!sid_split_rid(&sid, &alias_rid))
2141 return NT_STATUS_UNSUCCESSFUL;
2143 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2144 member, &member_sid, &member_type);
2146 if (!NT_STATUS_IS_OK(result)) {
2147 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2152 /* Get sam policy handle */
2153 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2155 MAXIMUM_ALLOWED_ACCESS,
2157 if (!NT_STATUS_IS_OK(result)) {
2161 /* Get domain policy handle */
2162 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2164 MAXIMUM_ALLOWED_ACCESS,
2167 if (!NT_STATUS_IS_OK(result)) {
2171 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2173 MAXIMUM_ALLOWED_ACCESS,
2177 if (!NT_STATUS_IS_OK(result))
2180 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2184 if (!NT_STATUS_IS_OK(result))
2188 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2192 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2193 const struct dom_sid *domain_sid,
2194 const char *domain_name,
2195 struct cli_state *cli,
2196 struct rpc_pipe_client *pipe_hnd,
2197 TALLOC_CTX *mem_ctx,
2201 struct dom_sid group_sid;
2202 enum lsa_SidType group_type;
2204 if (argc != 2 || c->display_usage) {
2207 _("net rpc group delmem <group> <member>\n"
2208 " Delete a member from a group\n"
2209 " group\tGroup to delete member from\n"
2210 " member\tMember to delete from group\n"));
2211 return NT_STATUS_UNSUCCESSFUL;
2214 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2215 &group_sid, &group_type))) {
2216 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2218 return NT_STATUS_UNSUCCESSFUL;
2221 if (group_type == SID_NAME_DOM_GRP) {
2222 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2223 &group_sid, argv[1]);
2225 if (!NT_STATUS_IS_OK(result)) {
2226 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2227 argv[1], argv[0], nt_errstr(result));
2232 if (group_type == SID_NAME_ALIAS) {
2233 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2234 &group_sid, argv[1]);
2236 if (!NT_STATUS_IS_OK(result)) {
2237 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2238 argv[1], argv[0], nt_errstr(result));
2243 d_fprintf(stderr, _("Can only delete members from global or local "
2244 "groups which %s is not\n"), argv[0]);
2246 return NT_STATUS_UNSUCCESSFUL;
2249 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2251 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2252 rpc_group_delmem_internals,
2257 * List groups on a remote RPC server.
2259 * All parameters are provided by the run_rpc_command function, except for
2260 * argc, argv which are passes through.
2262 * @param domain_sid The domain sid acquired from the remote server.
2263 * @param cli A cli_state connected to the server.
2264 * @param mem_ctx Talloc context, destroyed on completion of the function.
2265 * @param argc Standard main() style argc.
2266 * @param argv Standard main() style argv. Initial components are already
2269 * @return Normal NTSTATUS return.
2272 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2273 const struct dom_sid *domain_sid,
2274 const char *domain_name,
2275 struct cli_state *cli,
2276 struct rpc_pipe_client *pipe_hnd,
2277 TALLOC_CTX *mem_ctx,
2281 struct policy_handle connect_pol, domain_pol;
2282 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2283 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2284 struct samr_SamArray *groups = NULL;
2285 bool global = false;
2287 bool builtin = false;
2289 if (c->display_usage) {
2292 _("net rpc group list [global] [local] [builtin]\n"
2293 " List groups on RPC server\n"
2294 " global\tList global groups\n"
2295 " local\tList local groups\n"
2296 " builtin\tList builtin groups\n"
2297 " If none of global, local or builtin is "
2298 "specified, all three options are considered "
2300 return NT_STATUS_OK;
2309 for (i=0; i<argc; i++) {
2310 if (strequal(argv[i], "global"))
2313 if (strequal(argv[i], "local"))
2316 if (strequal(argv[i], "builtin"))
2320 /* Get sam policy handle */
2322 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2324 MAXIMUM_ALLOWED_ACCESS,
2326 if (!NT_STATUS_IS_OK(result)) {
2330 /* Get domain policy handle */
2332 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2334 MAXIMUM_ALLOWED_ACCESS,
2335 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2337 if (!NT_STATUS_IS_OK(result)) {
2341 /* Query domain groups */
2342 if (c->opt_long_list_entries)
2343 d_printf(_("\nGroup name Comment"
2344 "\n-----------------------------\n"));
2346 uint32_t max_size, total_size, returned_size;
2347 union samr_DispInfo info;
2351 get_query_dispinfo_params(
2352 loop_count, &max_entries, &max_size);
2354 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2363 num_entries = info.info3.count;
2364 start_idx += info.info3.count;
2366 if (!NT_STATUS_IS_OK(result) &&
2367 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2370 for (i = 0; i < num_entries; i++) {
2372 const char *group = NULL;
2373 const char *desc = NULL;
2375 group = info.info3.entries[i].account_name.string;
2376 desc = info.info3.entries[i].description.string;
2378 if (c->opt_long_list_entries)
2379 printf("%-21.21s %-50.50s\n",
2382 printf("%s\n", group);
2384 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2385 /* query domain aliases */
2390 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2396 if (!NT_STATUS_IS_OK(result) &&
2397 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2400 for (i = 0; i < num_entries; i++) {
2402 const char *description = NULL;
2404 if (c->opt_long_list_entries) {
2406 struct policy_handle alias_pol;
2407 union samr_AliasInfo *info = NULL;
2409 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2412 groups->entries[i].idx,
2414 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2418 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2420 description = info->description.string;
2424 if (description != NULL) {
2425 printf("%-21.21s %-50.50s\n",
2426 groups->entries[i].name.string,
2429 printf("%s\n", groups->entries[i].name.string);
2432 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2433 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2434 /* Get builtin policy handle */
2436 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2438 MAXIMUM_ALLOWED_ACCESS,
2439 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2441 if (!NT_STATUS_IS_OK(result)) {
2444 /* query builtin aliases */
2447 if (!builtin) break;
2449 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2455 if (!NT_STATUS_IS_OK(result) &&
2456 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2459 for (i = 0; i < num_entries; i++) {
2461 const char *description = NULL;
2463 if (c->opt_long_list_entries) {
2465 struct policy_handle alias_pol;
2466 union samr_AliasInfo *info = NULL;
2468 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2471 groups->entries[i].idx,
2473 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2477 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2479 description = info->description.string;
2483 if (description != NULL) {
2484 printf("%-21.21s %-50.50s\n",
2485 groups->entries[i].name.string,
2488 printf("%s\n", groups->entries[i].name.string);
2491 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2497 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2499 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2500 rpc_group_list_internals,
2504 static NTSTATUS rpc_list_group_members(struct net_context *c,
2505 struct rpc_pipe_client *pipe_hnd,
2506 TALLOC_CTX *mem_ctx,
2507 const char *domain_name,
2508 const struct dom_sid *domain_sid,
2509 struct policy_handle *domain_pol,
2513 struct policy_handle group_pol;
2514 uint32 num_members, *group_rids;
2516 struct samr_RidAttrArray *rids = NULL;
2517 struct lsa_Strings names;
2518 struct samr_Ids types;
2521 sid_to_fstring(sid_str, domain_sid);
2523 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2525 MAXIMUM_ALLOWED_ACCESS,
2529 if (!NT_STATUS_IS_OK(result))
2532 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2536 if (!NT_STATUS_IS_OK(result))
2539 num_members = rids->count;
2540 group_rids = rids->rids;
2542 while (num_members > 0) {
2543 int this_time = 512;
2545 if (num_members < this_time)
2546 this_time = num_members;
2548 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2555 if (!NT_STATUS_IS_OK(result))
2558 /* We only have users as members, but make the output
2559 the same as the output of alias members */
2561 for (i = 0; i < this_time; i++) {
2563 if (c->opt_long_list_entries) {
2564 printf("%s-%d %s\\%s %d\n", sid_str,
2565 group_rids[i], domain_name,
2566 names.names[i].string,
2569 printf("%s\\%s\n", domain_name,
2570 names.names[i].string);
2574 num_members -= this_time;
2578 return NT_STATUS_OK;
2581 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2582 struct rpc_pipe_client *pipe_hnd,
2583 TALLOC_CTX *mem_ctx,
2584 struct policy_handle *domain_pol,
2588 struct rpc_pipe_client *lsa_pipe;
2589 struct policy_handle alias_pol, lsa_pol;
2591 struct dom_sid *alias_sids;
2594 enum lsa_SidType *types;
2596 struct lsa_SidArray sid_array;
2598 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2600 MAXIMUM_ALLOWED_ACCESS,
2604 if (!NT_STATUS_IS_OK(result))
2607 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2611 if (!NT_STATUS_IS_OK(result)) {
2612 d_fprintf(stderr, _("Couldn't list alias members\n"));
2616 num_members = sid_array.num_sids;
2618 if (num_members == 0) {
2619 return NT_STATUS_OK;
2622 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2623 &ndr_table_lsarpc.syntax_id,
2625 if (!NT_STATUS_IS_OK(result)) {
2626 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2627 nt_errstr(result) );
2631 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2632 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2634 if (!NT_STATUS_IS_OK(result)) {
2635 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2636 TALLOC_FREE(lsa_pipe);
2640 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, struct dom_sid, num_members);
2642 d_fprintf(stderr, _("Out of memory\n"));
2643 TALLOC_FREE(lsa_pipe);
2644 return NT_STATUS_NO_MEMORY;
2647 for (i=0; i<num_members; i++) {
2648 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2651 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2652 num_members, alias_sids,
2653 &domains, &names, &types);
2655 if (!NT_STATUS_IS_OK(result) &&
2656 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2657 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2658 TALLOC_FREE(lsa_pipe);
2662 for (i = 0; i < num_members; i++) {
2664 sid_to_fstring(sid_str, &alias_sids[i]);
2666 if (c->opt_long_list_entries) {
2667 printf("%s %s\\%s %d\n", sid_str,
2668 domains[i] ? domains[i] : _("*unknown*"),
2669 names[i] ? names[i] : _("*unknown*"), types[i]);
2672 printf("%s\\%s\n", domains[i], names[i]);
2674 printf("%s\n", sid_str);
2678 TALLOC_FREE(lsa_pipe);
2679 return NT_STATUS_OK;
2682 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2683 const struct dom_sid *domain_sid,
2684 const char *domain_name,
2685 struct cli_state *cli,
2686 struct rpc_pipe_client *pipe_hnd,
2687 TALLOC_CTX *mem_ctx,
2692 struct policy_handle connect_pol, domain_pol;
2693 struct samr_Ids rids, rid_types;
2694 struct lsa_String lsa_acct_name;
2696 /* Get sam policy handle */
2698 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2700 MAXIMUM_ALLOWED_ACCESS,
2703 if (!NT_STATUS_IS_OK(result))
2706 /* Get domain policy handle */
2708 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2710 MAXIMUM_ALLOWED_ACCESS,
2711 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2714 if (!NT_STATUS_IS_OK(result))
2717 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2719 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2726 if (!NT_STATUS_IS_OK(result)) {
2728 /* Ok, did not find it in the global sam, try with builtin */
2730 struct dom_sid sid_Builtin;
2732 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2734 sid_copy(&sid_Builtin, &global_sid_Builtin);
2736 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2738 MAXIMUM_ALLOWED_ACCESS,
2742 if (!NT_STATUS_IS_OK(result)) {
2743 d_fprintf(stderr, _("Couldn't find group %s\n"),
2748 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2755 if (!NT_STATUS_IS_OK(result)) {
2756 d_fprintf(stderr, _("Couldn't find group %s\n"),
2762 if (rids.count != 1) {
2763 d_fprintf(stderr, _("Couldn't find group %s\n"),
2768 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2769 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2770 domain_sid, &domain_pol,
2774 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2775 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2779 return NT_STATUS_NO_SUCH_GROUP;
2782 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2784 if (argc != 1 || c->display_usage) {
2785 return rpc_group_usage(c, argc, argv);
2788 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2789 rpc_group_members_internals,
2793 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2795 NET_API_STATUS status;
2796 struct GROUP_INFO_0 g0;
2800 d_printf(_("Usage:\n"));
2801 d_printf("net rpc group rename group newname\n");
2805 g0.grpi0_name = argv[1];
2807 status = NetGroupSetInfo(c->opt_host,
2814 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
2815 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2823 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2825 if (argc != 2 || c->display_usage) {
2826 return rpc_group_usage(c, argc, argv);
2829 return rpc_group_rename_internals(c, argc, argv);
2833 * 'net rpc group' entrypoint.
2834 * @param argc Standard main() style argc.
2835 * @param argv Standard main() style argv. Initial components are already
2839 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2841 NET_API_STATUS status;
2843 struct functable func[] = {
2848 N_("Create specified group"),
2849 N_("net rpc group add\n"
2850 " Create specified group")
2856 N_("Delete specified group"),
2857 N_("net rpc group delete\n"
2858 " Delete specified group")
2864 N_("Add member to group"),
2865 N_("net rpc group addmem\n"
2866 " Add member to group")
2872 N_("Remove member from group"),
2873 N_("net rpc group delmem\n"
2874 " Remove member from group")
2881 N_("net rpc group list\n"
2888 N_("List group members"),
2889 N_("net rpc group members\n"
2890 " List group members")
2897 N_("net rpc group rename\n"
2900 {NULL, NULL, 0, NULL, NULL}
2903 status = libnetapi_net_init(&c->netapi_ctx);
2907 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2908 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2909 if (c->opt_kerberos) {
2910 libnetapi_set_use_kerberos(c->netapi_ctx);
2914 if (c->display_usage) {
2915 d_printf(_("Usage:\n"));
2916 d_printf(_("net rpc group\n"
2917 " Alias for net rpc group list global "
2918 "local builtin\n"));
2919 net_display_usage_from_functable(func);
2923 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2924 rpc_group_list_internals,
2928 return net_run_function(c, argc, argv, "net rpc group", func);
2931 /****************************************************************************/
2933 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2935 return net_share_usage(c, argc, argv);
2939 * Add a share on a remote RPC server.
2941 * @param argc Standard main() style argc.
2942 * @param argv Standard main() style argv. Initial components are already
2945 * @return A shell status integer (0 for success).
2948 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2950 NET_API_STATUS status;
2953 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2954 uint32 num_users=0, perms=0;
2955 char *password=NULL; /* don't allow a share password */
2956 struct SHARE_INFO_2 i2;
2957 uint32_t parm_error = 0;
2959 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2960 return rpc_share_usage(c, argc, argv);
2963 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2967 path = strchr(sharename, '=');
2974 i2.shi2_netname = sharename;
2975 i2.shi2_type = type;
2976 i2.shi2_remark = c->opt_comment;
2977 i2.shi2_permissions = perms;
2978 i2.shi2_max_uses = c->opt_maxusers;
2979 i2.shi2_current_uses = num_users;
2980 i2.shi2_path = path;
2981 i2.shi2_passwd = password;
2983 status = NetShareAdd(c->opt_host,
2988 printf(_("NetShareAdd failed with: %s\n"),
2989 libnetapi_get_error_string(c->netapi_ctx, status));
2996 * Delete a share on a remote RPC server.
2998 * @param domain_sid The domain sid acquired from the remote server.
2999 * @param argc Standard main() style argc.
3000 * @param argv Standard main() style argv. Initial components are already
3003 * @return A shell status integer (0 for success).
3005 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3007 if (argc < 1 || c->display_usage) {
3008 return rpc_share_usage(c, argc, argv);
3011 return NetShareDel(c->opt_host, argv[0], 0);
3015 * Formatted print of share info
3017 * @param r pointer to SHARE_INFO_1 to format
3020 static void display_share_info_1(struct net_context *c,
3021 struct SHARE_INFO_1 *r)
3023 if (c->opt_long_list_entries) {
3024 d_printf("%-12s %-8.8s %-50s\n",
3026 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
3029 d_printf("%s\n", r->shi1_netname);
3033 static WERROR get_share_info(struct net_context *c,
3034 struct rpc_pipe_client *pipe_hnd,
3035 TALLOC_CTX *mem_ctx,
3039 struct srvsvc_NetShareInfoCtr *info_ctr)
3043 union srvsvc_NetShareInfo info;
3045 /* no specific share requested, enumerate all */
3048 uint32_t preferred_len = 0xffffffff;
3049 uint32_t total_entries = 0;
3050 uint32_t resume_handle = 0;
3052 info_ctr->level = level;
3054 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
3064 /* request just one share */
3065 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
3072 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
3077 ZERO_STRUCTP(info_ctr);
3079 info_ctr->level = level;
3084 struct srvsvc_NetShareCtr1 *ctr1;
3086 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3087 W_ERROR_HAVE_NO_MEMORY(ctr1);
3090 ctr1->array = info.info1;
3092 info_ctr->ctr.ctr1 = ctr1;
3098 struct srvsvc_NetShareCtr2 *ctr2;
3100 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3101 W_ERROR_HAVE_NO_MEMORY(ctr2);
3104 ctr2->array = info.info2;
3106 info_ctr->ctr.ctr2 = ctr2;
3112 struct srvsvc_NetShareCtr502 *ctr502;
3114 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3115 W_ERROR_HAVE_NO_MEMORY(ctr502);
3118 ctr502->array = info.info502;
3120 info_ctr->ctr.ctr502 = ctr502;
3130 * 'net rpc share list' entrypoint.
3131 * @param argc Standard main() style argc.
3132 * @param argv Standard main() style argv. Initial components are already
3135 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3137 NET_API_STATUS status;
3138 struct SHARE_INFO_1 *i1 = NULL;
3139 uint32_t entries_read = 0;
3140 uint32_t total_entries = 0;
3141 uint32_t resume_handle = 0;
3142 uint32_t i, level = 1;
3144 if (c->display_usage) {
3146 "net rpc share list\n"
3149 _("List shares on remote server"));
3153 status = NetShareEnum(c->opt_host,
3155 (uint8_t **)(void *)&i1,
3164 /* Display results */
3166 if (c->opt_long_list_entries) {
3168 "\nEnumerating shared resources (exports) on remote server:\n\n"
3169 "\nShare name Type Description\n"
3170 "---------- ---- -----------\n"));
3172 for (i = 0; i < entries_read; i++)
3173 display_share_info_1(c, &i1[i]);
3178 static bool check_share_availability(struct cli_state *cli, const char *netname)
3182 status = cli_tcon_andx(cli, netname, "A:", "", 0);
3183 if (!NT_STATUS_IS_OK(status)) {
3184 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3188 status = cli_tdis(cli);
3189 if (!NT_STATUS_IS_OK(status)) {
3190 d_printf(_("cli_tdis returned %s\n"), nt_errstr(status));
3197 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3198 const char *netname, uint32 type)
3200 /* only support disk shares */
3201 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3202 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3207 /* skip builtin shares */
3208 /* FIXME: should print$ be added too ? */
3209 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3210 strequal(netname,"global"))
3213 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3214 printf(_("excluding [%s]\n"), netname);
3218 return check_share_availability(cli, netname);
3222 * Migrate shares from a remote RPC server to the local RPC server.
3224 * All parameters are provided by the run_rpc_command function, except for
3225 * argc, argv which are passed through.
3227 * @param domain_sid The domain sid acquired from the remote server.
3228 * @param cli A cli_state connected to the server.
3229 * @param mem_ctx Talloc context, destroyed on completion of the function.
3230 * @param argc Standard main() style argc.
3231 * @param argv Standard main() style argv. Initial components are already
3234 * @return Normal NTSTATUS return.
3237 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3238 const struct dom_sid *domain_sid,
3239 const char *domain_name,
3240 struct cli_state *cli,
3241 struct rpc_pipe_client *pipe_hnd,
3242 TALLOC_CTX *mem_ctx,
3247 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3248 struct srvsvc_NetShareInfoCtr ctr_src;
3250 struct rpc_pipe_client *srvsvc_pipe = NULL;
3251 struct cli_state *cli_dst = NULL;
3252 uint32 level = 502; /* includes secdesc */
3253 uint32_t parm_error = 0;
3255 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3257 if (!W_ERROR_IS_OK(result))
3260 /* connect destination PI_SRVSVC */
3261 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3262 &ndr_table_srvsvc.syntax_id);
3263 if (!NT_STATUS_IS_OK(nt_status))
3267 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3269 union srvsvc_NetShareInfo info;
3270 struct srvsvc_NetShareInfo502 info502 =
3271 ctr_src.ctr.ctr502->array[i];
3273 /* reset error-code */
3274 nt_status = NT_STATUS_UNSUCCESSFUL;
3276 if (!check_share_sanity(c, cli, info502.name, info502.type))
3279 /* finally add the share on the dst server */
3281 printf(_("migrating: [%s], path: %s, comment: %s, without "
3283 info502.name, info502.path, info502.comment);
3285 info.info502 = &info502;
3287 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3288 srvsvc_pipe->desthost,
3294 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3295 printf(_(" [%s] does already exist\n"),
3300 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3301 printf(_("cannot add share: %s\n"), win_errstr(result));
3307 nt_status = NT_STATUS_OK;
3311 cli_shutdown(cli_dst);
3319 * Migrate shares from a RPC server to another.
3321 * @param argc Standard main() style argc.
3322 * @param argv Standard main() style argv. Initial components are already
3325 * @return A shell status integer (0 for success).
3327 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3330 if (c->display_usage) {
3332 "net rpc share migrate shares\n"
3335 _("Migrate shares to local server"));
3340 printf(_("no server to migrate\n"));
3344 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3345 rpc_share_migrate_shares_internals,
3352 * @param f file_info
3353 * @param mask current search mask
3354 * @param state arg-pointer
3357 static NTSTATUS copy_fn(const char *mnt, struct file_info *f,
3358 const char *mask, void *state)
3360 static NTSTATUS nt_status;
3361 static struct copy_clistate *local_state;
3362 static fstring filename, new_mask;
3365 struct net_context *c;
3367 local_state = (struct copy_clistate *)state;
3368 nt_status = NT_STATUS_UNSUCCESSFUL;
3372 if (strequal(f->name, ".") || strequal(f->name, ".."))
3373 return NT_STATUS_OK;
3375 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3378 if (f->mode & aDIR) {
3380 DEBUG(3,("got dir: %s\n", f->name));
3382 fstrcpy(dir, local_state->cwd);
3384 fstrcat(dir, f->name);
3386 switch (net_mode_share)
3388 case NET_MODE_SHARE_MIGRATE:
3389 /* create that directory */
3390 nt_status = net_copy_file(c, local_state->mem_ctx,
3391 local_state->cli_share_src,
3392 local_state->cli_share_dst,
3394 c->opt_acls? true : false,
3395 c->opt_attrs? true : false,
3396 c->opt_timestamps? true:false,
3400 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3401 return NT_STATUS_INTERNAL_ERROR;
3404 if (!NT_STATUS_IS_OK(nt_status)) {
3405 printf(_("could not handle dir %s: %s\n"),
3406 dir, nt_errstr(nt_status));
3410 /* search below that directory */
3411 fstrcpy(new_mask, dir);
3412 fstrcat(new_mask, "\\*");
3414 old_dir = local_state->cwd;
3415 local_state->cwd = dir;
3416 nt_status = sync_files(local_state, new_mask);
3417 if (!NT_STATUS_IS_OK(nt_status)) {
3418 printf(_("could not handle files\n"));
3420 local_state->cwd = old_dir;
3427 fstrcpy(filename, local_state->cwd);
3428 fstrcat(filename, "\\");
3429 fstrcat(filename, f->name);
3431 DEBUG(3,("got file: %s\n", filename));
3433 switch (net_mode_share)
3435 case NET_MODE_SHARE_MIGRATE:
3436 nt_status = net_copy_file(c, local_state->mem_ctx,
3437 local_state->cli_share_src,
3438 local_state->cli_share_dst,
3440 c->opt_acls? true : false,
3441 c->opt_attrs? true : false,
3442 c->opt_timestamps? true: false,
3446 d_fprintf(stderr, _("Unsupported file mode %d\n"),
3448 return NT_STATUS_INTERNAL_ERROR;
3451 if (!NT_STATUS_IS_OK(nt_status))
3452 printf(_("could not handle file %s: %s\n"),
3453 filename, nt_errstr(nt_status));
3458 * sync files, can be called recursivly to list files
3459 * and then call copy_fn for each file
3461 * @param cp_clistate pointer to the copy_clistate we work with
3462 * @param mask the current search mask
3464 * @return Boolean result
3466 static NTSTATUS sync_files(struct copy_clistate *cp_clistate, const char *mask)
3468 struct cli_state *targetcli;
3469 char *targetpath = NULL;
3472 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3474 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3475 mask, &targetcli, &targetpath ) ) {
3476 d_fprintf(stderr, _("cli_resolve_path %s failed with error: "
3478 mask, cli_errstr(cp_clistate->cli_share_src));
3479 return cli_nt_error(cp_clistate->cli_share_src);
3482 status = cli_list(targetcli, targetpath, cp_clistate->attribute,
3483 copy_fn, cp_clistate);
3484 if (!NT_STATUS_IS_OK(status)) {
3485 d_fprintf(stderr, _("listing %s failed with error: %s\n"),
3486 mask, nt_errstr(status));
3494 * Set the top level directory permissions before we do any further copies.
3495 * Should set up ACL inheritance.
3498 bool copy_top_level_perms(struct net_context *c,
3499 struct copy_clistate *cp_clistate,
3500 const char *sharename)
3502 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3504 switch (net_mode_share) {
3505 case NET_MODE_SHARE_MIGRATE:
3506 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3507 nt_status = net_copy_fileattr(c,
3508 cp_clistate->mem_ctx,
3509 cp_clistate->cli_share_src,
3510 cp_clistate->cli_share_dst,
3512 c->opt_acls? true : false,
3513 c->opt_attrs? true : false,
3514 c->opt_timestamps? true: false,
3518 d_fprintf(stderr, _("Unsupported mode %d\n"), net_mode_share);
3522 if (!NT_STATUS_IS_OK(nt_status)) {
3523 printf(_("Could handle directory attributes for top level "
3524 "directory of share %s. Error %s\n"),
3525 sharename, nt_errstr(nt_status));
3533 * Sync all files inside a remote share to another share (over smb).
3535 * All parameters are provided by the run_rpc_command function, except for
3536 * argc, argv which are passed through.
3538 * @param domain_sid The domain sid acquired from the remote server.
3539 * @param cli A cli_state connected to the server.
3540 * @param mem_ctx Talloc context, destroyed on completion of the function.
3541 * @param argc Standard main() style argc.
3542 * @param argv Standard main() style argv. Initial components are already
3545 * @return Normal NTSTATUS return.
3548 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3549 const struct dom_sid *domain_sid,
3550 const char *domain_name,
3551 struct cli_state *cli,
3552 struct rpc_pipe_client *pipe_hnd,
3553 TALLOC_CTX *mem_ctx,
3558 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3559 struct srvsvc_NetShareInfoCtr ctr_src;
3562 struct copy_clistate cp_clistate;
3563 bool got_src_share = false;
3564 bool got_dst_share = false;
3565 const char *mask = "\\*";
3568 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3570 nt_status = NT_STATUS_NO_MEMORY;
3574 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3577 if (!W_ERROR_IS_OK(result))
3580 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3582 struct srvsvc_NetShareInfo502 info502 =
3583 ctr_src.ctr.ctr502->array[i];
3585 if (!check_share_sanity(c, cli, info502.name, info502.type))
3588 /* one might not want to mirror whole discs :) */
3589 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3590 d_printf(_("skipping [%s]: builtin/hidden share\n"),
3595 switch (net_mode_share)
3597 case NET_MODE_SHARE_MIGRATE:
3601 d_fprintf(stderr, _("Unsupported mode %d\n"),
3605 printf(_(" [%s] files and directories %s ACLs, %s DOS "
3608 c->opt_acls ? _("including") : _("without"),
3609 c->opt_attrs ? _("including") : _("without"),
3610 c->opt_timestamps ? _("(preserving timestamps)") : "");
3612 cp_clistate.mem_ctx = mem_ctx;
3613 cp_clistate.cli_share_src = NULL;
3614 cp_clistate.cli_share_dst = NULL;
3615 cp_clistate.cwd = NULL;
3616 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3619 /* open share source */
3620 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3621 &cli->dest_ss, cli->desthost,
3622 info502.name, "A:");
3623 if (!NT_STATUS_IS_OK(nt_status))
3626 got_src_share = true;
3628 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3629 /* open share destination */
3630 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3631 NULL, dst, info502.name, "A:");
3632 if (!NT_STATUS_IS_OK(nt_status))
3635 got_dst_share = true;
3638 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3639 d_fprintf(stderr, _("Could not handle the top level "
3640 "directory permissions for the "
3641 "share: %s\n"), info502.name);
3642 nt_status = NT_STATUS_UNSUCCESSFUL;
3646 nt_status = sync_files(&cp_clistate, mask);
3647 if (!NT_STATUS_IS_OK(nt_status)) {
3648 d_fprintf(stderr, _("could not handle files for share: "
3649 "%s\n"), info502.name);
3654 nt_status = NT_STATUS_OK;
3659 cli_shutdown(cp_clistate.cli_share_src);
3662 cli_shutdown(cp_clistate.cli_share_dst);
3669 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3671 if (c->display_usage) {
3673 "net share migrate files\n"
3676 _("Migrate files to local server"));
3681 d_printf(_("no server to migrate\n"));
3685 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3686 rpc_share_migrate_files_internals,
3691 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3693 * All parameters are provided by the run_rpc_command function, except for
3694 * argc, argv which are passed through.
3696 * @param domain_sid The domain sid acquired from the remote server.
3697 * @param cli A cli_state connected to the server.
3698 * @param mem_ctx Talloc context, destroyed on completion of the function.
3699 * @param argc Standard main() style argc.
3700 * @param argv Standard main() style argv. Initial components are already
3703 * @return Normal NTSTATUS return.
3706 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3707 const struct dom_sid *domain_sid,
3708 const char *domain_name,
3709 struct cli_state *cli,
3710 struct rpc_pipe_client *pipe_hnd,
3711 TALLOC_CTX *mem_ctx,
3716 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3717 struct srvsvc_NetShareInfoCtr ctr_src;
3718 union srvsvc_NetShareInfo info;
3720 struct rpc_pipe_client *srvsvc_pipe = NULL;
3721 struct cli_state *cli_dst = NULL;
3722 uint32 level = 502; /* includes secdesc */
3723 uint32_t parm_error = 0;
3725 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3728 if (!W_ERROR_IS_OK(result))
3731 /* connect destination PI_SRVSVC */
3732 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3733 &ndr_table_srvsvc.syntax_id);
3734 if (!NT_STATUS_IS_OK(nt_status))
3738 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3740 struct srvsvc_NetShareInfo502 info502 =
3741 ctr_src.ctr.ctr502->array[i];
3743 /* reset error-code */
3744 nt_status = NT_STATUS_UNSUCCESSFUL;
3746 if (!check_share_sanity(c, cli, info502.name, info502.type))
3749 printf(_("migrating: [%s], path: %s, comment: %s, including "
3751 info502.name, info502.path, info502.comment);
3754 display_sec_desc(info502.sd_buf.sd);
3756 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3757 info.info502 = &info502;
3759 /* finally modify the share on the dst server */
3760 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3761 srvsvc_pipe->desthost,
3767 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3768 printf(_("cannot set share-acl: %s\n"),
3769 win_errstr(result));
3775 nt_status = NT_STATUS_OK;
3779 cli_shutdown(cli_dst);
3787 * Migrate share-acls from a RPC server to another.
3789 * @param argc Standard main() style argc.
3790 * @param argv Standard main() style argv. Initial components are already
3793 * @return A shell status integer (0 for success).
3795 static int rpc_share_migrate_security(struct net_context *c, int argc,
3798 if (c->display_usage) {
3800 "net rpc share migrate security\n"
3803 _("Migrate share-acls to local server"));
3808 d_printf(_("no server to migrate\n"));
3812 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3813 rpc_share_migrate_security_internals,
3818 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3819 * from one server to another.
3821 * @param argc Standard main() style argc.
3822 * @param argv Standard main() style argv. Initial components are already
3825 * @return A shell status integer (0 for success).
3828 static int rpc_share_migrate_all(struct net_context *c, int argc,
3833 if (c->display_usage) {
3835 "net rpc share migrate all\n"
3838 _("Migrates shares including all share settings"));
3843 d_printf(_("no server to migrate\n"));
3847 /* order is important. we don't want to be locked out by the share-acl
3848 * before copying files - gd */
3850 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3851 rpc_share_migrate_shares_internals, argc, argv);
3855 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3856 rpc_share_migrate_files_internals, argc, argv);
3860 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3861 rpc_share_migrate_security_internals, argc,
3867 * 'net rpc share migrate' entrypoint.
3868 * @param argc Standard main() style argc.
3869 * @param argv Standard main() style argv. Initial components are already
3872 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3875 struct functable func[] = {
3878 rpc_share_migrate_all,
3880 N_("Migrate shares from remote to local server"),
3881 N_("net rpc share migrate all\n"
3882 " Migrate shares from remote to local server")
3886 rpc_share_migrate_files,
3888 N_("Migrate files from remote to local server"),
3889 N_("net rpc share migrate files\n"
3890 " Migrate files from remote to local server")
3894 rpc_share_migrate_security,
3896 N_("Migrate share-ACLs from remote to local server"),
3897 N_("net rpc share migrate security\n"
3898 " Migrate share-ACLs from remote to local server")
3902 rpc_share_migrate_shares,
3904 N_("Migrate shares from remote to local server"),
3905 N_("net rpc share migrate shares\n"
3906 " Migrate shares from remote to local server")
3908 {NULL, NULL, 0, NULL, NULL}
3911 net_mode_share = NET_MODE_SHARE_MIGRATE;
3913 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3919 struct dom_sid *members;
3922 static int num_server_aliases;
3923 static struct full_alias *server_aliases;
3926 * Add an alias to the static list.
3928 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3930 if (server_aliases == NULL)
3931 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3933 server_aliases[num_server_aliases] = *alias;
3934 num_server_aliases += 1;
3938 * For a specific domain on the server, fetch all the aliases
3939 * and their members. Add all of them to the server_aliases.
3942 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3943 TALLOC_CTX *mem_ctx,
3944 struct policy_handle *connect_pol,
3945 const struct dom_sid *domain_sid)
3947 uint32 start_idx, max_entries, num_entries, i;
3948 struct samr_SamArray *groups = NULL;
3950 struct policy_handle domain_pol;
3952 /* Get domain policy handle */
3954 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3956 MAXIMUM_ALLOWED_ACCESS,
3957 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3959 if (!NT_STATUS_IS_OK(result))
3966 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3972 for (i = 0; i < num_entries; i++) {
3974 struct policy_handle alias_pol;
3975 struct full_alias alias;
3976 struct lsa_SidArray sid_array;
3979 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3981 MAXIMUM_ALLOWED_ACCESS,
3982 groups->entries[i].idx,
3984 if (!NT_STATUS_IS_OK(result))
3987 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3990 if (!NT_STATUS_IS_OK(result))
3993 alias.num_members = sid_array.num_sids;
3995 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3996 if (!NT_STATUS_IS_OK(result))
3999 alias.members = NULL;
4001 if (alias.num_members > 0) {
4002 alias.members = SMB_MALLOC_ARRAY(struct dom_sid, alias.num_members);
4004 for (j = 0; j < alias.num_members; j++)
4005 sid_copy(&alias.members[j],
4006 sid_array.sids[j].sid);
4009 sid_compose(&alias.sid, domain_sid,
4010 groups->entries[i].idx);
4012 push_alias(mem_ctx, &alias);
4014 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4016 result = NT_STATUS_OK;
4019 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4025 * Dump server_aliases as names for debugging purposes.
4028 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4029 const struct dom_sid *domain_sid,
4030 const char *domain_name,
4031 struct cli_state *cli,
4032 struct rpc_pipe_client *pipe_hnd,
4033 TALLOC_CTX *mem_ctx,
4039 struct policy_handle lsa_pol;
4041 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4042 SEC_FLAG_MAXIMUM_ALLOWED,
4044 if (!NT_STATUS_IS_OK(result))
4047 for (i=0; i<num_server_aliases; i++) {
4050 enum lsa_SidType *types;
4053 struct full_alias *alias = &server_aliases[i];
4055 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4057 &domains, &names, &types);
4058 if (!NT_STATUS_IS_OK(result))
4061 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4063 if (alias->num_members == 0) {
4068 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4071 &domains, &names, &types);
4073 if (!NT_STATUS_IS_OK(result) &&
4074 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4077 for (j=0; j<alias->num_members; j++)
4078 DEBUG(1, ("%s\\%s (%d); ",
4079 domains[j] ? domains[j] : "*unknown*",
4080 names[j] ? names[j] : "*unknown*",types[j]));
4084 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4086 return NT_STATUS_OK;
4090 * Fetch a list of all server aliases and their members into
4094 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4095 const struct dom_sid *domain_sid,
4096 const char *domain_name,
4097 struct cli_state *cli,
4098 struct rpc_pipe_client *pipe_hnd,
4099 TALLOC_CTX *mem_ctx,
4104 struct policy_handle connect_pol;
4106 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4108 MAXIMUM_ALLOWED_ACCESS,
4111 if (!NT_STATUS_IS_OK(result))
4114 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4115 &global_sid_Builtin);
4117 if (!NT_STATUS_IS_OK(result))
4120 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4123 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4128 static void init_user_token(struct security_token *token, struct dom_sid *user_sid)
4130 token->num_sids = 4;
4132 if (!(token->sids = SMB_MALLOC_ARRAY(struct dom_sid, 4))) {
4133 d_fprintf(stderr, "malloc %s\n",_("failed"));
4134 token->num_sids = 0;
4138 token->sids[0] = *user_sid;
4139 sid_copy(&token->sids[1], &global_sid_World);
4140 sid_copy(&token->sids[2], &global_sid_Network);
4141 sid_copy(&token->sids[3], &global_sid_Authenticated_Users);
4144 static void free_user_token(struct security_token *token)
4146 SAFE_FREE(token->sids);
4149 static void add_sid_to_token(struct security_token *token, struct dom_sid *sid)
4151 if (security_token_has_sid(token, sid))
4154 token->sids = SMB_REALLOC_ARRAY(token->sids, struct dom_sid, token->num_sids+1);
4159 sid_copy(&token->sids[token->num_sids], sid);
4161 token->num_sids += 1;
4166 struct security_token token;
4169 static void dump_user_token(struct user_token *token)
4173 d_printf("%s\n", token->name);
4175 for (i=0; i<token->token.num_sids; i++) {
4176 d_printf(" %s\n", sid_string_tos(&token->token.sids[i]));
4180 static bool is_alias_member(struct dom_sid *sid, struct full_alias *alias)
4184 for (i=0; i<alias->num_members; i++) {
4185 if (dom_sid_compare(sid, &alias->members[i]) == 0)
4192 static void collect_sid_memberships(struct security_token *token, struct dom_sid sid)
4196 for (i=0; i<num_server_aliases; i++) {
4197 if (is_alias_member(&sid, &server_aliases[i]))
4198 add_sid_to_token(token, &server_aliases[i].sid);
4203 * We got a user token with all the SIDs we can know about without asking the
4204 * server directly. These are the user and domain group sids. All of these can
4205 * be members of aliases. So scan the list of aliases for each of the SIDs and
4206 * add them to the token.
4209 static void collect_alias_memberships(struct security_token *token)
4211 int num_global_sids = token->num_sids;
4214 for (i=0; i<num_global_sids; i++) {
4215 collect_sid_memberships(token, token->sids[i]);
4219 static bool get_user_sids(const char *domain, const char *user, struct security_token *token)
4221 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4222 enum wbcSidType type;
4224 struct wbcDomainSid wsid;
4225 char *sid_str = NULL;
4226 struct dom_sid user_sid;
4227 uint32_t num_groups;
4228 gid_t *groups = NULL;
4231 fstr_sprintf(full_name, "%s%c%s",
4232 domain, *lp_winbind_separator(), user);
4234 /* First let's find out the user sid */
4236 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4238 if (!WBC_ERROR_IS_OK(wbc_status)) {
4239 DEBUG(1, ("winbind could not find %s: %s\n",
4240 full_name, wbcErrorString(wbc_status)));
4244 wbc_status = wbcSidToString(&wsid, &sid_str);
4245 if (!WBC_ERROR_IS_OK(wbc_status)) {
4249 if (type != WBC_SID_NAME_USER) {
4250 wbcFreeMemory(sid_str);
4251 DEBUG(1, ("%s is not a user\n", full_name));
4255 if (!string_to_sid(&user_sid, sid_str)) {
4256 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4260 wbcFreeMemory(sid_str);
4263 init_user_token(token, &user_sid);
4265 /* And now the groups winbind knows about */
4267 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4268 if (!WBC_ERROR_IS_OK(wbc_status)) {
4269 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4270 full_name, wbcErrorString(wbc_status)));
4274 for (i = 0; i < num_groups; i++) {
4275 gid_t gid = groups[i];
4278 wbc_status = wbcGidToSid(gid, &wsid);
4279 if (!WBC_ERROR_IS_OK(wbc_status)) {
4280 DEBUG(1, ("winbind could not find SID of gid %u: %s\n",
4281 (unsigned int)gid, wbcErrorString(wbc_status)));
4282 wbcFreeMemory(groups);
4286 wbc_status = wbcSidToString(&wsid, &sid_str);
4287 if (!WBC_ERROR_IS_OK(wbc_status)) {
4288 wbcFreeMemory(groups);
4292 DEBUG(3, (" %s\n", sid_str));
4294 string_to_sid(&sid, sid_str);
4295 wbcFreeMemory(sid_str);
4298 add_sid_to_token(token, &sid);
4300 wbcFreeMemory(groups);
4306 * Get a list of all user tokens we want to look at
4309 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4310 struct user_token **user_tokens)
4312 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4313 uint32_t i, num_users;
4315 struct user_token *result;
4316 TALLOC_CTX *frame = NULL;
4318 if (lp_winbind_use_default_domain() &&
4319 (c->opt_target_workgroup == NULL)) {
4320 d_fprintf(stderr, _("winbind use default domain = yes set, "
4321 "please specify a workgroup\n"));
4325 /* Send request to winbind daemon */
4327 wbc_status = wbcListUsers(NULL, &num_users, &users);
4328 if (!WBC_ERROR_IS_OK(wbc_status)) {
4329 DEBUG(1, (_("winbind could not list users: %s\n"),
4330 wbcErrorString(wbc_status)));
4334 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4336 if (result == NULL) {
4337 DEBUG(1, ("Could not malloc sid array\n"));
4338 wbcFreeMemory(users);
4342 frame = talloc_stackframe();
4343 for (i=0; i < num_users; i++) {
4344 fstring domain, user;
4347 fstrcpy(result[i].name, users[i]);
4349 p = strchr(users[i], *lp_winbind_separator());
4351 DEBUG(3, ("%s\n", users[i]));
4354 fstrcpy(domain, c->opt_target_workgroup);
4355 fstrcpy(user, users[i]);
4358 fstrcpy(domain, users[i]);
4363 get_user_sids(domain, user, &(result[i].token));
4367 wbcFreeMemory(users);
4369 *num_tokens = num_users;
4370 *user_tokens = result;
4375 static bool get_user_tokens_from_file(FILE *f,
4377 struct user_token **tokens)
4379 struct user_token *token = NULL;
4384 if (fgets(line, sizeof(line)-1, f) == NULL) {
4388 if ((strlen(line) > 0) && (line[strlen(line)-1] == '\n')) {
4389 line[strlen(line)-1] = '\0';
4392 if (line[0] == ' ') {
4396 if(!string_to_sid(&sid, &line[1])) {
4397 DEBUG(1,("get_user_tokens_from_file: Could "
4398 "not convert sid %s \n",&line[1]));
4402 if (token == NULL) {
4403 DEBUG(0, ("File does not begin with username"));
4407 add_sid_to_token(&token->token, &sid);
4411 /* And a new user... */
4414 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4415 if (*tokens == NULL) {
4416 DEBUG(0, ("Could not realloc tokens\n"));
4420 token = &((*tokens)[*num_tokens-1]);
4422 fstrcpy(token->name, line);
4423 token->token.num_sids = 0;
4424 token->token.sids = NULL;
4433 * Show the list of all users that have access to a share
4436 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4437 TALLOC_CTX *mem_ctx,
4438 const char *netname,
4440 struct user_token *tokens)
4443 struct security_descriptor *share_sd = NULL;
4444 struct security_descriptor *root_sd = NULL;
4445 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4447 union srvsvc_NetShareInfo info;
4452 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4459 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4460 DEBUG(1, ("Coult not query secdesc for share %s\n",
4465 share_sd = info.info502->sd_buf.sd;
4466 if (share_sd == NULL) {
4467 DEBUG(1, ("Got no secdesc for share %s\n",
4473 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4477 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, "\\", 0, READ_CONTROL_ACCESS, 0,
4478 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum))) {
4479 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4482 for (i=0; i<num_tokens; i++) {
4485 if (share_sd != NULL) {
4486 status = se_access_check(share_sd, &tokens[i].token,
4489 if (!NT_STATUS_IS_OK(status)) {
4490 DEBUG(1, ("Could not check share_sd for "
4497 if (root_sd == NULL) {
4498 d_printf(" %s\n", tokens[i].name);
4502 status = se_access_check(root_sd, &tokens[i].token,
4504 if (!NT_STATUS_IS_OK(status)) {
4505 DEBUG(1, ("Could not check root_sd for user %s\n",
4509 d_printf(" %s\n", tokens[i].name);
4512 if (fnum != (uint16_t)-1)
4513 cli_close(cli, fnum);
4525 static void collect_share(const char *name, uint32 m,
4526 const char *comment, void *state)
4528 struct share_list *share_list = (struct share_list *)state;
4530 if (m != STYPE_DISKTREE)
4533 share_list->num_shares += 1;
4534 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4535 if (!share_list->shares) {
4536 share_list->num_shares = 0;
4539 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4543 * List shares on a remote RPC server, including the security descriptors.
4545 * All parameters are provided by the run_rpc_command function, except for
4546 * argc, argv which are passed through.
4548 * @param domain_sid The domain sid acquired from the remote server.
4549 * @param cli A cli_state connected to the server.
4550 * @param mem_ctx Talloc context, destroyed on completion of the function.
4551 * @param argc Standard main() style argc.
4552 * @param argv Standard main() style argv. Initial components are already
4555 * @return Normal NTSTATUS return.
4558 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4559 const struct dom_sid *domain_sid,
4560 const char *domain_name,
4561 struct cli_state *cli,
4562 struct rpc_pipe_client *pipe_hnd,
4563 TALLOC_CTX *mem_ctx,
4572 struct user_token *tokens = NULL;
4575 struct share_list share_list;
4580 f = fopen(argv[0], "r");
4584 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4585 return NT_STATUS_UNSUCCESSFUL;
4588 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4594 DEBUG(0, ("Could not read users from file\n"));
4595 return NT_STATUS_UNSUCCESSFUL;
4598 for (i=0; i<num_tokens; i++)
4599 collect_alias_memberships(&tokens[i].token);
4601 share_list.num_shares = 0;
4602 share_list.shares = NULL;
4604 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4607 DEBUG(0, ("Error returning browse list: %s\n",
4612 for (i = 0; i < share_list.num_shares; i++) {
4613 char *netname = share_list.shares[i];
4615 if (netname[strlen(netname)-1] == '$')
4618 d_printf("%s\n", netname);
4620 show_userlist(pipe_hnd, mem_ctx, netname,
4621 num_tokens, tokens);
4624 for (i=0; i<num_tokens; i++) {
4625 free_user_token(&tokens[i].token);
4628 SAFE_FREE(share_list.shares);
4630 return NT_STATUS_OK;
4633 static int rpc_share_allowedusers(struct net_context *c, int argc,
4638 if (c->display_usage) {
4640 "net rpc share allowedusers\n"
4643 _("List allowed users"));
4647 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4648 rpc_aliaslist_internals,
4653 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4659 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4660 rpc_share_allowedusers_internals,
4664 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4667 struct user_token *tokens = NULL;
4671 net_usersidlist_usage(c, argc, argv);
4675 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4676 DEBUG(0, ("Could not get the user/sid list\n"));
4680 for (i=0; i<num_tokens; i++) {
4681 dump_user_token(&tokens[i]);
4682 free_user_token(&tokens[i].token);
4689 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4691 d_printf(_("net usersidlist\n"
4692 "\tprints out a list of all users the running winbind knows\n"
4693 "\tabout, together with all their SIDs. This is used as\n"
4694 "\tinput to the 'net rpc share allowedusers' command.\n\n"));
4696 net_common_flags_usage(c, argc, argv);
4701 * 'net rpc share' entrypoint.
4702 * @param argc Standard main() style argc.
4703 * @param argv Standard main() style argv. Initial components are already
4707 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4709 NET_API_STATUS status;
4711 struct functable func[] = {
4717 N_("net rpc share add\n"
4725 N_("net rpc share delete\n"
4730 rpc_share_allowedusers,
4732 N_("Modify allowed users"),
4733 N_("net rpc share allowedusers\n"
4734 " Modify allowed users")
4740 N_("Migrate share to local server"),
4741 N_("net rpc share migrate\n"
4742 " Migrate share to local server")
4749 N_("net rpc share list\n"
4752 {NULL, NULL, 0, NULL, NULL}
4755 status = libnetapi_net_init(&c->netapi_ctx);
4759 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4760 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4761 if (c->opt_kerberos) {
4762 libnetapi_set_use_kerberos(c->netapi_ctx);
4766 if (c->display_usage) {
4771 " Alias for net rpc share list\n"));
4772 net_display_usage_from_functable(func);
4776 return rpc_share_list(c, argc, argv);
4779 return net_run_function(c, argc, argv, "net rpc share", func);
4782 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4783 TALLOC_CTX *mem_ctx,
4784 struct rpc_sh_ctx *ctx,
4785 struct rpc_pipe_client *pipe_hnd,
4786 int argc, const char **argv)
4789 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4792 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4793 TALLOC_CTX *mem_ctx,
4794 struct rpc_sh_ctx *ctx,
4795 struct rpc_pipe_client *pipe_hnd,
4796 int argc, const char **argv)
4798 NET_API_STATUS status;
4799 uint32_t parm_err = 0;
4800 struct SHARE_INFO_2 i2;
4802 if ((argc < 2) || (argc > 3)) {
4803 d_fprintf(stderr, _("Usage: %s <share> <path> [comment]\n"),
4805 return NT_STATUS_INVALID_PARAMETER;
4808 i2.shi2_netname = argv[0];
4809 i2.shi2_type = STYPE_DISKTREE;
4810 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4811 i2.shi2_permissions = 0;
4812 i2.shi2_max_uses = 0;
4813 i2.shi2_current_uses = 0;
4814 i2.shi2_path = argv[1];
4815 i2.shi2_passwd = NULL;
4817 status = NetShareAdd(pipe_hnd->desthost,
4822 return werror_to_ntstatus(W_ERROR(status));
4825 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4826 TALLOC_CTX *mem_ctx,
4827 struct rpc_sh_ctx *ctx,
4828 struct rpc_pipe_client *pipe_hnd,
4829 int argc, const char **argv)
4832 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
4833 return NT_STATUS_INVALID_PARAMETER;
4836 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4839 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4840 TALLOC_CTX *mem_ctx,
4841 struct rpc_sh_ctx *ctx,
4842 struct rpc_pipe_client *pipe_hnd,
4843 int argc, const char **argv)
4845 union srvsvc_NetShareInfo info;
4850 d_fprintf(stderr, "%s %s <share>\n", _("Usage:"), ctx->whoami);
4851 return NT_STATUS_INVALID_PARAMETER;
4854 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4860 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4864 d_printf(_("Name: %s\n"), info.info2->name);
4865 d_printf(_("Comment: %s\n"), info.info2->comment);
4866 d_printf(_("Path: %s\n"), info.info2->path);
4867 d_printf(_("Password: %s\n"), info.info2->password);
4870 return werror_to_ntstatus(result);
4873 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4874 struct rpc_sh_ctx *ctx)
4876 static struct rpc_sh_cmd cmds[] = {
4878 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4879 N_("List available shares") },
4881 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4882 N_("Add a share") },
4884 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4885 N_("Delete a share") },
4887 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4888 N_("Get information about a share") },
4890 { NULL, NULL, 0, NULL, NULL }
4896 /****************************************************************************/
4898 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4900 return net_file_usage(c, argc, argv);
4904 * Close a file on a remote RPC server.
4906 * @param argc Standard main() style argc.
4907 * @param argv Standard main() style argv. Initial components are already
4910 * @return A shell status integer (0 for success).
4912 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4914 if (argc < 1 || c->display_usage) {
4915 return rpc_file_usage(c, argc, argv);
4918 return NetFileClose(c->opt_host, atoi(argv[0]));
4922 * Formatted print of open file info
4924 * @param r struct FILE_INFO_3 contents
4927 static void display_file_info_3(struct FILE_INFO_3 *r)
4929 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4930 r->fi3_id, r->fi3_username, r->fi3_permissions,
4931 r->fi3_num_locks, r->fi3_pathname);
4935 * List files for a user on a remote RPC server.
4937 * @param argc Standard main() style argc.
4938 * @param argv Standard main() style argv. Initial components are already
4941 * @return A shell status integer (0 for success)..
4944 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4946 NET_API_STATUS status;
4947 uint32 preferred_len = 0xffffffff, i;
4948 const char *username=NULL;
4949 uint32_t total_entries = 0;
4950 uint32_t entries_read = 0;
4951 uint32_t resume_handle = 0;
4952 struct FILE_INFO_3 *i3 = NULL;
4954 if (c->display_usage) {
4955 return rpc_file_usage(c, argc, argv);
4958 /* if argc > 0, must be user command */
4960 username = smb_xstrdup(argv[0]);
4963 status = NetFileEnum(c->opt_host,
4967 (uint8_t **)(void *)&i3,
4977 /* Display results */
4980 "\nEnumerating open files on remote server:\n\n"
4981 "\nFileId Opened by Perms Locks Path"
4982 "\n------ --------- ----- ----- ---- \n"));
4983 for (i = 0; i < entries_read; i++) {
4984 display_file_info_3(&i3[i]);
4991 * 'net rpc file' entrypoint.
4992 * @param argc Standard main() style argc.
4993 * @param argv Standard main() style argv. Initial components are already
4997 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4999 NET_API_STATUS status;
5001 struct functable func[] = {
5006 N_("Close opened file"),
5007 N_("net rpc file close\n"
5008 " Close opened file")
5014 N_("List files opened by user"),
5015 N_("net rpc file user\n"
5016 " List files opened by user")
5023 N_("Display information about opened file"),
5024 N_("net rpc file info\n"
5025 " Display information about opened file")
5028 {NULL, NULL, 0, NULL, NULL}
5031 status = libnetapi_net_init(&c->netapi_ctx);
5035 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
5036 libnetapi_set_password(c->netapi_ctx, c->opt_password);
5037 if (c->opt_kerberos) {
5038 libnetapi_set_use_kerberos(c->netapi_ctx);
5042 if (c->display_usage) {
5043 d_printf(_("Usage:\n"));
5044 d_printf(_("net rpc file\n"
5045 " List opened files\n"));
5046 net_display_usage_from_functable(func);
5050 return rpc_file_user(c, argc, argv);
5053 return net_run_function(c, argc, argv, "net rpc file", func);
5057 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5059 * All parameters are provided by the run_rpc_command function, except for
5060 * argc, argv which are passed through.
5062 * @param c A net_context structure.
5063 * @param domain_sid The domain sid acquired from the remote server.
5064 * @param cli A cli_state connected to the server.
5065 * @param mem_ctx Talloc context, destroyed on completion of the function.
5066 * @param argc Standard main() style argc.
5067 * @param argv Standard main() style argv. Initial components are already
5070 * @return Normal NTSTATUS return.
5073 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5074 const struct dom_sid *domain_sid,
5075 const char *domain_name,
5076 struct cli_state *cli,
5077 struct rpc_pipe_client *pipe_hnd,
5078 TALLOC_CTX *mem_ctx,
5082 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5084 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5086 status = dcerpc_initshutdown_Abort(b, mem_ctx, NULL, &result);
5087 if (!NT_STATUS_IS_OK(status)) {
5090 if (W_ERROR_IS_OK(result)) {
5091 d_printf(_("\nShutdown successfully aborted\n"));
5092 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5094 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5096 return werror_to_ntstatus(result);
5100 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5102 * All parameters are provided by the run_rpc_command function, except for
5103 * argc, argv which are passed through.
5105 * @param c A net_context structure.
5106 * @param domain_sid The domain sid acquired from the remote server.
5107 * @param cli A cli_state connected to the server.
5108 * @param mem_ctx Talloc context, destroyed on completion of the function.
5109 * @param argc Standard main() style argc.
5110 * @param argv Standard main() style argv. Initial components are already
5113 * @return Normal NTSTATUS return.
5116 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5117 const struct dom_sid *domain_sid,
5118 const char *domain_name,
5119 struct cli_state *cli,
5120 struct rpc_pipe_client *pipe_hnd,
5121 TALLOC_CTX *mem_ctx,
5125 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5127 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5129 if (NT_STATUS_IS_OK(result)) {
5130 d_printf(_("\nShutdown successfully aborted\n"));
5131 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5133 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5139 * ABORT the shutdown of a remote RPC server.
5141 * @param argc Standard main() style argc.
5142 * @param argv Standard main() style argv. Initial components are already
5145 * @return A shell status integer (0 for success).
5148 static int rpc_shutdown_abort(struct net_context *c, int argc,
5153 if (c->display_usage) {
5155 "net rpc abortshutdown\n"
5158 _("Abort a scheduled shutdown"));
5162 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5163 rpc_shutdown_abort_internals, argc, argv);
5168 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5170 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5171 rpc_reg_shutdown_abort_internals,
5176 * Shut down a remote RPC Server via initshutdown pipe.
5178 * All parameters are provided by the run_rpc_command function, except for
5179 * argc, argv which are passed through.
5181 * @param c A net_context structure.
5182 * @param domain_sid The domain sid acquired from the remote server.
5183 * @param cli A cli_state connected to the server.
5184 * @param mem_ctx Talloc context, destroyed on completion of the function.
5185 * @param argc Standard main() style argc.
5186 * @param argv Standard main() style argv. Initial components are already
5189 * @return Normal NTSTATUS return.
5192 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5193 const struct dom_sid *domain_sid,
5194 const char *domain_name,
5195 struct cli_state *cli,
5196 struct rpc_pipe_client *pipe_hnd,
5197 TALLOC_CTX *mem_ctx,
5201 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5203 const char *msg = N_("This machine will be shutdown shortly");
5204 uint32 timeout = 20;
5205 struct lsa_StringLarge msg_string;
5206 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
5208 if (c->opt_comment) {
5209 msg = c->opt_comment;
5211 if (c->opt_timeout) {
5212 timeout = c->opt_timeout;
5215 msg_string.string = msg;
5217 /* create an entry */
5218 status = dcerpc_initshutdown_Init(b, mem_ctx, NULL,
5219 &msg_string, timeout, c->opt_force, c->opt_reboot,
5221 if (!NT_STATUS_IS_OK(status)) {
5224 if (W_ERROR_IS_OK(result)) {
5225 d_printf(_("\nShutdown of remote machine succeeded\n"));
5226 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5228 DEBUG(1,("Shutdown of remote machine failed!\n"));
5230 return werror_to_ntstatus(result);
5234 * Shut down a remote RPC Server via winreg pipe.
5236 * All parameters are provided by the run_rpc_command function, except for
5237 * argc, argv which are passed through.
5239 * @param c A net_context structure.
5240 * @param domain_sid The domain sid acquired from the remote server.
5241 * @param cli A cli_state connected to the server.
5242 * @param mem_ctx Talloc context, destroyed on completion of the function.
5243 * @param argc Standard main() style argc.
5244 * @param argv Standard main() style argv. Initial components are already
5247 * @return Normal NTSTATUS return.
5250 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5251 const struct dom_sid *domain_sid,
5252 const char *domain_name,
5253 struct cli_state *cli,
5254 struct rpc_pipe_client *pipe_hnd,
5255 TALLOC_CTX *mem_ctx,
5259 const char *msg = N_("This machine will be shutdown shortly");
5260 uint32 timeout = 20;
5261 struct lsa_StringLarge msg_string;
5265 if (c->opt_comment) {
5266 msg = c->opt_comment;
5268 msg_string.string = msg;
5270 if (c->opt_timeout) {
5271 timeout = c->opt_timeout;
5274 /* create an entry */
5275 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5276 &msg_string, timeout, c->opt_force, c->opt_reboot,
5279 if (NT_STATUS_IS_OK(result)) {
5280 d_printf(_("\nShutdown of remote machine succeeded\n"));
5282 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5283 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5284 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5286 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5293 * Shut down a remote RPC server.
5295 * @param argc Standard main() style argc.
5296 * @param argv Standard main() style argv. Initial components are already
5299 * @return A shell status integer (0 for success).
5302 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5306 if (c->display_usage) {
5308 "net rpc shutdown\n"
5311 _("Shut down a remote RPC server"));
5315 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5316 rpc_init_shutdown_internals, argc, argv);
5319 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5320 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5321 rpc_reg_shutdown_internals, argc, argv);
5327 /***************************************************************************
5328 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5329 ***************************************************************************/
5332 * Add interdomain trust account to the RPC server.
5333 * All parameters (except for argc and argv) are passed by run_rpc_command
5336 * @param c A net_context structure.
5337 * @param domain_sid The domain sid acquired from the server.
5338 * @param cli A cli_state connected to the server.
5339 * @param mem_ctx Talloc context, destroyed on completion of the function.
5340 * @param argc Standard main() style argc.
5341 * @param argv Standard main() style argv. Initial components are already
5344 * @return normal NTSTATUS return code.
5347 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5348 const struct dom_sid *domain_sid,
5349 const char *domain_name,
5350 struct cli_state *cli,
5351 struct rpc_pipe_client *pipe_hnd,
5352 TALLOC_CTX *mem_ctx,
5356 struct policy_handle connect_pol, domain_pol, user_pol;
5357 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5359 struct lsa_String lsa_acct_name;
5361 uint32 acct_flags=0;
5363 uint32_t access_granted = 0;
5364 union samr_UserInfo info;
5365 unsigned int orig_timeout;
5370 _(" net rpc trustdom add <domain_name> "
5371 "<trust password>\n"));
5372 return NT_STATUS_INVALID_PARAMETER;
5376 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5379 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5380 return NT_STATUS_NO_MEMORY;
5383 strupper_m(acct_name);
5385 init_lsa_String(&lsa_acct_name, acct_name);
5387 /* Get samr policy handle */
5388 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5390 MAXIMUM_ALLOWED_ACCESS,
5392 if (!NT_STATUS_IS_OK(result)) {
5396 /* Get domain policy handle */
5397 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5399 MAXIMUM_ALLOWED_ACCESS,
5400 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5402 if (!NT_STATUS_IS_OK(result)) {
5406 /* This call can take a long time - allow the server to time out.
5407 * 35 seconds should do it. */
5409 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5411 /* Create trusting domain's account */
5412 acb_info = ACB_NORMAL;
5413 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5414 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5415 SAMR_USER_ACCESS_SET_PASSWORD |
5416 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5417 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5419 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5428 /* And restore our original timeout. */
5429 rpccli_set_timeout(pipe_hnd, orig_timeout);
5431 if (!NT_STATUS_IS_OK(result)) {
5432 d_printf(_("net rpc trustdom add: create user %s failed %s\n"),
5433 acct_name, nt_errstr(result));
5438 struct samr_CryptPassword crypt_pwd;
5440 ZERO_STRUCT(info.info23);
5442 init_samr_CryptPassword(argv[1],
5443 &cli->user_session_key,
5446 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5447 SAMR_FIELD_NT_PASSWORD_PRESENT;
5448 info.info23.info.acct_flags = ACB_DOMTRUST;
5449 info.info23.password = crypt_pwd;
5451 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5456 if (!NT_STATUS_IS_OK(result)) {
5457 DEBUG(0,("Could not set trust account password: %s\n",
5458 nt_errstr(result)));
5464 SAFE_FREE(acct_name);
5469 * Create interdomain trust account for a remote domain.
5471 * @param argc Standard argc.
5472 * @param argv Standard argv without initial components.
5474 * @return Integer status (0 means success).
5477 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5479 if (argc > 0 && !c->display_usage) {
5480 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5481 rpc_trustdom_add_internals, argc, argv);
5485 _("net rpc trustdom add <domain_name> <trust "
5493 * Remove interdomain trust account from the RPC server.
5494 * All parameters (except for argc and argv) are passed by run_rpc_command
5497 * @param c A net_context structure.
5498 * @param domain_sid The domain sid acquired from the server.
5499 * @param cli A cli_state connected to the server.
5500 * @param mem_ctx Talloc context, destroyed on completion of the function.
5501 * @param argc Standard main() style argc.
5502 * @param argv Standard main() style argv. Initial components are already
5505 * @return normal NTSTATUS return code.
5508 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5509 const struct dom_sid *domain_sid,
5510 const char *domain_name,
5511 struct cli_state *cli,
5512 struct rpc_pipe_client *pipe_hnd,
5513 TALLOC_CTX *mem_ctx,
5517 struct policy_handle connect_pol, domain_pol, user_pol;
5518 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5520 struct dom_sid trust_acct_sid;
5521 struct samr_Ids user_rids, name_types;
5522 struct lsa_String lsa_acct_name;
5527 _(" net rpc trustdom del <domain_name>\n"));
5528 return NT_STATUS_INVALID_PARAMETER;
5532 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5534 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5536 if (acct_name == NULL)
5537 return NT_STATUS_NO_MEMORY;
5539 strupper_m(acct_name);
5541 /* Get samr policy handle */
5542 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5544 MAXIMUM_ALLOWED_ACCESS,
5546 if (!NT_STATUS_IS_OK(result)) {
5550 /* Get domain policy handle */
5551 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5553 MAXIMUM_ALLOWED_ACCESS,
5554 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5556 if (!NT_STATUS_IS_OK(result)) {
5560 init_lsa_String(&lsa_acct_name, acct_name);
5562 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5569 if (!NT_STATUS_IS_OK(result)) {
5570 d_printf(_("net rpc trustdom del: LookupNames on user %s "
5572 acct_name, nt_errstr(result) );
5576 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5578 MAXIMUM_ALLOWED_ACCESS,
5582 if (!NT_STATUS_IS_OK(result)) {
5583 d_printf(_("net rpc trustdom del: OpenUser on user %s failed "
5585 acct_name, nt_errstr(result) );
5589 /* append the rid to the domain sid */
5590 if (!sid_compose(&trust_acct_sid, domain_sid, user_rids.ids[0])) {
5594 /* remove the sid */
5596 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5599 if (!NT_STATUS_IS_OK(result)) {
5600 d_printf(_("net rpc trustdom del: RemoveMemberFromForeignDomain"
5601 " on user %s failed %s\n"),
5602 acct_name, nt_errstr(result) );
5608 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5611 if (!NT_STATUS_IS_OK(result)) {
5612 d_printf(_("net rpc trustdom del: DeleteUser on user %s failed "
5614 acct_name, nt_errstr(result) );
5618 if (!NT_STATUS_IS_OK(result)) {
5619 d_printf(_("Could not set trust account password: %s\n"),
5629 * Delete interdomain trust account for a remote domain.
5631 * @param argc Standard argc.
5632 * @param argv Standard argv without initial components.
5634 * @return Integer status (0 means success).
5637 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5639 if (argc > 0 && !c->display_usage) {
5640 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5641 rpc_trustdom_del_internals, argc, argv);
5645 _("net rpc trustdom del <domain>\n"));
5650 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5651 struct cli_state *cli,
5652 TALLOC_CTX *mem_ctx,
5653 const char *domain_name)
5655 char *dc_name = NULL;
5656 const char *buffer = NULL;
5657 struct rpc_pipe_client *netr;
5660 /* Use NetServerEnum2 */
5662 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5664 return NT_STATUS_OK;
5667 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5668 for domain %s\n", domain_name));
5670 /* Try netr_GetDcName */
5672 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5674 if (!NT_STATUS_IS_OK(status)) {
5678 status = rpccli_netr_GetDcName(netr, mem_ctx,
5685 if (NT_STATUS_IS_OK(status)) {
5689 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5690 for domain %s\n", domain_name));
5696 * Establish trust relationship to a trusting domain.
5697 * Interdomain account must already be created on remote PDC.
5699 * @param c A net_context structure.
5700 * @param argc Standard argc.
5701 * @param argv Standard argv without initial components.
5703 * @return Integer status (0 means success).
5706 static int rpc_trustdom_establish(struct net_context *c, int argc,
5709 struct cli_state *cli = NULL;
5710 struct sockaddr_storage server_ss;
5711 struct rpc_pipe_client *pipe_hnd = NULL;
5712 struct policy_handle connect_hnd;
5713 TALLOC_CTX *mem_ctx;
5715 struct dom_sid *domain_sid;
5720 union lsa_PolicyInformation *info = NULL;
5723 * Connect to \\server\ipc$ as 'our domain' account with password
5726 if (argc != 1 || c->display_usage) {
5729 _("net rpc trustdom establish <domain_name>\n"));
5733 domain_name = smb_xstrdup(argv[0]);
5734 strupper_m(domain_name);
5736 /* account name used at first is our domain's name with '$' */
5737 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
5740 strupper_m(acct_name);
5743 * opt_workgroup will be used by connection functions further,
5744 * hence it should be set to remote domain name instead of ours
5746 if (c->opt_workgroup) {
5747 c->opt_workgroup = smb_xstrdup(domain_name);
5750 c->opt_user_name = acct_name;
5752 /* find the domain controller */
5753 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5754 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5758 /* connect to ipc$ as username/password */
5759 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5760 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5762 /* Is it trusting domain account for sure ? */
5763 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5764 nt_errstr(nt_status)));
5768 /* store who we connected to */
5770 saf_store( domain_name, pdc_name );
5773 * Connect to \\server\ipc$ again (this time anonymously)
5776 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5779 if (NT_STATUS_IS_ERR(nt_status)) {
5780 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5781 domain_name, nt_errstr(nt_status)));
5785 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5786 "domain %s", domain_name))) {
5787 DEBUG(0, ("talloc_init() failed\n"));
5792 /* Make sure we're talking to a proper server */
5794 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5795 if (!NT_STATUS_IS_OK(nt_status)) {
5797 talloc_destroy(mem_ctx);
5802 * Call LsaOpenPolicy and LsaQueryInfo
5805 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5807 if (!NT_STATUS_IS_OK(nt_status)) {
5808 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5810 talloc_destroy(mem_ctx);
5814 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
5816 if (NT_STATUS_IS_ERR(nt_status)) {
5817 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5818 nt_errstr(nt_status)));
5820 talloc_destroy(mem_ctx);
5824 /* Querying info level 5 */
5826 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5828 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5830 if (NT_STATUS_IS_ERR(nt_status)) {
5831 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5832 nt_errstr(nt_status)));
5834 talloc_destroy(mem_ctx);
5838 domain_sid = info->account_domain.sid;
5840 /* There should be actually query info level 3 (following nt serv behaviour),
5841 but I still don't know if it's _really_ necessary */
5844 * Store the password in secrets db
5847 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5848 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5850 talloc_destroy(mem_ctx);
5855 * Close the pipes and clean up
5858 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5859 if (NT_STATUS_IS_ERR(nt_status)) {
5860 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5861 nt_errstr(nt_status)));
5863 talloc_destroy(mem_ctx);
5869 talloc_destroy(mem_ctx);
5871 d_printf(_("Trust to domain %s established\n"), domain_name);
5876 * Revoke trust relationship to the remote domain.
5878 * @param c A net_context structure.
5879 * @param argc Standard argc.
5880 * @param argv Standard argv without initial components.
5882 * @return Integer status (0 means success).
5885 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5891 if (argc < 1 || c->display_usage) {
5894 _("net rpc trustdom revoke <domain_name>\n"
5895 " Revoke trust relationship\n"
5896 " domain_name\tName of domain to revoke trust\n"));
5900 /* generate upper cased domain name */
5901 domain_name = smb_xstrdup(argv[0]);
5902 strupper_m(domain_name);
5904 /* delete password of the trust */
5905 if (!pdb_del_trusteddom_pw(domain_name)) {
5906 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5913 SAFE_FREE(domain_name);
5917 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5918 const struct dom_sid *domain_sid,
5919 const char *domain_name,
5920 struct cli_state *cli,
5921 struct rpc_pipe_client *pipe_hnd,
5922 TALLOC_CTX *mem_ctx,
5927 if (!sid_to_fstring(str_sid, domain_sid)) {
5928 return NT_STATUS_UNSUCCESSFUL;
5930 d_printf("%s\n", str_sid);
5931 return NT_STATUS_OK;
5934 static void print_trusted_domain(struct dom_sid *dom_sid, const char *trusted_dom_name)
5938 /* convert sid into ascii string */
5939 sid_to_fstring(ascii_sid, dom_sid);
5941 d_printf("%-20s%s\n", trusted_dom_name, ascii_sid);
5944 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5945 TALLOC_CTX *mem_ctx,
5946 struct policy_handle *pol,
5947 struct dom_sid dom_sid,
5948 const char *trusted_dom_name)
5951 union lsa_TrustedDomainInfo *info = NULL;
5952 char *cleartextpwd = NULL;
5953 uint8_t session_key[16];
5954 DATA_BLOB session_key_blob;
5955 DATA_BLOB data = data_blob_null;
5957 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5960 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5962 if (NT_STATUS_IS_ERR(nt_status)) {
5963 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5964 nt_errstr(nt_status)));
5968 data = data_blob(info->password.password->data,
5969 info->password.password->length);
5971 if (!rpccli_get_pwd_hash(pipe_hnd, session_key)) {
5972 DEBUG(0, ("Could not retrieve password hash\n"));
5976 session_key_blob = data_blob_const(session_key, sizeof(session_key));
5977 cleartextpwd = sess_decrypt_string(mem_ctx, &data, &session_key_blob);
5979 if (cleartextpwd == NULL) {
5980 DEBUG(0,("retrieved NULL password\n"));
5981 nt_status = NT_STATUS_UNSUCCESSFUL;
5985 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5986 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5987 nt_status = NT_STATUS_UNSUCCESSFUL;
5991 #ifdef DEBUG_PASSWORD
5992 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5993 "password: [%s]\n", trusted_dom_name,
5994 sid_string_dbg(&dom_sid), cleartextpwd));
5998 SAFE_FREE(cleartextpwd);
5999 data_blob_free(&data);
6004 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6007 /* common variables */
6008 TALLOC_CTX* mem_ctx;
6009 struct cli_state *cli = NULL;
6010 struct rpc_pipe_client *pipe_hnd = NULL;
6012 const char *domain_name = NULL;
6013 struct dom_sid *queried_dom_sid;
6014 struct policy_handle connect_hnd;
6015 union lsa_PolicyInformation *info = NULL;
6017 /* trusted domains listing variables */
6018 unsigned int enum_ctx = 0;
6020 struct lsa_DomainList dom_list;
6023 if (c->display_usage) {
6025 "net rpc trustdom vampire\n"
6028 _("Vampire trust relationship from remote server"));
6033 * Listing trusted domains (stored in secrets.tdb, if local)
6036 mem_ctx = talloc_init("trust relationships vampire");
6039 * set domain and pdc name to local samba server (default)
6040 * or to remote one given in command line
6043 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6044 domain_name = c->opt_workgroup;
6045 c->opt_target_workgroup = c->opt_workgroup;
6047 fstrcpy(pdc_name, global_myname());
6048 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6049 c->opt_target_workgroup = domain_name;
6052 /* open \PIPE\lsarpc and open policy handle */
6053 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6054 if (!NT_STATUS_IS_OK(nt_status)) {
6055 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6056 nt_errstr(nt_status)));
6057 talloc_destroy(mem_ctx);
6061 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6063 if (!NT_STATUS_IS_OK(nt_status)) {
6064 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6065 nt_errstr(nt_status) ));
6067 talloc_destroy(mem_ctx);
6071 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6073 if (NT_STATUS_IS_ERR(nt_status)) {
6074 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6075 nt_errstr(nt_status)));
6077 talloc_destroy(mem_ctx);
6081 /* query info level 5 to obtain sid of a domain being queried */
6082 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6084 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6087 if (NT_STATUS_IS_ERR(nt_status)) {
6088 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6089 nt_errstr(nt_status)));
6091 talloc_destroy(mem_ctx);
6095 queried_dom_sid = info->account_domain.sid;
6098 * Keep calling LsaEnumTrustdom over opened pipe until
6099 * the end of enumeration is reached
6102 d_printf(_("Vampire trusted domains:\n\n"));
6105 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6110 if (NT_STATUS_IS_ERR(nt_status)) {
6111 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6112 nt_errstr(nt_status)));
6114 talloc_destroy(mem_ctx);
6118 for (i = 0; i < dom_list.count; i++) {
6120 print_trusted_domain(dom_list.domains[i].sid,
6121 dom_list.domains[i].name.string);
6123 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6124 *dom_list.domains[i].sid,
6125 dom_list.domains[i].name.string);
6126 if (!NT_STATUS_IS_OK(nt_status)) {
6128 talloc_destroy(mem_ctx);
6134 * in case of no trusted domains say something rather
6135 * than just display blank line
6137 if (!dom_list.count) d_printf(_("none\n"));
6139 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6141 /* close this connection before doing next one */
6142 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6143 if (NT_STATUS_IS_ERR(nt_status)) {
6144 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6145 nt_errstr(nt_status)));
6147 talloc_destroy(mem_ctx);
6151 /* close lsarpc pipe and connection to IPC$ */
6154 talloc_destroy(mem_ctx);
6158 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6160 /* common variables */
6161 TALLOC_CTX* mem_ctx;
6162 struct cli_state *cli = NULL, *remote_cli = NULL;
6163 struct rpc_pipe_client *pipe_hnd = NULL;
6165 const char *domain_name = NULL;
6166 struct dom_sid *queried_dom_sid;
6167 int ascii_dom_name_len;
6168 struct policy_handle connect_hnd;
6169 union lsa_PolicyInformation *info = NULL;
6171 /* trusted domains listing variables */
6172 unsigned int num_domains, enum_ctx = 0;
6174 struct lsa_DomainList dom_list;
6178 /* trusting domains listing variables */
6179 struct policy_handle domain_hnd;
6180 struct samr_SamArray *trusts = NULL;
6182 if (c->display_usage) {
6184 "net rpc trustdom list\n"
6187 _("List incoming and outgoing trust relationships"));
6192 * Listing trusted domains (stored in secrets.tdb, if local)
6195 mem_ctx = talloc_init("trust relationships listing");
6198 * set domain and pdc name to local samba server (default)
6199 * or to remote one given in command line
6202 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6203 domain_name = c->opt_workgroup;
6204 c->opt_target_workgroup = c->opt_workgroup;
6206 fstrcpy(pdc_name, global_myname());
6207 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6208 c->opt_target_workgroup = domain_name;
6211 /* open \PIPE\lsarpc and open policy handle */
6212 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6213 if (!NT_STATUS_IS_OK(nt_status)) {
6214 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6215 nt_errstr(nt_status)));
6216 talloc_destroy(mem_ctx);
6220 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6222 if (!NT_STATUS_IS_OK(nt_status)) {
6223 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6224 nt_errstr(nt_status) ));
6226 talloc_destroy(mem_ctx);
6230 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6232 if (NT_STATUS_IS_ERR(nt_status)) {
6233 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6234 nt_errstr(nt_status)));
6236 talloc_destroy(mem_ctx);
6240 /* query info level 5 to obtain sid of a domain being queried */
6241 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6243 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6246 if (NT_STATUS_IS_ERR(nt_status)) {
6247 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6248 nt_errstr(nt_status)));
6250 talloc_destroy(mem_ctx);
6254 queried_dom_sid = info->account_domain.sid;
6257 * Keep calling LsaEnumTrustdom over opened pipe until
6258 * the end of enumeration is reached
6261 d_printf(_("Trusted domains list:\n\n"));
6263 found_domain = false;
6266 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6271 if (NT_STATUS_IS_ERR(nt_status)) {
6272 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6273 nt_errstr(nt_status)));
6275 talloc_destroy(mem_ctx);
6279 for (i = 0; i < dom_list.count; i++) {
6280 print_trusted_domain(dom_list.domains[i].sid,
6281 dom_list.domains[i].name.string);
6282 found_domain = true;
6286 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6289 * in case of no trusted domains say something rather
6290 * than just display blank line
6292 if (!found_domain) {
6293 d_printf(_("none\n"));
6296 /* close this connection before doing next one */
6297 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6298 if (NT_STATUS_IS_ERR(nt_status)) {
6299 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6300 nt_errstr(nt_status)));
6302 talloc_destroy(mem_ctx);
6306 TALLOC_FREE(pipe_hnd);
6309 * Listing trusting domains (stored in passdb backend, if local)
6312 d_printf(_("\nTrusting domains list:\n\n"));
6315 * Open \PIPE\samr and get needed policy handles
6317 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6319 if (!NT_STATUS_IS_OK(nt_status)) {
6320 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6322 talloc_destroy(mem_ctx);
6327 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6329 SAMR_ACCESS_LOOKUP_DOMAIN,
6331 if (!NT_STATUS_IS_OK(nt_status)) {
6332 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6333 nt_errstr(nt_status)));
6335 talloc_destroy(mem_ctx);
6339 /* SamrOpenDomain - we have to open domain policy handle in order to be
6340 able to enumerate accounts*/
6341 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6343 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6346 if (!NT_STATUS_IS_OK(nt_status)) {
6347 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6348 nt_errstr(nt_status)));
6350 talloc_destroy(mem_ctx);
6355 * perform actual enumeration
6358 found_domain = false;
6360 enum_ctx = 0; /* reset enumeration context from last enumeration */
6363 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6370 if (NT_STATUS_IS_ERR(nt_status)) {
6371 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6372 nt_errstr(nt_status)));
6374 talloc_destroy(mem_ctx);
6378 for (i = 0; i < num_domains; i++) {
6380 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6382 found_domain = true;
6385 * get each single domain's sid (do we _really_ need this ?):
6386 * 1) connect to domain's pdc
6387 * 2) query the pdc for domain's sid
6390 /* get rid of '$' tail */
6391 ascii_dom_name_len = strlen(str);
6392 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6393 str[ascii_dom_name_len - 1] = '\0';
6395 /* set opt_* variables to remote domain */
6397 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6398 c->opt_target_workgroup = c->opt_workgroup;
6400 d_printf("%-20s", str);
6402 /* connect to remote domain controller */
6403 nt_status = net_make_ipc_connection(c,
6404 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6406 if (NT_STATUS_IS_OK(nt_status)) {
6407 /* query for domain's sid */
6408 if (run_rpc_command(
6410 &ndr_table_lsarpc.syntax_id, 0,
6411 rpc_query_domain_sid, argc,
6413 d_printf(_("strange - couldn't get domain's sid\n"));
6415 cli_shutdown(remote_cli);
6418 d_fprintf(stderr, _("domain controller is not "
6419 "responding: %s\n"),
6420 nt_errstr(nt_status));
6421 d_printf(_("couldn't get domain's sid\n"));
6425 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6427 if (!found_domain) {
6431 /* close opened samr and domain policy handles */
6432 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6433 if (!NT_STATUS_IS_OK(nt_status)) {
6434 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6437 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6438 if (!NT_STATUS_IS_OK(nt_status)) {
6439 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6442 /* close samr pipe and connection to IPC$ */
6445 talloc_destroy(mem_ctx);
6450 * Entrypoint for 'net rpc trustdom' code.
6452 * @param argc Standard argc.
6453 * @param argv Standard argv without initial components.
6455 * @return Integer status (0 means success).
6458 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6460 struct functable func[] = {
6465 N_("Add trusting domain's account"),
6466 N_("net rpc trustdom add\n"
6467 " Add trusting domain's account")
6473 N_("Remove trusting domain's account"),
6474 N_("net rpc trustdom del\n"
6475 " Remove trusting domain's account")
6479 rpc_trustdom_establish,
6481 N_("Establish outgoing trust relationship"),
6482 N_("net rpc trustdom establish\n"
6483 " Establish outgoing trust relationship")
6487 rpc_trustdom_revoke,
6489 N_("Revoke outgoing trust relationship"),
6490 N_("net rpc trustdom revoke\n"
6491 " Revoke outgoing trust relationship")
6497 N_("List in- and outgoing domain trusts"),
6498 N_("net rpc trustdom list\n"
6499 " List in- and outgoing domain trusts")
6503 rpc_trustdom_vampire,
6505 N_("Vampire trusts from remote server"),
6506 N_("net rpc trustdom vampire\n"
6507 " Vampire trusts from remote server")
6509 {NULL, NULL, 0, NULL, NULL}
6512 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6516 * Check if a server will take rpc commands
6517 * @param flags Type of server to connect to (PDC, DMB, localhost)
6518 * if the host is not explicitly specified
6519 * @return bool (true means rpc supported)
6521 bool net_rpc_check(struct net_context *c, unsigned flags)
6523 struct cli_state *cli;
6525 struct sockaddr_storage server_ss;
6526 char *server_name = NULL;
6529 /* flags (i.e. server type) may depend on command */
6530 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6533 if ((cli = cli_initialise()) == NULL) {
6537 status = cli_connect(cli, server_name, &server_ss);
6538 if (!NT_STATUS_IS_OK(status))
6540 if (!attempt_netbios_session_request(&cli, global_myname(),
6541 server_name, &server_ss))
6543 status = cli_negprot(cli);
6544 if (!NT_STATUS_IS_OK(status))
6546 if (cli->protocol < PROTOCOL_NT1)
6555 /* dump sam database via samsync rpc calls */
6556 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6557 if (c->display_usage) {
6562 _("Dump remote SAM database"));
6566 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6567 NET_FLAGS_ANONYMOUS,
6568 rpc_samdump_internals, argc, argv);
6571 /* syncronise sam database via samsync rpc calls */
6572 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6574 struct functable func[] = {
6579 N_("Dump remote SAM database to ldif"),
6580 N_("net rpc vampire ldif\n"
6581 " Dump remote SAM database to LDIF file or "
6588 N_("Dump remote SAM database to Kerberos Keytab"),
6589 N_("net rpc vampire keytab\n"
6590 " Dump remote SAM database to Kerberos keytab "
6597 N_("Dump remote SAM database to passdb"),
6598 N_("net rpc vampire passdb\n"
6599 " Dump remote SAM database to passdb")
6602 {NULL, NULL, 0, NULL, NULL}
6606 if (c->display_usage) {
6611 _("Vampire remote SAM database"));
6615 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6616 NET_FLAGS_ANONYMOUS,
6617 rpc_vampire_internals,
6621 return net_run_function(c, argc, argv, "net rpc vampire", func);
6625 * Migrate everything from a print server.
6627 * @param c A net_context structure.
6628 * @param argc Standard main() style argc.
6629 * @param argv Standard main() style argv. Initial components are already
6632 * @return A shell status integer (0 for success).
6634 * The order is important !
6635 * To successfully add drivers the print queues have to exist !
6636 * Applying ACLs should be the last step, because you're easily locked out.
6639 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6644 if (c->display_usage) {
6646 "net rpc printer migrate all\n"
6649 _("Migrate everything from a print server"));
6654 d_printf(_("no server to migrate\n"));
6658 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6659 rpc_printer_migrate_printers_internals, argc,
6664 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6665 rpc_printer_migrate_drivers_internals, argc,
6670 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6671 rpc_printer_migrate_forms_internals, argc, argv);
6675 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6676 rpc_printer_migrate_settings_internals, argc,
6681 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6682 rpc_printer_migrate_security_internals, argc,
6688 * Migrate print drivers from a print server.
6690 * @param c A net_context structure.
6691 * @param argc Standard main() style argc.
6692 * @param argv Standard main() style argv. Initial components are already
6695 * @return A shell status integer (0 for success).
6697 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6700 if (c->display_usage) {
6702 "net rpc printer migrate drivers\n"
6705 _("Migrate print-drivers from a print-server"));
6710 d_printf(_("no server to migrate\n"));
6714 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6715 rpc_printer_migrate_drivers_internals,
6720 * Migrate print-forms from a print-server.
6722 * @param c A net_context structure.
6723 * @param argc Standard main() style argc.
6724 * @param argv Standard main() style argv. Initial components are already
6727 * @return A shell status integer (0 for success).
6729 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6732 if (c->display_usage) {
6734 "net rpc printer migrate forms\n"
6737 _("Migrate print-forms from a print-server"));
6742 d_printf(_("no server to migrate\n"));
6746 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6747 rpc_printer_migrate_forms_internals,
6752 * Migrate printers from a print-server.
6754 * @param c A net_context structure.
6755 * @param argc Standard main() style argc.
6756 * @param argv Standard main() style argv. Initial components are already
6759 * @return A shell status integer (0 for success).
6761 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6764 if (c->display_usage) {
6766 "net rpc printer migrate printers\n"
6769 _("Migrate printers from a print-server"));
6774 d_printf(_("no server to migrate\n"));
6778 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6779 rpc_printer_migrate_printers_internals,
6784 * Migrate printer-ACLs from a print-server
6786 * @param c A net_context structure.
6787 * @param argc Standard main() style argc.
6788 * @param argv Standard main() style argv. Initial components are already
6791 * @return A shell status integer (0 for success).
6793 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6796 if (c->display_usage) {
6798 "net rpc printer migrate security\n"
6801 _("Migrate printer-ACLs from a print-server"));
6806 d_printf(_("no server to migrate\n"));
6810 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6811 rpc_printer_migrate_security_internals,
6816 * Migrate printer-settings from a print-server.
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_migrate_settings(struct net_context *c, int argc,
6828 if (c->display_usage) {
6830 "net rpc printer migrate settings\n"
6833 _("Migrate printer-settings from a "
6839 d_printf(_("no server to migrate\n"));
6843 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6844 rpc_printer_migrate_settings_internals,
6849 * 'net rpc printer' entrypoint.
6851 * @param c A net_context structure.
6852 * @param argc Standard main() style argc.
6853 * @param argv Standard main() style argv. Initial components are already
6857 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6860 /* ouch: when addriver and setdriver are called from within
6861 rpc_printer_migrate_drivers_internals, the printer-queue already
6864 struct functable func[] = {
6867 rpc_printer_migrate_all,
6869 N_("Migrate all from remote to local print server"),
6870 N_("net rpc printer migrate all\n"
6871 " Migrate all from remote to local print server")
6875 rpc_printer_migrate_drivers,
6877 N_("Migrate drivers to local server"),
6878 N_("net rpc printer migrate drivers\n"
6879 " Migrate drivers to local server")
6883 rpc_printer_migrate_forms,
6885 N_("Migrate froms to local server"),
6886 N_("net rpc printer migrate forms\n"
6887 " Migrate froms to local server")
6891 rpc_printer_migrate_printers,
6893 N_("Migrate printers to local server"),
6894 N_("net rpc printer migrate printers\n"
6895 " Migrate printers to local server")
6899 rpc_printer_migrate_security,
6901 N_("Mirgate printer ACLs to local server"),
6902 N_("net rpc printer migrate security\n"
6903 " Mirgate printer ACLs to local server")
6907 rpc_printer_migrate_settings,
6909 N_("Migrate printer settings to local server"),
6910 N_("net rpc printer migrate settings\n"
6911 " Migrate printer settings to local server")
6913 {NULL, NULL, 0, NULL, NULL}
6916 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6921 * List printers on a remote RPC server.
6923 * @param c A net_context structure.
6924 * @param argc Standard main() style argc.
6925 * @param argv Standard main() style argv. Initial components are already
6928 * @return A shell status integer (0 for success).
6930 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6932 if (c->display_usage) {
6934 "net rpc printer list\n"
6937 _("List printers on a remote RPC server"));
6941 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6942 rpc_printer_list_internals,
6947 * List printer-drivers on a remote RPC server.
6949 * @param c A net_context structure.
6950 * @param argc Standard main() style argc.
6951 * @param argv Standard main() style argv. Initial components are already
6954 * @return A shell status integer (0 for success).
6956 static int rpc_printer_driver_list(struct net_context *c, int argc,
6959 if (c->display_usage) {
6961 "net rpc printer driver\n"
6964 _("List printer-drivers on a remote RPC server"));
6968 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6969 rpc_printer_driver_list_internals,
6974 * Publish printer in ADS via MSRPC.
6976 * @param c A net_context structure.
6977 * @param argc Standard main() style argc.
6978 * @param argv Standard main() style argv. Initial components are already
6981 * @return A shell status integer (0 for success).
6983 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6986 if (c->display_usage) {
6988 "net rpc printer publish publish\n"
6991 _("Publish printer in ADS via MSRPC"));
6995 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6996 rpc_printer_publish_publish_internals,
7001 * Update printer in ADS via MSRPC.
7003 * @param c A net_context structure.
7004 * @param argc Standard main() style argc.
7005 * @param argv Standard main() style argv. Initial components are already
7008 * @return A shell status integer (0 for success).
7010 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7012 if (c->display_usage) {
7014 "net rpc printer publish update\n"
7017 _("Update printer in ADS via MSRPC"));
7021 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7022 rpc_printer_publish_update_internals,
7027 * UnPublish printer in ADS via MSRPC.
7029 * @param c A net_context structure.
7030 * @param argc Standard main() style argc.
7031 * @param argv Standard main() style argv. Initial components are already
7034 * @return A shell status integer (0 for success).
7036 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7039 if (c->display_usage) {
7041 "net rpc printer publish unpublish\n"
7044 _("UnPublish printer in ADS via MSRPC"));
7048 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7049 rpc_printer_publish_unpublish_internals,
7054 * List published printers via MSRPC.
7056 * @param c A net_context structure.
7057 * @param argc Standard main() style argc.
7058 * @param argv Standard main() style argv. Initial components are already
7061 * @return A shell status integer (0 for success).
7063 static int rpc_printer_publish_list(struct net_context *c, int argc,
7066 if (c->display_usage) {
7068 "net rpc printer publish list\n"
7071 _("List published printers via MSRPC"));
7075 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7076 rpc_printer_publish_list_internals,
7082 * Publish printer in ADS.
7084 * @param c A net_context structure.
7085 * @param argc Standard main() style argc.
7086 * @param argv Standard main() style argv. Initial components are already
7089 * @return A shell status integer (0 for success).
7091 static int rpc_printer_publish(struct net_context *c, int argc,
7095 struct functable func[] = {
7098 rpc_printer_publish_publish,
7100 N_("Publish printer in AD"),
7101 N_("net rpc printer publish publish\n"
7102 " Publish printer in AD")
7106 rpc_printer_publish_update,
7108 N_("Update printer in AD"),
7109 N_("net rpc printer publish update\n"
7110 " Update printer in AD")
7114 rpc_printer_publish_unpublish,
7116 N_("Unpublish printer"),
7117 N_("net rpc printer publish unpublish\n"
7118 " Unpublish printer")
7122 rpc_printer_publish_list,
7124 N_("List published printers"),
7125 N_("net rpc printer publish list\n"
7126 " List published printers")
7128 {NULL, NULL, 0, NULL, NULL}
7132 if (c->display_usage) {
7133 d_printf(_("Usage:\n"));
7134 d_printf(_("net rpc printer publish\n"
7135 " List published printers\n"
7136 " Alias of net rpc printer publish "
7138 net_display_usage_from_functable(func);
7141 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7142 rpc_printer_publish_list_internals,
7146 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7152 * Display rpc printer help page.
7154 * @param c A net_context structure.
7155 * @param argc Standard main() style argc.
7156 * @param argv Standard main() style argv. Initial components are already
7159 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7161 d_printf(_("net rpc printer LIST [printer] [misc. options] [targets]\n"
7162 "\tlists all printers on print-server\n\n"));
7163 d_printf(_("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7164 "\tlists all printer-drivers on print-server\n\n"));
7165 d_printf(_("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7166 "\tpublishes printer settings in Active Directory\n"
7167 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n"));
7168 d_printf(_("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7169 "\n\tmigrates printers from remote to local server\n\n"));
7170 d_printf(_("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7171 "\n\tmigrates printer-settings from remote to local server\n\n"));
7172 d_printf(_("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7173 "\n\tmigrates printer-drivers from remote to local server\n\n"));
7174 d_printf(_("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7175 "\n\tmigrates printer-forms from remote to local server\n\n"));
7176 d_printf(_("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7177 "\n\tmigrates printer-ACLs from remote to local server\n\n"));
7178 d_printf(_("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7179 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7180 "\tremote to local print-server\n\n"));
7181 net_common_methods_usage(c, argc, argv);
7182 net_common_flags_usage(c, argc, argv);
7184 "\t-v or --verbose\t\t\tgive verbose output\n"
7185 "\t --destination\t\tmigration target server (default: localhost)\n"));
7191 * 'net rpc printer' entrypoint.
7193 * @param c A net_context structure.
7194 * @param argc Standard main() style argc.
7195 * @param argv Standard main() style argv. Initial components are already
7198 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7200 struct functable func[] = {
7205 N_("List all printers on print server"),
7206 N_("net rpc printer list\n"
7207 " List all printers on print server")
7211 rpc_printer_migrate,
7213 N_("Migrate printer to local server"),
7214 N_("net rpc printer migrate\n"
7215 " Migrate printer to local server")
7219 rpc_printer_driver_list,
7221 N_("List printer drivers"),
7222 N_("net rpc printer driver\n"
7223 " List printer drivers")
7227 rpc_printer_publish,
7229 N_("Publish printer in AD"),
7230 N_("net rpc printer publish\n"
7231 " Publish printer in AD")
7233 {NULL, NULL, 0, NULL, NULL}
7237 if (c->display_usage) {
7238 d_printf(_("Usage:\n"));
7239 d_printf(_("net rpc printer\n"
7240 " List printers\n"));
7241 net_display_usage_from_functable(func);
7244 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
7245 rpc_printer_list_internals,
7249 return net_run_function(c, argc, argv, "net rpc printer", func);
7253 * 'net rpc' entrypoint.
7255 * @param c A net_context structure.
7256 * @param argc Standard main() style argc.
7257 * @param argv Standard main() style argv. Initial components are already
7261 int net_rpc(struct net_context *c, int argc, const char **argv)
7263 NET_API_STATUS status;
7265 struct functable func[] = {
7270 N_("Modify global audit settings"),
7271 N_("net rpc audit\n"
7272 " Modify global audit settings")
7278 N_("Show basic info about a domain"),
7280 " Show basic info about a domain")
7286 N_("Join a domain"),
7294 N_("Join a domain created in server manager"),
7295 N_("net rpc oldjoin\n"
7296 " Join a domain created in server manager")
7302 N_("Test that a join is valid"),
7303 N_("net rpc testjoin\n"
7304 " Test that a join is valid")
7310 N_("List/modify users"),
7312 " List/modify users")
7318 N_("Change a user password"),
7319 N_("net rpc password\n"
7320 " Change a user password\n"
7321 " Alias for net rpc user password")
7327 N_("List/modify groups"),
7328 N_("net rpc group\n"
7329 " List/modify groups")
7335 N_("List/modify shares"),
7336 N_("net rpc share\n"
7337 " List/modify shares")
7343 N_("List open files"),
7351 N_("List/modify printers"),
7352 N_("net rpc printer\n"
7353 " List/modify printers")
7357 net_rpc_changetrustpw,
7359 N_("Change trust account password"),
7360 N_("net rpc changetrustpw\n"
7361 " Change trust account password")
7367 N_("Modify domain trusts"),
7368 N_("net rpc trustdom\n"
7369 " Modify domain trusts")
7375 N_("Abort a remote shutdown"),
7376 N_("net rpc abortshutdown\n"
7377 " Abort a remote shutdown")
7383 N_("Shutdown a remote server"),
7384 N_("net rpc shutdown\n"
7385 " Shutdown a remote server")
7391 N_("Dump SAM data of remote NT PDC"),
7392 N_("net rpc samdump\n"
7393 " Dump SAM data of remote NT PDC")
7399 N_("Sync a remote NT PDC's data into local passdb"),
7400 N_("net rpc vampire\n"
7401 " Sync a remote NT PDC's data into local passdb")
7407 N_("Fetch the domain sid into local secrets.tdb"),
7408 N_("net rpc getsid\n"
7409 " Fetch the domain sid into local secrets.tdb")
7415 N_("Manage privileges assigned to SID"),
7416 N_("net rpc rights\n"
7417 " Manage privileges assigned to SID")
7423 N_("Start/stop/query remote services"),
7424 N_("net rpc service\n"
7425 " Start/stop/query remote services")
7431 N_("Manage registry hives"),
7432 N_("net rpc registry\n"
7433 " Manage registry hives")
7439 N_("Open interactive shell on remote server"),
7440 N_("net rpc shell\n"
7441 " Open interactive shell on remote server")
7443 {NULL, NULL, 0, NULL, NULL}
7446 status = libnetapi_net_init(&c->netapi_ctx);
7450 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
7451 libnetapi_set_password(c->netapi_ctx, c->opt_password);
7452 if (c->opt_kerberos) {
7453 libnetapi_set_use_kerberos(c->netapi_ctx);
7455 if (c->opt_ccache) {
7456 libnetapi_set_use_ccache(c->netapi_ctx);
7459 return net_run_function(c, argc, argv, "net rpc", func);