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"
27 static int net_mode_share;
28 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
33 * @brief RPC based subcommands for the 'net' utility.
35 * This file should contain much of the functionality that used to
36 * be found in rpcclient, execpt that the commands should change
37 * less often, and the fucntionality should be sane (the user is not
38 * expected to know a rid/sid before they conduct an operation etc.)
40 * @todo Perhaps eventually these should be split out into a number
41 * of files, as this could get quite big.
46 * Many of the RPC functions need the domain sid. This function gets
47 * it at the start of every run
49 * @param cli A cli_state already connected to the remote machine
51 * @return The Domain SID of the remote machine.
54 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
56 const char **domain_name)
58 struct rpc_pipe_client *lsa_pipe = NULL;
59 struct policy_handle pol;
60 NTSTATUS result = NT_STATUS_OK;
61 union lsa_PolicyInformation *info = NULL;
63 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
65 if (!NT_STATUS_IS_OK(result)) {
66 d_fprintf(stderr, _("Could not initialise lsa pipe\n"));
70 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
71 SEC_FLAG_MAXIMUM_ALLOWED,
73 if (!NT_STATUS_IS_OK(result)) {
74 d_fprintf(stderr, _("open_policy failed: %s\n"),
79 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
81 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
83 if (!NT_STATUS_IS_OK(result)) {
84 d_fprintf(stderr, _("lsaquery failed: %s\n"),
89 *domain_name = info->account_domain.name.string;
90 *domain_sid = info->account_domain.sid;
92 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
93 TALLOC_FREE(lsa_pipe);
99 * Run a single RPC command, from start to finish.
101 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
102 * @param conn_flag a NET_FLAG_ combination. Passed to
103 * net_make_ipc_connection.
104 * @param argc Standard main() style argc.
105 * @param argv Standard main() style argv. Initial components are already
107 * @return A shell status integer (0 for success).
110 int run_rpc_command(struct net_context *c,
111 struct cli_state *cli_arg,
112 const struct ndr_syntax_id *interface,
118 struct cli_state *cli = NULL;
119 struct rpc_pipe_client *pipe_hnd = NULL;
123 const char *domain_name;
126 /* make use of cli_state handed over as an argument, if possible */
128 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
129 if (!NT_STATUS_IS_OK(nt_status)) {
130 DEBUG(1, ("failed to make ipc connection: %s\n",
131 nt_errstr(nt_status)));
144 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
145 DEBUG(0, ("talloc_init() failed\n"));
149 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
151 if (!NT_STATUS_IS_OK(nt_status)) {
155 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
156 if (lp_client_schannel()
157 && (ndr_syntax_id_equal(interface,
158 &ndr_table_netlogon.syntax_id))) {
159 /* Always try and create an schannel netlogon pipe. */
160 nt_status = cli_rpc_pipe_open_schannel(
161 cli, interface, NCACN_NP,
162 DCERPC_AUTH_LEVEL_PRIVACY, domain_name,
164 if (!NT_STATUS_IS_OK(nt_status)) {
165 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
166 nt_errstr(nt_status) ));
170 if (conn_flags & NET_FLAGS_SEAL) {
171 nt_status = cli_rpc_pipe_open_ntlmssp(
173 (conn_flags & NET_FLAGS_TCP) ?
174 NCACN_IP_TCP : NCACN_NP,
175 DCERPC_AUTH_LEVEL_PRIVACY,
176 lp_workgroup(), c->opt_user_name,
177 c->opt_password, &pipe_hnd);
179 nt_status = cli_rpc_pipe_open_noauth(
183 if (!NT_STATUS_IS_OK(nt_status)) {
184 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
185 get_pipe_name_from_iface(interface),
186 nt_errstr(nt_status) ));
192 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
194 if (!NT_STATUS_IS_OK(nt_status)) {
195 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
198 DEBUG(5, ("rpc command function succedded\n"));
201 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
203 TALLOC_FREE(pipe_hnd);
208 /* close the connection only if it was opened here */
213 talloc_destroy(mem_ctx);
218 * Force a change of the trust acccount password.
220 * All parameters are provided by the run_rpc_command function, except for
221 * argc, argv which are passed through.
223 * @param domain_sid The domain sid acquired from the remote server.
224 * @param cli A cli_state connected to the server.
225 * @param mem_ctx Talloc context, destroyed on completion of the function.
226 * @param argc Standard main() style argc.
227 * @param argv Standard main() style argv. Initial components are already
230 * @return Normal NTSTATUS return.
233 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
234 const DOM_SID *domain_sid,
235 const char *domain_name,
236 struct cli_state *cli,
237 struct rpc_pipe_client *pipe_hnd,
244 status = trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
245 if (!NT_STATUS_IS_OK(status)) {
246 d_fprintf(stderr, _("Failed to change machine account password: %s\n"),
255 * Force a change of the trust acccount password.
257 * @param argc Standard main() style argc.
258 * @param argv Standard main() style argv. Initial components are already
261 * @return A shell status integer (0 for success).
264 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
266 if (c->display_usage) {
267 d_printf(_("Usage:\n"
268 "net rpc changetrustpw\n"
269 " Change the machine trust password\n"));
273 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
274 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
275 rpc_changetrustpw_internals,
280 * Join a domain, the old way.
282 * This uses 'machinename' as the inital password, and changes it.
284 * The password should be created with 'server manager' or equiv first.
286 * All parameters are provided by the run_rpc_command function, except for
287 * argc, argv which are passed through.
289 * @param domain_sid The domain sid acquired from the remote server.
290 * @param cli A cli_state connected to the server.
291 * @param mem_ctx Talloc context, destroyed on completion of the function.
292 * @param argc Standard main() style argc.
293 * @param argv Standard main() style argv. Initial components are already
296 * @return Normal NTSTATUS return.
299 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
300 const DOM_SID *domain_sid,
301 const char *domain_name,
302 struct cli_state *cli,
303 struct rpc_pipe_client *pipe_hnd,
309 fstring trust_passwd;
310 unsigned char orig_trust_passwd_hash[16];
312 enum netr_SchannelType sec_channel_type;
314 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
316 if (!NT_STATUS_IS_OK(result)) {
317 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
320 nt_errstr(result) ));
325 check what type of join - if the user want's to join as
326 a BDC, the server must agree that we are a BDC.
329 sec_channel_type = get_sec_channel_type(argv[0]);
331 sec_channel_type = get_sec_channel_type(NULL);
334 fstrcpy(trust_passwd, global_myname());
335 strlower_m(trust_passwd);
338 * Machine names can be 15 characters, but the max length on
339 * a password is 14. --jerry
342 trust_passwd[14] = '\0';
344 E_md4hash(trust_passwd, orig_trust_passwd_hash);
346 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
348 orig_trust_passwd_hash,
351 if (NT_STATUS_IS_OK(result))
352 printf(_("Joined domain %s.\n"), c->opt_target_workgroup);
355 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
356 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
357 result = NT_STATUS_UNSUCCESSFUL;
364 * Join a domain, the old way.
366 * @param argc Standard main() style argc.
367 * @param argv Standard main() style argv. Initial components are already
370 * @return A shell status integer (0 for success).
373 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
375 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
376 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
377 rpc_oldjoin_internals,
382 * Join a domain, the old way. This function exists to allow
383 * the message to be displayed when oldjoin was explicitly
384 * requested, but not when it was implied by "net rpc join".
386 * @param argc Standard main() style argc.
387 * @param argv Standard main() style argv. Initial components are already
390 * @return A shell status integer (0 for success).
393 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
397 if (c->display_usage) {
398 d_printf(_("Usage:\n"
400 " Join a domain the old way\n"));
404 rc = net_rpc_perform_oldjoin(c, argc, argv);
407 d_fprintf(stderr, _("Failed to join domain\n"));
414 * 'net rpc join' entrypoint.
415 * @param argc Standard main() style argc.
416 * @param argv Standard main() style argv. Initial components are already
419 * Main 'net_rpc_join()' (where the admin username/password is used) is
421 * Try to just change the password, but if that doesn't work, use/prompt
422 * for a username/password.
425 int net_rpc_join(struct net_context *c, int argc, const char **argv)
427 if (c->display_usage) {
428 d_printf(_("Usage:\n"
429 "net rpc join -U <username>[%%password] <type>\n"
431 " username\tName of the admin user"
432 " password\tPassword of the admin user, will "
433 "prompt if not specified\n"
434 " type\tCan be one of the following:\n"
435 "\t\tMEMBER\tJoin as member server (default)\n"
436 "\t\tBDC\tJoin as BDC\n"
437 "\t\tPDC\tJoin as PDC\n"));
441 if (lp_server_role() == ROLE_STANDALONE) {
442 d_printf(_("cannot join as standalone machine\n"));
446 if (strlen(global_myname()) > 15) {
447 d_printf(_("Our netbios name can be at most 15 chars long, "
448 "\"%s\" is %u chars long\n"),
449 global_myname(), (unsigned int)strlen(global_myname()));
453 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
456 return net_rpc_join_newstyle(c, argc, argv);
460 * display info about a rpc domain
462 * All parameters are provided by the run_rpc_command function, except for
463 * argc, argv which are passed through.
465 * @param domain_sid The domain sid acquired from the remote server
466 * @param cli A cli_state connected to the server.
467 * @param mem_ctx Talloc context, destroyed on completion of the function.
468 * @param argc Standard main() style argc.
469 * @param argv Standard main() style argv. Initial components are already
472 * @return Normal NTSTATUS return.
475 NTSTATUS rpc_info_internals(struct net_context *c,
476 const DOM_SID *domain_sid,
477 const char *domain_name,
478 struct cli_state *cli,
479 struct rpc_pipe_client *pipe_hnd,
484 struct policy_handle connect_pol, domain_pol;
485 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
486 union samr_DomainInfo *info = NULL;
489 sid_to_fstring(sid_str, domain_sid);
491 /* Get sam policy handle */
492 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
494 MAXIMUM_ALLOWED_ACCESS,
496 if (!NT_STATUS_IS_OK(result)) {
497 d_fprintf(stderr, _("Could not connect to SAM: %s\n"),
502 /* Get domain policy handle */
503 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
505 MAXIMUM_ALLOWED_ACCESS,
506 CONST_DISCARD(struct dom_sid2 *, domain_sid),
508 if (!NT_STATUS_IS_OK(result)) {
509 d_fprintf(stderr, _("Could not open domain: %s\n"),
514 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
518 if (NT_STATUS_IS_OK(result)) {
519 d_printf(_("Domain Name: %s\n"),
520 info->general.domain_name.string);
521 d_printf(_("Domain SID: %s\n"), sid_str);
522 d_printf(_("Sequence number: %llu\n"),
523 (unsigned long long)info->general.sequence_num);
524 d_printf(_("Num users: %u\n"), info->general.num_users);
525 d_printf(_("Num domain groups: %u\n"),info->general.num_groups);
526 d_printf(_("Num local groups: %u\n"),info->general.num_aliases);
534 * 'net rpc info' entrypoint.
535 * @param argc Standard main() style argc.
536 * @param argv Standard main() style argv. Initial components are already
540 int net_rpc_info(struct net_context *c, int argc, const char **argv)
542 if (c->display_usage) {
543 d_printf(_("Usage:\n"
545 " Display information about the domain\n"));
549 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
550 NET_FLAGS_PDC, rpc_info_internals,
555 * Fetch domain SID into the local secrets.tdb.
557 * All parameters are provided by the run_rpc_command function, except for
558 * argc, argv which are passed through.
560 * @param domain_sid The domain sid acquired from the remote server.
561 * @param cli A cli_state connected to the server.
562 * @param mem_ctx Talloc context, destroyed on completion of the function.
563 * @param argc Standard main() style argc.
564 * @param argv Standard main() style argv. Initial components are already
567 * @return Normal NTSTATUS return.
570 static NTSTATUS rpc_getsid_internals(struct net_context *c,
571 const DOM_SID *domain_sid,
572 const char *domain_name,
573 struct cli_state *cli,
574 struct rpc_pipe_client *pipe_hnd,
581 sid_to_fstring(sid_str, domain_sid);
582 d_printf(_("Storing SID %s for Domain %s in secrets.tdb\n"),
583 sid_str, domain_name);
585 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
586 DEBUG(0,("Can't store domain SID\n"));
587 return NT_STATUS_UNSUCCESSFUL;
594 * 'net rpc getsid' entrypoint.
595 * @param argc Standard main() style argc.
596 * @param argv Standard main() style argv. Initial components are already
600 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
602 if (c->display_usage) {
603 d_printf(_("Usage:\n"
605 " Fetch domain SID into local secrets.tdb\n"));
609 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
610 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
611 rpc_getsid_internals,
615 /****************************************************************************/
618 * Basic usage function for 'net rpc user'.
619 * @param argc Standard main() style argc.
620 * @param argv Standard main() style argv. Initial components are already
624 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
626 return net_user_usage(c, argc, argv);
630 * Add a new user to a remote RPC server.
632 * @param argc Standard main() style argc.
633 * @param argv Standard main() style argv. Initial components are already
636 * @return A shell status integer (0 for success).
639 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
641 NET_API_STATUS status;
642 struct USER_INFO_1 info1;
643 uint32_t parm_error = 0;
645 if (argc < 1 || c->display_usage) {
646 rpc_user_usage(c, argc, argv);
652 info1.usri1_name = argv[0];
654 info1.usri1_password = argv[1];
657 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
660 d_fprintf(stderr,_("Failed to add user '%s' with error: %s.\n"),
661 argv[0], libnetapi_get_error_string(c->netapi_ctx,
665 d_printf(_("Added user '%s'.\n"), argv[0]);
672 * Rename a user on a remote RPC server.
674 * @param argc Standard main() style argc.
675 * @param argv Standard main() style argv. Initial components are already
678 * @return A shell status integer (0 for success).
681 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
683 NET_API_STATUS status;
684 struct USER_INFO_0 u0;
685 uint32_t parm_err = 0;
687 if (argc != 2 || c->display_usage) {
688 rpc_user_usage(c, argc, argv);
692 u0.usri0_name = argv[1];
694 status = NetUserSetInfo(c->opt_host, argv[0],
695 0, (uint8_t *)&u0, &parm_err);
698 _("Failed to rename user from %s to %s - %s\n"),
700 libnetapi_get_error_string(c->netapi_ctx, status));
702 d_printf(_("Renamed user from %s to %s\n"), argv[0], argv[1]);
709 * Delete a user from a remote RPC server.
711 * @param argc Standard main() style argc.
712 * @param argv Standard main() style argv. Initial components are already
715 * @return A shell status integer (0 for success).
718 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
720 NET_API_STATUS status;
722 if (argc < 1 || c->display_usage) {
723 rpc_user_usage(c, argc, argv);
727 status = NetUserDel(c->opt_host, argv[0]);
730 d_fprintf(stderr, _("Failed to delete user '%s' with: %s.\n"),
732 libnetapi_get_error_string(c->netapi_ctx, status));
735 d_printf(_("Deleted user '%s'.\n"), argv[0]);
742 * Set a user's password on a remote RPC server.
744 * @param argc Standard main() style argc.
745 * @param argv Standard main() style argv. Initial components are already
748 * @return A shell status integer (0 for success).
751 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
753 NET_API_STATUS status;
755 struct USER_INFO_1003 u1003;
756 uint32_t parm_err = 0;
759 if (argc < 1 || c->display_usage) {
760 rpc_user_usage(c, argc, argv);
765 u1003.usri1003_password = argv[1];
767 ret = asprintf(&prompt, _("Enter new password for %s:"),
772 u1003.usri1003_password = getpass(prompt);
776 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
778 /* Display results */
781 _("Failed to set password for '%s' with error: %s.\n"),
782 argv[0], libnetapi_get_error_string(c->netapi_ctx,
791 * List a user's groups from a remote RPC server.
793 * @param argc Standard main() style argc.
794 * @param argv Standard main() style argv. Initial components are already
797 * @return A shell status integer (0 for success)
800 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
803 NET_API_STATUS status;
804 struct GROUP_USERS_INFO_0 *u0 = NULL;
805 uint32_t entries_read = 0;
806 uint32_t total_entries = 0;
810 if (argc < 1 || c->display_usage) {
811 rpc_user_usage(c, argc, argv);
815 status = NetUserGetGroups(c->opt_host,
818 (uint8_t **)(void *)&u0,
824 _("Failed to get groups for '%s' with error: %s.\n"),
825 argv[0], libnetapi_get_error_string(c->netapi_ctx,
830 for (i=0; i < entries_read; i++) {
831 printf("%s\n", u0->grui0_name);
839 * List users on a remote RPC server.
841 * All parameters are provided by the run_rpc_command function, except for
842 * argc, argv which are passed through.
844 * @param domain_sid The domain sid acquired from the remote server.
845 * @param cli A cli_state connected to the server.
846 * @param mem_ctx Talloc context, destroyed on completion of the function.
847 * @param argc Standard main() style argc.
848 * @param argv Standard main() style argv. Initial components are already
851 * @return Normal NTSTATUS return.
854 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
856 NET_API_STATUS status;
857 uint32_t start_idx=0, num_entries, i, loop_count = 0;
858 struct NET_DISPLAY_USER *info = NULL;
861 /* Query domain users */
862 if (c->opt_long_list_entries)
863 d_printf(_("\nUser name Comment"
864 "\n-----------------------------\n"));
866 uint32_t max_entries, max_size;
868 get_query_dispinfo_params(
869 loop_count, &max_entries, &max_size);
871 status = NetQueryDisplayInformation(c->opt_host,
878 if (status != 0 && status != ERROR_MORE_DATA) {
882 info = (struct NET_DISPLAY_USER *)buffer;
884 for (i = 0; i < num_entries; i++) {
886 if (c->opt_long_list_entries)
887 printf("%-21.21s %s\n", info->usri1_name,
888 info->usri1_comment);
890 printf("%s\n", info->usri1_name);
894 NetApiBufferFree(buffer);
897 start_idx += num_entries;
899 } while (status == ERROR_MORE_DATA);
905 * 'net rpc user' entrypoint.
906 * @param argc Standard main() style argc.
907 * @param argv Standard main() style argv. Initial components are already
911 int net_rpc_user(struct net_context *c, int argc, const char **argv)
913 NET_API_STATUS status;
915 struct functable func[] = {
920 N_("Add specified user"),
921 N_("net rpc user add\n"
922 " Add specified user")
928 N_("List domain groups of user"),
929 N_("net rpc user info\n"
930 " Lis domain groups of user")
936 N_("Remove specified user"),
937 N_("net rpc user delete\n"
938 " Remove specified user")
944 N_("Change user password"),
945 N_("net rpc user password\n"
946 " Change user password")
952 N_("Rename specified user"),
953 N_("net rpc user rename\n"
954 " Rename specified user")
956 {NULL, NULL, 0, NULL, NULL}
959 status = libnetapi_init(&c->netapi_ctx);
963 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
964 libnetapi_set_password(c->netapi_ctx, c->opt_password);
965 if (c->opt_kerberos) {
966 libnetapi_set_use_kerberos(c->netapi_ctx);
970 if (c->display_usage) {
971 d_printf(_("Usage:\n"));
972 d_printf(_("net rpc user\n"
973 " List all users\n"));
974 net_display_usage_from_functable(func);
978 return rpc_user_list(c, argc, argv);
981 return net_run_function(c, argc, argv, "net rpc user", func);
984 static NTSTATUS rpc_sh_user_list(struct net_context *c,
986 struct rpc_sh_ctx *ctx,
987 struct rpc_pipe_client *pipe_hnd,
988 int argc, const char **argv)
990 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
993 static NTSTATUS rpc_sh_user_info(struct net_context *c,
995 struct rpc_sh_ctx *ctx,
996 struct rpc_pipe_client *pipe_hnd,
997 int argc, const char **argv)
999 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
1002 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1003 TALLOC_CTX *mem_ctx,
1004 struct rpc_sh_ctx *ctx,
1005 struct rpc_pipe_client *pipe_hnd,
1006 int argc, const char **argv,
1008 struct net_context *c,
1009 TALLOC_CTX *mem_ctx,
1010 struct rpc_sh_ctx *ctx,
1011 struct rpc_pipe_client *pipe_hnd,
1012 struct policy_handle *user_hnd,
1013 int argc, const char **argv))
1015 struct policy_handle connect_pol, domain_pol, user_pol;
1016 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1019 enum lsa_SidType type;
1022 d_fprintf(stderr, _("usage: %s <username>\n"), ctx->whoami);
1023 return NT_STATUS_INVALID_PARAMETER;
1026 ZERO_STRUCT(connect_pol);
1027 ZERO_STRUCT(domain_pol);
1028 ZERO_STRUCT(user_pol);
1030 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1031 argv[0], NULL, NULL, &sid, &type);
1032 if (!NT_STATUS_IS_OK(result)) {
1033 d_fprintf(stderr, _("Could not lookup %s: %s\n"), argv[0],
1038 if (type != SID_NAME_USER) {
1039 d_fprintf(stderr, _("%s is a %s, not a user\n"), argv[0],
1040 sid_type_lookup(type));
1041 result = NT_STATUS_NO_SUCH_USER;
1045 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1046 d_fprintf(stderr, _("%s is not in our domain\n"), argv[0]);
1047 result = NT_STATUS_NO_SUCH_USER;
1051 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1053 MAXIMUM_ALLOWED_ACCESS,
1055 if (!NT_STATUS_IS_OK(result)) {
1059 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1061 MAXIMUM_ALLOWED_ACCESS,
1064 if (!NT_STATUS_IS_OK(result)) {
1068 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1070 MAXIMUM_ALLOWED_ACCESS,
1073 if (!NT_STATUS_IS_OK(result)) {
1077 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1080 if (is_valid_policy_hnd(&user_pol)) {
1081 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1083 if (is_valid_policy_hnd(&domain_pol)) {
1084 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1086 if (is_valid_policy_hnd(&connect_pol)) {
1087 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1092 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1093 TALLOC_CTX *mem_ctx,
1094 struct rpc_sh_ctx *ctx,
1095 struct rpc_pipe_client *pipe_hnd,
1096 struct policy_handle *user_hnd,
1097 int argc, const char **argv)
1100 union samr_UserInfo *info = NULL;
1103 d_fprintf(stderr, _("usage: %s show <username>\n"),ctx->whoami);
1104 return NT_STATUS_INVALID_PARAMETER;
1107 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1111 if (!NT_STATUS_IS_OK(result)) {
1115 d_printf(_("user rid: %d, group rid: %d\n"),
1117 info->info21.primary_gid);
1122 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1123 TALLOC_CTX *mem_ctx,
1124 struct rpc_sh_ctx *ctx,
1125 struct rpc_pipe_client *pipe_hnd,
1126 int argc, const char **argv)
1128 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1129 rpc_sh_user_show_internals);
1132 #define FETCHSTR(name, rec) \
1133 do { if (strequal(ctx->thiscmd, name)) { \
1134 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1137 #define SETSTR(name, rec, flag) \
1138 do { if (strequal(ctx->thiscmd, name)) { \
1139 init_lsa_String(&(info->info21.rec), argv[0]); \
1140 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1143 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1144 TALLOC_CTX *mem_ctx,
1145 struct rpc_sh_ctx *ctx,
1146 struct rpc_pipe_client *pipe_hnd,
1147 struct policy_handle *user_hnd,
1148 int argc, const char **argv)
1151 const char *username;
1152 const char *oldval = "";
1153 union samr_UserInfo *info = NULL;
1156 d_fprintf(stderr, _("usage: %s <username> [new value|NULL]\n"),
1158 return NT_STATUS_INVALID_PARAMETER;
1161 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1165 if (!NT_STATUS_IS_OK(result)) {
1169 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1171 FETCHSTR("fullname", full_name);
1172 FETCHSTR("homedir", home_directory);
1173 FETCHSTR("homedrive", home_drive);
1174 FETCHSTR("logonscript", logon_script);
1175 FETCHSTR("profilepath", profile_path);
1176 FETCHSTR("description", description);
1179 d_printf(_("%s's %s: [%s]\n"), username, ctx->thiscmd, oldval);
1183 if (strcmp(argv[0], "NULL") == 0) {
1187 ZERO_STRUCT(info->info21);
1189 SETSTR("fullname", full_name, FULL_NAME);
1190 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1191 SETSTR("homedrive", home_drive, HOME_DRIVE);
1192 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1193 SETSTR("profilepath", profile_path, PROFILE_PATH);
1194 SETSTR("description", description, DESCRIPTION);
1196 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1201 d_printf(_("Set %s's %s from [%s] to [%s]\n"), username,
1202 ctx->thiscmd, oldval, argv[0]);
1209 #define HANDLEFLG(name, rec) \
1210 do { if (strequal(ctx->thiscmd, name)) { \
1211 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1213 newflags = oldflags | ACB_##rec; \
1215 newflags = oldflags & ~ACB_##rec; \
1218 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1219 TALLOC_CTX *mem_ctx,
1220 struct rpc_sh_ctx *ctx,
1221 struct rpc_pipe_client *pipe_hnd,
1222 int argc, const char **argv)
1224 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1225 rpc_sh_user_str_edit_internals);
1228 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1229 TALLOC_CTX *mem_ctx,
1230 struct rpc_sh_ctx *ctx,
1231 struct rpc_pipe_client *pipe_hnd,
1232 struct policy_handle *user_hnd,
1233 int argc, const char **argv)
1236 const char *username;
1237 const char *oldval = "unknown";
1238 uint32 oldflags, newflags;
1240 union samr_UserInfo *info = NULL;
1243 ((argc == 1) && !strequal(argv[0], "yes") &&
1244 !strequal(argv[0], "no"))) {
1245 /* TRANSATORS: The yes|no here are program keywords. Please do
1247 d_fprintf(stderr, _("usage: %s <username> [yes|no]\n"),
1249 return NT_STATUS_INVALID_PARAMETER;
1252 newval = strequal(argv[0], "yes");
1254 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1258 if (!NT_STATUS_IS_OK(result)) {
1262 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1263 oldflags = info->info21.acct_flags;
1264 newflags = info->info21.acct_flags;
1266 HANDLEFLG("disabled", DISABLED);
1267 HANDLEFLG("pwnotreq", PWNOTREQ);
1268 HANDLEFLG("autolock", AUTOLOCK);
1269 HANDLEFLG("pwnoexp", PWNOEXP);
1272 d_printf(_("%s's %s flag: %s\n"), username, ctx->thiscmd,
1277 ZERO_STRUCT(info->info21);
1279 info->info21.acct_flags = newflags;
1280 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1282 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1287 if (NT_STATUS_IS_OK(result)) {
1288 d_printf(_("Set %s's %s flag from [%s] to [%s]\n"), username,
1289 ctx->thiscmd, oldval, argv[0]);
1297 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1298 TALLOC_CTX *mem_ctx,
1299 struct rpc_sh_ctx *ctx,
1300 struct rpc_pipe_client *pipe_hnd,
1301 int argc, const char **argv)
1303 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1304 rpc_sh_user_flag_edit_internals);
1307 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1308 TALLOC_CTX *mem_ctx,
1309 struct rpc_sh_ctx *ctx)
1311 static struct rpc_sh_cmd cmds[] = {
1313 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1314 N_("Show/Set a user's full name") },
1316 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1317 N_("Show/Set a user's home directory") },
1319 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1320 N_("Show/Set a user's home drive") },
1322 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1323 N_("Show/Set a user's logon script") },
1325 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1326 N_("Show/Set a user's profile path") },
1328 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1329 N_("Show/Set a user's description") },
1331 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1332 N_("Show/Set whether a user is disabled") },
1334 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1335 N_("Show/Set whether a user locked out") },
1337 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1338 N_("Show/Set whether a user does not need a password") },
1340 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1341 N_("Show/Set whether a user's password does not expire") },
1343 { NULL, NULL, 0, NULL, NULL }
1349 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1350 TALLOC_CTX *mem_ctx,
1351 struct rpc_sh_ctx *ctx)
1353 static struct rpc_sh_cmd cmds[] = {
1355 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1356 N_("List available users") },
1358 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1359 N_("List the domain groups a user is member of") },
1361 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1362 N_("Show info about a user") },
1364 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1365 N_("Show/Modify a user's fields") },
1367 { NULL, NULL, 0, NULL, NULL }
1373 /****************************************************************************/
1376 * Basic usage function for 'net rpc group'.
1377 * @param argc Standard main() style argc.
1378 * @param argv Standard main() style argv. Initial components are already
1382 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1384 return net_group_usage(c, argc, argv);
1388 * Delete group on a remote RPC server.
1390 * All parameters are provided by the run_rpc_command function, except for
1391 * argc, argv which are passed through.
1393 * @param domain_sid The domain sid acquired from the remote server.
1394 * @param cli A cli_state connected to the server.
1395 * @param mem_ctx Talloc context, destroyed on completion of the function.
1396 * @param argc Standard main() style argc.
1397 * @param argv Standard main() style argv. Initial components are already
1400 * @return Normal NTSTATUS return.
1403 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1404 const DOM_SID *domain_sid,
1405 const char *domain_name,
1406 struct cli_state *cli,
1407 struct rpc_pipe_client *pipe_hnd,
1408 TALLOC_CTX *mem_ctx,
1412 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1413 bool group_is_primary = false;
1414 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1416 struct samr_RidTypeArray *rids = NULL;
1419 /* struct samr_RidWithAttribute *user_gids; */
1421 struct samr_Ids group_rids, name_types;
1422 struct lsa_String lsa_acct_name;
1423 union samr_UserInfo *info = NULL;
1425 if (argc < 1 || c->display_usage) {
1426 rpc_group_usage(c, argc,argv);
1427 return NT_STATUS_OK; /* ok? */
1430 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1432 MAXIMUM_ALLOWED_ACCESS,
1435 if (!NT_STATUS_IS_OK(result)) {
1436 d_fprintf(stderr, _("Request samr_Connect2 failed\n"));
1440 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1442 MAXIMUM_ALLOWED_ACCESS,
1443 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1446 if (!NT_STATUS_IS_OK(result)) {
1447 d_fprintf(stderr, _("Request open_domain failed\n"));
1451 init_lsa_String(&lsa_acct_name, argv[0]);
1453 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1459 if (!NT_STATUS_IS_OK(result)) {
1460 d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
1464 switch (name_types.ids[0])
1466 case SID_NAME_DOM_GRP:
1467 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1469 MAXIMUM_ALLOWED_ACCESS,
1472 if (!NT_STATUS_IS_OK(result)) {
1473 d_fprintf(stderr, _("Request open_group failed"));
1477 group_rid = group_rids.ids[0];
1479 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1483 if (!NT_STATUS_IS_OK(result)) {
1485 _("Unable to query group members of %s"),
1490 if (c->opt_verbose) {
1492 _("Domain Group %s (rid: %d) has %d members\n"),
1493 argv[0],group_rid, rids->count);
1496 /* Check if group is anyone's primary group */
1497 for (i = 0; i < rids->count; i++)
1499 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1501 MAXIMUM_ALLOWED_ACCESS,
1505 if (!NT_STATUS_IS_OK(result)) {
1507 _("Unable to open group member %d\n"),
1512 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1517 if (!NT_STATUS_IS_OK(result)) {
1519 _("Unable to lookup userinfo for group "
1525 if (info->info21.primary_gid == group_rid) {
1526 if (c->opt_verbose) {
1527 d_printf(_("Group is primary group "
1529 info->info21.account_name.string);
1531 group_is_primary = true;
1534 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1537 if (group_is_primary) {
1538 d_fprintf(stderr, _("Unable to delete group because "
1539 "some of it's members have it as primary "
1541 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1545 /* remove all group members */
1546 for (i = 0; i < rids->count; i++)
1549 d_printf(_("Remove group member %d..."),
1551 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1555 if (NT_STATUS_IS_OK(result)) {
1557 d_printf(_("ok\n"));
1560 d_printf(_("failed\n"));
1565 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1569 /* removing a local group is easier... */
1570 case SID_NAME_ALIAS:
1571 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1573 MAXIMUM_ALLOWED_ACCESS,
1577 if (!NT_STATUS_IS_OK(result)) {
1578 d_fprintf(stderr, _("Request open_alias failed\n"));
1582 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1586 d_fprintf(stderr, _("%s is of type %s. This command is only "
1587 "for deleting local or global groups\n"),
1588 argv[0],sid_type_lookup(name_types.ids[0]));
1589 result = NT_STATUS_UNSUCCESSFUL;
1593 if (NT_STATUS_IS_OK(result)) {
1595 d_printf(_("Deleted %s '%s'\n"),
1596 sid_type_lookup(name_types.ids[0]), argv[0]);
1598 d_fprintf(stderr, _("Deleting of %s failed: %s\n"), argv[0],
1599 get_friendly_nt_error_msg(result));
1607 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1609 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1610 rpc_group_delete_internals, argc,argv);
1613 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1615 NET_API_STATUS status;
1616 struct GROUP_INFO_1 info1;
1617 uint32_t parm_error = 0;
1619 if (argc != 1 || c->display_usage) {
1620 rpc_group_usage(c, argc, argv);
1626 info1.grpi1_name = argv[0];
1627 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1628 info1.grpi1_comment = c->opt_comment;
1631 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1635 _("Failed to add group '%s' with error: %s.\n"),
1636 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1640 d_printf(_("Added group '%s'.\n"), argv[0]);
1646 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1648 NET_API_STATUS status;
1649 struct LOCALGROUP_INFO_1 info1;
1650 uint32_t parm_error = 0;
1652 if (argc != 1 || c->display_usage) {
1653 rpc_group_usage(c, argc, argv);
1659 info1.lgrpi1_name = argv[0];
1660 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1661 info1.lgrpi1_comment = c->opt_comment;
1664 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1668 _("Failed to add alias '%s' with error: %s.\n"),
1669 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1673 d_printf(_("Added alias '%s'.\n"), argv[0]);
1679 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1681 if (c->opt_localgroup)
1682 return rpc_alias_add_internals(c, argc, argv);
1684 return rpc_group_add_internals(c, argc, argv);
1687 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1688 TALLOC_CTX *mem_ctx,
1691 enum lsa_SidType *type)
1693 DOM_SID *sids = NULL;
1694 enum lsa_SidType *types = NULL;
1695 struct rpc_pipe_client *pipe_hnd = NULL;
1696 struct policy_handle lsa_pol;
1697 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1699 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1701 if (!NT_STATUS_IS_OK(result)) {
1705 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1706 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1708 if (!NT_STATUS_IS_OK(result)) {
1712 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1713 &name, NULL, 1, &sids, &types);
1715 if (NT_STATUS_IS_OK(result)) {
1716 sid_copy(sid, &sids[0]);
1720 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1724 TALLOC_FREE(pipe_hnd);
1727 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1729 /* Try as S-1-5-whatever */
1733 if (string_to_sid(&tmp_sid, name)) {
1734 sid_copy(sid, &tmp_sid);
1735 *type = SID_NAME_UNKNOWN;
1736 result = NT_STATUS_OK;
1743 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1744 TALLOC_CTX *mem_ctx,
1745 const DOM_SID *group_sid,
1748 struct policy_handle connect_pol, domain_pol;
1751 struct policy_handle group_pol;
1753 struct samr_Ids rids, rid_types;
1754 struct lsa_String lsa_acct_name;
1758 sid_copy(&sid, group_sid);
1760 if (!sid_split_rid(&sid, &group_rid)) {
1761 return NT_STATUS_UNSUCCESSFUL;
1764 /* Get sam policy handle */
1765 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1767 MAXIMUM_ALLOWED_ACCESS,
1769 if (!NT_STATUS_IS_OK(result)) {
1773 /* Get domain policy handle */
1774 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1776 MAXIMUM_ALLOWED_ACCESS,
1779 if (!NT_STATUS_IS_OK(result)) {
1783 init_lsa_String(&lsa_acct_name, member);
1785 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1792 if (!NT_STATUS_IS_OK(result)) {
1793 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1798 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1800 MAXIMUM_ALLOWED_ACCESS,
1804 if (!NT_STATUS_IS_OK(result)) {
1808 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1811 0x0005); /* unknown flags */
1814 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1818 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1819 TALLOC_CTX *mem_ctx,
1820 const DOM_SID *alias_sid,
1823 struct policy_handle connect_pol, domain_pol;
1826 struct policy_handle alias_pol;
1829 enum lsa_SidType member_type;
1833 sid_copy(&sid, alias_sid);
1835 if (!sid_split_rid(&sid, &alias_rid)) {
1836 return NT_STATUS_UNSUCCESSFUL;
1839 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1840 member, &member_sid, &member_type);
1842 if (!NT_STATUS_IS_OK(result)) {
1843 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
1848 /* Get sam policy handle */
1849 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1851 MAXIMUM_ALLOWED_ACCESS,
1853 if (!NT_STATUS_IS_OK(result)) {
1857 /* Get domain policy handle */
1858 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1860 MAXIMUM_ALLOWED_ACCESS,
1863 if (!NT_STATUS_IS_OK(result)) {
1867 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1869 MAXIMUM_ALLOWED_ACCESS,
1873 if (!NT_STATUS_IS_OK(result)) {
1877 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1881 if (!NT_STATUS_IS_OK(result)) {
1886 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1890 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1891 const DOM_SID *domain_sid,
1892 const char *domain_name,
1893 struct cli_state *cli,
1894 struct rpc_pipe_client *pipe_hnd,
1895 TALLOC_CTX *mem_ctx,
1900 enum lsa_SidType group_type;
1902 if (argc != 2 || c->display_usage) {
1903 d_printf(_("Usage:\n"
1904 "net rpc group addmem <group> <member>\n"
1905 " Add a member to a group\n"
1906 " group\tGroup to add member to\n"
1907 " member\tMember to add to group\n"));
1908 return NT_STATUS_UNSUCCESSFUL;
1911 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1912 &group_sid, &group_type))) {
1913 d_fprintf(stderr, _("Could not lookup group name %s\n"),
1915 return NT_STATUS_UNSUCCESSFUL;
1918 if (group_type == SID_NAME_DOM_GRP) {
1919 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
1920 &group_sid, argv[1]);
1922 if (!NT_STATUS_IS_OK(result)) {
1923 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
1924 argv[1], argv[0], nt_errstr(result));
1929 if (group_type == SID_NAME_ALIAS) {
1930 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
1931 &group_sid, argv[1]);
1933 if (!NT_STATUS_IS_OK(result)) {
1934 d_fprintf(stderr, _("Could not add %s to %s: %s\n"),
1935 argv[1], argv[0], nt_errstr(result));
1940 d_fprintf(stderr, _("Can only add members to global or local groups "
1941 "which %s is not\n"), argv[0]);
1943 return NT_STATUS_UNSUCCESSFUL;
1946 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
1948 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1949 rpc_group_addmem_internals,
1953 static NTSTATUS rpc_del_groupmem(struct net_context *c,
1954 struct rpc_pipe_client *pipe_hnd,
1955 TALLOC_CTX *mem_ctx,
1956 const DOM_SID *group_sid,
1959 struct policy_handle connect_pol, domain_pol;
1962 struct policy_handle group_pol;
1964 struct samr_Ids rids, rid_types;
1965 struct lsa_String lsa_acct_name;
1969 sid_copy(&sid, group_sid);
1971 if (!sid_split_rid(&sid, &group_rid))
1972 return NT_STATUS_UNSUCCESSFUL;
1974 /* Get sam policy handle */
1975 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1977 MAXIMUM_ALLOWED_ACCESS,
1979 if (!NT_STATUS_IS_OK(result))
1982 /* Get domain policy handle */
1983 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1985 MAXIMUM_ALLOWED_ACCESS,
1988 if (!NT_STATUS_IS_OK(result))
1991 init_lsa_String(&lsa_acct_name, member);
1993 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1999 if (!NT_STATUS_IS_OK(result)) {
2000 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2005 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2007 MAXIMUM_ALLOWED_ACCESS,
2011 if (!NT_STATUS_IS_OK(result))
2014 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2019 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2023 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2024 TALLOC_CTX *mem_ctx,
2025 const DOM_SID *alias_sid,
2028 struct policy_handle connect_pol, domain_pol;
2031 struct policy_handle alias_pol;
2034 enum lsa_SidType member_type;
2038 sid_copy(&sid, alias_sid);
2040 if (!sid_split_rid(&sid, &alias_rid))
2041 return NT_STATUS_UNSUCCESSFUL;
2043 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2044 member, &member_sid, &member_type);
2046 if (!NT_STATUS_IS_OK(result)) {
2047 d_fprintf(stderr, _("Could not lookup up group member %s\n"),
2052 /* Get sam policy handle */
2053 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2055 MAXIMUM_ALLOWED_ACCESS,
2057 if (!NT_STATUS_IS_OK(result)) {
2061 /* Get domain policy handle */
2062 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2064 MAXIMUM_ALLOWED_ACCESS,
2067 if (!NT_STATUS_IS_OK(result)) {
2071 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2073 MAXIMUM_ALLOWED_ACCESS,
2077 if (!NT_STATUS_IS_OK(result))
2080 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2084 if (!NT_STATUS_IS_OK(result))
2088 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2092 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2093 const DOM_SID *domain_sid,
2094 const char *domain_name,
2095 struct cli_state *cli,
2096 struct rpc_pipe_client *pipe_hnd,
2097 TALLOC_CTX *mem_ctx,
2102 enum lsa_SidType group_type;
2104 if (argc != 2 || c->display_usage) {
2105 d_printf(_("Usage:\n"
2106 "net rpc group delmem <group> <member>\n"
2107 " Delete a member from a group\n"
2108 " group\tGroup to delete member from\n"
2109 " member\tMember to delete from group\n"));
2110 return NT_STATUS_UNSUCCESSFUL;
2113 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2114 &group_sid, &group_type))) {
2115 d_fprintf(stderr, _("Could not lookup group name %s\n"),
2117 return NT_STATUS_UNSUCCESSFUL;
2120 if (group_type == SID_NAME_DOM_GRP) {
2121 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2122 &group_sid, argv[1]);
2124 if (!NT_STATUS_IS_OK(result)) {
2125 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2126 argv[1], argv[0], nt_errstr(result));
2131 if (group_type == SID_NAME_ALIAS) {
2132 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2133 &group_sid, argv[1]);
2135 if (!NT_STATUS_IS_OK(result)) {
2136 d_fprintf(stderr, _("Could not del %s from %s: %s\n"),
2137 argv[1], argv[0], nt_errstr(result));
2142 d_fprintf(stderr, _("Can only delete members from global or local "
2143 "groups which %s is not\n"), argv[0]);
2145 return NT_STATUS_UNSUCCESSFUL;
2148 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2150 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2151 rpc_group_delmem_internals,
2156 * List groups on a remote RPC server.
2158 * All parameters are provided by the run_rpc_command function, except for
2159 * argc, argv which are passes through.
2161 * @param domain_sid The domain sid acquired from the remote server.
2162 * @param cli A cli_state connected to the server.
2163 * @param mem_ctx Talloc context, destroyed on completion of the function.
2164 * @param argc Standard main() style argc.
2165 * @param argv Standard main() style argv. Initial components are already
2168 * @return Normal NTSTATUS return.
2171 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2172 const DOM_SID *domain_sid,
2173 const char *domain_name,
2174 struct cli_state *cli,
2175 struct rpc_pipe_client *pipe_hnd,
2176 TALLOC_CTX *mem_ctx,
2180 struct policy_handle connect_pol, domain_pol;
2181 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2182 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2183 struct samr_SamArray *groups = NULL;
2184 bool global = false;
2186 bool builtin = false;
2188 if (c->display_usage) {
2189 d_printf(_("Usage:\n"
2190 "net rpc group list [global] [local] [builtin]\n"
2191 " List groups on RPC server\n"
2192 " global\tList global groups\n"
2193 " local\tList local groups\n"
2194 " builtin\tList builtin groups\n"
2195 " If none of global, local or builtin is "
2196 "specified, all three options are considered "
2198 return NT_STATUS_OK;
2207 for (i=0; i<argc; i++) {
2208 if (strequal(argv[i], "global"))
2211 if (strequal(argv[i], "local"))
2214 if (strequal(argv[i], "builtin"))
2218 /* Get sam policy handle */
2220 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2222 MAXIMUM_ALLOWED_ACCESS,
2224 if (!NT_STATUS_IS_OK(result)) {
2228 /* Get domain policy handle */
2230 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2232 MAXIMUM_ALLOWED_ACCESS,
2233 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2235 if (!NT_STATUS_IS_OK(result)) {
2239 /* Query domain groups */
2240 if (c->opt_long_list_entries)
2241 d_printf(_("\nGroup name Comment"
2242 "\n-----------------------------\n"));
2244 uint32_t max_size, total_size, returned_size;
2245 union samr_DispInfo info;
2249 get_query_dispinfo_params(
2250 loop_count, &max_entries, &max_size);
2252 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2261 num_entries = info.info3.count;
2262 start_idx += info.info3.count;
2264 if (!NT_STATUS_IS_OK(result) &&
2265 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2268 for (i = 0; i < num_entries; i++) {
2270 const char *group = NULL;
2271 const char *desc = NULL;
2273 group = info.info3.entries[i].account_name.string;
2274 desc = info.info3.entries[i].description.string;
2276 if (c->opt_long_list_entries)
2277 printf("%-21.21s %-50.50s\n",
2280 printf("%s\n", group);
2282 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2283 /* query domain aliases */
2288 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2294 if (!NT_STATUS_IS_OK(result) &&
2295 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2298 for (i = 0; i < num_entries; i++) {
2300 const char *description = NULL;
2302 if (c->opt_long_list_entries) {
2304 struct policy_handle alias_pol;
2305 union samr_AliasInfo *info = NULL;
2307 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2310 groups->entries[i].idx,
2312 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2316 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2318 description = info->description.string;
2322 if (description != NULL) {
2323 printf("%-21.21s %-50.50s\n",
2324 groups->entries[i].name.string,
2327 printf("%s\n", groups->entries[i].name.string);
2330 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2331 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2332 /* Get builtin policy handle */
2334 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2336 MAXIMUM_ALLOWED_ACCESS,
2337 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2339 if (!NT_STATUS_IS_OK(result)) {
2342 /* query builtin aliases */
2345 if (!builtin) break;
2347 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2353 if (!NT_STATUS_IS_OK(result) &&
2354 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2357 for (i = 0; i < num_entries; i++) {
2359 const char *description = NULL;
2361 if (c->opt_long_list_entries) {
2363 struct policy_handle alias_pol;
2364 union samr_AliasInfo *info = NULL;
2366 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2369 groups->entries[i].idx,
2371 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2375 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2377 description = info->description.string;
2381 if (description != NULL) {
2382 printf("%-21.21s %-50.50s\n",
2383 groups->entries[i].name.string,
2386 printf("%s\n", groups->entries[i].name.string);
2389 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2395 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2397 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2398 rpc_group_list_internals,
2402 static NTSTATUS rpc_list_group_members(struct net_context *c,
2403 struct rpc_pipe_client *pipe_hnd,
2404 TALLOC_CTX *mem_ctx,
2405 const char *domain_name,
2406 const DOM_SID *domain_sid,
2407 struct policy_handle *domain_pol,
2411 struct policy_handle group_pol;
2412 uint32 num_members, *group_rids;
2414 struct samr_RidTypeArray *rids = NULL;
2415 struct lsa_Strings names;
2416 struct samr_Ids types;
2419 sid_to_fstring(sid_str, domain_sid);
2421 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2423 MAXIMUM_ALLOWED_ACCESS,
2427 if (!NT_STATUS_IS_OK(result))
2430 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2434 if (!NT_STATUS_IS_OK(result))
2437 num_members = rids->count;
2438 group_rids = rids->rids;
2440 while (num_members > 0) {
2441 int this_time = 512;
2443 if (num_members < this_time)
2444 this_time = num_members;
2446 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2453 if (!NT_STATUS_IS_OK(result))
2456 /* We only have users as members, but make the output
2457 the same as the output of alias members */
2459 for (i = 0; i < this_time; i++) {
2461 if (c->opt_long_list_entries) {
2462 printf("%s-%d %s\\%s %d\n", sid_str,
2463 group_rids[i], domain_name,
2464 names.names[i].string,
2467 printf("%s\\%s\n", domain_name,
2468 names.names[i].string);
2472 num_members -= this_time;
2476 return NT_STATUS_OK;
2479 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2480 struct rpc_pipe_client *pipe_hnd,
2481 TALLOC_CTX *mem_ctx,
2482 struct policy_handle *domain_pol,
2486 struct rpc_pipe_client *lsa_pipe;
2487 struct policy_handle alias_pol, lsa_pol;
2489 DOM_SID *alias_sids;
2492 enum lsa_SidType *types;
2494 struct lsa_SidArray sid_array;
2496 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2498 MAXIMUM_ALLOWED_ACCESS,
2502 if (!NT_STATUS_IS_OK(result))
2505 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2509 if (!NT_STATUS_IS_OK(result)) {
2510 d_fprintf(stderr, _("Couldn't list alias members\n"));
2514 num_members = sid_array.num_sids;
2516 if (num_members == 0) {
2517 return NT_STATUS_OK;
2520 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2521 &ndr_table_lsarpc.syntax_id,
2523 if (!NT_STATUS_IS_OK(result)) {
2524 d_fprintf(stderr, _("Couldn't open LSA pipe. Error was %s\n"),
2525 nt_errstr(result) );
2529 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2530 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2532 if (!NT_STATUS_IS_OK(result)) {
2533 d_fprintf(stderr, _("Couldn't open LSA policy handle\n"));
2534 TALLOC_FREE(lsa_pipe);
2538 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2540 d_fprintf(stderr, _("Out of memory\n"));
2541 TALLOC_FREE(lsa_pipe);
2542 return NT_STATUS_NO_MEMORY;
2545 for (i=0; i<num_members; i++) {
2546 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2549 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2550 num_members, alias_sids,
2551 &domains, &names, &types);
2553 if (!NT_STATUS_IS_OK(result) &&
2554 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2555 d_fprintf(stderr, _("Couldn't lookup SIDs\n"));
2556 TALLOC_FREE(lsa_pipe);
2560 for (i = 0; i < num_members; i++) {
2562 sid_to_fstring(sid_str, &alias_sids[i]);
2564 if (c->opt_long_list_entries) {
2565 printf("%s %s\\%s %d\n", sid_str,
2566 domains[i] ? domains[i] : _("*unknown*"),
2567 names[i] ? names[i] : _("*unknown*"), types[i]);
2570 printf("%s\\%s\n", domains[i], names[i]);
2572 printf("%s\n", sid_str);
2576 TALLOC_FREE(lsa_pipe);
2577 return NT_STATUS_OK;
2580 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2581 const DOM_SID *domain_sid,
2582 const char *domain_name,
2583 struct cli_state *cli,
2584 struct rpc_pipe_client *pipe_hnd,
2585 TALLOC_CTX *mem_ctx,
2590 struct policy_handle connect_pol, domain_pol;
2591 struct samr_Ids rids, rid_types;
2592 struct lsa_String lsa_acct_name;
2594 /* Get sam policy handle */
2596 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2598 MAXIMUM_ALLOWED_ACCESS,
2601 if (!NT_STATUS_IS_OK(result))
2604 /* Get domain policy handle */
2606 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2608 MAXIMUM_ALLOWED_ACCESS,
2609 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2612 if (!NT_STATUS_IS_OK(result))
2615 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2617 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2624 if (!NT_STATUS_IS_OK(result)) {
2626 /* Ok, did not find it in the global sam, try with builtin */
2628 DOM_SID sid_Builtin;
2630 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2632 sid_copy(&sid_Builtin, &global_sid_Builtin);
2634 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2636 MAXIMUM_ALLOWED_ACCESS,
2640 if (!NT_STATUS_IS_OK(result)) {
2641 d_fprintf(stderr, _("Couldn't find group %s\n"),
2646 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2653 if (!NT_STATUS_IS_OK(result)) {
2654 d_fprintf(stderr, _("Couldn't find group %s\n"),
2660 if (rids.count != 1) {
2661 d_fprintf(stderr, _("Couldn't find group %s\n"),
2666 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2667 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2668 domain_sid, &domain_pol,
2672 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2673 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2677 return NT_STATUS_NO_SUCH_GROUP;
2680 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2682 if (argc != 1 || c->display_usage) {
2683 return rpc_group_usage(c, argc, argv);
2686 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2687 rpc_group_members_internals,
2691 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2693 NET_API_STATUS status;
2694 struct GROUP_INFO_0 g0;
2698 d_printf(_("Usage: 'net rpc group rename group newname'\n"));
2702 g0.grpi0_name = argv[1];
2704 status = NetGroupSetInfo(c->opt_host,
2711 d_fprintf(stderr, _("Renaming group %s failed with: %s\n"),
2712 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2720 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2722 if (argc != 2 || c->display_usage) {
2723 return rpc_group_usage(c, argc, argv);
2726 return rpc_group_rename_internals(c, argc, argv);
2730 * 'net rpc group' entrypoint.
2731 * @param argc Standard main() style argc.
2732 * @param argv Standard main() style argv. Initial components are already
2736 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2738 NET_API_STATUS status;
2740 struct functable func[] = {
2745 N_("Create specified group"),
2746 N_("net rpc group add\n"
2747 " Create specified group")
2753 N_("Delete specified group"),
2754 N_("net rpc group delete\n"
2755 " Delete specified group")
2761 N_("Add member to group"),
2762 N_("net rpc group addmem\n"
2763 " Add member to group")
2769 N_("Remove member from group"),
2770 N_("net rpc group delmem\n"
2771 " Remove member from group")
2778 N_("net rpc group list\n"
2785 N_("List group members"),
2786 N_("net rpc group members\n"
2787 " List group members")
2794 N_("net rpc group rename\n"
2797 {NULL, NULL, 0, NULL, NULL}
2800 status = libnetapi_init(&c->netapi_ctx);
2804 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2805 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2806 if (c->opt_kerberos) {
2807 libnetapi_set_use_kerberos(c->netapi_ctx);
2811 if (c->display_usage) {
2812 d_printf(_("Usage:\n"));
2813 d_printf(_("net rpc group\n"
2814 " Alias for net rpc group list global "
2815 "local builtin\n"));
2816 net_display_usage_from_functable(func);
2820 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2821 rpc_group_list_internals,
2825 return net_run_function(c, argc, argv, "net rpc group", func);
2828 /****************************************************************************/
2830 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2832 return net_share_usage(c, argc, argv);
2836 * Add a share on a remote RPC server.
2838 * @param argc Standard main() style argc.
2839 * @param argv Standard main() style argv. Initial components are already
2842 * @return A shell status integer (0 for success).
2845 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2847 NET_API_STATUS status;
2850 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2851 uint32 num_users=0, perms=0;
2852 char *password=NULL; /* don't allow a share password */
2853 struct SHARE_INFO_2 i2;
2854 uint32_t parm_error = 0;
2856 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2857 return rpc_share_usage(c, argc, argv);
2860 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2864 path = strchr(sharename, '=');
2871 i2.shi2_netname = sharename;
2872 i2.shi2_type = type;
2873 i2.shi2_remark = c->opt_comment;
2874 i2.shi2_permissions = perms;
2875 i2.shi2_max_uses = c->opt_maxusers;
2876 i2.shi2_current_uses = num_users;
2877 i2.shi2_path = path;
2878 i2.shi2_passwd = password;
2880 status = NetShareAdd(c->opt_host,
2885 printf(_("NetShareAdd failed with: %s\n"),
2886 libnetapi_get_error_string(c->netapi_ctx, status));
2893 * Delete a share on a remote RPC server.
2895 * @param domain_sid The domain sid acquired from the remote server.
2896 * @param argc Standard main() style argc.
2897 * @param argv Standard main() style argv. Initial components are already
2900 * @return A shell status integer (0 for success).
2902 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
2904 if (argc < 1 || c->display_usage) {
2905 return rpc_share_usage(c, argc, argv);
2908 return NetShareDel(c->opt_host, argv[0], 0);
2912 * Formatted print of share info
2914 * @param r pointer to SHARE_INFO_1 to format
2917 static void display_share_info_1(struct net_context *c,
2918 struct SHARE_INFO_1 *r)
2920 if (c->opt_long_list_entries) {
2921 d_printf("%-12s %-8.8s %-50s\n",
2923 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
2926 d_printf("%s\n", r->shi1_netname);
2930 static WERROR get_share_info(struct net_context *c,
2931 struct rpc_pipe_client *pipe_hnd,
2932 TALLOC_CTX *mem_ctx,
2936 struct srvsvc_NetShareInfoCtr *info_ctr)
2940 union srvsvc_NetShareInfo info;
2942 /* no specific share requested, enumerate all */
2945 uint32_t preferred_len = 0xffffffff;
2946 uint32_t total_entries = 0;
2947 uint32_t resume_handle = 0;
2949 info_ctr->level = level;
2951 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
2961 /* request just one share */
2962 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
2969 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
2974 ZERO_STRUCTP(info_ctr);
2976 info_ctr->level = level;
2981 struct srvsvc_NetShareCtr1 *ctr1;
2983 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
2984 W_ERROR_HAVE_NO_MEMORY(ctr1);
2987 ctr1->array = info.info1;
2989 info_ctr->ctr.ctr1 = ctr1;
2993 struct srvsvc_NetShareCtr2 *ctr2;
2995 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
2996 W_ERROR_HAVE_NO_MEMORY(ctr2);
2999 ctr2->array = info.info2;
3001 info_ctr->ctr.ctr2 = ctr2;
3005 struct srvsvc_NetShareCtr502 *ctr502;
3007 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3008 W_ERROR_HAVE_NO_MEMORY(ctr502);
3011 ctr502->array = info.info502;
3013 info_ctr->ctr.ctr502 = ctr502;
3021 * 'net rpc share list' entrypoint.
3022 * @param argc Standard main() style argc.
3023 * @param argv Standard main() style argv. Initial components are already
3026 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3028 NET_API_STATUS status;
3029 struct SHARE_INFO_1 *i1 = NULL;
3030 uint32_t entries_read = 0;
3031 uint32_t total_entries = 0;
3032 uint32_t resume_handle = 0;
3033 uint32_t i, level = 1;
3035 if (c->display_usage) {
3036 d_printf(_("Usage\n"
3037 "net rpc share list\n"
3038 " List shares on remote server\n"));
3042 status = NetShareEnum(c->opt_host,
3044 (uint8_t **)(void *)&i1,
3053 /* Display results */
3055 if (c->opt_long_list_entries) {
3057 "\nEnumerating shared resources (exports) on remote server:\n\n"
3058 "\nShare name Type Description\n"
3059 "---------- ---- -----------\n"));
3061 for (i = 0; i < entries_read; i++)
3062 display_share_info_1(c, &i1[i]);
3067 static bool check_share_availability(struct cli_state *cli, const char *netname)
3069 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
3070 d_printf(_("skipping [%s]: not a file share.\n"), netname);
3080 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3081 const char *netname, uint32 type)
3083 /* only support disk shares */
3084 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3085 printf(_("share [%s] is not a diskshare (type: %x)\n"), netname,
3090 /* skip builtin shares */
3091 /* FIXME: should print$ be added too ? */
3092 if (strequal(netname,"IPC$") || strequal(netname,"