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 int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1811 NET_API_STATUS status;
1812 struct GROUP_INFO_1 info1;
1813 uint32_t parm_error = 0;
1816 d_printf("Group name must be specified\n");
1817 rpc_group_usage(c, argc, argv);
1823 info1.grpi1_name = argv[0];
1824 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1825 info1.grpi1_comment = c->opt_comment;
1828 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1831 d_fprintf(stderr, "Failed to add group '%s' with: %s.\n",
1832 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1836 d_printf("Added group '%s'.\n", argv[0]);
1842 static NTSTATUS rpc_alias_add_internals(struct net_context *c,
1843 const DOM_SID *domain_sid,
1844 const char *domain_name,
1845 struct cli_state *cli,
1846 struct rpc_pipe_client *pipe_hnd,
1847 TALLOC_CTX *mem_ctx,
1851 POLICY_HND connect_pol, domain_pol, alias_pol;
1852 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1853 union samr_AliasInfo alias_info;
1854 struct lsa_String alias_name;
1858 d_printf("Alias name must be specified\n");
1859 rpc_group_usage(c, argc, argv);
1860 return NT_STATUS_OK;
1863 init_lsa_String(&alias_name, argv[0]);
1865 /* Get sam policy handle */
1867 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1869 MAXIMUM_ALLOWED_ACCESS,
1871 if (!NT_STATUS_IS_OK(result)) goto done;
1873 /* Get domain policy handle */
1875 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1877 MAXIMUM_ALLOWED_ACCESS,
1878 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1880 if (!NT_STATUS_IS_OK(result)) goto done;
1882 /* Create the group */
1884 result = rpccli_samr_CreateDomAlias(pipe_hnd, mem_ctx,
1887 MAXIMUM_ALLOWED_ACCESS,
1890 if (!NT_STATUS_IS_OK(result)) goto done;
1892 if (strlen(c->opt_comment) == 0) goto done;
1894 /* We've got a comment to set */
1896 init_lsa_String(&alias_info.description, c->opt_comment);
1898 result = rpccli_samr_SetAliasInfo(pipe_hnd, mem_ctx,
1903 if (!NT_STATUS_IS_OK(result)) goto done;
1906 if (NT_STATUS_IS_OK(result))
1907 DEBUG(5, ("add alias succeeded\n"));
1909 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1914 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1916 if (c->opt_localgroup)
1917 return run_rpc_command(c, NULL, PI_SAMR, 0,
1918 rpc_alias_add_internals,
1921 return rpc_group_add_internals(c, argc, argv);
1924 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1925 TALLOC_CTX *mem_ctx,
1928 enum lsa_SidType *type)
1930 DOM_SID *sids = NULL;
1931 enum lsa_SidType *types = NULL;
1932 struct rpc_pipe_client *pipe_hnd;
1934 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1936 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result);
1941 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1942 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1944 if (!NT_STATUS_IS_OK(result)) {
1948 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1949 &name, NULL, 1, &sids, &types);
1951 if (NT_STATUS_IS_OK(result)) {
1952 sid_copy(sid, &sids[0]);
1956 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1960 TALLOC_FREE(pipe_hnd);
1963 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1965 /* Try as S-1-5-whatever */
1969 if (string_to_sid(&tmp_sid, name)) {
1970 sid_copy(sid, &tmp_sid);
1971 *type = SID_NAME_UNKNOWN;
1972 result = NT_STATUS_OK;
1979 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1980 TALLOC_CTX *mem_ctx,
1981 const DOM_SID *group_sid,
1984 POLICY_HND connect_pol, domain_pol;
1987 POLICY_HND group_pol;
1989 struct samr_Ids rids, rid_types;
1990 struct lsa_String lsa_acct_name;
1994 sid_copy(&sid, group_sid);
1996 if (!sid_split_rid(&sid, &group_rid)) {
1997 return NT_STATUS_UNSUCCESSFUL;
2000 /* Get sam policy handle */
2001 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2003 MAXIMUM_ALLOWED_ACCESS,
2005 if (!NT_STATUS_IS_OK(result)) {
2009 /* Get domain policy handle */
2010 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2012 MAXIMUM_ALLOWED_ACCESS,
2015 if (!NT_STATUS_IS_OK(result)) {
2019 init_lsa_String(&lsa_acct_name, member);
2021 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2028 if (!NT_STATUS_IS_OK(result)) {
2029 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2033 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2035 MAXIMUM_ALLOWED_ACCESS,
2039 if (!NT_STATUS_IS_OK(result)) {
2043 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
2046 0x0005); /* unknown flags */
2049 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2053 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2054 TALLOC_CTX *mem_ctx,
2055 const DOM_SID *alias_sid,
2058 POLICY_HND connect_pol, domain_pol;
2061 POLICY_HND alias_pol;
2064 enum lsa_SidType member_type;
2068 sid_copy(&sid, alias_sid);
2070 if (!sid_split_rid(&sid, &alias_rid)) {
2071 return NT_STATUS_UNSUCCESSFUL;
2074 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2075 member, &member_sid, &member_type);
2077 if (!NT_STATUS_IS_OK(result)) {
2078 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2082 /* Get sam policy handle */
2083 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2085 MAXIMUM_ALLOWED_ACCESS,
2087 if (!NT_STATUS_IS_OK(result)) {
2091 /* Get domain policy handle */
2092 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2094 MAXIMUM_ALLOWED_ACCESS,
2097 if (!NT_STATUS_IS_OK(result)) {
2101 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2103 MAXIMUM_ALLOWED_ACCESS,
2107 if (!NT_STATUS_IS_OK(result)) {
2111 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
2115 if (!NT_STATUS_IS_OK(result)) {
2120 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2124 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2125 const DOM_SID *domain_sid,
2126 const char *domain_name,
2127 struct cli_state *cli,
2128 struct rpc_pipe_client *pipe_hnd,
2129 TALLOC_CTX *mem_ctx,
2134 enum lsa_SidType group_type;
2137 d_printf("Usage: 'net rpc group addmem <group> <member>\n");
2138 return NT_STATUS_UNSUCCESSFUL;
2141 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2142 &group_sid, &group_type))) {
2143 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2144 return NT_STATUS_UNSUCCESSFUL;
2147 if (group_type == SID_NAME_DOM_GRP) {
2148 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2149 &group_sid, argv[1]);
2151 if (!NT_STATUS_IS_OK(result)) {
2152 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2153 argv[1], argv[0], nt_errstr(result));
2158 if (group_type == SID_NAME_ALIAS) {
2159 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2160 &group_sid, argv[1]);
2162 if (!NT_STATUS_IS_OK(result)) {
2163 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2164 argv[1], argv[0], nt_errstr(result));
2169 d_fprintf(stderr, "Can only add members to global or local groups "
2170 "which %s is not\n", argv[0]);
2172 return NT_STATUS_UNSUCCESSFUL;
2175 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2177 return run_rpc_command(c, NULL, PI_SAMR, 0,
2178 rpc_group_addmem_internals,
2182 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2183 struct rpc_pipe_client *pipe_hnd,
2184 TALLOC_CTX *mem_ctx,
2185 const DOM_SID *group_sid,
2188 POLICY_HND connect_pol, domain_pol;
2191 POLICY_HND group_pol;
2193 struct samr_Ids rids, rid_types;
2194 struct lsa_String lsa_acct_name;
2198 sid_copy(&sid, group_sid);
2200 if (!sid_split_rid(&sid, &group_rid))
2201 return NT_STATUS_UNSUCCESSFUL;
2203 /* Get sam policy handle */
2204 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2206 MAXIMUM_ALLOWED_ACCESS,
2208 if (!NT_STATUS_IS_OK(result))
2211 /* Get domain policy handle */
2212 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2214 MAXIMUM_ALLOWED_ACCESS,
2217 if (!NT_STATUS_IS_OK(result))
2220 init_lsa_String(&lsa_acct_name, member);
2222 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2228 if (!NT_STATUS_IS_OK(result)) {
2229 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2233 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2235 MAXIMUM_ALLOWED_ACCESS,
2239 if (!NT_STATUS_IS_OK(result))
2242 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2247 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2251 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2252 TALLOC_CTX *mem_ctx,
2253 const DOM_SID *alias_sid,
2256 POLICY_HND connect_pol, domain_pol;
2259 POLICY_HND alias_pol;
2262 enum lsa_SidType member_type;
2266 sid_copy(&sid, alias_sid);
2268 if (!sid_split_rid(&sid, &alias_rid))
2269 return NT_STATUS_UNSUCCESSFUL;
2271 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2272 member, &member_sid, &member_type);
2274 if (!NT_STATUS_IS_OK(result)) {
2275 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2279 /* Get sam policy handle */
2280 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2282 MAXIMUM_ALLOWED_ACCESS,
2284 if (!NT_STATUS_IS_OK(result)) {
2288 /* Get domain policy handle */
2289 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2291 MAXIMUM_ALLOWED_ACCESS,
2294 if (!NT_STATUS_IS_OK(result)) {
2298 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2300 MAXIMUM_ALLOWED_ACCESS,
2304 if (!NT_STATUS_IS_OK(result))
2307 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2311 if (!NT_STATUS_IS_OK(result))
2315 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2319 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2320 const DOM_SID *domain_sid,
2321 const char *domain_name,
2322 struct cli_state *cli,
2323 struct rpc_pipe_client *pipe_hnd,
2324 TALLOC_CTX *mem_ctx,
2329 enum lsa_SidType group_type;
2332 d_printf("Usage: 'net rpc group delmem <group> <member>\n");
2333 return NT_STATUS_UNSUCCESSFUL;
2336 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2337 &group_sid, &group_type))) {
2338 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2339 return NT_STATUS_UNSUCCESSFUL;
2342 if (group_type == SID_NAME_DOM_GRP) {
2343 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2344 &group_sid, argv[1]);
2346 if (!NT_STATUS_IS_OK(result)) {
2347 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2348 argv[1], argv[0], nt_errstr(result));
2353 if (group_type == SID_NAME_ALIAS) {
2354 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2355 &group_sid, argv[1]);
2357 if (!NT_STATUS_IS_OK(result)) {
2358 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2359 argv[1], argv[0], nt_errstr(result));
2364 d_fprintf(stderr, "Can only delete members from global or local groups "
2365 "which %s is not\n", argv[0]);
2367 return NT_STATUS_UNSUCCESSFUL;
2370 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2372 return run_rpc_command(c, NULL, PI_SAMR, 0,
2373 rpc_group_delmem_internals,
2378 * List groups on a remote RPC server
2380 * All parameters are provided by the run_rpc_command function, except for
2381 * argc, argv which are passes through.
2383 * @param domain_sid The domain sid acquired from the remote server
2384 * @param cli A cli_state connected to the server.
2385 * @param mem_ctx Talloc context, destoyed on completion of the function.
2386 * @param argc Standard main() style argc
2387 * @param argv Standard main() style argv. Initial components are already
2390 * @return Normal NTSTATUS return.
2393 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2394 const DOM_SID *domain_sid,
2395 const char *domain_name,
2396 struct cli_state *cli,
2397 struct rpc_pipe_client *pipe_hnd,
2398 TALLOC_CTX *mem_ctx,
2402 POLICY_HND connect_pol, domain_pol;
2403 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2404 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2405 struct samr_SamArray *groups = NULL;
2406 bool global = false;
2408 bool builtin = false;
2416 for (i=0; i<argc; i++) {
2417 if (strequal(argv[i], "global"))
2420 if (strequal(argv[i], "local"))
2423 if (strequal(argv[i], "builtin"))
2427 /* Get sam policy handle */
2429 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2431 MAXIMUM_ALLOWED_ACCESS,
2433 if (!NT_STATUS_IS_OK(result)) {
2437 /* Get domain policy handle */
2439 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2441 MAXIMUM_ALLOWED_ACCESS,
2442 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2444 if (!NT_STATUS_IS_OK(result)) {
2448 /* Query domain groups */
2449 if (c->opt_long_list_entries)
2450 d_printf("\nGroup name Comment"\
2451 "\n-----------------------------\n");
2453 uint32_t max_size, total_size, returned_size;
2454 union samr_DispInfo info;
2458 get_query_dispinfo_params(
2459 loop_count, &max_entries, &max_size);
2461 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2470 num_entries = info.info3.count;
2471 start_idx += info.info3.count;
2473 if (!NT_STATUS_IS_OK(result) &&
2474 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2477 for (i = 0; i < num_entries; i++) {
2479 const char *group = NULL;
2480 const char *desc = NULL;
2482 group = info.info3.entries[i].account_name.string;
2483 desc = info.info3.entries[i].description.string;
2485 if (c->opt_long_list_entries)
2486 printf("%-21.21s %-50.50s\n",
2489 printf("%s\n", group);
2491 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2492 /* query domain aliases */
2497 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2503 if (!NT_STATUS_IS_OK(result) &&
2504 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2507 for (i = 0; i < num_entries; i++) {
2509 const char *description = NULL;
2511 if (c->opt_long_list_entries) {
2513 POLICY_HND alias_pol;
2514 union samr_AliasInfo *info = NULL;
2516 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2519 groups->entries[i].idx,
2521 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2525 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2527 description = info->description.string;
2531 if (description != NULL) {
2532 printf("%-21.21s %-50.50s\n",
2533 groups->entries[i].name.string,
2536 printf("%s\n", groups->entries[i].name.string);
2539 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2540 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2541 /* Get builtin policy handle */
2543 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2545 MAXIMUM_ALLOWED_ACCESS,
2546 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2548 if (!NT_STATUS_IS_OK(result)) {
2551 /* query builtin aliases */
2554 if (!builtin) break;
2556 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2562 if (!NT_STATUS_IS_OK(result) &&
2563 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2566 for (i = 0; i < num_entries; i++) {
2568 const char *description = NULL;
2570 if (c->opt_long_list_entries) {
2572 POLICY_HND alias_pol;
2573 union samr_AliasInfo *info = NULL;
2575 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2578 groups->entries[i].idx,
2580 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2584 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2586 description = info->description.string;
2590 if (description != NULL) {
2591 printf("%-21.21s %-50.50s\n",
2592 groups->entries[i].name.string,
2595 printf("%s\n", groups->entries[i].name.string);
2598 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2604 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2606 return run_rpc_command(c, NULL, PI_SAMR, 0,
2607 rpc_group_list_internals,
2611 static NTSTATUS rpc_list_group_members(struct net_context *c,
2612 struct rpc_pipe_client *pipe_hnd,
2613 TALLOC_CTX *mem_ctx,
2614 const char *domain_name,
2615 const DOM_SID *domain_sid,
2616 POLICY_HND *domain_pol,
2620 POLICY_HND group_pol;
2621 uint32 num_members, *group_rids;
2623 struct samr_RidTypeArray *rids = NULL;
2624 struct lsa_Strings names;
2625 struct samr_Ids types;
2628 sid_to_fstring(sid_str, domain_sid);
2630 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2632 MAXIMUM_ALLOWED_ACCESS,
2636 if (!NT_STATUS_IS_OK(result))
2639 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2643 if (!NT_STATUS_IS_OK(result))
2646 num_members = rids->count;
2647 group_rids = rids->rids;
2649 while (num_members > 0) {
2650 int this_time = 512;
2652 if (num_members < this_time)
2653 this_time = num_members;
2655 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2662 if (!NT_STATUS_IS_OK(result))
2665 /* We only have users as members, but make the output
2666 the same as the output of alias members */
2668 for (i = 0; i < this_time; i++) {
2670 if (c->opt_long_list_entries) {
2671 printf("%s-%d %s\\%s %d\n", sid_str,
2672 group_rids[i], domain_name,
2673 names.names[i].string,
2676 printf("%s\\%s\n", domain_name,
2677 names.names[i].string);
2681 num_members -= this_time;
2685 return NT_STATUS_OK;
2688 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2689 struct rpc_pipe_client *pipe_hnd,
2690 TALLOC_CTX *mem_ctx,
2691 POLICY_HND *domain_pol,
2695 struct rpc_pipe_client *lsa_pipe;
2696 POLICY_HND alias_pol, lsa_pol;
2698 DOM_SID *alias_sids;
2701 enum lsa_SidType *types;
2703 struct lsa_SidArray sid_array;
2705 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2707 MAXIMUM_ALLOWED_ACCESS,
2711 if (!NT_STATUS_IS_OK(result))
2714 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2718 if (!NT_STATUS_IS_OK(result)) {
2719 d_fprintf(stderr, "Couldn't list alias members\n");
2723 num_members = sid_array.num_sids;
2725 if (num_members == 0) {
2726 return NT_STATUS_OK;
2729 lsa_pipe = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2730 PI_LSARPC, &result);
2732 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2733 nt_errstr(result) );
2737 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2738 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2740 if (!NT_STATUS_IS_OK(result)) {
2741 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2742 TALLOC_FREE(lsa_pipe);
2746 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2748 d_fprintf(stderr, "Out of memory\n");
2749 TALLOC_FREE(lsa_pipe);
2750 return NT_STATUS_NO_MEMORY;
2753 for (i=0; i<num_members; i++) {
2754 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2757 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2758 num_members, alias_sids,
2759 &domains, &names, &types);
2761 if (!NT_STATUS_IS_OK(result) &&
2762 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2763 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2764 TALLOC_FREE(lsa_pipe);
2768 for (i = 0; i < num_members; i++) {
2770 sid_to_fstring(sid_str, &alias_sids[i]);
2772 if (c->opt_long_list_entries) {
2773 printf("%s %s\\%s %d\n", sid_str,
2774 domains[i] ? domains[i] : "*unknown*",
2775 names[i] ? names[i] : "*unknown*", types[i]);
2778 printf("%s\\%s\n", domains[i], names[i]);
2780 printf("%s\n", sid_str);
2784 TALLOC_FREE(lsa_pipe);
2785 return NT_STATUS_OK;
2788 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2789 const DOM_SID *domain_sid,
2790 const char *domain_name,
2791 struct cli_state *cli,
2792 struct rpc_pipe_client *pipe_hnd,
2793 TALLOC_CTX *mem_ctx,
2798 POLICY_HND connect_pol, domain_pol;
2799 struct samr_Ids rids, rid_types;
2800 struct lsa_String lsa_acct_name;
2802 /* Get sam policy handle */
2804 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2806 MAXIMUM_ALLOWED_ACCESS,
2809 if (!NT_STATUS_IS_OK(result))
2812 /* Get domain policy handle */
2814 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2816 MAXIMUM_ALLOWED_ACCESS,
2817 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2820 if (!NT_STATUS_IS_OK(result))
2823 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2825 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2832 if (!NT_STATUS_IS_OK(result)) {
2834 /* Ok, did not find it in the global sam, try with builtin */
2836 DOM_SID sid_Builtin;
2838 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2840 sid_copy(&sid_Builtin, &global_sid_Builtin);
2842 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2844 MAXIMUM_ALLOWED_ACCESS,
2848 if (!NT_STATUS_IS_OK(result)) {
2849 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2853 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2860 if (!NT_STATUS_IS_OK(result)) {
2861 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2866 if (rids.count != 1) {
2867 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2871 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2872 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2873 domain_sid, &domain_pol,
2877 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2878 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2882 return NT_STATUS_NO_SUCH_GROUP;
2885 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2888 return rpc_group_usage(c, argc, argv);
2891 return run_rpc_command(c, NULL, PI_SAMR, 0,
2892 rpc_group_members_internals,
2896 static NTSTATUS rpc_group_rename_internals(struct net_context *c,
2897 const DOM_SID *domain_sid,
2898 const char *domain_name,
2899 struct cli_state *cli,
2900 struct rpc_pipe_client *pipe_hnd,
2901 TALLOC_CTX *mem_ctx,
2906 POLICY_HND connect_pol, domain_pol, group_pol;
2907 union samr_GroupInfo group_info;
2908 struct samr_Ids rids, rid_types;
2909 struct lsa_String lsa_acct_name;
2912 d_printf("Usage: 'net rpc group rename group newname'\n");
2913 return NT_STATUS_UNSUCCESSFUL;
2916 /* Get sam policy handle */
2918 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2920 MAXIMUM_ALLOWED_ACCESS,
2923 if (!NT_STATUS_IS_OK(result))
2926 /* Get domain policy handle */
2928 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2930 MAXIMUM_ALLOWED_ACCESS,
2931 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2934 if (!NT_STATUS_IS_OK(result))
2937 init_lsa_String(&lsa_acct_name, argv[0]);
2939 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2946 if (rids.count != 1) {
2947 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2951 if (rid_types.ids[0] != SID_NAME_DOM_GRP) {
2952 d_fprintf(stderr, "Can only rename domain groups\n");
2953 return NT_STATUS_UNSUCCESSFUL;
2956 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2958 MAXIMUM_ALLOWED_ACCESS,
2962 if (!NT_STATUS_IS_OK(result))
2965 init_lsa_String(&group_info.name, argv[1]);
2967 result = rpccli_samr_SetGroupInfo(pipe_hnd, mem_ctx,
2972 if (!NT_STATUS_IS_OK(result))
2975 return NT_STATUS_NO_SUCH_GROUP;
2978 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2981 return rpc_group_usage(c, argc, argv);
2984 return run_rpc_command(c, NULL, PI_SAMR, 0,
2985 rpc_group_rename_internals,
2990 * 'net rpc group' entrypoint.
2991 * @param argc Standard main() style argc
2992 * @param argc Standard main() style argv. Initial components are already
2996 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2998 NET_API_STATUS status;
3000 struct functable func[] = {
3001 {"add", rpc_group_add},
3002 {"delete", rpc_group_delete},
3003 {"addmem", rpc_group_addmem},
3004 {"delmem", rpc_group_delmem},
3005 {"list", rpc_group_list},
3006 {"members", rpc_group_members},
3007 {"rename", rpc_group_rename},
3011 status = libnetapi_init(&c->netapi_ctx);
3015 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3016 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3019 return run_rpc_command(c, NULL, PI_SAMR, 0,
3020 rpc_group_list_internals,
3024 return net_run_function(c, argc, argv, func, rpc_group_usage);
3027 /****************************************************************************/
3029 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3031 return net_share_usage(c, argc, argv);
3035 * Add a share on a remote RPC server
3037 * All parameters are provided by the run_rpc_command function, except for
3038 * argc, argv which are passes through.
3040 * @param domain_sid The domain sid acquired from the remote server
3041 * @param cli A cli_state connected to the server.
3042 * @param mem_ctx Talloc context, destoyed on completion of the function.
3043 * @param argc Standard main() style argc
3044 * @param argv Standard main() style argv. Initial components are already
3047 * @return Normal NTSTATUS return.
3049 static NTSTATUS rpc_share_add_internals(struct net_context *c,
3050 const DOM_SID *domain_sid,
3051 const char *domain_name,
3052 struct cli_state *cli,
3053 struct rpc_pipe_client *pipe_hnd,
3054 TALLOC_CTX *mem_ctx,int argc,
3061 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3062 uint32 num_users=0, perms=0;
3063 char *password=NULL; /* don't allow a share password */
3065 union srvsvc_NetShareInfo info;
3066 struct srvsvc_NetShareInfo2 info2;
3067 uint32_t parm_error = 0;
3069 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3070 return NT_STATUS_NO_MEMORY;
3073 path = strchr(sharename, '=');
3075 return NT_STATUS_UNSUCCESSFUL;
3078 info2.name = sharename;
3080 info2.comment = c->opt_comment;
3081 info2.permissions = perms;
3082 info2.max_users = c->opt_maxusers;
3083 info2.current_users = num_users;
3085 info2.password = password;
3087 info.info2 = &info2;
3089 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
3098 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3100 if ((argc < 1) || !strchr(argv[0], '=')) {
3101 DEBUG(1,("Sharename or path not specified on add\n"));
3102 return rpc_share_usage(c, argc, argv);
3104 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3105 rpc_share_add_internals,
3110 * Delete a share on a remote RPC server
3112 * All parameters are provided by the run_rpc_command function, except for
3113 * argc, argv which are passes through.
3115 * @param domain_sid The domain sid acquired from the remote server
3116 * @param cli A cli_state connected to the server.
3117 * @param mem_ctx Talloc context, destoyed on completion of the function.
3118 * @param argc Standard main() style argc
3119 * @param argv Standard main() style argv. Initial components are already
3122 * @return Normal NTSTATUS return.
3124 static NTSTATUS rpc_share_del_internals(struct net_context *c,
3125 const DOM_SID *domain_sid,
3126 const char *domain_name,
3127 struct cli_state *cli,
3128 struct rpc_pipe_client *pipe_hnd,
3129 TALLOC_CTX *mem_ctx,
3135 return rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
3143 * Delete a share on a remote RPC server
3145 * @param domain_sid The domain sid acquired from the remote server
3146 * @param argc Standard main() style argc
3147 * @param argv Standard main() style argv. Initial components are already
3150 * @return A shell status integer (0 for success)
3152 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3155 DEBUG(1,("Sharename not specified on delete\n"));
3156 return rpc_share_usage(c, argc, argv);
3158 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3159 rpc_share_del_internals,
3164 * Formatted print of share info
3166 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3169 static void display_share_info_1(struct net_context *c,
3170 struct srvsvc_NetShareInfo1 *r)
3172 if (c->opt_long_list_entries) {
3173 d_printf("%-12s %-8.8s %-50s\n",
3175 c->share_type[r->type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)],
3178 d_printf("%s\n", r->name);
3182 static WERROR get_share_info(struct net_context *c,
3183 struct rpc_pipe_client *pipe_hnd,
3184 TALLOC_CTX *mem_ctx,
3188 struct srvsvc_NetShareInfoCtr *info_ctr)
3192 union srvsvc_NetShareInfo info;
3194 /* no specific share requested, enumerate all */
3197 uint32_t preferred_len = 0xffffffff;
3198 uint32_t total_entries = 0;
3199 uint32_t resume_handle = 0;
3201 info_ctr->level = level;
3203 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
3213 /* request just one share */
3214 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
3221 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
3226 ZERO_STRUCTP(info_ctr);
3228 info_ctr->level = level;
3233 struct srvsvc_NetShareCtr1 *ctr1;
3235 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3236 W_ERROR_HAVE_NO_MEMORY(ctr1);
3239 ctr1->array = info.info1;
3241 info_ctr->ctr.ctr1 = ctr1;
3245 struct srvsvc_NetShareCtr2 *ctr2;
3247 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3248 W_ERROR_HAVE_NO_MEMORY(ctr2);
3251 ctr2->array = info.info2;
3253 info_ctr->ctr.ctr2 = ctr2;
3257 struct srvsvc_NetShareCtr502 *ctr502;
3259 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3260 W_ERROR_HAVE_NO_MEMORY(ctr502);
3263 ctr502->array = info.info502;
3265 info_ctr->ctr.ctr502 = ctr502;
3273 * List shares on a remote RPC server
3275 * All parameters are provided by the run_rpc_command function, except for
3276 * argc, argv which are passes through.
3278 * @param domain_sid The domain sid acquired from the remote server
3279 * @param cli A cli_state connected to the server.
3280 * @param mem_ctx Talloc context, destoyed on completion of the function.
3281 * @param argc Standard main() style argc
3282 * @param argv Standard main() style argv. Initial components are already
3285 * @return Normal NTSTATUS return.
3288 static NTSTATUS rpc_share_list_internals(struct net_context *c,
3289 const DOM_SID *domain_sid,
3290 const char *domain_name,
3291 struct cli_state *cli,
3292 struct rpc_pipe_client *pipe_hnd,
3293 TALLOC_CTX *mem_ctx,
3297 struct srvsvc_NetShareInfoCtr info_ctr;
3298 struct srvsvc_NetShareCtr1 ctr1;
3300 uint32 i, level = 1;
3302 ZERO_STRUCT(info_ctr);
3306 info_ctr.ctr.ctr1 = &ctr1;
3308 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3310 if (!W_ERROR_IS_OK(result))
3313 /* Display results */
3315 if (c->opt_long_list_entries) {
3317 "\nEnumerating shared resources (exports) on remote server:\n\n"\
3318 "\nShare name Type Description\n"\
3319 "---------- ---- -----------\n");
3321 for (i = 0; i < info_ctr.ctr.ctr1->count; i++)
3322 display_share_info_1(c, &info_ctr.ctr.ctr1->array[i]);
3324 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3328 * 'net rpc share list' entrypoint.
3329 * @param argc Standard main() style argc
3330 * @param argv Standard main() style argv. Initial components are already
3333 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3335 return run_rpc_command(c, NULL, PI_SRVSVC, 0, rpc_share_list_internals,
3339 static bool check_share_availability(struct cli_state *cli, const char *netname)
3341 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3342 d_printf("skipping [%s]: not a file share.\n", netname);
3352 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3353 const char *netname, uint32 type)
3355 /* only support disk shares */
3356 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3357 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3361 /* skip builtin shares */
3362 /* FIXME: should print$ be added too ? */
3363 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3364 strequal(netname,"global"))
3367 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3368 printf("excluding [%s]\n", netname);
3372 return check_share_availability(cli, netname);
3376 * Migrate shares from a remote RPC server to the local RPC server
3378 * All parameters are provided by the run_rpc_command function, except for
3379 * argc, argv which are passed through.
3381 * @param domain_sid The domain sid acquired from the remote server
3382 * @param cli A cli_state connected to the server.
3383 * @param mem_ctx Talloc context, destroyed on completion of the function.
3384 * @param argc Standard main() style argc
3385 * @param argv Standard main() style argv. Initial components are already
3388 * @return Normal NTSTATUS return.
3391 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3392 const DOM_SID *domain_sid,
3393 const char *domain_name,
3394 struct cli_state *cli,
3395 struct rpc_pipe_client *pipe_hnd,
3396 TALLOC_CTX *mem_ctx,
3401 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3402 struct srvsvc_NetShareInfoCtr ctr_src;
3404 struct rpc_pipe_client *srvsvc_pipe = NULL;
3405 struct cli_state *cli_dst = NULL;
3406 uint32 level = 502; /* includes secdesc */
3407 uint32_t parm_error = 0;
3409 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3411 if (!W_ERROR_IS_OK(result))
3414 /* connect destination PI_SRVSVC */
3415 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe, PI_SRVSVC);
3416 if (!NT_STATUS_IS_OK(nt_status))
3420 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3422 union srvsvc_NetShareInfo info;
3423 struct srvsvc_NetShareInfo502 info502 =
3424 ctr_src.ctr.ctr502->array[i];
3426 /* reset error-code */
3427 nt_status = NT_STATUS_UNSUCCESSFUL;
3429 if (!check_share_sanity(c, cli, info502.name, info502.type))
3432 /* finally add the share on the dst server */
3434 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3435 info502.name, info502.path, info502.comment);
3437 info.info502 = &info502;
3439 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3440 srvsvc_pipe->desthost,
3446 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3447 printf(" [%s] does already exist\n",
3452 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3453 printf("cannot add share: %s\n", dos_errstr(result));
3459 nt_status = NT_STATUS_OK;
3463 cli_shutdown(cli_dst);
3471 * Migrate shares from a rpc-server to another
3473 * @param argc Standard main() style argc
3474 * @param argv Standard main() style argv. Initial components are already
3477 * @return A shell status integer (0 for success)
3479 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3484 printf("no server to migrate\n");
3488 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3489 rpc_share_migrate_shares_internals,
3496 * @param f file_info
3497 * @param mask current search mask
3498 * @param state arg-pointer
3501 static void copy_fn(const char *mnt, file_info *f,
3502 const char *mask, void *state)
3504 static NTSTATUS nt_status;
3505 static struct copy_clistate *local_state;
3506 static fstring filename, new_mask;
3509 struct net_context *c;
3511 local_state = (struct copy_clistate *)state;
3512 nt_status = NT_STATUS_UNSUCCESSFUL;
3516 if (strequal(f->name, ".") || strequal(f->name, ".."))
3519 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3522 if (f->mode & aDIR) {
3524 DEBUG(3,("got dir: %s\n", f->name));
3526 fstrcpy(dir, local_state->cwd);
3528 fstrcat(dir, f->name);
3530 switch (net_mode_share)
3532 case NET_MODE_SHARE_MIGRATE:
3533 /* create that directory */
3534 nt_status = net_copy_file(c, local_state->mem_ctx,
3535 local_state->cli_share_src,
3536 local_state->cli_share_dst,
3538 c->opt_acls? true : false,
3539 c->opt_attrs? true : false,
3540 c->opt_timestamps? true:false,
3544 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3548 if (!NT_STATUS_IS_OK(nt_status))
3549 printf("could not handle dir %s: %s\n",
3550 dir, nt_errstr(nt_status));
3552 /* search below that directory */
3553 fstrcpy(new_mask, dir);
3554 fstrcat(new_mask, "\\*");
3556 old_dir = local_state->cwd;
3557 local_state->cwd = dir;
3558 if (!sync_files(local_state, new_mask))
3559 printf("could not handle files\n");
3560 local_state->cwd = old_dir;
3567 fstrcpy(filename, local_state->cwd);
3568 fstrcat(filename, "\\");
3569 fstrcat(filename, f->name);
3571 DEBUG(3,("got file: %s\n", filename));
3573 switch (net_mode_share)
3575 case NET_MODE_SHARE_MIGRATE:
3576 nt_status = net_copy_file(c, local_state->mem_ctx,
3577 local_state->cli_share_src,
3578 local_state->cli_share_dst,
3580 c->opt_acls? true : false,
3581 c->opt_attrs? true : false,
3582 c->opt_timestamps? true: false,
3586 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3590 if (!NT_STATUS_IS_OK(nt_status))
3591 printf("could not handle file %s: %s\n",
3592 filename, nt_errstr(nt_status));
3597 * sync files, can be called recursivly to list files
3598 * and then call copy_fn for each file
3600 * @param cp_clistate pointer to the copy_clistate we work with
3601 * @param mask the current search mask
3603 * @return Boolean result
3605 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3607 struct cli_state *targetcli;
3608 char *targetpath = NULL;
3610 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3612 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate->cli_share_src,
3613 mask, &targetcli, &targetpath ) ) {
3614 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3615 mask, cli_errstr(cp_clistate->cli_share_src));
3619 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3620 d_fprintf(stderr, "listing %s failed with error: %s\n",
3621 mask, cli_errstr(targetcli));
3630 * Set the top level directory permissions before we do any further copies.
3631 * Should set up ACL inheritance.
3634 bool copy_top_level_perms(struct net_context *c,
3635 struct copy_clistate *cp_clistate,
3636 const char *sharename)
3638 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3640 switch (net_mode_share) {
3641 case NET_MODE_SHARE_MIGRATE:
3642 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3643 nt_status = net_copy_fileattr(c,
3644 cp_clistate->mem_ctx,
3645 cp_clistate->cli_share_src,
3646 cp_clistate->cli_share_dst,
3648 c->opt_acls? true : false,
3649 c->opt_attrs? true : false,
3650 c->opt_timestamps? true: false,
3654 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3658 if (!NT_STATUS_IS_OK(nt_status)) {
3659 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3660 sharename, nt_errstr(nt_status));
3668 * Sync all files inside a remote share to another share (over smb)
3670 * All parameters are provided by the run_rpc_command function, except for
3671 * argc, argv which are passes through.
3673 * @param domain_sid The domain sid acquired from the remote server
3674 * @param cli A cli_state connected to the server.
3675 * @param mem_ctx Talloc context, destoyed on completion of the function.
3676 * @param argc Standard main() style argc
3677 * @param argv Standard main() style argv. Initial components are already
3680 * @return Normal NTSTATUS return.
3683 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3684 const DOM_SID *domain_sid,
3685 const char *domain_name,
3686 struct cli_state *cli,
3687 struct rpc_pipe_client *pipe_hnd,
3688 TALLOC_CTX *mem_ctx,
3693 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3694 struct srvsvc_NetShareInfoCtr ctr_src;
3697 struct copy_clistate cp_clistate;
3698 bool got_src_share = false;
3699 bool got_dst_share = false;
3700 const char *mask = "\\*";
3703 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3705 nt_status = NT_STATUS_NO_MEMORY;
3709 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3712 if (!W_ERROR_IS_OK(result))
3715 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3717 struct srvsvc_NetShareInfo502 info502 =
3718 ctr_src.ctr.ctr502->array[i];
3720 if (!check_share_sanity(c, cli, info502.name, info502.type))
3723 /* one might not want to mirror whole discs :) */
3724 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3725 d_printf("skipping [%s]: builtin/hidden share\n", info502.name);
3729 switch (net_mode_share)
3731 case NET_MODE_SHARE_MIGRATE:
3735 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3738 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3740 c->opt_acls ? "including" : "without",
3741 c->opt_attrs ? "including" : "without",
3742 c->opt_timestamps ? "(preserving timestamps)" : "");
3744 cp_clistate.mem_ctx = mem_ctx;
3745 cp_clistate.cli_share_src = NULL;
3746 cp_clistate.cli_share_dst = NULL;
3747 cp_clistate.cwd = NULL;
3748 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3751 /* open share source */
3752 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3753 &cli->dest_ss, cli->desthost,
3754 info502.name, "A:");
3755 if (!NT_STATUS_IS_OK(nt_status))
3758 got_src_share = true;
3760 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3761 /* open share destination */
3762 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3763 NULL, dst, info502.name, "A:");
3764 if (!NT_STATUS_IS_OK(nt_status))
3767 got_dst_share = true;
3770 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3771 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", info502.name);
3772 nt_status = NT_STATUS_UNSUCCESSFUL;
3776 if (!sync_files(&cp_clistate, mask)) {
3777 d_fprintf(stderr, "could not handle files for share: %s\n", info502.name);
3778 nt_status = NT_STATUS_UNSUCCESSFUL;
3783 nt_status = NT_STATUS_OK;
3788 cli_shutdown(cp_clistate.cli_share_src);
3791 cli_shutdown(cp_clistate.cli_share_dst);
3798 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3802 printf("no server to migrate\n");
3806 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3807 rpc_share_migrate_files_internals,
3812 * Migrate share-ACLs from a remote RPC server to the local RPC srever
3814 * All parameters are provided by the run_rpc_command function, except for
3815 * argc, argv which are passes through.
3817 * @param domain_sid The domain sid acquired from the remote server
3818 * @param cli A cli_state connected to the server.
3819 * @param mem_ctx Talloc context, destoyed on completion of the function.
3820 * @param argc Standard main() style argc
3821 * @param argv Standard main() style argv. Initial components are already
3824 * @return Normal NTSTATUS return.
3827 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3828 const DOM_SID *domain_sid,
3829 const char *domain_name,
3830 struct cli_state *cli,
3831 struct rpc_pipe_client *pipe_hnd,
3832 TALLOC_CTX *mem_ctx,
3837 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3838 struct srvsvc_NetShareInfoCtr ctr_src;
3839 union srvsvc_NetShareInfo info;
3841 struct rpc_pipe_client *srvsvc_pipe = NULL;
3842 struct cli_state *cli_dst = NULL;
3843 uint32 level = 502; /* includes secdesc */
3844 uint32_t parm_error = 0;
3846 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3849 if (!W_ERROR_IS_OK(result))
3852 /* connect destination PI_SRVSVC */
3853 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe, PI_SRVSVC);
3854 if (!NT_STATUS_IS_OK(nt_status))
3858 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3860 struct srvsvc_NetShareInfo502 info502 =
3861 ctr_src.ctr.ctr502->array[i];
3863 /* reset error-code */
3864 nt_status = NT_STATUS_UNSUCCESSFUL;
3866 if (!check_share_sanity(c, cli, info502.name, info502.type))
3869 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3870 info502.name, info502.path, info502.comment);
3873 display_sec_desc(info502.sd_buf.sd);
3875 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3876 info.info502 = &info502;
3878 /* finally modify the share on the dst server */
3879 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3880 srvsvc_pipe->desthost,
3886 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3887 printf("cannot set share-acl: %s\n", dos_errstr(result));
3893 nt_status = NT_STATUS_OK;
3897 cli_shutdown(cli_dst);
3905 * Migrate share-acls from a rpc-server to another
3907 * @param argc Standard main() style argc
3908 * @param argv Standard main() style argv. Initial components are already
3911 * @return A shell status integer (0 for success)
3913 static int rpc_share_migrate_security(struct net_context *c, int argc,
3918 printf("no server to migrate\n");
3922 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3923 rpc_share_migrate_security_internals,
3928 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3929 * from one server to another
3931 * @param argc Standard main() style argc
3932 * @param argv Standard main() style argv. Initial components are already
3935 * @return A shell status integer (0 for success)
3938 static int rpc_share_migrate_all(struct net_context *c, int argc,
3944 printf("no server to migrate\n");
3948 /* order is important. we don't want to be locked out by the share-acl
3949 * before copying files - gd */
3951 ret = run_rpc_command(c, NULL, PI_SRVSVC, 0,
3952 rpc_share_migrate_shares_internals, argc, argv);
3956 ret = run_rpc_command(c, NULL, PI_SRVSVC, 0,
3957 rpc_share_migrate_files_internals, argc, argv);
3961 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
3962 rpc_share_migrate_security_internals, argc,
3968 * 'net rpc share migrate' entrypoint.
3969 * @param argc Standard main() style argc
3970 * @param argv Standard main() style argv. Initial components are already
3973 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3976 struct functable func[] = {
3977 {"all", rpc_share_migrate_all},
3978 {"files", rpc_share_migrate_files},
3979 {"help", rpc_share_usage},
3980 {"security", rpc_share_migrate_security},
3981 {"shares", rpc_share_migrate_shares},
3985 net_mode_share = NET_MODE_SHARE_MIGRATE;
3987 return net_run_function(c, argc, argv, func, rpc_share_usage);
3996 static int num_server_aliases;
3997 static struct full_alias *server_aliases;
4000 * Add an alias to the static list.
4002 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4004 if (server_aliases == NULL)
4005 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4007 server_aliases[num_server_aliases] = *alias;
4008 num_server_aliases += 1;
4012 * For a specific domain on the server, fetch all the aliases
4013 * and their members. Add all of them to the server_aliases.
4016 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4017 TALLOC_CTX *mem_ctx,
4018 POLICY_HND *connect_pol,
4019 const DOM_SID *domain_sid)
4021 uint32 start_idx, max_entries, num_entries, i;
4022 struct samr_SamArray *groups = NULL;
4024 POLICY_HND domain_pol;
4026 /* Get domain policy handle */
4028 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
4030 MAXIMUM_ALLOWED_ACCESS,
4031 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4033 if (!NT_STATUS_IS_OK(result))
4040 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
4046 for (i = 0; i < num_entries; i++) {
4048 POLICY_HND alias_pol;
4049 struct full_alias alias;
4050 struct lsa_SidArray sid_array;
4053 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
4055 MAXIMUM_ALLOWED_ACCESS,
4056 groups->entries[i].idx,
4058 if (!NT_STATUS_IS_OK(result))
4061 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
4064 if (!NT_STATUS_IS_OK(result))
4067 alias.num_members = sid_array.num_sids;
4069 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
4070 if (!NT_STATUS_IS_OK(result))
4073 alias.members = NULL;
4075 if (alias.num_members > 0) {
4076 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
4078 for (j = 0; j < alias.num_members; j++)
4079 sid_copy(&alias.members[j],
4080 sid_array.sids[j].sid);
4083 sid_copy(&alias.sid, domain_sid);
4084 sid_append_rid(&alias.sid, groups->entries[i].idx);
4086 push_alias(mem_ctx, &alias);
4088 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4090 result = NT_STATUS_OK;
4093 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4099 * Dump server_aliases as names for debugging purposes.
4102 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4103 const DOM_SID *domain_sid,
4104 const char *domain_name,
4105 struct cli_state *cli,
4106 struct rpc_pipe_client *pipe_hnd,
4107 TALLOC_CTX *mem_ctx,
4115 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4116 SEC_RIGHTS_MAXIMUM_ALLOWED,
4118 if (!NT_STATUS_IS_OK(result))
4121 for (i=0; i<num_server_aliases; i++) {
4124 enum lsa_SidType *types;
4127 struct full_alias *alias = &server_aliases[i];
4129 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4131 &domains, &names, &types);
4132 if (!NT_STATUS_IS_OK(result))
4135 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4137 if (alias->num_members == 0) {
4142 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4145 &domains, &names, &types);
4147 if (!NT_STATUS_IS_OK(result) &&
4148 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4151 for (j=0; j<alias->num_members; j++)
4152 DEBUG(1, ("%s\\%s (%d); ",
4153 domains[j] ? domains[j] : "*unknown*",
4154 names[j] ? names[j] : "*unknown*",types[j]));
4158 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4160 return NT_STATUS_OK;
4164 * Fetch a list of all server aliases and their members into
4168 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4169 const DOM_SID *domain_sid,
4170 const char *domain_name,
4171 struct cli_state *cli,
4172 struct rpc_pipe_client *pipe_hnd,
4173 TALLOC_CTX *mem_ctx,
4178 POLICY_HND connect_pol;
4180 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4182 MAXIMUM_ALLOWED_ACCESS,
4185 if (!NT_STATUS_IS_OK(result))
4188 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4189 &global_sid_Builtin);
4191 if (!NT_STATUS_IS_OK(result))
4194 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4197 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4202 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4204 token->num_sids = 4;
4206 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4207 d_fprintf(stderr, "malloc failed\n");
4208 token->num_sids = 0;
4212 token->user_sids[0] = *user_sid;
4213 sid_copy(&token->user_sids[1], &global_sid_World);
4214 sid_copy(&token->user_sids[2], &global_sid_Network);
4215 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4218 static void free_user_token(NT_USER_TOKEN *token)
4220 SAFE_FREE(token->user_sids);
4223 static bool is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4227 for (i=0; i<token->num_sids; i++) {
4228 if (sid_compare(sid, &token->user_sids[i]) == 0)
4234 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4236 if (is_sid_in_token(token, sid))
4239 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4240 if (!token->user_sids) {
4244 sid_copy(&token->user_sids[token->num_sids], sid);
4246 token->num_sids += 1;
4251 NT_USER_TOKEN token;
4254 static void dump_user_token(struct user_token *token)
4258 d_printf("%s\n", token->name);
4260 for (i=0; i<token->token.num_sids; i++) {
4261 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4265 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4269 for (i=0; i<alias->num_members; i++) {
4270 if (sid_compare(sid, &alias->members[i]) == 0)
4277 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4281 for (i=0; i<num_server_aliases; i++) {
4282 if (is_alias_member(&sid, &server_aliases[i]))
4283 add_sid_to_token(token, &server_aliases[i].sid);
4288 * We got a user token with all the SIDs we can know about without asking the
4289 * server directly. These are the user and domain group sids. All of these can
4290 * be members of aliases. So scan the list of aliases for each of the SIDs and
4291 * add them to the token.
4294 static void collect_alias_memberships(NT_USER_TOKEN *token)
4296 int num_global_sids = token->num_sids;
4299 for (i=0; i<num_global_sids; i++) {
4300 collect_sid_memberships(token, token->user_sids[i]);
4304 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4306 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4307 enum wbcSidType type;
4309 struct wbcDomainSid wsid;
4310 char *sid_str = NULL;
4312 uint32_t num_groups;
4313 gid_t *groups = NULL;
4316 fstr_sprintf(full_name, "%s%c%s",
4317 domain, *lp_winbind_separator(), user);
4319 /* First let's find out the user sid */
4321 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4323 if (!WBC_ERROR_IS_OK(wbc_status)) {
4324 DEBUG(1, ("winbind could not find %s: %s\n",
4325 full_name, wbcErrorString(wbc_status)));
4329 wbc_status = wbcSidToString(&wsid, &sid_str);
4330 if (!WBC_ERROR_IS_OK(wbc_status)) {
4334 if (type != SID_NAME_USER) {
4335 wbcFreeMemory(sid_str);
4336 DEBUG(1, ("%s is not a user\n", full_name));
4340 string_to_sid(&user_sid, sid_str);
4341 wbcFreeMemory(sid_str);
4344 init_user_token(token, &user_sid);
4346 /* And now the groups winbind knows about */
4348 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4349 if (!WBC_ERROR_IS_OK(wbc_status)) {
4350 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4351 full_name, wbcErrorString(wbc_status)));
4355 for (i = 0; i < num_groups; i++) {
4356 gid_t gid = groups[i];
4359 wbc_status = wbcGidToSid(gid, &wsid);
4360 if (!WBC_ERROR_IS_OK(wbc_status)) {
4361 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4362 gid, wbcErrorString(wbc_status)));
4363 wbcFreeMemory(groups);
4367 wbc_status = wbcSidToString(&wsid, &sid_str);
4368 if (!WBC_ERROR_IS_OK(wbc_status)) {
4369 wbcFreeMemory(groups);
4373 DEBUG(3, (" %s\n", sid_str));
4375 string_to_sid(&sid, sid_str);
4376 wbcFreeMemory(sid_str);
4379 add_sid_to_token(token, &sid);
4381 wbcFreeMemory(groups);
4387 * Get a list of all user tokens we want to look at
4390 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4391 struct user_token **user_tokens)
4393 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4394 uint32_t i, num_users;
4396 struct user_token *result;
4397 TALLOC_CTX *frame = NULL;
4399 if (lp_winbind_use_default_domain() &&
4400 (c->opt_target_workgroup == NULL)) {
4401 d_fprintf(stderr, "winbind use default domain = yes set, "
4402 "please specify a workgroup\n");
4406 /* Send request to winbind daemon */
4408 wbc_status = wbcListUsers(NULL, &num_users, &users);
4409 if (!WBC_ERROR_IS_OK(wbc_status)) {
4410 DEBUG(1, ("winbind could not list users: %s\n",
4411 wbcErrorString(wbc_status)));
4415 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4417 if (result == NULL) {
4418 DEBUG(1, ("Could not malloc sid array\n"));
4419 wbcFreeMemory(users);
4423 frame = talloc_stackframe();
4424 for (i=0; i < num_users; i++) {
4425 fstring domain, user;
4428 fstrcpy(result[i].name, users[i]);
4430 p = strchr(users[i], *lp_winbind_separator());
4432 DEBUG(3, ("%s\n", users[i]));
4435 fstrcpy(domain, c->opt_target_workgroup);
4436 fstrcpy(user, users[i]);
4439 fstrcpy(domain, users[i]);
4444 get_user_sids(domain, user, &(result[i].token));
4448 wbcFreeMemory(users);
4450 *num_tokens = num_users;
4451 *user_tokens = result;
4456 static bool get_user_tokens_from_file(FILE *f,
4458 struct user_token **tokens)
4460 struct user_token *token = NULL;
4465 if (fgets(line, sizeof(line)-1, f) == NULL) {
4469 if (line[strlen(line)-1] == '\n')
4470 line[strlen(line)-1] = '\0';
4472 if (line[0] == ' ') {
4476 string_to_sid(&sid, &line[1]);
4478 if (token == NULL) {
4479 DEBUG(0, ("File does not begin with username"));
4483 add_sid_to_token(&token->token, &sid);
4487 /* And a new user... */
4490 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4491 if (*tokens == NULL) {
4492 DEBUG(0, ("Could not realloc tokens\n"));
4496 token = &((*tokens)[*num_tokens-1]);
4498 fstrcpy(token->name, line);
4499 token->token.num_sids = 0;
4500 token->token.user_sids = NULL;
4509 * Show the list of all users that have access to a share
4512 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4513 TALLOC_CTX *mem_ctx,
4514 const char *netname,
4516 struct user_token *tokens)
4519 SEC_DESC *share_sd = NULL;
4520 SEC_DESC *root_sd = NULL;
4521 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4523 union srvsvc_NetShareInfo info;
4528 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4535 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4536 DEBUG(1, ("Coult not query secdesc for share %s\n",
4541 share_sd = info.info502->sd_buf.sd;
4542 if (share_sd == NULL) {
4543 DEBUG(1, ("Got no secdesc for share %s\n",
4549 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4553 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4556 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4559 for (i=0; i<num_tokens; i++) {
4562 if (share_sd != NULL) {
4563 if (!se_access_check(share_sd, &tokens[i].token,
4564 1, &acc_granted, &status)) {
4565 DEBUG(1, ("Could not check share_sd for "
4571 if (!NT_STATUS_IS_OK(status))
4575 if (root_sd == NULL) {
4576 d_printf(" %s\n", tokens[i].name);
4580 if (!se_access_check(root_sd, &tokens[i].token,
4581 1, &acc_granted, &status)) {
4582 DEBUG(1, ("Could not check root_sd for user %s\n",
4587 if (!NT_STATUS_IS_OK(status))
4590 d_printf(" %s\n", tokens[i].name);
4594 cli_close(cli, fnum);
4606 static void collect_share(const char *name, uint32 m,
4607 const char *comment, void *state)
4609 struct share_list *share_list = (struct share_list *)state;
4611 if (m != STYPE_DISKTREE)
4614 share_list->num_shares += 1;
4615 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4616 if (!share_list->shares) {
4617 share_list->num_shares = 0;
4620 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4623 static void rpc_share_userlist_usage(void)
4629 * List shares on a remote RPC server, including the security descriptors
4631 * All parameters are provided by the run_rpc_command function, except for
4632 * argc, argv which are passes through.
4634 * @param domain_sid The domain sid acquired from the remote server
4635 * @param cli A cli_state connected to the server.
4636 * @param mem_ctx Talloc context, destoyed on completion of the function.
4637 * @param argc Standard main() style argc
4638 * @param argv Standard main() style argv. Initial components are already
4641 * @return Normal NTSTATUS return.
4644 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4645 const DOM_SID *domain_sid,
4646 const char *domain_name,
4647 struct cli_state *cli,
4648 struct rpc_pipe_client *pipe_hnd,
4649 TALLOC_CTX *mem_ctx,
4659 struct user_token *tokens = NULL;
4662 struct share_list share_list;
4665 rpc_share_userlist_usage();
4666 return NT_STATUS_UNSUCCESSFUL;
4672 f = fopen(argv[0], "r");
4676 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4677 return NT_STATUS_UNSUCCESSFUL;
4680 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4686 DEBUG(0, ("Could not read users from file\n"));
4687 return NT_STATUS_UNSUCCESSFUL;
4690 for (i=0; i<num_tokens; i++)
4691 collect_alias_memberships(&tokens[i].token);
4693 init_enum_hnd(&hnd, 0);
4695 share_list.num_shares = 0;
4696 share_list.shares = NULL;
4698 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4701 DEBUG(0, ("Error returning browse list: %s\n",
4706 for (i = 0; i < share_list.num_shares; i++) {
4707 char *netname = share_list.shares[i];
4709 if (netname[strlen(netname)-1] == '$')
4712 d_printf("%s\n", netname);
4714 show_userlist(pipe_hnd, mem_ctx, netname,
4715 num_tokens, tokens);
4718 for (i=0; i<num_tokens; i++) {
4719 free_user_token(&tokens[i].token);
4722 SAFE_FREE(share_list.shares);
4724 return NT_STATUS_OK;
4727 static int rpc_share_allowedusers(struct net_context *c, int argc,
4732 result = run_rpc_command(c, NULL, PI_SAMR, 0,
4733 rpc_aliaslist_internals,
4738 result = run_rpc_command(c, NULL, PI_LSARPC, 0,
4744 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
4745 rpc_share_allowedusers_internals,
4749 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4752 struct user_token *tokens = NULL;
4756 net_usersidlist_usage(c, argc, argv);
4760 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4761 DEBUG(0, ("Could not get the user/sid list\n"));
4765 for (i=0; i<num_tokens; i++) {
4766 dump_user_token(&tokens[i]);
4767 free_user_token(&tokens[i].token);
4774 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4776 d_printf("net usersidlist\n"
4777 "\tprints out a list of all users the running winbind knows\n"
4778 "\tabout, together with all their SIDs. This is used as\n"
4779 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4781 net_common_flags_usage(c, argc, argv);
4786 * 'net rpc share' entrypoint.
4787 * @param argc Standard main() style argc
4788 * @param argv Standard main() style argv. Initial components are already
4792 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4794 struct functable func[] = {
4795 {"add", rpc_share_add},
4796 {"delete", rpc_share_delete},
4797 {"allowedusers", rpc_share_allowedusers},
4798 {"migrate", rpc_share_migrate},
4799 {"list", rpc_share_list},
4804 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
4805 rpc_share_list_internals,
4808 return net_run_function(c, argc, argv, func, rpc_share_usage);
4811 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4812 TALLOC_CTX *mem_ctx,
4813 struct rpc_sh_ctx *ctx,
4814 struct rpc_pipe_client *pipe_hnd,
4815 int argc, const char **argv)
4817 return rpc_share_list_internals(c, ctx->domain_sid, ctx->domain_name,
4818 ctx->cli, pipe_hnd, mem_ctx,
4822 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4823 TALLOC_CTX *mem_ctx,
4824 struct rpc_sh_ctx *ctx,
4825 struct rpc_pipe_client *pipe_hnd,
4826 int argc, const char **argv)
4830 uint32_t parm_err = 0;
4831 union srvsvc_NetShareInfo info;
4832 struct srvsvc_NetShareInfo2 info2;
4834 if ((argc < 2) || (argc > 3)) {
4835 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4837 return NT_STATUS_INVALID_PARAMETER;
4840 info2.name = argv[0];
4841 info2.type = STYPE_DISKTREE;
4842 info2.comment = (argc == 3) ? argv[2] : "";
4843 info2.permissions = 0;
4844 info2.max_users = 0;
4845 info2.current_users = 0;
4846 info2.path = argv[1];
4847 info2.password = NULL;
4849 info.info2 = &info2;
4851 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
4861 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4862 TALLOC_CTX *mem_ctx,
4863 struct rpc_sh_ctx *ctx,
4864 struct rpc_pipe_client *pipe_hnd,
4865 int argc, const char **argv)
4871 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4872 return NT_STATUS_INVALID_PARAMETER;
4875 status = rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
4884 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4885 TALLOC_CTX *mem_ctx,
4886 struct rpc_sh_ctx *ctx,
4887 struct rpc_pipe_client *pipe_hnd,
4888 int argc, const char **argv)
4890 union srvsvc_NetShareInfo info;
4895 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4896 return NT_STATUS_INVALID_PARAMETER;
4899 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4905 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4909 d_printf("Name: %s\n", info.info2->name);
4910 d_printf("Comment: %s\n", info.info2->comment);
4911 d_printf("Path: %s\n", info.info2->path);
4912 d_printf("Password: %s\n", info.info2->password);
4915 return werror_to_ntstatus(result);
4918 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4919 struct rpc_sh_ctx *ctx)
4921 static struct rpc_sh_cmd cmds[] = {
4923 { "list", NULL, PI_SRVSVC, rpc_sh_share_list,
4924 "List available shares" },
4926 { "add", NULL, PI_SRVSVC, rpc_sh_share_add,
4929 { "delete", NULL, PI_SRVSVC, rpc_sh_share_delete,
4932 { "info", NULL, PI_SRVSVC, rpc_sh_share_info,
4933 "Get information about a share" },
4935 { NULL, NULL, 0, NULL, NULL }
4941 /****************************************************************************/
4943 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4945 return net_file_usage(c, argc, argv);
4949 * Close a file on a remote RPC server
4951 * All parameters are provided by the run_rpc_command function, except for
4952 * argc, argv which are passes through.
4954 * @param c A net_context structure
4955 * @param domain_sid The domain sid acquired from the remote server
4956 * @param cli A cli_state connected to the server.
4957 * @param mem_ctx Talloc context, destoyed on completion of the function.
4958 * @param argc Standard main() style argc
4959 * @param argv Standard main() style argv. Initial components are already
4962 * @return Normal NTSTATUS return.
4964 static NTSTATUS rpc_file_close_internals(struct net_context *c,
4965 const DOM_SID *domain_sid,
4966 const char *domain_name,
4967 struct cli_state *cli,
4968 struct rpc_pipe_client *pipe_hnd,
4969 TALLOC_CTX *mem_ctx,
4973 return rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx,
4975 atoi(argv[0]), NULL);
4979 * Close a file on a remote RPC server
4981 * @param argc Standard main() style argc
4982 * @param argv Standard main() style argv. Initial components are already
4985 * @return A shell status integer (0 for success)
4987 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4990 DEBUG(1, ("No fileid given on close\n"));
4991 return rpc_file_usage(c, argc, argv);
4994 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
4995 rpc_file_close_internals,
5000 * Formatted print of open file info
5002 * @param r struct srvsvc_NetFileInfo3 contents
5005 static void display_file_info_3(struct srvsvc_NetFileInfo3 *r)
5007 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5008 r->fid, r->user, r->permissions, r->num_locks, r->path);
5012 * List open files on a remote RPC server
5014 * All parameters are provided by the run_rpc_command function, except for
5015 * argc, argv which are passes through.
5017 * @param c A net_context structure
5018 * @param domain_sid The domain sid acquired from the remote server
5019 * @param cli A cli_state connected to the server.
5020 * @param mem_ctx Talloc context, destoyed on completion of the function.
5021 * @param argc Standard main() style argc
5022 * @param argv Standard main() style argv. Initial components are already
5025 * @return Normal NTSTATUS return.
5028 static NTSTATUS rpc_file_list_internals(struct net_context *c,
5029 const DOM_SID *domain_sid,
5030 const char *domain_name,
5031 struct cli_state *cli,
5032 struct rpc_pipe_client *pipe_hnd,
5033 TALLOC_CTX *mem_ctx,
5037 struct srvsvc_NetFileInfoCtr info_ctr;
5038 struct srvsvc_NetFileCtr3 ctr3;
5041 uint32 preferred_len = 0xffffffff, i;
5042 const char *username=NULL;
5043 uint32_t total_entries = 0;
5044 uint32_t resume_handle = 0;
5046 /* if argc > 0, must be user command */
5048 username = smb_xstrdup(argv[0]);
5050 ZERO_STRUCT(info_ctr);
5054 info_ctr.ctr.ctr3 = &ctr3;
5056 status = rpccli_srvsvc_NetFileEnum(pipe_hnd, mem_ctx,
5066 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
5069 /* Display results */
5072 "\nEnumerating open files on remote server:\n\n"\
5073 "\nFileId Opened by Perms Locks Path"\
5074 "\n------ --------- ----- ----- ---- \n");
5075 for (i = 0; i < total_entries; i++)
5076 display_file_info_3(&info_ctr.ctr.ctr3->array[i]);
5078 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
5082 * List files for a user on a remote RPC server
5084 * @param argc Standard main() style argc
5085 * @param argv Standard main() style argv. Initial components are already
5088 * @return A shell status integer (0 for success)
5091 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5094 DEBUG(1, ("No username given\n"));
5095 return rpc_file_usage(c, argc, argv);
5098 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
5099 rpc_file_list_internals,
5104 * 'net rpc file' entrypoint.
5105 * @param argc Standard main() style argc
5106 * @param argv Standard main() style argv. Initial components are already
5110 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5112 struct functable func[] = {
5113 {"close", rpc_file_close},
5114 {"user", rpc_file_user},
5116 {"info", rpc_file_info},
5122 return run_rpc_command(c, NULL, PI_SRVSVC, 0,
5123 rpc_file_list_internals,
5126 return net_run_function(c, argc, argv, func, rpc_file_usage);
5130 * ABORT the shutdown of a remote RPC Server over, initshutdown pipe
5132 * All parameters are provided by the run_rpc_command function, except for
5133 * argc, argv which are passed through.
5135 * @param c A net_context structure
5136 * @param domain_sid The domain sid aquired from the remote server
5137 * @param cli A cli_state connected to the server.
5138 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5139 * @param argc Standard main() style argc
5140 * @param argv Standard main() style argv. Initial components are already
5143 * @return Normal NTSTATUS return.
5146 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5147 const DOM_SID *domain_sid,
5148 const char *domain_name,
5149 struct cli_state *cli,
5150 struct rpc_pipe_client *pipe_hnd,
5151 TALLOC_CTX *mem_ctx,
5155 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5157 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5159 if (NT_STATUS_IS_OK(result)) {
5160 d_printf("\nShutdown successfully aborted\n");
5161 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5163 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5169 * ABORT the shutdown of a remote RPC Server, over winreg pipe
5171 * All parameters are provided by the run_rpc_command function, except for
5172 * argc, argv which are passed through.
5174 * @param c A net_context structure
5175 * @param domain_sid The domain sid aquired from the remote server
5176 * @param cli A cli_state connected to the server.
5177 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5178 * @param argc Standard main() style argc
5179 * @param argv Standard main() style argv. Initial components are already
5182 * @return Normal NTSTATUS return.
5185 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5186 const DOM_SID *domain_sid,
5187 const char *domain_name,
5188 struct cli_state *cli,
5189 struct rpc_pipe_client *pipe_hnd,
5190 TALLOC_CTX *mem_ctx,
5194 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5196 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5198 if (NT_STATUS_IS_OK(result)) {
5199 d_printf("\nShutdown successfully aborted\n");
5200 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5202 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5208 * ABORT the Shut down of a remote RPC server
5210 * @param argc Standard main() style argc
5211 * @param argv Standard main() style argv. Initial components are already
5214 * @return A shell status integer (0 for success)
5217 static int rpc_shutdown_abort(struct net_context *c, int argc,
5220 int rc = run_rpc_command(c, NULL, PI_INITSHUTDOWN, 0,
5221 rpc_shutdown_abort_internals,
5227 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5229 return run_rpc_command(c, NULL, PI_WINREG, 0,
5230 rpc_reg_shutdown_abort_internals,
5235 * Shut down a remote RPC Server via initshutdown pipe
5237 * All parameters are provided by the run_rpc_command function, except for
5238 * argc, argv which are passes through.
5240 * @param c A net_context structure
5241 * @param domain_sid The domain sid aquired from the remote server
5242 * @param cli A cli_state connected to the server.
5243 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5244 * @param argc Standard main() style argc
5245 * @param argc Standard main() style argv. Initial components are already
5248 * @return Normal NTSTATUS return.
5251 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5252 const DOM_SID *domain_sid,
5253 const char *domain_name,
5254 struct cli_state *cli,
5255 struct rpc_pipe_client *pipe_hnd,
5256 TALLOC_CTX *mem_ctx,
5260 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5261 const char *msg = "This machine will be shutdown shortly";
5262 uint32 timeout = 20;
5263 struct initshutdown_String msg_string;
5264 struct initshutdown_String_sub s;
5266 if (c->opt_comment) {
5267 msg = c->opt_comment;
5269 if (c->opt_timeout) {
5270 timeout = c->opt_timeout;
5274 msg_string.name = &s;
5276 /* create an entry */
5277 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5278 &msg_string, timeout, c->opt_force, c->opt_reboot,
5281 if (NT_STATUS_IS_OK(result)) {
5282 d_printf("\nShutdown of remote machine succeeded\n");
5283 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5285 DEBUG(1,("Shutdown of remote machine failed!\n"));
5291 * Shut down a remote RPC Server via winreg pipe
5293 * All parameters are provided by the run_rpc_command function, except for
5294 * argc, argv which are passes through.
5296 * @param c A net_context structure
5297 * @param domain_sid The domain sid aquired from the remote server
5298 * @param cli A cli_state connected to the server.
5299 * @param mem_ctx Talloc context, destoyed on compleation of the function.
5300 * @param argc Standard main() style argc
5301 * @param argc Standard main() style argv. Initial components are already
5304 * @return Normal NTSTATUS return.
5307 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5308 const DOM_SID *domain_sid,
5309 const char *domain_name,
5310 struct cli_state *cli,
5311 struct rpc_pipe_client *pipe_hnd,
5312 TALLOC_CTX *mem_ctx,
5316 const char *msg = "This machine will be shutdown shortly";
5317 uint32 timeout = 20;
5318 struct initshutdown_String msg_string;
5319 struct initshutdown_String_sub s;
5323 if (c->opt_comment) {
5324 msg = c->opt_comment;
5327 msg_string.name = &s;
5329 if (c->opt_timeout) {
5330 timeout = c->opt_timeout;
5333 /* create an entry */
5334 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5335 &msg_string, timeout, c->opt_force, c->opt_reboot,
5338 if (NT_STATUS_IS_OK(result)) {
5339 d_printf("\nShutdown of remote machine succeeded\n");
5341 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5342 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5343 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5345 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(werr));
5352 * Shut down a remote RPC server
5354 * @param argc Standard main() style argc
5355 * @param argc Standard main() style argv. Initial components are already
5358 * @return A shell status integer (0 for success)
5361 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5363 int rc = run_rpc_command(c, NULL, PI_INITSHUTDOWN, 0,
5364 rpc_init_shutdown_internals,
5368 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5369 rc = run_rpc_command(c, NULL, PI_WINREG, 0,
5370 rpc_reg_shutdown_internals, argc, argv);
5376 /***************************************************************************
5377 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5378 ***************************************************************************/
5381 * Add interdomain trust account to the RPC server.
5382 * All parameters (except for argc and argv) are passed by run_rpc_command
5385 * @param c A net_context structure
5386 * @param domain_sid The domain sid acquired from the server
5387 * @param cli A cli_state connected to the server.
5388 * @param mem_ctx Talloc context, destoyed on completion of the function.
5389 * @param argc Standard main() style argc
5390 * @param argc Standard main() style argv. Initial components are already
5393 * @return normal NTSTATUS return code
5396 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5397 const DOM_SID *domain_sid,
5398 const char *domain_name,
5399 struct cli_state *cli,
5400 struct rpc_pipe_client *pipe_hnd,
5401 TALLOC_CTX *mem_ctx,
5405 POLICY_HND connect_pol, domain_pol, user_pol;
5406 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5408 struct lsa_String lsa_acct_name;
5410 uint32 acct_flags=0;
5412 uint32_t access_granted = 0;
5413 union samr_UserInfo info;
5416 d_printf("Usage: net rpc trustdom add <domain_name> <pw>\n");
5417 return NT_STATUS_INVALID_PARAMETER;
5421 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5424 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5425 return NT_STATUS_NO_MEMORY;
5428 strupper_m(acct_name);
5430 init_lsa_String(&lsa_acct_name, acct_name);
5432 /* Get samr policy handle */
5433 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5435 MAXIMUM_ALLOWED_ACCESS,
5437 if (!NT_STATUS_IS_OK(result)) {
5441 /* Get domain policy handle */
5442 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5444 MAXIMUM_ALLOWED_ACCESS,
5445 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5447 if (!NT_STATUS_IS_OK(result)) {
5451 /* Create trusting domain's account */
5452 acb_info = ACB_NORMAL;
5453 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5454 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5455 SAMR_USER_ACCESS_SET_PASSWORD |
5456 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5457 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5459 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5467 if (!NT_STATUS_IS_OK(result)) {
5473 struct samr_LogonHours hours;
5474 struct lsa_BinaryString parameters;
5475 const int units_per_week = 168;
5478 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5480 ZERO_STRUCT(notime);
5482 ZERO_STRUCT(parameters);
5484 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
5486 result = NT_STATUS_NO_MEMORY;
5489 hours.units_per_week = units_per_week;
5490 memset(hours.bits, 0xFF, units_per_week);
5492 init_samr_user_info23(&info.info23,
5493 notime, notime, notime,
5494 notime, notime, notime,
5495 NULL, NULL, NULL, NULL, NULL,
5496 NULL, NULL, NULL, NULL, ¶meters,
5497 0, 0, ACB_DOMTRUST, SAMR_FIELD_ACCT_FLAGS,
5499 0, 0, 0, 0, 0, 0, 0,
5502 SamOEMhashBlob(info.info23.password.data, 516,
5503 &cli->user_session_key);
5505 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5510 if (!NT_STATUS_IS_OK(result)) {
5511 DEBUG(0,("Could not set trust account password: %s\n",
5512 nt_errstr(result)));
5518 SAFE_FREE(acct_name);
5523 * Create interdomain trust account for a remote domain.
5525 * @param argc standard argc
5526 * @param argv standard argv without initial components
5528 * @return Integer status (0 means success)
5531 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5534 return run_rpc_command(c, NULL, PI_SAMR, 0,
5535 rpc_trustdom_add_internals, argc, argv);
5537 d_printf("Usage: net rpc trustdom add <domain>\n");
5544 * Remove interdomain trust account from the RPC server.
5545 * All parameters (except for argc and argv) are passed by run_rpc_command
5548 * @param c A net_context structure
5549 * @param domain_sid The domain sid acquired from the server
5550 * @param cli A cli_state connected to the server.
5551 * @param mem_ctx Talloc context, destoyed on completion of the function.
5552 * @param argc Standard main() style argc
5553 * @param argc Standard main() style argv. Initial components are already
5556 * @return normal NTSTATUS return code
5559 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5560 const DOM_SID *domain_sid,
5561 const char *domain_name,
5562 struct cli_state *cli,
5563 struct rpc_pipe_client *pipe_hnd,
5564 TALLOC_CTX *mem_ctx,
5568 POLICY_HND connect_pol, domain_pol, user_pol;
5569 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5571 DOM_SID trust_acct_sid;
5572 struct samr_Ids user_rids, name_types;
5573 struct lsa_String lsa_acct_name;
5576 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5577 return NT_STATUS_INVALID_PARAMETER;
5581 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5583 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5585 if (acct_name == NULL)
5586 return NT_STATUS_NO_MEMORY;
5588 strupper_m(acct_name);
5590 /* Get samr policy handle */
5591 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5593 MAXIMUM_ALLOWED_ACCESS,
5595 if (!NT_STATUS_IS_OK(result)) {
5599 /* Get domain policy handle */
5600 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5602 MAXIMUM_ALLOWED_ACCESS,
5603 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5605 if (!NT_STATUS_IS_OK(result)) {
5609 init_lsa_String(&lsa_acct_name, acct_name);
5611 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5618 if (!NT_STATUS_IS_OK(result)) {
5622 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5624 MAXIMUM_ALLOWED_ACCESS,
5628 if (!NT_STATUS_IS_OK(result)) {
5632 /* append the rid to the domain sid */
5633 sid_copy(&trust_acct_sid, domain_sid);
5634 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5638 /* remove the sid */
5640 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5643 if (!NT_STATUS_IS_OK(result)) {
5649 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5652 if (!NT_STATUS_IS_OK(result)) {
5656 if (!NT_STATUS_IS_OK(result)) {
5657 DEBUG(0,("Could not set trust account password: %s\n",
5658 nt_errstr(result)));
5667 * Delete interdomain trust account for a remote domain.
5669 * @param argc standard argc
5670 * @param argv standard argv without initial components
5672 * @return Integer status (0 means success)
5675 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5678 return run_rpc_command(c, NULL, PI_SAMR, 0,
5679 rpc_trustdom_del_internals, argc, argv);
5681 d_printf("Usage: net rpc trustdom del <domain>\n");
5686 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5687 struct cli_state *cli,
5688 TALLOC_CTX *mem_ctx,
5689 const char *domain_name)
5691 char *dc_name = NULL;
5692 const char *buffer = NULL;
5693 struct rpc_pipe_client *netr;
5696 /* Use NetServerEnum2 */
5698 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5700 return NT_STATUS_OK;
5703 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5704 for domain %s\n", domain_name));
5706 /* Try netr_GetDcName */
5708 netr = cli_rpc_pipe_open_noauth(cli, PI_NETLOGON, &status);
5713 status = rpccli_netr_GetDcName(netr, mem_ctx,
5720 if (NT_STATUS_IS_OK(status)) {
5724 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5725 for domain %s\n", domain_name));
5731 * Establish trust relationship to a trusting domain.
5732 * Interdomain account must already be created on remote PDC.
5734 * @param c A net_context structure
5735 * @param argc standard argc
5736 * @param argv standard argv without initial components
5738 * @return Integer status (0 means success)
5741 static int rpc_trustdom_establish(struct net_context *c, int argc,
5744 struct cli_state *cli = NULL;
5745 struct sockaddr_storage server_ss;
5746 struct rpc_pipe_client *pipe_hnd = NULL;
5747 POLICY_HND connect_hnd;
5748 TALLOC_CTX *mem_ctx;
5750 DOM_SID *domain_sid;
5755 union lsa_PolicyInformation *info = NULL;
5758 * Connect to \\server\ipc$ as 'our domain' account with password
5762 d_printf("Usage: net rpc trustdom establish <domain_name>\n");
5766 domain_name = smb_xstrdup(argv[0]);
5767 strupper_m(domain_name);
5769 /* account name used at first is our domain's name with '$' */
5770 asprintf(&acct_name, "%s$", lp_workgroup());
5771 strupper_m(acct_name);
5774 * opt_workgroup will be used by connection functions further,
5775 * hence it should be set to remote domain name instead of ours
5777 if (c->opt_workgroup) {
5778 c->opt_workgroup = smb_xstrdup(domain_name);
5781 c->opt_user_name = acct_name;
5783 /* find the domain controller */
5784 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5785 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5789 /* connect to ipc$ as username/password */
5790 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5791 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5793 /* Is it trusting domain account for sure ? */
5794 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5795 nt_errstr(nt_status)));
5799 /* store who we connected to */
5801 saf_store( domain_name, pdc_name );
5804 * Connect to \\server\ipc$ again (this time anonymously)
5807 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5810 if (NT_STATUS_IS_ERR(nt_status)) {
5811 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5812 domain_name, nt_errstr(nt_status)));
5816 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5817 "domain %s", domain_name))) {
5818 DEBUG(0, ("talloc_init() failed\n"));
5823 /* Make sure we're talking to a proper server */
5825 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5826 if (!NT_STATUS_IS_OK(nt_status)) {
5828 talloc_destroy(mem_ctx);
5833 * Call LsaOpenPolicy and LsaQueryInfo
5836 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
5838 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5840 talloc_destroy(mem_ctx);
5844 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, SEC_RIGHTS_QUERY_VALUE,
5846 if (NT_STATUS_IS_ERR(nt_status)) {
5847 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5848 nt_errstr(nt_status)));
5850 talloc_destroy(mem_ctx);
5854 /* Querying info level 5 */
5856 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5858 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5860 if (NT_STATUS_IS_ERR(nt_status)) {
5861 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5862 nt_errstr(nt_status)));
5864 talloc_destroy(mem_ctx);
5868 domain_sid = info->account_domain.sid;
5870 /* There should be actually query info level 3 (following nt serv behaviour),
5871 but I still don't know if it's _really_ necessary */
5874 * Store the password in secrets db
5877 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5878 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5880 talloc_destroy(mem_ctx);
5885 * Close the pipes and clean up
5888 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5889 if (NT_STATUS_IS_ERR(nt_status)) {
5890 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5891 nt_errstr(nt_status)));
5893 talloc_destroy(mem_ctx);
5899 talloc_destroy(mem_ctx);
5901 d_printf("Trust to domain %s established\n", domain_name);
5906 * Revoke trust relationship to the remote domain
5908 * @param c A net_context structure
5909 * @param argc standard argc
5910 * @param argv standard argv without initial components
5912 * @return Integer status (0 means success)
5915 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5921 if (argc < 1) return -1;
5923 /* generate upper cased domain name */
5924 domain_name = smb_xstrdup(argv[0]);
5925 strupper_m(domain_name);
5927 /* delete password of the trust */
5928 if (!pdb_del_trusteddom_pw(domain_name)) {
5929 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5936 SAFE_FREE(domain_name);
5941 * Usage for 'net rpc trustdom' command
5943 * @param argc standard argc
5944 * @param argv standard argv without inital components
5946 * @return Integer status returned to shell
5949 static int rpc_trustdom_usage(struct net_context *c, int argc,
5952 d_printf(" net rpc trustdom add \t\t add trusting domain's account\n");
5953 d_printf(" net rpc trustdom del \t\t delete trusting domain's account\n");
5954 d_printf(" net rpc trustdom establish \t establish relationship to trusted domain\n");
5955 d_printf(" net rpc trustdom revoke \t abandon relationship to trusted domain\n");
5956 d_printf(" net rpc trustdom list \t show current interdomain trust relationships\n");
5957 d_printf(" net rpc trustdom vampire \t vampire interdomain trust relationships from remote server\n");
5962 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5963 const DOM_SID *domain_sid,
5964 const char *domain_name,
5965 struct cli_state *cli,
5966 struct rpc_pipe_client *pipe_hnd,
5967 TALLOC_CTX *mem_ctx,
5972 sid_to_fstring(str_sid, domain_sid);
5973 d_printf("%s\n", str_sid);
5974 return NT_STATUS_OK;
5977 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5979 fstring ascii_sid, padding;
5980 int pad_len, col_len = 20;
5982 /* convert sid into ascii string */
5983 sid_to_fstring(ascii_sid, dom_sid);
5985 /* calculate padding space for d_printf to look nicer */
5986 pad_len = col_len - strlen(trusted_dom_name);
5987 padding[pad_len] = 0;
5988 do padding[--pad_len] = ' '; while (pad_len);
5990 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5993 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5994 TALLOC_CTX *mem_ctx,
5997 const char *trusted_dom_name)
6000 union lsa_TrustedDomainInfo *info = NULL;
6001 char *cleartextpwd = NULL;
6002 uint8_t nt_hash[16];
6005 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
6008 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6010 if (NT_STATUS_IS_ERR(nt_status)) {
6011 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6012 nt_errstr(nt_status)));
6016 data = data_blob(info->password.password->data,
6017 info->password.password->length);
6019 if (!rpccli_get_pwd_hash(pipe_hnd, nt_hash)) {
6020 DEBUG(0, ("Could not retrieve password hash\n"));
6024 cleartextpwd = decrypt_trustdom_secret(nt_hash, &data);
6026 if (cleartextpwd == NULL) {
6027 DEBUG(0,("retrieved NULL password\n"));
6028 nt_status = NT_STATUS_UNSUCCESSFUL;
6032 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6033 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6034 nt_status = NT_STATUS_UNSUCCESSFUL;
6038 #ifdef DEBUG_PASSWORD
6039 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6040 "password: [%s]\n", trusted_dom_name,
6041 sid_string_dbg(&dom_sid), cleartextpwd));
6045 SAFE_FREE(cleartextpwd);
6046 data_blob_free(&data);
6051 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6054 /* common variables */
6055 TALLOC_CTX* mem_ctx;
6056 struct cli_state *cli = NULL;
6057 struct rpc_pipe_client *pipe_hnd = NULL;
6059 const char *domain_name = NULL;
6060 DOM_SID *queried_dom_sid;
6061 POLICY_HND connect_hnd;
6062 union lsa_PolicyInformation *info = NULL;
6064 /* trusted domains listing variables */
6065 unsigned int enum_ctx = 0;
6067 struct lsa_DomainList dom_list;
6071 * Listing trusted domains (stored in secrets.tdb, if local)
6074 mem_ctx = talloc_init("trust relationships vampire");
6077 * set domain and pdc name to local samba server (default)
6078 * or to remote one given in command line
6081 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6082 domain_name = c->opt_workgroup;
6083 c->opt_target_workgroup = c->opt_workgroup;
6085 fstrcpy(pdc_name, global_myname());
6086 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6087 c->opt_target_workgroup = domain_name;
6090 /* open \PIPE\lsarpc and open policy handle */
6091 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6092 if (!NT_STATUS_IS_OK(nt_status)) {
6093 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6094 nt_errstr(nt_status)));
6095 talloc_destroy(mem_ctx);
6099 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6101 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6102 nt_errstr(nt_status) ));
6104 talloc_destroy(mem_ctx);
6108 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6110 if (NT_STATUS_IS_ERR(nt_status)) {
6111 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6112 nt_errstr(nt_status)));
6114 talloc_destroy(mem_ctx);
6118 /* query info level 5 to obtain sid of a domain being queried */
6119 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6121 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6124 if (NT_STATUS_IS_ERR(nt_status)) {
6125 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6126 nt_errstr(nt_status)));
6128 talloc_destroy(mem_ctx);
6132 queried_dom_sid = info->account_domain.sid;
6135 * Keep calling LsaEnumTrustdom over opened pipe until
6136 * the end of enumeration is reached
6139 d_printf("Vampire trusted domains:\n\n");
6142 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6147 if (NT_STATUS_IS_ERR(nt_status)) {
6148 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6149 nt_errstr(nt_status)));
6151 talloc_destroy(mem_ctx);
6155 for (i = 0; i < dom_list.count; i++) {
6157 print_trusted_domain(dom_list.domains[i].sid,
6158 dom_list.domains[i].name.string);
6160 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6161 *dom_list.domains[i].sid,
6162 dom_list.domains[i].name.string);
6163 if (!NT_STATUS_IS_OK(nt_status)) {
6165 talloc_destroy(mem_ctx);
6171 * in case of no trusted domains say something rather
6172 * than just display blank line
6174 if (!dom_list.count) d_printf("none\n");
6176 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6178 /* close this connection before doing next one */
6179 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6180 if (NT_STATUS_IS_ERR(nt_status)) {
6181 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6182 nt_errstr(nt_status)));
6184 talloc_destroy(mem_ctx);
6188 /* close lsarpc pipe and connection to IPC$ */
6191 talloc_destroy(mem_ctx);
6195 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6197 /* common variables */
6198 TALLOC_CTX* mem_ctx;
6199 struct cli_state *cli = NULL, *remote_cli = NULL;
6200 struct rpc_pipe_client *pipe_hnd = NULL;
6202 const char *domain_name = NULL;
6203 DOM_SID *queried_dom_sid;
6205 int ascii_dom_name_len;
6206 POLICY_HND connect_hnd;
6207 union lsa_PolicyInformation *info = NULL;
6209 /* trusted domains listing variables */
6210 unsigned int num_domains, enum_ctx = 0;
6211 int i, pad_len, col_len = 20;
6212 struct lsa_DomainList dom_list;
6215 /* trusting domains listing variables */
6216 POLICY_HND domain_hnd;
6217 struct samr_SamArray *trusts = NULL;
6220 * Listing trusted domains (stored in secrets.tdb, if local)
6223 mem_ctx = talloc_init("trust relationships listing");
6226 * set domain and pdc name to local samba server (default)
6227 * or to remote one given in command line
6230 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6231 domain_name = c->opt_workgroup;
6232 c->opt_target_workgroup = c->opt_workgroup;
6234 fstrcpy(pdc_name, global_myname());
6235 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6236 c->opt_target_workgroup = domain_name;
6239 /* open \PIPE\lsarpc and open policy handle */
6240 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6241 if (!NT_STATUS_IS_OK(nt_status)) {
6242 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6243 nt_errstr(nt_status)));
6244 talloc_destroy(mem_ctx);
6248 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &nt_status);
6250 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6251 nt_errstr(nt_status) ));
6253 talloc_destroy(mem_ctx);
6257 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6259 if (NT_STATUS_IS_ERR(nt_status)) {
6260 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6261 nt_errstr(nt_status)));
6263 talloc_destroy(mem_ctx);
6267 /* query info level 5 to obtain sid of a domain being queried */
6268 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6270 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6273 if (NT_STATUS_IS_ERR(nt_status)) {
6274 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6275 nt_errstr(nt_status)));
6277 talloc_destroy(mem_ctx);
6281 queried_dom_sid = info->account_domain.sid;
6284 * Keep calling LsaEnumTrustdom over opened pipe until
6285 * the end of enumeration is reached
6288 d_printf("Trusted domains list:\n\n");
6291 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6296 if (NT_STATUS_IS_ERR(nt_status)) {
6297 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6298 nt_errstr(nt_status)));
6300 talloc_destroy(mem_ctx);
6304 for (i = 0; i < dom_list.count; i++) {
6305 print_trusted_domain(dom_list.domains[i].sid,
6306 dom_list.domains[i].name.string);
6310 * in case of no trusted domains say something rather
6311 * than just display blank line
6313 if (!dom_list.count) d_printf("none\n");
6315 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6317 /* close this connection before doing next one */
6318 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6319 if (NT_STATUS_IS_ERR(nt_status)) {
6320 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6321 nt_errstr(nt_status)));
6323 talloc_destroy(mem_ctx);
6327 TALLOC_FREE(pipe_hnd);
6330 * Listing trusting domains (stored in passdb backend, if local)
6333 d_printf("\nTrusting domains list:\n\n");
6336 * Open \PIPE\samr and get needed policy handles
6338 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SAMR, &nt_status);
6340 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6342 talloc_destroy(mem_ctx);
6347 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6349 SA_RIGHT_SAM_OPEN_DOMAIN,
6351 if (!NT_STATUS_IS_OK(nt_status)) {
6352 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6353 nt_errstr(nt_status)));
6355 talloc_destroy(mem_ctx);
6359 /* SamrOpenDomain - we have to open domain policy handle in order to be
6360 able to enumerate accounts*/
6361 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6363 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6366 if (!NT_STATUS_IS_OK(nt_status)) {
6367 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6368 nt_errstr(nt_status)));
6370 talloc_destroy(mem_ctx);
6375 * perform actual enumeration
6378 enum_ctx = 0; /* reset enumeration context from last enumeration */
6381 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6388 if (NT_STATUS_IS_ERR(nt_status)) {
6389 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6390 nt_errstr(nt_status)));
6392 talloc_destroy(mem_ctx);
6396 for (i = 0; i < num_domains; i++) {
6398 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6401 * get each single domain's sid (do we _really_ need this ?):
6402 * 1) connect to domain's pdc
6403 * 2) query the pdc for domain's sid
6406 /* get rid of '$' tail */
6407 ascii_dom_name_len = strlen(str);
6408 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6409 str[ascii_dom_name_len - 1] = '\0';
6411 /* calculate padding space for d_printf to look nicer */
6412 pad_len = col_len - strlen(str);
6413 padding[pad_len] = 0;
6414 do padding[--pad_len] = ' '; while (pad_len);
6416 /* set opt_* variables to remote domain */
6418 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6419 c->opt_target_workgroup = c->opt_workgroup;
6421 d_printf("%s%s", str, padding);
6423 /* connect to remote domain controller */
6424 nt_status = net_make_ipc_connection(c,
6425 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6427 if (NT_STATUS_IS_OK(nt_status)) {
6428 /* query for domain's sid */
6429 if (run_rpc_command(c, remote_cli, PI_LSARPC, 0,
6430 rpc_query_domain_sid, argc,
6432 d_fprintf(stderr, "couldn't get domain's sid\n");
6434 cli_shutdown(remote_cli);
6437 d_fprintf(stderr, "domain controller is not "
6439 nt_errstr(nt_status));
6443 if (!num_domains) d_printf("none\n");
6445 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6447 /* close opened samr and domain policy handles */
6448 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6449 if (!NT_STATUS_IS_OK(nt_status)) {
6450 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6453 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6454 if (!NT_STATUS_IS_OK(nt_status)) {
6455 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6458 /* close samr pipe and connection to IPC$ */
6461 talloc_destroy(mem_ctx);
6466 * Entrypoint for 'net rpc trustdom' code
6468 * @param argc standard argc
6469 * @param argv standard argv without initial components
6471 * @return Integer status (0 means success)
6474 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6476 struct functable func[] = {
6477 {"add", rpc_trustdom_add},
6478 {"del", rpc_trustdom_del},
6479 {"establish", rpc_trustdom_establish},
6480 {"revoke", rpc_trustdom_revoke},
6481 {"help", rpc_trustdom_usage},
6482 {"list", rpc_trustdom_list},
6483 {"vampire", rpc_trustdom_vampire},
6488 rpc_trustdom_usage(c, argc, argv);
6492 return net_run_function(c, argc, argv, func, rpc_user_usage);
6496 * Check if a server will take rpc commands
6497 * @param flags Type of server to connect to (PDC, DMB, localhost)
6498 * if the host is not explicitly specified
6499 * @return bool (true means rpc supported)
6501 bool net_rpc_check(struct net_context *c, unsigned flags)
6503 struct cli_state *cli;
6505 struct sockaddr_storage server_ss;
6506 char *server_name = NULL;
6509 /* flags (i.e. server type) may depend on command */
6510 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6513 if ((cli = cli_initialise()) == NULL) {
6517 status = cli_connect(cli, server_name, &server_ss);
6518 if (!NT_STATUS_IS_OK(status))
6520 if (!attempt_netbios_session_request(&cli, global_myname(),
6521 server_name, &server_ss))
6523 if (!cli_negprot(cli))
6525 if (cli->protocol < PROTOCOL_NT1)
6534 /* dump sam database via samsync rpc calls */
6535 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6536 return run_rpc_command(c, NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS,
6537 rpc_samdump_internals, argc, argv);
6540 /* syncronise sam database via samsync rpc calls */
6541 static int rpc_vampire(struct net_context *c, int argc, const char **argv) {
6542 return run_rpc_command(c, NULL, PI_NETLOGON, NET_FLAGS_ANONYMOUS,
6543 rpc_vampire_internals, argc, argv);
6547 * Migrate everything from a print-server
6549 * @param c A net_context structure
6550 * @param argc Standard main() style argc
6551 * @param argv Standard main() style argv. Initial components are already
6554 * @return A shell status integer (0 for success)
6556 * The order is important !
6557 * To successfully add drivers the print-queues have to exist !
6558 * Applying ACLs should be the last step, because you're easily locked out
6561 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6567 printf("no server to migrate\n");
6571 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6572 rpc_printer_migrate_printers_internals, argc,
6577 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6578 rpc_printer_migrate_drivers_internals, argc,
6583 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6584 rpc_printer_migrate_forms_internals, argc, argv);
6588 ret = run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6589 rpc_printer_migrate_settings_internals, argc,
6594 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6595 rpc_printer_migrate_security_internals, argc,
6601 * Migrate print-drivers from a print-server
6603 * @param c A net_context structure
6604 * @param argc Standard main() style argc
6605 * @param argv Standard main() style argv. Initial components are already
6608 * @return A shell status integer (0 for success)
6610 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6614 printf("no server to migrate\n");
6618 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6619 rpc_printer_migrate_drivers_internals,
6624 * Migrate print-forms from a print-server
6626 * @param c A net_context structure
6627 * @param argc Standard main() style argc
6628 * @param argv Standard main() style argv. Initial components are already
6631 * @return A shell status integer (0 for success)
6633 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6637 printf("no server to migrate\n");
6641 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6642 rpc_printer_migrate_forms_internals,
6647 * Migrate printers from a print-server
6649 * @param c A net_context structure
6650 * @param argc Standard main() style argc
6651 * @param argv Standard main() style argv. Initial components are already
6654 * @return A shell status integer (0 for success)
6656 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6660 printf("no server to migrate\n");
6664 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6665 rpc_printer_migrate_printers_internals,
6670 * Migrate printer-ACLs from a print-server
6672 * @param c A net_context structure
6673 * @param argc Standard main() style argc
6674 * @param argv Standard main() style argv. Initial components are already
6677 * @return A shell status integer (0 for success)
6679 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6683 printf("no server to migrate\n");
6687 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6688 rpc_printer_migrate_security_internals,
6693 * Migrate printer-settings from a print-server
6695 * @param c A net_context structure
6696 * @param argc Standard main() style argc
6697 * @param argv Standard main() style argv. Initial components are already
6700 * @return A shell status integer (0 for success)
6702 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6706 printf("no server to migrate\n");
6710 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6711 rpc_printer_migrate_settings_internals,
6716 * 'net rpc printer' entrypoint.
6718 * @param c A net_context structure
6719 * @param argc Standard main() style argc
6720 * @param argv Standard main() style argv. Initial components are already
6724 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6727 /* ouch: when addriver and setdriver are called from within
6728 rpc_printer_migrate_drivers_internals, the printer-queue already
6731 struct functable func[] = {
6732 {"all", rpc_printer_migrate_all},
6733 {"drivers", rpc_printer_migrate_drivers},
6734 {"forms", rpc_printer_migrate_forms},
6735 {"help", rpc_printer_usage},
6736 {"printers", rpc_printer_migrate_printers},
6737 {"security", rpc_printer_migrate_security},
6738 {"settings", rpc_printer_migrate_settings},
6742 return net_run_function(c, argc, argv, func, rpc_printer_usage);
6747 * List printers on a remote RPC server
6749 * @param c A net_context structure
6750 * @param argc Standard main() style argc
6751 * @param argv Standard main() style argv. Initial components are already
6754 * @return A shell status integer (0 for success)
6756 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6759 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6760 rpc_printer_list_internals,
6765 * List printer-drivers on a remote RPC server
6767 * @param c A net_context structure
6768 * @param argc Standard main() style argc
6769 * @param argv Standard main() style argv. Initial components are already
6772 * @return A shell status integer (0 for success)
6774 static int rpc_printer_driver_list(struct net_context *c, int argc,
6778 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6779 rpc_printer_driver_list_internals,
6784 * Publish printer in ADS via MSRPC
6786 * @param c A net_context structure
6787 * @param argc Standard main() style argc
6788 * @param argv Standard main() style argv. Initial components are already
6791 * @return A shell status integer (0 for success)
6793 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6797 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6798 rpc_printer_publish_publish_internals,
6803 * Update printer in ADS via MSRPC
6805 * @param c A net_context structure
6806 * @param argc Standard main() style argc
6807 * @param argv Standard main() style argv. Initial components are already
6810 * @return A shell status integer (0 for success)
6812 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6815 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6816 rpc_printer_publish_update_internals,
6821 * UnPublish printer in ADS via MSRPC
6823 * @param c A net_context structure
6824 * @param argc Standard main() style argc
6825 * @param argv Standard main() style argv. Initial components are already
6828 * @return A shell status integer (0 for success)
6830 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
6834 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6835 rpc_printer_publish_unpublish_internals,
6840 * List published printers via MSRPC
6842 * @param c A net_context structure
6843 * @param argc Standard main() style argc
6844 * @param argv Standard main() style argv. Initial components are already
6847 * @return A shell status integer (0 for success)
6849 static int rpc_printer_publish_list(struct net_context *c, int argc,
6853 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6854 rpc_printer_publish_list_internals,
6860 * Publish printer in ADS
6862 * @param c A net_context structure
6863 * @param argc Standard main() style argc
6864 * @param argv Standard main() style argv. Initial components are already
6867 * @return A shell status integer (0 for success)
6869 static int rpc_printer_publish(struct net_context *c, int argc,
6873 struct functable func[] = {
6874 {"publish", rpc_printer_publish_publish},
6875 {"update", rpc_printer_publish_update},
6876 {"unpublish", rpc_printer_publish_unpublish},
6877 {"list", rpc_printer_publish_list},
6878 {"help", rpc_printer_usage},
6883 return run_rpc_command(c, NULL, PI_SPOOLSS, 0,
6884 rpc_printer_publish_list_internals,
6887 return net_run_function(c, argc, argv, func, rpc_printer_usage);
6893 * Display rpc printer help page.
6895 * @param c A net_context structure
6896 * @param argc Standard main() style argc
6897 * @param argv Standard main() style argv. Initial components are already
6900 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
6902 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"\
6903 "\tlists all printers on print-server\n\n");
6904 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"\
6905 "\tlists all printer-drivers on print-server\n\n");
6906 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"\
6907 "\tpublishes printer settings in Active Directory\n"
6908 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
6909 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"\
6910 "\n\tmigrates printers from remote to local server\n\n");
6911 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"\
6912 "\n\tmigrates printer-settings from remote to local server\n\n");
6913 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"\
6914 "\n\tmigrates printer-drivers from remote to local server\n\n");
6915 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"\
6916 "\n\tmigrates printer-forms from remote to local server\n\n");
6917 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"\
6918 "\n\tmigrates printer-ACLs from remote to local server\n\n");
6919 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"\
6920 "\n\tmigrates drivers, forms, queues, settings and acls from\n"\
6921 "\tremote to local print-server\n\n");
6922 net_common_methods_usage(c, argc, argv);
6923 net_common_flags_usage(c, argc, argv);
6925 "\t-v or --verbose\t\t\tgive verbose output\n"
6926 "\t --destination\t\tmigration target server (default: localhost)\n");
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 help 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_help(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_usage(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},
7025 {"help", net_rpc_help},
7030 net_rpc_help(c, argc, argv);
7034 return net_run_function(c, argc, argv, func, net_rpc_help);
7038 * 'net rpc' entrypoint.
7040 * @param c A net_context structure
7041 * @param argc Standard main() style argc
7042 * @param argv Standard main() style argv. Initial components are already
7046 int net_rpc(struct net_context *c, int argc, const char **argv)
7048 struct functable func[] = {
7049 {"audit", net_rpc_audit},
7050 {"info", net_rpc_info},
7051 {"join", net_rpc_join},
7052 {"oldjoin", net_rpc_oldjoin},
7053 {"testjoin", net_rpc_testjoin},
7054 {"user", net_rpc_user},
7055 {"password", rpc_user_password},
7056 {"group", net_rpc_group},
7057 {"share", net_rpc_share},
7058 {"file", net_rpc_file},
7059 {"printer", net_rpc_printer},
7060 {"changetrustpw", net_rpc_changetrustpw},
7061 {"trustdom", rpc_trustdom},
7062 {"abortshutdown", rpc_shutdown_abort},
7063 {"shutdown", rpc_shutdown},
7064 {"samdump", rpc_samdump},
7065 {"vampire", rpc_vampire},
7066 {"getsid", net_rpc_getsid},
7067 {"rights", net_rpc_rights},
7068 {"service", net_rpc_service},
7069 {"registry", net_rpc_registry},
7070 {"shell", net_rpc_shell},
7071 {"help", net_rpc_help},
7074 return net_run_function(c, argc, argv, func, net_rpc_usage);