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 lsa_pipe = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
64 d_fprintf(stderr, "Could not initialise lsa pipe\n");
68 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
69 SEC_RIGHTS_MAXIMUM_ALLOWED,
71 if (!NT_STATUS_IS_OK(result)) {
72 d_fprintf(stderr, "open_policy failed: %s\n",
77 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
79 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
81 if (!NT_STATUS_IS_OK(result)) {
82 d_fprintf(stderr, "lsaquery failed: %s\n",
87 *domain_name = info->account_domain.name.string;
88 *domain_sid = info->account_domain.sid;
90 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
91 TALLOC_FREE(lsa_pipe);
97 * Run a single RPC command, from start to finish.
99 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
100 * @param conn_flag a NET_FLAG_ combination. Passed to
101 * net_make_ipc_connection.
102 * @param argc Standard main() style argc
103 * @param argc Standard main() style argv. Initial components are already
105 * @return A shell status integer (0 for success)
108 int run_rpc_command(struct net_context *c,
109 struct cli_state *cli_arg,
116 struct cli_state *cli = NULL;
117 struct rpc_pipe_client *pipe_hnd = NULL;
121 const char *domain_name;
123 /* make use of cli_state handed over as an argument, if possible */
125 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
126 if (!NT_STATUS_IS_OK(nt_status)) {
127 DEBUG(1, ("failed to make ipc connection: %s\n",
128 nt_errstr(nt_status)));
141 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
142 DEBUG(0, ("talloc_init() failed\n"));
147 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
149 if (!NT_STATUS_IS_OK(nt_status)) {
154 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
155 if (lp_client_schannel() && (pipe_idx == PI_NETLOGON)) {
156 /* Always try and create an schannel netlogon pipe. */
157 pipe_hnd = cli_rpc_pipe_open_schannel(cli, pipe_idx,
158 PIPE_AUTH_LEVEL_PRIVACY,
162 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
163 nt_errstr(nt_status) ));
168 pipe_hnd = cli_rpc_pipe_open_noauth(cli, pipe_idx, &nt_status);
170 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
171 cli_get_pipe_name(pipe_idx),
172 nt_errstr(nt_status) ));
179 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
181 if (!NT_STATUS_IS_OK(nt_status)) {
182 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
184 DEBUG(5, ("rpc command function succedded\n"));
187 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
189 TALLOC_FREE(pipe_hnd);
193 /* close the connection only if it was opened here */
198 talloc_destroy(mem_ctx);
199 return (!NT_STATUS_IS_OK(nt_status));
203 * Force a change of the trust acccount password.
205 * All parameters are provided by the run_rpc_command function, except for
206 * argc, argv which are passes through.
208 * @param domain_sid The domain sid aquired from the remote server
209 * @param cli A cli_state connected to the server.
210 * @param mem_ctx Talloc context, destoyed on compleation of the function.
211 * @param argc Standard main() style argc
212 * @param argc Standard main() style argv. Initial components are already
215 * @return Normal NTSTATUS return.
218 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
219 const DOM_SID *domain_sid,
220 const char *domain_name,
221 struct cli_state *cli,
222 struct rpc_pipe_client *pipe_hnd,
228 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
232 * Force a change of the trust acccount password.
234 * @param argc Standard main() style argc
235 * @param argc Standard main() style argv. Initial components are already
238 * @return A shell status integer (0 for success)
241 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
243 if (c->display_usage) {
245 "net rpc changetrustpw\n"
246 " Change the machine trust password\n");
250 return run_rpc_command(c, NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
251 rpc_changetrustpw_internals,
256 * Join a domain, the old way.
258 * This uses 'machinename' as the inital password, and changes it.
260 * The password should be created with 'server manager' or equiv first.
262 * All parameters are provided by the run_rpc_command function, except for
263 * argc, argv which are passes through.
265 * @param domain_sid The domain sid aquired from the remote server
266 * @param cli A cli_state connected to the server.
267 * @param mem_ctx Talloc context, destoyed on compleation of the function.
268 * @param argc Standard main() style argc
269 * @param argc Standard main() style argv. Initial components are already
272 * @return Normal NTSTATUS return.
275 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
276 const DOM_SID *domain_sid,
277 const char *domain_name,
278 struct cli_state *cli,
279 struct rpc_pipe_client *pipe_hnd,
285 fstring trust_passwd;
286 unsigned char orig_trust_passwd_hash[16];
288 uint32 sec_channel_type;
290 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &result);
292 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
295 nt_errstr(result) ));
300 check what type of join - if the user want's to join as
301 a BDC, the server must agree that we are a BDC.
304 sec_channel_type = get_sec_channel_type(argv[0]);
306 sec_channel_type = get_sec_channel_type(NULL);
309 fstrcpy(trust_passwd, global_myname());
310 strlower_m(trust_passwd);
313 * Machine names can be 15 characters, but the max length on
314 * a password is 14. --jerry
317 trust_passwd[14] = '\0';
319 E_md4hash(trust_passwd, orig_trust_passwd_hash);
321 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
322 orig_trust_passwd_hash,
325 if (NT_STATUS_IS_OK(result))
326 printf("Joined domain %s.\n", c->opt_target_workgroup);
329 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
330 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
331 result = NT_STATUS_UNSUCCESSFUL;
338 * Join a domain, the old way.
340 * @param argc Standard main() style argc
341 * @param argc Standard main() style argv. Initial components are already
344 * @return A shell status integer (0 for success)
347 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
349 return run_rpc_command(c, NULL, PI_NETLOGON,
350 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
351 rpc_oldjoin_internals,
356 * Join a domain, the old way. This function exists to allow
357 * the message to be displayed when oldjoin was explicitly
358 * requested, but not when it was implied by "net rpc join"
360 * @param argc Standard main() style argc
361 * @param argc Standard main() style argv. Initial components are already
364 * @return A shell status integer (0 for success)
367 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
371 if (c->display_usage) {
374 " Join a domain the old way\n");
378 rc = net_rpc_perform_oldjoin(c, argc, argv);
381 d_fprintf(stderr, "Failed to join domain\n");
388 * 'net rpc join' entrypoint.
389 * @param argc Standard main() style argc
390 * @param argc Standard main() style argv. Initial components are already
393 * Main 'net_rpc_join()' (where the admin username/password is used) is
395 * Try to just change the password, but if that doesn't work, use/prompt
396 * for a username/password.
399 int net_rpc_join(struct net_context *c, int argc, const char **argv)
401 if (c->display_usage) {
403 "net rpc join -U <username>[%%password] <type>\n"
405 " username\tName of the admin user"
406 " password\tPassword of the admin user, will "
407 "prompt if not specified\n"
408 " type\tCan be one of the following:\n"
409 "\t\tMEMBER\tJoin as member server (default)\n"
410 "\t\tBDC\tJoin as BDC\n"
411 "\t\tPDC\tJoin as PDC\n");
415 if (lp_server_role() == ROLE_STANDALONE) {
416 d_printf("cannot join as standalone machine\n");
420 if (strlen(global_myname()) > 15) {
421 d_printf("Our netbios name can be at most 15 chars long, "
422 "\"%s\" is %u chars long\n",
423 global_myname(), (unsigned int)strlen(global_myname()));
427 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
430 return net_rpc_join_newstyle(c, argc, argv);
434 * display info about a rpc domain
436 * All parameters are provided by the run_rpc_command function, except for
437 * argc, argv which are passed through.
439 * @param domain_sid The domain sid acquired from the remote server
440 * @param cli A cli_state connected to the server.
441 * @param mem_ctx Talloc context, destoyed on completion of the function.
442 * @param argc Standard main() style argc
443 * @param argv Standard main() style argv. Initial components are already
446 * @return Normal NTSTATUS return.
449 NTSTATUS rpc_info_internals(struct net_context *c,
450 const DOM_SID *domain_sid,
451 const char *domain_name,
452 struct cli_state *cli,
453 struct rpc_pipe_client *pipe_hnd,
458 POLICY_HND connect_pol, domain_pol;
459 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
460 union samr_DomainInfo *info = NULL;
463 sid_to_fstring(sid_str, domain_sid);
465 /* Get sam policy handle */
466 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
468 MAXIMUM_ALLOWED_ACCESS,
470 if (!NT_STATUS_IS_OK(result)) {
471 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
475 /* Get domain policy handle */
476 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
478 MAXIMUM_ALLOWED_ACCESS,
479 CONST_DISCARD(struct dom_sid2 *, domain_sid),
481 if (!NT_STATUS_IS_OK(result)) {
482 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
486 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
490 if (NT_STATUS_IS_OK(result)) {
491 d_printf("Domain Name: %s\n", info->info2.domain_name.string);
492 d_printf("Domain SID: %s\n", sid_str);
493 d_printf("Sequence number: %llu\n",
494 (unsigned long long)info->info2.sequence_num);
495 d_printf("Num users: %u\n", info->info2.num_users);
496 d_printf("Num domain groups: %u\n", info->info2.num_groups);
497 d_printf("Num local groups: %u\n", info->info2.num_aliases);
505 * 'net rpc info' entrypoint.
506 * @param argc Standard main() style argc
507 * @param argc Standard main() style argv. Initial components are already
511 int net_rpc_info(struct net_context *c, int argc, const char **argv)
513 if (c->display_usage) {
516 " Display information about the domain\n");
520 return run_rpc_command(c, NULL, PI_SAMR, NET_FLAGS_PDC,
526 * Fetch domain SID into the local secrets.tdb
528 * All parameters are provided by the run_rpc_command function, except for
529 * argc, argv which are passes through.
531 * @param domain_sid The domain sid acquired from the remote server
532 * @param cli A cli_state connected to the server.
533 * @param mem_ctx Talloc context, destoyed on completion of thea function.
534 * @param argc Standard main() style argc
535 * @param argv Standard main() style argv. Initial components are already
538 * @return Normal NTSTATUS return.
541 static NTSTATUS rpc_getsid_internals(struct net_context *c,
542 const DOM_SID *domain_sid,
543 const char *domain_name,
544 struct cli_state *cli,
545 struct rpc_pipe_client *pipe_hnd,
552 sid_to_fstring(sid_str, domain_sid);
553 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
554 sid_str, domain_name);
556 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
557 DEBUG(0,("Can't store domain SID\n"));
558 return NT_STATUS_UNSUCCESSFUL;
565 * 'net rpc getsid' entrypoint.
566 * @param argc Standard main() style argc
567 * @param argc Standard main() style argv. Initial components are already
571 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
573 if (c->display_usage) {
576 " Fetch domain SID into local secrets.tdb\n");
580 return run_rpc_command(c, NULL, PI_SAMR,
581 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
582 rpc_getsid_internals,
586 /****************************************************************************/
589 * Basic usage function for 'net rpc user'
590 * @param argc Standard main() style argc.
591 * @param argv Standard main() style argv. Initial components are already
595 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
597 return net_user_usage(c, argc, argv);
601 * Add a new user to a remote RPC server
603 * @param argc Standard main() style argc
604 * @param argv Standard main() style argv. Initial components are already
607 * @return A shell status integer (0 for success)
610 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
612 NET_API_STATUS status;
613 struct USER_INFO_1 info1;
614 uint32_t parm_error = 0;
616 if (argc < 1 || c->display_usage) {
617 rpc_user_usage(c, argc, argv);
623 info1.usri1_name = argv[0];
625 info1.usri1_password = argv[1];
628 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
631 d_fprintf(stderr, "Failed to add user '%s' with: %s.\n",
632 argv[0], libnetapi_get_error_string(c->netapi_ctx,
636 d_printf("Added user '%s'.\n", argv[0]);
643 * Rename a user on a remote RPC server
645 * All parameters are provided by the run_rpc_command function, except for
646 * argc, argv which are passes through.
648 * @param domain_sid The domain sid acquired from the remote server
649 * @param cli A cli_state connected to the server.
650 * @param mem_ctx Talloc context, destoyed on completion of the function.
651 * @param argc Standard main() style argc
652 * @param argv Standard main() style argv. Initial components are already
655 * @return Normal NTSTATUS return.
658 static NTSTATUS rpc_user_rename_internals(struct net_context *c,
659 const DOM_SID *domain_sid,
660 const char *domain_name,
661 struct cli_state *cli,
662 struct rpc_pipe_client *pipe_hnd,
667 POLICY_HND connect_pol, domain_pol, user_pol;
668 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
669 uint32 info_level = 7;
670 const char *old_name, *new_name;
671 struct samr_Ids user_rids, name_types;
672 struct lsa_String lsa_acct_name;
673 union samr_UserInfo *info = NULL;
675 if (argc != 2 || c->display_usage) {
676 rpc_user_usage(c, argc, argv);
683 /* Get sam policy handle */
685 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
687 MAXIMUM_ALLOWED_ACCESS,
690 if (!NT_STATUS_IS_OK(result)) {
694 /* Get domain policy handle */
696 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
698 MAXIMUM_ALLOWED_ACCESS,
699 CONST_DISCARD(struct dom_sid2 *, domain_sid),
701 if (!NT_STATUS_IS_OK(result)) {
705 init_lsa_String(&lsa_acct_name, old_name);
707 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
713 if (!NT_STATUS_IS_OK(result)) {
717 /* Open domain user */
718 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
720 MAXIMUM_ALLOWED_ACCESS,
724 if (!NT_STATUS_IS_OK(result)) {
728 /* Query user info */
729 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
734 if (!NT_STATUS_IS_OK(result)) {
738 init_samr_user_info7(&info->info7, new_name);
741 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
746 if (!NT_STATUS_IS_OK(result)) {
751 if (!NT_STATUS_IS_OK(result)) {
752 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
755 d_printf("Renamed user from %s to %s\n", old_name, new_name);
761 * Rename a user on a remote RPC server
763 * @param argc Standard main() style argc
764 * @param argv Standard main() style argv. Initial components are already
767 * @return A shell status integer (0 for success)
770 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
772 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_user_rename_internals,
777 * Delete a user from a remote RPC server
779 * @param argc Standard main() style argc
780 * @param argv Standard main() style argv. Initial components are already
783 * @return A shell status integer (0 for success)
786 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
788 NET_API_STATUS status;
790 if (argc < 1 || c->display_usage) {
791 rpc_user_usage(c, argc, argv);
795 status = NetUserDel(c->opt_host, argv[0]);
798 d_fprintf(stderr, "Failed to delete user '%s' with: %s.\n",
800 libnetapi_get_error_string(c->netapi_ctx, status));
803 d_printf("Deleted user '%s'.\n", argv[0]);
810 * Set a password for a user on a remote RPC server
812 * All parameters are provided by the run_rpc_command function, except for
813 * argc, argv which are passes through.
815 * @param domain_sid The domain sid acquired from the remote server
816 * @param cli A cli_state connected to the server.
817 * @param mem_ctx Talloc context, destoyed on completion of the function.
818 * @param argc Standard main() style argc
819 * @param argv Standard main() style argv. Initial components are already
822 * @return Normal NTSTATUS return.
825 static NTSTATUS rpc_user_password_internals(struct net_context *c,
826 const DOM_SID *domain_sid,
827 const char *domain_name,
828 struct cli_state *cli,
829 struct rpc_pipe_client *pipe_hnd,
834 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
835 POLICY_HND connect_pol, domain_pol, user_pol;
838 const char *new_password;
840 union samr_UserInfo info;
842 if (argc < 1 || c->display_usage) {
843 rpc_user_usage(c, argc, argv);
850 new_password = argv[1];
852 asprintf(&prompt, "Enter new password for %s:", user);
853 new_password = getpass(prompt);
857 /* Get sam policy and domain handles */
859 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
861 MAXIMUM_ALLOWED_ACCESS,
864 if (!NT_STATUS_IS_OK(result)) {
868 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
870 MAXIMUM_ALLOWED_ACCESS,
871 CONST_DISCARD(struct dom_sid2 *, domain_sid),
874 if (!NT_STATUS_IS_OK(result)) {
878 /* Get handle on user */
881 struct samr_Ids user_rids, name_types;
882 struct lsa_String lsa_acct_name;
884 init_lsa_String(&lsa_acct_name, user);
886 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
892 if (!NT_STATUS_IS_OK(result)) {
896 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
898 MAXIMUM_ALLOWED_ACCESS,
902 if (!NT_STATUS_IS_OK(result)) {
907 /* Set password on account */
909 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
911 init_samr_user_info24(&info.info24, pwbuf, 24);
913 SamOEMhashBlob(info.info24.password.data, 516,
914 &cli->user_session_key);
916 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
921 if (!NT_STATUS_IS_OK(result)) {
925 /* Display results */
933 * Set a user's password on a remote RPC server
935 * @param argc Standard main() style argc
936 * @param argv Standard main() style argv. Initial components are already
939 * @return A shell status integer (0 for success)
942 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
944 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_user_password_internals,
949 * List user's groups on a remote RPC server
951 * All parameters are provided by the run_rpc_command function, except for
952 * argc, argv which are passes through.
954 * @param domain_sid The domain sid acquired from the remote server
955 * @param cli A cli_state connected to the server.
956 * @param mem_ctx Talloc context, destoyed on completion of the function.
957 * @param argc Standard main() style argc
958 * @param argv Standard main() style argv. Initial components are already
961 * @return Normal NTSTATUS return.
964 static NTSTATUS rpc_user_info_internals(struct net_context *c,
965 const DOM_SID *domain_sid,
966 const char *domain_name,
967 struct cli_state *cli,
968 struct rpc_pipe_client *pipe_hnd,
973 POLICY_HND connect_pol, domain_pol, user_pol;
974 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
976 struct samr_RidWithAttributeArray *rid_array = NULL;
977 struct lsa_Strings names;
978 struct samr_Ids types;
979 uint32_t *lrids = NULL;
980 struct samr_Ids rids, name_types;
981 struct lsa_String lsa_acct_name;
984 if (argc < 1 || c->display_usage) {
985 rpc_user_usage(c, argc, argv);
988 /* Get sam policy handle */
990 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
992 MAXIMUM_ALLOWED_ACCESS,
994 if (!NT_STATUS_IS_OK(result)) goto done;
996 /* Get domain policy handle */
998 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1000 MAXIMUM_ALLOWED_ACCESS,
1001 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1003 if (!NT_STATUS_IS_OK(result)) goto done;
1005 /* Get handle on user */
1007 init_lsa_String(&lsa_acct_name, argv[0]);
1009 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1016 if (!NT_STATUS_IS_OK(result)) goto done;
1018 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1020 MAXIMUM_ALLOWED_ACCESS,
1023 if (!NT_STATUS_IS_OK(result)) goto done;
1025 result = rpccli_samr_GetGroupsForUser(pipe_hnd, mem_ctx,
1029 if (!NT_STATUS_IS_OK(result)) goto done;
1033 if (rid_array->count) {
1034 if ((lrids = TALLOC_ARRAY(mem_ctx, uint32, rid_array->count)) == NULL) {
1035 result = NT_STATUS_NO_MEMORY;
1039 for (i = 0; i < rid_array->count; i++)
1040 lrids[i] = rid_array->rids[i].rid;
1042 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
1049 if (!NT_STATUS_IS_OK(result)) {
1053 /* Display results */
1055 for (i = 0; i < names.count; i++)
1056 printf("%s\n", names.names[i].string);
1063 * List a user's groups from a remote RPC server
1065 * @param argc Standard main() style argc
1066 * @param argv Standard main() style argv. Initial components are already
1069 * @return A shell status integer (0 for success)
1072 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
1074 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_user_info_internals,
1079 * List users on a remote RPC server
1081 * All parameters are provided by the run_rpc_command function, except for
1082 * argc, argv which are passes through.
1084 * @param domain_sid The domain sid acquired from the remote server
1085 * @param cli A cli_state connected to the server.
1086 * @param mem_ctx Talloc context, destoyed on completion of the function.
1087 * @param argc Standard main() style argc
1088 * @param argv Standard main() style argv. Initial components are already
1091 * @return Normal NTSTATUS return.
1094 static NTSTATUS rpc_user_list_internals(struct net_context *c,
1095 const DOM_SID *domain_sid,
1096 const char *domain_name,
1097 struct cli_state *cli,
1098 struct rpc_pipe_client *pipe_hnd,
1099 TALLOC_CTX *mem_ctx,
1103 POLICY_HND connect_pol, domain_pol;
1104 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1105 uint32 start_idx=0, num_entries, i, loop_count = 0;
1107 /* Get sam policy handle */
1109 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1111 MAXIMUM_ALLOWED_ACCESS,
1113 if (!NT_STATUS_IS_OK(result)) {
1117 /* Get domain policy handle */
1119 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1121 MAXIMUM_ALLOWED_ACCESS,
1122 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1124 if (!NT_STATUS_IS_OK(result)) {
1128 /* Query domain users */
1129 if (c->opt_long_list_entries)
1130 d_printf("\nUser name Comment"
1131 "\n-----------------------------\n");
1133 const char *user = NULL;
1134 const char *desc = NULL;
1135 uint32 max_entries, max_size;
1136 uint32_t total_size, returned_size;
1137 union samr_DispInfo info;
1139 get_query_dispinfo_params(
1140 loop_count, &max_entries, &max_size);
1142 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
1152 start_idx += info.info1.count;
1153 num_entries = info.info1.count;
1155 for (i = 0; i < num_entries; i++) {
1156 user = info.info1.entries[i].account_name.string;
1157 if (c->opt_long_list_entries)
1158 desc = info.info1.entries[i].description.string;
1159 if (c->opt_long_list_entries)
1160 printf("%-21.21s %s\n", user, desc);
1162 printf("%s\n", user);
1164 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1171 * 'net rpc user' entrypoint.
1172 * @param argc Standard main() style argc
1173 * @param argc Standard main() style argv. Initial components are already
1177 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1179 NET_API_STATUS status;
1181 struct functable func[] = {
1186 "Add specified user",
1187 "net rpc user add\n"
1188 " Add specified user"
1194 "List domain groups of user",
1195 "net rpc user info\n"
1196 " Lis domain groups of user"
1202 "Remove specified user",
1203 "net rpc user delete\n"
1204 " Remove specified user"
1210 "Change user password",
1211 "net rpc user password\n"
1212 " Change user password"
1218 "Rename specified user",
1219 "net rpc user rename\n"
1220 " Rename specified user"
1222 {NULL, NULL, 0, NULL, NULL}
1225 status = libnetapi_init(&c->netapi_ctx);
1229 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1230 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1233 if (c->display_usage) {
1234 d_printf("Usage:\n");
1235 d_printf("net rpc user\n"
1236 " List all users\n");
1237 net_display_usage_from_functable(func);
1241 return run_rpc_command(c, NULL,PI_SAMR, 0,
1242 rpc_user_list_internals,
1246 return net_run_function(c, argc, argv, "net rpc user", func);
1249 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1250 TALLOC_CTX *mem_ctx,
1251 struct rpc_sh_ctx *ctx,
1252 struct rpc_pipe_client *pipe_hnd,
1253 int argc, const char **argv)
1255 return rpc_user_list_internals(c, ctx->domain_sid, ctx->domain_name,
1256 ctx->cli, pipe_hnd, mem_ctx,
1260 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1261 TALLOC_CTX *mem_ctx,
1262 struct rpc_sh_ctx *ctx,
1263 struct rpc_pipe_client *pipe_hnd,
1264 int argc, const char **argv)
1266 return rpc_user_info_internals(c, ctx->domain_sid, ctx->domain_name,
1267 ctx->cli, pipe_hnd, mem_ctx,
1271 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1272 TALLOC_CTX *mem_ctx,
1273 struct rpc_sh_ctx *ctx,
1274 struct rpc_pipe_client *pipe_hnd,
1275 int argc, const char **argv,
1277 struct net_context *c,
1278 TALLOC_CTX *mem_ctx,
1279 struct rpc_sh_ctx *ctx,
1280 struct rpc_pipe_client *pipe_hnd,
1281 POLICY_HND *user_hnd,
1282 int argc, const char **argv))
1284 POLICY_HND connect_pol, domain_pol, user_pol;
1285 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1288 enum lsa_SidType type;
1291 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1292 return NT_STATUS_INVALID_PARAMETER;
1295 ZERO_STRUCT(connect_pol);
1296 ZERO_STRUCT(domain_pol);
1297 ZERO_STRUCT(user_pol);
1299 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1300 argv[0], NULL, NULL, &sid, &type);
1301 if (!NT_STATUS_IS_OK(result)) {
1302 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1307 if (type != SID_NAME_USER) {
1308 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1309 sid_type_lookup(type));
1310 result = NT_STATUS_NO_SUCH_USER;
1314 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1315 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1316 result = NT_STATUS_NO_SUCH_USER;
1320 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1322 MAXIMUM_ALLOWED_ACCESS,
1324 if (!NT_STATUS_IS_OK(result)) {
1328 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1330 MAXIMUM_ALLOWED_ACCESS,
1333 if (!NT_STATUS_IS_OK(result)) {
1337 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1339 MAXIMUM_ALLOWED_ACCESS,
1342 if (!NT_STATUS_IS_OK(result)) {
1346 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1349 if (is_valid_policy_hnd(&user_pol)) {
1350 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1352 if (is_valid_policy_hnd(&domain_pol)) {
1353 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1355 if (is_valid_policy_hnd(&connect_pol)) {
1356 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1361 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1362 TALLOC_CTX *mem_ctx,
1363 struct rpc_sh_ctx *ctx,
1364 struct rpc_pipe_client *pipe_hnd,
1365 POLICY_HND *user_hnd,
1366 int argc, const char **argv)
1369 union samr_UserInfo *info = NULL;
1372 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1373 return NT_STATUS_INVALID_PARAMETER;
1376 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1380 if (!NT_STATUS_IS_OK(result)) {
1384 d_printf("user rid: %d, group rid: %d\n",
1386 info->info21.primary_gid);
1391 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1392 TALLOC_CTX *mem_ctx,
1393 struct rpc_sh_ctx *ctx,
1394 struct rpc_pipe_client *pipe_hnd,
1395 int argc, const char **argv)
1397 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1398 rpc_sh_user_show_internals);
1401 #define FETCHSTR(name, rec) \
1402 do { if (strequal(ctx->thiscmd, name)) { \
1403 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1406 #define SETSTR(name, rec, flag) \
1407 do { if (strequal(ctx->thiscmd, name)) { \
1408 init_lsa_String(&(info->info21.rec), argv[0]); \
1409 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1412 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1413 TALLOC_CTX *mem_ctx,
1414 struct rpc_sh_ctx *ctx,
1415 struct rpc_pipe_client *pipe_hnd,
1416 POLICY_HND *user_hnd,
1417 int argc, const char **argv)
1420 const char *username;
1421 const char *oldval = "";
1422 union samr_UserInfo *info = NULL;
1425 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1427 return NT_STATUS_INVALID_PARAMETER;
1430 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1434 if (!NT_STATUS_IS_OK(result)) {
1438 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1440 FETCHSTR("fullname", full_name);
1441 FETCHSTR("homedir", home_directory);
1442 FETCHSTR("homedrive", home_drive);
1443 FETCHSTR("logonscript", logon_script);
1444 FETCHSTR("profilepath", profile_path);
1445 FETCHSTR("description", description);
1448 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1452 if (strcmp(argv[0], "NULL") == 0) {
1456 ZERO_STRUCT(info->info21);
1458 SETSTR("fullname", full_name, FULL_NAME);
1459 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1460 SETSTR("homedrive", home_drive, HOME_DRIVE);
1461 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1462 SETSTR("profilepath", profile_path, PROFILE_PATH);
1463 SETSTR("description", description, DESCRIPTION);
1465 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1470 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1471 ctx->thiscmd, oldval, argv[0]);
1478 #define HANDLEFLG(name, rec) \
1479 do { if (strequal(ctx->thiscmd, name)) { \
1480 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1482 newflags = oldflags | ACB_##rec; \
1484 newflags = oldflags & ~ACB_##rec; \
1487 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1488 TALLOC_CTX *mem_ctx,
1489 struct rpc_sh_ctx *ctx,
1490 struct rpc_pipe_client *pipe_hnd,
1491 int argc, const char **argv)
1493 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1494 rpc_sh_user_str_edit_internals);
1497 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1498 TALLOC_CTX *mem_ctx,
1499 struct rpc_sh_ctx *ctx,
1500 struct rpc_pipe_client *pipe_hnd,
1501 POLICY_HND *user_hnd,
1502 int argc, const char **argv)
1505 const char *username;
1506 const char *oldval = "unknown";
1507 uint32 oldflags, newflags;
1509 union samr_UserInfo *info = NULL;
1512 ((argc == 1) && !strequal(argv[0], "yes") &&
1513 !strequal(argv[0], "no"))) {
1514 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1516 return NT_STATUS_INVALID_PARAMETER;
1519 newval = strequal(argv[0], "yes");
1521 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1525 if (!NT_STATUS_IS_OK(result)) {
1529 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1530 oldflags = info->info21.acct_flags;
1531 newflags = info->info21.acct_flags;
1533 HANDLEFLG("disabled", DISABLED);
1534 HANDLEFLG("pwnotreq", PWNOTREQ);
1535 HANDLEFLG("autolock", AUTOLOCK);
1536 HANDLEFLG("pwnoexp", PWNOEXP);
1539 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1543 ZERO_STRUCT(info->info21);
1545 info->info21.acct_flags = newflags;
1546 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1548 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1553 if (NT_STATUS_IS_OK(result)) {
1554 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1555 ctx->thiscmd, oldval, argv[0]);
1563 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1564 TALLOC_CTX *mem_ctx,
1565 struct rpc_sh_ctx *ctx,
1566 struct rpc_pipe_client *pipe_hnd,
1567 int argc, const char **argv)
1569 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1570 rpc_sh_user_flag_edit_internals);
1573 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1574 TALLOC_CTX *mem_ctx,
1575 struct rpc_sh_ctx *ctx)
1577 static struct rpc_sh_cmd cmds[] = {
1579 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1580 "Show/Set a user's full name" },
1582 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1583 "Show/Set a user's home directory" },
1585 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1586 "Show/Set a user's home drive" },
1588 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1589 "Show/Set a user's logon script" },
1591 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1592 "Show/Set a user's profile path" },
1594 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1595 "Show/Set a user's description" },
1597 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1598 "Show/Set whether a user is disabled" },
1600 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1601 "Show/Set whether a user locked out" },
1603 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1604 "Show/Set whether a user does not need a password" },
1606 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1607 "Show/Set whether a user's password does not expire" },
1609 { NULL, NULL, 0, NULL, NULL }
1615 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1616 TALLOC_CTX *mem_ctx,
1617 struct rpc_sh_ctx *ctx)
1619 static struct rpc_sh_cmd cmds[] = {
1621 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1622 "List available users" },
1624 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1625 "List the domain groups a user is member of" },
1627 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1628 "Show info about a user" },
1630 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1631 "Show/Modify a user's fields" },
1633 { NULL, NULL, 0, NULL, NULL }
1639 /****************************************************************************/
1642 * Basic usage function for 'net rpc group'
1643 * @param argc Standard main() style argc.
1644 * @param argv Standard main() style argv. Initial components are already
1648 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1650 return net_group_usage(c, argc, argv);
1654 * Delete group on a remote RPC server
1656 * All parameters are provided by the run_rpc_command function, except for
1657 * argc, argv which are passes through.
1659 * @param domain_sid The domain sid acquired from the remote server
1660 * @param cli A cli_state connected to the server.
1661 * @param mem_ctx Talloc context, destoyed on completion of the function.
1662 * @param argc Standard main() style argc
1663 * @param argv Standard main() style argv. Initial components are already
1666 * @return Normal NTSTATUS return.
1669 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1670 const DOM_SID *domain_sid,
1671 const char *domain_name,
1672 struct cli_state *cli,
1673 struct rpc_pipe_client *pipe_hnd,
1674 TALLOC_CTX *mem_ctx,
1678 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1679 bool group_is_primary = false;
1680 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1682 struct samr_RidTypeArray *rids = NULL;
1685 /* DOM_GID *user_gids; */
1687 struct samr_Ids group_rids, name_types;
1688 struct lsa_String lsa_acct_name;
1689 union samr_UserInfo *info = NULL;
1691 if (argc < 1 || c->display_usage) {
1692 rpc_group_usage(c, argc,argv);
1693 return NT_STATUS_OK; /* ok? */
1696 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1698 MAXIMUM_ALLOWED_ACCESS,
1701 if (!NT_STATUS_IS_OK(result)) {
1702 d_fprintf(stderr, "Request samr_Connect2 failed\n");
1706 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1708 MAXIMUM_ALLOWED_ACCESS,
1709 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1712 if (!NT_STATUS_IS_OK(result)) {
1713 d_fprintf(stderr, "Request open_domain failed\n");
1717 init_lsa_String(&lsa_acct_name, argv[0]);
1719 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1725 if (!NT_STATUS_IS_OK(result)) {
1726 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1730 switch (name_types.ids[0])
1732 case SID_NAME_DOM_GRP:
1733 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1735 MAXIMUM_ALLOWED_ACCESS,
1738 if (!NT_STATUS_IS_OK(result)) {
1739 d_fprintf(stderr, "Request open_group failed");
1743 group_rid = group_rids.ids[0];
1745 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1749 if (!NT_STATUS_IS_OK(result)) {
1750 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1754 if (c->opt_verbose) {
1755 d_printf("Domain Group %s (rid: %d) has %d members\n",
1756 argv[0],group_rid, rids->count);
1759 /* Check if group is anyone's primary group */
1760 for (i = 0; i < rids->count; i++)
1762 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1764 MAXIMUM_ALLOWED_ACCESS,
1768 if (!NT_STATUS_IS_OK(result)) {
1769 d_fprintf(stderr, "Unable to open group member %d\n",
1774 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1779 if (!NT_STATUS_IS_OK(result)) {
1780 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",
1785 if (info->info21.primary_gid == group_rid) {
1786 if (c->opt_verbose) {
1787 d_printf("Group is primary group of %s\n",
1788 info->info21.account_name.string);
1790 group_is_primary = true;
1793 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1796 if (group_is_primary) {
1797 d_fprintf(stderr, "Unable to delete group because some "
1798 "of it's members have it as primary group\n");
1799 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1803 /* remove all group members */
1804 for (i = 0; i < rids->count; i++)
1807 d_printf("Remove group member %d...",
1809 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1813 if (NT_STATUS_IS_OK(result)) {
1818 d_printf("failed\n");
1823 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1827 /* removing a local group is easier... */
1828 case SID_NAME_ALIAS:
1829 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1831 MAXIMUM_ALLOWED_ACCESS,
1835 if (!NT_STATUS_IS_OK(result)) {
1836 d_fprintf(stderr, "Request open_alias failed\n");
1840 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1844 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1845 argv[0],sid_type_lookup(name_types.ids[0]));
1846 result = NT_STATUS_UNSUCCESSFUL;
1850 if (NT_STATUS_IS_OK(result)) {
1852 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types.ids[0]),argv[0]);
1854 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1855 get_friendly_nt_error_msg(result));
1863 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1865 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_group_delete_internals,
1869 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1871 NET_API_STATUS status;
1872 struct GROUP_INFO_1 info1;
1873 uint32_t parm_error = 0;
1875 if (argc != 1 || c->display_usage) {
1876 rpc_group_usage(c, argc, argv);
1882 info1.grpi1_name = argv[0];
1883 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1884 info1.grpi1_comment = c->opt_comment;
1887 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1890 d_fprintf(stderr, "Failed to add group '%s' with: %s.\n",
1891 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1895 d_printf("Added group '%s'.\n", argv[0]);
1901 static NTSTATUS rpc_alias_add_internals(struct net_context *c,
1902 const DOM_SID *domain_sid,
1903 const char *domain_name,
1904 struct cli_state *cli,
1905 struct rpc_pipe_client *pipe_hnd,
1906 TALLOC_CTX *mem_ctx,
1910 POLICY_HND connect_pol, domain_pol, alias_pol;
1911 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1912 union samr_AliasInfo alias_info;
1913 struct lsa_String alias_name;
1916 if (argc != 1 || c->display_usage) {
1917 rpc_group_usage(c, argc, argv);
1918 return NT_STATUS_OK;
1921 init_lsa_String(&alias_name, argv[0]);
1923 /* Get sam policy handle */
1925 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1927 MAXIMUM_ALLOWED_ACCESS,
1929 if (!NT_STATUS_IS_OK(result)) goto done;
1931 /* Get domain policy handle */
1933 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1935 MAXIMUM_ALLOWED_ACCESS,
1936 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1938 if (!NT_STATUS_IS_OK(result)) goto done;
1940 /* Create the group */
1942 result = rpccli_samr_CreateDomAlias(pipe_hnd, mem_ctx,
1945 MAXIMUM_ALLOWED_ACCESS,
1948 if (!NT_STATUS_IS_OK(result)) goto done;
1950 if (strlen(c->opt_comment) == 0) goto done;
1952 /* We've got a comment to set */
1954 init_lsa_String(&alias_info.description, c->opt_comment);
1956 result = rpccli_samr_SetAliasInfo(pipe_hnd, mem_ctx,
1961 if (!NT_STATUS_IS_OK(result)) goto done;
1964 if (NT_STATUS_IS_OK(result))
1965 DEBUG(5, ("add alias succeeded\n"));
1967 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1972 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1974 if (c->opt_localgroup)
1975 return run_rpc_command(c, NULL, PI_SAMR, 0,
1976 rpc_alias_add_internals,
1979 return rpc_group_add_internals(c, argc, argv);
1982 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1983 TALLOC_CTX *mem_ctx,
1986 enum lsa_SidType *type)
1988 DOM_SID *sids = NULL;
1989 enum lsa_SidType *types = NULL;
1990 struct rpc_pipe_client *pipe_hnd;
1992 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1994 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
1999 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
2000 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2002 if (!NT_STATUS_IS_OK(result)) {
2006 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2007 &name, NULL, 1, &sids, &types);
2009 if (NT_STATUS_IS_OK(result)) {
2010 sid_copy(sid, &sids[0]);
2014 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
2018 TALLOC_FREE(pipe_hnd);
2021 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
2023 /* Try as S-1-5-whatever */
2027 if (string_to_sid(&tmp_sid, name)) {
2028 sid_copy(sid, &tmp_sid);
2029 *type = SID_NAME_UNKNOWN;
2030 result = NT_STATUS_OK;
2037 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2038 TALLOC_CTX *mem_ctx,
2039 const DOM_SID *group_sid,
2042 POLICY_HND connect_pol, domain_pol;
2045 POLICY_HND group_pol;
2047 struct samr_Ids rids, rid_types;
2048 struct lsa_String lsa_acct_name;
2052 sid_copy(&sid, group_sid);
2054 if (!sid_split_rid(&sid, &group_rid)) {
2055 return NT_STATUS_UNSUCCESSFUL;
2058 /* Get sam policy handle */
2059 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2061 MAXIMUM_ALLOWED_ACCESS,
2063 if (!NT_STATUS_IS_OK(result)) {
2067 /* Get domain policy handle */
2068 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2070 MAXIMUM_ALLOWED_ACCESS,
2073 if (!NT_STATUS_IS_OK(result)) {
2077 init_lsa_String(&lsa_acct_name, member);
2079 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2086 if (!NT_STATUS_IS_OK(result)) {
2087 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2091 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2093 MAXIMUM_ALLOWED_ACCESS,
2097 if (!NT_STATUS_IS_OK(result)) {
2101 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
2104 0x0005); /* unknown flags */
2107 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2111 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2112 TALLOC_CTX *mem_ctx,
2113 const DOM_SID *alias_sid,
2116 POLICY_HND connect_pol, domain_pol;
2119 POLICY_HND alias_pol;
2122 enum lsa_SidType member_type;
2126 sid_copy(&sid, alias_sid);
2128 if (!sid_split_rid(&sid, &alias_rid)) {
2129 return NT_STATUS_UNSUCCESSFUL;
2132 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2133 member, &member_sid, &member_type);
2135 if (!NT_STATUS_IS_OK(result)) {
2136 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2140 /* Get sam policy handle */
2141 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2143 MAXIMUM_ALLOWED_ACCESS,
2145 if (!NT_STATUS_IS_OK(result)) {
2149 /* Get domain policy handle */
2150 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2152 MAXIMUM_ALLOWED_ACCESS,
2155 if (!NT_STATUS_IS_OK(result)) {
2159 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2161 MAXIMUM_ALLOWED_ACCESS,
2165 if (!NT_STATUS_IS_OK(result)) {
2169 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
2173 if (!NT_STATUS_IS_OK(result)) {
2178 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2182 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2183 const DOM_SID *domain_sid,
2184 const char *domain_name,
2185 struct cli_state *cli,
2186 struct rpc_pipe_client *pipe_hnd,
2187 TALLOC_CTX *mem_ctx,
2192 enum lsa_SidType group_type;
2194 if (argc != 2 || c->display_usage) {
2196 "net rpc group addmem <group> <member>\n"
2197 " Add a member to a group\n"
2198 " group\tGroup to add member to\n"
2199 " member\tMember to add to group\n");
2200 return NT_STATUS_UNSUCCESSFUL;
2203 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2204 &group_sid, &group_type))) {
2205 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2206 return NT_STATUS_UNSUCCESSFUL;
2209 if (group_type == SID_NAME_DOM_GRP) {
2210 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2211 &group_sid, argv[1]);
2213 if (!NT_STATUS_IS_OK(result)) {
2214 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2215 argv[1], argv[0], nt_errstr(result));
2220 if (group_type == SID_NAME_ALIAS) {
2221 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2222 &group_sid, argv[1]);
2224 if (!NT_STATUS_IS_OK(result)) {
2225 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2226 argv[1], argv[0], nt_errstr(result));
2231 d_fprintf(stderr, "Can only add members to global or local groups "
2232 "which %s is not\n", argv[0]);
2234 return NT_STATUS_UNSUCCESSFUL;
2237 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2239 return run_rpc_command(c, NULL, PI_SAMR, 0,
2240 rpc_group_addmem_internals,
2244 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2245 struct rpc_pipe_client *pipe_hnd,
2246 TALLOC_CTX *mem_ctx,
2247 const DOM_SID *group_sid,
2250 POLICY_HND connect_pol, domain_pol;
2253 POLICY_HND group_pol;
2255 struct samr_Ids rids, rid_types;
2256 struct lsa_String lsa_acct_name;
2260 sid_copy(&sid, group_sid);
2262 if (!sid_split_rid(&sid, &group_rid))
2263 return NT_STATUS_UNSUCCESSFUL;
2265 /* Get sam policy handle */
2266 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2268 MAXIMUM_ALLOWED_ACCESS,
2270 if (!NT_STATUS_IS_OK(result))
2273 /* Get domain policy handle */
2274 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2276 MAXIMUM_ALLOWED_ACCESS,
2279 if (!NT_STATUS_IS_OK(result))
2282 init_lsa_String(&lsa_acct_name, member);
2284 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2290 if (!NT_STATUS_IS_OK(result)) {
2291 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2295 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2297 MAXIMUM_ALLOWED_ACCESS,
2301 if (!NT_STATUS_IS_OK(result))
2304 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2309 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2313 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2314 TALLOC_CTX *mem_ctx,
2315 const DOM_SID *alias_sid,
2318 POLICY_HND connect_pol, domain_pol;
2321 POLICY_HND alias_pol;
2324 enum lsa_SidType member_type;
2328 sid_copy(&sid, alias_sid);
2330 if (!sid_split_rid(&sid, &alias_rid))
2331 return NT_STATUS_UNSUCCESSFUL;
2333 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2334 member, &member_sid, &member_type);
2336 if (!NT_STATUS_IS_OK(result)) {
2337 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2341 /* Get sam policy handle */
2342 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2344 MAXIMUM_ALLOWED_ACCESS,
2346 if (!NT_STATUS_IS_OK(result)) {
2350 /* Get domain policy handle */
2351 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2353 MAXIMUM_ALLOWED_ACCESS,
2356 if (!NT_STATUS_IS_OK(result)) {
2360 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2362 MAXIMUM_ALLOWED_ACCESS,
2366 if (!NT_STATUS_IS_OK(result))
2369 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2373 if (!NT_STATUS_IS_OK(result))
2377 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2381 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2382 const DOM_SID *domain_sid,
2383 const char *domain_name,
2384 struct cli_state *cli,
2385 struct rpc_pipe_client *pipe_hnd,
2386 TALLOC_CTX *mem_ctx,
2391 enum lsa_SidType group_type;
2393 if (argc != 2 || c->display_usage) {
2395 "net rpc group delmem <group> <member>\n"
2396 " Delete a member from a group\n"
2397 " group\tGroup to delete member from\n"
2398 " member\tMember to delete from group\n");
2399 return NT_STATUS_UNSUCCESSFUL;
2402 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2403 &group_sid, &group_type))) {
2404 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2405 return NT_STATUS_UNSUCCESSFUL;
2408 if (group_type == SID_NAME_DOM_GRP) {
2409 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2410 &group_sid, argv[1]);
2412 if (!NT_STATUS_IS_OK(result)) {
2413 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2414 argv[1], argv[0], nt_errstr(result));
2419 if (group_type == SID_NAME_ALIAS) {
2420 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2421 &group_sid, argv[1]);
2423 if (!NT_STATUS_IS_OK(result)) {
2424 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2425 argv[1], argv[0], nt_errstr(result));
2430 d_fprintf(stderr, "Can only delete members from global or local groups "
2431 "which %s is not\n", argv[0]);
2433 return NT_STATUS_UNSUCCESSFUL;
2436 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2438 return run_rpc_command(c, NULL, PI_SAMR, 0,
2439 rpc_group_delmem_internals,
2444 * List groups on a remote RPC server
2446 * All parameters are provided by the run_rpc_command function, except for
2447 * argc, argv which are passes through.
2449 * @param domain_sid The domain sid acquired from the remote server
2450 * @param cli A cli_state connected to the server.
2451 * @param mem_ctx Talloc context, destoyed on completion of the function.
2452 * @param argc Standard main() style argc
2453 * @param argv Standard main() style argv. Initial components are already
2456 * @return Normal NTSTATUS return.
2459 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2460 const DOM_SID *domain_sid,
2461 const char *domain_name,
2462 struct cli_state *cli,
2463 struct rpc_pipe_client *pipe_hnd,
2464 TALLOC_CTX *mem_ctx,
2468 POLICY_HND connect_pol, domain_pol;
2469 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2470 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2471 struct samr_SamArray *groups = NULL;
2472 bool global = false;
2474 bool builtin = false;
2476 if (c->display_usage) {
2478 "net rpc group list [global] [local] [builtin]\n"
2479 " List groups on RPC server\n"
2480 " global\tList global groups\n"
2481 " local\tList local groups\n"
2482 " builtin\tList builtin groups\n"
2483 " If none of global, local or builtin is "
2484 "specified, all three options are considered set\n");
2485 return NT_STATUS_OK;
2494 for (i=0; i<argc; i++) {
2495 if (strequal(argv[i], "global"))
2498 if (strequal(argv[i], "local"))
2501 if (strequal(argv[i], "builtin"))
2505 /* Get sam policy handle */
2507 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2509 MAXIMUM_ALLOWED_ACCESS,
2511 if (!NT_STATUS_IS_OK(result)) {
2515 /* Get domain policy handle */
2517 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2519 MAXIMUM_ALLOWED_ACCESS,
2520 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2522 if (!NT_STATUS_IS_OK(result)) {
2526 /* Query domain groups */
2527 if (c->opt_long_list_entries)
2528 d_printf("\nGroup name Comment"
2529 "\n-----------------------------\n");
2531 uint32_t max_size, total_size, returned_size;
2532 union samr_DispInfo info;
2536 get_query_dispinfo_params(
2537 loop_count, &max_entries, &max_size);
2539 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2548 num_entries = info.info3.count;
2549 start_idx += info.info3.count;
2551 if (!NT_STATUS_IS_OK(result) &&
2552 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2555 for (i = 0; i < num_entries; i++) {
2557 const char *group = NULL;
2558 const char *desc = NULL;
2560 group = info.info3.entries[i].account_name.string;
2561 desc = info.info3.entries[i].description.string;
2563 if (c->opt_long_list_entries)
2564 printf("%-21.21s %-50.50s\n",
2567 printf("%s\n", group);
2569 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2570 /* query domain aliases */
2575 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2581 if (!NT_STATUS_IS_OK(result) &&
2582 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2585 for (i = 0; i < num_entries; i++) {
2587 const char *description = NULL;
2589 if (c->opt_long_list_entries) {
2591 POLICY_HND alias_pol;
2592 union samr_AliasInfo *info = NULL;
2594 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2597 groups->entries[i].idx,
2599 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2603 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2605 description = info->description.string;
2609 if (description != NULL) {
2610 printf("%-21.21s %-50.50s\n",
2611 groups->entries[i].name.string,
2614 printf("%s\n", groups->entries[i].name.string);
2617 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2618 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2619 /* Get builtin policy handle */
2621 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2623 MAXIMUM_ALLOWED_ACCESS,
2624 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2626 if (!NT_STATUS_IS_OK(result)) {
2629 /* query builtin aliases */
2632 if (!builtin) break;
2634 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2640 if (!NT_STATUS_IS_OK(result) &&
2641 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2644 for (i = 0; i < num_entries; i++) {
2646 const char *description = NULL;
2648 if (c->opt_long_list_entries) {
2650 POLICY_HND alias_pol;
2651 union samr_AliasInfo *info = NULL;
2653 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2656 groups->entries[i].idx,
2658 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2662 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2664 description = info->description.string;
2668 if (description != NULL) {
2669 printf("%-21.21s %-50.50s\n",
2670 groups->entries[i].name.string,
2673 printf("%s\n", groups->entries[i].name.string);
2676 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2682 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2684 return run_rpc_command(c, NULL, PI_SAMR, 0,
2685 rpc_group_list_internals,
2689 static NTSTATUS rpc_list_group_members(struct net_context *c,
2690 struct rpc_pipe_client *pipe_hnd,
2691 TALLOC_CTX *mem_ctx,
2692 const char *domain_name,
2693 const DOM_SID *domain_sid,
2694 POLICY_HND *domain_pol,
2698 POLICY_HND group_pol;
2699 uint32 num_members, *group_rids;
2701 struct samr_RidTypeArray *rids = NULL;
2702 struct lsa_Strings names;
2703 struct samr_Ids types;
2706 sid_to_fstring(sid_str, domain_sid);
2708 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2710 MAXIMUM_ALLOWED_ACCESS,
2714 if (!NT_STATUS_IS_OK(result))
2717 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2721 if (!NT_STATUS_IS_OK(result))
2724 num_members = rids->count;
2725 group_rids = rids->rids;
2727 while (num_members > 0) {
2728 int this_time = 512;
2730 if (num_members < this_time)
2731 this_time = num_members;
2733 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2740 if (!NT_STATUS_IS_OK(result))
2743 /* We only have users as members, but make the output
2744 the same as the output of alias members */
2746 for (i = 0; i < this_time; i++) {
2748 if (c->opt_long_list_entries) {
2749 printf("%s-%d %s\\%s %d\n", sid_str,
2750 group_rids[i], domain_name,
2751 names.names[i].string,
2754 printf("%s\\%s\n", domain_name,
2755 names.names[i].string);
2759 num_members -= this_time;
2763 return NT_STATUS_OK;
2766 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2767 struct rpc_pipe_client *pipe_hnd,
2768 TALLOC_CTX *mem_ctx,
2769 POLICY_HND *domain_pol,
2773 struct rpc_pipe_client *lsa_pipe;
2774 POLICY_HND alias_pol, lsa_pol;
2776 DOM_SID *alias_sids;
2779 enum lsa_SidType *types;
2781 struct lsa_SidArray sid_array;
2783 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2785 MAXIMUM_ALLOWED_ACCESS,
2789 if (!NT_STATUS_IS_OK(result))
2792 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2796 if (!NT_STATUS_IS_OK(result)) {
2797 d_fprintf(stderr, "Couldn't list alias members\n");
2801 num_members = sid_array.num_sids;
2803 if (num_members == 0) {
2804 return NT_STATUS_OK;
2807 lsa_pipe = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2808 PI_LSARPC, &result);
2810 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2811 nt_errstr(result) );
2815 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2816 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2818 if (!NT_STATUS_IS_OK(result)) {
2819 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2820 TALLOC_FREE(lsa_pipe);
2824 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2826 d_fprintf(stderr, "Out of memory\n");
2827 TALLOC_FREE(lsa_pipe);
2828 return NT_STATUS_NO_MEMORY;
2831 for (i=0; i<num_members; i++) {
2832 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2835 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2836 num_members, alias_sids,
2837 &domains, &names, &types);
2839 if (!NT_STATUS_IS_OK(result) &&
2840 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2841 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2842 TALLOC_FREE(lsa_pipe);
2846 for (i = 0; i < num_members; i++) {
2848 sid_to_fstring(sid_str, &alias_sids[i]);
2850 if (c->opt_long_list_entries) {
2851 printf("%s %s\\%s %d\n", sid_str,
2852 domains[i] ? domains[i] : "*unknown*",
2853 names[i] ? names[i] : "*unknown*", types[i]);
2856 printf("%s\\%s\n", domains[i], names[i]);
2858 printf("%s\n", sid_str);
2862 TALLOC_FREE(lsa_pipe);
2863 return NT_STATUS_OK;
2866 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2867 const DOM_SID *domain_sid,
2868 const char *domain_name,
2869 struct cli_state *cli,
2870 struct rpc_pipe_client *pipe_hnd,
2871 TALLOC_CTX *mem_ctx,
2876 POLICY_HND connect_pol, domain_pol;
2877 struct samr_Ids rids, rid_types;
2878 struct lsa_String lsa_acct_name;
2880 /* Get sam policy handle */
2882 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2884 MAXIMUM_ALLOWED_ACCESS,
2887 if (!NT_STATUS_IS_OK(result))
2890 /* Get domain policy handle */
2892 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2894 MAXIMUM_ALLOWED_ACCESS,
2895 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2898 if (!NT_STATUS_IS_OK(result))
2901 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2903 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2910 if (!NT_STATUS_IS_OK(result)) {
2912 /* Ok, did not find it in the global sam, try with builtin */
2914 DOM_SID sid_Builtin;
2916 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2918 sid_copy(&sid_Builtin, &global_sid_Builtin);
2920 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2922 MAXIMUM_ALLOWED_ACCESS,
2926 if (!NT_STATUS_IS_OK(result)) {
2927 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2931 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2938 if (!NT_STATUS_IS_OK(result)) {
2939 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2944 if (rids.count != 1) {
2945 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2949 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2950 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2951 domain_sid, &domain_pol,
2955 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2956 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2960 return NT_STATUS_NO_SUCH_GROUP;
2963 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2965 if (argc != 1 || c->display_usage) {
2966 return rpc_group_usage(c, argc, argv);
2969 return run_rpc_command(c, NULL, PI_SAMR, 0,
2970 rpc_group_members_internals,
2974 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2976 NET_API_STATUS status;
2977 struct GROUP_INFO_0 g0;
2981 d_printf("Usage: 'net rpc group rename group newname'\n");
2985 g0.grpi0_name = argv[1];
2987 status = NetGroupSetInfo(c->opt_host,
2994 d_fprintf(stderr, "Renaming group %s failed with: %s\n",
2995 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3003 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3005 if (argc != 2 || c->display_usage) {
3006 return rpc_group_usage(c, argc, argv);
3009 return rpc_group_rename_internals(c, argc, argv);
3013 * 'net rpc group' entrypoint.
3014 * @param argc Standard main() style argc
3015 * @param argc Standard main() style argv. Initial components are already
3019 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3021 NET_API_STATUS status;
3023 struct functable func[] = {
3028 "Create specified group",
3029 "net rpc group add\n"
3030 " Create specified group"
3036 "Delete specified group",
3037 "net rpc group delete\n"
3038 " Delete specified group"
3044 "Add member to group",
3045 "net rpc group addmem\n"
3046 " Add member to group"
3052 "Remove member from group",
3053 "net rpc group delmem\n"
3054 " Remove member from group"
3061 "net rpc group list\n"
3068 "List group members",
3069 "net rpc group members\n"
3070 " List group members"
3077 "net rpc group rename\n"
3080 {NULL, NULL, 0, NULL, NULL}
3083 status = libnetapi_init(&c->netapi_ctx);
3087 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3088 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3091 if (c->display_usage) {
3092 d_printf("Usage:\n");
3093 d_printf("net rpc group\n"
3094 " Alias for net rpc group list global local "
3096 net_display_usage_from_functable(func);
3100 return run_rpc_command(c, NULL, PI_SAMR, 0,
3101 rpc_group_list_internals,
3105 return net_run_function(c, argc, argv, "net rpc group", func);
3108 /****************************************************************************/
3110 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3112 return net_share_usage(c, argc, argv);
3116 * Add a share on a remote RPC server
3118 * All parameters are provided by the run_rpc_command function, except for
3119 * argc, argv which are passes through.
3121 * @param domain_sid The domain sid acquired from the remote server
3122 * @param cli A cli_state connected to the server.
3123 * @param mem_ctx Talloc context, destoyed on completion of the function.
3124 * @param argc Standard main() style argc
3125 * @param argv Standard main() style argv. Initial components are already
3128 * @return Normal NTSTATUS return.
3130 static NTSTATUS rpc_share_add_internals(struct net_context *c,
3131 const DOM_SID *domain_sid,
3132 const char *domain_name,
3133 struct cli_state *cli,
3134 struct rpc_pipe_client *pipe_hnd,
3135 TALLOC_CTX *mem_ctx,int argc,
3142 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3143 uint32 num_users=0, perms=0;
3144 char *password=NULL; /* don't allow a share password */
3146 union srvsvc_NetShareInfo info;
3147 struct srvsvc_NetShareInfo2 info2;
3148 uint32_t parm_error = 0;
3150 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3151 return NT_STATUS_NO_MEMORY;
3154 path = strchr(sharename, '=');
3156 return NT_STATUS_UNSUCCESSFUL;
3159 info2.name = sharename;
3161 info2.comment = c->opt_comment;
3162 info2.permissions = perms;
3163 info2.max_users = c->opt_maxusers;
3164 info2.current_users = num_users;
3166 info2.password = password;
3168 info.info2 = &info2;
3170 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
3179 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3181 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3182 return rpc_share_usage(c, argc, argv);
3184 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3185 rpc_share_add_internals,
3190 * Delete a share on a remote RPC server
3192 * All parameters are provided by the run_rpc_command function, except for
3193 * argc, argv which are passes through.
3195 * @param domain_sid The domain sid acquired from the remote server
3196 * @param cli A cli_state connected to the server.
3197 * @param mem_ctx Talloc context, destoyed on completion of the function.
3198 * @param argc Standard main() style argc
3199 * @param argv Standard main() style argv. Initial components are already
3202 * @return Normal NTSTATUS return.
3204 static NTSTATUS rpc_share_del_internals(struct net_context *c,
3205 const DOM_SID *domain_sid,
3206 const char *domain_name,
3207 struct cli_state *cli,
3208 struct rpc_pipe_client *pipe_hnd,
3209 TALLOC_CTX *mem_ctx,
3215 return rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
3223 * Delete a share on a remote RPC server
3225 * @param domain_sid The domain sid acquired from the remote server
3226 * @param argc Standard main() style argc
3227 * @param argv Standard main() style argv. Initial components are already
3230 * @return A shell status integer (0 for success)
3232 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3234 if (argc < 1 || c->display_usage) {
3235 return rpc_share_usage(c, argc, argv);
3237 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3238 rpc_share_del_internals,
3243 * Formatted print of share info
3245 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3248 static void display_share_info_1(struct net_context *c,
3249 struct srvsvc_NetShareInfo1 *r)
3251 if (c->opt_long_list_entries) {
3252 d_printf("%-12s %-8.8s %-50s\n",
3254 c->share_type[r->type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)],
3257 d_printf("%s\n", r->name);
3261 static WERROR get_share_info(struct net_context *c,
3262 struct rpc_pipe_client *pipe_hnd,
3263 TALLOC_CTX *mem_ctx,
3267 struct srvsvc_NetShareInfoCtr *info_ctr)
3271 union srvsvc_NetShareInfo info;
3273 /* no specific share requested, enumerate all */
3276 uint32_t preferred_len = 0xffffffff;
3277 uint32_t total_entries = 0;
3278 uint32_t resume_handle = 0;
3280 info_ctr->level = level;
3282 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
3292 /* request just one share */
3293 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
3300 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
3305 ZERO_STRUCTP(info_ctr);
3307 info_ctr->level = level;
3312 struct srvsvc_NetShareCtr1 *ctr1;
3314 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3315 W_ERROR_HAVE_NO_MEMORY(ctr1);
3318 ctr1->array = info.info1;
3320 info_ctr->ctr.ctr1 = ctr1;
3324 struct srvsvc_NetShareCtr2 *ctr2;
3326 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3327 W_ERROR_HAVE_NO_MEMORY(ctr2);
3330 ctr2->array = info.info2;
3332 info_ctr->ctr.ctr2 = ctr2;
3336 struct srvsvc_NetShareCtr502 *ctr502;
3338 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3339 W_ERROR_HAVE_NO_MEMORY(ctr502);
3342 ctr502->array = info.info502;
3344 info_ctr->ctr.ctr502 = ctr502;
3352 * List shares on a remote RPC server
3354 * All parameters are provided by the run_rpc_command function, except for
3355 * argc, argv which are passes through.
3357 * @param domain_sid The domain sid acquired from the remote server
3358 * @param cli A cli_state connected to the server.
3359 * @param mem_ctx Talloc context, destoyed on completion of the function.
3360 * @param argc Standard main() style argc
3361 * @param argv Standard main() style argv. Initial components are already
3364 * @return Normal NTSTATUS return.
3367 static NTSTATUS rpc_share_list_internals(struct net_context *c,
3368 const DOM_SID *domain_sid,
3369 const char *domain_name,
3370 struct cli_state *cli,
3371 struct rpc_pipe_client *pipe_hnd,
3372 TALLOC_CTX *mem_ctx,
3376 struct srvsvc_NetShareInfoCtr info_ctr;
3377 struct srvsvc_NetShareCtr1 ctr1;
3379 uint32 i, level = 1;
3381 ZERO_STRUCT(info_ctr);
3385 info_ctr.ctr.ctr1 = &ctr1;
3387 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3389 if (!W_ERROR_IS_OK(result))
3392 /* Display results */
3394 if (c->opt_long_list_entries) {
3396 "\nEnumerating shared resources (exports) on remote server:\n\n"
3397 "\nShare name Type Description\n"
3398 "---------- ---- -----------\n");
3400 for (i = 0; i < info_ctr.ctr.ctr1->count; i++)
3401 display_share_info_1(c, &info_ctr.ctr.ctr1->array[i]);
3403 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3407 * 'net rpc share list' entrypoint.
3408 * @param argc Standard main() style argc
3409 * @param argv Standard main() style argv. Initial components are already
3412 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3414 if (c->display_usage) {
3416 "net rpc share list\n"
3417 " List shares on remote server\n");
3421 return run_rpc_command(c, NULL, PI_SRVSVC, 0, rpc_share_list_internals,
3425 static bool check_share_availability(struct cli_state *cli, const char *netname)
3427 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3428 d_printf("skipping [%s]: not a file share.\n", netname);
3438 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3439 const char *netname, uint32 type)
3441 /* only support disk shares */
3442 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3443 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3447 /* skip builtin shares */
3448 /* FIXME: should print$ be added too ? */
3449 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3450 strequal(netname,"global"))
3453 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3454 printf("excluding [%s]\n", netname);
3458 return check_share_availability(cli, netname);
3462 * Migrate shares from a remote RPC server to the local RPC server
3464 * All parameters are provided by the run_rpc_command function, except for
3465 * argc, argv which are passed through.
3467 * @param domain_sid The domain sid acquired from the remote server
3468 * @param cli A cli_state connected to the server.
3469 * @param mem_ctx Talloc context, destroyed on completion of the function.
3470 * @param argc Standard main() style argc
3471 * @param argv Standard main() style argv. Initial components are already
3474 * @return Normal NTSTATUS return.
3477 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3478 const DOM_SID *domain_sid,
3479 const char *domain_name,
3480 struct cli_state *cli,
3481 struct rpc_pipe_client *pipe_hnd,
3482 TALLOC_CTX *mem_ctx,
3487 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3488 struct srvsvc_NetShareInfoCtr ctr_src;
3490 struct rpc_pipe_client *srvsvc_pipe = NULL;
3491 struct cli_state *cli_dst = NULL;
3492 uint32 level = 502; /* includes secdesc */
3493 uint32_t parm_error = 0;
3495 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3497 if (!W_ERROR_IS_OK(result))
3500 /* connect destination PI_SRVSVC */
3501 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe, PI_SRVSVC);
3502 if (!NT_STATUS_IS_OK(nt_status))
3506 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3508 union srvsvc_NetShareInfo info;
3509 struct srvsvc_NetShareInfo502 info502 =
3510 ctr_src.ctr.ctr502->array[i];
3512 /* reset error-code */
3513 nt_status = NT_STATUS_UNSUCCESSFUL;
3515 if (!check_share_sanity(c, cli, info502.name, info502.type))
3518 /* finally add the share on the dst server */
3520 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3521 info502.name, info502.path, info502.comment);
3523 info.info502 = &info502;
3525 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3526 srvsvc_pipe->desthost,
3532 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3533 printf(" [%s] does already exist\n",
3538 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3539 printf("cannot add share: %s\n", dos_errstr(result));
3545 nt_status = NT_STATUS_OK;
3549 cli_shutdown(cli_dst);
3557 * Migrate shares from a rpc-server to another
3559 * @param argc Standard main() style argc
3560 * @param argv Standard main() style argv. Initial components are already
3563 * @return A shell status integer (0 for success)
3565 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3568 if (c->display_usage) {
3570 "net rpc share migrate shares\n"
3571 " Migrate shares to local server\n");
3576 printf("no server to migrate\n");
3580 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3581 rpc_share_migrate_shares_internals,
3588 * @param f file_info
3589 * @param mask current search mask
3590 * @param state arg-pointer
3593 static void copy_fn(const char *mnt, file_info *f,
3594 const char *mask, void *state)
3596 static NTSTATUS nt_status;
3597 static struct copy_clistate *local_state;
3598 static fstring filename, new_mask;
3601 struct net_context *c;
3603 local_state = (struct copy_clistate *)state;
3604 nt_status = NT_STATUS_UNSUCCESSFUL;
3608 if (strequal(f->name, ".") || strequal(f->name, ".."))
3611 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3614 if (f->mode & aDIR) {
3616 DEBUG(3,("got dir: %s\n", f->name));
3618 fstrcpy(dir, local_state->cwd);
3620 fstrcat(dir, f->name);
3622 switch (net_mode_share)
3624 case NET_MODE_SHARE_MIGRATE:
3625 /* create that directory */
3626 nt_status = net_copy_file(c, local_state->mem_ctx,
3627 local_state->cli_share_src,
3628 local_state->cli_share_dst,
3630 c->opt_acls? true : false,
3631 c->opt_attrs? true : false,
3632 c->opt_timestamps? true:false,
3636 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3640 if (!NT_STATUS_IS_OK(nt_status))
3641 printf("could not handle dir %s: %s\n",
3642 dir, nt_errstr(nt_status));
3644 /* search below that directory */
3645 fstrcpy(new_mask, dir);
3646 fstrcat(new_mask, "\\*");
3648 old_dir = local_state->cwd;
3649 local_state->cwd = dir;
3650 if (!sync_files(local_state, new_mask))
3651 printf("could not handle files\n");
3652 local_state->cwd = old_dir;
3659 fstrcpy(filename, local_state->cwd);
3660 fstrcat(filename, "\\");
3661 fstrcat(filename, f->name);
3663 DEBUG(3,("got file: %s\n", filename));
3665 switch (net_mode_share)
3667 case NET_MODE_SHARE_MIGRATE:
3668 nt_status = net_copy_file(c, local_state->mem_ctx,
3669 local_state->cli_share_src,
3670 local_state->cli_share_dst,
3672 c->opt_acls? true : false,
3673 c->opt_attrs? true : false,
3674 c->opt_timestamps? true: false,
3678 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3682 if (!NT_STATUS_IS_OK(nt_status))
3683 printf("could not handle file %s: %s\n",
3684 filename, nt_errstr(nt_status));
3689 * sync files, can be called recursivly to list files
3690 * and then call copy_fn for each file
3692 * @param cp_clistate pointer to the copy_clistate we work with
3693 * @param mask the current search mask
3695 * @return Boolean result
3697 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3699 struct cli_state *targetcli;
3700 char *targetpath = NULL;
3702 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3704 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate->cli_share_src,
3705 mask, &targetcli, &targetpath ) ) {
3706 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3707 mask, cli_errstr(cp_clistate->cli_share_src));
3711 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3712 d_fprintf(stderr, "listing %s failed with error: %s\n",
3713 mask, cli_errstr(targetcli));
3722 * Set the top level directory permissions before we do any further copies.
3723 * Should set up ACL inheritance.
3726 bool copy_top_level_perms(struct net_context *c,
3727 struct copy_clistate *cp_clistate,
3728 const char *sharename)
3730 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3732 switch (net_mode_share) {
3733 case NET_MODE_SHARE_MIGRATE:
3734 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3735 nt_status = net_copy_fileattr(c,
3736 cp_clistate->mem_ctx,
3737 cp_clistate->cli_share_src,
3738 cp_clistate->cli_share_dst,
3740 c->opt_acls? true : false,
3741 c->opt_attrs? true : false,
3742 c->opt_timestamps? true: false,
3746 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3750 if (!NT_STATUS_IS_OK(nt_status)) {
3751 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3752 sharename, nt_errstr(nt_status));
3760 * Sync all files inside a remote share to another share (over smb)
3762 * All parameters are provided by the run_rpc_command function, except for
3763 * argc, argv which are passes through.
3765 * @param domain_sid The domain sid acquired from the remote server
3766 * @param cli A cli_state connected to the server.
3767 * @param mem_ctx Talloc context, destoyed on completion of the function.
3768 * @param argc Standard main() style argc
3769 * @param argv Standard main() style argv. Initial components are already
3772 * @return Normal NTSTATUS return.
3775 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3776 const DOM_SID *domain_sid,
3777 const char *domain_name,
3778 struct cli_state *cli,
3779 struct rpc_pipe_client *pipe_hnd,
3780 TALLOC_CTX *mem_ctx,
3785 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3786 struct srvsvc_NetShareInfoCtr ctr_src;
3789 struct copy_clistate cp_clistate;
3790 bool got_src_share = false;
3791 bool got_dst_share = false;
3792 const char *mask = "\\*";
3795 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3797 nt_status = NT_STATUS_NO_MEMORY;
3801 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3804 if (!W_ERROR_IS_OK(result))
3807 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3809 struct srvsvc_NetShareInfo502 info502 =
3810 ctr_src.ctr.ctr502->array[i];
3812 if (!check_share_sanity(c, cli, info502.name, info502.type))
3815 /* one might not want to mirror whole discs :) */
3816 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3817 d_printf("skipping [%s]: builtin/hidden share\n", info502.name);
3821 switch (net_mode_share)
3823 case NET_MODE_SHARE_MIGRATE:
3827 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3830 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3832 c->opt_acls ? "including" : "without",
3833 c->opt_attrs ? "including" : "without",
3834 c->opt_timestamps ? "(preserving timestamps)" : "");
3836 cp_clistate.mem_ctx = mem_ctx;
3837 cp_clistate.cli_share_src = NULL;
3838 cp_clistate.cli_share_dst = NULL;
3839 cp_clistate.cwd = NULL;
3840 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3843 /* open share source */
3844 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3845 &cli->dest_ss, cli->desthost,
3846 info502.name, "A:");
3847 if (!NT_STATUS_IS_OK(nt_status))
3850 got_src_share = true;
3852 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3853 /* open share destination */
3854 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3855 NULL, dst, info502.name, "A:");
3856 if (!NT_STATUS_IS_OK(nt_status))
3859 got_dst_share = true;
3862 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3863 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", info502.name);
3864 nt_status = NT_STATUS_UNSUCCESSFUL;
3868 if (!sync_files(&cp_clistate, mask)) {
3869 d_fprintf(stderr, "could not handle files for share: %s\n", info502.name);
3870 nt_status = NT_STATUS_UNSUCCESSFUL;
3875 nt_status = NT_STATUS_OK;
3880 cli_shutdown(cp_clistate.cli_share_src);
3883 cli_shutdown(cp_clistate.cli_share_dst);
3890 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3892 if (c->display_usage) {
3894 "net share migrate files\n"
3895 " Migrate files to local server\n");
3900 d_printf("no server to migrate\n");
3904 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3905 rpc_share_migrate_files_internals,
3910 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3912 * All parameters are provided by the run_rpc_command function, except for
3913 * argc, argv which are passes through.
3915 * @param domain_sid The domain sid acquired from the remote server
3916 * @param cli A cli_state connected to the server.
3917 * @param mem_ctx Talloc context, destoyed on completion of the function.
3918 * @param argc Standard main() style argc
3919 * @param argv Standard main() style argv. Initial components are already
3922 * @return Normal NTSTATUS return.
3925 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3926 const DOM_SID *domain_sid,
3927 const char *domain_name,
3928 struct cli_state *cli,
3929 struct rpc_pipe_client *pipe_hnd,
3930 TALLOC_CTX *mem_ctx,
3935 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3936 struct srvsvc_NetShareInfoCtr ctr_src;
3937 union srvsvc_NetShareInfo info;
3939 struct rpc_pipe_client *srvsvc_pipe = NULL;
3940 struct cli_state *cli_dst = NULL;
3941 uint32 level = 502; /* includes secdesc */
3942 uint32_t parm_error = 0;
3944 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3947 if (!W_ERROR_IS_OK(result))
3950 /* connect destination PI_SRVSVC */
3951 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe, PI_SRVSVC);
3952 if (!NT_STATUS_IS_OK(nt_status))
3956 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3958 struct srvsvc_NetShareInfo502 info502 =
3959 ctr_src.ctr.ctr502->array[i];
3961 /* reset error-code */
3962 nt_status = NT_STATUS_UNSUCCESSFUL;
3964 if (!check_share_sanity(c, cli, info502.name, info502.type))
3967 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3968 info502.name, info502.path, info502.comment);
3971 display_sec_desc(info502.sd_buf.sd);
3973 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3974 info.info502 = &info502;
3976 /* finally modify the share on the dst server */
3977 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3978 srvsvc_pipe->desthost,
3984 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3985 printf("cannot set share-acl: %s\n", dos_errstr(result));
3991 nt_status = NT_STATUS_OK;
3995 cli_shutdown(cli_dst);
4003 * Migrate share-acls from a rpc-server to another
4005 * @param argc Standard main() style argc
4006 * @param argv Standard main() style argv. Initial components are already
4009 * @return A shell status integer (0 for success)
4011 static int rpc_share_migrate_security(struct net_context *c, int argc,
4014 if (c->display_usage) {
4016 "net rpc share migrate security\n"
4017 " Migrate share-acls to local server\n");
4022 d_printf("no server to migrate\n");
4026 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
4027 rpc_share_migrate_security_internals,
4032 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4033 * from one server to another
4035 * @param argc Standard main() style argc
4036 * @param argv Standard main() style argv. Initial components are already
4039 * @return A shell status integer (0 for success)
4042 static int rpc_share_migrate_all(struct net_context *c, int argc,
4047 if (c->display_usage) {
4049 "net rpc share migrate all\n"
4050 " Migrates shares including all share settings\n");
4055 d_printf("no server to migrate\n");
4059 /* order is important. we don't want to be locked out by the share-acl
4060 * before copying files - gd */
4062 ret = run_rpc_command(c, NULL, PI_SRVSVC, 0,
4063 rpc_share_migrate_shares_internals, argc, argv);
4067 ret = run_rpc_command(c, NULL, PI_SRVSVC, 0,
4068 rpc_share_migrate_files_internals, argc, argv);
4072 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
4073 rpc_share_migrate_security_internals, argc,
4079 * 'net rpc share migrate' entrypoint.
4080 * @param argc Standard main() style argc
4081 * @param argv Standard main() style argv. Initial components are already
4084 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4087 struct functable func[] = {
4090 rpc_share_migrate_all,
4092 "Migrate shares from remote to local server",
4093 "net rpc share migrate all\n"
4094 " Migrate shares from remote to local server"
4098 rpc_share_migrate_files,
4100 "Migrate files from remote to local server",
4101 "net rpc share migrate files\n"
4102 " Migrate files from remote to local server"
4106 rpc_share_migrate_security,
4108 "Migrate share-ACLs from remote to local server",
4109 "net rpc share migrate security\n"
4110 " Migrate share-ACLs from remote to local server"
4114 rpc_share_migrate_shares,
4116 "Migrate shares from remote to local server",
4117 "net rpc share migrate shares\n"
4118 " Migrate shares from remote to local server"
4120 {NULL, NULL, 0, NULL, NULL}
4123 net_mode_share = NET_MODE_SHARE_MIGRATE;
4125 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4134 static int num_server_aliases;
4135 static struct full_alias *server_aliases;
4138 * Add an alias to the static list.
4140 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4142 if (server_aliases == NULL)
4143 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4145 server_aliases[num_server_aliases] = *alias;
4146 num_server_aliases += 1;
4150 * For a specific domain on the server, fetch all the aliases
4151 * and their members. Add all of them to the server_aliases.
4154 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4155 TALLOC_CTX *mem_ctx,
4156 POLICY_HND *connect_pol,
4157 const DOM_SID *domain_sid)
4159 uint32 start_idx, max_entries, num_entries, i;
4160 struct samr_SamArray *groups = NULL;
4162 POLICY_HND domain_pol;
4164 /* Get domain policy handle */
4166 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
4168 MAXIMUM_ALLOWED_ACCESS,
4169 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4171 if (!NT_STATUS_IS_OK(result))
4178 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
4184 for (i = 0; i < num_entries; i++) {
4186 POLICY_HND alias_pol;
4187 struct full_alias alias;
4188 struct lsa_SidArray sid_array;
4191 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
4193 MAXIMUM_ALLOWED_ACCESS,
4194 groups->entries[i].idx,
4196 if (!NT_STATUS_IS_OK(result))
4199 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
4202 if (!NT_STATUS_IS_OK(result))
4205 alias.num_members = sid_array.num_sids;
4207 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
4208 if (!NT_STATUS_IS_OK(result))
4211 alias.members = NULL;
4213 if (alias.num_members > 0) {
4214 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
4216 for (j = 0; j < alias.num_members; j++)
4217 sid_copy(&alias.members[j],
4218 sid_array.sids[j].sid);
4221 sid_copy(&alias.sid, domain_sid);
4222 sid_append_rid(&alias.sid, groups->entries[i].idx);
4224 push_alias(mem_ctx, &alias);
4226 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4228 result = NT_STATUS_OK;
4231 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4237 * Dump server_aliases as names for debugging purposes.
4240 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4241 const DOM_SID *domain_sid,
4242 const char *domain_name,
4243 struct cli_state *cli,
4244 struct rpc_pipe_client *pipe_hnd,
4245 TALLOC_CTX *mem_ctx,
4253 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4254 SEC_RIGHTS_MAXIMUM_ALLOWED,
4256 if (!NT_STATUS_IS_OK(result))
4259 for (i=0; i<num_server_aliases; i++) {
4262 enum lsa_SidType *types;
4265 struct full_alias *alias = &server_aliases[i];
4267 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4269 &domains, &names, &types);
4270 if (!NT_STATUS_IS_OK(result))
4273 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4275 if (alias->num_members == 0) {
4280 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4283 &domains, &names, &types);
4285 if (!NT_STATUS_IS_OK(result) &&
4286 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4289 for (j=0; j<alias->num_members; j++)
4290 DEBUG(1, ("%s\\%s (%d); ",
4291 domains[j] ? domains[j] : "*unknown*",
4292 names[j] ? names[j] : "*unknown*",types[j]));
4296 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4298 return NT_STATUS_OK;
4302 * Fetch a list of all server aliases and their members into
4306 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4307 const DOM_SID *domain_sid,
4308 const char *domain_name,
4309 struct cli_state *cli,
4310 struct rpc_pipe_client *pipe_hnd,
4311 TALLOC_CTX *mem_ctx,
4316 POLICY_HND connect_pol;
4318 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4320 MAXIMUM_ALLOWED_ACCESS,
4323 if (!NT_STATUS_IS_OK(result))
4326 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4327 &global_sid_Builtin);
4329 if (!NT_STATUS_IS_OK(result))
4332 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4335 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4340 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4342 token->num_sids = 4;
4344 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4345 d_fprintf(stderr, "malloc failed\n");
4346 token->num_sids = 0;
4350 token->user_sids[0] = *user_sid;
4351 sid_copy(&token->user_sids[1], &global_sid_World);
4352 sid_copy(&token->user_sids[2], &global_sid_Network);
4353 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4356 static void free_user_token(NT_USER_TOKEN *token)
4358 SAFE_FREE(token->user_sids);
4361 static bool is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4365 for (i=0; i<token->num_sids; i++) {
4366 if (sid_compare(sid, &token->user_sids[i]) == 0)
4372 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4374 if (is_sid_in_token(token, sid))
4377 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4378 if (!token->user_sids) {
4382 sid_copy(&token->user_sids[token->num_sids], sid);
4384 token->num_sids += 1;
4389 NT_USER_TOKEN token;
4392 static void dump_user_token(struct user_token *token)
4396 d_printf("%s\n", token->name);
4398 for (i=0; i<token->token.num_sids; i++) {
4399 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4403 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4407 for (i=0; i<alias->num_members; i++) {
4408 if (sid_compare(sid, &alias->members[i]) == 0)
4415 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4419 for (i=0; i<num_server_aliases; i++) {
4420 if (is_alias_member(&sid, &server_aliases[i]))
4421 add_sid_to_token(token, &server_aliases[i].sid);
4426 * We got a user token with all the SIDs we can know about without asking the
4427 * server directly. These are the user and domain group sids. All of these can
4428 * be members of aliases. So scan the list of aliases for each of the SIDs and
4429 * add them to the token.
4432 static void collect_alias_memberships(NT_USER_TOKEN *token)
4434 int num_global_sids = token->num_sids;
4437 for (i=0; i<num_global_sids; i++) {
4438 collect_sid_memberships(token, token->user_sids[i]);
4442 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4444 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4445 enum wbcSidType type;
4447 struct wbcDomainSid wsid;
4448 char *sid_str = NULL;
4450 uint32_t num_groups;
4451 gid_t *groups = NULL;
4454 fstr_sprintf(full_name, "%s%c%s",
4455 domain, *lp_winbind_separator(), user);
4457 /* First let's find out the user sid */
4459 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4461 if (!WBC_ERROR_IS_OK(wbc_status)) {
4462 DEBUG(1, ("winbind could not find %s: %s\n",
4463 full_name, wbcErrorString(wbc_status)));
4467 wbc_status = wbcSidToString(&wsid, &sid_str);
4468 if (!WBC_ERROR_IS_OK(wbc_status)) {
4472 if (type != SID_NAME_USER) {
4473 wbcFreeMemory(sid_str);
4474 DEBUG(1, ("%s is not a user\n", full_name));
4478 string_to_sid(&user_sid, sid_str);
4479 wbcFreeMemory(sid_str);
4482 init_user_token(token, &user_sid);
4484 /* And now the groups winbind knows about */
4486 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4487 if (!WBC_ERROR_IS_OK(wbc_status)) {
4488 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4489 full_name, wbcErrorString(wbc_status)));
4493 for (i = 0; i < num_groups; i++) {
4494 gid_t gid = groups[i];
4497 wbc_status = wbcGidToSid(gid, &wsid);
4498 if (!WBC_ERROR_IS_OK(wbc_status)) {
4499 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4500 gid, wbcErrorString(wbc_status)));
4501 wbcFreeMemory(groups);
4505 wbc_status = wbcSidToString(&wsid, &sid_str);
4506 if (!WBC_ERROR_IS_OK(wbc_status)) {
4507 wbcFreeMemory(groups);
4511 DEBUG(3, (" %s\n", sid_str));
4513 string_to_sid(&sid, sid_str);
4514 wbcFreeMemory(sid_str);
4517 add_sid_to_token(token, &sid);
4519 wbcFreeMemory(groups);
4525 * Get a list of all user tokens we want to look at
4528 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4529 struct user_token **user_tokens)
4531 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4532 uint32_t i, num_users;
4534 struct user_token *result;
4535 TALLOC_CTX *frame = NULL;
4537 if (lp_winbind_use_default_domain() &&
4538 (c->opt_target_workgroup == NULL)) {
4539 d_fprintf(stderr, "winbind use default domain = yes set, "
4540 "please specify a workgroup\n");
4544 /* Send request to winbind daemon */
4546 wbc_status = wbcListUsers(NULL, &num_users, &users);
4547 if (!WBC_ERROR_IS_OK(wbc_status)) {
4548 DEBUG(1, ("winbind could not list users: %s\n",
4549 wbcErrorString(wbc_status)));
4553 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4555 if (result == NULL) {
4556 DEBUG(1, ("Could not malloc sid array\n"));
4557 wbcFreeMemory(users);
4561 frame = talloc_stackframe();
4562 for (i=0; i < num_users; i++) {
4563 fstring domain, user;
4566 fstrcpy(result[i].name, users[i]);
4568 p = strchr(users[i], *lp_winbind_separator());
4570 DEBUG(3, ("%s\n", users[i]));
4573 fstrcpy(domain, c->opt_target_workgroup);
4574 fstrcpy(user, users[i]);
4577 fstrcpy(domain, users[i]);
4582 get_user_sids(domain, user, &(result[i].token));
4586 wbcFreeMemory(users);
4588 *num_tokens = num_users;
4589 *user_tokens = result;
4594 static bool get_user_tokens_from_file(FILE *f,
4596 struct user_token **tokens)
4598 struct user_token *token = NULL;
4603 if (fgets(line, sizeof(line)-1, f) == NULL) {
4607 if (line[strlen(line)-1] == '\n')
4608 line[strlen(line)-1] = '\0';
4610 if (line[0] == ' ') {
4614 string_to_sid(&sid, &line[1]);
4616 if (token == NULL) {
4617 DEBUG(0, ("File does not begin with username"));
4621 add_sid_to_token(&token->token, &sid);
4625 /* And a new user... */
4628 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4629 if (*tokens == NULL) {
4630 DEBUG(0, ("Could not realloc tokens\n"));
4634 token = &((*tokens)[*num_tokens-1]);
4636 fstrcpy(token->name, line);
4637 token->token.num_sids = 0;
4638 token->token.user_sids = NULL;
4647 * Show the list of all users that have access to a share
4650 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4651 TALLOC_CTX *mem_ctx,
4652 const char *netname,
4654 struct user_token *tokens)
4657 SEC_DESC *share_sd = NULL;
4658 SEC_DESC *root_sd = NULL;
4659 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4661 union srvsvc_NetShareInfo info;
4666 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4673 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4674 DEBUG(1, ("Coult not query secdesc for share %s\n",
4679 share_sd = info.info502->sd_buf.sd;
4680 if (share_sd == NULL) {
4681 DEBUG(1, ("Got no secdesc for share %s\n",
4687 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4691 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4694 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4697 for (i=0; i<num_tokens; i++) {
4700 if (share_sd != NULL) {
4701 if (!se_access_check(share_sd, &tokens[i].token,
4702 1, &acc_granted, &status)) {
4703 DEBUG(1, ("Could not check share_sd for "
4709 if (!NT_STATUS_IS_OK(status))
4713 if (root_sd == NULL) {
4714 d_printf(" %s\n", tokens[i].name);
4718 if (!se_access_check(root_sd, &tokens[i].token,
4719 1, &acc_granted, &status)) {
4720 DEBUG(1, ("Could not check root_sd for user %s\n",
4725 if (!NT_STATUS_IS_OK(status))
4728 d_printf(" %s\n", tokens[i].name);
4732 cli_close(cli, fnum);
4744 static void collect_share(const char *name, uint32 m,
4745 const char *comment, void *state)
4747 struct share_list *share_list = (struct share_list *)state;
4749 if (m != STYPE_DISKTREE)
4752 share_list->num_shares += 1;
4753 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4754 if (!share_list->shares) {
4755 share_list->num_shares = 0;
4758 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4762 * List shares on a remote RPC server, including the security descriptors
4764 * All parameters are provided by the run_rpc_command function, except for
4765 * argc, argv which are passes through.
4767 * @param domain_sid The domain sid acquired from the remote server
4768 * @param cli A cli_state connected to the server.
4769 * @param mem_ctx Talloc context, destoyed on completion of the function.
4770 * @param argc Standard main() style argc
4771 * @param argv Standard main() style argv. Initial components are already
4774 * @return Normal NTSTATUS return.
4777 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4778 const DOM_SID *domain_sid,
4779 const char *domain_name,
4780 struct cli_state *cli,
4781 struct rpc_pipe_client *pipe_hnd,
4782 TALLOC_CTX *mem_ctx,
4792 struct user_token *tokens = NULL;
4795 struct share_list share_list;
4800 f = fopen(argv[0], "r");
4804 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4805 return NT_STATUS_UNSUCCESSFUL;
4808 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4814 DEBUG(0, ("Could not read users from file\n"));
4815 return NT_STATUS_UNSUCCESSFUL;
4818 for (i=0; i<num_tokens; i++)
4819 collect_alias_memberships(&tokens[i].token);
4821 init_enum_hnd(&hnd, 0);
4823 share_list.num_shares = 0;
4824 share_list.shares = NULL;
4826 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4829 DEBUG(0, ("Error returning browse list: %s\n",
4834 for (i = 0; i < share_list.num_shares; i++) {
4835 char *netname = share_list.shares[i];
4837 if (netname[strlen(netname)-1] == '$')
4840 d_printf("%s\n", netname);
4842 show_userlist(pipe_hnd, mem_ctx, netname,
4843 num_tokens, tokens);
4846 for (i=0; i<num_tokens; i++) {
4847 free_user_token(&tokens[i].token);
4850 SAFE_FREE(share_list.shares);
4852 return NT_STATUS_OK;
4855 static int rpc_share_allowedusers(struct net_context *c, int argc,
4860 if (c->display_usage) {
4862 "net rpc share allowedusers\n"
4863 " List allowed users\n");
4867 result = run_rpc_command(c, NULL, PI_SAMR, 0,
4868 rpc_aliaslist_internals,
4873 result = run_rpc_command(c, NULL, PI_LSARPC, 0,
4879 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
4880 rpc_share_allowedusers_internals,
4884 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4887 struct user_token *tokens = NULL;
4891 net_usersidlist_usage(c, argc, argv);
4895 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4896 DEBUG(0, ("Could not get the user/sid list\n"));
4900 for (i=0; i<num_tokens; i++) {
4901 dump_user_token(&tokens[i]);
4902 free_user_token(&tokens[i].token);
4909 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4911 d_printf("net usersidlist\n"
4912 "\tprints out a list of all users the running winbind knows\n"
4913 "\tabout, together with all their SIDs. This is used as\n"
4914 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4916 net_common_flags_usage(c, argc, argv);
4921 * 'net rpc share' entrypoint.
4922 * @param argc Standard main() style argc
4923 * @param argv Standard main() style argv. Initial components are already
4927 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4929 struct functable func[] = {
4935 "net rpc share add\n"
4943 "net rpc share delete\n"
4948 rpc_share_allowedusers,
4950 "Modify allowed users",
4951 "net rpc share allowedusers\n"
4952 " Modify allowed users"
4958 "Migrate share to local server",
4959 "net rpc share migrate\n"
4960 " Migrate share to local server"
4967 "net rpc share list\n"
4970 {NULL, NULL, 0, NULL, NULL}
4975 if (c->display_usage) {
4979 " Alias for net rpc share list\n");
4980 net_display_usage_from_functable(func);
4984 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
4985 rpc_share_list_internals,
4989 return net_run_function(c, argc, argv, "net rpc share", func);
4992 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4993 TALLOC_CTX *mem_ctx,
4994 struct rpc_sh_ctx *ctx,
4995 struct rpc_pipe_client *pipe_hnd,
4996 int argc, const char **argv)
4998 return rpc_share_list_internals(c, ctx->domain_sid, ctx->domain_name,
4999 ctx->cli, pipe_hnd, mem_ctx,
5003 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5004 TALLOC_CTX *mem_ctx,
5005 struct rpc_sh_ctx *ctx,
5006 struct rpc_pipe_client *pipe_hnd,
5007 int argc, const char **argv)
5011 uint32_t parm_err = 0;
5012 union srvsvc_NetShareInfo info;
5013 struct srvsvc_NetShareInfo2 info2;
5015 if ((argc < 2) || (argc > 3)) {
5016 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
5018 return NT_STATUS_INVALID_PARAMETER;
5021 info2.name = argv[0];
5022 info2.type = STYPE_DISKTREE;
5023 info2.comment = (argc == 3) ? argv[2] : "";
5024 info2.permissions = 0;
5025 info2.max_users = 0;
5026 info2.current_users = 0;
5027 info2.path = argv[1];
5028 info2.password = NULL;
5030 info.info2 = &info2;
5032 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
5042 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5043 TALLOC_CTX *mem_ctx,
5044 struct rpc_sh_ctx *ctx,
5045 struct rpc_pipe_client *pipe_hnd,
5046 int argc, const char **argv)
5052 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
5053 return NT_STATUS_INVALID_PARAMETER;
5056 status = rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
5065 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5066 TALLOC_CTX *mem_ctx,
5067 struct rpc_sh_ctx *ctx,
5068 struct rpc_pipe_client *pipe_hnd,
5069 int argc, const char **argv)
5071 union srvsvc_NetShareInfo info;
5076 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
5077 return NT_STATUS_INVALID_PARAMETER;
5080 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
5086 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
5090 d_printf("Name: %s\n", info.info2->name);
5091 d_printf("Comment: %s\n", info.info2->comment);
5092 d_printf("Path: %s\n", info.info2->path);
5093 d_printf("Password: %s\n", info.info2->password);
5096 return werror_to_ntstatus(result);
5099 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5100 struct rpc_sh_ctx *ctx)
5102 static struct rpc_sh_cmd cmds[] = {
5104 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
5105 "List available shares" },
5107 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
5110 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
5113 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
5114 "Get information about a share" },
5116 { NULL, NULL, 0, NULL, NULL }
5122 /****************************************************************************/
5124 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5126 return net_file_usage(c, argc, argv);
5130 * Close a file on a remote RPC server
5132 * All parameters are provided by the run_rpc_command function, except for
5133 * argc, argv which are passes through.
5135 * @param c A net_context structure
5136 * @param domain_sid The domain sid acquired from the remote server
5137 * @param cli A cli_state connected to the server.
5138 * @param mem_ctx Talloc context, destoyed on completion of the function.
5139 * @param argc Standard main() style argc
5140 * @param argv Standard main() style argv. Initial components are already
5143 * @return Normal NTSTATUS return.
5145 static NTSTATUS rpc_file_close_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 return rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx,
5156 atoi(argv[0]), NULL);
5160 * Close a file on a remote RPC server
5162 * @param argc Standard main() style argc
5163 * @param argv Standard main() style argv. Initial components are already
5166 * @return A shell status integer (0 for success)
5168 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5170 if (argc < 1 || c->display_usage) {
5171 return rpc_file_usage(c, argc, argv);
5174 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
5175 rpc_file_close_internals,
5180 * Formatted print of open file info
5182 * @param r struct srvsvc_NetFileInfo3 contents
5185 static void display_file_info_3(struct srvsvc_NetFileInfo3 *r)
5187 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5188 r->fid, r->user, r->permissions, r->num_locks, r->path);
5192 * List open files on a remote RPC server
5194 * All parameters are provided by the run_rpc_command function, except for
5195 * argc, argv which are passes through.
5197 * @param c A net_context structure
5198 * @param domain_sid The domain sid acquired from the remote server
5199 * @param cli A cli_state connected to the server.
5200 * @param mem_ctx Talloc context, destoyed on completion of the function.
5201 * @param argc Standard main() style argc
5202 * @param argv Standard main() style argv. Initial components are already
5205 * @return Normal NTSTATUS return.
5208 static NTSTATUS rpc_file_list_internals(struct net_context *c,
5209 const DOM_SID *domain_sid,
5210 const char *domain_name,
5211 struct cli_state *cli,
5212 struct rpc_pipe_client *pipe_hnd,
5213 TALLOC_CTX *mem_ctx,
5217 struct srvsvc_NetFileInfoCtr info_ctr;
5218 struct srvsvc_NetFileCtr3 ctr3;
5221 uint32 preferred_len = 0xffffffff, i;
5222 const char *username=NULL;
5223 uint32_t total_entries = 0;
5224 uint32_t resume_handle = 0;
5226 /* if argc > 0, must be user command */
5228 username = smb_xstrdup(argv[0]);
5230 ZERO_STRUCT(info_ctr);
5234 info_ctr.ctr.ctr3 = &ctr3;
5236 status = rpccli_srvsvc_NetFileEnum(pipe_hnd, mem_ctx,
5246 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
5249 /* Display results */
5252 "\nEnumerating open files on remote server:\n\n"
5253 "\nFileId Opened by Perms Locks Path"
5254 "\n------ --------- ----- ----- ---- \n");
5255 for (i = 0; i < total_entries; i++)
5256 display_file_info_3(&info_ctr.ctr.ctr3->array[i]);
5258 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
5262 * List files for a user on a remote RPC server
5264 * @param argc Standard main() style argc
5265 * @param argv Standard main() style argv. Initial components are already
5268 * @return A shell status integer (0 for success)
5271 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5273 if (argc < 1 || c->display_usage) {
5274 return rpc_file_usage(c, argc, argv);
5277 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
5278 rpc_file_list_internals,
5283 * 'net rpc file' entrypoint.
5284 * @param argc Standard main() style argc
5285 * @param argv Standard main() style argv. Initial components are already
5289 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5291 struct functable func[] = {
5296 "Close opened file",
5297 "net rpc file close\n"
5298 " Close opened file"
5304 "List files opened by user",
5305 "net rpc file user\n"
5306 " List files opened by user"
5313 "Display information about opened file",
5314 "net rpc file info\n"
5315 " Display information about opened file"
5318 {NULL, NULL, 0, NULL, NULL}
5322 if (c->display_usage) {
5323 d_printf("Usage:\n");
5324 d_printf("net rpc file\n"
5325 " List opened files\n");
5326 net_display_usage_from_functable(func);
5330 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
5331 rpc_file_list_internals,
5335 return net_run_function(c, argc, argv, "net rpc file", func);
5339 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
5341 * All parameters are provided by the run_rpc_command function, except for
5342 * argc, argv which are passed through.
5344 * @param c A net_context structure
5345 * @param domain_sid The domain sid aquired from the remote server
5346 * @param cli A cli_state connected to the server.
5347 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5348 * @param argc Standard main() style argc
5349 * @param argv Standard main() style argv. Initial components are already
5352 * @return Normal NTSTATUS return.
5355 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5356 const DOM_SID *domain_sid,
5357 const char *domain_name,
5358 struct cli_state *cli,
5359 struct rpc_pipe_client *pipe_hnd,
5360 TALLOC_CTX *mem_ctx,
5364 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5366 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5368 if (NT_STATUS_IS_OK(result)) {
5369 d_printf("\nShutdown successfully aborted\n");
5370 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5372 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5378 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5380 * All parameters are provided by the run_rpc_command function, except for
5381 * argc, argv which are passed through.
5383 * @param c A net_context structure
5384 * @param domain_sid The domain sid aquired from the remote server
5385 * @param cli A cli_state connected to the server.
5386 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5387 * @param argc Standard main() style argc
5388 * @param argv Standard main() style argv. Initial components are already
5391 * @return Normal NTSTATUS return.
5394 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5395 const DOM_SID *domain_sid,
5396 const char *domain_name,
5397 struct cli_state *cli,
5398 struct rpc_pipe_client *pipe_hnd,
5399 TALLOC_CTX *mem_ctx,
5403 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5405 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5407 if (NT_STATUS_IS_OK(result)) {
5408 d_printf("\nShutdown successfully aborted\n");
5409 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5411 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5417 * ABORT the Shut down of a remote RPC server
5419 * @param argc Standard main() style argc
5420 * @param argv Standard main() style argv. Initial components are already
5423 * @return A shell status integer (0 for success)
5426 static int rpc_shutdown_abort(struct net_context *c, int argc,
5431 if (c->display_usage) {
5433 "net rpc abortshutdown\n"
5434 " Abort a scheduled shutdown\n");
5438 rc = run_rpc_command(c, NULL, PI_INITSHUTDOWN, 0,
5439 rpc_shutdown_abort_internals, argc, argv);
5444 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5446 return run_rpc_command(c, NULL, PI_WINREG, 0,
5447 rpc_reg_shutdown_abort_internals,
5452 * Shut down a remote RPC Server via initshutdown pipe
5454 * All parameters are provided by the run_rpc_command function, except for
5455 * argc, argv which are passes through.
5457 * @param c A net_context structure
5458 * @param domain_sid The domain sid aquired from the remote server
5459 * @param cli A cli_state connected to the server.
5460 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5461 * @param argc Standard main() style argc
5462 * @param argc Standard main() style argv. Initial components are already
5465 * @return Normal NTSTATUS return.
5468 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5469 const DOM_SID *domain_sid,
5470 const char *domain_name,
5471 struct cli_state *cli,
5472 struct rpc_pipe_client *pipe_hnd,
5473 TALLOC_CTX *mem_ctx,
5477 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5478 const char *msg = "This machine will be shutdown shortly";
5479 uint32 timeout = 20;
5480 struct initshutdown_String msg_string;
5481 struct initshutdown_String_sub s;
5483 if (c->opt_comment) {
5484 msg = c->opt_comment;
5486 if (c->opt_timeout) {
5487 timeout = c->opt_timeout;
5491 msg_string.name = &s;
5493 /* create an entry */
5494 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5495 &msg_string, timeout, c->opt_force, c->opt_reboot,
5498 if (NT_STATUS_IS_OK(result)) {
5499 d_printf("\nShutdown of remote machine succeeded\n");
5500 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5502 DEBUG(1,("Shutdown of remote machine failed!\n"));
5508 * Shut down a remote RPC Server via winreg pipe
5510 * All parameters are provided by the run_rpc_command function, except for
5511 * argc, argv which are passes through.
5513 * @param c A net_context structure
5514 * @param domain_sid The domain sid aquired from the remote server
5515 * @param cli A cli_state connected to the server.
5516 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5517 * @param argc Standard main() style argc
5518 * @param argc Standard main() style argv. Initial components are already
5521 * @return Normal NTSTATUS return.
5524 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5525 const DOM_SID *domain_sid,
5526 const char *domain_name,
5527 struct cli_state *cli,
5528 struct rpc_pipe_client *pipe_hnd,
5529 TALLOC_CTX *mem_ctx,
5533 const char *msg = "This machine will be shutdown shortly";
5534 uint32 timeout = 20;
5535 struct initshutdown_String msg_string;
5536 struct initshutdown_String_sub s;
5540 if (c->opt_comment) {
5541 msg = c->opt_comment;
5544 msg_string.name = &s;
5546 if (c->opt_timeout) {
5547 timeout = c->opt_timeout;
5550 /* create an entry */
5551 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5552 &msg_string, timeout, c->opt_force, c->opt_reboot,
5555 if (NT_STATUS_IS_OK(result)) {
5556 d_printf("\nShutdown of remote machine succeeded\n");
5558 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5559 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5560 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5562 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(werr));
5569 * Shut down a remote RPC server
5571 * @param argc Standard main() style argc
5572 * @param argc Standard main() style argv. Initial components are already
5575 * @return A shell status integer (0 for success)
5578 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5582 if (c->display_usage) {
5584 "net rpc shutdown\n"
5585 " Shut down a remote RPC server\n");
5589 rc = run_rpc_command(c, NULL, PI_INITSHUTDOWN, 0,
5590 rpc_init_shutdown_internals, argc, argv);
5593 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5594 rc = run_rpc_command(c, NULL, PI_WINREG, 0,
5595 rpc_reg_shutdown_internals, argc, argv);
5601 /***************************************************************************
5602 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5603 ***************************************************************************/
5606 * Add interdomain trust account to the RPC server.
5607 * All parameters (except for argc and argv) are passed by run_rpc_command
5610 * @param c A net_context structure
5611 * @param domain_sid The domain sid acquired from the server
5612 * @param cli A cli_state connected to the server.
5613 * @param mem_ctx Talloc context, destoyed on completion of the function.
5614 * @param argc Standard main() style argc
5615 * @param argc Standard main() style argv. Initial components are already
5618 * @return normal NTSTATUS return code
5621 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5622 const DOM_SID *domain_sid,
5623 const char *domain_name,
5624 struct cli_state *cli,
5625 struct rpc_pipe_client *pipe_hnd,
5626 TALLOC_CTX *mem_ctx,
5630 POLICY_HND connect_pol, domain_pol, user_pol;
5631 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5633 struct lsa_String lsa_acct_name;
5635 uint32 acct_flags=0;
5637 uint32_t access_granted = 0;
5638 union samr_UserInfo info;
5641 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5642 return NT_STATUS_INVALID_PARAMETER;
5646 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5649 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5650 return NT_STATUS_NO_MEMORY;
5653 strupper_m(acct_name);
5655 init_lsa_String(&lsa_acct_name, acct_name);
5657 /* Get samr policy handle */
5658 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5660 MAXIMUM_ALLOWED_ACCESS,
5662 if (!NT_STATUS_IS_OK(result)) {
5666 /* Get domain policy handle */
5667 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5669 MAXIMUM_ALLOWED_ACCESS,
5670 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5672 if (!NT_STATUS_IS_OK(result)) {
5676 /* Create trusting domain's account */
5677 acb_info = ACB_NORMAL;
5678 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5679 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5680 SAMR_USER_ACCESS_SET_PASSWORD |
5681 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5682 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5684 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5692 if (!NT_STATUS_IS_OK(result)) {
5698 struct samr_LogonHours hours;
5699 struct lsa_BinaryString parameters;
5700 const int units_per_week = 168;
5703 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5705 ZERO_STRUCT(notime);
5707 ZERO_STRUCT(parameters);
5709 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
5711 result = NT_STATUS_NO_MEMORY;
5714 hours.units_per_week = units_per_week;
5715 memset(hours.bits, 0xFF, units_per_week);
5717 init_samr_user_info23(&info.info23,
5718 notime, notime, notime,
5719 notime, notime, notime,
5720 NULL, NULL, NULL, NULL, NULL,
5721 NULL, NULL, NULL, NULL, ¶meters,
5722 0, 0, ACB_DOMTRUST, SAMR_FIELD_ACCT_FLAGS,
5724 0, 0, 0, 0, 0, 0, 0,
5727 SamOEMhashBlob(info.info23.password.data, 516,
5728 &cli->user_session_key);
5730 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5735 if (!NT_STATUS_IS_OK(result)) {
5736 DEBUG(0,("Could not set trust account password: %s\n",
5737 nt_errstr(result)));
5743 SAFE_FREE(acct_name);
5748 * Create interdomain trust account for a remote domain.
5750 * @param argc standard argc
5751 * @param argv standard argv without initial components
5753 * @return Integer status (0 means success)
5756 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5758 if (argc > 0 && !c->display_usage) {
5759 return run_rpc_command(c, NULL, PI_SAMR, 0,
5760 rpc_trustdom_add_internals, argc, argv);
5763 "net rpc trustdom add <domain>\n");
5770 * Remove interdomain trust account from the RPC server.
5771 * All parameters (except for argc and argv) are passed by run_rpc_command
5774 * @param c A net_context structure
5775 * @param domain_sid The domain sid acquired from the server
5776 * @param cli A cli_state connected to the server.
5777 * @param mem_ctx Talloc context, destoyed on completion of the function.
5778 * @param argc Standard main() style argc
5779 * @param argc Standard main() style argv. Initial components are already
5782 * @return normal NTSTATUS return code
5785 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5786 const DOM_SID *domain_sid,
5787 const char *domain_name,
5788 struct cli_state *cli,
5789 struct rpc_pipe_client *pipe_hnd,
5790 TALLOC_CTX *mem_ctx,
5794 POLICY_HND connect_pol, domain_pol, user_pol;
5795 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5797 DOM_SID trust_acct_sid;
5798 struct samr_Ids user_rids, name_types;
5799 struct lsa_String lsa_acct_name;
5802 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5803 return NT_STATUS_INVALID_PARAMETER;
5807 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5809 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5811 if (acct_name == NULL)
5812 return NT_STATUS_NO_MEMORY;
5814 strupper_m(acct_name);
5816 /* Get samr policy handle */
5817 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5819 MAXIMUM_ALLOWED_ACCESS,
5821 if (!NT_STATUS_IS_OK(result)) {
5825 /* Get domain policy handle */
5826 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5828 MAXIMUM_ALLOWED_ACCESS,
5829 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5831 if (!NT_STATUS_IS_OK(result)) {
5835 init_lsa_String(&lsa_acct_name, acct_name);
5837 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5844 if (!NT_STATUS_IS_OK(result)) {
5848 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5850 MAXIMUM_ALLOWED_ACCESS,
5854 if (!NT_STATUS_IS_OK(result)) {
5858 /* append the rid to the domain sid */
5859 sid_copy(&trust_acct_sid, domain_sid);
5860 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5864 /* remove the sid */
5866 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5869 if (!NT_STATUS_IS_OK(result)) {
5875 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5878 if (!NT_STATUS_IS_OK(result)) {
5882 if (!NT_STATUS_IS_OK(result)) {
5883 DEBUG(0,("Could not set trust account password: %s\n",
5884 nt_errstr(result)));
5893 * Delete interdomain trust account for a remote domain.
5895 * @param argc standard argc
5896 * @param argv standard argv without initial components
5898 * @return Integer status (0 means success)
5901 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5903 if (argc > 0 && !c->display_usage) {
5904 return run_rpc_command(c, NULL, PI_SAMR, 0,
5905 rpc_trustdom_del_internals, argc, argv);
5908 "net rpc trustdom del <domain>\n");
5913 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5914 struct cli_state *cli,
5915 TALLOC_CTX *mem_ctx,
5916 const char *domain_name)
5918 char *dc_name = NULL;
5919 const char *buffer = NULL;
5920 struct rpc_pipe_client *netr;
5923 /* Use NetServerEnum2 */
5925 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5927 return NT_STATUS_OK;
5930 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5931 for domain %s\n", domain_name));
5933 /* Try netr_GetDcName */
5935 netr = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &status);
5940 status = rpccli_netr_GetDcName(netr, mem_ctx,
5947 if (NT_STATUS_IS_OK(status)) {
5951 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5952 for domain %s\n", domain_name));
5958 * Establish trust relationship to a trusting domain.
5959 * Interdomain account must already be created on remote PDC.
5961 * @param c A net_context structure
5962 * @param argc standard argc
5963 * @param argv standard argv without initial components
5965 * @return Integer status (0 means success)
5968 static int rpc_trustdom_establish(struct net_context *c, int argc,
5971 struct cli_state *cli = NULL;
5972 struct sockaddr_storage server_ss;
5973 struct rpc_pipe_client *pipe_hnd = NULL;
5974 POLICY_HND connect_hnd;
5975 TALLOC_CTX *mem_ctx;
5977 DOM_SID *domain_sid;
5982 union lsa_PolicyInformation *info = NULL;
5985 * Connect to \\server\ipc$ as 'our domain' account with password
5988 if (argc != 1 || c->display_usage) {
5990 "net rpc trustdom establish <domain_name>\n");
5994 domain_name = smb_xstrdup(argv[0]);
5995 strupper_m(domain_name);
5997 /* account name used at first is our domain's name with '$' */
5998 asprintf(&acct_name, "%s$", lp_workgroup());
5999 strupper_m(acct_name);
6002 * opt_workgroup will be used by connection functions further,
6003 * hence it should be set to remote domain name instead of ours
6005 if (c->opt_workgroup) {
6006 c->opt_workgroup = smb_xstrdup(domain_name);
6009 c->opt_user_name = acct_name;
6011 /* find the domain controller */
6012 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6013 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6017 /* connect to ipc$ as username/password */
6018 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6019 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6021 /* Is it trusting domain account for sure ? */
6022 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6023 nt_errstr(nt_status)));
6027 /* store who we connected to */
6029 saf_store( domain_name, pdc_name );
6032 * Connect to \\server\ipc$ again (this time anonymously)
6035 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6038 if (NT_STATUS_IS_ERR(nt_status)) {
6039 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6040 domain_name, nt_errstr(nt_status)));
6044 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6045 "domain %s", domain_name))) {
6046 DEBUG(0, ("talloc_init() failed\n"));
6051 /* Make sure we're talking to a proper server */
6053 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6054 if (!NT_STATUS_IS_OK(nt_status)) {
6056 talloc_destroy(mem_ctx);
6061 * Call LsaOpenPolicy and LsaQueryInfo
6064 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6066 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6068 talloc_destroy(mem_ctx);
6072 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, SEC_RIGHTS_QUERY_VALUE,
6074 if (NT_STATUS_IS_ERR(nt_status)) {
6075 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6076 nt_errstr(nt_status)));
6078 talloc_destroy(mem_ctx);
6082 /* Querying info level 5 */
6084 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6086 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6088 if (NT_STATUS_IS_ERR(nt_status)) {
6089 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6090 nt_errstr(nt_status)));
6092 talloc_destroy(mem_ctx);
6096 domain_sid = info->account_domain.sid;
6098 /* There should be actually query info level 3 (following nt serv behaviour),
6099 but I still don't know if it's _really_ necessary */
6102 * Store the password in secrets db
6105 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6106 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6108 talloc_destroy(mem_ctx);
6113 * Close the pipes and clean up
6116 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6117 if (NT_STATUS_IS_ERR(nt_status)) {
6118 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6119 nt_errstr(nt_status)));
6121 talloc_destroy(mem_ctx);
6127 talloc_destroy(mem_ctx);
6129 d_printf("Trust to domain %s established\n", domain_name);
6134 * Revoke trust relationship to the remote domain
6136 * @param c A net_context structure
6137 * @param argc standard argc
6138 * @param argv standard argv without initial components
6140 * @return Integer status (0 means success)
6143 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6149 if (argc < 1 || c->display_usage) {
6151 "net rpc trustdom revoke <domain_name>\n"
6152 " Revoke trust relationship\n"
6153 " domain_name\tName of domain to revoke trust\n");
6157 /* generate upper cased domain name */
6158 domain_name = smb_xstrdup(argv[0]);
6159 strupper_m(domain_name);
6161 /* delete password of the trust */
6162 if (!pdb_del_trusteddom_pw(domain_name)) {
6163 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6170 SAFE_FREE(domain_name);
6174 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6175 const DOM_SID *domain_sid,
6176 const char *domain_name,
6177 struct cli_state *cli,
6178 struct rpc_pipe_client *pipe_hnd,
6179 TALLOC_CTX *mem_ctx,
6184 sid_to_fstring(str_sid, domain_sid);
6185 d_printf("%s\n", str_sid);
6186 return NT_STATUS_OK;
6189 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
6191 fstring ascii_sid, padding;
6192 int pad_len, col_len = 20;
6194 /* convert sid into ascii string */
6195 sid_to_fstring(ascii_sid, dom_sid);
6197 /* calculate padding space for d_printf to look nicer */
6198 pad_len = col_len - strlen(trusted_dom_name);
6199 padding[pad_len] = 0;
6200 do padding[--pad_len] = ' '; while (pad_len);
6202 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
6205 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6206 TALLOC_CTX *mem_ctx,
6209 const char *trusted_dom_name)
6212 union lsa_TrustedDomainInfo *info = NULL;
6213 char *cleartextpwd = NULL;
6214 uint8_t nt_hash[16];
6217 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
6220 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6222 if (NT_STATUS_IS_ERR(nt_status)) {
6223 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6224 nt_errstr(nt_status)));
6228 data = data_blob(info->password.password->data,
6229 info->password.password->length);
6231 if (!rpccli_get_pwd_hash(pipe_hnd, nt_hash)) {
6232 DEBUG(0, ("Could not retrieve password hash\n"));
6236 cleartextpwd = decrypt_trustdom_secret(nt_hash, &data);
6238 if (cleartextpwd == NULL) {
6239 DEBUG(0,("retrieved NULL password\n"));
6240 nt_status = NT_STATUS_UNSUCCESSFUL;
6244 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6245 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6246 nt_status = NT_STATUS_UNSUCCESSFUL;
6250 #ifdef DEBUG_PASSWORD
6251 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6252 "password: [%s]\n", trusted_dom_name,
6253 sid_string_dbg(&dom_sid), cleartextpwd));
6257 SAFE_FREE(cleartextpwd);
6258 data_blob_free(&data);
6263 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6266 /* common variables */
6267 TALLOC_CTX* mem_ctx;
6268 struct cli_state *cli = NULL;
6269 struct rpc_pipe_client *pipe_hnd = NULL;
6271 const char *domain_name = NULL;
6272 DOM_SID *queried_dom_sid;
6273 POLICY_HND connect_hnd;
6274 union lsa_PolicyInformation *info = NULL;
6276 /* trusted domains listing variables */
6277 unsigned int enum_ctx = 0;
6279 struct lsa_DomainList dom_list;
6282 if (c->display_usage) {
6284 "net rpc trustdom vampire\n"
6285 " Vampire trust relationship from remote server\n");
6290 * Listing trusted domains (stored in secrets.tdb, if local)
6293 mem_ctx = talloc_init("trust relationships vampire");
6296 * set domain and pdc name to local samba server (default)
6297 * or to remote one given in command line
6300 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6301 domain_name = c->opt_workgroup;
6302 c->opt_target_workgroup = c->opt_workgroup;
6304 fstrcpy(pdc_name, global_myname());
6305 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6306 c->opt_target_workgroup = domain_name;
6309 /* open \PIPE\lsarpc and open policy handle */
6310 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6311 if (!NT_STATUS_IS_OK(nt_status)) {
6312 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6313 nt_errstr(nt_status)));
6314 talloc_destroy(mem_ctx);
6318 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6320 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6321 nt_errstr(nt_status) ));
6323 talloc_destroy(mem_ctx);
6327 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6329 if (NT_STATUS_IS_ERR(nt_status)) {
6330 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6331 nt_errstr(nt_status)));
6333 talloc_destroy(mem_ctx);
6337 /* query info level 5 to obtain sid of a domain being queried */
6338 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6340 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6343 if (NT_STATUS_IS_ERR(nt_status)) {
6344 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6345 nt_errstr(nt_status)));
6347 talloc_destroy(mem_ctx);
6351 queried_dom_sid = info->account_domain.sid;
6354 * Keep calling LsaEnumTrustdom over opened pipe until
6355 * the end of enumeration is reached
6358 d_printf("Vampire trusted domains:\n\n");
6361 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6366 if (NT_STATUS_IS_ERR(nt_status)) {
6367 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6368 nt_errstr(nt_status)));
6370 talloc_destroy(mem_ctx);
6374 for (i = 0; i < dom_list.count; i++) {
6376 print_trusted_domain(dom_list.domains[i].sid,
6377 dom_list.domains[i].name.string);
6379 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6380 *dom_list.domains[i].sid,
6381 dom_list.domains[i].name.string);
6382 if (!NT_STATUS_IS_OK(nt_status)) {
6384 talloc_destroy(mem_ctx);
6390 * in case of no trusted domains say something rather
6391 * than just display blank line
6393 if (!dom_list.count) d_printf("none\n");
6395 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6397 /* close this connection before doing next one */
6398 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6399 if (NT_STATUS_IS_ERR(nt_status)) {
6400 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6401 nt_errstr(nt_status)));
6403 talloc_destroy(mem_ctx);
6407 /* close lsarpc pipe and connection to IPC$ */
6410 talloc_destroy(mem_ctx);
6414 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6416 /* common variables */
6417 TALLOC_CTX* mem_ctx;
6418 struct cli_state *cli = NULL, *remote_cli = NULL;
6419 struct rpc_pipe_client *pipe_hnd = NULL;
6421 const char *domain_name = NULL;
6422 DOM_SID *queried_dom_sid;
6424 int ascii_dom_name_len;
6425 POLICY_HND connect_hnd;
6426 union lsa_PolicyInformation *info = NULL;
6428 /* trusted domains listing variables */
6429 unsigned int num_domains, enum_ctx = 0;
6430 int i, pad_len, col_len = 20;
6431 struct lsa_DomainList dom_list;
6434 /* trusting domains listing variables */
6435 POLICY_HND domain_hnd;
6436 struct samr_SamArray *trusts = NULL;
6438 if (c->display_usage) {
6440 "net rpc trustdom list\n"
6441 " List trust relationships\n");
6446 * Listing trusted domains (stored in secrets.tdb, if local)
6449 mem_ctx = talloc_init("trust relationships listing");
6452 * set domain and pdc name to local samba server (default)
6453 * or to remote one given in command line
6456 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6457 domain_name = c->opt_workgroup;
6458 c->opt_target_workgroup = c->opt_workgroup;
6460 fstrcpy(pdc_name, global_myname());
6461 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6462 c->opt_target_workgroup = domain_name;
6465 /* open \PIPE\lsarpc and open policy handle */
6466 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6467 if (!NT_STATUS_IS_OK(nt_status)) {
6468 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6469 nt_errstr(nt_status)));
6470 talloc_destroy(mem_ctx);
6474 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6476 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6477 nt_errstr(nt_status) ));
6479 talloc_destroy(mem_ctx);
6483 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6485 if (NT_STATUS_IS_ERR(nt_status)) {
6486 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6487 nt_errstr(nt_status)));
6489 talloc_destroy(mem_ctx);
6493 /* query info level 5 to obtain sid of a domain being queried */
6494 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6496 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6499 if (NT_STATUS_IS_ERR(nt_status)) {
6500 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6501 nt_errstr(nt_status)));
6503 talloc_destroy(mem_ctx);
6507 queried_dom_sid = info->account_domain.sid;
6510 * Keep calling LsaEnumTrustdom over opened pipe until
6511 * the end of enumeration is reached
6514 d_printf("Trusted domains list:\n\n");
6517 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6522 if (NT_STATUS_IS_ERR(nt_status)) {
6523 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6524 nt_errstr(nt_status)));
6526 talloc_destroy(mem_ctx);
6530 for (i = 0; i < dom_list.count; i++) {
6531 print_trusted_domain(dom_list.domains[i].sid,
6532 dom_list.domains[i].name.string);
6536 * in case of no trusted domains say something rather
6537 * than just display blank line
6539 if (!dom_list.count) d_printf("none\n");
6541 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6543 /* close this connection before doing next one */
6544 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6545 if (NT_STATUS_IS_ERR(nt_status)) {
6546 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6547 nt_errstr(nt_status)));
6549 talloc_destroy(mem_ctx);
6553 TALLOC_FREE(pipe_hnd);
6556 * Listing trusting domains (stored in passdb backend, if local)
6559 d_printf("\nTrusting domains list:\n\n");
6562 * Open \PIPE\samr and get needed policy handles
6564 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6566 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6568 talloc_destroy(mem_ctx);
6573 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6575 SA_RIGHT_SAM_OPEN_DOMAIN,
6577 if (!NT_STATUS_IS_OK(nt_status)) {
6578 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6579 nt_errstr(nt_status)));
6581 talloc_destroy(mem_ctx);
6585 /* SamrOpenDomain - we have to open domain policy handle in order to be
6586 able to enumerate accounts*/
6587 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6589 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6592 if (!NT_STATUS_IS_OK(nt_status)) {
6593 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6594 nt_errstr(nt_status)));
6596 talloc_destroy(mem_ctx);
6601 * perform actual enumeration
6604 enum_ctx = 0; /* reset enumeration context from last enumeration */
6607 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6614 if (NT_STATUS_IS_ERR(nt_status)) {
6615 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6616 nt_errstr(nt_status)));
6618 talloc_destroy(mem_ctx);
6622 for (i = 0; i < num_domains; i++) {
6624 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6627 * get each single domain's sid (do we _really_ need this ?):
6628 * 1) connect to domain's pdc
6629 * 2) query the pdc for domain's sid
6632 /* get rid of '$' tail */
6633 ascii_dom_name_len = strlen(str);
6634 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6635 str[ascii_dom_name_len - 1] = '\0';
6637 /* calculate padding space for d_printf to look nicer */
6638 pad_len = col_len - strlen(str);
6639 padding[pad_len] = 0;
6640 do padding[--pad_len] = ' '; while (pad_len);
6642 /* set opt_* variables to remote domain */
6644 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6645 c->opt_target_workgroup = c->opt_workgroup;
6647 d_printf("%s%s", str, padding);
6649 /* connect to remote domain controller */
6650 nt_status = net_make_ipc_connection(c,
6651 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6653 if (NT_STATUS_IS_OK(nt_status)) {
6654 /* query for domain's sid */
6655 if (run_rpc_command(c, remote_cli, PI_LSARPC, 0,
6656 rpc_query_domain_sid, argc,
6658 d_fprintf(stderr, "couldn't get domain's sid\n");
6660 cli_shutdown(remote_cli);
6663 d_fprintf(stderr, "domain controller is not "
6665 nt_errstr(nt_status));
6669 if (!num_domains) d_printf("none\n");
6671 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6673 /* close opened samr and domain policy handles */
6674 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6675 if (!NT_STATUS_IS_OK(nt_status)) {
6676 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6679 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6680 if (!NT_STATUS_IS_OK(nt_status)) {
6681 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6684 /* close samr pipe and connection to IPC$ */
6687 talloc_destroy(mem_ctx);
6692 * Entrypoint for 'net rpc trustdom' code
6694 * @param argc standard argc
6695 * @param argv standard argv without initial components
6697 * @return Integer status (0 means success)
6700 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6702 struct functable func[] = {
6707 "Add trusted domain's account",
6708 "net rpc trustdom add\n"
6709 " Add trusted domain's account"
6715 "Remove trusted domain's account",
6716 "net rpc trustdom del\n"
6717 " Remove trusted domain's account"
6721 rpc_trustdom_establish,
6723 "Establish trust relationship",
6724 "net rpc trustdom establish\n"
6725 " Establish trust relationship"
6729 rpc_trustdom_revoke,
6731 "Revoke trust relationship",
6732 "net rpc trustdom revoke\n"
6733 " Revoke trust relationship"
6739 "List domain trusts",
6740 "net rpc trustdom list\n"
6741 " List domain trusts"
6745 rpc_trustdom_vampire,
6747 "Vampire trusts from remote server",
6748 "net rpc trustdom vampire\n"
6749 " Vampire trusts from remote server"
6751 {NULL, NULL, 0, NULL, NULL}
6754 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6758 * Check if a server will take rpc commands
6759 * @param flags Type of server to connect to (PDC, DMB, localhost)
6760 * if the host is not explicitly specified
6761 * @return bool (true means rpc supported)
6763 bool net_rpc_check(struct net_context *c, unsigned flags)
6765 struct cli_state *cli;
6767 struct sockaddr_storage server_ss;
6768 char *server_name = NULL;
6771 /* flags (i.e. server type) may depend on command */
6772 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6775 if ((cli = cli_initialise()) == NULL) {
6779 status = cli_connect(cli, server_name, &server_ss);
6780 if (!NT_STATUS_IS_OK(status))
6782 if (!attempt_netbios_session_request(&cli, global_myname(),
6783 server_name, &server_ss))
6785 if (!cli_negprot(cli))
6787 if (cli->protocol < PROTOCOL_NT1)
6796 /* dump sam database via samsync rpc calls */
6797 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6798 if (c->display_usage) {
6801 " Dump remote SAM database\n");
6805 return run_rpc_command(c, NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS,
6806 rpc_samdump_internals, argc, argv);
6809 /* syncronise sam database via samsync rpc calls */
6810 static int rpc_vampire(struct net_context *c, int argc, const char **argv) {
6811 if (c->display_usage) {
6814 " Vampire remote SAM database\n");
6818 return run_rpc_command(c, NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS,
6819 rpc_vampire_internals, argc, argv);
6823 * Migrate everything from a print-server
6825 * @param c A net_context structure
6826 * @param argc Standard main() style argc
6827 * @param argv Standard main() style argv. Initial components are already
6830 * @return A shell status integer (0 for success)
6832 * The order is important !
6833 * To successfully add drivers the print-queues have to exist !
6834 * Applying ACLs should be the last step, because you're easily locked out
6837 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6842 if (c->display_usage) {
6844 "net rpc printer migrate all\n"
6845 " Migrate everything from a print server\n");
6850 d_printf("no server to migrate\n");
6854 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6855 rpc_printer_migrate_printers_internals, argc,
6860 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6861 rpc_printer_migrate_drivers_internals, argc,
6866 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6867 rpc_printer_migrate_forms_internals, argc, argv);
6871 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6872 rpc_printer_migrate_settings_internals, argc,
6877 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6878 rpc_printer_migrate_security_internals, argc,
6884 * Migrate print-drivers from a print-server
6886 * @param c A net_context structure
6887 * @param argc Standard main() style argc
6888 * @param argv Standard main() style argv. Initial components are already
6891 * @return A shell status integer (0 for success)
6893 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6896 if (c->display_usage) {
6898 "net rpc printer migrate drivers\n"
6899 " Migrate print-drivers from a print-server\n");
6904 d_printf("no server to migrate\n");
6908 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6909 rpc_printer_migrate_drivers_internals,
6914 * Migrate print-forms from a print-server
6916 * @param c A net_context structure
6917 * @param argc Standard main() style argc
6918 * @param argv Standard main() style argv. Initial components are already
6921 * @return A shell status integer (0 for success)
6923 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6926 if (c->display_usage) {
6928 "net rpc printer migrate forms\n"
6929 " Migrate print-forms from a print-server\n");
6934 d_printf("no server to migrate\n");
6938 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6939 rpc_printer_migrate_forms_internals,
6944 * Migrate printers from a print-server
6946 * @param c A net_context structure
6947 * @param argc Standard main() style argc
6948 * @param argv Standard main() style argv. Initial components are already
6951 * @return A shell status integer (0 for success)
6953 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6956 if (c->display_usage) {
6958 "net rpc printer migrate printers\n"
6959 " Migrate printers from a print-server\n");
6964 d_printf("no server to migrate\n");
6968 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6969 rpc_printer_migrate_printers_internals,
6974 * Migrate printer-ACLs from a print-server
6976 * @param c A net_context structure
6977 * @param argc Standard main() style argc
6978 * @param argv Standard main() style argv. Initial components are already
6981 * @return A shell status integer (0 for success)
6983 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6986 if (c->display_usage) {
6988 "net rpc printer migrate security\n"
6989 " Migrate printer-ACLs from a print-server\n");
6994 d_printf("no server to migrate\n");
6998 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6999 rpc_printer_migrate_security_internals,
7004 * Migrate printer-settings from a print-server
7006 * @param c A net_context structure
7007 * @param argc Standard main() style argc
7008 * @param argv Standard main() style argv. Initial components are already
7011 * @return A shell status integer (0 for success)
7013 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7016 if (c->display_usage) {
7018 "net rpc printer migrate settings\n"
7019 " Migrate printer-settings from a print-server\n");
7024 d_printf("no server to migrate\n");
7028 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7029 rpc_printer_migrate_settings_internals,
7034 * 'net rpc printer' entrypoint.
7036 * @param c A net_context structure
7037 * @param argc Standard main() style argc
7038 * @param argv Standard main() style argv. Initial components are already
7042 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7045 /* ouch: when addriver and setdriver are called from within
7046 rpc_printer_migrate_drivers_internals, the printer-queue already
7049 struct functable func[] = {
7052 rpc_printer_migrate_all,
7054 "Migrate all from remote to local print server",
7055 "net rpc printer migrate all\n"
7056 " Migrate all from remote to local print server"
7060 rpc_printer_migrate_drivers,
7062 "Migrate drivers to local server",
7063 "net rpc printer migrate drivers\n"
7064 " Migrate drivers to local server"
7068 rpc_printer_migrate_forms,
7070 "Migrate froms to local server",
7071 "net rpc printer migrate forms\n"
7072 " Migrate froms to local server"
7076 rpc_printer_migrate_printers,
7078 "Migrate printers to local server",
7079 "net rpc printer migrate printers\n"
7080 " Migrate printers to local server"
7084 rpc_printer_migrate_security,
7086 "Mirgate printer ACLs to local server",
7087 "net rpc printer migrate security\n"
7088 " Mirgate printer ACLs to local server"
7092 rpc_printer_migrate_settings,
7094 "Migrate printer settings to local server",
7095 "net rpc printer migrate settings\n"
7096 " Migrate printer settings to local server"
7098 {NULL, NULL, 0, NULL, NULL}
7101 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7106 * List printers on a remote RPC server
7108 * @param c A net_context structure
7109 * @param argc Standard main() style argc
7110 * @param argv Standard main() style argv. Initial components are already
7113 * @return A shell status integer (0 for success)
7115 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7117 if (c->display_usage) {
7119 "net rpc printer list\n"
7120 " List printers on a remote RPC server\n");
7124 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7125 rpc_printer_list_internals,
7130 * List printer-drivers on a remote RPC server
7132 * @param c A net_context structure
7133 * @param argc Standard main() style argc
7134 * @param argv Standard main() style argv. Initial components are already
7137 * @return A shell status integer (0 for success)
7139 static int rpc_printer_driver_list(struct net_context *c, int argc,
7142 if (c->display_usage) {
7144 "net rpc printer driver\n"
7145 " List printer-drivers on a remote RPC server\n");
7149 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7150 rpc_printer_driver_list_internals,
7155 * Publish printer in ADS via MSRPC
7157 * @param c A net_context structure
7158 * @param argc Standard main() style argc
7159 * @param argv Standard main() style argv. Initial components are already
7162 * @return A shell status integer (0 for success)
7164 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7167 if (c->display_usage) {
7169 "net rpc printer publish publish\n"
7170 " Publish printer in ADS via MSRPC\n");
7174 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7175 rpc_printer_publish_publish_internals,
7180 * Update printer in ADS via MSRPC
7182 * @param c A net_context structure
7183 * @param argc Standard main() style argc
7184 * @param argv Standard main() style argv. Initial components are already
7187 * @return A shell status integer (0 for success)
7189 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7191 if (c->display_usage) {
7193 "net rpc printer publish update\n"
7194 " Update printer in ADS via MSRPC\n");
7198 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7199 rpc_printer_publish_update_internals,
7204 * UnPublish printer in ADS via MSRPC
7206 * @param c A net_context structure
7207 * @param argc Standard main() style argc
7208 * @param argv Standard main() style argv. Initial components are already
7211 * @return A shell status integer (0 for success)
7213 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7216 if (c->display_usage) {
7218 "net rpc printer publish unpublish\n"
7219 " UnPublish printer in ADS via MSRPC\n");
7223 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7224 rpc_printer_publish_unpublish_internals,
7229 * List published printers via MSRPC
7231 * @param c A net_context structure
7232 * @param argc Standard main() style argc
7233 * @param argv Standard main() style argv. Initial components are already
7236 * @return A shell status integer (0 for success)
7238 static int rpc_printer_publish_list(struct net_context *c, int argc,
7241 if (c->display_usage) {
7243 "net rpc printer publish list\n"
7244 " List published printers via MSRPC\n");
7248 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7249 rpc_printer_publish_list_internals,
7255 * Publish printer in ADS
7257 * @param c A net_context structure
7258 * @param argc Standard main() style argc
7259 * @param argv Standard main() style argv. Initial components are already
7262 * @return A shell status integer (0 for success)
7264 static int rpc_printer_publish(struct net_context *c, int argc,
7268 struct functable func[] = {
7271 rpc_printer_publish_publish,
7273 "Publish printer in AD",
7274 "net rpc printer publish publish\n"
7275 " Publish printer in AD"
7279 rpc_printer_publish_update,
7281 "Update printer in AD",
7282 "net rpc printer publish update\n"
7283 " Update printer in AD"
7287 rpc_printer_publish_unpublish,
7289 "Unpublish printer",
7290 "net rpc printer publish unpublish\n"
7291 " Unpublish printer"
7295 rpc_printer_publish_list,
7297 "List published printers",
7298 "net rpc printer publish list\n"
7299 " List published printers"
7301 {NULL, NULL, 0, NULL, NULL}
7305 if (c->display_usage) {
7306 d_printf("Usage:\n");
7307 d_printf("net rpc printer publish\n"
7308 " List published printers\n"
7309 " Alias of net rpc printer publish list\n");
7310 net_display_usage_from_functable(func);
7313 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7314 rpc_printer_publish_list_internals,
7318 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7324 * Display rpc printer help page.
7326 * @param c A net_context structure
7327 * @param argc Standard main() style argc
7328 * @param argv Standard main() style argv. Initial components are already
7331 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7333 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"
7334 "\tlists all printers on print-server\n\n");
7335 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7336 "\tlists all printer-drivers on print-server\n\n");
7337 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7338 "\tpublishes printer settings in Active Directory\n"
7339 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
7340 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7341 "\n\tmigrates printers from remote to local server\n\n");
7342 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7343 "\n\tmigrates printer-settings from remote to local server\n\n");
7344 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7345 "\n\tmigrates printer-drivers from remote to local server\n\n");
7346 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7347 "\n\tmigrates printer-forms from remote to local server\n\n");
7348 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7349 "\n\tmigrates printer-ACLs from remote to local server\n\n");
7350 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7351 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7352 "\tremote to local print-server\n\n");
7353 net_common_methods_usage(c, argc, argv);
7354 net_common_flags_usage(c, argc, argv);
7356 "\t-v or --verbose\t\t\tgive verbose output\n"
7357 "\t --destination\t\tmigration target server (default: localhost)\n");
7363 * 'net rpc printer' entrypoint.
7365 * @param c A net_context structure
7366 * @param argc Standard main() style argc
7367 * @param argv Standard main() style argv. Initial components are already
7370 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7372 struct functable func[] = {
7377 "List all printers on print server",
7378 "net rpc printer list\n"
7379 " List all printers on print server"
7383 rpc_printer_migrate,
7385 "Migrate printer to local server",
7386 "net rpc printer migrate\n"
7387 " Migrate printer to local server"
7391 rpc_printer_driver_list,
7393 "List printer drivers",
7394 "net rpc printer driver\n"
7395 " List printer drivers"
7399 rpc_printer_publish,
7401 "Publish printer in AD",
7402 "net rpc printer publish\n"
7403 " Publish printer in AD"
7405 {NULL, NULL, 0, NULL, NULL}
7409 if (c->display_usage) {
7410 d_printf("Usage:\n");
7411 d_printf("net rpc printer\n"
7412 " List printers\n");
7413 net_display_usage_from_functable(func);
7416 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
7417 rpc_printer_list_internals,
7421 return net_run_function(c, argc, argv, "net rpc printer", func);
7425 * 'net rpc' entrypoint.
7427 * @param c A net_context structure
7428 * @param argc Standard main() style argc
7429 * @param argv Standard main() style argv. Initial components are already
7433 int net_rpc(struct net_context *c, int argc, const char **argv)
7435 struct functable func[] = {
7440 "Modify global audit settings",
7442 " Modify global audit settings"
7448 "Show basic info about a domain",
7450 " Show basic info about a domain"
7464 "Join a domain created in server manager",
7466 " Join a domain created in server manager"
7472 "Test that a join is valid",
7473 "net rpc testjoin\n"
7474 " Test that a join is valid"
7480 "List/modify users",
7482 " List/modify users"
7488 "Change a user password",
7489 "net rpc password\n"
7490 " Change a user password\n"
7491 " Alias for net rpc user password"
7497 "List/modify groups",
7499 " List/modify groups"
7505 "List/modify shares",
7507 " List/modify shares"
7521 "List/modify printers",
7523 " List/modify printers"
7527 net_rpc_changetrustpw,
7529 "Change trust account password",
7530 "net rpc changetrustpw\n"
7531 " Change trust account password"
7537 "Modify domain trusts",
7538 "net rpc trustdom\n"
7539 " Modify domain trusts"
7545 "Abort a remote shutdown",
7546 "net rpc abortshutdown\n"
7547 " Abort a remote shutdown"
7553 "Shutdown a remote server",
7554 "net rpc shutdown\n"
7555 " Shutdown a remote server"
7561 "Dump SAM data of remote NT PDC",
7563 " Dump SAM data of remote NT PDC"
7569 "Sync a remote NT PDC's data into local passdb",
7571 " Sync a remote NT PDC's data into local passdb"
7577 "Fetch the domain sid into local secrets.tdb",
7579 " Fetch the domain sid into local secrets.tdb"
7585 "Manage privileges assigned to SID",
7587 " Manage privileges assigned to SID"
7593 "Start/stop/query remote services",
7595 " Start/stop/query remote services"
7601 "Manage registry hives",
7602 "net rpc registry\n"
7603 " Manage registry hives"
7609 "Open interactive shell on remote server",
7611 " Open interactive shell on remote server"
7613 {NULL, NULL, 0, NULL, NULL}
7615 return net_run_function(c, argc, argv, "net rpc", func);