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 return run_rpc_command(c, NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
244 rpc_changetrustpw_internals,
249 * Join a domain, the old way.
251 * This uses 'machinename' as the inital password, and changes it.
253 * The password should be created with 'server manager' or equiv first.
255 * All parameters are provided by the run_rpc_command function, except for
256 * argc, argv which are passes through.
258 * @param domain_sid The domain sid aquired from the remote server
259 * @param cli A cli_state connected to the server.
260 * @param mem_ctx Talloc context, destoyed on compleation of the function.
261 * @param argc Standard main() style argc
262 * @param argc Standard main() style argv. Initial components are already
265 * @return Normal NTSTATUS return.
268 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
269 const DOM_SID *domain_sid,
270 const char *domain_name,
271 struct cli_state *cli,
272 struct rpc_pipe_client *pipe_hnd,
278 fstring trust_passwd;
279 unsigned char orig_trust_passwd_hash[16];
281 uint32 sec_channel_type;
283 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &result);
285 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
288 nt_errstr(result) ));
293 check what type of join - if the user want's to join as
294 a BDC, the server must agree that we are a BDC.
297 sec_channel_type = get_sec_channel_type(argv[0]);
299 sec_channel_type = get_sec_channel_type(NULL);
302 fstrcpy(trust_passwd, global_myname());
303 strlower_m(trust_passwd);
306 * Machine names can be 15 characters, but the max length on
307 * a password is 14. --jerry
310 trust_passwd[14] = '\0';
312 E_md4hash(trust_passwd, orig_trust_passwd_hash);
314 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
315 orig_trust_passwd_hash,
318 if (NT_STATUS_IS_OK(result))
319 printf("Joined domain %s.\n", c->opt_target_workgroup);
322 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
323 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
324 result = NT_STATUS_UNSUCCESSFUL;
331 * Join a domain, the old way.
333 * @param argc Standard main() style argc
334 * @param argc Standard main() style argv. Initial components are already
337 * @return A shell status integer (0 for success)
340 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
342 return run_rpc_command(c, NULL, PI_NETLOGON,
343 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
344 rpc_oldjoin_internals,
349 * Join a domain, the old way. This function exists to allow
350 * the message to be displayed when oldjoin was explicitly
351 * requested, but not when it was implied by "net rpc join"
353 * @param argc Standard main() style argc
354 * @param argc Standard main() style argv. Initial components are already
357 * @return A shell status integer (0 for success)
360 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
362 int rc = net_rpc_perform_oldjoin(c, argc, argv);
365 d_fprintf(stderr, "Failed to join domain\n");
372 * Basic usage function for 'net rpc join'
373 * @param argc Standard main() style argc
374 * @param argc Standard main() style argv. Initial components are already
378 static int rpc_join_usage(struct net_context *c, int argc, const char **argv)
380 d_printf("net rpc join -U <username>[%%password] <type>[options]\n"\
381 "\t to join a domain with admin username & password\n"\
382 "\t\t password will be prompted if needed and none is specified\n"\
383 "\t <type> can be (default MEMBER)\n"\
384 "\t\t BDC - Join as a BDC\n"\
385 "\t\t PDC - Join as a PDC\n"\
386 "\t\t MEMBER - Join as a MEMBER server\n");
388 net_common_flags_usage(c, argc, argv);
393 * 'net rpc join' entrypoint.
394 * @param argc Standard main() style argc
395 * @param argc Standard main() style argv. Initial components are already
398 * Main 'net_rpc_join()' (where the admin username/password is used) is
400 * Try to just change the password, but if that doesn't work, use/prompt
401 * for a username/password.
404 int net_rpc_join(struct net_context *c, int argc, const char **argv)
406 if (lp_server_role() == ROLE_STANDALONE) {
407 d_printf("cannot join as standalone machine\n");
411 if (strlen(global_myname()) > 15) {
412 d_printf("Our netbios name can be at most 15 chars long, "
413 "\"%s\" is %u chars long\n",
414 global_myname(), (unsigned int)strlen(global_myname()));
418 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
421 return net_rpc_join_newstyle(c, argc, argv);
425 * display info about a rpc domain
427 * All parameters are provided by the run_rpc_command function, except for
428 * argc, argv which are passed through.
430 * @param domain_sid The domain sid acquired from the remote server
431 * @param cli A cli_state connected to the server.
432 * @param mem_ctx Talloc context, destoyed on completion of the function.
433 * @param argc Standard main() style argc
434 * @param argv Standard main() style argv. Initial components are already
437 * @return Normal NTSTATUS return.
440 NTSTATUS rpc_info_internals(struct net_context *c,
441 const DOM_SID *domain_sid,
442 const char *domain_name,
443 struct cli_state *cli,
444 struct rpc_pipe_client *pipe_hnd,
449 POLICY_HND connect_pol, domain_pol;
450 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
451 union samr_DomainInfo *info = NULL;
454 sid_to_fstring(sid_str, domain_sid);
456 /* Get sam policy handle */
457 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
459 MAXIMUM_ALLOWED_ACCESS,
461 if (!NT_STATUS_IS_OK(result)) {
462 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
466 /* Get domain policy handle */
467 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
469 MAXIMUM_ALLOWED_ACCESS,
470 CONST_DISCARD(struct dom_sid2 *, domain_sid),
472 if (!NT_STATUS_IS_OK(result)) {
473 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
477 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
481 if (NT_STATUS_IS_OK(result)) {
482 d_printf("Domain Name: %s\n", info->info2.domain_name.string);
483 d_printf("Domain SID: %s\n", sid_str);
484 d_printf("Sequence number: %llu\n",
485 (unsigned long long)info->info2.sequence_num);
486 d_printf("Num users: %u\n", info->info2.num_users);
487 d_printf("Num domain groups: %u\n", info->info2.num_groups);
488 d_printf("Num local groups: %u\n", info->info2.num_aliases);
496 * 'net rpc info' entrypoint.
497 * @param argc Standard main() style argc
498 * @param argc Standard main() style argv. Initial components are already
502 int net_rpc_info(struct net_context *c, int argc, const char **argv)
504 return run_rpc_command(c, NULL, PI_SAMR, NET_FLAGS_PDC,
510 * Fetch domain SID into the local secrets.tdb
512 * All parameters are provided by the run_rpc_command function, except for
513 * argc, argv which are passes through.
515 * @param domain_sid The domain sid acquired from the remote server
516 * @param cli A cli_state connected to the server.
517 * @param mem_ctx Talloc context, destoyed on completion of thea function.
518 * @param argc Standard main() style argc
519 * @param argv Standard main() style argv. Initial components are already
522 * @return Normal NTSTATUS return.
525 static NTSTATUS rpc_getsid_internals(struct net_context *c,
526 const DOM_SID *domain_sid,
527 const char *domain_name,
528 struct cli_state *cli,
529 struct rpc_pipe_client *pipe_hnd,
536 sid_to_fstring(sid_str, domain_sid);
537 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
538 sid_str, domain_name);
540 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
541 DEBUG(0,("Can't store domain SID\n"));
542 return NT_STATUS_UNSUCCESSFUL;
549 * 'net rpc getsid' entrypoint.
550 * @param argc Standard main() style argc
551 * @param argc Standard main() style argv. Initial components are already
555 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
557 return run_rpc_command(c, NULL, PI_SAMR,
558 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
559 rpc_getsid_internals,
563 /****************************************************************************/
566 * Basic usage function for 'net rpc user'
567 * @param argc Standard main() style argc.
568 * @param argv Standard main() style argv. Initial components are already
572 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
574 return net_user_usage(c, argc, argv);
578 * Add a new user to a remote RPC server
580 * @param argc Standard main() style argc
581 * @param argv Standard main() style argv. Initial components are already
584 * @return A shell status integer (0 for success)
587 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
589 NET_API_STATUS status;
590 struct USER_INFO_1 info1;
591 uint32_t parm_error = 0;
594 d_printf("User must be specified\n");
595 rpc_user_usage(c, argc, argv);
601 info1.usri1_name = argv[0];
603 info1.usri1_password = argv[1];
606 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
609 d_fprintf(stderr, "Failed to add user '%s' with: %s.\n",
610 argv[0], libnetapi_get_error_string(c->netapi_ctx,
614 d_printf("Added user '%s'.\n", argv[0]);
621 * Rename a user on a remote RPC server
623 * All parameters are provided by the run_rpc_command function, except for
624 * argc, argv which are passes through.
626 * @param domain_sid The domain sid acquired from the remote server
627 * @param cli A cli_state connected to the server.
628 * @param mem_ctx Talloc context, destoyed on completion of the function.
629 * @param argc Standard main() style argc
630 * @param argv Standard main() style argv. Initial components are already
633 * @return Normal NTSTATUS return.
636 static NTSTATUS rpc_user_rename_internals(struct net_context *c,
637 const DOM_SID *domain_sid,
638 const char *domain_name,
639 struct cli_state *cli,
640 struct rpc_pipe_client *pipe_hnd,
645 POLICY_HND connect_pol, domain_pol, user_pol;
646 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
647 uint32 info_level = 7;
648 const char *old_name, *new_name;
649 struct samr_Ids user_rids, name_types;
650 struct lsa_String lsa_acct_name;
651 union samr_UserInfo *info = NULL;
654 d_printf("Old and new username must be specified\n");
655 rpc_user_usage(c, argc, argv);
662 /* Get sam policy handle */
664 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
666 MAXIMUM_ALLOWED_ACCESS,
669 if (!NT_STATUS_IS_OK(result)) {
673 /* Get domain policy handle */
675 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
677 MAXIMUM_ALLOWED_ACCESS,
678 CONST_DISCARD(struct dom_sid2 *, domain_sid),
680 if (!NT_STATUS_IS_OK(result)) {
684 init_lsa_String(&lsa_acct_name, old_name);
686 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
692 if (!NT_STATUS_IS_OK(result)) {
696 /* Open domain user */
697 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
699 MAXIMUM_ALLOWED_ACCESS,
703 if (!NT_STATUS_IS_OK(result)) {
707 /* Query user info */
708 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
713 if (!NT_STATUS_IS_OK(result)) {
717 init_samr_user_info7(&info->info7, new_name);
720 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
725 if (!NT_STATUS_IS_OK(result)) {
730 if (!NT_STATUS_IS_OK(result)) {
731 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
734 d_printf("Renamed user from %s to %s\n", old_name, new_name);
740 * Rename a user on a remote RPC server
742 * @param argc Standard main() style argc
743 * @param argv Standard main() style argv. Initial components are already
746 * @return A shell status integer (0 for success)
749 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
751 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_user_rename_internals,
756 * Delete a user from a remote RPC server
758 * @param argc Standard main() style argc
759 * @param argv Standard main() style argv. Initial components are already
762 * @return A shell status integer (0 for success)
765 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
767 NET_API_STATUS status;
770 d_printf("User must be specified\n");
771 rpc_user_usage(c, argc, argv);
775 status = NetUserDel(c->opt_host, argv[0]);
778 d_fprintf(stderr, "Failed to delete user '%s' with: %s.\n",
780 libnetapi_get_error_string(c->netapi_ctx, status));
783 d_printf("Deleted user '%s'.\n", argv[0]);
790 * Set a password for a user on a remote RPC server
792 * All parameters are provided by the run_rpc_command function, except for
793 * argc, argv which are passes through.
795 * @param domain_sid The domain sid acquired from the remote server
796 * @param cli A cli_state connected to the server.
797 * @param mem_ctx Talloc context, destoyed on completion of the function.
798 * @param argc Standard main() style argc
799 * @param argv Standard main() style argv. Initial components are already
802 * @return Normal NTSTATUS return.
805 static NTSTATUS rpc_user_password_internals(struct net_context *c,
806 const DOM_SID *domain_sid,
807 const char *domain_name,
808 struct cli_state *cli,
809 struct rpc_pipe_client *pipe_hnd,
814 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
815 POLICY_HND connect_pol, domain_pol, user_pol;
818 const char *new_password;
820 union samr_UserInfo info;
823 d_printf("User must be specified\n");
824 rpc_user_usage(c, argc, argv);
831 new_password = argv[1];
833 asprintf(&prompt, "Enter new password for %s:", user);
834 new_password = getpass(prompt);
838 /* Get sam policy and domain handles */
840 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
842 MAXIMUM_ALLOWED_ACCESS,
845 if (!NT_STATUS_IS_OK(result)) {
849 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
851 MAXIMUM_ALLOWED_ACCESS,
852 CONST_DISCARD(struct dom_sid2 *, domain_sid),
855 if (!NT_STATUS_IS_OK(result)) {
859 /* Get handle on user */
862 struct samr_Ids user_rids, name_types;
863 struct lsa_String lsa_acct_name;
865 init_lsa_String(&lsa_acct_name, user);
867 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
873 if (!NT_STATUS_IS_OK(result)) {
877 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
879 MAXIMUM_ALLOWED_ACCESS,
883 if (!NT_STATUS_IS_OK(result)) {
888 /* Set password on account */
890 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
892 init_samr_user_info24(&info.info24, pwbuf, 24);
894 SamOEMhashBlob(info.info24.password.data, 516,
895 &cli->user_session_key);
897 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
902 if (!NT_STATUS_IS_OK(result)) {
906 /* Display results */
914 * Set a user's password on a remote RPC server
916 * @param argc Standard main() style argc
917 * @param argv Standard main() style argv. Initial components are already
920 * @return A shell status integer (0 for success)
923 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
925 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_user_password_internals,
930 * List user's groups on a remote RPC server
932 * All parameters are provided by the run_rpc_command function, except for
933 * argc, argv which are passes through.
935 * @param domain_sid The domain sid acquired from the remote server
936 * @param cli A cli_state connected to the server.
937 * @param mem_ctx Talloc context, destoyed on completion of the function.
938 * @param argc Standard main() style argc
939 * @param argv Standard main() style argv. Initial components are already
942 * @return Normal NTSTATUS return.
945 static NTSTATUS rpc_user_info_internals(struct net_context *c,
946 const DOM_SID *domain_sid,
947 const char *domain_name,
948 struct cli_state *cli,
949 struct rpc_pipe_client *pipe_hnd,
954 POLICY_HND connect_pol, domain_pol, user_pol;
955 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
957 struct samr_RidWithAttributeArray *rid_array = NULL;
958 struct lsa_Strings names;
959 struct samr_Ids types;
960 uint32_t *lrids = NULL;
961 struct samr_Ids rids, name_types;
962 struct lsa_String lsa_acct_name;
966 d_printf("User must be specified\n");
967 rpc_user_usage(c, argc, argv);
970 /* Get sam policy handle */
972 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
974 MAXIMUM_ALLOWED_ACCESS,
976 if (!NT_STATUS_IS_OK(result)) goto done;
978 /* Get domain policy handle */
980 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
982 MAXIMUM_ALLOWED_ACCESS,
983 CONST_DISCARD(struct dom_sid2 *, domain_sid),
985 if (!NT_STATUS_IS_OK(result)) goto done;
987 /* Get handle on user */
989 init_lsa_String(&lsa_acct_name, argv[0]);
991 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
998 if (!NT_STATUS_IS_OK(result)) goto done;
1000 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1002 MAXIMUM_ALLOWED_ACCESS,
1005 if (!NT_STATUS_IS_OK(result)) goto done;
1007 result = rpccli_samr_GetGroupsForUser(pipe_hnd, mem_ctx,
1011 if (!NT_STATUS_IS_OK(result)) goto done;
1015 if (rid_array->count) {
1016 if ((lrids = TALLOC_ARRAY(mem_ctx, uint32, rid_array->count)) == NULL) {
1017 result = NT_STATUS_NO_MEMORY;
1021 for (i = 0; i < rid_array->count; i++)
1022 lrids[i] = rid_array->rids[i].rid;
1024 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
1031 if (!NT_STATUS_IS_OK(result)) {
1035 /* Display results */
1037 for (i = 0; i < names.count; i++)
1038 printf("%s\n", names.names[i].string);
1045 * List a user's groups from a remote RPC server
1047 * @param argc Standard main() style argc
1048 * @param argv Standard main() style argv. Initial components are already
1051 * @return A shell status integer (0 for success)
1054 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
1056 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_user_info_internals,
1061 * List users on a remote RPC server
1063 * All parameters are provided by the run_rpc_command function, except for
1064 * argc, argv which are passes through.
1066 * @param domain_sid The domain sid acquired from the remote server
1067 * @param cli A cli_state connected to the server.
1068 * @param mem_ctx Talloc context, destoyed on completion of the function.
1069 * @param argc Standard main() style argc
1070 * @param argv Standard main() style argv. Initial components are already
1073 * @return Normal NTSTATUS return.
1076 static NTSTATUS rpc_user_list_internals(struct net_context *c,
1077 const DOM_SID *domain_sid,
1078 const char *domain_name,
1079 struct cli_state *cli,
1080 struct rpc_pipe_client *pipe_hnd,
1081 TALLOC_CTX *mem_ctx,
1085 POLICY_HND connect_pol, domain_pol;
1086 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1087 uint32 start_idx=0, num_entries, i, loop_count = 0;
1089 /* Get sam policy handle */
1091 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1093 MAXIMUM_ALLOWED_ACCESS,
1095 if (!NT_STATUS_IS_OK(result)) {
1099 /* Get domain policy handle */
1101 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1103 MAXIMUM_ALLOWED_ACCESS,
1104 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1106 if (!NT_STATUS_IS_OK(result)) {
1110 /* Query domain users */
1111 if (c->opt_long_list_entries)
1112 d_printf("\nUser name Comment"\
1113 "\n-----------------------------\n");
1115 const char *user = NULL;
1116 const char *desc = NULL;
1117 uint32 max_entries, max_size;
1118 uint32_t total_size, returned_size;
1119 union samr_DispInfo info;
1121 get_query_dispinfo_params(
1122 loop_count, &max_entries, &max_size);
1124 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
1134 start_idx += info.info1.count;
1135 num_entries = info.info1.count;
1137 for (i = 0; i < num_entries; i++) {
1138 user = info.info1.entries[i].account_name.string;
1139 if (c->opt_long_list_entries)
1140 desc = info.info1.entries[i].description.string;
1141 if (c->opt_long_list_entries)
1142 printf("%-21.21s %s\n", user, desc);
1144 printf("%s\n", user);
1146 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1153 * 'net rpc user' entrypoint.
1154 * @param argc Standard main() style argc
1155 * @param argc Standard main() style argv. Initial components are already
1159 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1161 NET_API_STATUS status;
1163 struct functable func[] = {
1164 {"add", rpc_user_add},
1165 {"info", rpc_user_info},
1166 {"delete", rpc_user_delete},
1167 {"password", rpc_user_password},
1168 {"rename", rpc_user_rename},
1172 status = libnetapi_init(&c->netapi_ctx);
1176 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1177 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1180 return run_rpc_command(c, NULL,PI_SAMR, 0,
1181 rpc_user_list_internals,
1185 return net_run_function(c, argc, argv, func, rpc_user_usage);
1188 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1189 TALLOC_CTX *mem_ctx,
1190 struct rpc_sh_ctx *ctx,
1191 struct rpc_pipe_client *pipe_hnd,
1192 int argc, const char **argv)
1194 return rpc_user_list_internals(c, ctx->domain_sid, ctx->domain_name,
1195 ctx->cli, pipe_hnd, mem_ctx,
1199 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1200 TALLOC_CTX *mem_ctx,
1201 struct rpc_sh_ctx *ctx,
1202 struct rpc_pipe_client *pipe_hnd,
1203 int argc, const char **argv)
1205 return rpc_user_info_internals(c, ctx->domain_sid, ctx->domain_name,
1206 ctx->cli, pipe_hnd, mem_ctx,
1210 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1211 TALLOC_CTX *mem_ctx,
1212 struct rpc_sh_ctx *ctx,
1213 struct rpc_pipe_client *pipe_hnd,
1214 int argc, const char **argv,
1216 struct net_context *c,
1217 TALLOC_CTX *mem_ctx,
1218 struct rpc_sh_ctx *ctx,
1219 struct rpc_pipe_client *pipe_hnd,
1220 POLICY_HND *user_hnd,
1221 int argc, const char **argv))
1223 POLICY_HND connect_pol, domain_pol, user_pol;
1224 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1227 enum lsa_SidType type;
1230 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1231 return NT_STATUS_INVALID_PARAMETER;
1234 ZERO_STRUCT(connect_pol);
1235 ZERO_STRUCT(domain_pol);
1236 ZERO_STRUCT(user_pol);
1238 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1239 argv[0], NULL, NULL, &sid, &type);
1240 if (!NT_STATUS_IS_OK(result)) {
1241 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1246 if (type != SID_NAME_USER) {
1247 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1248 sid_type_lookup(type));
1249 result = NT_STATUS_NO_SUCH_USER;
1253 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1254 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1255 result = NT_STATUS_NO_SUCH_USER;
1259 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1261 MAXIMUM_ALLOWED_ACCESS,
1263 if (!NT_STATUS_IS_OK(result)) {
1267 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1269 MAXIMUM_ALLOWED_ACCESS,
1272 if (!NT_STATUS_IS_OK(result)) {
1276 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1278 MAXIMUM_ALLOWED_ACCESS,
1281 if (!NT_STATUS_IS_OK(result)) {
1285 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1288 if (is_valid_policy_hnd(&user_pol)) {
1289 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1291 if (is_valid_policy_hnd(&domain_pol)) {
1292 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1294 if (is_valid_policy_hnd(&connect_pol)) {
1295 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1300 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1301 TALLOC_CTX *mem_ctx,
1302 struct rpc_sh_ctx *ctx,
1303 struct rpc_pipe_client *pipe_hnd,
1304 POLICY_HND *user_hnd,
1305 int argc, const char **argv)
1308 union samr_UserInfo *info = NULL;
1311 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1312 return NT_STATUS_INVALID_PARAMETER;
1315 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1319 if (!NT_STATUS_IS_OK(result)) {
1323 d_printf("user rid: %d, group rid: %d\n",
1325 info->info21.primary_gid);
1330 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1331 TALLOC_CTX *mem_ctx,
1332 struct rpc_sh_ctx *ctx,
1333 struct rpc_pipe_client *pipe_hnd,
1334 int argc, const char **argv)
1336 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1337 rpc_sh_user_show_internals);
1340 #define FETCHSTR(name, rec) \
1341 do { if (strequal(ctx->thiscmd, name)) { \
1342 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1345 #define SETSTR(name, rec, flag) \
1346 do { if (strequal(ctx->thiscmd, name)) { \
1347 init_lsa_String(&(info->info21.rec), argv[0]); \
1348 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1351 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1352 TALLOC_CTX *mem_ctx,
1353 struct rpc_sh_ctx *ctx,
1354 struct rpc_pipe_client *pipe_hnd,
1355 POLICY_HND *user_hnd,
1356 int argc, const char **argv)
1359 const char *username;
1360 const char *oldval = "";
1361 union samr_UserInfo *info = NULL;
1364 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1366 return NT_STATUS_INVALID_PARAMETER;
1369 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1373 if (!NT_STATUS_IS_OK(result)) {
1377 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1379 FETCHSTR("fullname", full_name);
1380 FETCHSTR("homedir", home_directory);
1381 FETCHSTR("homedrive", home_drive);
1382 FETCHSTR("logonscript", logon_script);
1383 FETCHSTR("profilepath", profile_path);
1384 FETCHSTR("description", description);
1387 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1391 if (strcmp(argv[0], "NULL") == 0) {
1395 ZERO_STRUCT(info->info21);
1397 SETSTR("fullname", full_name, FULL_NAME);
1398 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1399 SETSTR("homedrive", home_drive, HOME_DRIVE);
1400 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1401 SETSTR("profilepath", profile_path, PROFILE_PATH);
1402 SETSTR("description", description, DESCRIPTION);
1404 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1409 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1410 ctx->thiscmd, oldval, argv[0]);
1417 #define HANDLEFLG(name, rec) \
1418 do { if (strequal(ctx->thiscmd, name)) { \
1419 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1421 newflags = oldflags | ACB_##rec; \
1423 newflags = oldflags & ~ACB_##rec; \
1426 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1427 TALLOC_CTX *mem_ctx,
1428 struct rpc_sh_ctx *ctx,
1429 struct rpc_pipe_client *pipe_hnd,
1430 int argc, const char **argv)
1432 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1433 rpc_sh_user_str_edit_internals);
1436 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1437 TALLOC_CTX *mem_ctx,
1438 struct rpc_sh_ctx *ctx,
1439 struct rpc_pipe_client *pipe_hnd,
1440 POLICY_HND *user_hnd,
1441 int argc, const char **argv)
1444 const char *username;
1445 const char *oldval = "unknown";
1446 uint32 oldflags, newflags;
1448 union samr_UserInfo *info = NULL;
1451 ((argc == 1) && !strequal(argv[0], "yes") &&
1452 !strequal(argv[0], "no"))) {
1453 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1455 return NT_STATUS_INVALID_PARAMETER;
1458 newval = strequal(argv[0], "yes");
1460 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1464 if (!NT_STATUS_IS_OK(result)) {
1468 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1469 oldflags = info->info21.acct_flags;
1470 newflags = info->info21.acct_flags;
1472 HANDLEFLG("disabled", DISABLED);
1473 HANDLEFLG("pwnotreq", PWNOTREQ);
1474 HANDLEFLG("autolock", AUTOLOCK);
1475 HANDLEFLG("pwnoexp", PWNOEXP);
1478 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1482 ZERO_STRUCT(info->info21);
1484 info->info21.acct_flags = newflags;
1485 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1487 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1492 if (NT_STATUS_IS_OK(result)) {
1493 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1494 ctx->thiscmd, oldval, argv[0]);
1502 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1503 TALLOC_CTX *mem_ctx,
1504 struct rpc_sh_ctx *ctx,
1505 struct rpc_pipe_client *pipe_hnd,
1506 int argc, const char **argv)
1508 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1509 rpc_sh_user_flag_edit_internals);
1512 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1513 TALLOC_CTX *mem_ctx,
1514 struct rpc_sh_ctx *ctx)
1516 static struct rpc_sh_cmd cmds[] = {
1518 { "fullname", NULL, PI_SAMR, rpc_sh_user_str_edit,
1519 "Show/Set a user's full name" },
1521 { "homedir", NULL, PI_SAMR, rpc_sh_user_str_edit,
1522 "Show/Set a user's home directory" },
1524 { "homedrive", NULL, PI_SAMR, rpc_sh_user_str_edit,
1525 "Show/Set a user's home drive" },
1527 { "logonscript", NULL, PI_SAMR, rpc_sh_user_str_edit,
1528 "Show/Set a user's logon script" },
1530 { "profilepath", NULL, PI_SAMR, rpc_sh_user_str_edit,
1531 "Show/Set a user's profile path" },
1533 { "description", NULL, PI_SAMR, rpc_sh_user_str_edit,
1534 "Show/Set a user's description" },
1536 { "disabled", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1537 "Show/Set whether a user is disabled" },
1539 { "autolock", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1540 "Show/Set whether a user locked out" },
1542 { "pwnotreq", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1543 "Show/Set whether a user does not need a password" },
1545 { "pwnoexp", NULL, PI_SAMR, rpc_sh_user_flag_edit,
1546 "Show/Set whether a user's password does not expire" },
1548 { NULL, NULL, 0, NULL, NULL }
1554 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1555 TALLOC_CTX *mem_ctx,
1556 struct rpc_sh_ctx *ctx)
1558 static struct rpc_sh_cmd cmds[] = {
1560 { "list", NULL, PI_SAMR, rpc_sh_user_list,
1561 "List available users" },
1563 { "info", NULL, PI_SAMR, rpc_sh_user_info,
1564 "List the domain groups a user is member of" },
1566 { "show", NULL, PI_SAMR, rpc_sh_user_show,
1567 "Show info about a user" },
1569 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1570 "Show/Modify a user's fields" },
1572 { NULL, NULL, 0, NULL, NULL }
1578 /****************************************************************************/
1581 * Basic usage function for 'net rpc group'
1582 * @param argc Standard main() style argc.
1583 * @param argv Standard main() style argv. Initial components are already
1587 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1589 return net_group_usage(c, argc, argv);
1593 * Delete group on a remote RPC server
1595 * All parameters are provided by the run_rpc_command function, except for
1596 * argc, argv which are passes through.
1598 * @param domain_sid The domain sid acquired from the remote server
1599 * @param cli A cli_state connected to the server.
1600 * @param mem_ctx Talloc context, destoyed on completion of the function.
1601 * @param argc Standard main() style argc
1602 * @param argv Standard main() style argv. Initial components are already
1605 * @return Normal NTSTATUS return.
1608 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1609 const DOM_SID *domain_sid,
1610 const char *domain_name,
1611 struct cli_state *cli,
1612 struct rpc_pipe_client *pipe_hnd,
1613 TALLOC_CTX *mem_ctx,
1617 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1618 bool group_is_primary = false;
1619 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1621 struct samr_RidTypeArray *rids = NULL;
1624 /* DOM_GID *user_gids; */
1626 struct samr_Ids group_rids, name_types;
1627 struct lsa_String lsa_acct_name;
1628 union samr_UserInfo *info = NULL;
1631 d_printf("specify group\n");
1632 rpc_group_usage(c, argc,argv);
1633 return NT_STATUS_OK; /* ok? */
1636 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1638 MAXIMUM_ALLOWED_ACCESS,
1641 if (!NT_STATUS_IS_OK(result)) {
1642 d_fprintf(stderr, "Request samr_Connect2 failed\n");
1646 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1648 MAXIMUM_ALLOWED_ACCESS,
1649 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1652 if (!NT_STATUS_IS_OK(result)) {
1653 d_fprintf(stderr, "Request open_domain failed\n");
1657 init_lsa_String(&lsa_acct_name, argv[0]);
1659 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1665 if (!NT_STATUS_IS_OK(result)) {
1666 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1670 switch (name_types.ids[0])
1672 case SID_NAME_DOM_GRP:
1673 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1675 MAXIMUM_ALLOWED_ACCESS,
1678 if (!NT_STATUS_IS_OK(result)) {
1679 d_fprintf(stderr, "Request open_group failed");
1683 group_rid = group_rids.ids[0];
1685 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1689 if (!NT_STATUS_IS_OK(result)) {
1690 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1694 if (c->opt_verbose) {
1695 d_printf("Domain Group %s (rid: %d) has %d members\n",
1696 argv[0],group_rid, rids->count);
1699 /* Check if group is anyone's primary group */
1700 for (i = 0; i < rids->count; i++)
1702 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1704 MAXIMUM_ALLOWED_ACCESS,
1708 if (!NT_STATUS_IS_OK(result)) {
1709 d_fprintf(stderr, "Unable to open group member %d\n",
1714 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1719 if (!NT_STATUS_IS_OK(result)) {
1720 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",
1725 if (info->info21.primary_gid == group_rid) {
1726 if (c->opt_verbose) {
1727 d_printf("Group is primary group of %s\n",
1728 info->info21.account_name.string);
1730 group_is_primary = true;
1733 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1736 if (group_is_primary) {
1737 d_fprintf(stderr, "Unable to delete group because some "
1738 "of it's members have it as primary group\n");
1739 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1743 /* remove all group members */
1744 for (i = 0; i < rids->count; i++)
1747 d_printf("Remove group member %d...",
1749 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1753 if (NT_STATUS_IS_OK(result)) {
1758 d_printf("failed\n");
1763 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1767 /* removing a local group is easier... */
1768 case SID_NAME_ALIAS:
1769 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1771 MAXIMUM_ALLOWED_ACCESS,
1775 if (!NT_STATUS_IS_OK(result)) {
1776 d_fprintf(stderr, "Request open_alias failed\n");
1780 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1784 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1785 argv[0],sid_type_lookup(name_types.ids[0]));
1786 result = NT_STATUS_UNSUCCESSFUL;
1790 if (NT_STATUS_IS_OK(result)) {
1792 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types.ids[0]),argv[0]);
1794 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1795 get_friendly_nt_error_msg(result));
1803 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1805 return run_rpc_command(c, NULL, PI_SAMR, 0, rpc_group_delete_internals,
1809 static NTSTATUS rpc_group_add_internals(struct net_context *c,
1810 const DOM_SID *domain_sid,
1811 const char *domain_name,
1812 struct cli_state *cli,
1813 struct rpc_pipe_client *pipe_hnd,
1814 TALLOC_CTX *mem_ctx,
1818 POLICY_HND connect_pol, domain_pol, group_pol;
1819 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1820 union samr_GroupInfo group_info;
1821 struct lsa_String grp_name;
1825 d_printf("Group name must be specified\n");
1826 rpc_group_usage(c, argc, argv);
1827 return NT_STATUS_OK;
1830 init_lsa_String(&grp_name, argv[0]);
1832 /* Get sam policy handle */
1834 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1836 MAXIMUM_ALLOWED_ACCESS,
1838 if (!NT_STATUS_IS_OK(result)) goto done;
1840 /* Get domain policy handle */
1842 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1844 MAXIMUM_ALLOWED_ACCESS,
1845 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1847 if (!NT_STATUS_IS_OK(result)) goto done;
1849 /* Create the group */
1851 result = rpccli_samr_CreateDomainGroup(pipe_hnd, mem_ctx,
1854 MAXIMUM_ALLOWED_ACCESS,
1857 if (!NT_STATUS_IS_OK(result)) goto done;
1859 if (strlen(c->opt_comment) == 0) goto done;
1861 /* We've got a comment to set */
1863 init_lsa_String(&group_info.description, c->opt_comment);
1865 result = rpccli_samr_SetGroupInfo(pipe_hnd, mem_ctx,
1869 if (!NT_STATUS_IS_OK(result)) goto done;
1872 if (NT_STATUS_IS_OK(result))
1873 DEBUG(5, ("add group succeeded\n"));
1875 d_fprintf(stderr, "add group failed: %s\n", nt_errstr(result));
1880 static NTSTATUS rpc_alias_add_internals(struct net_context *c,
1881 const DOM_SID *domain_sid,
1882 const char *domain_name,
1883 struct cli_state *cli,
1884 struct rpc_pipe_client *pipe_hnd,
1885 TALLOC_CTX *mem_ctx,
1889 POLICY_HND connect_pol, domain_pol, alias_pol;
1890 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1891 union samr_AliasInfo alias_info;
1892 struct lsa_String alias_name;
1896 d_printf("Alias name must be specified\n");
1897 rpc_group_usage(c, argc, argv);
1898 return NT_STATUS_OK;
1901 init_lsa_String(&alias_name, argv[0]);
1903 /* Get sam policy handle */
1905 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1907 MAXIMUM_ALLOWED_ACCESS,
1909 if (!NT_STATUS_IS_OK(result)) goto done;
1911 /* Get domain policy handle */
1913 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1915 MAXIMUM_ALLOWED_ACCESS,
1916 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1918 if (!NT_STATUS_IS_OK(result)) goto done;
1920 /* Create the group */
1922 result = rpccli_samr_CreateDomAlias(pipe_hnd, mem_ctx,
1925 MAXIMUM_ALLOWED_ACCESS,
1928 if (!NT_STATUS_IS_OK(result)) goto done;
1930 if (strlen(c->opt_comment) == 0) goto done;
1932 /* We've got a comment to set */
1934 init_lsa_String(&alias_info.description, c->opt_comment);
1936 result = rpccli_samr_SetAliasInfo(pipe_hnd, mem_ctx,
1941 if (!NT_STATUS_IS_OK(result)) goto done;
1944 if (NT_STATUS_IS_OK(result))
1945 DEBUG(5, ("add alias succeeded\n"));
1947 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1952 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1954 if (c->opt_localgroup)
1955 return run_rpc_command(c, NULL, PI_SAMR, 0,
1956 rpc_alias_add_internals,
1959 return run_rpc_command(c, NULL, PI_SAMR, 0,
1960 rpc_group_add_internals,
1964 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1965 TALLOC_CTX *mem_ctx,
1968 enum lsa_SidType *type)
1970 DOM_SID *sids = NULL;
1971 enum lsa_SidType *types = NULL;
1972 struct rpc_pipe_client *pipe_hnd;
1974 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1976 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
1981 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1982 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1984 if (!NT_STATUS_IS_OK(result)) {
1988 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1989 &name, NULL, 1, &sids, &types);
1991 if (NT_STATUS_IS_OK(result)) {
1992 sid_copy(sid, &sids[0]);
1996 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
2000 TALLOC_FREE(pipe_hnd);
2003 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
2005 /* Try as S-1-5-whatever */
2009 if (string_to_sid(&tmp_sid, name)) {
2010 sid_copy(sid, &tmp_sid);
2011 *type = SID_NAME_UNKNOWN;
2012 result = NT_STATUS_OK;
2019 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2020 TALLOC_CTX *mem_ctx,
2021 const DOM_SID *group_sid,
2024 POLICY_HND connect_pol, domain_pol;
2027 POLICY_HND group_pol;
2029 struct samr_Ids rids, rid_types;
2030 struct lsa_String lsa_acct_name;
2034 sid_copy(&sid, group_sid);
2036 if (!sid_split_rid(&sid, &group_rid)) {
2037 return NT_STATUS_UNSUCCESSFUL;
2040 /* Get sam policy handle */
2041 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2043 MAXIMUM_ALLOWED_ACCESS,
2045 if (!NT_STATUS_IS_OK(result)) {
2049 /* Get domain policy handle */
2050 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2052 MAXIMUM_ALLOWED_ACCESS,
2055 if (!NT_STATUS_IS_OK(result)) {
2059 init_lsa_String(&lsa_acct_name, member);
2061 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2068 if (!NT_STATUS_IS_OK(result)) {
2069 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2073 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2075 MAXIMUM_ALLOWED_ACCESS,
2079 if (!NT_STATUS_IS_OK(result)) {
2083 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
2086 0x0005); /* unknown flags */
2089 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2093 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2094 TALLOC_CTX *mem_ctx,
2095 const DOM_SID *alias_sid,
2098 POLICY_HND connect_pol, domain_pol;
2101 POLICY_HND alias_pol;
2104 enum lsa_SidType member_type;
2108 sid_copy(&sid, alias_sid);
2110 if (!sid_split_rid(&sid, &alias_rid)) {
2111 return NT_STATUS_UNSUCCESSFUL;
2114 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2115 member, &member_sid, &member_type);
2117 if (!NT_STATUS_IS_OK(result)) {
2118 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2122 /* Get sam policy handle */
2123 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2125 MAXIMUM_ALLOWED_ACCESS,
2127 if (!NT_STATUS_IS_OK(result)) {
2131 /* Get domain policy handle */
2132 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2134 MAXIMUM_ALLOWED_ACCESS,
2137 if (!NT_STATUS_IS_OK(result)) {
2141 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2143 MAXIMUM_ALLOWED_ACCESS,
2147 if (!NT_STATUS_IS_OK(result)) {
2151 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
2155 if (!NT_STATUS_IS_OK(result)) {
2160 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2164 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2165 const DOM_SID *domain_sid,
2166 const char *domain_name,
2167 struct cli_state *cli,
2168 struct rpc_pipe_client *pipe_hnd,
2169 TALLOC_CTX *mem_ctx,
2174 enum lsa_SidType group_type;
2177 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2178 return NT_STATUS_UNSUCCESSFUL;
2181 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2182 &group_sid, &group_type))) {
2183 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2184 return NT_STATUS_UNSUCCESSFUL;
2187 if (group_type == SID_NAME_DOM_GRP) {
2188 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2189 &group_sid, argv[1]);
2191 if (!NT_STATUS_IS_OK(result)) {
2192 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2193 argv[1], argv[0], nt_errstr(result));
2198 if (group_type == SID_NAME_ALIAS) {
2199 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2200 &group_sid, argv[1]);
2202 if (!NT_STATUS_IS_OK(result)) {
2203 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2204 argv[1], argv[0], nt_errstr(result));
2209 d_fprintf(stderr, "Can only add members to global or local groups "
2210 "which %s is not\n", argv[0]);
2212 return NT_STATUS_UNSUCCESSFUL;
2215 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2217 return run_rpc_command(c, NULL, PI_SAMR, 0,
2218 rpc_group_addmem_internals,
2222 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2223 struct rpc_pipe_client *pipe_hnd,
2224 TALLOC_CTX *mem_ctx,
2225 const DOM_SID *group_sid,
2228 POLICY_HND connect_pol, domain_pol;
2231 POLICY_HND group_pol;
2233 struct samr_Ids rids, rid_types;
2234 struct lsa_String lsa_acct_name;
2238 sid_copy(&sid, group_sid);
2240 if (!sid_split_rid(&sid, &group_rid))
2241 return NT_STATUS_UNSUCCESSFUL;
2243 /* Get sam policy handle */
2244 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2246 MAXIMUM_ALLOWED_ACCESS,
2248 if (!NT_STATUS_IS_OK(result))
2251 /* Get domain policy handle */
2252 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2254 MAXIMUM_ALLOWED_ACCESS,
2257 if (!NT_STATUS_IS_OK(result))
2260 init_lsa_String(&lsa_acct_name, member);
2262 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2268 if (!NT_STATUS_IS_OK(result)) {
2269 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2273 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2275 MAXIMUM_ALLOWED_ACCESS,
2279 if (!NT_STATUS_IS_OK(result))
2282 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2287 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2291 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2292 TALLOC_CTX *mem_ctx,
2293 const DOM_SID *alias_sid,
2296 POLICY_HND connect_pol, domain_pol;
2299 POLICY_HND alias_pol;
2302 enum lsa_SidType member_type;
2306 sid_copy(&sid, alias_sid);
2308 if (!sid_split_rid(&sid, &alias_rid))
2309 return NT_STATUS_UNSUCCESSFUL;
2311 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2312 member, &member_sid, &member_type);
2314 if (!NT_STATUS_IS_OK(result)) {
2315 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2319 /* Get sam policy handle */
2320 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2322 MAXIMUM_ALLOWED_ACCESS,
2324 if (!NT_STATUS_IS_OK(result)) {
2328 /* Get domain policy handle */
2329 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2331 MAXIMUM_ALLOWED_ACCESS,
2334 if (!NT_STATUS_IS_OK(result)) {
2338 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2340 MAXIMUM_ALLOWED_ACCESS,
2344 if (!NT_STATUS_IS_OK(result))
2347 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2351 if (!NT_STATUS_IS_OK(result))
2355 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2359 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2360 const DOM_SID *domain_sid,
2361 const char *domain_name,
2362 struct cli_state *cli,
2363 struct rpc_pipe_client *pipe_hnd,
2364 TALLOC_CTX *mem_ctx,
2369 enum lsa_SidType group_type;
2372 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2373 return NT_STATUS_UNSUCCESSFUL;
2376 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2377 &group_sid, &group_type))) {
2378 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2379 return NT_STATUS_UNSUCCESSFUL;
2382 if (group_type == SID_NAME_DOM_GRP) {
2383 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2384 &group_sid, argv[1]);
2386 if (!NT_STATUS_IS_OK(result)) {
2387 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2388 argv[1], argv[0], nt_errstr(result));
2393 if (group_type == SID_NAME_ALIAS) {
2394 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2395 &group_sid, argv[1]);
2397 if (!NT_STATUS_IS_OK(result)) {
2398 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2399 argv[1], argv[0], nt_errstr(result));
2404 d_fprintf(stderr, "Can only delete members from global or local groups "
2405 "which %s is not\n", argv[0]);
2407 return NT_STATUS_UNSUCCESSFUL;
2410 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2412 return run_rpc_command(c, NULL, PI_SAMR, 0,
2413 rpc_group_delmem_internals,
2418 * List groups on a remote RPC server
2420 * All parameters are provided by the run_rpc_command function, except for
2421 * argc, argv which are passes through.
2423 * @param domain_sid The domain sid acquired from the remote server
2424 * @param cli A cli_state connected to the server.
2425 * @param mem_ctx Talloc context, destoyed on completion of the function.
2426 * @param argc Standard main() style argc
2427 * @param argv Standard main() style argv. Initial components are already
2430 * @return Normal NTSTATUS return.
2433 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2434 const DOM_SID *domain_sid,
2435 const char *domain_name,
2436 struct cli_state *cli,
2437 struct rpc_pipe_client *pipe_hnd,
2438 TALLOC_CTX *mem_ctx,
2442 POLICY_HND connect_pol, domain_pol;
2443 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2444 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2445 struct samr_SamArray *groups = NULL;
2446 bool global = false;
2448 bool builtin = false;
2456 for (i=0; i<argc; i++) {
2457 if (strequal(argv[i], "global"))
2460 if (strequal(argv[i], "local"))
2463 if (strequal(argv[i], "builtin"))
2467 /* Get sam policy handle */
2469 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2471 MAXIMUM_ALLOWED_ACCESS,
2473 if (!NT_STATUS_IS_OK(result)) {
2477 /* Get domain policy handle */
2479 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2481 MAXIMUM_ALLOWED_ACCESS,
2482 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2484 if (!NT_STATUS_IS_OK(result)) {
2488 /* Query domain groups */
2489 if (c->opt_long_list_entries)
2490 d_printf("\nGroup name Comment"\
2491 "\n-----------------------------\n");
2493 uint32_t max_size, total_size, returned_size;
2494 union samr_DispInfo info;
2498 get_query_dispinfo_params(
2499 loop_count, &max_entries, &max_size);
2501 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2510 num_entries = info.info3.count;
2511 start_idx += info.info3.count;
2513 if (!NT_STATUS_IS_OK(result) &&
2514 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2517 for (i = 0; i < num_entries; i++) {
2519 const char *group = NULL;
2520 const char *desc = NULL;
2522 group = info.info3.entries[i].account_name.string;
2523 desc = info.info3.entries[i].description.string;
2525 if (c->opt_long_list_entries)
2526 printf("%-21.21s %-50.50s\n",
2529 printf("%s\n", group);
2531 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2532 /* query domain aliases */
2537 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2543 if (!NT_STATUS_IS_OK(result) &&
2544 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2547 for (i = 0; i < num_entries; i++) {
2549 const char *description = NULL;
2551 if (c->opt_long_list_entries) {
2553 POLICY_HND alias_pol;
2554 union samr_AliasInfo *info = NULL;
2556 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2559 groups->entries[i].idx,
2561 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2565 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2567 description = info->description.string;
2571 if (description != NULL) {
2572 printf("%-21.21s %-50.50s\n",
2573 groups->entries[i].name.string,
2576 printf("%s\n", groups->entries[i].name.string);
2579 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2580 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2581 /* Get builtin policy handle */
2583 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2585 MAXIMUM_ALLOWED_ACCESS,
2586 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2588 if (!NT_STATUS_IS_OK(result)) {
2591 /* query builtin aliases */
2594 if (!builtin) break;
2596 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2602 if (!NT_STATUS_IS_OK(result) &&
2603 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2606 for (i = 0; i < num_entries; i++) {
2608 const char *description = NULL;
2610 if (c->opt_long_list_entries) {
2612 POLICY_HND alias_pol;
2613 union samr_AliasInfo *info = NULL;
2615 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2618 groups->entries[i].idx,
2620 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2624 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2626 description = info->description.string;
2630 if (description != NULL) {
2631 printf("%-21.21s %-50.50s\n",
2632 groups->entries[i].name.string,
2635 printf("%s\n", groups->entries[i].name.string);
2638 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2644 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2646 return run_rpc_command(c, NULL, PI_SAMR, 0,
2647 rpc_group_list_internals,
2651 static NTSTATUS rpc_list_group_members(struct net_context *c,
2652 struct rpc_pipe_client *pipe_hnd,
2653 TALLOC_CTX *mem_ctx,
2654 const char *domain_name,
2655 const DOM_SID *domain_sid,
2656 POLICY_HND *domain_pol,
2660 POLICY_HND group_pol;
2661 uint32 num_members, *group_rids;
2663 struct samr_RidTypeArray *rids = NULL;
2664 struct lsa_Strings names;
2665 struct samr_Ids types;
2668 sid_to_fstring(sid_str, domain_sid);
2670 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2672 MAXIMUM_ALLOWED_ACCESS,
2676 if (!NT_STATUS_IS_OK(result))
2679 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2683 if (!NT_STATUS_IS_OK(result))
2686 num_members = rids->count;
2687 group_rids = rids->rids;
2689 while (num_members > 0) {
2690 int this_time = 512;
2692 if (num_members < this_time)
2693 this_time = num_members;
2695 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2702 if (!NT_STATUS_IS_OK(result))
2705 /* We only have users as members, but make the output
2706 the same as the output of alias members */
2708 for (i = 0; i < this_time; i++) {
2710 if (c->opt_long_list_entries) {
2711 printf("%s-%d %s\\%s %d\n", sid_str,
2712 group_rids[i], domain_name,
2713 names.names[i].string,
2716 printf("%s\\%s\n", domain_name,
2717 names.names[i].string);
2721 num_members -= this_time;
2725 return NT_STATUS_OK;
2728 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2729 struct rpc_pipe_client *pipe_hnd,
2730 TALLOC_CTX *mem_ctx,
2731 POLICY_HND *domain_pol,
2735 struct rpc_pipe_client *lsa_pipe;
2736 POLICY_HND alias_pol, lsa_pol;
2738 DOM_SID *alias_sids;
2741 enum lsa_SidType *types;
2743 struct lsa_SidArray sid_array;
2745 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2747 MAXIMUM_ALLOWED_ACCESS,
2751 if (!NT_STATUS_IS_OK(result))
2754 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2758 if (!NT_STATUS_IS_OK(result)) {
2759 d_fprintf(stderr, "Couldn't list alias members\n");
2763 num_members = sid_array.num_sids;
2765 if (num_members == 0) {
2766 return NT_STATUS_OK;
2769 lsa_pipe = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2770 PI_LSARPC, &result);
2772 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2773 nt_errstr(result) );
2777 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2778 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2780 if (!NT_STATUS_IS_OK(result)) {
2781 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2782 TALLOC_FREE(lsa_pipe);
2786 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2788 d_fprintf(stderr, "Out of memory\n");
2789 TALLOC_FREE(lsa_pipe);
2790 return NT_STATUS_NO_MEMORY;
2793 for (i=0; i<num_members; i++) {
2794 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2797 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2798 num_members, alias_sids,
2799 &domains, &names, &types);
2801 if (!NT_STATUS_IS_OK(result) &&
2802 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2803 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2804 TALLOC_FREE(lsa_pipe);
2808 for (i = 0; i < num_members; i++) {
2810 sid_to_fstring(sid_str, &alias_sids[i]);
2812 if (c->opt_long_list_entries) {
2813 printf("%s %s\\%s %d\n", sid_str,
2814 domains[i] ? domains[i] : "*unknown*",
2815 names[i] ? names[i] : "*unknown*", types[i]);
2818 printf("%s\\%s\n", domains[i], names[i]);
2820 printf("%s\n", sid_str);
2824 TALLOC_FREE(lsa_pipe);
2825 return NT_STATUS_OK;
2828 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2829 const DOM_SID *domain_sid,
2830 const char *domain_name,
2831 struct cli_state *cli,
2832 struct rpc_pipe_client *pipe_hnd,
2833 TALLOC_CTX *mem_ctx,
2838 POLICY_HND connect_pol, domain_pol;
2839 struct samr_Ids rids, rid_types;
2840 struct lsa_String lsa_acct_name;
2842 /* Get sam policy handle */
2844 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2846 MAXIMUM_ALLOWED_ACCESS,
2849 if (!NT_STATUS_IS_OK(result))
2852 /* Get domain policy handle */
2854 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2856 MAXIMUM_ALLOWED_ACCESS,
2857 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2860 if (!NT_STATUS_IS_OK(result))
2863 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2865 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2872 if (!NT_STATUS_IS_OK(result)) {
2874 /* Ok, did not find it in the global sam, try with builtin */
2876 DOM_SID sid_Builtin;
2878 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2880 sid_copy(&sid_Builtin, &global_sid_Builtin);
2882 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2884 MAXIMUM_ALLOWED_ACCESS,
2888 if (!NT_STATUS_IS_OK(result)) {
2889 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2893 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2900 if (!NT_STATUS_IS_OK(result)) {
2901 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2906 if (rids.count != 1) {
2907 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2911 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2912 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2913 domain_sid, &domain_pol,
2917 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2918 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2922 return NT_STATUS_NO_SUCH_GROUP;
2925 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2928 return rpc_group_usage(c, argc, argv);
2931 return run_rpc_command(c, NULL, PI_SAMR, 0,
2932 rpc_group_members_internals,
2936 static NTSTATUS rpc_group_rename_internals(struct net_context *c,
2937 const DOM_SID *domain_sid,
2938 const char *domain_name,
2939 struct cli_state *cli,
2940 struct rpc_pipe_client *pipe_hnd,
2941 TALLOC_CTX *mem_ctx,
2946 POLICY_HND connect_pol, domain_pol, group_pol;
2947 union samr_GroupInfo group_info;
2948 struct samr_Ids rids, rid_types;
2949 struct lsa_String lsa_acct_name;
2952 d_printf("Usage: 'net rpc group rename group newname'\n");
2953 return NT_STATUS_UNSUCCESSFUL;
2956 /* Get sam policy handle */
2958 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2960 MAXIMUM_ALLOWED_ACCESS,
2963 if (!NT_STATUS_IS_OK(result))
2966 /* Get domain policy handle */
2968 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2970 MAXIMUM_ALLOWED_ACCESS,
2971 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2974 if (!NT_STATUS_IS_OK(result))
2977 init_lsa_String(&lsa_acct_name, argv[0]);
2979 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2986 if (rids.count != 1) {
2987 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2991 if (rid_types.ids[0] != SID_NAME_DOM_GRP) {
2992 d_fprintf(stderr, "Can only rename domain groups\n");
2993 return NT_STATUS_UNSUCCESSFUL;
2996 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2998 MAXIMUM_ALLOWED_ACCESS,
3002 if (!NT_STATUS_IS_OK(result))
3005 init_lsa_String(&group_info.name, argv[1]);
3007 result = rpccli_samr_SetGroupInfo(pipe_hnd, mem_ctx,
3012 if (!NT_STATUS_IS_OK(result))
3015 return NT_STATUS_NO_SUCH_GROUP;
3018 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3021 return rpc_group_usage(c, argc, argv);
3024 return run_rpc_command(c, NULL, PI_SAMR, 0,
3025 rpc_group_rename_internals,
3030 * 'net rpc group' entrypoint.
3031 * @param argc Standard main() style argc
3032 * @param argc Standard main() style argv. Initial components are already
3036 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3038 struct functable func[] = {
3039 {"add", rpc_group_add},
3040 {"delete", rpc_group_delete},
3041 {"addmem", rpc_group_addmem},
3042 {"delmem", rpc_group_delmem},
3043 {"list", rpc_group_list},
3044 {"members", rpc_group_members},
3045 {"rename", rpc_group_rename},
3050 return run_rpc_command(c, NULL, PI_SAMR, 0,
3051 rpc_group_list_internals,
3055 return net_run_function(c, argc, argv, func, rpc_group_usage);
3058 /****************************************************************************/
3060 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3062 return net_share_usage(c, argc, argv);
3066 * Add a share on a remote RPC server
3068 * All parameters are provided by the run_rpc_command function, except for
3069 * argc, argv which are passes through.
3071 * @param domain_sid The domain sid acquired from the remote server
3072 * @param cli A cli_state connected to the server.
3073 * @param mem_ctx Talloc context, destoyed on completion of the function.
3074 * @param argc Standard main() style argc
3075 * @param argv Standard main() style argv. Initial components are already
3078 * @return Normal NTSTATUS return.
3080 static NTSTATUS rpc_share_add_internals(struct net_context *c,
3081 const DOM_SID *domain_sid,
3082 const char *domain_name,
3083 struct cli_state *cli,
3084 struct rpc_pipe_client *pipe_hnd,
3085 TALLOC_CTX *mem_ctx,int argc,
3092 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3093 uint32 num_users=0, perms=0;
3094 char *password=NULL; /* don't allow a share password */
3096 union srvsvc_NetShareInfo info;
3097 struct srvsvc_NetShareInfo2 info2;
3098 uint32_t parm_error = 0;
3100 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3101 return NT_STATUS_NO_MEMORY;
3104 path = strchr(sharename, '=');
3106 return NT_STATUS_UNSUCCESSFUL;
3109 info2.name = sharename;
3111 info2.comment = c->opt_comment;
3112 info2.permissions = perms;
3113 info2.max_users = c->opt_maxusers;
3114 info2.current_users = num_users;
3116 info2.password = password;
3118 info.info2 = &info2;
3120 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
3129 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3131 if ((argc < 1) || !strchr(argv[0], '=')) {
3132 DEBUG(1,("Sharename or path not specified on add\n"));
3133 return rpc_share_usage(c, argc, argv);
3135 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3136 rpc_share_add_internals,
3141 * Delete a share on a remote RPC server
3143 * All parameters are provided by the run_rpc_command function, except for
3144 * argc, argv which are passes through.
3146 * @param domain_sid The domain sid acquired from the remote server
3147 * @param cli A cli_state connected to the server.
3148 * @param mem_ctx Talloc context, destoyed on completion of the function.
3149 * @param argc Standard main() style argc
3150 * @param argv Standard main() style argv. Initial components are already
3153 * @return Normal NTSTATUS return.
3155 static NTSTATUS rpc_share_del_internals(struct net_context *c,
3156 const DOM_SID *domain_sid,
3157 const char *domain_name,
3158 struct cli_state *cli,
3159 struct rpc_pipe_client *pipe_hnd,
3160 TALLOC_CTX *mem_ctx,
3166 return rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
3174 * Delete a share on a remote RPC server
3176 * @param domain_sid The domain sid acquired from the remote server
3177 * @param argc Standard main() style argc
3178 * @param argv Standard main() style argv. Initial components are already
3181 * @return A shell status integer (0 for success)
3183 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3186 DEBUG(1,("Sharename not specified on delete\n"));
3187 return rpc_share_usage(c, argc, argv);
3189 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3190 rpc_share_del_internals,
3195 * Formatted print of share info
3197 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3200 static void display_share_info_1(struct net_context *c,
3201 struct srvsvc_NetShareInfo1 *r)
3203 if (c->opt_long_list_entries) {
3204 d_printf("%-12s %-8.8s %-50s\n",
3206 c->share_type[r->type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)],
3209 d_printf("%s\n", r->name);
3213 static WERROR get_share_info(struct net_context *c,
3214 struct rpc_pipe_client *pipe_hnd,
3215 TALLOC_CTX *mem_ctx,
3219 struct srvsvc_NetShareInfoCtr *info_ctr)
3223 union srvsvc_NetShareInfo info;
3225 /* no specific share requested, enumerate all */
3228 uint32_t preferred_len = 0xffffffff;
3229 uint32_t total_entries = 0;
3230 uint32_t resume_handle = 0;
3232 info_ctr->level = level;
3234 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
3244 /* request just one share */
3245 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
3252 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
3257 ZERO_STRUCTP(info_ctr);
3259 info_ctr->level = level;
3264 struct srvsvc_NetShareCtr1 *ctr1;
3266 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3267 W_ERROR_HAVE_NO_MEMORY(ctr1);
3270 ctr1->array = info.info1;
3272 info_ctr->ctr.ctr1 = ctr1;
3276 struct srvsvc_NetShareCtr2 *ctr2;
3278 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3279 W_ERROR_HAVE_NO_MEMORY(ctr2);
3282 ctr2->array = info.info2;
3284 info_ctr->ctr.ctr2 = ctr2;
3288 struct srvsvc_NetShareCtr502 *ctr502;
3290 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3291 W_ERROR_HAVE_NO_MEMORY(ctr502);
3294 ctr502->array = info.info502;
3296 info_ctr->ctr.ctr502 = ctr502;
3304 * List shares on a remote RPC server
3306 * All parameters are provided by the run_rpc_command function, except for
3307 * argc, argv which are passes through.
3309 * @param domain_sid The domain sid acquired from the remote server
3310 * @param cli A cli_state connected to the server.
3311 * @param mem_ctx Talloc context, destoyed on completion of the function.
3312 * @param argc Standard main() style argc
3313 * @param argv Standard main() style argv. Initial components are already
3316 * @return Normal NTSTATUS return.
3319 static NTSTATUS rpc_share_list_internals(struct net_context *c,
3320 const DOM_SID *domain_sid,
3321 const char *domain_name,
3322 struct cli_state *cli,
3323 struct rpc_pipe_client *pipe_hnd,
3324 TALLOC_CTX *mem_ctx,
3328 struct srvsvc_NetShareInfoCtr info_ctr;
3329 struct srvsvc_NetShareCtr1 ctr1;
3331 uint32 i, level = 1;
3333 ZERO_STRUCT(info_ctr);
3337 info_ctr.ctr.ctr1 = &ctr1;
3339 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3341 if (!W_ERROR_IS_OK(result))
3344 /* Display results */
3346 if (c->opt_long_list_entries) {
3348 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3349 "\nShare name Type Description\n"\
3350 "---------- ---- -----------\n");
3352 for (i = 0; i < info_ctr.ctr.ctr1->count; i++)
3353 display_share_info_1(c, &info_ctr.ctr.ctr1->array[i]);
3355 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3359 * 'net rpc share list' entrypoint.
3360 * @param argc Standard main() style argc
3361 * @param argv Standard main() style argv. Initial components are already
3364 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3366 return run_rpc_command(c, NULL, PI_SRVSVC, 0, rpc_share_list_internals,
3370 static bool check_share_availability(struct cli_state *cli, const char *netname)
3372 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3373 d_printf("skipping [%s]: not a file share.\n", netname);
3383 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3384 const char *netname, uint32 type)
3386 /* only support disk shares */
3387 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3388 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3392 /* skip builtin shares */
3393 /* FIXME: should print$ be added too ? */
3394 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3395 strequal(netname,"global"))
3398 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3399 printf("excluding [%s]\n", netname);
3403 return check_share_availability(cli, netname);
3407 * Migrate shares from a remote RPC server to the local RPC server
3409 * All parameters are provided by the run_rpc_command function, except for
3410 * argc, argv which are passed through.
3412 * @param domain_sid The domain sid acquired from the remote server
3413 * @param cli A cli_state connected to the server.
3414 * @param mem_ctx Talloc context, destroyed on completion of the function.
3415 * @param argc Standard main() style argc
3416 * @param argv Standard main() style argv. Initial components are already
3419 * @return Normal NTSTATUS return.
3422 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3423 const DOM_SID *domain_sid,
3424 const char *domain_name,
3425 struct cli_state *cli,
3426 struct rpc_pipe_client *pipe_hnd,
3427 TALLOC_CTX *mem_ctx,
3432 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3433 struct srvsvc_NetShareInfoCtr ctr_src;
3435 struct rpc_pipe_client *srvsvc_pipe = NULL;
3436 struct cli_state *cli_dst = NULL;
3437 uint32 level = 502; /* includes secdesc */
3438 uint32_t parm_error = 0;
3440 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3442 if (!W_ERROR_IS_OK(result))
3445 /* connect destination PI_SRVSVC */
3446 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe, PI_SRVSVC);
3447 if (!NT_STATUS_IS_OK(nt_status))
3451 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3453 union srvsvc_NetShareInfo info;
3454 struct srvsvc_NetShareInfo502 info502 =
3455 ctr_src.ctr.ctr502->array[i];
3457 /* reset error-code */
3458 nt_status = NT_STATUS_UNSUCCESSFUL;
3460 if (!check_share_sanity(c, cli, info502.name, info502.type))
3463 /* finally add the share on the dst server */
3465 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3466 info502.name, info502.path, info502.comment);
3468 info.info502 = &info502;
3470 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3471 srvsvc_pipe->desthost,
3477 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3478 printf(" [%s] does already exist\n",
3483 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3484 printf("cannot add share: %s\n", dos_errstr(result));
3490 nt_status = NT_STATUS_OK;
3494 cli_shutdown(cli_dst);
3502 * Migrate shares from a rpc-server to another
3504 * @param argc Standard main() style argc
3505 * @param argv Standard main() style argv. Initial components are already
3508 * @return A shell status integer (0 for success)
3510 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3515 printf("no server to migrate\n");
3519 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3520 rpc_share_migrate_shares_internals,
3527 * @param f file_info
3528 * @param mask current search mask
3529 * @param state arg-pointer
3532 static void copy_fn(const char *mnt, file_info *f,
3533 const char *mask, void *state)
3535 static NTSTATUS nt_status;
3536 static struct copy_clistate *local_state;
3537 static fstring filename, new_mask;
3540 struct net_context *c;
3542 local_state = (struct copy_clistate *)state;
3543 nt_status = NT_STATUS_UNSUCCESSFUL;
3547 if (strequal(f->name, ".") || strequal(f->name, ".."))
3550 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3553 if (f->mode & aDIR) {
3555 DEBUG(3,("got dir: %s\n", f->name));
3557 fstrcpy(dir, local_state->cwd);
3559 fstrcat(dir, f->name);
3561 switch (net_mode_share)
3563 case NET_MODE_SHARE_MIGRATE:
3564 /* create that directory */
3565 nt_status = net_copy_file(c, local_state->mem_ctx,
3566 local_state->cli_share_src,
3567 local_state->cli_share_dst,
3569 c->opt_acls? true : false,
3570 c->opt_attrs? true : false,
3571 c->opt_timestamps? true:false,
3575 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3579 if (!NT_STATUS_IS_OK(nt_status))
3580 printf("could not handle dir %s: %s\n",
3581 dir, nt_errstr(nt_status));
3583 /* search below that directory */
3584 fstrcpy(new_mask, dir);
3585 fstrcat(new_mask, "\\*");
3587 old_dir = local_state->cwd;
3588 local_state->cwd = dir;
3589 if (!sync_files(local_state, new_mask))
3590 printf("could not handle files\n");
3591 local_state->cwd = old_dir;
3598 fstrcpy(filename, local_state->cwd);
3599 fstrcat(filename, "\\");
3600 fstrcat(filename, f->name);
3602 DEBUG(3,("got file: %s\n", filename));
3604 switch (net_mode_share)
3606 case NET_MODE_SHARE_MIGRATE:
3607 nt_status = net_copy_file(c, local_state->mem_ctx,
3608 local_state->cli_share_src,
3609 local_state->cli_share_dst,
3611 c->opt_acls? true : false,
3612 c->opt_attrs? true : false,
3613 c->opt_timestamps? true: false,
3617 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3621 if (!NT_STATUS_IS_OK(nt_status))
3622 printf("could not handle file %s: %s\n",
3623 filename, nt_errstr(nt_status));
3628 * sync files, can be called recursivly to list files
3629 * and then call copy_fn for each file
3631 * @param cp_clistate pointer to the copy_clistate we work with
3632 * @param mask the current search mask
3634 * @return Boolean result
3636 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3638 struct cli_state *targetcli;
3639 char *targetpath = NULL;
3641 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3643 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate->cli_share_src,
3644 mask, &targetcli, &targetpath ) ) {
3645 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3646 mask, cli_errstr(cp_clistate->cli_share_src));
3650 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3651 d_fprintf(stderr, "listing %s failed with error: %s\n",
3652 mask, cli_errstr(targetcli));
3661 * Set the top level directory permissions before we do any further copies.
3662 * Should set up ACL inheritance.
3665 bool copy_top_level_perms(struct net_context *c,
3666 struct copy_clistate *cp_clistate,
3667 const char *sharename)
3669 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3671 switch (net_mode_share) {
3672 case NET_MODE_SHARE_MIGRATE:
3673 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3674 nt_status = net_copy_fileattr(c,
3675 cp_clistate->mem_ctx,
3676 cp_clistate->cli_share_src,
3677 cp_clistate->cli_share_dst,
3679 c->opt_acls? true : false,
3680 c->opt_attrs? true : false,
3681 c->opt_timestamps? true: false,
3685 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3689 if (!NT_STATUS_IS_OK(nt_status)) {
3690 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3691 sharename, nt_errstr(nt_status));
3699 * Sync all files inside a remote share to another share (over smb)
3701 * All parameters are provided by the run_rpc_command function, except for
3702 * argc, argv which are passes through.
3704 * @param domain_sid The domain sid acquired from the remote server
3705 * @param cli A cli_state connected to the server.
3706 * @param mem_ctx Talloc context, destoyed on completion of the function.
3707 * @param argc Standard main() style argc
3708 * @param argv Standard main() style argv. Initial components are already
3711 * @return Normal NTSTATUS return.
3714 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3715 const DOM_SID *domain_sid,
3716 const char *domain_name,
3717 struct cli_state *cli,
3718 struct rpc_pipe_client *pipe_hnd,
3719 TALLOC_CTX *mem_ctx,
3724 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3725 struct srvsvc_NetShareInfoCtr ctr_src;
3728 struct copy_clistate cp_clistate;
3729 bool got_src_share = false;
3730 bool got_dst_share = false;
3731 const char *mask = "\\*";
3734 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3736 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3739 if (!W_ERROR_IS_OK(result))
3742 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3744 struct srvsvc_NetShareInfo502 info502 =
3745 ctr_src.ctr.ctr502->array[i];
3747 if (!check_share_sanity(c, cli, info502.name, info502.type))
3750 /* one might not want to mirror whole discs :) */
3751 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3752 d_printf("skipping [%s]: builtin/hidden share\n", info502.name);
3756 switch (net_mode_share)
3758 case NET_MODE_SHARE_MIGRATE:
3762 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3765 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3767 c->opt_acls ? "including" : "without",
3768 c->opt_attrs ? "including" : "without",
3769 c->opt_timestamps ? "(preserving timestamps)" : "");
3771 cp_clistate.mem_ctx = mem_ctx;
3772 cp_clistate.cli_share_src = NULL;
3773 cp_clistate.cli_share_dst = NULL;
3774 cp_clistate.cwd = NULL;
3775 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3778 /* open share source */
3779 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3780 &cli->dest_ss, cli->desthost,
3781 info502.name, "A:");
3782 if (!NT_STATUS_IS_OK(nt_status))
3785 got_src_share = true;
3787 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3788 /* open share destination */
3789 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3790 NULL, dst, info502.name, "A:");
3791 if (!NT_STATUS_IS_OK(nt_status))
3794 got_dst_share = true;
3797 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3798 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", info502.name);
3799 nt_status = NT_STATUS_UNSUCCESSFUL;
3803 if (!sync_files(&cp_clistate, mask)) {
3804 d_fprintf(stderr, "could not handle files for share: %s\n", info502.name);
3805 nt_status = NT_STATUS_UNSUCCESSFUL;
3810 nt_status = NT_STATUS_OK;
3815 cli_shutdown(cp_clistate.cli_share_src);
3818 cli_shutdown(cp_clistate.cli_share_dst);
3824 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3828 printf("no server to migrate\n");
3832 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3833 rpc_share_migrate_files_internals,
3838 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3840 * All parameters are provided by the run_rpc_command function, except for
3841 * argc, argv which are passes through.
3843 * @param domain_sid The domain sid acquired from the remote server
3844 * @param cli A cli_state connected to the server.
3845 * @param mem_ctx Talloc context, destoyed on completion of the function.
3846 * @param argc Standard main() style argc
3847 * @param argv Standard main() style argv. Initial components are already
3850 * @return Normal NTSTATUS return.
3853 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3854 const DOM_SID *domain_sid,
3855 const char *domain_name,
3856 struct cli_state *cli,
3857 struct rpc_pipe_client *pipe_hnd,
3858 TALLOC_CTX *mem_ctx,
3863 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3864 struct srvsvc_NetShareInfoCtr ctr_src;
3865 union srvsvc_NetShareInfo info;
3867 struct rpc_pipe_client *srvsvc_pipe = NULL;
3868 struct cli_state *cli_dst = NULL;
3869 uint32 level = 502; /* includes secdesc */
3870 uint32_t parm_error = 0;
3872 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3875 if (!W_ERROR_IS_OK(result))
3878 /* connect destination PI_SRVSVC */
3879 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe, PI_SRVSVC);
3880 if (!NT_STATUS_IS_OK(nt_status))
3884 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3886 struct srvsvc_NetShareInfo502 info502 =
3887 ctr_src.ctr.ctr502->array[i];
3889 /* reset error-code */
3890 nt_status = NT_STATUS_UNSUCCESSFUL;
3892 if (!check_share_sanity(c, cli, info502.name, info502.type))
3895 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3896 info502.name, info502.path, info502.comment);
3899 display_sec_desc(info502.sd_buf.sd);
3901 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3902 info.info502 = &info502;
3904 /* finally modify the share on the dst server */
3905 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3906 srvsvc_pipe->desthost,
3912 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3913 printf("cannot set share-acl: %s\n", dos_errstr(result));
3919 nt_status = NT_STATUS_OK;
3923 cli_shutdown(cli_dst);
3931 * Migrate share-acls from a rpc-server to another
3933 * @param argc Standard main() style argc
3934 * @param argv Standard main() style argv. Initial components are already
3937 * @return A shell status integer (0 for success)
3939 static int rpc_share_migrate_security(struct net_context *c, int argc,
3944 printf("no server to migrate\n");
3948 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3949 rpc_share_migrate_security_internals,
3954 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3955 * from one server to another
3957 * @param argc Standard main() style argc
3958 * @param argv Standard main() style argv. Initial components are already
3961 * @return A shell status integer (0 for success)
3964 static int rpc_share_migrate_all(struct net_context *c, int argc,
3970 printf("no server to migrate\n");
3974 /* order is important. we don't want to be locked out by the share-acl
3975 * before copying files - gd */
3977 ret = run_rpc_command(c, NULL, PI_SRVSVC, 0,
3978 rpc_share_migrate_shares_internals, argc, argv);
3982 ret = run_rpc_command(c, NULL, PI_SRVSVC, 0,
3983 rpc_share_migrate_files_internals, argc, argv);
3987 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3988 rpc_share_migrate_security_internals, argc,
3994 * 'net rpc share migrate' entrypoint.
3995 * @param argc Standard main() style argc
3996 * @param argv Standard main() style argv. Initial components are already
3999 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4002 struct functable func[] = {
4003 {"all", rpc_share_migrate_all},
4004 {"files", rpc_share_migrate_files},
4005 {"help", rpc_share_usage},
4006 {"security", rpc_share_migrate_security},
4007 {"shares", rpc_share_migrate_shares},
4011 net_mode_share = NET_MODE_SHARE_MIGRATE;
4013 return net_run_function(c, argc, argv, func, rpc_share_usage);
4022 static int num_server_aliases;
4023 static struct full_alias *server_aliases;
4026 * Add an alias to the static list.
4028 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4030 if (server_aliases == NULL)
4031 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4033 server_aliases[num_server_aliases] = *alias;
4034 num_server_aliases += 1;
4038 * For a specific domain on the server, fetch all the aliases
4039 * and their members. Add all of them to the server_aliases.
4042 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4043 TALLOC_CTX *mem_ctx,
4044 POLICY_HND *connect_pol,
4045 const DOM_SID *domain_sid)
4047 uint32 start_idx, max_entries, num_entries, i;
4048 struct samr_SamArray *groups = NULL;
4050 POLICY_HND domain_pol;
4052 /* Get domain policy handle */
4054 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
4056 MAXIMUM_ALLOWED_ACCESS,
4057 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4059 if (!NT_STATUS_IS_OK(result))
4066 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
4072 for (i = 0; i < num_entries; i++) {
4074 POLICY_HND alias_pol;
4075 struct full_alias alias;
4076 struct lsa_SidArray sid_array;
4079 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
4081 MAXIMUM_ALLOWED_ACCESS,
4082 groups->entries[i].idx,
4084 if (!NT_STATUS_IS_OK(result))
4087 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
4090 if (!NT_STATUS_IS_OK(result))
4093 alias.num_members = sid_array.num_sids;
4095 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
4096 if (!NT_STATUS_IS_OK(result))
4099 alias.members = NULL;
4101 if (alias.num_members > 0) {
4102 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
4104 for (j = 0; j < alias.num_members; j++)
4105 sid_copy(&alias.members[j],
4106 sid_array.sids[j].sid);
4109 sid_copy(&alias.sid, domain_sid);
4110 sid_append_rid(&alias.sid, groups->entries[i].idx);
4112 push_alias(mem_ctx, &alias);
4114 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4116 result = NT_STATUS_OK;
4119 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4125 * Dump server_aliases as names for debugging purposes.
4128 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4129 const DOM_SID *domain_sid,
4130 const char *domain_name,
4131 struct cli_state *cli,
4132 struct rpc_pipe_client *pipe_hnd,
4133 TALLOC_CTX *mem_ctx,
4141 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4142 SEC_RIGHTS_MAXIMUM_ALLOWED,
4144 if (!NT_STATUS_IS_OK(result))
4147 for (i=0; i<num_server_aliases; i++) {
4150 enum lsa_SidType *types;
4153 struct full_alias *alias = &server_aliases[i];
4155 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4157 &domains, &names, &types);
4158 if (!NT_STATUS_IS_OK(result))
4161 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4163 if (alias->num_members == 0) {
4168 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4171 &domains, &names, &types);
4173 if (!NT_STATUS_IS_OK(result) &&
4174 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4177 for (j=0; j<alias->num_members; j++)
4178 DEBUG(1, ("%s\\%s (%d); ",
4179 domains[j] ? domains[j] : "*unknown*",
4180 names[j] ? names[j] : "*unknown*",types[j]));
4184 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4186 return NT_STATUS_OK;
4190 * Fetch a list of all server aliases and their members into
4194 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4195 const DOM_SID *domain_sid,
4196 const char *domain_name,
4197 struct cli_state *cli,
4198 struct rpc_pipe_client *pipe_hnd,
4199 TALLOC_CTX *mem_ctx,
4204 POLICY_HND connect_pol;
4206 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4208 MAXIMUM_ALLOWED_ACCESS,
4211 if (!NT_STATUS_IS_OK(result))
4214 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4215 &global_sid_Builtin);
4217 if (!NT_STATUS_IS_OK(result))
4220 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4223 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4228 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4230 token->num_sids = 4;
4232 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4233 d_fprintf(stderr, "malloc failed\n");
4234 token->num_sids = 0;
4238 token->user_sids[0] = *user_sid;
4239 sid_copy(&token->user_sids[1], &global_sid_World);
4240 sid_copy(&token->user_sids[2], &global_sid_Network);
4241 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4244 static void free_user_token(NT_USER_TOKEN *token)
4246 SAFE_FREE(token->user_sids);
4249 static bool is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4253 for (i=0; i<token->num_sids; i++) {
4254 if (sid_compare(sid, &token->user_sids[i]) == 0)
4260 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4262 if (is_sid_in_token(token, sid))
4265 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4266 if (!token->user_sids) {
4270 sid_copy(&token->user_sids[token->num_sids], sid);
4272 token->num_sids += 1;
4277 NT_USER_TOKEN token;
4280 static void dump_user_token(struct user_token *token)
4284 d_printf("%s\n", token->name);
4286 for (i=0; i<token->token.num_sids; i++) {
4287 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4291 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4295 for (i=0; i<alias->num_members; i++) {
4296 if (sid_compare(sid, &alias->members[i]) == 0)
4303 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4307 for (i=0; i<num_server_aliases; i++) {
4308 if (is_alias_member(&sid, &server_aliases[i]))
4309 add_sid_to_token(token, &server_aliases[i].sid);
4314 * We got a user token with all the SIDs we can know about without asking the
4315 * server directly. These are the user and domain group sids. All of these can
4316 * be members of aliases. So scan the list of aliases for each of the SIDs and
4317 * add them to the token.
4320 static void collect_alias_memberships(NT_USER_TOKEN *token)
4322 int num_global_sids = token->num_sids;
4325 for (i=0; i<num_global_sids; i++) {
4326 collect_sid_memberships(token, token->user_sids[i]);
4330 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4332 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4333 enum wbcSidType type;
4335 struct wbcDomainSid wsid;
4336 char *sid_str = NULL;
4338 uint32_t num_groups;
4339 gid_t *groups = NULL;
4342 fstr_sprintf(full_name, "%s%c%s",
4343 domain, *lp_winbind_separator(), user);
4345 /* First let's find out the user sid */
4347 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4349 if (!WBC_ERROR_IS_OK(wbc_status)) {
4350 DEBUG(1, ("winbind could not find %s: %s\n",
4351 full_name, wbcErrorString(wbc_status)));
4355 wbc_status = wbcSidToString(&wsid, &sid_str);
4356 if (!WBC_ERROR_IS_OK(wbc_status)) {
4360 if (type != SID_NAME_USER) {
4361 wbcFreeMemory(sid_str);
4362 DEBUG(1, ("%s is not a user\n", full_name));
4366 string_to_sid(&user_sid, sid_str);
4367 wbcFreeMemory(sid_str);
4370 init_user_token(token, &user_sid);
4372 /* And now the groups winbind knows about */
4374 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4375 if (!WBC_ERROR_IS_OK(wbc_status)) {
4376 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4377 full_name, wbcErrorString(wbc_status)));
4381 for (i = 0; i < num_groups; i++) {
4382 gid_t gid = groups[i];
4385 wbc_status = wbcGidToSid(gid, &wsid);
4386 if (!WBC_ERROR_IS_OK(wbc_status)) {
4387 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4388 gid, wbcErrorString(wbc_status)));
4389 wbcFreeMemory(groups);
4393 wbc_status = wbcSidToString(&wsid, &sid_str);
4394 if (!WBC_ERROR_IS_OK(wbc_status)) {
4395 wbcFreeMemory(groups);
4399 DEBUG(3, (" %s\n", sid_str));
4401 string_to_sid(&sid, sid_str);
4402 wbcFreeMemory(sid_str);
4405 add_sid_to_token(token, &sid);
4407 wbcFreeMemory(groups);
4413 * Get a list of all user tokens we want to look at
4416 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4417 struct user_token **user_tokens)
4419 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4420 uint32_t i, num_users;
4422 struct user_token *result;
4423 TALLOC_CTX *frame = NULL;
4425 if (lp_winbind_use_default_domain() &&
4426 (c->opt_target_workgroup == NULL)) {
4427 d_fprintf(stderr, "winbind use default domain = yes set, "
4428 "please specify a workgroup\n");
4432 /* Send request to winbind daemon */
4434 wbc_status = wbcListUsers(NULL, &num_users, &users);
4435 if (!WBC_ERROR_IS_OK(wbc_status)) {
4436 DEBUG(1, ("winbind could not list users: %s\n",
4437 wbcErrorString(wbc_status)));
4441 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4443 if (result == NULL) {
4444 DEBUG(1, ("Could not malloc sid array\n"));
4445 wbcFreeMemory(users);
4449 frame = talloc_stackframe();
4450 for (i=0; i < num_users; i++) {
4451 fstring domain, user;
4454 fstrcpy(result[i].name, users[i]);
4456 p = strchr(users[i], *lp_winbind_separator());
4458 DEBUG(3, ("%s\n", users[i]));
4461 fstrcpy(domain, c->opt_target_workgroup);
4462 fstrcpy(user, users[i]);
4465 fstrcpy(domain, users[i]);
4470 get_user_sids(domain, user, &(result[i].token));
4474 wbcFreeMemory(users);
4476 *num_tokens = num_users;
4477 *user_tokens = result;
4482 static bool get_user_tokens_from_file(FILE *f,
4484 struct user_token **tokens)
4486 struct user_token *token = NULL;
4491 if (fgets(line, sizeof(line)-1, f) == NULL) {
4495 if (line[strlen(line)-1] == '\n')
4496 line[strlen(line)-1] = '\0';
4498 if (line[0] == ' ') {
4502 string_to_sid(&sid, &line[1]);
4504 if (token == NULL) {
4505 DEBUG(0, ("File does not begin with username"));
4509 add_sid_to_token(&token->token, &sid);
4513 /* And a new user... */
4516 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4517 if (*tokens == NULL) {
4518 DEBUG(0, ("Could not realloc tokens\n"));
4522 token = &((*tokens)[*num_tokens-1]);
4524 fstrcpy(token->name, line);
4525 token->token.num_sids = 0;
4526 token->token.user_sids = NULL;
4535 * Show the list of all users that have access to a share
4538 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4539 TALLOC_CTX *mem_ctx,
4540 const char *netname,
4542 struct user_token *tokens)
4545 SEC_DESC *share_sd = NULL;
4546 SEC_DESC *root_sd = NULL;
4547 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4549 union srvsvc_NetShareInfo info;
4554 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4561 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4562 DEBUG(1, ("Coult not query secdesc for share %s\n",
4567 share_sd = info.info502->sd_buf.sd;
4568 if (share_sd == NULL) {
4569 DEBUG(1, ("Got no secdesc for share %s\n",
4575 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4579 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4582 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4585 for (i=0; i<num_tokens; i++) {
4588 if (share_sd != NULL) {
4589 if (!se_access_check(share_sd, &tokens[i].token,
4590 1, &acc_granted, &status)) {
4591 DEBUG(1, ("Could not check share_sd for "
4597 if (!NT_STATUS_IS_OK(status))
4601 if (root_sd == NULL) {
4602 d_printf(" %s\n", tokens[i].name);
4606 if (!se_access_check(root_sd, &tokens[i].token,
4607 1, &acc_granted, &status)) {
4608 DEBUG(1, ("Could not check root_sd for user %s\n",
4613 if (!NT_STATUS_IS_OK(status))
4616 d_printf(" %s\n", tokens[i].name);
4620 cli_close(cli, fnum);
4632 static void collect_share(const char *name, uint32 m,
4633 const char *comment, void *state)
4635 struct share_list *share_list = (struct share_list *)state;
4637 if (m != STYPE_DISKTREE)
4640 share_list->num_shares += 1;
4641 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4642 if (!share_list->shares) {
4643 share_list->num_shares = 0;
4646 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4649 static void rpc_share_userlist_usage(void)
4655 * List shares on a remote RPC server, including the security descriptors
4657 * All parameters are provided by the run_rpc_command function, except for
4658 * argc, argv which are passes through.
4660 * @param domain_sid The domain sid acquired from the remote server
4661 * @param cli A cli_state connected to the server.
4662 * @param mem_ctx Talloc context, destoyed on completion of the function.
4663 * @param argc Standard main() style argc
4664 * @param argv Standard main() style argv. Initial components are already
4667 * @return Normal NTSTATUS return.
4670 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4671 const DOM_SID *domain_sid,
4672 const char *domain_name,
4673 struct cli_state *cli,
4674 struct rpc_pipe_client *pipe_hnd,
4675 TALLOC_CTX *mem_ctx,
4685 struct user_token *tokens = NULL;
4688 struct share_list share_list;
4691 rpc_share_userlist_usage();
4692 return NT_STATUS_UNSUCCESSFUL;
4698 f = fopen(argv[0], "r");
4702 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4703 return NT_STATUS_UNSUCCESSFUL;
4706 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4712 DEBUG(0, ("Could not read users from file\n"));
4713 return NT_STATUS_UNSUCCESSFUL;
4716 for (i=0; i<num_tokens; i++)
4717 collect_alias_memberships(&tokens[i].token);
4719 init_enum_hnd(&hnd, 0);
4721 share_list.num_shares = 0;
4722 share_list.shares = NULL;
4724 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4727 DEBUG(0, ("Error returning browse list: %s\n",
4732 for (i = 0; i < share_list.num_shares; i++) {
4733 char *netname = share_list.shares[i];
4735 if (netname[strlen(netname)-1] == '$')
4738 d_printf("%s\n", netname);
4740 show_userlist(pipe_hnd, mem_ctx, netname,
4741 num_tokens, tokens);
4744 for (i=0; i<num_tokens; i++) {
4745 free_user_token(&tokens[i].token);
4748 SAFE_FREE(share_list.shares);
4750 return NT_STATUS_OK;
4753 static int rpc_share_allowedusers(struct net_context *c, int argc,
4758 result = run_rpc_command(c, NULL, PI_SAMR, 0,
4759 rpc_aliaslist_internals,
4764 result = run_rpc_command(c, NULL, PI_LSARPC, 0,
4770 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
4771 rpc_share_allowedusers_internals,
4775 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4778 struct user_token *tokens = NULL;
4782 net_usersidlist_usage(c, argc, argv);
4786 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4787 DEBUG(0, ("Could not get the user/sid list\n"));
4791 for (i=0; i<num_tokens; i++) {
4792 dump_user_token(&tokens[i]);
4793 free_user_token(&tokens[i].token);
4800 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4802 d_printf("net usersidlist\n"
4803 "\tprints out a list of all users the running winbind knows\n"
4804 "\tabout, together with all their SIDs. This is used as\n"
4805 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4807 net_common_flags_usage(c, argc, argv);
4812 * 'net rpc share' entrypoint.
4813 * @param argc Standard main() style argc
4814 * @param argv Standard main() style argv. Initial components are already
4818 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4820 struct functable func[] = {
4821 {"add", rpc_share_add},
4822 {"delete", rpc_share_delete},
4823 {"allowedusers", rpc_share_allowedusers},
4824 {"migrate", rpc_share_migrate},
4825 {"list", rpc_share_list},
4830 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
4831 rpc_share_list_internals,
4834 return net_run_function(c, argc, argv, func, rpc_share_usage);
4837 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4838 TALLOC_CTX *mem_ctx,
4839 struct rpc_sh_ctx *ctx,
4840 struct rpc_pipe_client *pipe_hnd,
4841 int argc, const char **argv)
4843 return rpc_share_list_internals(c, ctx->domain_sid, ctx->domain_name,
4844 ctx->cli, pipe_hnd, mem_ctx,
4848 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4849 TALLOC_CTX *mem_ctx,
4850 struct rpc_sh_ctx *ctx,
4851 struct rpc_pipe_client *pipe_hnd,
4852 int argc, const char **argv)
4856 uint32_t parm_err = 0;
4857 union srvsvc_NetShareInfo info;
4858 struct srvsvc_NetShareInfo2 info2;
4860 if ((argc < 2) || (argc > 3)) {
4861 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4863 return NT_STATUS_INVALID_PARAMETER;
4866 info2.name = argv[0];
4867 info2.type = STYPE_DISKTREE;
4868 info2.comment = (argc == 3) ? argv[2] : "";
4869 info2.permissions = 0;
4870 info2.max_users = 0;
4871 info2.current_users = 0;
4872 info2.path = argv[1];
4873 info2.password = NULL;
4875 info.info2 = &info2;
4877 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
4887 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4888 TALLOC_CTX *mem_ctx,
4889 struct rpc_sh_ctx *ctx,
4890 struct rpc_pipe_client *pipe_hnd,
4891 int argc, const char **argv)
4897 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4898 return NT_STATUS_INVALID_PARAMETER;
4901 status = rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
4910 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4911 TALLOC_CTX *mem_ctx,
4912 struct rpc_sh_ctx *ctx,
4913 struct rpc_pipe_client *pipe_hnd,
4914 int argc, const char **argv)
4916 union srvsvc_NetShareInfo info;
4921 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4922 return NT_STATUS_INVALID_PARAMETER;
4925 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4931 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4935 d_printf("Name: %s\n", info.info2->name);
4936 d_printf("Comment: %s\n", info.info2->comment);
4937 d_printf("Path: %s\n", info.info2->path);
4938 d_printf("Password: %s\n", info.info2->password);
4941 return werror_to_ntstatus(result);
4944 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4945 struct rpc_sh_ctx *ctx)
4947 static struct rpc_sh_cmd cmds[] = {
4949 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
4950 "List available shares" },
4952 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
4955 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
4958 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
4959 "Get information about a share" },
4961 { NULL, NULL, 0, NULL, NULL }
4967 /****************************************************************************/
4969 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4971 return net_file_usage(c, argc, argv);
4975 * Close a file on a remote RPC server
4977 * All parameters are provided by the run_rpc_command function, except for
4978 * argc, argv which are passes through.
4980 * @param c A net_context structure
4981 * @param domain_sid The domain sid acquired from the remote server
4982 * @param cli A cli_state connected to the server.
4983 * @param mem_ctx Talloc context, destoyed on completion of the function.
4984 * @param argc Standard main() style argc
4985 * @param argv Standard main() style argv. Initial components are already
4988 * @return Normal NTSTATUS return.
4990 static NTSTATUS rpc_file_close_internals(struct net_context *c,
4991 const DOM_SID *domain_sid,
4992 const char *domain_name,
4993 struct cli_state *cli,
4994 struct rpc_pipe_client *pipe_hnd,
4995 TALLOC_CTX *mem_ctx,
4999 return rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx,
5001 atoi(argv[0]), NULL);
5005 * Close a file on a remote RPC server
5007 * @param argc Standard main() style argc
5008 * @param argv Standard main() style argv. Initial components are already
5011 * @return A shell status integer (0 for success)
5013 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5016 DEBUG(1, ("No fileid given on close\n"));
5017 return rpc_file_usage(c, argc, argv);
5020 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
5021 rpc_file_close_internals,
5026 * Formatted print of open file info
5028 * @param r struct srvsvc_NetFileInfo3 contents
5031 static void display_file_info_3(struct srvsvc_NetFileInfo3 *r)
5033 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5034 r->fid, r->user, r->permissions, r->num_locks, r->path);
5038 * List open files on a remote RPC server
5040 * All parameters are provided by the run_rpc_command function, except for
5041 * argc, argv which are passes through.
5043 * @param c A net_context structure
5044 * @param domain_sid The domain sid acquired from the remote server
5045 * @param cli A cli_state connected to the server.
5046 * @param mem_ctx Talloc context, destoyed on completion of the function.
5047 * @param argc Standard main() style argc
5048 * @param argv Standard main() style argv. Initial components are already
5051 * @return Normal NTSTATUS return.
5054 static NTSTATUS rpc_file_list_internals(struct net_context *c,
5055 const DOM_SID *domain_sid,
5056 const char *domain_name,
5057 struct cli_state *cli,
5058 struct rpc_pipe_client *pipe_hnd,
5059 TALLOC_CTX *mem_ctx,
5063 struct srvsvc_NetFileInfoCtr info_ctr;
5064 struct srvsvc_NetFileCtr3 ctr3;
5067 uint32 preferred_len = 0xffffffff, i;
5068 const char *username=NULL;
5069 uint32_t total_entries = 0;
5070 uint32_t resume_handle = 0;
5072 /* if argc > 0, must be user command */
5074 username = smb_xstrdup(argv[0]);
5076 ZERO_STRUCT(info_ctr);
5080 info_ctr.ctr.ctr3 = &ctr3;
5082 status = rpccli_srvsvc_NetFileEnum(pipe_hnd, mem_ctx,
5092 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
5095 /* Display results */
5098 "\nEnumerating open files on remote server:\n\n"\
5099 "\nFileId Opened by Perms Locks Path"\
5100 "\n------ --------- ----- ----- ---- \n");
5101 for (i = 0; i < total_entries; i++)
5102 display_file_info_3(&info_ctr.ctr.ctr3->array[i]);
5104 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
5108 * List files for a user on a remote RPC server
5110 * @param argc Standard main() style argc
5111 * @param argv Standard main() style argv. Initial components are already
5114 * @return A shell status integer (0 for success)
5117 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5120 DEBUG(1, ("No username given\n"));
5121 return rpc_file_usage(c, argc, argv);
5124 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
5125 rpc_file_list_internals,
5130 * 'net rpc file' entrypoint.
5131 * @param argc Standard main() style argc
5132 * @param argv Standard main() style argv. Initial components are already
5136 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5138 struct functable func[] = {
5139 {"close", rpc_file_close},
5140 {"user", rpc_file_user},
5142 {"info", rpc_file_info},
5148 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
5149 rpc_file_list_internals,
5152 return net_run_function(c, argc, argv, func, rpc_file_usage);
5156 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
5158 * All parameters are provided by the run_rpc_command function, except for
5159 * argc, argv which are passed through.
5161 * @param c A net_context structure
5162 * @param domain_sid The domain sid aquired from the remote server
5163 * @param cli A cli_state connected to the server.
5164 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5165 * @param argc Standard main() style argc
5166 * @param argv Standard main() style argv. Initial components are already
5169 * @return Normal NTSTATUS return.
5172 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5173 const DOM_SID *domain_sid,
5174 const char *domain_name,
5175 struct cli_state *cli,
5176 struct rpc_pipe_client *pipe_hnd,
5177 TALLOC_CTX *mem_ctx,
5181 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5183 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5185 if (NT_STATUS_IS_OK(result)) {
5186 d_printf("\nShutdown successfully aborted\n");
5187 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5189 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5195 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5197 * All parameters are provided by the run_rpc_command function, except for
5198 * argc, argv which are passed through.
5200 * @param c A net_context structure
5201 * @param domain_sid The domain sid aquired from the remote server
5202 * @param cli A cli_state connected to the server.
5203 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5204 * @param argc Standard main() style argc
5205 * @param argv Standard main() style argv. Initial components are already
5208 * @return Normal NTSTATUS return.
5211 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5212 const DOM_SID *domain_sid,
5213 const char *domain_name,
5214 struct cli_state *cli,
5215 struct rpc_pipe_client *pipe_hnd,
5216 TALLOC_CTX *mem_ctx,
5220 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5222 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5224 if (NT_STATUS_IS_OK(result)) {
5225 d_printf("\nShutdown successfully aborted\n");
5226 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5228 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5234 * ABORT the Shut down of a remote RPC server
5236 * @param argc Standard main() style argc
5237 * @param argv Standard main() style argv. Initial components are already
5240 * @return A shell status integer (0 for success)
5243 static int rpc_shutdown_abort(struct net_context *c, int argc,
5246 int rc = run_rpc_command(c, NULL, PI_INITSHUTDOWN, 0,
5247 rpc_shutdown_abort_internals,
5253 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5255 return run_rpc_command(c, NULL, PI_WINREG, 0,
5256 rpc_reg_shutdown_abort_internals,
5261 * Shut down a remote RPC Server via initshutdown pipe
5263 * All parameters are provided by the run_rpc_command function, except for
5264 * argc, argv which are passes through.
5266 * @param c A net_context structure
5267 * @param domain_sid The domain sid aquired from the remote server
5268 * @param cli A cli_state connected to the server.
5269 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5270 * @param argc Standard main() style argc
5271 * @param argc Standard main() style argv. Initial components are already
5274 * @return Normal NTSTATUS return.
5277 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5278 const DOM_SID *domain_sid,
5279 const char *domain_name,
5280 struct cli_state *cli,
5281 struct rpc_pipe_client *pipe_hnd,
5282 TALLOC_CTX *mem_ctx,
5286 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5287 const char *msg = "This machine will be shutdown shortly";
5288 uint32 timeout = 20;
5289 struct initshutdown_String msg_string;
5290 struct initshutdown_String_sub s;
5292 if (c->opt_comment) {
5293 msg = c->opt_comment;
5295 if (c->opt_timeout) {
5296 timeout = c->opt_timeout;
5300 msg_string.name = &s;
5302 /* create an entry */
5303 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5304 &msg_string, timeout, c->opt_force, c->opt_reboot,
5307 if (NT_STATUS_IS_OK(result)) {
5308 d_printf("\nShutdown of remote machine succeeded\n");
5309 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5311 DEBUG(1,("Shutdown of remote machine failed!\n"));
5317 * Shut down a remote RPC Server via winreg pipe
5319 * All parameters are provided by the run_rpc_command function, except for
5320 * argc, argv which are passes through.
5322 * @param c A net_context structure
5323 * @param domain_sid The domain sid aquired from the remote server
5324 * @param cli A cli_state connected to the server.
5325 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5326 * @param argc Standard main() style argc
5327 * @param argc Standard main() style argv. Initial components are already
5330 * @return Normal NTSTATUS return.
5333 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5334 const DOM_SID *domain_sid,
5335 const char *domain_name,
5336 struct cli_state *cli,
5337 struct rpc_pipe_client *pipe_hnd,
5338 TALLOC_CTX *mem_ctx,
5342 const char *msg = "This machine will be shutdown shortly";
5343 uint32 timeout = 20;
5344 struct initshutdown_String msg_string;
5345 struct initshutdown_String_sub s;
5349 if (c->opt_comment) {
5350 msg = c->opt_comment;
5353 msg_string.name = &s;
5355 if (c->opt_timeout) {
5356 timeout = c->opt_timeout;
5359 /* create an entry */
5360 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5361 &msg_string, timeout, c->opt_force, c->opt_reboot,
5364 if (NT_STATUS_IS_OK(result)) {
5365 d_printf("\nShutdown of remote machine succeeded\n");
5367 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5368 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5369 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5371 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(werr));
5378 * Shut down a remote RPC server
5380 * @param argc Standard main() style argc
5381 * @param argc Standard main() style argv. Initial components are already
5384 * @return A shell status integer (0 for success)
5387 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5389 int rc = run_rpc_command(c, NULL, PI_INITSHUTDOWN, 0,
5390 rpc_init_shutdown_internals,
5394 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5395 rc = run_rpc_command(c, NULL, PI_WINREG, 0,
5396 rpc_reg_shutdown_internals, argc, argv);
5402 /***************************************************************************
5403 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5404 ***************************************************************************/
5407 * Add interdomain trust account to the RPC server.
5408 * All parameters (except for argc and argv) are passed by run_rpc_command
5411 * @param c A net_context structure
5412 * @param domain_sid The domain sid acquired from the server
5413 * @param cli A cli_state connected to the server.
5414 * @param mem_ctx Talloc context, destoyed on completion of the function.
5415 * @param argc Standard main() style argc
5416 * @param argc Standard main() style argv. Initial components are already
5419 * @return normal NTSTATUS return code
5422 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5423 const DOM_SID *domain_sid,
5424 const char *domain_name,
5425 struct cli_state *cli,
5426 struct rpc_pipe_client *pipe_hnd,
5427 TALLOC_CTX *mem_ctx,
5431 POLICY_HND connect_pol, domain_pol, user_pol;
5432 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5434 struct lsa_String lsa_acct_name;
5436 uint32 acct_flags=0;
5438 uint32_t access_granted = 0;
5439 union samr_UserInfo info;
5442 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5443 return NT_STATUS_INVALID_PARAMETER;
5447 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5450 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5451 return NT_STATUS_NO_MEMORY;
5454 strupper_m(acct_name);
5456 init_lsa_String(&lsa_acct_name, acct_name);
5458 /* Get samr policy handle */
5459 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5461 MAXIMUM_ALLOWED_ACCESS,
5463 if (!NT_STATUS_IS_OK(result)) {
5467 /* Get domain policy handle */
5468 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5470 MAXIMUM_ALLOWED_ACCESS,
5471 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5473 if (!NT_STATUS_IS_OK(result)) {
5477 /* Create trusting domain's account */
5478 acb_info = ACB_NORMAL;
5479 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5480 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5481 SAMR_USER_ACCESS_SET_PASSWORD |
5482 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5483 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5485 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5493 if (!NT_STATUS_IS_OK(result)) {
5499 struct samr_LogonHours hours;
5500 struct lsa_BinaryString parameters;
5501 const int units_per_week = 168;
5504 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5506 ZERO_STRUCT(notime);
5508 ZERO_STRUCT(parameters);
5510 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
5512 result = NT_STATUS_NO_MEMORY;
5515 hours.units_per_week = units_per_week;
5516 memset(hours.bits, 0xFF, units_per_week);
5518 init_samr_user_info23(&info.info23,
5519 notime, notime, notime,
5520 notime, notime, notime,
5521 NULL, NULL, NULL, NULL, NULL,
5522 NULL, NULL, NULL, NULL, ¶meters,
5523 0, 0, ACB_DOMTRUST, SAMR_FIELD_ACCT_FLAGS,
5525 0, 0, 0, 0, 0, 0, 0,
5528 SamOEMhashBlob(info.info23.password.data, 516,
5529 &cli->user_session_key);
5531 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5536 if (!NT_STATUS_IS_OK(result)) {
5537 DEBUG(0,("Could not set trust account password: %s\n",
5538 nt_errstr(result)));
5544 SAFE_FREE(acct_name);
5549 * Create interdomain trust account for a remote domain.
5551 * @param argc standard argc
5552 * @param argv standard argv without initial components
5554 * @return Integer status (0 means success)
5557 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5560 return run_rpc_command(c, NULL, PI_SAMR, 0,
5561 rpc_trustdom_add_internals, argc, argv);
5563 d_printf("Usage: net rpc trustdom add <domain>\n");
5570 * Remove interdomain trust account from the RPC server.
5571 * All parameters (except for argc and argv) are passed by run_rpc_command
5574 * @param c A net_context structure
5575 * @param domain_sid The domain sid acquired from the server
5576 * @param cli A cli_state connected to the server.
5577 * @param mem_ctx Talloc context, destoyed on completion of the function.
5578 * @param argc Standard main() style argc
5579 * @param argc Standard main() style argv. Initial components are already
5582 * @return normal NTSTATUS return code
5585 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5586 const DOM_SID *domain_sid,
5587 const char *domain_name,
5588 struct cli_state *cli,
5589 struct rpc_pipe_client *pipe_hnd,
5590 TALLOC_CTX *mem_ctx,
5594 POLICY_HND connect_pol, domain_pol, user_pol;
5595 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5597 DOM_SID trust_acct_sid;
5598 struct samr_Ids user_rids, name_types;
5599 struct lsa_String lsa_acct_name;
5602 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5603 return NT_STATUS_INVALID_PARAMETER;
5607 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5609 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5611 if (acct_name == NULL)
5612 return NT_STATUS_NO_MEMORY;
5614 strupper_m(acct_name);
5616 /* Get samr policy handle */
5617 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5619 MAXIMUM_ALLOWED_ACCESS,
5621 if (!NT_STATUS_IS_OK(result)) {
5625 /* Get domain policy handle */
5626 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5628 MAXIMUM_ALLOWED_ACCESS,
5629 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5631 if (!NT_STATUS_IS_OK(result)) {
5635 init_lsa_String(&lsa_acct_name, acct_name);
5637 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5644 if (!NT_STATUS_IS_OK(result)) {
5648 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5650 MAXIMUM_ALLOWED_ACCESS,
5654 if (!NT_STATUS_IS_OK(result)) {
5658 /* append the rid to the domain sid */
5659 sid_copy(&trust_acct_sid, domain_sid);
5660 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5664 /* remove the sid */
5666 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5669 if (!NT_STATUS_IS_OK(result)) {
5675 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5678 if (!NT_STATUS_IS_OK(result)) {
5682 if (!NT_STATUS_IS_OK(result)) {
5683 DEBUG(0,("Could not set trust account password: %s\n",
5684 nt_errstr(result)));
5693 * Delete interdomain trust account for a remote domain.
5695 * @param argc standard argc
5696 * @param argv standard argv without initial components
5698 * @return Integer status (0 means success)
5701 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5704 return run_rpc_command(c, NULL, PI_SAMR, 0,
5705 rpc_trustdom_del_internals, argc, argv);
5707 d_printf("Usage: net rpc trustdom del <domain>\n");
5712 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5713 struct cli_state *cli,
5714 TALLOC_CTX *mem_ctx,
5715 const char *domain_name)
5717 char *dc_name = NULL;
5718 const char *buffer = NULL;
5719 struct rpc_pipe_client *netr;
5722 /* Use NetServerEnum2 */
5724 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5726 return NT_STATUS_OK;
5729 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5730 for domain %s\n", domain_name));
5732 /* Try netr_GetDcName */
5734 netr = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &status);
5739 status = rpccli_netr_GetDcName(netr, mem_ctx,
5746 if (NT_STATUS_IS_OK(status)) {
5750 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5751 for domain %s\n", domain_name));
5757 * Establish trust relationship to a trusting domain.
5758 * Interdomain account must already be created on remote PDC.
5760 * @param c A net_context structure
5761 * @param argc standard argc
5762 * @param argv standard argv without initial components
5764 * @return Integer status (0 means success)
5767 static int rpc_trustdom_establish(struct net_context *c, int argc,
5770 struct cli_state *cli = NULL;
5771 struct sockaddr_storage server_ss;
5772 struct rpc_pipe_client *pipe_hnd = NULL;
5773 POLICY_HND connect_hnd;
5774 TALLOC_CTX *mem_ctx;
5776 DOM_SID *domain_sid;
5781 union lsa_PolicyInformation *info = NULL;
5784 * Connect to \\server\ipc$ as 'our domain' account with password
5788 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5792 domain_name = smb_xstrdup(argv[0]);
5793 strupper_m(domain_name);
5795 /* account name used at first is our domain's name with '$' */
5796 asprintf(&acct_name, "%s$", lp_workgroup());
5797 strupper_m(acct_name);
5800 * opt_workgroup will be used by connection functions further,
5801 * hence it should be set to remote domain name instead of ours
5803 if (c->opt_workgroup) {
5804 c->opt_workgroup = smb_xstrdup(domain_name);
5807 c->opt_user_name = acct_name;
5809 /* find the domain controller */
5810 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5811 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5815 /* connect to ipc$ as username/password */
5816 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5817 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5819 /* Is it trusting domain account for sure ? */
5820 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5821 nt_errstr(nt_status)));
5825 /* store who we connected to */
5827 saf_store( domain_name, pdc_name );
5830 * Connect to \\server\ipc$ again (this time anonymously)
5833 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5836 if (NT_STATUS_IS_ERR(nt_status)) {
5837 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5838 domain_name, nt_errstr(nt_status)));
5842 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5843 "domain %s", domain_name))) {
5844 DEBUG(0, ("talloc_init() failed\n"));
5849 /* Make sure we're talking to a proper server */
5851 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5852 if (!NT_STATUS_IS_OK(nt_status)) {
5854 talloc_destroy(mem_ctx);
5859 * Call LsaOpenPolicy and LsaQueryInfo
5862 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5864 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5866 talloc_destroy(mem_ctx);
5870 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, SEC_RIGHTS_QUERY_VALUE,
5872 if (NT_STATUS_IS_ERR(nt_status)) {
5873 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5874 nt_errstr(nt_status)));
5876 talloc_destroy(mem_ctx);
5880 /* Querying info level 5 */
5882 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5884 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5886 if (NT_STATUS_IS_ERR(nt_status)) {
5887 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5888 nt_errstr(nt_status)));
5890 talloc_destroy(mem_ctx);
5894 domain_sid = info->account_domain.sid;
5896 /* There should be actually query info level 3 (following nt serv behaviour),
5897 but I still don't know if it's _really_ necessary */
5900 * Store the password in secrets db
5903 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5904 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5906 talloc_destroy(mem_ctx);
5911 * Close the pipes and clean up
5914 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5915 if (NT_STATUS_IS_ERR(nt_status)) {
5916 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5917 nt_errstr(nt_status)));
5919 talloc_destroy(mem_ctx);
5925 talloc_destroy(mem_ctx);
5927 d_printf("Trust to domain %s established\n", domain_name);
5932 * Revoke trust relationship to the remote domain
5934 * @param c A net_context structure
5935 * @param argc standard argc
5936 * @param argv standard argv without initial components
5938 * @return Integer status (0 means success)
5941 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5947 if (argc < 1) return -1;
5949 /* generate upper cased domain name */
5950 domain_name = smb_xstrdup(argv[0]);
5951 strupper_m(domain_name);
5953 /* delete password of the trust */
5954 if (!pdb_del_trusteddom_pw(domain_name)) {
5955 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5962 SAFE_FREE(domain_name);
5967 * Usage for 'net rpc trustdom' command
5969 * @param argc standard argc
5970 * @param argv standard argv without inital components
5972 * @return Integer status returned to shell
5975 static int rpc_trustdom_usage(struct net_context *c, int argc,
5978 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
5979 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
5980 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
5981 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5982 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
5983 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5988 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5989 const DOM_SID *domain_sid,
5990 const char *domain_name,
5991 struct cli_state *cli,
5992 struct rpc_pipe_client *pipe_hnd,
5993 TALLOC_CTX *mem_ctx,
5998 sid_to_fstring(str_sid, domain_sid);
5999 d_printf("%s\n", str_sid);
6000 return NT_STATUS_OK;
6003 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
6005 fstring ascii_sid, padding;
6006 int pad_len, col_len = 20;
6008 /* convert sid into ascii string */
6009 sid_to_fstring(ascii_sid, dom_sid);
6011 /* calculate padding space for d_printf to look nicer */
6012 pad_len = col_len - strlen(trusted_dom_name);
6013 padding[pad_len] = 0;
6014 do padding[--pad_len] = ' '; while (pad_len);
6016 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
6019 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6020 TALLOC_CTX *mem_ctx,
6023 const char *trusted_dom_name)
6026 union lsa_TrustedDomainInfo *info = NULL;
6027 char *cleartextpwd = NULL;
6028 uint8_t nt_hash[16];
6031 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
6034 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6036 if (NT_STATUS_IS_ERR(nt_status)) {
6037 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6038 nt_errstr(nt_status)));
6042 data = data_blob(info->password.password->data,
6043 info->password.password->length);
6045 if (!rpccli_get_pwd_hash(pipe_hnd, nt_hash)) {
6046 DEBUG(0, ("Could not retrieve password hash\n"));
6050 cleartextpwd = decrypt_trustdom_secret(nt_hash, &data);
6052 if (cleartextpwd == NULL) {
6053 DEBUG(0,("retrieved NULL password\n"));
6054 nt_status = NT_STATUS_UNSUCCESSFUL;
6058 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6059 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6060 nt_status = NT_STATUS_UNSUCCESSFUL;
6064 #ifdef DEBUG_PASSWORD
6065 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6066 "password: [%s]\n", trusted_dom_name,
6067 sid_string_dbg(&dom_sid), cleartextpwd));
6071 SAFE_FREE(cleartextpwd);
6072 data_blob_free(&data);
6077 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6080 /* common variables */
6081 TALLOC_CTX* mem_ctx;
6082 struct cli_state *cli = NULL;
6083 struct rpc_pipe_client *pipe_hnd = NULL;
6085 const char *domain_name = NULL;
6086 DOM_SID *queried_dom_sid;
6087 POLICY_HND connect_hnd;
6088 union lsa_PolicyInformation *info = NULL;
6090 /* trusted domains listing variables */
6091 unsigned int enum_ctx = 0;
6093 struct lsa_DomainList dom_list;
6097 * Listing trusted domains (stored in secrets.tdb, if local)
6100 mem_ctx = talloc_init("trust relationships vampire");
6103 * set domain and pdc name to local samba server (default)
6104 * or to remote one given in command line
6107 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6108 domain_name = c->opt_workgroup;
6109 c->opt_target_workgroup = c->opt_workgroup;
6111 fstrcpy(pdc_name, global_myname());
6112 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6113 c->opt_target_workgroup = domain_name;
6116 /* open \PIPE\lsarpc and open policy handle */
6117 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6118 if (!NT_STATUS_IS_OK(nt_status)) {
6119 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6120 nt_errstr(nt_status)));
6121 talloc_destroy(mem_ctx);
6125 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6127 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6128 nt_errstr(nt_status) ));
6130 talloc_destroy(mem_ctx);
6134 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6136 if (NT_STATUS_IS_ERR(nt_status)) {
6137 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6138 nt_errstr(nt_status)));
6140 talloc_destroy(mem_ctx);
6144 /* query info level 5 to obtain sid of a domain being queried */
6145 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6147 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6150 if (NT_STATUS_IS_ERR(nt_status)) {
6151 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6152 nt_errstr(nt_status)));
6154 talloc_destroy(mem_ctx);
6158 queried_dom_sid = info->account_domain.sid;
6161 * Keep calling LsaEnumTrustdom over opened pipe until
6162 * the end of enumeration is reached
6165 d_printf("Vampire trusted domains:\n\n");
6168 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6173 if (NT_STATUS_IS_ERR(nt_status)) {
6174 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6175 nt_errstr(nt_status)));
6177 talloc_destroy(mem_ctx);
6181 for (i = 0; i < dom_list.count; i++) {
6183 print_trusted_domain(dom_list.domains[i].sid,
6184 dom_list.domains[i].name.string);
6186 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6187 *dom_list.domains[i].sid,
6188 dom_list.domains[i].name.string);
6189 if (!NT_STATUS_IS_OK(nt_status)) {
6191 talloc_destroy(mem_ctx);
6197 * in case of no trusted domains say something rather
6198 * than just display blank line
6200 if (!dom_list.count) d_printf("none\n");
6202 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6204 /* close this connection before doing next one */
6205 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6206 if (NT_STATUS_IS_ERR(nt_status)) {
6207 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6208 nt_errstr(nt_status)));
6210 talloc_destroy(mem_ctx);
6214 /* close lsarpc pipe and connection to IPC$ */
6217 talloc_destroy(mem_ctx);
6221 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6223 /* common variables */
6224 TALLOC_CTX* mem_ctx;
6225 struct cli_state *cli = NULL, *remote_cli = NULL;
6226 struct rpc_pipe_client *pipe_hnd = NULL;
6228 const char *domain_name = NULL;
6229 DOM_SID *queried_dom_sid;
6231 int ascii_dom_name_len;
6232 POLICY_HND connect_hnd;
6233 union lsa_PolicyInformation *info = NULL;
6235 /* trusted domains listing variables */
6236 unsigned int num_domains, enum_ctx = 0;
6237 int i, pad_len, col_len = 20;
6238 struct lsa_DomainList dom_list;
6241 /* trusting domains listing variables */
6242 POLICY_HND domain_hnd;
6243 struct samr_SamArray *trusts = NULL;
6246 * Listing trusted domains (stored in secrets.tdb, if local)
6249 mem_ctx = talloc_init("trust relationships listing");
6252 * set domain and pdc name to local samba server (default)
6253 * or to remote one given in command line
6256 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6257 domain_name = c->opt_workgroup;
6258 c->opt_target_workgroup = c->opt_workgroup;
6260 fstrcpy(pdc_name, global_myname());
6261 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6262 c->opt_target_workgroup = domain_name;
6265 /* open \PIPE\lsarpc and open policy handle */
6266 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6267 if (!NT_STATUS_IS_OK(nt_status)) {
6268 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6269 nt_errstr(nt_status)));
6270 talloc_destroy(mem_ctx);
6274 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6276 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6277 nt_errstr(nt_status) ));
6279 talloc_destroy(mem_ctx);
6283 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6285 if (NT_STATUS_IS_ERR(nt_status)) {
6286 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6287 nt_errstr(nt_status)));
6289 talloc_destroy(mem_ctx);
6293 /* query info level 5 to obtain sid of a domain being queried */
6294 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6296 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6299 if (NT_STATUS_IS_ERR(nt_status)) {
6300 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6301 nt_errstr(nt_status)));
6303 talloc_destroy(mem_ctx);
6307 queried_dom_sid = info->account_domain.sid;
6310 * Keep calling LsaEnumTrustdom over opened pipe until
6311 * the end of enumeration is reached
6314 d_printf("Trusted domains list:\n\n");
6317 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6322 if (NT_STATUS_IS_ERR(nt_status)) {
6323 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6324 nt_errstr(nt_status)));
6326 talloc_destroy(mem_ctx);
6330 for (i = 0; i < dom_list.count; i++) {
6331 print_trusted_domain(dom_list.domains[i].sid,
6332 dom_list.domains[i].name.string);
6336 * in case of no trusted domains say something rather
6337 * than just display blank line
6339 if (!dom_list.count) d_printf("none\n");
6341 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6343 /* close this connection before doing next one */
6344 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6345 if (NT_STATUS_IS_ERR(nt_status)) {
6346 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6347 nt_errstr(nt_status)));
6349 talloc_destroy(mem_ctx);
6353 TALLOC_FREE(pipe_hnd);
6356 * Listing trusting domains (stored in passdb backend, if local)
6359 d_printf("\nTrusting domains list:\n\n");
6362 * Open \PIPE\samr and get needed policy handles
6364 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6366 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6368 talloc_destroy(mem_ctx);
6373 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6375 SA_RIGHT_SAM_OPEN_DOMAIN,
6377 if (!NT_STATUS_IS_OK(nt_status)) {
6378 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6379 nt_errstr(nt_status)));
6381 talloc_destroy(mem_ctx);
6385 /* SamrOpenDomain - we have to open domain policy handle in order to be
6386 able to enumerate accounts*/
6387 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6389 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6392 if (!NT_STATUS_IS_OK(nt_status)) {
6393 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6394 nt_errstr(nt_status)));
6396 talloc_destroy(mem_ctx);
6401 * perform actual enumeration
6404 enum_ctx = 0; /* reset enumeration context from last enumeration */
6407 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6414 if (NT_STATUS_IS_ERR(nt_status)) {
6415 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6416 nt_errstr(nt_status)));
6418 talloc_destroy(mem_ctx);
6422 for (i = 0; i < num_domains; i++) {
6424 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6427 * get each single domain's sid (do we _really_ need this ?):
6428 * 1) connect to domain's pdc
6429 * 2) query the pdc for domain's sid
6432 /* get rid of '$' tail */
6433 ascii_dom_name_len = strlen(str);
6434 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6435 str[ascii_dom_name_len - 1] = '\0';
6437 /* calculate padding space for d_printf to look nicer */
6438 pad_len = col_len - strlen(str);
6439 padding[pad_len] = 0;
6440 do padding[--pad_len] = ' '; while (pad_len);
6442 /* set opt_* variables to remote domain */
6444 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6445 c->opt_target_workgroup = c->opt_workgroup;
6447 d_printf("%s%s", str, padding);
6449 /* connect to remote domain controller */
6450 nt_status = net_make_ipc_connection(c,
6451 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6453 if (NT_STATUS_IS_OK(nt_status)) {
6454 /* query for domain's sid */
6455 if (run_rpc_command(c, remote_cli, PI_LSARPC, 0,
6456 rpc_query_domain_sid, argc,
6458 d_fprintf(stderr, "couldn't get domain's sid\n");
6460 cli_shutdown(remote_cli);
6463 d_fprintf(stderr, "domain controller is not "
6465 nt_errstr(nt_status));
6469 if (!num_domains) d_printf("none\n");
6471 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6473 /* close opened samr and domain policy handles */
6474 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6475 if (!NT_STATUS_IS_OK(nt_status)) {
6476 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6479 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6480 if (!NT_STATUS_IS_OK(nt_status)) {
6481 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6484 /* close samr pipe and connection to IPC$ */
6487 talloc_destroy(mem_ctx);
6492 * Entrypoint for 'net rpc trustdom' code
6494 * @param argc standard argc
6495 * @param argv standard argv without initial components
6497 * @return Integer status (0 means success)
6500 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6502 struct functable func[] = {
6503 {"add", rpc_trustdom_add},
6504 {"del", rpc_trustdom_del},
6505 {"establish", rpc_trustdom_establish},
6506 {"revoke", rpc_trustdom_revoke},
6507 {"help", rpc_trustdom_usage},
6508 {"list", rpc_trustdom_list},
6509 {"vampire", rpc_trustdom_vampire},
6514 rpc_trustdom_usage(c, argc, argv);
6518 return net_run_function(c, argc, argv, func, rpc_user_usage);
6522 * Check if a server will take rpc commands
6523 * @param flags Type of server to connect to (PDC, DMB, localhost)
6524 * if the host is not explicitly specified
6525 * @return bool (true means rpc supported)
6527 bool net_rpc_check(struct net_context *c, unsigned flags)
6529 struct cli_state *cli;
6531 struct sockaddr_storage server_ss;
6532 char *server_name = NULL;
6535 /* flags (i.e. server type) may depend on command */
6536 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6539 if ((cli = cli_initialise()) == NULL) {
6543 status = cli_connect(cli, server_name, &server_ss);
6544 if (!NT_STATUS_IS_OK(status))
6546 if (!attempt_netbios_session_request(&cli, global_myname(),
6547 server_name, &server_ss))
6549 if (!cli_negprot(cli))
6551 if (cli->protocol < PROTOCOL_NT1)
6560 /* dump sam database via samsync rpc calls */
6561 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6562 return run_rpc_command(c, NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS,
6563 rpc_samdump_internals, argc, argv);
6566 /* syncronise sam database via samsync rpc calls */
6567 static int rpc_vampire(struct net_context *c, int argc, const char **argv) {
6568 return run_rpc_command(c, NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS,
6569 rpc_vampire_internals, argc, argv);
6573 * Migrate everything from a print-server
6575 * @param c A net_context structure
6576 * @param argc Standard main() style argc
6577 * @param argv Standard main() style argv. Initial components are already
6580 * @return A shell status integer (0 for success)
6582 * The order is important !
6583 * To successfully add drivers the print-queues have to exist !
6584 * Applying ACLs should be the last step, because you're easily locked out
6587 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6593 printf("no server to migrate\n");
6597 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6598 rpc_printer_migrate_printers_internals, argc,
6603 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6604 rpc_printer_migrate_drivers_internals, argc,
6609 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6610 rpc_printer_migrate_forms_internals, argc, argv);
6614 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6615 rpc_printer_migrate_settings_internals, argc,
6620 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6621 rpc_printer_migrate_security_internals, argc,
6627 * Migrate print-drivers from a print-server
6629 * @param c A net_context structure
6630 * @param argc Standard main() style argc
6631 * @param argv Standard main() style argv. Initial components are already
6634 * @return A shell status integer (0 for success)
6636 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6640 printf("no server to migrate\n");
6644 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6645 rpc_printer_migrate_drivers_internals,
6650 * Migrate print-forms from a print-server
6652 * @param c A net_context structure
6653 * @param argc Standard main() style argc
6654 * @param argv Standard main() style argv. Initial components are already
6657 * @return A shell status integer (0 for success)
6659 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6663 printf("no server to migrate\n");
6667 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6668 rpc_printer_migrate_forms_internals,
6673 * Migrate printers from a print-server
6675 * @param c A net_context structure
6676 * @param argc Standard main() style argc
6677 * @param argv Standard main() style argv. Initial components are already
6680 * @return A shell status integer (0 for success)
6682 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6686 printf("no server to migrate\n");
6690 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6691 rpc_printer_migrate_printers_internals,
6696 * Migrate printer-ACLs from a print-server
6698 * @param c A net_context structure
6699 * @param argc Standard main() style argc
6700 * @param argv Standard main() style argv. Initial components are already
6703 * @return A shell status integer (0 for success)
6705 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6709 printf("no server to migrate\n");
6713 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6714 rpc_printer_migrate_security_internals,
6719 * Migrate printer-settings from a print-server
6721 * @param c A net_context structure
6722 * @param argc Standard main() style argc
6723 * @param argv Standard main() style argv. Initial components are already
6726 * @return A shell status integer (0 for success)
6728 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6732 printf("no server to migrate\n");
6736 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6737 rpc_printer_migrate_settings_internals,
6742 * 'net rpc printer' entrypoint.
6744 * @param c A net_context structure
6745 * @param argc Standard main() style argc
6746 * @param argv Standard main() style argv. Initial components are already
6750 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6753 /* ouch: when addriver and setdriver are called from within
6754 rpc_printer_migrate_drivers_internals, the printer-queue already
6757 struct functable func[] = {
6758 {"all", rpc_printer_migrate_all},
6759 {"drivers", rpc_printer_migrate_drivers},
6760 {"forms", rpc_printer_migrate_forms},
6761 {"help", rpc_printer_usage},
6762 {"printers", rpc_printer_migrate_printers},
6763 {"security", rpc_printer_migrate_security},
6764 {"settings", rpc_printer_migrate_settings},
6768 return net_run_function(c, argc, argv, func, rpc_printer_usage);
6773 * List printers on a remote RPC server
6775 * @param c A net_context structure
6776 * @param argc Standard main() style argc
6777 * @param argv Standard main() style argv. Initial components are already
6780 * @return A shell status integer (0 for success)
6782 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6785 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6786 rpc_printer_list_internals,
6791 * List printer-drivers on a remote RPC server
6793 * @param c A net_context structure
6794 * @param argc Standard main() style argc
6795 * @param argv Standard main() style argv. Initial components are already
6798 * @return A shell status integer (0 for success)
6800 static int rpc_printer_driver_list(struct net_context *c, int argc,
6804 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6805 rpc_printer_driver_list_internals,
6810 * Publish printer in ADS via MSRPC
6812 * @param c A net_context structure
6813 * @param argc Standard main() style argc
6814 * @param argv Standard main() style argv. Initial components are already
6817 * @return A shell status integer (0 for success)
6819 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6823 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6824 rpc_printer_publish_publish_internals,
6829 * Update printer in ADS via MSRPC
6831 * @param c A net_context structure
6832 * @param argc Standard main() style argc
6833 * @param argv Standard main() style argv. Initial components are already
6836 * @return A shell status integer (0 for success)
6838 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6841 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6842 rpc_printer_publish_update_internals,
6847 * UnPublish printer in ADS via MSRPC
6849 * @param c A net_context structure
6850 * @param argc Standard main() style argc
6851 * @param argv Standard main() style argv. Initial components are already
6854 * @return A shell status integer (0 for success)
6856 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
6860 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6861 rpc_printer_publish_unpublish_internals,
6866 * List published printers via MSRPC
6868 * @param c A net_context structure
6869 * @param argc Standard main() style argc
6870 * @param argv Standard main() style argv. Initial components are already
6873 * @return A shell status integer (0 for success)
6875 static int rpc_printer_publish_list(struct net_context *c, int argc,
6879 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6880 rpc_printer_publish_list_internals,
6886 * Publish printer in ADS
6888 * @param c A net_context structure
6889 * @param argc Standard main() style argc
6890 * @param argv Standard main() style argv. Initial components are already
6893 * @return A shell status integer (0 for success)
6895 static int rpc_printer_publish(struct net_context *c, int argc,
6899 struct functable func[] = {
6900 {"publish", rpc_printer_publish_publish},
6901 {"update", rpc_printer_publish_update},
6902 {"unpublish", rpc_printer_publish_unpublish},
6903 {"list", rpc_printer_publish_list},
6904 {"help", rpc_printer_usage},
6909 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6910 rpc_printer_publish_list_internals,
6913 return net_run_function(c, argc, argv, func, rpc_printer_usage);
6919 * Display rpc printer help page.
6921 * @param c A net_context structure
6922 * @param argc Standard main() style argc
6923 * @param argv Standard main() style argv. Initial components are already
6926 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
6928 return net_help_printer(c, argc, argv);
6932 * 'net rpc printer' entrypoint.
6934 * @param c A net_context structure
6935 * @param argc Standard main() style argc
6936 * @param argv Standard main() style argv. Initial components are already
6939 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
6941 struct functable func[] = {
6942 {"list", rpc_printer_list},
6943 {"migrate", rpc_printer_migrate},
6944 {"driver", rpc_printer_driver_list},
6945 {"publish", rpc_printer_publish},
6950 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6951 rpc_printer_list_internals,
6954 return net_run_function(c, argc, argv, func, rpc_printer_usage);
6957 /****************************************************************************/
6961 * Basic usage function for 'net rpc'
6963 * @param c A net_context structure
6964 * @param argc Standard main() style argc
6965 * @param argv Standard main() style argv. Initial components are already
6969 int net_rpc_usage(struct net_context *c, int argc, const char **argv)
6971 d_printf(" net rpc info \t\t\tshow basic info about a domain \n");
6972 d_printf(" net rpc join \t\t\tto join a domain \n");
6973 d_printf(" net rpc oldjoin \t\tto join a domain created in server manager\n");
6974 d_printf(" net rpc testjoin \t\ttests that a join is valid\n");
6975 d_printf(" net rpc user \t\t\tto add, delete and list users\n");
6976 d_printf(" net rpc password <username> [<password>] -Uadmin_username%%admin_pass\n");
6977 d_printf(" net rpc group \t\tto list groups\n");
6978 d_printf(" net rpc share \t\tto add, delete, list and migrate shares\n");
6979 d_printf(" net rpc printer \t\tto list and migrate printers\n");
6980 d_printf(" net rpc file \t\t\tto list open files\n");
6981 d_printf(" net rpc changetrustpw \tto change the trust account password\n");
6982 d_printf(" net rpc getsid \t\tfetch the domain sid into the local secrets.tdb\n");
6983 d_printf(" net rpc vampire \t\tsyncronise an NT PDC's users and groups into the local passdb\n");
6984 d_printf(" net rpc samdump \t\tdisplay an NT PDC's users, groups and other data\n");
6985 d_printf(" net rpc trustdom \t\tto create trusting domain's account or establish trust\n");
6986 d_printf(" net rpc abortshutdown \tto abort the shutdown of a remote server\n");
6987 d_printf(" net rpc shutdown \t\tto shutdown a remote server\n");
6988 d_printf(" net rpc rights\t\tto manage privileges assigned to SIDs\n");
6989 d_printf(" net rpc registry\t\tto manage registry hives\n");
6990 d_printf(" net rpc service\t\tto start, stop and query services\n");
6991 d_printf(" net rpc audit\t\t\tto modify global auditing settings\n");
6992 d_printf(" net rpc shell\t\t\tto open an interactive shell for remote server/account management\n");
6994 d_printf("'net rpc shutdown' also accepts the following miscellaneous options:\n"); /* misc options */
6995 d_printf("\t-r or --reboot\trequest remote server reboot on shutdown\n");
6996 d_printf("\t-f or --force\trequest the remote server force its shutdown\n");
6997 d_printf("\t-t or --timeout=<timeout>\tnumber of seconds before shutdown\n");
6998 d_printf("\t-C or --comment=<message>\ttext message to display on impending shutdown\n");
7004 * Help function for 'net rpc'. Calls command specific help if requested
7005 * or displays usage of net rpc
7007 * @param c A net_context structure
7008 * @param argc Standard main() style argc
7009 * @param argv Standard main() style argv. Initial components are already
7013 int net_rpc_help(struct net_context *c, int argc, const char **argv)
7015 struct functable func[] = {
7016 {"join", rpc_join_usage},
7017 {"user", rpc_user_usage},
7018 {"group", rpc_group_usage},
7019 {"share", rpc_share_usage},
7020 /*{"changetrustpw", rpc_changetrustpw_usage}, */
7021 {"trustdom", rpc_trustdom_usage},
7022 /*{"abortshutdown", rpc_shutdown_abort_usage},*/
7023 /*{"shutdown", rpc_shutdown_usage}, */
7024 {"vampire", rpc_vampire_usage},
7029 net_rpc_usage(c, argc, argv);
7033 return net_run_function(c, argc, argv, func, rpc_user_usage);
7037 * 'net rpc' entrypoint.
7039 * @param c A net_context structure
7040 * @param argc Standard main() style argc
7041 * @param argv Standard main() style argv. Initial components are already
7045 int net_rpc(struct net_context *c, int argc, const char **argv)
7047 struct functable func[] = {
7048 {"audit", net_rpc_audit},
7049 {"info", net_rpc_info},
7050 {"join", net_rpc_join},
7051 {"oldjoin", net_rpc_oldjoin},
7052 {"testjoin", net_rpc_testjoin},
7053 {"user", net_rpc_user},
7054 {"password", rpc_user_password},
7055 {"group", net_rpc_group},
7056 {"share", net_rpc_share},
7057 {"file", net_rpc_file},
7058 {"printer", net_rpc_printer},
7059 {"changetrustpw", net_rpc_changetrustpw},
7060 {"trustdom", rpc_trustdom},
7061 {"abortshutdown", rpc_shutdown_abort},
7062 {"shutdown", rpc_shutdown},
7063 {"samdump", rpc_samdump},
7064 {"vampire", rpc_vampire},
7065 {"getsid", net_rpc_getsid},
7066 {"rights", net_rpc_rights},
7067 {"service", net_rpc_service},
7068 {"registry", net_rpc_registry},
7069 {"shell", net_rpc_shell},
7070 {"help", net_rpc_help},
7073 return net_run_function(c, argc, argv, func, net_rpc_usage);