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"
26 static int net_mode_share;
27 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
32 * @brief RPC based subcommands for the 'net' utility.
34 * This file should contain much of the functionality that used to
35 * be found in rpcclient, execpt that the commands should change
36 * less often, and the fucntionality should be sane (the user is not
37 * expected to know a rid/sid before they conduct an operation etc.)
39 * @todo Perhaps eventually these should be split out into a number
40 * of files, as this could get quite big.
45 * Many of the RPC functions need the domain sid. This function gets
46 * it at the start of every run
48 * @param cli A cli_state already connected to the remote machine
50 * @return The Domain SID of the remote machine.
53 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
55 const char **domain_name)
57 struct rpc_pipe_client *lsa_pipe;
59 NTSTATUS result = NT_STATUS_OK;
60 union lsa_PolicyInformation *info = NULL;
62 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
64 if (!NT_STATUS_IS_OK(result)) {
65 d_fprintf(stderr, "Could not initialise lsa pipe\n");
69 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
70 SEC_RIGHTS_MAXIMUM_ALLOWED,
72 if (!NT_STATUS_IS_OK(result)) {
73 d_fprintf(stderr, "open_policy failed: %s\n",
78 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
80 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
82 if (!NT_STATUS_IS_OK(result)) {
83 d_fprintf(stderr, "lsaquery failed: %s\n",
88 *domain_name = info->account_domain.name.string;
89 *domain_sid = info->account_domain.sid;
91 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
92 TALLOC_FREE(lsa_pipe);
98 * Run a single RPC command, from start to finish.
100 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
101 * @param conn_flag a NET_FLAG_ combination. Passed to
102 * net_make_ipc_connection.
103 * @param argc Standard main() style argc.
104 * @param argv Standard main() style argv. Initial components are already
106 * @return A shell status integer (0 for success).
109 int run_rpc_command(struct net_context *c,
110 struct cli_state *cli_arg,
111 const struct ndr_syntax_id *interface,
117 struct cli_state *cli = NULL;
118 struct rpc_pipe_client *pipe_hnd = NULL;
122 const char *domain_name;
124 /* make use of cli_state handed over as an argument, if possible */
126 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
127 if (!NT_STATUS_IS_OK(nt_status)) {
128 DEBUG(1, ("failed to make ipc connection: %s\n",
129 nt_errstr(nt_status)));
142 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
143 DEBUG(0, ("talloc_init() failed\n"));
148 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
150 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(
162 PIPE_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) ));
171 if (conn_flags & NET_FLAGS_SEAL) {
172 nt_status = cli_rpc_pipe_open_ntlmssp(
174 PIPE_AUTH_LEVEL_PRIVACY,
175 lp_workgroup(), c->opt_user_name,
176 c->opt_password, &pipe_hnd);
178 nt_status = cli_rpc_pipe_open_noauth(
182 if (!NT_STATUS_IS_OK(nt_status)) {
183 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
184 cli_get_pipe_name_from_iface(
185 debug_ctx(), cli, interface),
186 nt_errstr(nt_status) ));
193 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
195 if (!NT_STATUS_IS_OK(nt_status)) {
196 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);
207 /* close the connection only if it was opened here */
212 talloc_destroy(mem_ctx);
213 return (!NT_STATUS_IS_OK(nt_status));
217 * Force a change of the trust acccount password.
219 * All parameters are provided by the run_rpc_command function, except for
220 * argc, argv which are passed through.
222 * @param domain_sid The domain sid acquired from the remote server.
223 * @param cli A cli_state connected to the server.
224 * @param mem_ctx Talloc context, destroyed on completion of the function.
225 * @param argc Standard main() style argc.
226 * @param argv Standard main() style argv. Initial components are already
229 * @return Normal NTSTATUS return.
232 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
233 const DOM_SID *domain_sid,
234 const char *domain_name,
235 struct cli_state *cli,
236 struct rpc_pipe_client *pipe_hnd,
242 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
246 * Force a change of the trust acccount password.
248 * @param argc Standard main() style argc.
249 * @param argv Standard main() style argv. Initial components are already
252 * @return A shell status integer (0 for success).
255 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
257 if (c->display_usage) {
259 "net rpc changetrustpw\n"
260 " Change the machine trust password\n");
264 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
265 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
266 rpc_changetrustpw_internals,
271 * Join a domain, the old way.
273 * This uses 'machinename' as the inital password, and changes it.
275 * The password should be created with 'server manager' or equiv first.
277 * All parameters are provided by the run_rpc_command function, except for
278 * argc, argv which are passed through.
280 * @param domain_sid The domain sid acquired from the remote server.
281 * @param cli A cli_state connected to the server.
282 * @param mem_ctx Talloc context, destroyed on completion of the function.
283 * @param argc Standard main() style argc.
284 * @param argv Standard main() style argv. Initial components are already
287 * @return Normal NTSTATUS return.
290 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
291 const DOM_SID *domain_sid,
292 const char *domain_name,
293 struct cli_state *cli,
294 struct rpc_pipe_client *pipe_hnd,
300 fstring trust_passwd;
301 unsigned char orig_trust_passwd_hash[16];
303 uint32 sec_channel_type;
305 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
307 if (!NT_STATUS_IS_OK(result)) {
308 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
311 nt_errstr(result) ));
316 check what type of join - if the user want's to join as
317 a BDC, the server must agree that we are a BDC.
320 sec_channel_type = get_sec_channel_type(argv[0]);
322 sec_channel_type = get_sec_channel_type(NULL);
325 fstrcpy(trust_passwd, global_myname());
326 strlower_m(trust_passwd);
329 * Machine names can be 15 characters, but the max length on
330 * a password is 14. --jerry
333 trust_passwd[14] = '\0';
335 E_md4hash(trust_passwd, orig_trust_passwd_hash);
337 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
338 orig_trust_passwd_hash,
341 if (NT_STATUS_IS_OK(result))
342 printf("Joined domain %s.\n", c->opt_target_workgroup);
345 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
346 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
347 result = NT_STATUS_UNSUCCESSFUL;
354 * Join a domain, the old way.
356 * @param argc Standard main() style argc.
357 * @param argv Standard main() style argv. Initial components are already
360 * @return A shell status integer (0 for success).
363 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
365 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
366 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
367 rpc_oldjoin_internals,
372 * Join a domain, the old way. This function exists to allow
373 * the message to be displayed when oldjoin was explicitly
374 * requested, but not when it was implied by "net rpc join".
376 * @param argc Standard main() style argc.
377 * @param argv Standard main() style argv. Initial components are already
380 * @return A shell status integer (0 for success).
383 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
387 if (c->display_usage) {
390 " Join a domain the old way\n");
394 rc = net_rpc_perform_oldjoin(c, argc, argv);
397 d_fprintf(stderr, "Failed to join domain\n");
404 * 'net rpc join' entrypoint.
405 * @param argc Standard main() style argc.
406 * @param argv Standard main() style argv. Initial components are already
409 * Main 'net_rpc_join()' (where the admin username/password is used) is
411 * Try to just change the password, but if that doesn't work, use/prompt
412 * for a username/password.
415 int net_rpc_join(struct net_context *c, int argc, const char **argv)
417 if (c->display_usage) {
419 "net rpc join -U <username>[%%password] <type>\n"
421 " username\tName of the admin user"
422 " password\tPassword of the admin user, will "
423 "prompt if not specified\n"
424 " type\tCan be one of the following:\n"
425 "\t\tMEMBER\tJoin as member server (default)\n"
426 "\t\tBDC\tJoin as BDC\n"
427 "\t\tPDC\tJoin as PDC\n");
431 if (lp_server_role() == ROLE_STANDALONE) {
432 d_printf("cannot join as standalone machine\n");
436 if (strlen(global_myname()) > 15) {
437 d_printf("Our netbios name can be at most 15 chars long, "
438 "\"%s\" is %u chars long\n",
439 global_myname(), (unsigned int)strlen(global_myname()));
443 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
446 return net_rpc_join_newstyle(c, argc, argv);
450 * display info about a rpc domain
452 * All parameters are provided by the run_rpc_command function, except for
453 * argc, argv which are passed through.
455 * @param domain_sid The domain sid acquired from the remote server
456 * @param cli A cli_state connected to the server.
457 * @param mem_ctx Talloc context, destroyed on completion of the function.
458 * @param argc Standard main() style argc.
459 * @param argv Standard main() style argv. Initial components are already
462 * @return Normal NTSTATUS return.
465 NTSTATUS rpc_info_internals(struct net_context *c,
466 const DOM_SID *domain_sid,
467 const char *domain_name,
468 struct cli_state *cli,
469 struct rpc_pipe_client *pipe_hnd,
474 POLICY_HND connect_pol, domain_pol;
475 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
476 union samr_DomainInfo *info = NULL;
479 sid_to_fstring(sid_str, domain_sid);
481 /* Get sam policy handle */
482 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
484 MAXIMUM_ALLOWED_ACCESS,
486 if (!NT_STATUS_IS_OK(result)) {
487 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
491 /* Get domain policy handle */
492 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
494 MAXIMUM_ALLOWED_ACCESS,
495 CONST_DISCARD(struct dom_sid2 *, domain_sid),
497 if (!NT_STATUS_IS_OK(result)) {
498 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
502 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
506 if (NT_STATUS_IS_OK(result)) {
507 d_printf("Domain Name: %s\n", info->info2.domain_name.string);
508 d_printf("Domain SID: %s\n", sid_str);
509 d_printf("Sequence number: %llu\n",
510 (unsigned long long)info->info2.sequence_num);
511 d_printf("Num users: %u\n", info->info2.num_users);
512 d_printf("Num domain groups: %u\n", info->info2.num_groups);
513 d_printf("Num local groups: %u\n", info->info2.num_aliases);
521 * 'net rpc info' entrypoint.
522 * @param argc Standard main() style argc.
523 * @param argv Standard main() style argv. Initial components are already
527 int net_rpc_info(struct net_context *c, int argc, const char **argv)
529 if (c->display_usage) {
532 " Display information about the domain\n");
536 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
537 NET_FLAGS_PDC, rpc_info_internals,
542 * Fetch domain SID into the local secrets.tdb.
544 * All parameters are provided by the run_rpc_command function, except for
545 * argc, argv which are passed through.
547 * @param domain_sid The domain sid acquired from the remote server.
548 * @param cli A cli_state connected to the server.
549 * @param mem_ctx Talloc context, destroyed on completion of the function.
550 * @param argc Standard main() style argc.
551 * @param argv Standard main() style argv. Initial components are already
554 * @return Normal NTSTATUS return.
557 static NTSTATUS rpc_getsid_internals(struct net_context *c,
558 const DOM_SID *domain_sid,
559 const char *domain_name,
560 struct cli_state *cli,
561 struct rpc_pipe_client *pipe_hnd,
568 sid_to_fstring(sid_str, domain_sid);
569 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
570 sid_str, domain_name);
572 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
573 DEBUG(0,("Can't store domain SID\n"));
574 return NT_STATUS_UNSUCCESSFUL;
581 * 'net rpc getsid' entrypoint.
582 * @param argc Standard main() style argc.
583 * @param argv Standard main() style argv. Initial components are already
587 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
589 if (c->display_usage) {
592 " Fetch domain SID into local secrets.tdb\n");
596 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
597 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
598 rpc_getsid_internals,
602 /****************************************************************************/
605 * Basic usage function for 'net rpc user'.
606 * @param argc Standard main() style argc.
607 * @param argv Standard main() style argv. Initial components are already
611 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
613 return net_user_usage(c, argc, argv);
617 * Add a new user to a remote RPC server.
619 * @param argc Standard main() style argc.
620 * @param argv Standard main() style argv. Initial components are already
623 * @return A shell status integer (0 for success).
626 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
628 NET_API_STATUS status;
629 struct USER_INFO_1 info1;
630 uint32_t parm_error = 0;
632 if (argc < 1 || c->display_usage) {
633 rpc_user_usage(c, argc, argv);
639 info1.usri1_name = argv[0];
641 info1.usri1_password = argv[1];
644 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
647 d_fprintf(stderr, "Failed to add user '%s' with: %s.\n",
648 argv[0], libnetapi_get_error_string(c->netapi_ctx,
652 d_printf("Added user '%s'.\n", argv[0]);
659 * Rename a user on a remote RPC server.
661 * @param argc Standard main() style argc.
662 * @param argv Standard main() style argv. Initial components are already
665 * @return A shell status integer (0 for success).
668 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
670 NET_API_STATUS status;
671 struct USER_INFO_0 u0;
672 uint32_t parm_err = 0;
674 if (argc != 2 || c->display_usage) {
675 rpc_user_usage(c, argc, argv);
679 u0.usri0_name = argv[1];
681 status = NetUserSetInfo(c->opt_host, argv[0],
682 0, (uint8_t *)&u0, &parm_err);
684 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n",
686 libnetapi_get_error_string(c->netapi_ctx, status));
688 d_printf("Renamed user from %s to %s\n", argv[0], argv[1]);
695 * Delete a user from a remote RPC server.
697 * @param argc Standard main() style argc.
698 * @param argv Standard main() style argv. Initial components are already
701 * @return A shell status integer (0 for success).
704 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
706 NET_API_STATUS status;
708 if (argc < 1 || c->display_usage) {
709 rpc_user_usage(c, argc, argv);
713 status = NetUserDel(c->opt_host, argv[0]);
716 d_fprintf(stderr, "Failed to delete user '%s' with: %s.\n",
718 libnetapi_get_error_string(c->netapi_ctx, status));
721 d_printf("Deleted user '%s'.\n", argv[0]);
728 * Set a user's password on a remote RPC server.
730 * @param argc Standard main() style argc.
731 * @param argv Standard main() style argv. Initial components are already
734 * @return A shell status integer (0 for success).
737 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
739 NET_API_STATUS status;
741 struct USER_INFO_1003 u1003;
742 uint32_t parm_err = 0;
744 if (argc < 1 || c->display_usage) {
745 rpc_user_usage(c, argc, argv);
750 u1003.usri1003_password = argv[1];
752 asprintf(&prompt, "Enter new password for %s:", argv[0]);
753 u1003.usri1003_password = getpass(prompt);
757 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
759 /* Display results */
761 d_fprintf(stderr, "Failed to set password for '%s' with: %s.\n",
762 argv[0], libnetapi_get_error_string(c->netapi_ctx,
771 * List a user's groups from a remote RPC server.
773 * @param argc Standard main() style argc.
774 * @param argv Standard main() style argv. Initial components are already
777 * @return A shell status integer (0 for success)
780 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
783 NET_API_STATUS status;
784 struct GROUP_USERS_INFO_0 *u0 = NULL;
785 uint32_t entries_read = 0;
786 uint32_t total_entries = 0;
790 if (argc < 1 || c->display_usage) {
791 rpc_user_usage(c, argc, argv);
795 status = NetUserGetGroups(c->opt_host,
803 d_fprintf(stderr, "Failed to get groups for '%s' with: %s.\n",
804 argv[0], libnetapi_get_error_string(c->netapi_ctx,
809 for (i=0; i < entries_read; i++) {
810 printf("%s\n", u0->grui0_name);
818 * List users on a remote RPC server.
820 * All parameters are provided by the run_rpc_command function, except for
821 * argc, argv which are passed through.
823 * @param domain_sid The domain sid acquired from the remote server.
824 * @param cli A cli_state connected to the server.
825 * @param mem_ctx Talloc context, destroyed on completion of the function.
826 * @param argc Standard main() style argc.
827 * @param argv Standard main() style argv. Initial components are already
830 * @return Normal NTSTATUS return.
833 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
835 NET_API_STATUS status;
836 uint32_t start_idx=0, num_entries, i, loop_count = 0;
837 struct NET_DISPLAY_USER *info = NULL;
840 /* Query domain users */
841 if (c->opt_long_list_entries)
842 d_printf("\nUser name Comment"
843 "\n-----------------------------\n");
845 uint32_t max_entries, max_size;
847 get_query_dispinfo_params(
848 loop_count, &max_entries, &max_size);
850 status = NetQueryDisplayInformation(c->opt_host,
857 if (status != 0 && status != ERROR_MORE_DATA) {
861 info = (struct NET_DISPLAY_USER *)buffer;
863 for (i = 0; i < num_entries; i++) {
865 if (c->opt_long_list_entries)
866 printf("%-21.21s %s\n", info->usri1_name,
867 info->usri1_comment);
869 printf("%s\n", info->usri1_name);
873 NetApiBufferFree(buffer);
876 start_idx += num_entries;
878 } while (status == ERROR_MORE_DATA);
884 * 'net rpc user' entrypoint.
885 * @param argc Standard main() style argc.
886 * @param argv Standard main() style argv. Initial components are already
890 int net_rpc_user(struct net_context *c, int argc, const char **argv)
892 NET_API_STATUS status;
894 struct functable func[] = {
899 "Add specified user",
901 " Add specified user"
907 "List domain groups of user",
908 "net rpc user info\n"
909 " Lis domain groups of user"
915 "Remove specified user",
916 "net rpc user delete\n"
917 " Remove specified user"
923 "Change user password",
924 "net rpc user password\n"
925 " Change user password"
931 "Rename specified user",
932 "net rpc user rename\n"
933 " Rename specified user"
935 {NULL, NULL, 0, NULL, NULL}
938 status = libnetapi_init(&c->netapi_ctx);
942 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
943 libnetapi_set_password(c->netapi_ctx, c->opt_password);
944 if (c->opt_kerberos) {
945 libnetapi_set_use_kerberos(c->netapi_ctx);
949 if (c->display_usage) {
950 d_printf("Usage:\n");
951 d_printf("net rpc user\n"
952 " List all users\n");
953 net_display_usage_from_functable(func);
957 return rpc_user_list(c, argc, argv);
960 return net_run_function(c, argc, argv, "net rpc user", func);
963 static NTSTATUS rpc_sh_user_list(struct net_context *c,
965 struct rpc_sh_ctx *ctx,
966 struct rpc_pipe_client *pipe_hnd,
967 int argc, const char **argv)
969 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
972 static NTSTATUS rpc_sh_user_info(struct net_context *c,
974 struct rpc_sh_ctx *ctx,
975 struct rpc_pipe_client *pipe_hnd,
976 int argc, const char **argv)
978 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
981 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
983 struct rpc_sh_ctx *ctx,
984 struct rpc_pipe_client *pipe_hnd,
985 int argc, const char **argv,
987 struct net_context *c,
989 struct rpc_sh_ctx *ctx,
990 struct rpc_pipe_client *pipe_hnd,
991 POLICY_HND *user_hnd,
992 int argc, const char **argv))
994 POLICY_HND connect_pol, domain_pol, user_pol;
995 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
998 enum lsa_SidType type;
1001 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1002 return NT_STATUS_INVALID_PARAMETER;
1005 ZERO_STRUCT(connect_pol);
1006 ZERO_STRUCT(domain_pol);
1007 ZERO_STRUCT(user_pol);
1009 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1010 argv[0], NULL, NULL, &sid, &type);
1011 if (!NT_STATUS_IS_OK(result)) {
1012 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1017 if (type != SID_NAME_USER) {
1018 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1019 sid_type_lookup(type));
1020 result = NT_STATUS_NO_SUCH_USER;
1024 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1025 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1026 result = NT_STATUS_NO_SUCH_USER;
1030 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1032 MAXIMUM_ALLOWED_ACCESS,
1034 if (!NT_STATUS_IS_OK(result)) {
1038 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1040 MAXIMUM_ALLOWED_ACCESS,
1043 if (!NT_STATUS_IS_OK(result)) {
1047 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1049 MAXIMUM_ALLOWED_ACCESS,
1052 if (!NT_STATUS_IS_OK(result)) {
1056 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1059 if (is_valid_policy_hnd(&user_pol)) {
1060 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1062 if (is_valid_policy_hnd(&domain_pol)) {
1063 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1065 if (is_valid_policy_hnd(&connect_pol)) {
1066 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1071 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1072 TALLOC_CTX *mem_ctx,
1073 struct rpc_sh_ctx *ctx,
1074 struct rpc_pipe_client *pipe_hnd,
1075 POLICY_HND *user_hnd,
1076 int argc, const char **argv)
1079 union samr_UserInfo *info = NULL;
1082 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1083 return NT_STATUS_INVALID_PARAMETER;
1086 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1090 if (!NT_STATUS_IS_OK(result)) {
1094 d_printf("user rid: %d, group rid: %d\n",
1096 info->info21.primary_gid);
1101 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1102 TALLOC_CTX *mem_ctx,
1103 struct rpc_sh_ctx *ctx,
1104 struct rpc_pipe_client *pipe_hnd,
1105 int argc, const char **argv)
1107 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1108 rpc_sh_user_show_internals);
1111 #define FETCHSTR(name, rec) \
1112 do { if (strequal(ctx->thiscmd, name)) { \
1113 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1116 #define SETSTR(name, rec, flag) \
1117 do { if (strequal(ctx->thiscmd, name)) { \
1118 init_lsa_String(&(info->info21.rec), argv[0]); \
1119 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1122 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1123 TALLOC_CTX *mem_ctx,
1124 struct rpc_sh_ctx *ctx,
1125 struct rpc_pipe_client *pipe_hnd,
1126 POLICY_HND *user_hnd,
1127 int argc, const char **argv)
1130 const char *username;
1131 const char *oldval = "";
1132 union samr_UserInfo *info = NULL;
1135 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1137 return NT_STATUS_INVALID_PARAMETER;
1140 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1144 if (!NT_STATUS_IS_OK(result)) {
1148 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1150 FETCHSTR("fullname", full_name);
1151 FETCHSTR("homedir", home_directory);
1152 FETCHSTR("homedrive", home_drive);
1153 FETCHSTR("logonscript", logon_script);
1154 FETCHSTR("profilepath", profile_path);
1155 FETCHSTR("description", description);
1158 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1162 if (strcmp(argv[0], "NULL") == 0) {
1166 ZERO_STRUCT(info->info21);
1168 SETSTR("fullname", full_name, FULL_NAME);
1169 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1170 SETSTR("homedrive", home_drive, HOME_DRIVE);
1171 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1172 SETSTR("profilepath", profile_path, PROFILE_PATH);
1173 SETSTR("description", description, DESCRIPTION);
1175 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1180 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1181 ctx->thiscmd, oldval, argv[0]);
1188 #define HANDLEFLG(name, rec) \
1189 do { if (strequal(ctx->thiscmd, name)) { \
1190 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1192 newflags = oldflags | ACB_##rec; \
1194 newflags = oldflags & ~ACB_##rec; \
1197 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1198 TALLOC_CTX *mem_ctx,
1199 struct rpc_sh_ctx *ctx,
1200 struct rpc_pipe_client *pipe_hnd,
1201 int argc, const char **argv)
1203 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1204 rpc_sh_user_str_edit_internals);
1207 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1208 TALLOC_CTX *mem_ctx,
1209 struct rpc_sh_ctx *ctx,
1210 struct rpc_pipe_client *pipe_hnd,
1211 POLICY_HND *user_hnd,
1212 int argc, const char **argv)
1215 const char *username;
1216 const char *oldval = "unknown";
1217 uint32 oldflags, newflags;
1219 union samr_UserInfo *info = NULL;
1222 ((argc == 1) && !strequal(argv[0], "yes") &&
1223 !strequal(argv[0], "no"))) {
1224 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1226 return NT_STATUS_INVALID_PARAMETER;
1229 newval = strequal(argv[0], "yes");
1231 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1235 if (!NT_STATUS_IS_OK(result)) {
1239 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1240 oldflags = info->info21.acct_flags;
1241 newflags = info->info21.acct_flags;
1243 HANDLEFLG("disabled", DISABLED);
1244 HANDLEFLG("pwnotreq", PWNOTREQ);
1245 HANDLEFLG("autolock", AUTOLOCK);
1246 HANDLEFLG("pwnoexp", PWNOEXP);
1249 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1253 ZERO_STRUCT(info->info21);
1255 info->info21.acct_flags = newflags;
1256 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1258 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1263 if (NT_STATUS_IS_OK(result)) {
1264 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1265 ctx->thiscmd, oldval, argv[0]);
1273 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1274 TALLOC_CTX *mem_ctx,
1275 struct rpc_sh_ctx *ctx,
1276 struct rpc_pipe_client *pipe_hnd,
1277 int argc, const char **argv)
1279 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1280 rpc_sh_user_flag_edit_internals);
1283 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1284 TALLOC_CTX *mem_ctx,
1285 struct rpc_sh_ctx *ctx)
1287 static struct rpc_sh_cmd cmds[] = {
1289 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1290 "Show/Set a user's full name" },
1292 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1293 "Show/Set a user's home directory" },
1295 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1296 "Show/Set a user's home drive" },
1298 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1299 "Show/Set a user's logon script" },
1301 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1302 "Show/Set a user's profile path" },
1304 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1305 "Show/Set a user's description" },
1307 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1308 "Show/Set whether a user is disabled" },
1310 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1311 "Show/Set whether a user locked out" },
1313 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1314 "Show/Set whether a user does not need a password" },
1316 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1317 "Show/Set whether a user's password does not expire" },
1319 { NULL, NULL, 0, NULL, NULL }
1325 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1326 TALLOC_CTX *mem_ctx,
1327 struct rpc_sh_ctx *ctx)
1329 static struct rpc_sh_cmd cmds[] = {
1331 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1332 "List available users" },
1334 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1335 "List the domain groups a user is member of" },
1337 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1338 "Show info about a user" },
1340 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1341 "Show/Modify a user's fields" },
1343 { NULL, NULL, 0, NULL, NULL }
1349 /****************************************************************************/
1352 * Basic usage function for 'net rpc group'.
1353 * @param argc Standard main() style argc.
1354 * @param argv Standard main() style argv. Initial components are already
1358 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1360 return net_group_usage(c, argc, argv);
1364 * Delete group on a remote RPC server.
1366 * All parameters are provided by the run_rpc_command function, except for
1367 * argc, argv which are passed through.
1369 * @param domain_sid The domain sid acquired from the remote server.
1370 * @param cli A cli_state connected to the server.
1371 * @param mem_ctx Talloc context, destroyed on completion of the function.
1372 * @param argc Standard main() style argc.
1373 * @param argv Standard main() style argv. Initial components are already
1376 * @return Normal NTSTATUS return.
1379 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1380 const DOM_SID *domain_sid,
1381 const char *domain_name,
1382 struct cli_state *cli,
1383 struct rpc_pipe_client *pipe_hnd,
1384 TALLOC_CTX *mem_ctx,
1388 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1389 bool group_is_primary = false;
1390 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1392 struct samr_RidTypeArray *rids = NULL;
1395 /* DOM_GID *user_gids; */
1397 struct samr_Ids group_rids, name_types;
1398 struct lsa_String lsa_acct_name;
1399 union samr_UserInfo *info = NULL;
1401 if (argc < 1 || c->display_usage) {
1402 rpc_group_usage(c, argc,argv);
1403 return NT_STATUS_OK; /* ok? */
1406 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1408 MAXIMUM_ALLOWED_ACCESS,
1411 if (!NT_STATUS_IS_OK(result)) {
1412 d_fprintf(stderr, "Request samr_Connect2 failed\n");
1416 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1418 MAXIMUM_ALLOWED_ACCESS,
1419 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1422 if (!NT_STATUS_IS_OK(result)) {
1423 d_fprintf(stderr, "Request open_domain failed\n");
1427 init_lsa_String(&lsa_acct_name, argv[0]);
1429 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1435 if (!NT_STATUS_IS_OK(result)) {
1436 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1440 switch (name_types.ids[0])
1442 case SID_NAME_DOM_GRP:
1443 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1445 MAXIMUM_ALLOWED_ACCESS,
1448 if (!NT_STATUS_IS_OK(result)) {
1449 d_fprintf(stderr, "Request open_group failed");
1453 group_rid = group_rids.ids[0];
1455 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1459 if (!NT_STATUS_IS_OK(result)) {
1460 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1464 if (c->opt_verbose) {
1465 d_printf("Domain Group %s (rid: %d) has %d members\n",
1466 argv[0],group_rid, rids->count);
1469 /* Check if group is anyone's primary group */
1470 for (i = 0; i < rids->count; i++)
1472 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1474 MAXIMUM_ALLOWED_ACCESS,
1478 if (!NT_STATUS_IS_OK(result)) {
1479 d_fprintf(stderr, "Unable to open group member %d\n",
1484 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1489 if (!NT_STATUS_IS_OK(result)) {
1490 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",
1495 if (info->info21.primary_gid == group_rid) {
1496 if (c->opt_verbose) {
1497 d_printf("Group is primary group of %s\n",
1498 info->info21.account_name.string);
1500 group_is_primary = true;
1503 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1506 if (group_is_primary) {
1507 d_fprintf(stderr, "Unable to delete group because some "
1508 "of it's members have it as primary group\n");
1509 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1513 /* remove all group members */
1514 for (i = 0; i < rids->count; i++)
1517 d_printf("Remove group member %d...",
1519 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1523 if (NT_STATUS_IS_OK(result)) {
1528 d_printf("failed\n");
1533 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1537 /* removing a local group is easier... */
1538 case SID_NAME_ALIAS:
1539 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1541 MAXIMUM_ALLOWED_ACCESS,
1545 if (!NT_STATUS_IS_OK(result)) {
1546 d_fprintf(stderr, "Request open_alias failed\n");
1550 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1554 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1555 argv[0],sid_type_lookup(name_types.ids[0]));
1556 result = NT_STATUS_UNSUCCESSFUL;
1560 if (NT_STATUS_IS_OK(result)) {
1562 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types.ids[0]),argv[0]);
1564 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1565 get_friendly_nt_error_msg(result));
1573 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1575 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1576 rpc_group_delete_internals, argc,argv);
1579 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1581 NET_API_STATUS status;
1582 struct GROUP_INFO_1 info1;
1583 uint32_t parm_error = 0;
1585 if (argc != 1 || c->display_usage) {
1586 rpc_group_usage(c, argc, argv);
1592 info1.grpi1_name = argv[0];
1593 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1594 info1.grpi1_comment = c->opt_comment;
1597 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1600 d_fprintf(stderr, "Failed to add group '%s' with: %s.\n",
1601 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1605 d_printf("Added group '%s'.\n", argv[0]);
1611 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1613 NET_API_STATUS status;
1614 struct LOCALGROUP_INFO_1 info1;
1615 uint32_t parm_error = 0;
1617 if (argc != 1 || c->display_usage) {
1618 rpc_group_usage(c, argc, argv);
1624 info1.lgrpi1_name = argv[0];
1625 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1626 info1.lgrpi1_comment = c->opt_comment;
1629 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1632 d_fprintf(stderr, "Failed to add alias '%s' with: %s.\n",
1633 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1637 d_printf("Added alias '%s'.\n", argv[0]);
1643 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1645 if (c->opt_localgroup)
1646 return rpc_alias_add_internals(c, argc, argv);
1648 return rpc_group_add_internals(c, argc, argv);
1651 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1652 TALLOC_CTX *mem_ctx,
1655 enum lsa_SidType *type)
1657 DOM_SID *sids = NULL;
1658 enum lsa_SidType *types = NULL;
1659 struct rpc_pipe_client *pipe_hnd;
1661 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1663 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1665 if (!NT_STATUS_IS_OK(result)) {
1669 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1670 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1672 if (!NT_STATUS_IS_OK(result)) {
1676 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1677 &name, NULL, 1, &sids, &types);
1679 if (NT_STATUS_IS_OK(result)) {
1680 sid_copy(sid, &sids[0]);
1684 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1688 TALLOC_FREE(pipe_hnd);
1691 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1693 /* Try as S-1-5-whatever */
1697 if (string_to_sid(&tmp_sid, name)) {
1698 sid_copy(sid, &tmp_sid);
1699 *type = SID_NAME_UNKNOWN;
1700 result = NT_STATUS_OK;
1707 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1708 TALLOC_CTX *mem_ctx,
1709 const DOM_SID *group_sid,
1712 POLICY_HND connect_pol, domain_pol;
1715 POLICY_HND group_pol;
1717 struct samr_Ids rids, rid_types;
1718 struct lsa_String lsa_acct_name;
1722 sid_copy(&sid, group_sid);
1724 if (!sid_split_rid(&sid, &group_rid)) {
1725 return NT_STATUS_UNSUCCESSFUL;
1728 /* Get sam policy handle */
1729 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1731 MAXIMUM_ALLOWED_ACCESS,
1733 if (!NT_STATUS_IS_OK(result)) {
1737 /* Get domain policy handle */
1738 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1740 MAXIMUM_ALLOWED_ACCESS,
1743 if (!NT_STATUS_IS_OK(result)) {
1747 init_lsa_String(&lsa_acct_name, member);
1749 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1756 if (!NT_STATUS_IS_OK(result)) {
1757 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1761 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1763 MAXIMUM_ALLOWED_ACCESS,
1767 if (!NT_STATUS_IS_OK(result)) {
1771 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1774 0x0005); /* unknown flags */
1777 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1781 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1782 TALLOC_CTX *mem_ctx,
1783 const DOM_SID *alias_sid,
1786 POLICY_HND connect_pol, domain_pol;
1789 POLICY_HND alias_pol;
1792 enum lsa_SidType member_type;
1796 sid_copy(&sid, alias_sid);
1798 if (!sid_split_rid(&sid, &alias_rid)) {
1799 return NT_STATUS_UNSUCCESSFUL;
1802 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1803 member, &member_sid, &member_type);
1805 if (!NT_STATUS_IS_OK(result)) {
1806 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1810 /* Get sam policy handle */
1811 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1813 MAXIMUM_ALLOWED_ACCESS,
1815 if (!NT_STATUS_IS_OK(result)) {
1819 /* Get domain policy handle */
1820 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1822 MAXIMUM_ALLOWED_ACCESS,
1825 if (!NT_STATUS_IS_OK(result)) {
1829 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1831 MAXIMUM_ALLOWED_ACCESS,
1835 if (!NT_STATUS_IS_OK(result)) {
1839 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1843 if (!NT_STATUS_IS_OK(result)) {
1848 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1852 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1853 const DOM_SID *domain_sid,
1854 const char *domain_name,
1855 struct cli_state *cli,
1856 struct rpc_pipe_client *pipe_hnd,
1857 TALLOC_CTX *mem_ctx,
1862 enum lsa_SidType group_type;
1864 if (argc != 2 || c->display_usage) {
1866 "net rpc group addmem <group> <member>\n"
1867 " Add a member to a group\n"
1868 " group\tGroup to add member to\n"
1869 " member\tMember to add to group\n");
1870 return NT_STATUS_UNSUCCESSFUL;
1873 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1874 &group_sid, &group_type))) {
1875 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
1876 return NT_STATUS_UNSUCCESSFUL;
1879 if (group_type == SID_NAME_DOM_GRP) {
1880 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
1881 &group_sid, argv[1]);
1883 if (!NT_STATUS_IS_OK(result)) {
1884 d_fprintf(stderr, "Could not add %s to %s: %s\n",
1885 argv[1], argv[0], nt_errstr(result));
1890 if (group_type == SID_NAME_ALIAS) {
1891 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
1892 &group_sid, argv[1]);
1894 if (!NT_STATUS_IS_OK(result)) {
1895 d_fprintf(stderr, "Could not add %s to %s: %s\n",
1896 argv[1], argv[0], nt_errstr(result));
1901 d_fprintf(stderr, "Can only add members to global or local groups "
1902 "which %s is not\n", argv[0]);
1904 return NT_STATUS_UNSUCCESSFUL;
1907 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
1909 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1910 rpc_group_addmem_internals,
1914 static NTSTATUS rpc_del_groupmem(struct net_context *c,
1915 struct rpc_pipe_client *pipe_hnd,
1916 TALLOC_CTX *mem_ctx,
1917 const DOM_SID *group_sid,
1920 POLICY_HND connect_pol, domain_pol;
1923 POLICY_HND group_pol;
1925 struct samr_Ids rids, rid_types;
1926 struct lsa_String lsa_acct_name;
1930 sid_copy(&sid, group_sid);
1932 if (!sid_split_rid(&sid, &group_rid))
1933 return NT_STATUS_UNSUCCESSFUL;
1935 /* Get sam policy handle */
1936 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1938 MAXIMUM_ALLOWED_ACCESS,
1940 if (!NT_STATUS_IS_OK(result))
1943 /* Get domain policy handle */
1944 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1946 MAXIMUM_ALLOWED_ACCESS,
1949 if (!NT_STATUS_IS_OK(result))
1952 init_lsa_String(&lsa_acct_name, member);
1954 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1960 if (!NT_STATUS_IS_OK(result)) {
1961 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1965 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1967 MAXIMUM_ALLOWED_ACCESS,
1971 if (!NT_STATUS_IS_OK(result))
1974 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1979 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1983 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
1984 TALLOC_CTX *mem_ctx,
1985 const DOM_SID *alias_sid,
1988 POLICY_HND connect_pol, domain_pol;
1991 POLICY_HND alias_pol;
1994 enum lsa_SidType member_type;
1998 sid_copy(&sid, alias_sid);
2000 if (!sid_split_rid(&sid, &alias_rid))
2001 return NT_STATUS_UNSUCCESSFUL;
2003 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2004 member, &member_sid, &member_type);
2006 if (!NT_STATUS_IS_OK(result)) {
2007 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2011 /* Get sam policy handle */
2012 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2014 MAXIMUM_ALLOWED_ACCESS,
2016 if (!NT_STATUS_IS_OK(result)) {
2020 /* Get domain policy handle */
2021 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2023 MAXIMUM_ALLOWED_ACCESS,
2026 if (!NT_STATUS_IS_OK(result)) {
2030 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2032 MAXIMUM_ALLOWED_ACCESS,
2036 if (!NT_STATUS_IS_OK(result))
2039 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2043 if (!NT_STATUS_IS_OK(result))
2047 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2051 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2052 const DOM_SID *domain_sid,
2053 const char *domain_name,
2054 struct cli_state *cli,
2055 struct rpc_pipe_client *pipe_hnd,
2056 TALLOC_CTX *mem_ctx,
2061 enum lsa_SidType group_type;
2063 if (argc != 2 || c->display_usage) {
2065 "net rpc group delmem <group> <member>\n"
2066 " Delete a member from a group\n"
2067 " group\tGroup to delete member from\n"
2068 " member\tMember to delete from group\n");
2069 return NT_STATUS_UNSUCCESSFUL;
2072 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2073 &group_sid, &group_type))) {
2074 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2075 return NT_STATUS_UNSUCCESSFUL;
2078 if (group_type == SID_NAME_DOM_GRP) {
2079 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2080 &group_sid, argv[1]);
2082 if (!NT_STATUS_IS_OK(result)) {
2083 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2084 argv[1], argv[0], nt_errstr(result));
2089 if (group_type == SID_NAME_ALIAS) {
2090 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2091 &group_sid, argv[1]);
2093 if (!NT_STATUS_IS_OK(result)) {
2094 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2095 argv[1], argv[0], nt_errstr(result));
2100 d_fprintf(stderr, "Can only delete members from global or local groups "
2101 "which %s is not\n", argv[0]);
2103 return NT_STATUS_UNSUCCESSFUL;
2106 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2108 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2109 rpc_group_delmem_internals,
2114 * List groups on a remote RPC server.
2116 * All parameters are provided by the run_rpc_command function, except for
2117 * argc, argv which are passes through.
2119 * @param domain_sid The domain sid acquired from the remote server.
2120 * @param cli A cli_state connected to the server.
2121 * @param mem_ctx Talloc context, destroyed on completion of the function.
2122 * @param argc Standard main() style argc.
2123 * @param argv Standard main() style argv. Initial components are already
2126 * @return Normal NTSTATUS return.
2129 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2130 const DOM_SID *domain_sid,
2131 const char *domain_name,
2132 struct cli_state *cli,
2133 struct rpc_pipe_client *pipe_hnd,
2134 TALLOC_CTX *mem_ctx,
2138 POLICY_HND connect_pol, domain_pol;
2139 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2140 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2141 struct samr_SamArray *groups = NULL;
2142 bool global = false;
2144 bool builtin = false;
2146 if (c->display_usage) {
2148 "net rpc group list [global] [local] [builtin]\n"
2149 " List groups on RPC server\n"
2150 " global\tList global groups\n"
2151 " local\tList local groups\n"
2152 " builtin\tList builtin groups\n"
2153 " If none of global, local or builtin is "
2154 "specified, all three options are considered set\n");
2155 return NT_STATUS_OK;
2164 for (i=0; i<argc; i++) {
2165 if (strequal(argv[i], "global"))
2168 if (strequal(argv[i], "local"))
2171 if (strequal(argv[i], "builtin"))
2175 /* Get sam policy handle */
2177 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2179 MAXIMUM_ALLOWED_ACCESS,
2181 if (!NT_STATUS_IS_OK(result)) {
2185 /* Get domain policy handle */
2187 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2189 MAXIMUM_ALLOWED_ACCESS,
2190 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2192 if (!NT_STATUS_IS_OK(result)) {
2196 /* Query domain groups */
2197 if (c->opt_long_list_entries)
2198 d_printf("\nGroup name Comment"
2199 "\n-----------------------------\n");
2201 uint32_t max_size, total_size, returned_size;
2202 union samr_DispInfo info;
2206 get_query_dispinfo_params(
2207 loop_count, &max_entries, &max_size);
2209 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2218 num_entries = info.info3.count;
2219 start_idx += info.info3.count;
2221 if (!NT_STATUS_IS_OK(result) &&
2222 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2225 for (i = 0; i < num_entries; i++) {
2227 const char *group = NULL;
2228 const char *desc = NULL;
2230 group = info.info3.entries[i].account_name.string;
2231 desc = info.info3.entries[i].description.string;
2233 if (c->opt_long_list_entries)
2234 printf("%-21.21s %-50.50s\n",
2237 printf("%s\n", group);
2239 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2240 /* query domain aliases */
2245 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2251 if (!NT_STATUS_IS_OK(result) &&
2252 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2255 for (i = 0; i < num_entries; i++) {
2257 const char *description = NULL;
2259 if (c->opt_long_list_entries) {
2261 POLICY_HND alias_pol;
2262 union samr_AliasInfo *info = NULL;
2264 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2267 groups->entries[i].idx,
2269 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2273 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2275 description = info->description.string;
2279 if (description != NULL) {
2280 printf("%-21.21s %-50.50s\n",
2281 groups->entries[i].name.string,
2284 printf("%s\n", groups->entries[i].name.string);
2287 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2288 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2289 /* Get builtin policy handle */
2291 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2293 MAXIMUM_ALLOWED_ACCESS,
2294 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2296 if (!NT_STATUS_IS_OK(result)) {
2299 /* query builtin aliases */
2302 if (!builtin) break;
2304 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2310 if (!NT_STATUS_IS_OK(result) &&
2311 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2314 for (i = 0; i < num_entries; i++) {
2316 const char *description = NULL;
2318 if (c->opt_long_list_entries) {
2320 POLICY_HND alias_pol;
2321 union samr_AliasInfo *info = NULL;
2323 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2326 groups->entries[i].idx,
2328 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2332 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2334 description = info->description.string;
2338 if (description != NULL) {
2339 printf("%-21.21s %-50.50s\n",
2340 groups->entries[i].name.string,
2343 printf("%s\n", groups->entries[i].name.string);
2346 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2352 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2354 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2355 rpc_group_list_internals,
2359 static NTSTATUS rpc_list_group_members(struct net_context *c,
2360 struct rpc_pipe_client *pipe_hnd,
2361 TALLOC_CTX *mem_ctx,
2362 const char *domain_name,
2363 const DOM_SID *domain_sid,
2364 POLICY_HND *domain_pol,
2368 POLICY_HND group_pol;
2369 uint32 num_members, *group_rids;
2371 struct samr_RidTypeArray *rids = NULL;
2372 struct lsa_Strings names;
2373 struct samr_Ids types;
2376 sid_to_fstring(sid_str, domain_sid);
2378 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2380 MAXIMUM_ALLOWED_ACCESS,
2384 if (!NT_STATUS_IS_OK(result))
2387 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2391 if (!NT_STATUS_IS_OK(result))
2394 num_members = rids->count;
2395 group_rids = rids->rids;
2397 while (num_members > 0) {
2398 int this_time = 512;
2400 if (num_members < this_time)
2401 this_time = num_members;
2403 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2410 if (!NT_STATUS_IS_OK(result))
2413 /* We only have users as members, but make the output
2414 the same as the output of alias members */
2416 for (i = 0; i < this_time; i++) {
2418 if (c->opt_long_list_entries) {
2419 printf("%s-%d %s\\%s %d\n", sid_str,
2420 group_rids[i], domain_name,
2421 names.names[i].string,
2424 printf("%s\\%s\n", domain_name,
2425 names.names[i].string);
2429 num_members -= this_time;
2433 return NT_STATUS_OK;
2436 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2437 struct rpc_pipe_client *pipe_hnd,
2438 TALLOC_CTX *mem_ctx,
2439 POLICY_HND *domain_pol,
2443 struct rpc_pipe_client *lsa_pipe;
2444 POLICY_HND alias_pol, lsa_pol;
2446 DOM_SID *alias_sids;
2449 enum lsa_SidType *types;
2451 struct lsa_SidArray sid_array;
2453 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2455 MAXIMUM_ALLOWED_ACCESS,
2459 if (!NT_STATUS_IS_OK(result))
2462 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2466 if (!NT_STATUS_IS_OK(result)) {
2467 d_fprintf(stderr, "Couldn't list alias members\n");
2471 num_members = sid_array.num_sids;
2473 if (num_members == 0) {
2474 return NT_STATUS_OK;
2477 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2478 &ndr_table_lsarpc.syntax_id,
2480 if (!NT_STATUS_IS_OK(result)) {
2481 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2482 nt_errstr(result) );
2486 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2487 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2489 if (!NT_STATUS_IS_OK(result)) {
2490 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2491 TALLOC_FREE(lsa_pipe);
2495 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2497 d_fprintf(stderr, "Out of memory\n");
2498 TALLOC_FREE(lsa_pipe);
2499 return NT_STATUS_NO_MEMORY;
2502 for (i=0; i<num_members; i++) {
2503 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2506 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2507 num_members, alias_sids,
2508 &domains, &names, &types);
2510 if (!NT_STATUS_IS_OK(result) &&
2511 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2512 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2513 TALLOC_FREE(lsa_pipe);
2517 for (i = 0; i < num_members; i++) {
2519 sid_to_fstring(sid_str, &alias_sids[i]);
2521 if (c->opt_long_list_entries) {
2522 printf("%s %s\\%s %d\n", sid_str,
2523 domains[i] ? domains[i] : "*unknown*",
2524 names[i] ? names[i] : "*unknown*", types[i]);
2527 printf("%s\\%s\n", domains[i], names[i]);
2529 printf("%s\n", sid_str);
2533 TALLOC_FREE(lsa_pipe);
2534 return NT_STATUS_OK;
2537 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2538 const DOM_SID *domain_sid,
2539 const char *domain_name,
2540 struct cli_state *cli,
2541 struct rpc_pipe_client *pipe_hnd,
2542 TALLOC_CTX *mem_ctx,
2547 POLICY_HND connect_pol, domain_pol;
2548 struct samr_Ids rids, rid_types;
2549 struct lsa_String lsa_acct_name;
2551 /* Get sam policy handle */
2553 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2555 MAXIMUM_ALLOWED_ACCESS,
2558 if (!NT_STATUS_IS_OK(result))
2561 /* Get domain policy handle */
2563 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2565 MAXIMUM_ALLOWED_ACCESS,
2566 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2569 if (!NT_STATUS_IS_OK(result))
2572 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2574 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2581 if (!NT_STATUS_IS_OK(result)) {
2583 /* Ok, did not find it in the global sam, try with builtin */
2585 DOM_SID sid_Builtin;
2587 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2589 sid_copy(&sid_Builtin, &global_sid_Builtin);
2591 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2593 MAXIMUM_ALLOWED_ACCESS,
2597 if (!NT_STATUS_IS_OK(result)) {
2598 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2602 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2609 if (!NT_STATUS_IS_OK(result)) {
2610 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2615 if (rids.count != 1) {
2616 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2620 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2621 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2622 domain_sid, &domain_pol,
2626 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2627 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2631 return NT_STATUS_NO_SUCH_GROUP;
2634 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2636 if (argc != 1 || c->display_usage) {
2637 return rpc_group_usage(c, argc, argv);
2640 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2641 rpc_group_members_internals,
2645 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2647 NET_API_STATUS status;
2648 struct GROUP_INFO_0 g0;
2652 d_printf("Usage: 'net rpc group rename group newname'\n");
2656 g0.grpi0_name = argv[1];
2658 status = NetGroupSetInfo(c->opt_host,
2665 d_fprintf(stderr, "Renaming group %s failed with: %s\n",
2666 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2674 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2676 if (argc != 2 || c->display_usage) {
2677 return rpc_group_usage(c, argc, argv);
2680 return rpc_group_rename_internals(c, argc, argv);
2684 * 'net rpc group' entrypoint.
2685 * @param argc Standard main() style argc.
2686 * @param argv Standard main() style argv. Initial components are already
2690 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2692 NET_API_STATUS status;
2694 struct functable func[] = {
2699 "Create specified group",
2700 "net rpc group add\n"
2701 " Create specified group"
2707 "Delete specified group",
2708 "net rpc group delete\n"
2709 " Delete specified group"
2715 "Add member to group",
2716 "net rpc group addmem\n"
2717 " Add member to group"
2723 "Remove member from group",
2724 "net rpc group delmem\n"
2725 " Remove member from group"
2732 "net rpc group list\n"
2739 "List group members",
2740 "net rpc group members\n"
2741 " List group members"
2748 "net rpc group rename\n"
2751 {NULL, NULL, 0, NULL, NULL}
2754 status = libnetapi_init(&c->netapi_ctx);
2758 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2759 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2760 if (c->opt_kerberos) {
2761 libnetapi_set_use_kerberos(c->netapi_ctx);
2765 if (c->display_usage) {
2766 d_printf("Usage:\n");
2767 d_printf("net rpc group\n"
2768 " Alias for net rpc group list global local "
2770 net_display_usage_from_functable(func);
2774 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2775 rpc_group_list_internals,
2779 return net_run_function(c, argc, argv, "net rpc group", func);
2782 /****************************************************************************/
2784 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2786 return net_share_usage(c, argc, argv);
2790 * Add a share on a remote RPC server.
2792 * @param argc Standard main() style argc.
2793 * @param argv Standard main() style argv. Initial components are already
2796 * @return A shell status integer (0 for success).
2799 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2801 NET_API_STATUS status;
2804 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2805 uint32 num_users=0, perms=0;
2806 char *password=NULL; /* don't allow a share password */
2807 struct SHARE_INFO_2 i2;
2808 uint32_t parm_error = 0;
2810 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2811 return rpc_share_usage(c, argc, argv);
2814 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2818 path = strchr(sharename, '=');
2825 i2.shi2_netname = sharename;
2826 i2.shi2_type = type;
2827 i2.shi2_remark = c->opt_comment;
2828 i2.shi2_permissions = perms;
2829 i2.shi2_max_uses = c->opt_maxusers;
2830 i2.shi2_current_uses = num_users;
2831 i2.shi2_path = path;
2832 i2.shi2_passwd = password;
2834 status = NetShareAdd(c->opt_host,
2839 printf("NetShareAdd failed with: %s\n",
2840 libnetapi_get_error_string(c->netapi_ctx, status));
2847 * Delete a share on a remote RPC server.
2849 * @param domain_sid The domain sid acquired from the remote server.
2850 * @param argc Standard main() style argc.
2851 * @param argv Standard main() style argv. Initial components are already
2854 * @return A shell status integer (0 for success).
2856 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
2858 if (argc < 1 || c->display_usage) {
2859 return rpc_share_usage(c, argc, argv);
2862 return NetShareDel(c->opt_host, argv[0], 0);
2866 * Formatted print of share info
2868 * @param info1 pointer to SRV_SHARE_INFO_1 to format
2871 static void display_share_info_1(struct net_context *c,
2872 struct srvsvc_NetShareInfo1 *r)
2874 if (c->opt_long_list_entries) {
2875 d_printf("%-12s %-8.8s %-50s\n",
2877 c->share_type[r->type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)],
2880 d_printf("%s\n", r->name);
2884 static WERROR get_share_info(struct net_context *c,
2885 struct rpc_pipe_client *pipe_hnd,
2886 TALLOC_CTX *mem_ctx,
2890 struct srvsvc_NetShareInfoCtr *info_ctr)
2894 union srvsvc_NetShareInfo info;
2896 /* no specific share requested, enumerate all */
2899 uint32_t preferred_len = 0xffffffff;
2900 uint32_t total_entries = 0;
2901 uint32_t resume_handle = 0;
2903 info_ctr->level = level;
2905 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
2915 /* request just one share */
2916 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
2923 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
2928 ZERO_STRUCTP(info_ctr);
2930 info_ctr->level = level;
2935 struct srvsvc_NetShareCtr1 *ctr1;
2937 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
2938 W_ERROR_HAVE_NO_MEMORY(ctr1);
2941 ctr1->array = info.info1;
2943 info_ctr->ctr.ctr1 = ctr1;
2947 struct srvsvc_NetShareCtr2 *ctr2;
2949 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
2950 W_ERROR_HAVE_NO_MEMORY(ctr2);
2953 ctr2->array = info.info2;
2955 info_ctr->ctr.ctr2 = ctr2;
2959 struct srvsvc_NetShareCtr502 *ctr502;
2961 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
2962 W_ERROR_HAVE_NO_MEMORY(ctr502);
2965 ctr502->array = info.info502;
2967 info_ctr->ctr.ctr502 = ctr502;
2975 * List shares on a remote RPC server.
2977 * All parameters are provided by the run_rpc_command function, except for
2978 * argc, argv which are passed through.
2980 * @param domain_sid The domain sid acquired from the remote server.
2981 * @param cli A cli_state connected to the server.
2982 * @param mem_ctx Talloc context, destroyed on completion of the function.
2983 * @param argc Standard main() style argc.
2984 * @param argv Standard main() style argv. Initial components are already
2987 * @return Normal NTSTATUS return.
2990 static NTSTATUS rpc_share_list_internals(struct net_context *c,
2991 const DOM_SID *domain_sid,
2992 const char *domain_name,
2993 struct cli_state *cli,
2994 struct rpc_pipe_client *pipe_hnd,
2995 TALLOC_CTX *mem_ctx,
2999 struct srvsvc_NetShareInfoCtr info_ctr;
3000 struct srvsvc_NetShareCtr1 ctr1;
3002 uint32 i, level = 1;
3004 ZERO_STRUCT(info_ctr);
3008 info_ctr.ctr.ctr1 = &ctr1;
3010 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3012 if (!W_ERROR_IS_OK(result))
3015 /* Display results */
3017 if (c->opt_long_list_entries) {
3019 "\nEnumerating shared resources (exports) on remote server:\n\n"
3020 "\nShare name Type Description\n"
3021 "---------- ---- -----------\n");
3023 for (i = 0; i < info_ctr.ctr.ctr1->count; i++)
3024 display_share_info_1(c, &info_ctr.ctr.ctr1->array[i]);
3026 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3030 * 'net rpc share list' entrypoint.
3031 * @param argc Standard main() style argc.
3032 * @param argv Standard main() style argv. Initial components are already
3035 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3037 if (c->display_usage) {
3039 "net rpc share list\n"
3040 " List shares on remote server\n");
3044 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3045 rpc_share_list_internals, argc, argv);
3048 static bool check_share_availability(struct cli_state *cli, const char *netname)
3050 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3051 d_printf("skipping [%s]: not a file share.\n", netname);
3061 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3062 const char *netname, uint32 type)
3064 /* only support disk shares */
3065 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3066 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3070 /* skip builtin shares */
3071 /* FIXME: should print$ be added too ? */
3072 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3073 strequal(netname,"global"))
3076 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3077 printf("excluding [%s]\n", netname);
3081 return check_share_availability(cli, netname);
3085 * Migrate shares from a remote RPC server to the local RPC server.
3087 * All parameters are provided by the run_rpc_command function, except for
3088 * argc, argv which are passed through.
3090 * @param domain_sid The domain sid acquired from the remote server.
3091 * @param cli A cli_state connected to the server.
3092 * @param mem_ctx Talloc context, destroyed on completion of the function.
3093 * @param argc Standard main() style argc.
3094 * @param argv Standard main() style argv. Initial components are already
3097 * @return Normal NTSTATUS return.
3100 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3101 const DOM_SID *domain_sid,
3102 const char *domain_name,
3103 struct cli_state *cli,
3104 struct rpc_pipe_client *pipe_hnd,
3105 TALLOC_CTX *mem_ctx,
3110 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3111 struct srvsvc_NetShareInfoCtr ctr_src;
3113 struct rpc_pipe_client *srvsvc_pipe = NULL;
3114 struct cli_state *cli_dst = NULL;
3115 uint32 level = 502; /* includes secdesc */
3116 uint32_t parm_error = 0;
3118 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3120 if (!W_ERROR_IS_OK(result))
3123 /* connect destination PI_SRVSVC */
3124 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3125 &ndr_table_srvsvc.syntax_id);
3126 if (!NT_STATUS_IS_OK(nt_status))
3130 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3132 union srvsvc_NetShareInfo info;
3133 struct srvsvc_NetShareInfo502 info502 =
3134 ctr_src.ctr.ctr502->array[i];
3136 /* reset error-code */
3137 nt_status = NT_STATUS_UNSUCCESSFUL;
3139 if (!check_share_sanity(c, cli, info502.name, info502.type))
3142 /* finally add the share on the dst server */
3144 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3145 info502.name, info502.path, info502.comment);
3147 info.info502 = &info502;
3149 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3150 srvsvc_pipe->desthost,
3156 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3157 printf(" [%s] does already exist\n",
3162 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3163 printf("cannot add share: %s\n", dos_errstr(result));
3169 nt_status = NT_STATUS_OK;
3173 cli_shutdown(cli_dst);
3181 * Migrate shares from a RPC server to another.
3183 * @param argc Standard main() style argc.
3184 * @param argv Standard main() style argv. Initial components are already
3187 * @return A shell status integer (0 for success).
3189 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3192 if (c->display_usage) {
3194 "net rpc share migrate shares\n"
3195 " Migrate shares to local server\n");
3200 printf("no server to migrate\n");
3204 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3205 rpc_share_migrate_shares_internals,
3212 * @param f file_info
3213 * @param mask current search mask
3214 * @param state arg-pointer
3217 static void copy_fn(const char *mnt, file_info *f,
3218 const char *mask, void *state)
3220 static NTSTATUS nt_status;
3221 static struct copy_clistate *local_state;
3222 static fstring filename, new_mask;
3225 struct net_context *c;
3227 local_state = (struct copy_clistate *)state;
3228 nt_status = NT_STATUS_UNSUCCESSFUL;
3232 if (strequal(f->name, ".") || strequal(f->name, ".."))
3235 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3238 if (f->mode & aDIR) {
3240 DEBUG(3,("got dir: %s\n", f->name));
3242 fstrcpy(dir, local_state->cwd);
3244 fstrcat(dir, f->name);
3246 switch (net_mode_share)
3248 case NET_MODE_SHARE_MIGRATE:
3249 /* create that directory */
3250 nt_status = net_copy_file(c, local_state->mem_ctx,
3251 local_state->cli_share_src,
3252 local_state->cli_share_dst,
3254 c->opt_acls? true : false,
3255 c->opt_attrs? true : false,
3256 c->opt_timestamps? true:false,
3260 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3264 if (!NT_STATUS_IS_OK(nt_status))
3265 printf("could not handle dir %s: %s\n",
3266 dir, nt_errstr(nt_status));
3268 /* search below that directory */
3269 fstrcpy(new_mask, dir);
3270 fstrcat(new_mask, "\\*");
3272 old_dir = local_state->cwd;
3273 local_state->cwd = dir;
3274 if (!sync_files(local_state, new_mask))
3275 printf("could not handle files\n");
3276 local_state->cwd = old_dir;
3283 fstrcpy(filename, local_state->cwd);
3284 fstrcat(filename, "\\");
3285 fstrcat(filename, f->name);
3287 DEBUG(3,("got file: %s\n", filename));
3289 switch (net_mode_share)
3291 case NET_MODE_SHARE_MIGRATE:
3292 nt_status = net_copy_file(c, local_state->mem_ctx,
3293 local_state->cli_share_src,
3294 local_state->cli_share_dst,
3296 c->opt_acls? true : false,
3297 c->opt_attrs? true : false,
3298 c->opt_timestamps? true: false,
3302 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3306 if (!NT_STATUS_IS_OK(nt_status))
3307 printf("could not handle file %s: %s\n",
3308 filename, nt_errstr(nt_status));
3313 * sync files, can be called recursivly to list files
3314 * and then call copy_fn for each file
3316 * @param cp_clistate pointer to the copy_clistate we work with
3317 * @param mask the current search mask
3319 * @return Boolean result
3321 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3323 struct cli_state *targetcli;
3324 char *targetpath = NULL;
3326 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3328 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate->cli_share_src,
3329 mask, &targetcli, &targetpath ) ) {
3330 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3331 mask, cli_errstr(cp_clistate->cli_share_src));
3335 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3336 d_fprintf(stderr, "listing %s failed with error: %s\n",
3337 mask, cli_errstr(targetcli));
3346 * Set the top level directory permissions before we do any further copies.
3347 * Should set up ACL inheritance.
3350 bool copy_top_level_perms(struct net_context *c,
3351 struct copy_clistate *cp_clistate,
3352 const char *sharename)
3354 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3356 switch (net_mode_share) {
3357 case NET_MODE_SHARE_MIGRATE:
3358 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3359 nt_status = net_copy_fileattr(c,
3360 cp_clistate->mem_ctx,
3361 cp_clistate->cli_share_src,
3362 cp_clistate->cli_share_dst,
3364 c->opt_acls? true : false,
3365 c->opt_attrs? true : false,
3366 c->opt_timestamps? true: false,
3370 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3374 if (!NT_STATUS_IS_OK(nt_status)) {
3375 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3376 sharename, nt_errstr(nt_status));
3384 * Sync all files inside a remote share to another share (over smb).
3386 * All parameters are provided by the run_rpc_command function, except for
3387 * argc, argv which are passed through.
3389 * @param domain_sid The domain sid acquired from the remote server.
3390 * @param cli A cli_state connected to the server.
3391 * @param mem_ctx Talloc context, destroyed on completion of the function.
3392 * @param argc Standard main() style argc.
3393 * @param argv Standard main() style argv. Initial components are already
3396 * @return Normal NTSTATUS return.
3399 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3400 const DOM_SID *domain_sid,
3401 const char *domain_name,
3402 struct cli_state *cli,
3403 struct rpc_pipe_client *pipe_hnd,
3404 TALLOC_CTX *mem_ctx,
3409 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3410 struct srvsvc_NetShareInfoCtr ctr_src;
3413 struct copy_clistate cp_clistate;
3414 bool got_src_share = false;
3415 bool got_dst_share = false;
3416 const char *mask = "\\*";
3419 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3421 nt_status = NT_STATUS_NO_MEMORY;
3425 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3428 if (!W_ERROR_IS_OK(result))
3431 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3433 struct srvsvc_NetShareInfo502 info502 =
3434 ctr_src.ctr.ctr502->array[i];
3436 if (!check_share_sanity(c, cli, info502.name, info502.type))
3439 /* one might not want to mirror whole discs :) */
3440 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3441 d_printf("skipping [%s]: builtin/hidden share\n", info502.name);
3445 switch (net_mode_share)
3447 case NET_MODE_SHARE_MIGRATE:
3451 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3454 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3456 c->opt_acls ? "including" : "without",
3457 c->opt_attrs ? "including" : "without",
3458 c->opt_timestamps ? "(preserving timestamps)" : "");
3460 cp_clistate.mem_ctx = mem_ctx;
3461 cp_clistate.cli_share_src = NULL;
3462 cp_clistate.cli_share_dst = NULL;
3463 cp_clistate.cwd = NULL;
3464 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3467 /* open share source */
3468 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3469 &cli->dest_ss, cli->desthost,
3470 info502.name, "A:");
3471 if (!NT_STATUS_IS_OK(nt_status))
3474 got_src_share = true;
3476 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3477 /* open share destination */
3478 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3479 NULL, dst, info502.name, "A:");
3480 if (!NT_STATUS_IS_OK(nt_status))
3483 got_dst_share = true;
3486 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3487 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", info502.name);
3488 nt_status = NT_STATUS_UNSUCCESSFUL;
3492 if (!sync_files(&cp_clistate, mask)) {
3493 d_fprintf(stderr, "could not handle files for share: %s\n", info502.name);
3494 nt_status = NT_STATUS_UNSUCCESSFUL;
3499 nt_status = NT_STATUS_OK;
3504 cli_shutdown(cp_clistate.cli_share_src);
3507 cli_shutdown(cp_clistate.cli_share_dst);
3514 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3516 if (c->display_usage) {
3518 "net share migrate files\n"
3519 " Migrate files to local server\n");
3524 d_printf("no server to migrate\n");
3528 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3529 rpc_share_migrate_files_internals,
3534 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3536 * All parameters are provided by the run_rpc_command function, except for
3537 * argc, argv which are passed through.
3539 * @param domain_sid The domain sid acquired from the remote server.
3540 * @param cli A cli_state connected to the server.
3541 * @param mem_ctx Talloc context, destroyed on completion of the function.
3542 * @param argc Standard main() style argc.
3543 * @param argv Standard main() style argv. Initial components are already
3546 * @return Normal NTSTATUS return.
3549 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3550 const DOM_SID *domain_sid,
3551 const char *domain_name,
3552 struct cli_state *cli,
3553 struct rpc_pipe_client *pipe_hnd,
3554 TALLOC_CTX *mem_ctx,
3559 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3560 struct srvsvc_NetShareInfoCtr ctr_src;
3561 union srvsvc_NetShareInfo info;
3563 struct rpc_pipe_client *srvsvc_pipe = NULL;
3564 struct cli_state *cli_dst = NULL;
3565 uint32 level = 502; /* includes secdesc */
3566 uint32_t parm_error = 0;
3568 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3571 if (!W_ERROR_IS_OK(result))
3574 /* connect destination PI_SRVSVC */
3575 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3576 &ndr_table_srvsvc.syntax_id);
3577 if (!NT_STATUS_IS_OK(nt_status))
3581 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3583 struct srvsvc_NetShareInfo502 info502 =
3584 ctr_src.ctr.ctr502->array[i];
3586 /* reset error-code */
3587 nt_status = NT_STATUS_UNSUCCESSFUL;
3589 if (!check_share_sanity(c, cli, info502.name, info502.type))
3592 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3593 info502.name, info502.path, info502.comment);
3596 display_sec_desc(info502.sd_buf.sd);
3598 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3599 info.info502 = &info502;
3601 /* finally modify the share on the dst server */
3602 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3603 srvsvc_pipe->desthost,
3609 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3610 printf("cannot set share-acl: %s\n", dos_errstr(result));
3616 nt_status = NT_STATUS_OK;
3620 cli_shutdown(cli_dst);
3628 * Migrate share-acls from a RPC server to another.
3630 * @param argc Standard main() style argc.
3631 * @param argv Standard main() style argv. Initial components are already
3634 * @return A shell status integer (0 for success).
3636 static int rpc_share_migrate_security(struct net_context *c, int argc,
3639 if (c->display_usage) {
3641 "net rpc share migrate security\n"
3642 " Migrate share-acls to local server\n");
3647 d_printf("no server to migrate\n");
3651 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3652 rpc_share_migrate_security_internals,
3657 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3658 * from one server to another.
3660 * @param argc Standard main() style argc.
3661 * @param argv Standard main() style argv. Initial components are already
3664 * @return A shell status integer (0 for success).
3667 static int rpc_share_migrate_all(struct net_context *c, int argc,
3672 if (c->display_usage) {
3674 "net rpc share migrate all\n"
3675 " Migrates shares including all share settings\n");
3680 d_printf("no server to migrate\n");
3684 /* order is important. we don't want to be locked out by the share-acl
3685 * before copying files - gd */
3687 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3688 rpc_share_migrate_shares_internals, argc, argv);
3692 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3693 rpc_share_migrate_files_internals, argc, argv);
3697 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3698 rpc_share_migrate_security_internals, argc,
3704 * 'net rpc share migrate' entrypoint.
3705 * @param argc Standard main() style argc.
3706 * @param argv Standard main() style argv. Initial components are already
3709 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3712 struct functable func[] = {
3715 rpc_share_migrate_all,
3717 "Migrate shares from remote to local server",
3718 "net rpc share migrate all\n"
3719 " Migrate shares from remote to local server"
3723 rpc_share_migrate_files,
3725 "Migrate files from remote to local server",
3726 "net rpc share migrate files\n"
3727 " Migrate files from remote to local server"
3731 rpc_share_migrate_security,
3733 "Migrate share-ACLs from remote to local server",
3734 "net rpc share migrate security\n"
3735 " Migrate share-ACLs from remote to local server"
3739 rpc_share_migrate_shares,
3741 "Migrate shares from remote to local server",
3742 "net rpc share migrate shares\n"
3743 " Migrate shares from remote to local server"
3745 {NULL, NULL, 0, NULL, NULL}
3748 net_mode_share = NET_MODE_SHARE_MIGRATE;
3750 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3759 static int num_server_aliases;
3760 static struct full_alias *server_aliases;
3763 * Add an alias to the static list.
3765 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3767 if (server_aliases == NULL)
3768 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3770 server_aliases[num_server_aliases] = *alias;
3771 num_server_aliases += 1;
3775 * For a specific domain on the server, fetch all the aliases
3776 * and their members. Add all of them to the server_aliases.
3779 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3780 TALLOC_CTX *mem_ctx,
3781 POLICY_HND *connect_pol,
3782 const DOM_SID *domain_sid)
3784 uint32 start_idx, max_entries, num_entries, i;
3785 struct samr_SamArray *groups = NULL;
3787 POLICY_HND domain_pol;
3789 /* Get domain policy handle */
3791 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3793 MAXIMUM_ALLOWED_ACCESS,
3794 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3796 if (!NT_STATUS_IS_OK(result))
3803 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3809 for (i = 0; i < num_entries; i++) {
3811 POLICY_HND alias_pol;
3812 struct full_alias alias;
3813 struct lsa_SidArray sid_array;
3816 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3818 MAXIMUM_ALLOWED_ACCESS,
3819 groups->entries[i].idx,
3821 if (!NT_STATUS_IS_OK(result))
3824 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3827 if (!NT_STATUS_IS_OK(result))
3830 alias.num_members = sid_array.num_sids;
3832 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3833 if (!NT_STATUS_IS_OK(result))
3836 alias.members = NULL;
3838 if (alias.num_members > 0) {
3839 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3841 for (j = 0; j < alias.num_members; j++)
3842 sid_copy(&alias.members[j],
3843 sid_array.sids[j].sid);
3846 sid_copy(&alias.sid, domain_sid);
3847 sid_append_rid(&alias.sid, groups->entries[i].idx);
3849 push_alias(mem_ctx, &alias);
3851 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3853 result = NT_STATUS_OK;
3856 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3862 * Dump server_aliases as names for debugging purposes.
3865 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
3866 const DOM_SID *domain_sid,
3867 const char *domain_name,
3868 struct cli_state *cli,
3869 struct rpc_pipe_client *pipe_hnd,
3870 TALLOC_CTX *mem_ctx,
3878 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
3879 SEC_RIGHTS_MAXIMUM_ALLOWED,
3881 if (!NT_STATUS_IS_OK(result))
3884 for (i=0; i<num_server_aliases; i++) {
3887 enum lsa_SidType *types;
3890 struct full_alias *alias = &server_aliases[i];
3892 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3894 &domains, &names, &types);
3895 if (!NT_STATUS_IS_OK(result))
3898 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3900 if (alias->num_members == 0) {
3905 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
3908 &domains, &names, &types);
3910 if (!NT_STATUS_IS_OK(result) &&
3911 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3914 for (j=0; j<alias->num_members; j++)
3915 DEBUG(1, ("%s\\%s (%d); ",
3916 domains[j] ? domains[j] : "*unknown*",
3917 names[j] ? names[j] : "*unknown*",types[j]));
3921 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
3923 return NT_STATUS_OK;
3927 * Fetch a list of all server aliases and their members into
3931 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
3932 const DOM_SID *domain_sid,
3933 const char *domain_name,
3934 struct cli_state *cli,
3935 struct rpc_pipe_client *pipe_hnd,
3936 TALLOC_CTX *mem_ctx,
3941 POLICY_HND connect_pol;
3943 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3945 MAXIMUM_ALLOWED_ACCESS,
3948 if (!NT_STATUS_IS_OK(result))
3951 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3952 &global_sid_Builtin);
3954 if (!NT_STATUS_IS_OK(result))
3957 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3960 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
3965 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
3967 token->num_sids = 4;
3969 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
3970 d_fprintf(stderr, "malloc failed\n");
3971 token->num_sids = 0;
3975 token->user_sids[0] = *user_sid;
3976 sid_copy(&token->user_sids[1], &global_sid_World);
3977 sid_copy(&token->user_sids[2], &global_sid_Network);
3978 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
3981 static void free_user_token(NT_USER_TOKEN *token)
3983 SAFE_FREE(token->user_sids);
3986 static bool is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
3990 for (i=0; i<token->num_sids; i++) {
3991 if (sid_compare(sid, &token->user_sids[i]) == 0)
3997 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
3999 if (is_sid_in_token(token, sid))
4002 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4003 if (!token->user_sids) {
4007 sid_copy(&token->user_sids[token->num_sids], sid);
4009 token->num_sids += 1;
4014 NT_USER_TOKEN token;
4017 static void dump_user_token(struct user_token *token)
4021 d_printf("%s\n", token->name);
4023 for (i=0; i<token->token.num_sids; i++) {
4024 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4028 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4032 for (i=0; i<alias->num_members; i++) {
4033 if (sid_compare(sid, &alias->members[i]) == 0)
4040 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4044 for (i=0; i<num_server_aliases; i++) {
4045 if (is_alias_member(&sid, &server_aliases[i]))
4046 add_sid_to_token(token, &server_aliases[i].sid);
4051 * We got a user token with all the SIDs we can know about without asking the
4052 * server directly. These are the user and domain group sids. All of these can
4053 * be members of aliases. So scan the list of aliases for each of the SIDs and
4054 * add them to the token.
4057 static void collect_alias_memberships(NT_USER_TOKEN *token)
4059 int num_global_sids = token->num_sids;
4062 for (i=0; i<num_global_sids; i++) {
4063 collect_sid_memberships(token, token->user_sids[i]);
4067 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4069 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4070 enum wbcSidType type;
4072 struct wbcDomainSid wsid;
4073 char *sid_str = NULL;
4075 uint32_t num_groups;
4076 gid_t *groups = NULL;
4079 fstr_sprintf(full_name, "%s%c%s",
4080 domain, *lp_winbind_separator(), user);
4082 /* First let's find out the user sid */
4084 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4086 if (!WBC_ERROR_IS_OK(wbc_status)) {
4087 DEBUG(1, ("winbind could not find %s: %s\n",
4088 full_name, wbcErrorString(wbc_status)));
4092 wbc_status = wbcSidToString(&wsid, &sid_str);
4093 if (!WBC_ERROR_IS_OK(wbc_status)) {
4097 if (type != SID_NAME_USER) {
4098 wbcFreeMemory(sid_str);
4099 DEBUG(1, ("%s is not a user\n", full_name));
4103 string_to_sid(&user_sid, sid_str);
4104 wbcFreeMemory(sid_str);
4107 init_user_token(token, &user_sid);
4109 /* And now the groups winbind knows about */
4111 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4112 if (!WBC_ERROR_IS_OK(wbc_status)) {
4113 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4114 full_name, wbcErrorString(wbc_status)));
4118 for (i = 0; i < num_groups; i++) {
4119 gid_t gid = groups[i];
4122 wbc_status = wbcGidToSid(gid, &wsid);
4123 if (!WBC_ERROR_IS_OK(wbc_status)) {
4124 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4125 gid, wbcErrorString(wbc_status)));
4126 wbcFreeMemory(groups);
4130 wbc_status = wbcSidToString(&wsid, &sid_str);
4131 if (!WBC_ERROR_IS_OK(wbc_status)) {
4132 wbcFreeMemory(groups);
4136 DEBUG(3, (" %s\n", sid_str));
4138 string_to_sid(&sid, sid_str);
4139 wbcFreeMemory(sid_str);
4142 add_sid_to_token(token, &sid);
4144 wbcFreeMemory(groups);
4150 * Get a list of all user tokens we want to look at
4153 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4154 struct user_token **user_tokens)
4156 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4157 uint32_t i, num_users;
4159 struct user_token *result;
4160 TALLOC_CTX *frame = NULL;
4162 if (lp_winbind_use_default_domain() &&
4163 (c->opt_target_workgroup == NULL)) {
4164 d_fprintf(stderr, "winbind use default domain = yes set, "
4165 "please specify a workgroup\n");
4169 /* Send request to winbind daemon */
4171 wbc_status = wbcListUsers(NULL, &num_users, &users);
4172 if (!WBC_ERROR_IS_OK(wbc_status)) {
4173 DEBUG(1, ("winbind could not list users: %s\n",
4174 wbcErrorString(wbc_status)));
4178 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4180 if (result == NULL) {
4181 DEBUG(1, ("Could not malloc sid array\n"));
4182 wbcFreeMemory(users);
4186 frame = talloc_stackframe();
4187 for (i=0; i < num_users; i++) {
4188 fstring domain, user;
4191 fstrcpy(result[i].name, users[i]);
4193 p = strchr(users[i], *lp_winbind_separator());
4195 DEBUG(3, ("%s\n", users[i]));
4198 fstrcpy(domain, c->opt_target_workgroup);
4199 fstrcpy(user, users[i]);
4202 fstrcpy(domain, users[i]);
4207 get_user_sids(domain, user, &(result[i].token));
4211 wbcFreeMemory(users);
4213 *num_tokens = num_users;
4214 *user_tokens = result;
4219 static bool get_user_tokens_from_file(FILE *f,
4221 struct user_token **tokens)
4223 struct user_token *token = NULL;
4228 if (fgets(line, sizeof(line)-1, f) == NULL) {
4232 if (line[strlen(line)-1] == '\n')
4233 line[strlen(line)-1] = '\0';
4235 if (line[0] == ' ') {
4239 string_to_sid(&sid, &line[1]);
4241 if (token == NULL) {
4242 DEBUG(0, ("File does not begin with username"));
4246 add_sid_to_token(&token->token, &sid);
4250 /* And a new user... */
4253 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4254 if (*tokens == NULL) {
4255 DEBUG(0, ("Could not realloc tokens\n"));
4259 token = &((*tokens)[*num_tokens-1]);
4261 fstrcpy(token->name, line);
4262 token->token.num_sids = 0;
4263 token->token.user_sids = NULL;
4272 * Show the list of all users that have access to a share
4275 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4276 TALLOC_CTX *mem_ctx,
4277 const char *netname,
4279 struct user_token *tokens)
4282 SEC_DESC *share_sd = NULL;
4283 SEC_DESC *root_sd = NULL;
4284 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4286 union srvsvc_NetShareInfo info;
4291 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4298 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4299 DEBUG(1, ("Coult not query secdesc for share %s\n",
4304 share_sd = info.info502->sd_buf.sd;
4305 if (share_sd == NULL) {
4306 DEBUG(1, ("Got no secdesc for share %s\n",
4312 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4316 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4319 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4322 for (i=0; i<num_tokens; i++) {
4325 if (share_sd != NULL) {
4326 if (!se_access_check(share_sd, &tokens[i].token,
4327 1, &acc_granted, &status)) {
4328 DEBUG(1, ("Could not check share_sd for "
4334 if (!NT_STATUS_IS_OK(status))
4338 if (root_sd == NULL) {
4339 d_printf(" %s\n", tokens[i].name);
4343 if (!se_access_check(root_sd, &tokens[i].token,
4344 1, &acc_granted, &status)) {
4345 DEBUG(1, ("Could not check root_sd for user %s\n",
4350 if (!NT_STATUS_IS_OK(status))
4353 d_printf(" %s\n", tokens[i].name);
4357 cli_close(cli, fnum);
4369 static void collect_share(const char *name, uint32 m,
4370 const char *comment, void *state)
4372 struct share_list *share_list = (struct share_list *)state;
4374 if (m != STYPE_DISKTREE)
4377 share_list->num_shares += 1;
4378 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4379 if (!share_list->shares) {
4380 share_list->num_shares = 0;
4383 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4387 * List shares on a remote RPC server, including the security descriptors.
4389 * All parameters are provided by the run_rpc_command function, except for
4390 * argc, argv which are passed through.
4392 * @param domain_sid The domain sid acquired from the remote server.
4393 * @param cli A cli_state connected to the server.
4394 * @param mem_ctx Talloc context, destroyed on completion of the function.
4395 * @param argc Standard main() style argc.
4396 * @param argv Standard main() style argv. Initial components are already
4399 * @return Normal NTSTATUS return.
4402 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4403 const DOM_SID *domain_sid,
4404 const char *domain_name,
4405 struct cli_state *cli,
4406 struct rpc_pipe_client *pipe_hnd,
4407 TALLOC_CTX *mem_ctx,
4417 struct user_token *tokens = NULL;
4420 struct share_list share_list;
4425 f = fopen(argv[0], "r");
4429 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4430 return NT_STATUS_UNSUCCESSFUL;
4433 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4439 DEBUG(0, ("Could not read users from file\n"));
4440 return NT_STATUS_UNSUCCESSFUL;
4443 for (i=0; i<num_tokens; i++)
4444 collect_alias_memberships(&tokens[i].token);
4446 init_enum_hnd(&hnd, 0);
4448 share_list.num_shares = 0;
4449 share_list.shares = NULL;
4451 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4454 DEBUG(0, ("Error returning browse list: %s\n",
4459 for (i = 0; i < share_list.num_shares; i++) {
4460 char *netname = share_list.shares[i];
4462 if (netname[strlen(netname)-1] == '$')
4465 d_printf("%s\n", netname);
4467 show_userlist(pipe_hnd, mem_ctx, netname,
4468 num_tokens, tokens);
4471 for (i=0; i<num_tokens; i++) {
4472 free_user_token(&tokens[i].token);
4475 SAFE_FREE(share_list.shares);
4477 return NT_STATUS_OK;
4480 static int rpc_share_allowedusers(struct net_context *c, int argc,
4485 if (c->display_usage) {
4487 "net rpc share allowedusers\n"
4488 " List allowed users\n");
4492 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4493 rpc_aliaslist_internals,
4498 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4504 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4505 rpc_share_allowedusers_internals,
4509 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4512 struct user_token *tokens = NULL;
4516 net_usersidlist_usage(c, argc, argv);
4520 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4521 DEBUG(0, ("Could not get the user/sid list\n"));
4525 for (i=0; i<num_tokens; i++) {
4526 dump_user_token(&tokens[i]);
4527 free_user_token(&tokens[i].token);
4534 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4536 d_printf("net usersidlist\n"
4537 "\tprints out a list of all users the running winbind knows\n"
4538 "\tabout, together with all their SIDs. This is used as\n"
4539 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4541 net_common_flags_usage(c, argc, argv);
4546 * 'net rpc share' entrypoint.
4547 * @param argc Standard main() style argc.
4548 * @param argv Standard main() style argv. Initial components are already
4552 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4554 NET_API_STATUS status;
4556 struct functable func[] = {
4562 "net rpc share add\n"
4570 "net rpc share delete\n"
4575 rpc_share_allowedusers,
4577 "Modify allowed users",
4578 "net rpc share allowedusers\n"
4579 " Modify allowed users"
4585 "Migrate share to local server",
4586 "net rpc share migrate\n"
4587 " Migrate share to local server"
4594 "net rpc share list\n"
4597 {NULL, NULL, 0, NULL, NULL}
4600 status = libnetapi_init(&c->netapi_ctx);
4604 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4605 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4606 if (c->opt_kerberos) {
4607 libnetapi_set_use_kerberos(c->netapi_ctx);
4611 if (c->display_usage) {
4615 " Alias for net rpc share list\n");
4616 net_display_usage_from_functable(func);
4620 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4621 rpc_share_list_internals,
4625 return net_run_function(c, argc, argv, "net rpc share", func);
4628 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4629 TALLOC_CTX *mem_ctx,
4630 struct rpc_sh_ctx *ctx,
4631 struct rpc_pipe_client *pipe_hnd,
4632 int argc, const char **argv)
4634 return rpc_share_list_internals(c, ctx->domain_sid, ctx->domain_name,
4635 ctx->cli, pipe_hnd, mem_ctx,
4639 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4640 TALLOC_CTX *mem_ctx,
4641 struct rpc_sh_ctx *ctx,
4642 struct rpc_pipe_client *pipe_hnd,
4643 int argc, const char **argv)
4645 NET_API_STATUS status;
4646 uint32_t parm_err = 0;
4647 struct SHARE_INFO_2 i2;
4649 if ((argc < 2) || (argc > 3)) {
4650 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4652 return NT_STATUS_INVALID_PARAMETER;
4655 i2.shi2_netname = argv[0];
4656 i2.shi2_type = STYPE_DISKTREE;
4657 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4658 i2.shi2_permissions = 0;
4659 i2.shi2_max_uses = 0;
4660 i2.shi2_current_uses = 0;
4661 i2.shi2_path = argv[1];
4662 i2.shi2_passwd = NULL;
4664 status = NetShareAdd(pipe_hnd->desthost,
4669 return werror_to_ntstatus(W_ERROR(status));
4672 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4673 TALLOC_CTX *mem_ctx,
4674 struct rpc_sh_ctx *ctx,
4675 struct rpc_pipe_client *pipe_hnd,
4676 int argc, const char **argv)
4679 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4680 return NT_STATUS_INVALID_PARAMETER;
4683 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4686 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4687 TALLOC_CTX *mem_ctx,
4688 struct rpc_sh_ctx *ctx,
4689 struct rpc_pipe_client *pipe_hnd,
4690 int argc, const char **argv)
4692 union srvsvc_NetShareInfo info;
4697 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4698 return NT_STATUS_INVALID_PARAMETER;
4701 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4707 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4711 d_printf("Name: %s\n", info.info2->name);
4712 d_printf("Comment: %s\n", info.info2->comment);
4713 d_printf("Path: %s\n", info.info2->path);
4714 d_printf("Password: %s\n", info.info2->password);
4717 return werror_to_ntstatus(result);
4720 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4721 struct rpc_sh_ctx *ctx)
4723 static struct rpc_sh_cmd cmds[] = {
4725 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4726 "List available shares" },
4728 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4731 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4734 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4735 "Get information about a share" },
4737 { NULL, NULL, 0, NULL, NULL }
4743 /****************************************************************************/
4745 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4747 return net_file_usage(c, argc, argv);
4751 * Close a file on a remote RPC server.
4753 * All parameters are provided by the run_rpc_command function, except for
4754 * argc, argv which are passed through.
4756 * @param c A net_context structure.
4757 * @param domain_sid The domain sid acquired from the remote server.
4758 * @param cli A cli_state connected to the server.
4759 * @param mem_ctx Talloc context, destroyed on completion of the function.
4760 * @param argc Standard main() style argc.
4761 * @param argv Standard main() style argv. Initial components are already
4764 * @return Normal NTSTATUS return.
4766 static NTSTATUS rpc_file_close_internals(struct net_context *c,
4767 const DOM_SID *domain_sid,
4768 const char *domain_name,
4769 struct cli_state *cli,
4770 struct rpc_pipe_client *pipe_hnd,
4771 TALLOC_CTX *mem_ctx,
4775 return rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx,
4777 atoi(argv[0]), NULL);
4781 * Close a file on a remote RPC server.
4783 * @param argc Standard main() style argc.
4784 * @param argv Standard main() style argv. Initial components are already
4787 * @return A shell status integer (0 for success).
4789 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4791 if (argc < 1 || c->display_usage) {
4792 return rpc_file_usage(c, argc, argv);
4795 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4796 rpc_file_close_internals,
4801 * Formatted print of open file info
4803 * @param r struct srvsvc_NetFileInfo3 contents
4806 static void display_file_info_3(struct srvsvc_NetFileInfo3 *r)
4808 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4809 r->fid, r->user, r->permissions, r->num_locks, r->path);
4813 * List open files on a remote RPC server.
4815 * All parameters are provided by the run_rpc_command function, except for
4816 * argc, argv which are passed through.
4818 * @param c A net_context structure.
4819 * @param domain_sid The domain sid acquired from the remote server.
4820 * @param cli A cli_state connected to the server.
4821 * @param mem_ctx Talloc context, destroyed on completion of the function.
4822 * @param argc Standard main() style argc.
4823 * @param argv Standard main() style argv. Initial components are already
4826 * @return Normal NTSTATUS return.
4829 static NTSTATUS rpc_file_list_internals(struct net_context *c,
4830 const DOM_SID *domain_sid,
4831 const char *domain_name,
4832 struct cli_state *cli,
4833 struct rpc_pipe_client *pipe_hnd,
4834 TALLOC_CTX *mem_ctx,
4838 struct srvsvc_NetFileInfoCtr info_ctr;
4839 struct srvsvc_NetFileCtr3 ctr3;
4842 uint32 preferred_len = 0xffffffff, i;
4843 const char *username=NULL;
4844 uint32_t total_entries = 0;
4845 uint32_t resume_handle = 0;
4847 /* if argc > 0, must be user command */
4849 username = smb_xstrdup(argv[0]);
4851 ZERO_STRUCT(info_ctr);
4855 info_ctr.ctr.ctr3 = &ctr3;
4857 status = rpccli_srvsvc_NetFileEnum(pipe_hnd, mem_ctx,
4867 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
4870 /* Display results */
4873 "\nEnumerating open files on remote server:\n\n"
4874 "\nFileId Opened by Perms Locks Path"
4875 "\n------ --------- ----- ----- ---- \n");
4876 for (i = 0; i < total_entries; i++)
4877 display_file_info_3(&info_ctr.ctr.ctr3->array[i]);
4879 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4883 * List files for a user on a remote RPC server.
4885 * @param argc Standard main() style argc.
4886 * @param argv Standard main() style argv. Initial components are already
4889 * @return A shell status integer (0 for success)..
4892 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4894 if (argc < 1 || c->display_usage) {
4895 return rpc_file_usage(c, argc, argv);
4898 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4899 rpc_file_list_internals,
4904 * 'net rpc file' entrypoint.
4905 * @param argc Standard main() style argc.
4906 * @param argv Standard main() style argv. Initial components are already
4910 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4912 struct functable func[] = {
4917 "Close opened file",
4918 "net rpc file close\n"
4919 " Close opened file"
4925 "List files opened by user",
4926 "net rpc file user\n"
4927 " List files opened by user"
4934 "Display information about opened file",
4935 "net rpc file info\n"
4936 " Display information about opened file"
4939 {NULL, NULL, 0, NULL, NULL}
4943 if (c->display_usage) {
4944 d_printf("Usage:\n");
4945 d_printf("net rpc file\n"
4946 " List opened files\n");
4947 net_display_usage_from_functable(func);
4951 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4952 rpc_file_list_internals,
4956 return net_run_function(c, argc, argv, "net rpc file", func);
4960 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
4962 * All parameters are provided by the run_rpc_command function, except for
4963 * argc, argv which are passed through.
4965 * @param c A net_context structure.
4966 * @param domain_sid The domain sid acquired from the remote server.
4967 * @param cli A cli_state connected to the server.
4968 * @param mem_ctx Talloc context, destroyed on completion of the function.
4969 * @param argc Standard main() style argc.
4970 * @param argv Standard main() style argv. Initial components are already
4973 * @return Normal NTSTATUS return.
4976 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
4977 const DOM_SID *domain_sid,
4978 const char *domain_name,
4979 struct cli_state *cli,
4980 struct rpc_pipe_client *pipe_hnd,
4981 TALLOC_CTX *mem_ctx,
4985 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4987 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
4989 if (NT_STATUS_IS_OK(result)) {
4990 d_printf("\nShutdown successfully aborted\n");
4991 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4993 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4999 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5001 * All parameters are provided by the run_rpc_command function, except for
5002 * argc, argv which are passed through.
5004 * @param c A net_context structure.
5005 * @param domain_sid The domain sid acquired from the remote server.
5006 * @param cli A cli_state connected to the server.
5007 * @param mem_ctx Talloc context, destroyed on completion of the function.
5008 * @param argc Standard main() style argc.
5009 * @param argv Standard main() style argv. Initial components are already
5012 * @return Normal NTSTATUS return.
5015 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5016 const DOM_SID *domain_sid,
5017 const char *domain_name,
5018 struct cli_state *cli,
5019 struct rpc_pipe_client *pipe_hnd,
5020 TALLOC_CTX *mem_ctx,
5024 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5026 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5028 if (NT_STATUS_IS_OK(result)) {
5029 d_printf("\nShutdown successfully aborted\n");
5030 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5032 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5038 * ABORT the shutdown of a remote RPC server.
5040 * @param argc Standard main() style argc.
5041 * @param argv Standard main() style argv. Initial components are already
5044 * @return A shell status integer (0 for success).
5047 static int rpc_shutdown_abort(struct net_context *c, int argc,
5052 if (c->display_usage) {
5054 "net rpc abortshutdown\n"
5055 " Abort a scheduled shutdown\n");
5059 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5060 rpc_shutdown_abort_internals, argc, argv);
5065 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5067 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5068 rpc_reg_shutdown_abort_internals,
5073 * Shut down a remote RPC Server via initshutdown pipe.
5075 * All parameters are provided by the run_rpc_command function, except for
5076 * argc, argv which are passed through.
5078 * @param c A net_context structure.
5079 * @param domain_sid The domain sid acquired from the remote server.
5080 * @param cli A cli_state connected to the server.
5081 * @param mem_ctx Talloc context, destroyed on completion of the function.
5082 * @param argc Standard main() style argc.
5083 * @param argv Standard main() style argv. Initial components are already
5086 * @return Normal NTSTATUS return.
5089 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5090 const DOM_SID *domain_sid,
5091 const char *domain_name,
5092 struct cli_state *cli,
5093 struct rpc_pipe_client *pipe_hnd,
5094 TALLOC_CTX *mem_ctx,
5098 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5099 const char *msg = "This machine will be shutdown shortly";
5100 uint32 timeout = 20;
5101 struct initshutdown_String msg_string;
5102 struct initshutdown_String_sub s;
5104 if (c->opt_comment) {
5105 msg = c->opt_comment;
5107 if (c->opt_timeout) {
5108 timeout = c->opt_timeout;
5112 msg_string.name = &s;
5114 /* create an entry */
5115 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5116 &msg_string, timeout, c->opt_force, c->opt_reboot,
5119 if (NT_STATUS_IS_OK(result)) {
5120 d_printf("\nShutdown of remote machine succeeded\n");
5121 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5123 DEBUG(1,("Shutdown of remote machine failed!\n"));
5129 * Shut down a remote RPC Server via winreg pipe.
5131 * All parameters are provided by the run_rpc_command function, except for
5132 * argc, argv which are passed through.
5134 * @param c A net_context structure.
5135 * @param domain_sid The domain sid acquired from the remote server.
5136 * @param cli A cli_state connected to the server.
5137 * @param mem_ctx Talloc context, destroyed on completion of the function.
5138 * @param argc Standard main() style argc.
5139 * @param argv Standard main() style argv. Initial components are already
5142 * @return Normal NTSTATUS return.
5145 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5146 const DOM_SID *domain_sid,
5147 const char *domain_name,
5148 struct cli_state *cli,
5149 struct rpc_pipe_client *pipe_hnd,
5150 TALLOC_CTX *mem_ctx,
5154 const char *msg = "This machine will be shutdown shortly";
5155 uint32 timeout = 20;
5156 struct initshutdown_String msg_string;
5157 struct initshutdown_String_sub s;
5161 if (c->opt_comment) {
5162 msg = c->opt_comment;
5165 msg_string.name = &s;
5167 if (c->opt_timeout) {
5168 timeout = c->opt_timeout;
5171 /* create an entry */
5172 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5173 &msg_string, timeout, c->opt_force, c->opt_reboot,
5176 if (NT_STATUS_IS_OK(result)) {
5177 d_printf("\nShutdown of remote machine succeeded\n");
5179 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5180 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5181 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5183 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(werr));
5190 * Shut down a remote RPC server.
5192 * @param argc Standard main() style argc.
5193 * @param argv Standard main() style argv. Initial components are already
5196 * @return A shell status integer (0 for success).
5199 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5203 if (c->display_usage) {
5205 "net rpc shutdown\n"
5206 " Shut down a remote RPC server\n");
5210 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5211 rpc_init_shutdown_internals, argc, argv);
5214 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5215 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5216 rpc_reg_shutdown_internals, argc, argv);
5222 /***************************************************************************
5223 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5224 ***************************************************************************/
5227 * Add interdomain trust account to the RPC server.
5228 * All parameters (except for argc and argv) are passed by run_rpc_command
5231 * @param c A net_context structure.
5232 * @param domain_sid The domain sid acquired from the server.
5233 * @param cli A cli_state connected to the server.
5234 * @param mem_ctx Talloc context, destroyed on completion of the function.
5235 * @param argc Standard main() style argc.
5236 * @param argv Standard main() style argv. Initial components are already
5239 * @return normal NTSTATUS return code.
5242 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5243 const DOM_SID *domain_sid,
5244 const char *domain_name,
5245 struct cli_state *cli,
5246 struct rpc_pipe_client *pipe_hnd,
5247 TALLOC_CTX *mem_ctx,
5251 POLICY_HND connect_pol, domain_pol, user_pol;
5252 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5254 struct lsa_String lsa_acct_name;
5256 uint32 acct_flags=0;
5258 uint32_t access_granted = 0;
5259 union samr_UserInfo info;
5260 unsigned int orig_timeout;
5263 d_printf("Usage: net rpc trustdom add <domain_name> "
5264 "<trust password>\n");
5265 return NT_STATUS_INVALID_PARAMETER;
5269 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5272 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5273 return NT_STATUS_NO_MEMORY;
5276 strupper_m(acct_name);
5278 init_lsa_String(&lsa_acct_name, acct_name);
5280 /* Get samr policy handle */
5281 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5283 MAXIMUM_ALLOWED_ACCESS,
5285 if (!NT_STATUS_IS_OK(result)) {
5289 /* Get domain policy handle */
5290 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5292 MAXIMUM_ALLOWED_ACCESS,
5293 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5295 if (!NT_STATUS_IS_OK(result)) {
5299 /* This call can take a long time - allow the server to time out.
5300 * 35 seconds should do it. */
5302 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5304 /* Create trusting domain's account */
5305 acb_info = ACB_NORMAL;
5306 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5307 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5308 SAMR_USER_ACCESS_SET_PASSWORD |
5309 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5310 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5312 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5321 /* And restore our original timeout. */
5322 rpccli_set_timeout(pipe_hnd, orig_timeout);
5324 if (!NT_STATUS_IS_OK(result)) {
5325 d_printf("net rpc trustdom add: create user %s failed %s\n",
5326 acct_name, nt_errstr(result));
5332 struct samr_LogonHours hours;
5333 struct lsa_BinaryString parameters;
5334 const int units_per_week = 168;
5335 struct samr_CryptPassword crypt_pwd;
5337 ZERO_STRUCT(notime);
5339 ZERO_STRUCT(parameters);
5341 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
5343 result = NT_STATUS_NO_MEMORY;
5346 hours.units_per_week = units_per_week;
5347 memset(hours.bits, 0xFF, units_per_week);
5349 init_samr_CryptPassword(argv[1],
5350 &cli->user_session_key,
5353 init_samr_user_info23(&info.info23,
5354 notime, notime, notime,
5355 notime, notime, notime,
5356 NULL, NULL, NULL, NULL, NULL,
5357 NULL, NULL, NULL, NULL, ¶meters,
5358 0, 0, ACB_DOMTRUST, SAMR_FIELD_ACCT_FLAGS,
5360 0, 0, 0, 0, 0, 0, 0,
5361 crypt_pwd.data, 24);
5363 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5368 if (!NT_STATUS_IS_OK(result)) {
5369 DEBUG(0,("Could not set trust account password: %s\n",
5370 nt_errstr(result)));
5376 SAFE_FREE(acct_name);
5381 * Create interdomain trust account for a remote domain.
5383 * @param argc Standard argc.
5384 * @param argv Standard argv without initial components.
5386 * @return Integer status (0 means success).
5389 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5391 if (argc > 0 && !c->display_usage) {
5392 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5393 rpc_trustdom_add_internals, argc, argv);
5396 "net rpc trustdom add <domain_name> <trust password>\n");
5403 * Remove interdomain trust account from the RPC server.
5404 * All parameters (except for argc and argv) are passed by run_rpc_command
5407 * @param c A net_context structure.
5408 * @param domain_sid The domain sid acquired from the server.
5409 * @param cli A cli_state connected to the server.
5410 * @param mem_ctx Talloc context, destroyed on completion of the function.
5411 * @param argc Standard main() style argc.
5412 * @param argv Standard main() style argv. Initial components are already
5415 * @return normal NTSTATUS return code.
5418 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5419 const DOM_SID *domain_sid,
5420 const char *domain_name,
5421 struct cli_state *cli,
5422 struct rpc_pipe_client *pipe_hnd,
5423 TALLOC_CTX *mem_ctx,
5427 POLICY_HND connect_pol, domain_pol, user_pol;
5428 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5430 DOM_SID trust_acct_sid;
5431 struct samr_Ids user_rids, name_types;
5432 struct lsa_String lsa_acct_name;
5435 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5436 return NT_STATUS_INVALID_PARAMETER;
5440 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5442 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5444 if (acct_name == NULL)
5445 return NT_STATUS_NO_MEMORY;
5447 strupper_m(acct_name);
5449 /* Get samr policy handle */
5450 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5452 MAXIMUM_ALLOWED_ACCESS,
5454 if (!NT_STATUS_IS_OK(result)) {
5458 /* Get domain policy handle */
5459 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5461 MAXIMUM_ALLOWED_ACCESS,
5462 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5464 if (!NT_STATUS_IS_OK(result)) {
5468 init_lsa_String(&lsa_acct_name, acct_name);
5470 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5477 if (!NT_STATUS_IS_OK(result)) {
5478 d_printf("net rpc trustdom del: LookupNames on user %s failed %s\n",
5479 acct_name, nt_errstr(result) );
5483 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5485 MAXIMUM_ALLOWED_ACCESS,
5489 if (!NT_STATUS_IS_OK(result)) {
5490 d_printf("net rpc trustdom del: OpenUser on user %s failed %s\n",
5491 acct_name, nt_errstr(result) );
5495 /* append the rid to the domain sid */
5496 sid_copy(&trust_acct_sid, domain_sid);
5497 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5501 /* remove the sid */
5503 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5506 if (!NT_STATUS_IS_OK(result)) {
5507 d_printf("net rpc trustdom del: RemoveMemberFromForeignDomain on user %s failed %s\n",
5508 acct_name, nt_errstr(result) );
5514 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5517 if (!NT_STATUS_IS_OK(result)) {
5518 d_printf("net rpc trustdom del: DeleteUser on user %s failed %s\n",
5519 acct_name, nt_errstr(result) );
5523 if (!NT_STATUS_IS_OK(result)) {
5524 d_printf("Could not set trust account password: %s\n",
5534 * Delete interdomain trust account for a remote domain.
5536 * @param argc Standard argc.
5537 * @param argv Standard argv without initial components.
5539 * @return Integer status (0 means success).
5542 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5544 if (argc > 0 && !c->display_usage) {
5545 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5546 rpc_trustdom_del_internals, argc, argv);
5549 "net rpc trustdom del <domain>\n");
5554 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5555 struct cli_state *cli,
5556 TALLOC_CTX *mem_ctx,
5557 const char *domain_name)
5559 char *dc_name = NULL;
5560 const char *buffer = NULL;
5561 struct rpc_pipe_client *netr;
5564 /* Use NetServerEnum2 */
5566 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5568 return NT_STATUS_OK;
5571 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5572 for domain %s\n", domain_name));
5574 /* Try netr_GetDcName */
5576 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5578 if (!NT_STATUS_IS_OK(status)) {
5582 status = rpccli_netr_GetDcName(netr, mem_ctx,
5589 if (NT_STATUS_IS_OK(status)) {
5593 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5594 for domain %s\n", domain_name));
5600 * Establish trust relationship to a trusting domain.
5601 * Interdomain account must already be created on remote PDC.
5603 * @param c A net_context structure.
5604 * @param argc Standard argc.
5605 * @param argv Standard argv without initial components.
5607 * @return Integer status (0 means success).
5610 static int rpc_trustdom_establish(struct net_context *c, int argc,
5613 struct cli_state *cli = NULL;
5614 struct sockaddr_storage server_ss;
5615 struct rpc_pipe_client *pipe_hnd = NULL;
5616 POLICY_HND connect_hnd;
5617 TALLOC_CTX *mem_ctx;
5619 DOM_SID *domain_sid;
5624 union lsa_PolicyInformation *info = NULL;
5627 * Connect to \\server\ipc$ as 'our domain' account with password
5630 if (argc != 1 || c->display_usage) {
5632 "net rpc trustdom establish <domain_name>\n");
5636 domain_name = smb_xstrdup(argv[0]);
5637 strupper_m(domain_name);
5639 /* account name used at first is our domain's name with '$' */
5640 asprintf(&acct_name, "%s$", lp_workgroup());
5641 strupper_m(acct_name);
5644 * opt_workgroup will be used by connection functions further,
5645 * hence it should be set to remote domain name instead of ours
5647 if (c->opt_workgroup) {
5648 c->opt_workgroup = smb_xstrdup(domain_name);
5651 c->opt_user_name = acct_name;
5653 /* find the domain controller */
5654 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5655 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5659 /* connect to ipc$ as username/password */
5660 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5661 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5663 /* Is it trusting domain account for sure ? */
5664 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5665 nt_errstr(nt_status)));
5669 /* store who we connected to */
5671 saf_store( domain_name, pdc_name );
5674 * Connect to \\server\ipc$ again (this time anonymously)
5677 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5680 if (NT_STATUS_IS_ERR(nt_status)) {
5681 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5682 domain_name, nt_errstr(nt_status)));
5686 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5687 "domain %s", domain_name))) {
5688 DEBUG(0, ("talloc_init() failed\n"));
5693 /* Make sure we're talking to a proper server */
5695 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5696 if (!NT_STATUS_IS_OK(nt_status)) {
5698 talloc_destroy(mem_ctx);
5703 * Call LsaOpenPolicy and LsaQueryInfo
5706 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5708 if (!NT_STATUS_IS_OK(nt_status)) {
5709 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5711 talloc_destroy(mem_ctx);
5715 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, SEC_RIGHTS_QUERY_VALUE,
5717 if (NT_STATUS_IS_ERR(nt_status)) {
5718 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5719 nt_errstr(nt_status)));
5721 talloc_destroy(mem_ctx);
5725 /* Querying info level 5 */
5727 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5729 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5731 if (NT_STATUS_IS_ERR(nt_status)) {
5732 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5733 nt_errstr(nt_status)));
5735 talloc_destroy(mem_ctx);
5739 domain_sid = info->account_domain.sid;
5741 /* There should be actually query info level 3 (following nt serv behaviour),
5742 but I still don't know if it's _really_ necessary */
5745 * Store the password in secrets db
5748 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5749 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5751 talloc_destroy(mem_ctx);
5756 * Close the pipes and clean up
5759 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5760 if (NT_STATUS_IS_ERR(nt_status)) {
5761 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5762 nt_errstr(nt_status)));
5764 talloc_destroy(mem_ctx);
5770 talloc_destroy(mem_ctx);
5772 d_printf("Trust to domain %s established\n", domain_name);
5777 * Revoke trust relationship to the remote domain.
5779 * @param c A net_context structure.
5780 * @param argc Standard argc.
5781 * @param argv Standard argv without initial components.
5783 * @return Integer status (0 means success).
5786 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5792 if (argc < 1 || c->display_usage) {
5794 "net rpc trustdom revoke <domain_name>\n"
5795 " Revoke trust relationship\n"
5796 " domain_name\tName of domain to revoke trust\n");
5800 /* generate upper cased domain name */
5801 domain_name = smb_xstrdup(argv[0]);
5802 strupper_m(domain_name);
5804 /* delete password of the trust */
5805 if (!pdb_del_trusteddom_pw(domain_name)) {
5806 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5813 SAFE_FREE(domain_name);
5817 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5818 const DOM_SID *domain_sid,
5819 const char *domain_name,
5820 struct cli_state *cli,
5821 struct rpc_pipe_client *pipe_hnd,
5822 TALLOC_CTX *mem_ctx,
5827 sid_to_fstring(str_sid, domain_sid);
5828 d_printf("%s\n", str_sid);
5829 return NT_STATUS_OK;
5832 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5834 fstring ascii_sid, padding;
5835 int pad_len, col_len = 20;
5837 /* convert sid into ascii string */
5838 sid_to_fstring(ascii_sid, dom_sid);
5840 /* calculate padding space for d_printf to look nicer */
5841 pad_len = col_len - strlen(trusted_dom_name);
5842 padding[pad_len] = 0;
5843 do padding[--pad_len] = ' '; while (pad_len);
5845 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5848 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5849 TALLOC_CTX *mem_ctx,
5852 const char *trusted_dom_name)
5855 union lsa_TrustedDomainInfo *info = NULL;
5856 char *cleartextpwd = NULL;
5857 uint8_t nt_hash[16];
5860 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5863 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5865 if (NT_STATUS_IS_ERR(nt_status)) {
5866 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5867 nt_errstr(nt_status)));
5871 data = data_blob(info->password.password->data,
5872 info->password.password->length);
5874 if (!rpccli_get_pwd_hash(pipe_hnd, nt_hash)) {
5875 DEBUG(0, ("Could not retrieve password hash\n"));
5879 cleartextpwd = decrypt_trustdom_secret(nt_hash, &data);
5881 if (cleartextpwd == NULL) {
5882 DEBUG(0,("retrieved NULL password\n"));
5883 nt_status = NT_STATUS_UNSUCCESSFUL;
5887 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5888 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5889 nt_status = NT_STATUS_UNSUCCESSFUL;
5893 #ifdef DEBUG_PASSWORD
5894 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5895 "password: [%s]\n", trusted_dom_name,
5896 sid_string_dbg(&dom_sid), cleartextpwd));
5900 SAFE_FREE(cleartextpwd);
5901 data_blob_free(&data);
5906 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5909 /* common variables */
5910 TALLOC_CTX* mem_ctx;
5911 struct cli_state *cli = NULL;
5912 struct rpc_pipe_client *pipe_hnd = NULL;
5914 const char *domain_name = NULL;
5915 DOM_SID *queried_dom_sid;
5916 POLICY_HND connect_hnd;
5917 union lsa_PolicyInformation *info = NULL;
5919 /* trusted domains listing variables */
5920 unsigned int enum_ctx = 0;
5922 struct lsa_DomainList dom_list;
5925 if (c->display_usage) {
5927 "net rpc trustdom vampire\n"
5928 " Vampire trust relationship from remote server\n");
5933 * Listing trusted domains (stored in secrets.tdb, if local)
5936 mem_ctx = talloc_init("trust relationships vampire");
5939 * set domain and pdc name to local samba server (default)
5940 * or to remote one given in command line
5943 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
5944 domain_name = c->opt_workgroup;
5945 c->opt_target_workgroup = c->opt_workgroup;
5947 fstrcpy(pdc_name, global_myname());
5948 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5949 c->opt_target_workgroup = domain_name;
5952 /* open \PIPE\lsarpc and open policy handle */
5953 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
5954 if (!NT_STATUS_IS_OK(nt_status)) {
5955 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
5956 nt_errstr(nt_status)));
5957 talloc_destroy(mem_ctx);
5961 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5963 if (!NT_STATUS_IS_OK(nt_status)) {
5964 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5965 nt_errstr(nt_status) ));
5967 talloc_destroy(mem_ctx);
5971 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
5973 if (NT_STATUS_IS_ERR(nt_status)) {
5974 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5975 nt_errstr(nt_status)));
5977 talloc_destroy(mem_ctx);
5981 /* query info level 5 to obtain sid of a domain being queried */
5982 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5984 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5987 if (NT_STATUS_IS_ERR(nt_status)) {
5988 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5989 nt_errstr(nt_status)));
5991 talloc_destroy(mem_ctx);
5995 queried_dom_sid = info->account_domain.sid;
5998 * Keep calling LsaEnumTrustdom over opened pipe until
5999 * the end of enumeration is reached
6002 d_printf("Vampire trusted domains:\n\n");
6005 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6010 if (NT_STATUS_IS_ERR(nt_status)) {
6011 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6012 nt_errstr(nt_status)));
6014 talloc_destroy(mem_ctx);
6018 for (i = 0; i < dom_list.count; i++) {
6020 print_trusted_domain(dom_list.domains[i].sid,
6021 dom_list.domains[i].name.string);
6023 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6024 *dom_list.domains[i].sid,
6025 dom_list.domains[i].name.string);
6026 if (!NT_STATUS_IS_OK(nt_status)) {
6028 talloc_destroy(mem_ctx);
6034 * in case of no trusted domains say something rather
6035 * than just display blank line
6037 if (!dom_list.count) d_printf("none\n");
6039 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6041 /* close this connection before doing next one */
6042 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6043 if (NT_STATUS_IS_ERR(nt_status)) {
6044 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6045 nt_errstr(nt_status)));
6047 talloc_destroy(mem_ctx);
6051 /* close lsarpc pipe and connection to IPC$ */
6054 talloc_destroy(mem_ctx);
6058 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6060 /* common variables */
6061 TALLOC_CTX* mem_ctx;
6062 struct cli_state *cli = NULL, *remote_cli = NULL;
6063 struct rpc_pipe_client *pipe_hnd = NULL;
6065 const char *domain_name = NULL;
6066 DOM_SID *queried_dom_sid;
6068 int ascii_dom_name_len;
6069 POLICY_HND connect_hnd;
6070 union lsa_PolicyInformation *info = NULL;
6072 /* trusted domains listing variables */
6073 unsigned int num_domains, enum_ctx = 0;
6074 int i, pad_len, col_len = 20;
6075 struct lsa_DomainList dom_list;
6078 /* trusting domains listing variables */
6079 POLICY_HND domain_hnd;
6080 struct samr_SamArray *trusts = NULL;
6082 if (c->display_usage) {
6084 "net rpc trustdom list\n"
6085 " List trust relationships\n");
6090 * Listing trusted domains (stored in secrets.tdb, if local)
6093 mem_ctx = talloc_init("trust relationships listing");
6096 * set domain and pdc name to local samba server (default)
6097 * or to remote one given in command line
6100 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6101 domain_name = c->opt_workgroup;
6102 c->opt_target_workgroup = c->opt_workgroup;
6104 fstrcpy(pdc_name, global_myname());
6105 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6106 c->opt_target_workgroup = domain_name;
6109 /* open \PIPE\lsarpc and open policy handle */
6110 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6111 if (!NT_STATUS_IS_OK(nt_status)) {
6112 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6113 nt_errstr(nt_status)));
6114 talloc_destroy(mem_ctx);
6118 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6120 if (!NT_STATUS_IS_OK(nt_status)) {
6121 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6122 nt_errstr(nt_status) ));
6124 talloc_destroy(mem_ctx);
6128 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6130 if (NT_STATUS_IS_ERR(nt_status)) {
6131 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6132 nt_errstr(nt_status)));
6134 talloc_destroy(mem_ctx);
6138 /* query info level 5 to obtain sid of a domain being queried */
6139 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6141 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6144 if (NT_STATUS_IS_ERR(nt_status)) {
6145 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6146 nt_errstr(nt_status)));
6148 talloc_destroy(mem_ctx);
6152 queried_dom_sid = info->account_domain.sid;
6155 * Keep calling LsaEnumTrustdom over opened pipe until
6156 * the end of enumeration is reached
6159 d_printf("Trusted domains list:\n\n");
6162 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6167 if (NT_STATUS_IS_ERR(nt_status)) {
6168 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6169 nt_errstr(nt_status)));
6171 talloc_destroy(mem_ctx);
6175 for (i = 0; i < dom_list.count; i++) {
6176 print_trusted_domain(dom_list.domains[i].sid,
6177 dom_list.domains[i].name.string);
6181 * in case of no trusted domains say something rather
6182 * than just display blank line
6184 if (!dom_list.count) d_printf("none\n");
6186 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6188 /* close this connection before doing next one */
6189 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6190 if (NT_STATUS_IS_ERR(nt_status)) {
6191 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6192 nt_errstr(nt_status)));
6194 talloc_destroy(mem_ctx);
6198 TALLOC_FREE(pipe_hnd);
6201 * Listing trusting domains (stored in passdb backend, if local)
6204 d_printf("\nTrusting domains list:\n\n");
6207 * Open \PIPE\samr and get needed policy handles
6209 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6211 if (!NT_STATUS_IS_OK(nt_status)) {
6212 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6214 talloc_destroy(mem_ctx);
6219 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6221 SA_RIGHT_SAM_OPEN_DOMAIN,
6223 if (!NT_STATUS_IS_OK(nt_status)) {
6224 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6225 nt_errstr(nt_status)));
6227 talloc_destroy(mem_ctx);
6231 /* SamrOpenDomain - we have to open domain policy handle in order to be
6232 able to enumerate accounts*/
6233 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6235 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6238 if (!NT_STATUS_IS_OK(nt_status)) {
6239 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6240 nt_errstr(nt_status)));
6242 talloc_destroy(mem_ctx);
6247 * perform actual enumeration
6250 enum_ctx = 0; /* reset enumeration context from last enumeration */
6253 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6260 if (NT_STATUS_IS_ERR(nt_status)) {
6261 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6262 nt_errstr(nt_status)));
6264 talloc_destroy(mem_ctx);
6268 for (i = 0; i < num_domains; i++) {
6270 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6273 * get each single domain's sid (do we _really_ need this ?):
6274 * 1) connect to domain's pdc
6275 * 2) query the pdc for domain's sid
6278 /* get rid of '$' tail */
6279 ascii_dom_name_len = strlen(str);
6280 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6281 str[ascii_dom_name_len - 1] = '\0';
6283 /* calculate padding space for d_printf to look nicer */
6284 pad_len = col_len - strlen(str);
6285 padding[pad_len] = 0;
6286 do padding[--pad_len] = ' '; while (pad_len);
6288 /* set opt_* variables to remote domain */
6290 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6291 c->opt_target_workgroup = c->opt_workgroup;
6293 d_printf("%s%s", str, padding);
6295 /* connect to remote domain controller */
6296 nt_status = net_make_ipc_connection(c,
6297 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6299 if (NT_STATUS_IS_OK(nt_status)) {
6300 /* query for domain's sid */
6301 if (run_rpc_command(
6303 &ndr_table_lsarpc.syntax_id, 0,
6304 rpc_query_domain_sid, argc,
6306 d_fprintf(stderr, "couldn't get domain's sid\n");
6308 cli_shutdown(remote_cli);
6311 d_fprintf(stderr, "domain controller is not "
6313 nt_errstr(nt_status));
6317 if (!num_domains) d_printf("none\n");
6319 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6321 /* close opened samr and domain policy handles */
6322 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6323 if (!NT_STATUS_IS_OK(nt_status)) {
6324 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6327 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6328 if (!NT_STATUS_IS_OK(nt_status)) {
6329 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6332 /* close samr pipe and connection to IPC$ */
6335 talloc_destroy(mem_ctx);
6340 * Entrypoint for 'net rpc trustdom' code.
6342 * @param argc Standard argc.
6343 * @param argv Standard argv without initial components.
6345 * @return Integer status (0 means success).
6348 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6350 struct functable func[] = {
6355 "Add trusted domain's account",
6356 "net rpc trustdom add\n"
6357 " Add trusted domain's account"
6363 "Remove trusted domain's account",
6364 "net rpc trustdom del\n"
6365 " Remove trusted domain's account"
6369 rpc_trustdom_establish,
6371 "Establish trust relationship",
6372 "net rpc trustdom establish\n"
6373 " Establish trust relationship"
6377 rpc_trustdom_revoke,
6379 "Revoke trust relationship",
6380 "net rpc trustdom revoke\n"
6381 " Revoke trust relationship"
6387 "List domain trusts",
6388 "net rpc trustdom list\n"
6389 " List domain trusts"
6393 rpc_trustdom_vampire,
6395 "Vampire trusts from remote server",
6396 "net rpc trustdom vampire\n"
6397 " Vampire trusts from remote server"
6399 {NULL, NULL, 0, NULL, NULL}
6402 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6406 * Check if a server will take rpc commands
6407 * @param flags Type of server to connect to (PDC, DMB, localhost)
6408 * if the host is not explicitly specified
6409 * @return bool (true means rpc supported)
6411 bool net_rpc_check(struct net_context *c, unsigned flags)
6413 struct cli_state *cli;
6415 struct sockaddr_storage server_ss;
6416 char *server_name = NULL;
6419 /* flags (i.e. server type) may depend on command */
6420 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6423 if ((cli = cli_initialise()) == NULL) {
6427 status = cli_connect(cli, server_name, &server_ss);
6428 if (!NT_STATUS_IS_OK(status))
6430 if (!attempt_netbios_session_request(&cli, global_myname(),
6431 server_name, &server_ss))
6433 if (!cli_negprot(cli))
6435 if (cli->protocol < PROTOCOL_NT1)
6444 /* dump sam database via samsync rpc calls */
6445 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6446 if (c->display_usage) {
6449 " Dump remote SAM database\n");
6453 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6454 NET_FLAGS_ANONYMOUS,
6455 rpc_samdump_internals, argc, argv);
6458 /* syncronise sam database via samsync rpc calls */
6459 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6461 struct functable func[] = {
6466 "Dump remote SAM database to ldif",
6467 "net rpc vampire ldif\n"
6468 " Dump remote SAM database to LDIF file or stdout"
6474 "Dump remote SAM database to Kerberos Keytab",
6475 "net rpc vampire keytab\n"
6476 " Dump remote SAM database to Kerberos keytab file"
6479 {NULL, NULL, 0, NULL, NULL}
6483 if (c->display_usage) {
6486 " Vampire remote SAM database\n");
6490 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6491 NET_FLAGS_ANONYMOUS,
6492 rpc_vampire_internals,
6496 return net_run_function(c, argc, argv, "net rpc vampire", func);
6500 * Migrate everything from a print server.
6502 * @param c A net_context structure.
6503 * @param argc Standard main() style argc.
6504 * @param argv Standard main() style argv. Initial components are already
6507 * @return A shell status integer (0 for success).
6509 * The order is important !
6510 * To successfully add drivers the print queues have to exist !
6511 * Applying ACLs should be the last step, because you're easily locked out.
6514 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6519 if (c->display_usage) {
6521 "net rpc printer migrate all\n"
6522 " Migrate everything from a print server\n");
6527 d_printf("no server to migrate\n");
6531 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6532 rpc_printer_migrate_printers_internals, argc,
6537 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6538 rpc_printer_migrate_drivers_internals, argc,
6543 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6544 rpc_printer_migrate_forms_internals, argc, argv);
6548 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6549 rpc_printer_migrate_settings_internals, argc,
6554 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6555 rpc_printer_migrate_security_internals, argc,
6561 * Migrate print drivers from a print server.
6563 * @param c A net_context structure.
6564 * @param argc Standard main() style argc.
6565 * @param argv Standard main() style argv. Initial components are already
6568 * @return A shell status integer (0 for success).
6570 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6573 if (c->display_usage) {
6575 "net rpc printer migrate drivers\n"
6576 " Migrate print-drivers from a print-server\n");
6581 d_printf("no server to migrate\n");
6585 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6586 rpc_printer_migrate_drivers_internals,
6591 * Migrate print-forms from a print-server.
6593 * @param c A net_context structure.
6594 * @param argc Standard main() style argc.
6595 * @param argv Standard main() style argv. Initial components are already
6598 * @return A shell status integer (0 for success).
6600 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6603 if (c->display_usage) {
6605 "net rpc printer migrate forms\n"
6606 " Migrate print-forms from a print-server\n");
6611 d_printf("no server to migrate\n");
6615 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6616 rpc_printer_migrate_forms_internals,
6621 * Migrate printers from a print-server.
6623 * @param c A net_context structure.
6624 * @param argc Standard main() style argc.
6625 * @param argv Standard main() style argv. Initial components are already
6628 * @return A shell status integer (0 for success).
6630 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6633 if (c->display_usage) {
6635 "net rpc printer migrate printers\n"
6636 " Migrate printers from a print-server\n");
6641 d_printf("no server to migrate\n");
6645 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6646 rpc_printer_migrate_printers_internals,
6651 * Migrate printer-ACLs from a print-server
6653 * @param c A net_context structure.
6654 * @param argc Standard main() style argc.
6655 * @param argv Standard main() style argv. Initial components are already
6658 * @return A shell status integer (0 for success).
6660 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6663 if (c->display_usage) {
6665 "net rpc printer migrate security\n"
6666 " Migrate printer-ACLs from a print-server\n");
6671 d_printf("no server to migrate\n");
6675 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6676 rpc_printer_migrate_security_internals,
6681 * Migrate printer-settings from a print-server.
6683 * @param c A net_context structure.
6684 * @param argc Standard main() style argc.
6685 * @param argv Standard main() style argv. Initial components are already
6688 * @return A shell status integer (0 for success).
6690 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6693 if (c->display_usage) {
6695 "net rpc printer migrate settings\n"
6696 " Migrate printer-settings from a print-server\n");
6701 d_printf("no server to migrate\n");
6705 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6706 rpc_printer_migrate_settings_internals,
6711 * 'net rpc printer' entrypoint.
6713 * @param c A net_context structure.
6714 * @param argc Standard main() style argc.
6715 * @param argv Standard main() style argv. Initial components are already
6719 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6722 /* ouch: when addriver and setdriver are called from within
6723 rpc_printer_migrate_drivers_internals, the printer-queue already
6726 struct functable func[] = {
6729 rpc_printer_migrate_all,
6731 "Migrate all from remote to local print server",
6732 "net rpc printer migrate all\n"
6733 " Migrate all from remote to local print server"
6737 rpc_printer_migrate_drivers,
6739 "Migrate drivers to local server",
6740 "net rpc printer migrate drivers\n"
6741 " Migrate drivers to local server"
6745 rpc_printer_migrate_forms,
6747 "Migrate froms to local server",
6748 "net rpc printer migrate forms\n"
6749 " Migrate froms to local server"
6753 rpc_printer_migrate_printers,
6755 "Migrate printers to local server",
6756 "net rpc printer migrate printers\n"
6757 " Migrate printers to local server"
6761 rpc_printer_migrate_security,
6763 "Mirgate printer ACLs to local server",
6764 "net rpc printer migrate security\n"
6765 " Mirgate printer ACLs to local server"
6769 rpc_printer_migrate_settings,
6771 "Migrate printer settings to local server",
6772 "net rpc printer migrate settings\n"
6773 " Migrate printer settings to local server"
6775 {NULL, NULL, 0, NULL, NULL}
6778 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6783 * List printers on a remote RPC server.
6785 * @param c A net_context structure.
6786 * @param argc Standard main() style argc.
6787 * @param argv Standard main() style argv. Initial components are already
6790 * @return A shell status integer (0 for success).
6792 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6794 if (c->display_usage) {
6796 "net rpc printer list\n"
6797 " List printers on a remote RPC server\n");
6801 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6802 rpc_printer_list_internals,
6807 * List printer-drivers on a remote RPC server.
6809 * @param c A net_context structure.
6810 * @param argc Standard main() style argc.
6811 * @param argv Standard main() style argv. Initial components are already
6814 * @return A shell status integer (0 for success).
6816 static int rpc_printer_driver_list(struct net_context *c, int argc,
6819 if (c->display_usage) {
6821 "net rpc printer driver\n"
6822 " List printer-drivers on a remote RPC server\n");
6826 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6827 rpc_printer_driver_list_internals,
6832 * Publish printer in ADS via MSRPC.
6834 * @param c A net_context structure.
6835 * @param argc Standard main() style argc.
6836 * @param argv Standard main() style argv. Initial components are already
6839 * @return A shell status integer (0 for success).
6841 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6844 if (c->display_usage) {
6846 "net rpc printer publish publish\n"
6847 " Publish printer in ADS via MSRPC\n");
6851 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6852 rpc_printer_publish_publish_internals,
6857 * Update printer in ADS via MSRPC.
6859 * @param c A net_context structure.
6860 * @param argc Standard main() style argc.
6861 * @param argv Standard main() style argv. Initial components are already
6864 * @return A shell status integer (0 for success).
6866 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6868 if (c->display_usage) {
6870 "net rpc printer publish update\n"
6871 " Update printer in ADS via MSRPC\n");
6875 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6876 rpc_printer_publish_update_internals,
6881 * UnPublish printer in ADS via MSRPC.
6883 * @param c A net_context structure.
6884 * @param argc Standard main() style argc.
6885 * @param argv Standard main() style argv. Initial components are already
6888 * @return A shell status integer (0 for success).
6890 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
6893 if (c->display_usage) {
6895 "net rpc printer publish unpublish\n"
6896 " UnPublish printer in ADS via MSRPC\n");
6900 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6901 rpc_printer_publish_unpublish_internals,
6906 * List published printers via MSRPC.
6908 * @param c A net_context structure.
6909 * @param argc Standard main() style argc.
6910 * @param argv Standard main() style argv. Initial components are already
6913 * @return A shell status integer (0 for success).
6915 static int rpc_printer_publish_list(struct net_context *c, int argc,
6918 if (c->display_usage) {
6920 "net rpc printer publish list\n"
6921 " List published printers via MSRPC\n");
6925 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6926 rpc_printer_publish_list_internals,
6932 * Publish printer in ADS.
6934 * @param c A net_context structure.
6935 * @param argc Standard main() style argc.
6936 * @param argv Standard main() style argv. Initial components are already
6939 * @return A shell status integer (0 for success).
6941 static int rpc_printer_publish(struct net_context *c, int argc,
6945 struct functable func[] = {
6948 rpc_printer_publish_publish,
6950 "Publish printer in AD",
6951 "net rpc printer publish publish\n"
6952 " Publish printer in AD"
6956 rpc_printer_publish_update,
6958 "Update printer in AD",
6959 "net rpc printer publish update\n"
6960 " Update printer in AD"
6964 rpc_printer_publish_unpublish,
6966 "Unpublish printer",
6967 "net rpc printer publish unpublish\n"
6968 " Unpublish printer"
6972 rpc_printer_publish_list,
6974 "List published printers",
6975 "net rpc printer publish list\n"
6976 " List published printers"
6978 {NULL, NULL, 0, NULL, NULL}
6982 if (c->display_usage) {
6983 d_printf("Usage:\n");
6984 d_printf("net rpc printer publish\n"
6985 " List published printers\n"
6986 " Alias of net rpc printer publish list\n");
6987 net_display_usage_from_functable(func);
6990 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6991 rpc_printer_publish_list_internals,
6995 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7001 * Display rpc printer help page.
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 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7010 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"
7011 "\tlists all printers on print-server\n\n");
7012 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7013 "\tlists all printer-drivers on print-server\n\n");
7014 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7015 "\tpublishes printer settings in Active Directory\n"
7016 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
7017 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7018 "\n\tmigrates printers from remote to local server\n\n");
7019 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7020 "\n\tmigrates printer-settings from remote to local server\n\n");
7021 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7022 "\n\tmigrates printer-drivers from remote to local server\n\n");
7023 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7024 "\n\tmigrates printer-forms from remote to local server\n\n");
7025 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7026 "\n\tmigrates printer-ACLs from remote to local server\n\n");
7027 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7028 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7029 "\tremote to local print-server\n\n");
7030 net_common_methods_usage(c, argc, argv);
7031 net_common_flags_usage(c, argc, argv);
7033 "\t-v or --verbose\t\t\tgive verbose output\n"
7034 "\t --destination\t\tmigration target server (default: localhost)\n");
7040 * 'net rpc printer' entrypoint.
7042 * @param c A net_context structure.
7043 * @param argc Standard main() style argc.
7044 * @param argv Standard main() style argv. Initial components are already
7047 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7049 struct functable func[] = {
7054 "List all printers on print server",
7055 "net rpc printer list\n"
7056 " List all printers on print server"
7060 rpc_printer_migrate,
7062 "Migrate printer to local server",
7063 "net rpc printer migrate\n"
7064 " Migrate printer to local server"
7068 rpc_printer_driver_list,
7070 "List printer drivers",
7071 "net rpc printer driver\n"
7072 " List printer drivers"
7076 rpc_printer_publish,
7078 "Publish printer in AD",
7079 "net rpc printer publish\n"
7080 " Publish printer in AD"
7082 {NULL, NULL, 0, NULL, NULL}
7086 if (c->display_usage) {
7087 d_printf("Usage:\n");
7088 d_printf("net rpc printer\n"
7089 " List printers\n");
7090 net_display_usage_from_functable(func);
7093 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7094 rpc_printer_list_internals,
7098 return net_run_function(c, argc, argv, "net rpc printer", func);
7102 * 'net rpc' entrypoint.
7104 * @param c A net_context structure.
7105 * @param argc Standard main() style argc.
7106 * @param argv Standard main() style argv. Initial components are already
7110 int net_rpc(struct net_context *c, int argc, const char **argv)
7112 struct functable func[] = {
7117 "Modify global audit settings",
7119 " Modify global audit settings"
7125 "Show basic info about a domain",
7127 " Show basic info about a domain"
7141 "Join a domain created in server manager",
7143 " Join a domain created in server manager"
7149 "Test that a join is valid",
7150 "net rpc testjoin\n"
7151 " Test that a join is valid"
7157 "List/modify users",
7159 " List/modify users"
7165 "Change a user password",
7166 "net rpc password\n"
7167 " Change a user password\n"
7168 " Alias for net rpc user password"
7174 "List/modify groups",
7176 " List/modify groups"
7182 "List/modify shares",
7184 " List/modify shares"
7198 "List/modify printers",
7200 " List/modify printers"
7204 net_rpc_changetrustpw,
7206 "Change trust account password",
7207 "net rpc changetrustpw\n"
7208 " Change trust account password"
7214 "Modify domain trusts",
7215 "net rpc trustdom\n"
7216 " Modify domain trusts"
7222 "Abort a remote shutdown",
7223 "net rpc abortshutdown\n"
7224 " Abort a remote shutdown"
7230 "Shutdown a remote server",
7231 "net rpc shutdown\n"
7232 " Shutdown a remote server"
7238 "Dump SAM data of remote NT PDC",
7240 " Dump SAM data of remote NT PDC"
7246 "Sync a remote NT PDC's data into local passdb",
7248 " Sync a remote NT PDC's data into local passdb"
7254 "Fetch the domain sid into local secrets.tdb",
7256 " Fetch the domain sid into local secrets.tdb"
7262 "Manage privileges assigned to SID",
7264 " Manage privileges assigned to SID"
7270 "Start/stop/query remote services",
7272 " Start/stop/query remote services"
7278 "Manage registry hives",
7279 "net rpc registry\n"
7280 " Manage registry hives"
7286 "Open interactive shell on remote server",
7288 " Open interactive shell on remote server"
7290 {NULL, NULL, 0, NULL, NULL}
7292 return net_run_function(c, argc, argv, "net rpc", func);