2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2004,2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "utils/net.h"
26 static int net_mode_share;
27 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
32 * @brief RPC based subcommands for the 'net' utility.
34 * This file should contain much of the functionality that used to
35 * be found in rpcclient, execpt that the commands should change
36 * less often, and the fucntionality should be sane (the user is not
37 * expected to know a rid/sid before they conduct an operation etc.)
39 * @todo Perhaps eventually these should be split out into a number
40 * of files, as this could get quite big.
45 * Many of the RPC functions need the domain sid. This function gets
46 * it at the start of every run
48 * @param cli A cli_state already connected to the remote machine
50 * @return The Domain SID of the remote machine.
53 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
55 const char **domain_name)
57 struct rpc_pipe_client *lsa_pipe;
59 NTSTATUS result = NT_STATUS_OK;
60 union lsa_PolicyInformation *info = NULL;
62 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
64 if (!NT_STATUS_IS_OK(result)) {
65 d_fprintf(stderr, "Could not initialise lsa pipe\n");
69 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
70 SEC_RIGHTS_MAXIMUM_ALLOWED,
72 if (!NT_STATUS_IS_OK(result)) {
73 d_fprintf(stderr, "open_policy failed: %s\n",
78 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
80 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
82 if (!NT_STATUS_IS_OK(result)) {
83 d_fprintf(stderr, "lsaquery failed: %s\n",
88 *domain_name = info->account_domain.name.string;
89 *domain_sid = info->account_domain.sid;
91 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
92 TALLOC_FREE(lsa_pipe);
98 * Run a single RPC command, from start to finish.
100 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
101 * @param conn_flag a NET_FLAG_ combination. Passed to
102 * net_make_ipc_connection.
103 * @param argc Standard main() style argc.
104 * @param argv Standard main() style argv. Initial components are already
106 * @return A shell status integer (0 for success).
109 int run_rpc_command(struct net_context *c,
110 struct cli_state *cli_arg,
111 const struct ndr_syntax_id *interface,
117 struct cli_state *cli = NULL;
118 struct rpc_pipe_client *pipe_hnd = NULL;
122 const char *domain_name;
124 /* make use of cli_state handed over as an argument, if possible */
126 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
127 if (!NT_STATUS_IS_OK(nt_status)) {
128 DEBUG(1, ("failed to make ipc connection: %s\n",
129 nt_errstr(nt_status)));
142 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
143 DEBUG(0, ("talloc_init() failed\n"));
148 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
150 if (!NT_STATUS_IS_OK(nt_status)) {
155 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
156 if (lp_client_schannel()
157 && (ndr_syntax_id_equal(interface,
158 &ndr_table_netlogon.syntax_id))) {
159 /* Always try and create an schannel netlogon pipe. */
160 nt_status = cli_rpc_pipe_open_schannel(
162 PIPE_AUTH_LEVEL_PRIVACY, domain_name,
164 if (!NT_STATUS_IS_OK(nt_status)) {
165 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
166 nt_errstr(nt_status) ));
171 if (conn_flags & NET_FLAGS_SEAL) {
172 nt_status = cli_rpc_pipe_open_ntlmssp(
174 PIPE_AUTH_LEVEL_PRIVACY,
175 lp_workgroup(), c->opt_user_name,
176 c->opt_password, &pipe_hnd);
178 nt_status = cli_rpc_pipe_open_noauth(
182 if (!NT_STATUS_IS_OK(nt_status)) {
183 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
184 cli_get_pipe_name_from_iface(
185 debug_ctx(), cli, interface),
186 nt_errstr(nt_status) ));
193 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
195 if (!NT_STATUS_IS_OK(nt_status)) {
196 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
198 DEBUG(5, ("rpc command function succedded\n"));
201 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
203 TALLOC_FREE(pipe_hnd);
207 /* close the connection only if it was opened here */
212 talloc_destroy(mem_ctx);
213 return (!NT_STATUS_IS_OK(nt_status));
217 * Force a change of the trust acccount password.
219 * All parameters are provided by the run_rpc_command function, except for
220 * argc, argv which are passed through.
222 * @param domain_sid The domain sid acquired from the remote server.
223 * @param cli A cli_state connected to the server.
224 * @param mem_ctx Talloc context, destroyed on completion of the function.
225 * @param argc Standard main() style argc.
226 * @param argv Standard main() style argv. Initial components are already
229 * @return Normal NTSTATUS return.
232 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
233 const DOM_SID *domain_sid,
234 const char *domain_name,
235 struct cli_state *cli,
236 struct rpc_pipe_client *pipe_hnd,
242 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
246 * Force a change of the trust acccount password.
248 * @param argc Standard main() style argc.
249 * @param argv Standard main() style argv. Initial components are already
252 * @return A shell status integer (0 for success).
255 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
257 if (c->display_usage) {
259 "net rpc changetrustpw\n"
260 " Change the machine trust password\n");
264 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
265 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
266 rpc_changetrustpw_internals,
271 * Join a domain, the old way.
273 * This uses 'machinename' as the inital password, and changes it.
275 * The password should be created with 'server manager' or equiv first.
277 * All parameters are provided by the run_rpc_command function, except for
278 * argc, argv which are passed through.
280 * @param domain_sid The domain sid acquired from the remote server.
281 * @param cli A cli_state connected to the server.
282 * @param mem_ctx Talloc context, destroyed on completion of the function.
283 * @param argc Standard main() style argc.
284 * @param argv Standard main() style argv. Initial components are already
287 * @return Normal NTSTATUS return.
290 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
291 const DOM_SID *domain_sid,
292 const char *domain_name,
293 struct cli_state *cli,
294 struct rpc_pipe_client *pipe_hnd,
300 fstring trust_passwd;
301 unsigned char orig_trust_passwd_hash[16];
303 uint32 sec_channel_type;
305 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
307 if (!NT_STATUS_IS_OK(result)) {
308 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
311 nt_errstr(result) ));
316 check what type of join - if the user want's to join as
317 a BDC, the server must agree that we are a BDC.
320 sec_channel_type = get_sec_channel_type(argv[0]);
322 sec_channel_type = get_sec_channel_type(NULL);
325 fstrcpy(trust_passwd, global_myname());
326 strlower_m(trust_passwd);
329 * Machine names can be 15 characters, but the max length on
330 * a password is 14. --jerry
333 trust_passwd[14] = '\0';
335 E_md4hash(trust_passwd, orig_trust_passwd_hash);
337 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
338 orig_trust_passwd_hash,
341 if (NT_STATUS_IS_OK(result))
342 printf("Joined domain %s.\n", c->opt_target_workgroup);
345 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
346 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
347 result = NT_STATUS_UNSUCCESSFUL;
354 * Join a domain, the old way.
356 * @param argc Standard main() style argc.
357 * @param argv Standard main() style argv. Initial components are already
360 * @return A shell status integer (0 for success).
363 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
365 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
366 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
367 rpc_oldjoin_internals,
372 * Join a domain, the old way. This function exists to allow
373 * the message to be displayed when oldjoin was explicitly
374 * requested, but not when it was implied by "net rpc join".
376 * @param argc Standard main() style argc.
377 * @param argv Standard main() style argv. Initial components are already
380 * @return A shell status integer (0 for success).
383 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
387 if (c->display_usage) {
390 " Join a domain the old way\n");
394 rc = net_rpc_perform_oldjoin(c, argc, argv);
397 d_fprintf(stderr, "Failed to join domain\n");
404 * 'net rpc join' entrypoint.
405 * @param argc Standard main() style argc.
406 * @param argv Standard main() style argv. Initial components are already
409 * Main 'net_rpc_join()' (where the admin username/password is used) is
411 * Try to just change the password, but if that doesn't work, use/prompt
412 * for a username/password.
415 int net_rpc_join(struct net_context *c, int argc, const char **argv)
417 if (c->display_usage) {
419 "net rpc join -U <username>[%%password] <type>\n"
421 " username\tName of the admin user"
422 " password\tPassword of the admin user, will "
423 "prompt if not specified\n"
424 " type\tCan be one of the following:\n"
425 "\t\tMEMBER\tJoin as member server (default)\n"
426 "\t\tBDC\tJoin as BDC\n"
427 "\t\tPDC\tJoin as PDC\n");
431 if (lp_server_role() == ROLE_STANDALONE) {
432 d_printf("cannot join as standalone machine\n");
436 if (strlen(global_myname()) > 15) {
437 d_printf("Our netbios name can be at most 15 chars long, "
438 "\"%s\" is %u chars long\n",
439 global_myname(), (unsigned int)strlen(global_myname()));
443 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
446 return net_rpc_join_newstyle(c, argc, argv);
450 * display info about a rpc domain
452 * All parameters are provided by the run_rpc_command function, except for
453 * argc, argv which are passed through.
455 * @param domain_sid The domain sid acquired from the remote server
456 * @param cli A cli_state connected to the server.
457 * @param mem_ctx Talloc context, destroyed on completion of the function.
458 * @param argc Standard main() style argc.
459 * @param argv Standard main() style argv. Initial components are already
462 * @return Normal NTSTATUS return.
465 NTSTATUS rpc_info_internals(struct net_context *c,
466 const DOM_SID *domain_sid,
467 const char *domain_name,
468 struct cli_state *cli,
469 struct rpc_pipe_client *pipe_hnd,
474 POLICY_HND connect_pol, domain_pol;
475 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
476 union samr_DomainInfo *info = NULL;
479 sid_to_fstring(sid_str, domain_sid);
481 /* Get sam policy handle */
482 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
484 MAXIMUM_ALLOWED_ACCESS,
486 if (!NT_STATUS_IS_OK(result)) {
487 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
491 /* Get domain policy handle */
492 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
494 MAXIMUM_ALLOWED_ACCESS,
495 CONST_DISCARD(struct dom_sid2 *, domain_sid),
497 if (!NT_STATUS_IS_OK(result)) {
498 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
502 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
506 if (NT_STATUS_IS_OK(result)) {
507 d_printf("Domain Name: %s\n", info->info2.domain_name.string);
508 d_printf("Domain SID: %s\n", sid_str);
509 d_printf("Sequence number: %llu\n",
510 (unsigned long long)info->info2.sequence_num);
511 d_printf("Num users: %u\n", info->info2.num_users);
512 d_printf("Num domain groups: %u\n", info->info2.num_groups);
513 d_printf("Num local groups: %u\n", info->info2.num_aliases);
521 * 'net rpc info' entrypoint.
522 * @param argc Standard main() style argc.
523 * @param argv Standard main() style argv. Initial components are already
527 int net_rpc_info(struct net_context *c, int argc, const char **argv)
529 if (c->display_usage) {
532 " Display information about the domain\n");
536 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
537 NET_FLAGS_PDC, rpc_info_internals,
542 * Fetch domain SID into the local secrets.tdb.
544 * All parameters are provided by the run_rpc_command function, except for
545 * argc, argv which are passed through.
547 * @param domain_sid The domain sid acquired from the remote server.
548 * @param cli A cli_state connected to the server.
549 * @param mem_ctx Talloc context, destroyed on completion of the function.
550 * @param argc Standard main() style argc.
551 * @param argv Standard main() style argv. Initial components are already
554 * @return Normal NTSTATUS return.
557 static NTSTATUS rpc_getsid_internals(struct net_context *c,
558 const DOM_SID *domain_sid,
559 const char *domain_name,
560 struct cli_state *cli,
561 struct rpc_pipe_client *pipe_hnd,
568 sid_to_fstring(sid_str, domain_sid);
569 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
570 sid_str, domain_name);
572 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
573 DEBUG(0,("Can't store domain SID\n"));
574 return NT_STATUS_UNSUCCESSFUL;
581 * 'net rpc getsid' entrypoint.
582 * @param argc Standard main() style argc.
583 * @param argv Standard main() style argv. Initial components are already
587 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
589 if (c->display_usage) {
592 " Fetch domain SID into local secrets.tdb\n");
596 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
597 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
598 rpc_getsid_internals,
602 /****************************************************************************/
605 * Basic usage function for 'net rpc user'.
606 * @param argc Standard main() style argc.
607 * @param argv Standard main() style argv. Initial components are already
611 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
613 return net_user_usage(c, argc, argv);
617 * Add a new user to a remote RPC server.
619 * @param argc Standard main() style argc.
620 * @param argv Standard main() style argv. Initial components are already
623 * @return A shell status integer (0 for success).
626 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
628 NET_API_STATUS status;
629 struct USER_INFO_1 info1;
630 uint32_t parm_error = 0;
632 if (argc < 1 || c->display_usage) {
633 rpc_user_usage(c, argc, argv);
639 info1.usri1_name = argv[0];
641 info1.usri1_password = argv[1];
644 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
647 d_fprintf(stderr, "Failed to add user '%s' with: %s.\n",
648 argv[0], libnetapi_get_error_string(c->netapi_ctx,
652 d_printf("Added user '%s'.\n", argv[0]);
659 * Rename a user on a remote RPC server.
661 * @param argc Standard main() style argc.
662 * @param argv Standard main() style argv. Initial components are already
665 * @return A shell status integer (0 for success).
668 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
670 NET_API_STATUS status;
671 struct USER_INFO_0 u0;
672 uint32_t parm_err = 0;
674 if (argc != 2 || c->display_usage) {
675 rpc_user_usage(c, argc, argv);
679 u0.usri0_name = argv[1];
681 status = NetUserSetInfo(c->opt_host, argv[0],
682 0, (uint8_t *)&u0, &parm_err);
684 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n",
686 libnetapi_get_error_string(c->netapi_ctx, status));
688 d_printf("Renamed user from %s to %s\n", argv[0], argv[1]);
695 * Delete a user from a remote RPC server.
697 * @param argc Standard main() style argc.
698 * @param argv Standard main() style argv. Initial components are already
701 * @return A shell status integer (0 for success).
704 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
706 NET_API_STATUS status;
708 if (argc < 1 || c->display_usage) {
709 rpc_user_usage(c, argc, argv);
713 status = NetUserDel(c->opt_host, argv[0]);
716 d_fprintf(stderr, "Failed to delete user '%s' with: %s.\n",
718 libnetapi_get_error_string(c->netapi_ctx, status));
721 d_printf("Deleted user '%s'.\n", argv[0]);
728 * Set a password for a user on a remote RPC server.
730 * All parameters are provided by the run_rpc_command function, except for
731 * argc, argv which are passed through.
733 * @param domain_sid The domain sid acquired from the remote server.
734 * @param cli A cli_state connected to the server.
735 * @param mem_ctx Talloc context, destroyed on completion of the function.
736 * @param argc Standard main() style argc.
737 * @param argv Standard main() style argv. Initial components are already
740 * @return Normal NTSTATUS return.
743 static NTSTATUS rpc_user_password_internals(struct net_context *c,
744 const DOM_SID *domain_sid,
745 const char *domain_name,
746 struct cli_state *cli,
747 struct rpc_pipe_client *pipe_hnd,
752 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
753 POLICY_HND connect_pol, domain_pol, user_pol;
755 const char *new_password;
757 union samr_UserInfo info;
758 struct samr_CryptPassword crypt_pwd;
760 if (argc < 1 || c->display_usage) {
761 rpc_user_usage(c, argc, argv);
768 new_password = argv[1];
770 asprintf(&prompt, "Enter new password for %s:", user);
771 new_password = getpass(prompt);
775 /* Get sam policy and domain handles */
777 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
779 MAXIMUM_ALLOWED_ACCESS,
782 if (!NT_STATUS_IS_OK(result)) {
786 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
788 MAXIMUM_ALLOWED_ACCESS,
789 CONST_DISCARD(struct dom_sid2 *, domain_sid),
792 if (!NT_STATUS_IS_OK(result)) {
796 /* Get handle on user */
799 struct samr_Ids user_rids, name_types;
800 struct lsa_String lsa_acct_name;
802 init_lsa_String(&lsa_acct_name, user);
804 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
810 if (!NT_STATUS_IS_OK(result)) {
814 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
816 MAXIMUM_ALLOWED_ACCESS,
820 if (!NT_STATUS_IS_OK(result)) {
825 /* Set password on account */
827 init_samr_CryptPassword(new_password,
828 &cli->user_session_key,
831 init_samr_user_info24(&info.info24, crypt_pwd.data, 24);
833 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
838 if (!NT_STATUS_IS_OK(result)) {
842 /* Display results */
850 * Set a user's password on a remote RPC server.
852 * @param argc Standard main() style argc.
853 * @param argv Standard main() style argv. Initial components are already
856 * @return A shell status integer (0 for success).
859 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
861 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
862 rpc_user_password_internals, argc, argv);
866 * List user's groups on a remote RPC server.
868 * All parameters are provided by the run_rpc_command function, except for
869 * argc, argv which are passed through.
871 * @param domain_sid The domain sid acquired from the remote server.
872 * @param cli A cli_state connected to the server.
873 * @param mem_ctx Talloc context, destroyed on completion of the function.
874 * @param argc Standard main() style argc.
875 * @param argv Standard main() style argv. Initial components are already
878 * @return Normal NTSTATUS return.
881 static NTSTATUS rpc_user_info_internals(struct net_context *c,
882 const DOM_SID *domain_sid,
883 const char *domain_name,
884 struct cli_state *cli,
885 struct rpc_pipe_client *pipe_hnd,
890 POLICY_HND connect_pol, domain_pol, user_pol;
891 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
893 struct samr_RidWithAttributeArray *rid_array = NULL;
894 struct lsa_Strings names;
895 struct samr_Ids types;
896 uint32_t *lrids = NULL;
897 struct samr_Ids rids, name_types;
898 struct lsa_String lsa_acct_name;
901 if (argc < 1 || c->display_usage) {
902 rpc_user_usage(c, argc, argv);
905 /* Get sam policy handle */
907 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
909 MAXIMUM_ALLOWED_ACCESS,
911 if (!NT_STATUS_IS_OK(result)) goto done;
913 /* Get domain policy handle */
915 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
917 MAXIMUM_ALLOWED_ACCESS,
918 CONST_DISCARD(struct dom_sid2 *, domain_sid),
920 if (!NT_STATUS_IS_OK(result)) goto done;
922 /* Get handle on user */
924 init_lsa_String(&lsa_acct_name, argv[0]);
926 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
933 if (!NT_STATUS_IS_OK(result)) goto done;
935 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
937 MAXIMUM_ALLOWED_ACCESS,
940 if (!NT_STATUS_IS_OK(result)) goto done;
942 result = rpccli_samr_GetGroupsForUser(pipe_hnd, mem_ctx,
946 if (!NT_STATUS_IS_OK(result)) goto done;
950 if (rid_array->count) {
951 if ((lrids = TALLOC_ARRAY(mem_ctx, uint32, rid_array->count)) == NULL) {
952 result = NT_STATUS_NO_MEMORY;
956 for (i = 0; i < rid_array->count; i++)
957 lrids[i] = rid_array->rids[i].rid;
959 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
966 if (!NT_STATUS_IS_OK(result)) {
970 /* Display results */
972 for (i = 0; i < names.count; i++)
973 printf("%s\n", names.names[i].string);
980 * List a user's groups from a remote RPC server.
982 * @param argc Standard main() style argc.
983 * @param argv Standard main() style argv. Initial components are already
986 * @return A shell status integer (0 for success)
989 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
991 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
992 rpc_user_info_internals, argc, argv);
996 * List users on a remote RPC server.
998 * All parameters are provided by the run_rpc_command function, except for
999 * argc, argv which are passed through.
1001 * @param domain_sid The domain sid acquired from the remote server.
1002 * @param cli A cli_state connected to the server.
1003 * @param mem_ctx Talloc context, destroyed on completion of the function.
1004 * @param argc Standard main() style argc.
1005 * @param argv Standard main() style argv. Initial components are already
1008 * @return Normal NTSTATUS return.
1011 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
1013 NET_API_STATUS status;
1014 uint32_t start_idx=0, num_entries, i, loop_count = 0;
1015 struct NET_DISPLAY_USER *info = NULL;
1016 void *buffer = NULL;
1018 /* Query domain users */
1019 if (c->opt_long_list_entries)
1020 d_printf("\nUser name Comment"
1021 "\n-----------------------------\n");
1023 uint32_t max_entries, max_size;
1025 get_query_dispinfo_params(
1026 loop_count, &max_entries, &max_size);
1028 status = NetQueryDisplayInformation(c->opt_host,
1035 if (status != 0 && status != ERROR_MORE_DATA) {
1039 info = (struct NET_DISPLAY_USER *)buffer;
1041 for (i = 0; i < num_entries; i++) {
1043 if (c->opt_long_list_entries)
1044 printf("%-21.21s %s\n", info->usri1_name,
1045 info->usri1_comment);
1047 printf("%s\n", info->usri1_name);
1051 NetApiBufferFree(buffer);
1054 start_idx += num_entries;
1056 } while (status == ERROR_MORE_DATA);
1062 * 'net rpc user' entrypoint.
1063 * @param argc Standard main() style argc.
1064 * @param argv Standard main() style argv. Initial components are already
1068 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1070 NET_API_STATUS status;
1072 struct functable func[] = {
1077 "Add specified user",
1078 "net rpc user add\n"
1079 " Add specified user"
1085 "List domain groups of user",
1086 "net rpc user info\n"
1087 " Lis domain groups of user"
1093 "Remove specified user",
1094 "net rpc user delete\n"
1095 " Remove specified user"
1101 "Change user password",
1102 "net rpc user password\n"
1103 " Change user password"
1109 "Rename specified user",
1110 "net rpc user rename\n"
1111 " Rename specified user"
1113 {NULL, NULL, 0, NULL, NULL}
1116 status = libnetapi_init(&c->netapi_ctx);
1120 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1121 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1122 if (c->opt_kerberos) {
1123 libnetapi_set_use_kerberos(c->netapi_ctx);
1127 if (c->display_usage) {
1128 d_printf("Usage:\n");
1129 d_printf("net rpc user\n"
1130 " List all users\n");
1131 net_display_usage_from_functable(func);
1135 return rpc_user_list(c, argc, argv);
1138 return net_run_function(c, argc, argv, "net rpc user", func);
1141 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1142 TALLOC_CTX *mem_ctx,
1143 struct rpc_sh_ctx *ctx,
1144 struct rpc_pipe_client *pipe_hnd,
1145 int argc, const char **argv)
1147 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
1150 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1151 TALLOC_CTX *mem_ctx,
1152 struct rpc_sh_ctx *ctx,
1153 struct rpc_pipe_client *pipe_hnd,
1154 int argc, const char **argv)
1156 return rpc_user_info_internals(c, ctx->domain_sid, ctx->domain_name,
1157 ctx->cli, pipe_hnd, mem_ctx,
1161 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1162 TALLOC_CTX *mem_ctx,
1163 struct rpc_sh_ctx *ctx,
1164 struct rpc_pipe_client *pipe_hnd,
1165 int argc, const char **argv,
1167 struct net_context *c,
1168 TALLOC_CTX *mem_ctx,
1169 struct rpc_sh_ctx *ctx,
1170 struct rpc_pipe_client *pipe_hnd,
1171 POLICY_HND *user_hnd,
1172 int argc, const char **argv))
1174 POLICY_HND connect_pol, domain_pol, user_pol;
1175 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1178 enum lsa_SidType type;
1181 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1182 return NT_STATUS_INVALID_PARAMETER;
1185 ZERO_STRUCT(connect_pol);
1186 ZERO_STRUCT(domain_pol);
1187 ZERO_STRUCT(user_pol);
1189 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1190 argv[0], NULL, NULL, &sid, &type);
1191 if (!NT_STATUS_IS_OK(result)) {
1192 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1197 if (type != SID_NAME_USER) {
1198 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1199 sid_type_lookup(type));
1200 result = NT_STATUS_NO_SUCH_USER;
1204 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1205 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1206 result = NT_STATUS_NO_SUCH_USER;
1210 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1212 MAXIMUM_ALLOWED_ACCESS,
1214 if (!NT_STATUS_IS_OK(result)) {
1218 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1220 MAXIMUM_ALLOWED_ACCESS,
1223 if (!NT_STATUS_IS_OK(result)) {
1227 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1229 MAXIMUM_ALLOWED_ACCESS,
1232 if (!NT_STATUS_IS_OK(result)) {
1236 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1239 if (is_valid_policy_hnd(&user_pol)) {
1240 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1242 if (is_valid_policy_hnd(&domain_pol)) {
1243 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1245 if (is_valid_policy_hnd(&connect_pol)) {
1246 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1251 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1252 TALLOC_CTX *mem_ctx,
1253 struct rpc_sh_ctx *ctx,
1254 struct rpc_pipe_client *pipe_hnd,
1255 POLICY_HND *user_hnd,
1256 int argc, const char **argv)
1259 union samr_UserInfo *info = NULL;
1262 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1263 return NT_STATUS_INVALID_PARAMETER;
1266 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1270 if (!NT_STATUS_IS_OK(result)) {
1274 d_printf("user rid: %d, group rid: %d\n",
1276 info->info21.primary_gid);
1281 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1282 TALLOC_CTX *mem_ctx,
1283 struct rpc_sh_ctx *ctx,
1284 struct rpc_pipe_client *pipe_hnd,
1285 int argc, const char **argv)
1287 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1288 rpc_sh_user_show_internals);
1291 #define FETCHSTR(name, rec) \
1292 do { if (strequal(ctx->thiscmd, name)) { \
1293 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1296 #define SETSTR(name, rec, flag) \
1297 do { if (strequal(ctx->thiscmd, name)) { \
1298 init_lsa_String(&(info->info21.rec), argv[0]); \
1299 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1302 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1303 TALLOC_CTX *mem_ctx,
1304 struct rpc_sh_ctx *ctx,
1305 struct rpc_pipe_client *pipe_hnd,
1306 POLICY_HND *user_hnd,
1307 int argc, const char **argv)
1310 const char *username;
1311 const char *oldval = "";
1312 union samr_UserInfo *info = NULL;
1315 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1317 return NT_STATUS_INVALID_PARAMETER;
1320 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1324 if (!NT_STATUS_IS_OK(result)) {
1328 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1330 FETCHSTR("fullname", full_name);
1331 FETCHSTR("homedir", home_directory);
1332 FETCHSTR("homedrive", home_drive);
1333 FETCHSTR("logonscript", logon_script);
1334 FETCHSTR("profilepath", profile_path);
1335 FETCHSTR("description", description);
1338 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1342 if (strcmp(argv[0], "NULL") == 0) {
1346 ZERO_STRUCT(info->info21);
1348 SETSTR("fullname", full_name, FULL_NAME);
1349 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1350 SETSTR("homedrive", home_drive, HOME_DRIVE);
1351 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1352 SETSTR("profilepath", profile_path, PROFILE_PATH);
1353 SETSTR("description", description, DESCRIPTION);
1355 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1360 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1361 ctx->thiscmd, oldval, argv[0]);
1368 #define HANDLEFLG(name, rec) \
1369 do { if (strequal(ctx->thiscmd, name)) { \
1370 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1372 newflags = oldflags | ACB_##rec; \
1374 newflags = oldflags & ~ACB_##rec; \
1377 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1378 TALLOC_CTX *mem_ctx,
1379 struct rpc_sh_ctx *ctx,
1380 struct rpc_pipe_client *pipe_hnd,
1381 int argc, const char **argv)
1383 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1384 rpc_sh_user_str_edit_internals);
1387 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1388 TALLOC_CTX *mem_ctx,
1389 struct rpc_sh_ctx *ctx,
1390 struct rpc_pipe_client *pipe_hnd,
1391 POLICY_HND *user_hnd,
1392 int argc, const char **argv)
1395 const char *username;
1396 const char *oldval = "unknown";
1397 uint32 oldflags, newflags;
1399 union samr_UserInfo *info = NULL;
1402 ((argc == 1) && !strequal(argv[0], "yes") &&
1403 !strequal(argv[0], "no"))) {
1404 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1406 return NT_STATUS_INVALID_PARAMETER;
1409 newval = strequal(argv[0], "yes");
1411 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1415 if (!NT_STATUS_IS_OK(result)) {
1419 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1420 oldflags = info->info21.acct_flags;
1421 newflags = info->info21.acct_flags;
1423 HANDLEFLG("disabled", DISABLED);
1424 HANDLEFLG("pwnotreq", PWNOTREQ);
1425 HANDLEFLG("autolock", AUTOLOCK);
1426 HANDLEFLG("pwnoexp", PWNOEXP);
1429 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1433 ZERO_STRUCT(info->info21);
1435 info->info21.acct_flags = newflags;
1436 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1438 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1443 if (NT_STATUS_IS_OK(result)) {
1444 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1445 ctx->thiscmd, oldval, argv[0]);
1453 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1454 TALLOC_CTX *mem_ctx,
1455 struct rpc_sh_ctx *ctx,
1456 struct rpc_pipe_client *pipe_hnd,
1457 int argc, const char **argv)
1459 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1460 rpc_sh_user_flag_edit_internals);
1463 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1464 TALLOC_CTX *mem_ctx,
1465 struct rpc_sh_ctx *ctx)
1467 static struct rpc_sh_cmd cmds[] = {
1469 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1470 "Show/Set a user's full name" },
1472 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1473 "Show/Set a user's home directory" },
1475 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1476 "Show/Set a user's home drive" },
1478 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1479 "Show/Set a user's logon script" },
1481 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1482 "Show/Set a user's profile path" },
1484 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1485 "Show/Set a user's description" },
1487 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1488 "Show/Set whether a user is disabled" },
1490 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1491 "Show/Set whether a user locked out" },
1493 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1494 "Show/Set whether a user does not need a password" },
1496 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1497 "Show/Set whether a user's password does not expire" },
1499 { NULL, NULL, 0, NULL, NULL }
1505 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1506 TALLOC_CTX *mem_ctx,
1507 struct rpc_sh_ctx *ctx)
1509 static struct rpc_sh_cmd cmds[] = {
1511 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1512 "List available users" },
1514 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1515 "List the domain groups a user is member of" },
1517 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1518 "Show info about a user" },
1520 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1521 "Show/Modify a user's fields" },
1523 { NULL, NULL, 0, NULL, NULL }
1529 /****************************************************************************/
1532 * Basic usage function for 'net rpc group'.
1533 * @param argc Standard main() style argc.
1534 * @param argv Standard main() style argv. Initial components are already
1538 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1540 return net_group_usage(c, argc, argv);
1544 * Delete group on a remote RPC server.
1546 * All parameters are provided by the run_rpc_command function, except for
1547 * argc, argv which are passed through.
1549 * @param domain_sid The domain sid acquired from the remote server.
1550 * @param cli A cli_state connected to the server.
1551 * @param mem_ctx Talloc context, destroyed on completion of the function.
1552 * @param argc Standard main() style argc.
1553 * @param argv Standard main() style argv. Initial components are already
1556 * @return Normal NTSTATUS return.
1559 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1560 const DOM_SID *domain_sid,
1561 const char *domain_name,
1562 struct cli_state *cli,
1563 struct rpc_pipe_client *pipe_hnd,
1564 TALLOC_CTX *mem_ctx,
1568 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1569 bool group_is_primary = false;
1570 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1572 struct samr_RidTypeArray *rids = NULL;
1575 /* DOM_GID *user_gids; */
1577 struct samr_Ids group_rids, name_types;
1578 struct lsa_String lsa_acct_name;
1579 union samr_UserInfo *info = NULL;
1581 if (argc < 1 || c->display_usage) {
1582 rpc_group_usage(c, argc,argv);
1583 return NT_STATUS_OK; /* ok? */
1586 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1588 MAXIMUM_ALLOWED_ACCESS,
1591 if (!NT_STATUS_IS_OK(result)) {
1592 d_fprintf(stderr, "Request samr_Connect2 failed\n");
1596 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1598 MAXIMUM_ALLOWED_ACCESS,
1599 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1602 if (!NT_STATUS_IS_OK(result)) {
1603 d_fprintf(stderr, "Request open_domain failed\n");
1607 init_lsa_String(&lsa_acct_name, argv[0]);
1609 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1615 if (!NT_STATUS_IS_OK(result)) {
1616 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1620 switch (name_types.ids[0])
1622 case SID_NAME_DOM_GRP:
1623 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1625 MAXIMUM_ALLOWED_ACCESS,
1628 if (!NT_STATUS_IS_OK(result)) {
1629 d_fprintf(stderr, "Request open_group failed");
1633 group_rid = group_rids.ids[0];
1635 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1639 if (!NT_STATUS_IS_OK(result)) {
1640 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1644 if (c->opt_verbose) {
1645 d_printf("Domain Group %s (rid: %d) has %d members\n",
1646 argv[0],group_rid, rids->count);
1649 /* Check if group is anyone's primary group */
1650 for (i = 0; i < rids->count; i++)
1652 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1654 MAXIMUM_ALLOWED_ACCESS,
1658 if (!NT_STATUS_IS_OK(result)) {
1659 d_fprintf(stderr, "Unable to open group member %d\n",
1664 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1669 if (!NT_STATUS_IS_OK(result)) {
1670 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",
1675 if (info->info21.primary_gid == group_rid) {
1676 if (c->opt_verbose) {
1677 d_printf("Group is primary group of %s\n",
1678 info->info21.account_name.string);
1680 group_is_primary = true;
1683 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1686 if (group_is_primary) {
1687 d_fprintf(stderr, "Unable to delete group because some "
1688 "of it's members have it as primary group\n");
1689 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1693 /* remove all group members */
1694 for (i = 0; i < rids->count; i++)
1697 d_printf("Remove group member %d...",
1699 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1703 if (NT_STATUS_IS_OK(result)) {
1708 d_printf("failed\n");
1713 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1717 /* removing a local group is easier... */
1718 case SID_NAME_ALIAS:
1719 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1721 MAXIMUM_ALLOWED_ACCESS,
1725 if (!NT_STATUS_IS_OK(result)) {
1726 d_fprintf(stderr, "Request open_alias failed\n");
1730 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1734 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1735 argv[0],sid_type_lookup(name_types.ids[0]));
1736 result = NT_STATUS_UNSUCCESSFUL;
1740 if (NT_STATUS_IS_OK(result)) {
1742 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types.ids[0]),argv[0]);
1744 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1745 get_friendly_nt_error_msg(result));
1753 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1755 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1756 rpc_group_delete_internals, argc,argv);
1759 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1761 NET_API_STATUS status;
1762 struct GROUP_INFO_1 info1;
1763 uint32_t parm_error = 0;
1765 if (argc != 1 || c->display_usage) {
1766 rpc_group_usage(c, argc, argv);
1772 info1.grpi1_name = argv[0];
1773 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1774 info1.grpi1_comment = c->opt_comment;
1777 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1780 d_fprintf(stderr, "Failed to add group '%s' with: %s.\n",
1781 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1785 d_printf("Added group '%s'.\n", argv[0]);
1791 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1793 NET_API_STATUS status;
1794 struct LOCALGROUP_INFO_1 info1;
1795 uint32_t parm_error = 0;
1797 if (argc != 1 || c->display_usage) {
1798 rpc_group_usage(c, argc, argv);
1804 info1.lgrpi1_name = argv[0];
1805 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1806 info1.lgrpi1_comment = c->opt_comment;
1809 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1812 d_fprintf(stderr, "Failed to add alias '%s' with: %s.\n",
1813 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1817 d_printf("Added alias '%s'.\n", argv[0]);
1823 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1825 if (c->opt_localgroup)
1826 return rpc_alias_add_internals(c, argc, argv);
1828 return rpc_group_add_internals(c, argc, argv);
1831 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1832 TALLOC_CTX *mem_ctx,
1835 enum lsa_SidType *type)
1837 DOM_SID *sids = NULL;
1838 enum lsa_SidType *types = NULL;
1839 struct rpc_pipe_client *pipe_hnd;
1841 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1843 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1845 if (!NT_STATUS_IS_OK(result)) {
1849 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1850 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1852 if (!NT_STATUS_IS_OK(result)) {
1856 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1857 &name, NULL, 1, &sids, &types);
1859 if (NT_STATUS_IS_OK(result)) {
1860 sid_copy(sid, &sids[0]);
1864 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1868 TALLOC_FREE(pipe_hnd);
1871 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1873 /* Try as S-1-5-whatever */
1877 if (string_to_sid(&tmp_sid, name)) {
1878 sid_copy(sid, &tmp_sid);
1879 *type = SID_NAME_UNKNOWN;
1880 result = NT_STATUS_OK;
1887 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1888 TALLOC_CTX *mem_ctx,
1889 const DOM_SID *group_sid,
1892 POLICY_HND connect_pol, domain_pol;
1895 POLICY_HND group_pol;
1897 struct samr_Ids rids, rid_types;
1898 struct lsa_String lsa_acct_name;
1902 sid_copy(&sid, group_sid);
1904 if (!sid_split_rid(&sid, &group_rid)) {
1905 return NT_STATUS_UNSUCCESSFUL;
1908 /* Get sam policy handle */
1909 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1911 MAXIMUM_ALLOWED_ACCESS,
1913 if (!NT_STATUS_IS_OK(result)) {
1917 /* Get domain policy handle */
1918 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1920 MAXIMUM_ALLOWED_ACCESS,
1923 if (!NT_STATUS_IS_OK(result)) {
1927 init_lsa_String(&lsa_acct_name, member);
1929 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1936 if (!NT_STATUS_IS_OK(result)) {
1937 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1941 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1943 MAXIMUM_ALLOWED_ACCESS,
1947 if (!NT_STATUS_IS_OK(result)) {
1951 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1954 0x0005); /* unknown flags */
1957 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1961 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1962 TALLOC_CTX *mem_ctx,
1963 const DOM_SID *alias_sid,
1966 POLICY_HND connect_pol, domain_pol;
1969 POLICY_HND alias_pol;
1972 enum lsa_SidType member_type;
1976 sid_copy(&sid, alias_sid);
1978 if (!sid_split_rid(&sid, &alias_rid)) {
1979 return NT_STATUS_UNSUCCESSFUL;
1982 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1983 member, &member_sid, &member_type);
1985 if (!NT_STATUS_IS_OK(result)) {
1986 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1990 /* Get sam policy handle */
1991 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1993 MAXIMUM_ALLOWED_ACCESS,
1995 if (!NT_STATUS_IS_OK(result)) {
1999 /* Get domain policy handle */
2000 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2002 MAXIMUM_ALLOWED_ACCESS,
2005 if (!NT_STATUS_IS_OK(result)) {
2009 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2011 MAXIMUM_ALLOWED_ACCESS,
2015 if (!NT_STATUS_IS_OK(result)) {
2019 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
2023 if (!NT_STATUS_IS_OK(result)) {
2028 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2032 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2033 const DOM_SID *domain_sid,
2034 const char *domain_name,
2035 struct cli_state *cli,
2036 struct rpc_pipe_client *pipe_hnd,
2037 TALLOC_CTX *mem_ctx,
2042 enum lsa_SidType group_type;
2044 if (argc != 2 || c->display_usage) {
2046 "net rpc group addmem <group> <member>\n"
2047 " Add a member to a group\n"
2048 " group\tGroup to add member to\n"
2049 " member\tMember to add to group\n");
2050 return NT_STATUS_UNSUCCESSFUL;
2053 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2054 &group_sid, &group_type))) {
2055 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2056 return NT_STATUS_UNSUCCESSFUL;
2059 if (group_type == SID_NAME_DOM_GRP) {
2060 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2061 &group_sid, argv[1]);
2063 if (!NT_STATUS_IS_OK(result)) {
2064 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2065 argv[1], argv[0], nt_errstr(result));
2070 if (group_type == SID_NAME_ALIAS) {
2071 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2072 &group_sid, argv[1]);
2074 if (!NT_STATUS_IS_OK(result)) {
2075 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2076 argv[1], argv[0], nt_errstr(result));
2081 d_fprintf(stderr, "Can only add members to global or local groups "
2082 "which %s is not\n", argv[0]);
2084 return NT_STATUS_UNSUCCESSFUL;
2087 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2089 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2090 rpc_group_addmem_internals,
2094 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2095 struct rpc_pipe_client *pipe_hnd,
2096 TALLOC_CTX *mem_ctx,
2097 const DOM_SID *group_sid,
2100 POLICY_HND connect_pol, domain_pol;
2103 POLICY_HND group_pol;
2105 struct samr_Ids rids, rid_types;
2106 struct lsa_String lsa_acct_name;
2110 sid_copy(&sid, group_sid);
2112 if (!sid_split_rid(&sid, &group_rid))
2113 return NT_STATUS_UNSUCCESSFUL;
2115 /* Get sam policy handle */
2116 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2118 MAXIMUM_ALLOWED_ACCESS,
2120 if (!NT_STATUS_IS_OK(result))
2123 /* Get domain policy handle */
2124 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2126 MAXIMUM_ALLOWED_ACCESS,
2129 if (!NT_STATUS_IS_OK(result))
2132 init_lsa_String(&lsa_acct_name, member);
2134 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2140 if (!NT_STATUS_IS_OK(result)) {
2141 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2145 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2147 MAXIMUM_ALLOWED_ACCESS,
2151 if (!NT_STATUS_IS_OK(result))
2154 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2159 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2163 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2164 TALLOC_CTX *mem_ctx,
2165 const DOM_SID *alias_sid,
2168 POLICY_HND connect_pol, domain_pol;
2171 POLICY_HND alias_pol;
2174 enum lsa_SidType member_type;
2178 sid_copy(&sid, alias_sid);
2180 if (!sid_split_rid(&sid, &alias_rid))
2181 return NT_STATUS_UNSUCCESSFUL;
2183 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2184 member, &member_sid, &member_type);
2186 if (!NT_STATUS_IS_OK(result)) {
2187 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2191 /* Get sam policy handle */
2192 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2194 MAXIMUM_ALLOWED_ACCESS,
2196 if (!NT_STATUS_IS_OK(result)) {
2200 /* Get domain policy handle */
2201 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2203 MAXIMUM_ALLOWED_ACCESS,
2206 if (!NT_STATUS_IS_OK(result)) {
2210 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2212 MAXIMUM_ALLOWED_ACCESS,
2216 if (!NT_STATUS_IS_OK(result))
2219 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2223 if (!NT_STATUS_IS_OK(result))
2227 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2231 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2232 const DOM_SID *domain_sid,
2233 const char *domain_name,
2234 struct cli_state *cli,
2235 struct rpc_pipe_client *pipe_hnd,
2236 TALLOC_CTX *mem_ctx,
2241 enum lsa_SidType group_type;
2243 if (argc != 2 || c->display_usage) {
2245 "net rpc group delmem <group> <member>\n"
2246 " Delete a member from a group\n"
2247 " group\tGroup to delete member from\n"
2248 " member\tMember to delete from group\n");
2249 return NT_STATUS_UNSUCCESSFUL;
2252 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2253 &group_sid, &group_type))) {
2254 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2255 return NT_STATUS_UNSUCCESSFUL;
2258 if (group_type == SID_NAME_DOM_GRP) {
2259 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2260 &group_sid, argv[1]);
2262 if (!NT_STATUS_IS_OK(result)) {
2263 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2264 argv[1], argv[0], nt_errstr(result));
2269 if (group_type == SID_NAME_ALIAS) {
2270 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2271 &group_sid, argv[1]);
2273 if (!NT_STATUS_IS_OK(result)) {
2274 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2275 argv[1], argv[0], nt_errstr(result));
2280 d_fprintf(stderr, "Can only delete members from global or local groups "
2281 "which %s is not\n", argv[0]);
2283 return NT_STATUS_UNSUCCESSFUL;
2286 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2288 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2289 rpc_group_delmem_internals,
2294 * List groups on a remote RPC server.
2296 * All parameters are provided by the run_rpc_command function, except for
2297 * argc, argv which are passes through.
2299 * @param domain_sid The domain sid acquired from the remote server.
2300 * @param cli A cli_state connected to the server.
2301 * @param mem_ctx Talloc context, destroyed on completion of the function.
2302 * @param argc Standard main() style argc.
2303 * @param argv Standard main() style argv. Initial components are already
2306 * @return Normal NTSTATUS return.
2309 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2310 const DOM_SID *domain_sid,
2311 const char *domain_name,
2312 struct cli_state *cli,
2313 struct rpc_pipe_client *pipe_hnd,
2314 TALLOC_CTX *mem_ctx,
2318 POLICY_HND connect_pol, domain_pol;
2319 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2320 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2321 struct samr_SamArray *groups = NULL;
2322 bool global = false;
2324 bool builtin = false;
2326 if (c->display_usage) {
2328 "net rpc group list [global] [local] [builtin]\n"
2329 " List groups on RPC server\n"
2330 " global\tList global groups\n"
2331 " local\tList local groups\n"
2332 " builtin\tList builtin groups\n"
2333 " If none of global, local or builtin is "
2334 "specified, all three options are considered set\n");
2335 return NT_STATUS_OK;
2344 for (i=0; i<argc; i++) {
2345 if (strequal(argv[i], "global"))
2348 if (strequal(argv[i], "local"))
2351 if (strequal(argv[i], "builtin"))
2355 /* Get sam policy handle */
2357 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2359 MAXIMUM_ALLOWED_ACCESS,
2361 if (!NT_STATUS_IS_OK(result)) {
2365 /* Get domain policy handle */
2367 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2369 MAXIMUM_ALLOWED_ACCESS,
2370 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2372 if (!NT_STATUS_IS_OK(result)) {
2376 /* Query domain groups */
2377 if (c->opt_long_list_entries)
2378 d_printf("\nGroup name Comment"
2379 "\n-----------------------------\n");
2381 uint32_t max_size, total_size, returned_size;
2382 union samr_DispInfo info;
2386 get_query_dispinfo_params(
2387 loop_count, &max_entries, &max_size);
2389 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2398 num_entries = info.info3.count;
2399 start_idx += info.info3.count;
2401 if (!NT_STATUS_IS_OK(result) &&
2402 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2405 for (i = 0; i < num_entries; i++) {
2407 const char *group = NULL;
2408 const char *desc = NULL;
2410 group = info.info3.entries[i].account_name.string;
2411 desc = info.info3.entries[i].description.string;
2413 if (c->opt_long_list_entries)
2414 printf("%-21.21s %-50.50s\n",
2417 printf("%s\n", group);
2419 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2420 /* query domain aliases */
2425 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2431 if (!NT_STATUS_IS_OK(result) &&
2432 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2435 for (i = 0; i < num_entries; i++) {
2437 const char *description = NULL;
2439 if (c->opt_long_list_entries) {
2441 POLICY_HND alias_pol;
2442 union samr_AliasInfo *info = NULL;
2444 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2447 groups->entries[i].idx,
2449 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2453 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2455 description = info->description.string;
2459 if (description != NULL) {
2460 printf("%-21.21s %-50.50s\n",
2461 groups->entries[i].name.string,
2464 printf("%s\n", groups->entries[i].name.string);
2467 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2468 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2469 /* Get builtin policy handle */
2471 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2473 MAXIMUM_ALLOWED_ACCESS,
2474 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2476 if (!NT_STATUS_IS_OK(result)) {
2479 /* query builtin aliases */
2482 if (!builtin) break;
2484 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2490 if (!NT_STATUS_IS_OK(result) &&
2491 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2494 for (i = 0; i < num_entries; i++) {
2496 const char *description = NULL;
2498 if (c->opt_long_list_entries) {
2500 POLICY_HND alias_pol;
2501 union samr_AliasInfo *info = NULL;
2503 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2506 groups->entries[i].idx,
2508 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2512 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2514 description = info->description.string;
2518 if (description != NULL) {
2519 printf("%-21.21s %-50.50s\n",
2520 groups->entries[i].name.string,
2523 printf("%s\n", groups->entries[i].name.string);
2526 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2532 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2534 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2535 rpc_group_list_internals,
2539 static NTSTATUS rpc_list_group_members(struct net_context *c,
2540 struct rpc_pipe_client *pipe_hnd,
2541 TALLOC_CTX *mem_ctx,
2542 const char *domain_name,
2543 const DOM_SID *domain_sid,
2544 POLICY_HND *domain_pol,
2548 POLICY_HND group_pol;
2549 uint32 num_members, *group_rids;
2551 struct samr_RidTypeArray *rids = NULL;
2552 struct lsa_Strings names;
2553 struct samr_Ids types;
2556 sid_to_fstring(sid_str, domain_sid);
2558 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2560 MAXIMUM_ALLOWED_ACCESS,
2564 if (!NT_STATUS_IS_OK(result))
2567 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2571 if (!NT_STATUS_IS_OK(result))
2574 num_members = rids->count;
2575 group_rids = rids->rids;
2577 while (num_members > 0) {
2578 int this_time = 512;
2580 if (num_members < this_time)
2581 this_time = num_members;
2583 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2590 if (!NT_STATUS_IS_OK(result))
2593 /* We only have users as members, but make the output
2594 the same as the output of alias members */
2596 for (i = 0; i < this_time; i++) {
2598 if (c->opt_long_list_entries) {
2599 printf("%s-%d %s\\%s %d\n", sid_str,
2600 group_rids[i], domain_name,
2601 names.names[i].string,
2604 printf("%s\\%s\n", domain_name,
2605 names.names[i].string);
2609 num_members -= this_time;
2613 return NT_STATUS_OK;
2616 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2617 struct rpc_pipe_client *pipe_hnd,
2618 TALLOC_CTX *mem_ctx,
2619 POLICY_HND *domain_pol,
2623 struct rpc_pipe_client *lsa_pipe;
2624 POLICY_HND alias_pol, lsa_pol;
2626 DOM_SID *alias_sids;
2629 enum lsa_SidType *types;
2631 struct lsa_SidArray sid_array;
2633 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2635 MAXIMUM_ALLOWED_ACCESS,
2639 if (!NT_STATUS_IS_OK(result))
2642 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2646 if (!NT_STATUS_IS_OK(result)) {
2647 d_fprintf(stderr, "Couldn't list alias members\n");
2651 num_members = sid_array.num_sids;
2653 if (num_members == 0) {
2654 return NT_STATUS_OK;
2657 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2658 &ndr_table_lsarpc.syntax_id,
2660 if (!NT_STATUS_IS_OK(result)) {
2661 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2662 nt_errstr(result) );
2666 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2667 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2669 if (!NT_STATUS_IS_OK(result)) {
2670 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2671 TALLOC_FREE(lsa_pipe);
2675 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2677 d_fprintf(stderr, "Out of memory\n");
2678 TALLOC_FREE(lsa_pipe);
2679 return NT_STATUS_NO_MEMORY;
2682 for (i=0; i<num_members; i++) {
2683 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2686 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2687 num_members, alias_sids,
2688 &domains, &names, &types);
2690 if (!NT_STATUS_IS_OK(result) &&
2691 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2692 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2693 TALLOC_FREE(lsa_pipe);
2697 for (i = 0; i < num_members; i++) {
2699 sid_to_fstring(sid_str, &alias_sids[i]);
2701 if (c->opt_long_list_entries) {
2702 printf("%s %s\\%s %d\n", sid_str,
2703 domains[i] ? domains[i] : "*unknown*",
2704 names[i] ? names[i] : "*unknown*", types[i]);
2707 printf("%s\\%s\n", domains[i], names[i]);
2709 printf("%s\n", sid_str);
2713 TALLOC_FREE(lsa_pipe);
2714 return NT_STATUS_OK;
2717 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2718 const DOM_SID *domain_sid,
2719 const char *domain_name,
2720 struct cli_state *cli,
2721 struct rpc_pipe_client *pipe_hnd,
2722 TALLOC_CTX *mem_ctx,
2727 POLICY_HND connect_pol, domain_pol;
2728 struct samr_Ids rids, rid_types;
2729 struct lsa_String lsa_acct_name;
2731 /* Get sam policy handle */
2733 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2735 MAXIMUM_ALLOWED_ACCESS,
2738 if (!NT_STATUS_IS_OK(result))
2741 /* Get domain policy handle */
2743 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2745 MAXIMUM_ALLOWED_ACCESS,
2746 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2749 if (!NT_STATUS_IS_OK(result))
2752 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2754 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2761 if (!NT_STATUS_IS_OK(result)) {
2763 /* Ok, did not find it in the global sam, try with builtin */
2765 DOM_SID sid_Builtin;
2767 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2769 sid_copy(&sid_Builtin, &global_sid_Builtin);
2771 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2773 MAXIMUM_ALLOWED_ACCESS,
2777 if (!NT_STATUS_IS_OK(result)) {
2778 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2782 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2789 if (!NT_STATUS_IS_OK(result)) {
2790 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2795 if (rids.count != 1) {
2796 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2800 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2801 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2802 domain_sid, &domain_pol,
2806 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2807 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2811 return NT_STATUS_NO_SUCH_GROUP;
2814 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2816 if (argc != 1 || c->display_usage) {
2817 return rpc_group_usage(c, argc, argv);
2820 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2821 rpc_group_members_internals,
2825 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2827 NET_API_STATUS status;
2828 struct GROUP_INFO_0 g0;
2832 d_printf("Usage: 'net rpc group rename group newname'\n");
2836 g0.grpi0_name = argv[1];
2838 status = NetGroupSetInfo(c->opt_host,
2845 d_fprintf(stderr, "Renaming group %s failed with: %s\n",
2846 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2854 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2856 if (argc != 2 || c->display_usage) {
2857 return rpc_group_usage(c, argc, argv);
2860 return rpc_group_rename_internals(c, argc, argv);
2864 * 'net rpc group' entrypoint.
2865 * @param argc Standard main() style argc.
2866 * @param argv Standard main() style argv. Initial components are already
2870 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2872 NET_API_STATUS status;
2874 struct functable func[] = {
2879 "Create specified group",
2880 "net rpc group add\n"
2881 " Create specified group"
2887 "Delete specified group",
2888 "net rpc group delete\n"
2889 " Delete specified group"
2895 "Add member to group",
2896 "net rpc group addmem\n"
2897 " Add member to group"
2903 "Remove member from group",
2904 "net rpc group delmem\n"
2905 " Remove member from group"
2912 "net rpc group list\n"
2919 "List group members",
2920 "net rpc group members\n"
2921 " List group members"
2928 "net rpc group rename\n"
2931 {NULL, NULL, 0, NULL, NULL}
2934 status = libnetapi_init(&c->netapi_ctx);
2938 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2939 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2940 if (c->opt_kerberos) {
2941 libnetapi_set_use_kerberos(c->netapi_ctx);
2945 if (c->display_usage) {
2946 d_printf("Usage:\n");
2947 d_printf("net rpc group\n"
2948 " Alias for net rpc group list global local "
2950 net_display_usage_from_functable(func);
2954 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2955 rpc_group_list_internals,
2959 return net_run_function(c, argc, argv, "net rpc group", func);
2962 /****************************************************************************/
2964 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2966 return net_share_usage(c, argc, argv);
2970 * Add a share on a remote RPC server.
2972 * All parameters are provided by the run_rpc_command function, except for
2973 * argc, argv which are passed through.
2975 * @param domain_sid The domain sid acquired from the remote server.
2976 * @param cli A cli_state connected to the server.
2977 * @param mem_ctx Talloc context, destroyed on completion of the function.
2978 * @param argc Standard main() style argc.
2979 * @param argv Standard main() style argv. Initial components are already
2982 * @return Normal NTSTATUS return.
2984 static NTSTATUS rpc_share_add_internals(struct net_context *c,
2985 const DOM_SID *domain_sid,
2986 const char *domain_name,
2987 struct cli_state *cli,
2988 struct rpc_pipe_client *pipe_hnd,
2989 TALLOC_CTX *mem_ctx,int argc,
2996 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2997 uint32 num_users=0, perms=0;
2998 char *password=NULL; /* don't allow a share password */
3000 union srvsvc_NetShareInfo info;
3001 struct srvsvc_NetShareInfo2 info2;
3002 uint32_t parm_error = 0;
3004 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3005 return NT_STATUS_NO_MEMORY;
3008 path = strchr(sharename, '=');
3010 return NT_STATUS_UNSUCCESSFUL;
3013 info2.name = sharename;
3015 info2.comment = c->opt_comment;
3016 info2.permissions = perms;
3017 info2.max_users = c->opt_maxusers;
3018 info2.current_users = num_users;
3020 info2.password = password;
3022 info.info2 = &info2;
3024 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
3033 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3035 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3036 return rpc_share_usage(c, argc, argv);
3038 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3039 rpc_share_add_internals,
3044 * Delete a share on a remote RPC server.
3046 * All parameters are provided by the run_rpc_command function, except for
3047 * argc, argv which are passed through.
3049 * @param domain_sid The domain sid acquired from the remote server.
3050 * @param cli A cli_state connected to the server.
3051 * @param mem_ctx Talloc context, destroyed on completion of the function.
3052 * @param argc Standard main() style argc.
3053 * @param argv Standard main() style argv. Initial components are already
3056 * @return Normal NTSTATUS return.
3058 static NTSTATUS rpc_share_del_internals(struct net_context *c,
3059 const DOM_SID *domain_sid,
3060 const char *domain_name,
3061 struct cli_state *cli,
3062 struct rpc_pipe_client *pipe_hnd,
3063 TALLOC_CTX *mem_ctx,
3069 return rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
3077 * Delete a share on a remote RPC server.
3079 * @param domain_sid The domain sid acquired from the remote server.
3080 * @param argc Standard main() style argc.
3081 * @param argv Standard main() style argv. Initial components are already
3084 * @return A shell status integer (0 for success).
3086 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3088 if (argc < 1 || c->display_usage) {
3089 return rpc_share_usage(c, argc, argv);
3091 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3092 rpc_share_del_internals,
3097 * Formatted print of share info
3099 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3102 static void display_share_info_1(struct net_context *c,
3103 struct srvsvc_NetShareInfo1 *r)
3105 if (c->opt_long_list_entries) {
3106 d_printf("%-12s %-8.8s %-50s\n",
3108 c->share_type[r->type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)],
3111 d_printf("%s\n", r->name);
3115 static WERROR get_share_info(struct net_context *c,
3116 struct rpc_pipe_client *pipe_hnd,
3117 TALLOC_CTX *mem_ctx,
3121 struct srvsvc_NetShareInfoCtr *info_ctr)
3125 union srvsvc_NetShareInfo info;
3127 /* no specific share requested, enumerate all */
3130 uint32_t preferred_len = 0xffffffff;
3131 uint32_t total_entries = 0;
3132 uint32_t resume_handle = 0;
3134 info_ctr->level = level;
3136 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
3146 /* request just one share */
3147 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
3154 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
3159 ZERO_STRUCTP(info_ctr);
3161 info_ctr->level = level;
3166 struct srvsvc_NetShareCtr1 *ctr1;
3168 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3169 W_ERROR_HAVE_NO_MEMORY(ctr1);
3172 ctr1->array = info.info1;
3174 info_ctr->ctr.ctr1 = ctr1;
3178 struct srvsvc_NetShareCtr2 *ctr2;
3180 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3181 W_ERROR_HAVE_NO_MEMORY(ctr2);
3184 ctr2->array = info.info2;
3186 info_ctr->ctr.ctr2 = ctr2;
3190 struct srvsvc_NetShareCtr502 *ctr502;
3192 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3193 W_ERROR_HAVE_NO_MEMORY(ctr502);
3196 ctr502->array = info.info502;
3198 info_ctr->ctr.ctr502 = ctr502;
3206 * List shares on a remote RPC server.
3208 * All parameters are provided by the run_rpc_command function, except for
3209 * argc, argv which are passed through.
3211 * @param domain_sid The domain sid acquired from the remote server.
3212 * @param cli A cli_state connected to the server.
3213 * @param mem_ctx Talloc context, destroyed on completion of the function.
3214 * @param argc Standard main() style argc.
3215 * @param argv Standard main() style argv. Initial components are already
3218 * @return Normal NTSTATUS return.
3221 static NTSTATUS rpc_share_list_internals(struct net_context *c,
3222 const DOM_SID *domain_sid,
3223 const char *domain_name,
3224 struct cli_state *cli,
3225 struct rpc_pipe_client *pipe_hnd,
3226 TALLOC_CTX *mem_ctx,
3230 struct srvsvc_NetShareInfoCtr info_ctr;
3231 struct srvsvc_NetShareCtr1 ctr1;
3233 uint32 i, level = 1;
3235 ZERO_STRUCT(info_ctr);
3239 info_ctr.ctr.ctr1 = &ctr1;
3241 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3243 if (!W_ERROR_IS_OK(result))
3246 /* Display results */
3248 if (c->opt_long_list_entries) {
3250 "\nEnumerating shared resources (exports) on remote server:\n\n"
3251 "\nShare name Type Description\n"
3252 "---------- ---- -----------\n");
3254 for (i = 0; i < info_ctr.ctr.ctr1->count; i++)
3255 display_share_info_1(c, &info_ctr.ctr.ctr1->array[i]);
3257 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3261 * 'net rpc share list' entrypoint.
3262 * @param argc Standard main() style argc.
3263 * @param argv Standard main() style argv. Initial components are already
3266 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3268 if (c->display_usage) {
3270 "net rpc share list\n"
3271 " List shares on remote server\n");
3275 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3276 rpc_share_list_internals, argc, argv);
3279 static bool check_share_availability(struct cli_state *cli, const char *netname)
3281 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3282 d_printf("skipping [%s]: not a file share.\n", netname);
3292 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3293 const char *netname, uint32 type)
3295 /* only support disk shares */
3296 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3297 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3301 /* skip builtin shares */
3302 /* FIXME: should print$ be added too ? */
3303 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3304 strequal(netname,"global"))
3307 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3308 printf("excluding [%s]\n", netname);
3312 return check_share_availability(cli, netname);
3316 * Migrate shares from a remote RPC server to the local RPC server.
3318 * All parameters are provided by the run_rpc_command function, except for
3319 * argc, argv which are passed through.
3321 * @param domain_sid The domain sid acquired from the remote server.
3322 * @param cli A cli_state connected to the server.
3323 * @param mem_ctx Talloc context, destroyed on completion of the function.
3324 * @param argc Standard main() style argc.
3325 * @param argv Standard main() style argv. Initial components are already
3328 * @return Normal NTSTATUS return.
3331 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3332 const DOM_SID *domain_sid,
3333 const char *domain_name,
3334 struct cli_state *cli,
3335 struct rpc_pipe_client *pipe_hnd,
3336 TALLOC_CTX *mem_ctx,
3341 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3342 struct srvsvc_NetShareInfoCtr ctr_src;
3344 struct rpc_pipe_client *srvsvc_pipe = NULL;
3345 struct cli_state *cli_dst = NULL;
3346 uint32 level = 502; /* includes secdesc */
3347 uint32_t parm_error = 0;
3349 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3351 if (!W_ERROR_IS_OK(result))
3354 /* connect destination PI_SRVSVC */
3355 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3356 &ndr_table_srvsvc.syntax_id);
3357 if (!NT_STATUS_IS_OK(nt_status))
3361 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3363 union srvsvc_NetShareInfo info;
3364 struct srvsvc_NetShareInfo502 info502 =
3365 ctr_src.ctr.ctr502->array[i];
3367 /* reset error-code */
3368 nt_status = NT_STATUS_UNSUCCESSFUL;
3370 if (!check_share_sanity(c, cli, info502.name, info502.type))
3373 /* finally add the share on the dst server */
3375 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3376 info502.name, info502.path, info502.comment);
3378 info.info502 = &info502;
3380 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3381 srvsvc_pipe->desthost,
3387 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3388 printf(" [%s] does already exist\n",
3393 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3394 printf("cannot add share: %s\n", dos_errstr(result));
3400 nt_status = NT_STATUS_OK;
3404 cli_shutdown(cli_dst);
3412 * Migrate shares from a RPC server to another.
3414 * @param argc Standard main() style argc.
3415 * @param argv Standard main() style argv. Initial components are already
3418 * @return A shell status integer (0 for success).
3420 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3423 if (c->display_usage) {
3425 "net rpc share migrate shares\n"
3426 " Migrate shares to local server\n");
3431 printf("no server to migrate\n");
3435 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3436 rpc_share_migrate_shares_internals,
3443 * @param f file_info
3444 * @param mask current search mask
3445 * @param state arg-pointer
3448 static void copy_fn(const char *mnt, file_info *f,
3449 const char *mask, void *state)
3451 static NTSTATUS nt_status;
3452 static struct copy_clistate *local_state;
3453 static fstring filename, new_mask;
3456 struct net_context *c;
3458 local_state = (struct copy_clistate *)state;
3459 nt_status = NT_STATUS_UNSUCCESSFUL;
3463 if (strequal(f->name, ".") || strequal(f->name, ".."))
3466 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3469 if (f->mode & aDIR) {
3471 DEBUG(3,("got dir: %s\n", f->name));
3473 fstrcpy(dir, local_state->cwd);
3475 fstrcat(dir, f->name);
3477 switch (net_mode_share)
3479 case NET_MODE_SHARE_MIGRATE:
3480 /* create that directory */
3481 nt_status = net_copy_file(c, local_state->mem_ctx,
3482 local_state->cli_share_src,
3483 local_state->cli_share_dst,
3485 c->opt_acls? true : false,
3486 c->opt_attrs? true : false,
3487 c->opt_timestamps? true:false,
3491 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3495 if (!NT_STATUS_IS_OK(nt_status))
3496 printf("could not handle dir %s: %s\n",
3497 dir, nt_errstr(nt_status));
3499 /* search below that directory */
3500 fstrcpy(new_mask, dir);
3501 fstrcat(new_mask, "\\*");
3503 old_dir = local_state->cwd;
3504 local_state->cwd = dir;
3505 if (!sync_files(local_state, new_mask))
3506 printf("could not handle files\n");
3507 local_state->cwd = old_dir;
3514 fstrcpy(filename, local_state->cwd);
3515 fstrcat(filename, "\\");
3516 fstrcat(filename, f->name);
3518 DEBUG(3,("got file: %s\n", filename));
3520 switch (net_mode_share)
3522 case NET_MODE_SHARE_MIGRATE:
3523 nt_status = net_copy_file(c, local_state->mem_ctx,
3524 local_state->cli_share_src,
3525 local_state->cli_share_dst,
3527 c->opt_acls? true : false,
3528 c->opt_attrs? true : false,
3529 c->opt_timestamps? true: false,
3533 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3537 if (!NT_STATUS_IS_OK(nt_status))
3538 printf("could not handle file %s: %s\n",
3539 filename, nt_errstr(nt_status));
3544 * sync files, can be called recursivly to list files
3545 * and then call copy_fn for each file
3547 * @param cp_clistate pointer to the copy_clistate we work with
3548 * @param mask the current search mask
3550 * @return Boolean result
3552 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3554 struct cli_state *targetcli;
3555 char *targetpath = NULL;
3557 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3559 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate->cli_share_src,
3560 mask, &targetcli, &targetpath ) ) {
3561 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3562 mask, cli_errstr(cp_clistate->cli_share_src));
3566 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3567 d_fprintf(stderr, "listing %s failed with error: %s\n",
3568 mask, cli_errstr(targetcli));
3577 * Set the top level directory permissions before we do any further copies.
3578 * Should set up ACL inheritance.
3581 bool copy_top_level_perms(struct net_context *c,
3582 struct copy_clistate *cp_clistate,
3583 const char *sharename)
3585 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3587 switch (net_mode_share) {
3588 case NET_MODE_SHARE_MIGRATE:
3589 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3590 nt_status = net_copy_fileattr(c,
3591 cp_clistate->mem_ctx,
3592 cp_clistate->cli_share_src,
3593 cp_clistate->cli_share_dst,
3595 c->opt_acls? true : false,
3596 c->opt_attrs? true : false,
3597 c->opt_timestamps? true: false,
3601 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3605 if (!NT_STATUS_IS_OK(nt_status)) {
3606 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3607 sharename, nt_errstr(nt_status));
3615 * Sync all files inside a remote share to another share (over smb).
3617 * All parameters are provided by the run_rpc_command function, except for
3618 * argc, argv which are passed through.
3620 * @param domain_sid The domain sid acquired from the remote server.
3621 * @param cli A cli_state connected to the server.
3622 * @param mem_ctx Talloc context, destroyed on completion of the function.
3623 * @param argc Standard main() style argc.
3624 * @param argv Standard main() style argv. Initial components are already
3627 * @return Normal NTSTATUS return.
3630 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3631 const DOM_SID *domain_sid,
3632 const char *domain_name,
3633 struct cli_state *cli,
3634 struct rpc_pipe_client *pipe_hnd,
3635 TALLOC_CTX *mem_ctx,
3640 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3641 struct srvsvc_NetShareInfoCtr ctr_src;
3644 struct copy_clistate cp_clistate;
3645 bool got_src_share = false;
3646 bool got_dst_share = false;
3647 const char *mask = "\\*";
3650 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3652 nt_status = NT_STATUS_NO_MEMORY;
3656 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3659 if (!W_ERROR_IS_OK(result))
3662 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3664 struct srvsvc_NetShareInfo502 info502 =
3665 ctr_src.ctr.ctr502->array[i];
3667 if (!check_share_sanity(c, cli, info502.name, info502.type))
3670 /* one might not want to mirror whole discs :) */
3671 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3672 d_printf("skipping [%s]: builtin/hidden share\n", info502.name);
3676 switch (net_mode_share)
3678 case NET_MODE_SHARE_MIGRATE:
3682 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3685 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3687 c->opt_acls ? "including" : "without",
3688 c->opt_attrs ? "including" : "without",
3689 c->opt_timestamps ? "(preserving timestamps)" : "");
3691 cp_clistate.mem_ctx = mem_ctx;
3692 cp_clistate.cli_share_src = NULL;
3693 cp_clistate.cli_share_dst = NULL;
3694 cp_clistate.cwd = NULL;
3695 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3698 /* open share source */
3699 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3700 &cli->dest_ss, cli->desthost,
3701 info502.name, "A:");
3702 if (!NT_STATUS_IS_OK(nt_status))
3705 got_src_share = true;
3707 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3708 /* open share destination */
3709 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3710 NULL, dst, info502.name, "A:");
3711 if (!NT_STATUS_IS_OK(nt_status))
3714 got_dst_share = true;
3717 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3718 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", info502.name);
3719 nt_status = NT_STATUS_UNSUCCESSFUL;
3723 if (!sync_files(&cp_clistate, mask)) {
3724 d_fprintf(stderr, "could not handle files for share: %s\n", info502.name);
3725 nt_status = NT_STATUS_UNSUCCESSFUL;
3730 nt_status = NT_STATUS_OK;
3735 cli_shutdown(cp_clistate.cli_share_src);
3738 cli_shutdown(cp_clistate.cli_share_dst);
3745 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3747 if (c->display_usage) {
3749 "net share migrate files\n"
3750 " Migrate files to local server\n");
3755 d_printf("no server to migrate\n");
3759 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3760 rpc_share_migrate_files_internals,
3765 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3767 * All parameters are provided by the run_rpc_command function, except for
3768 * argc, argv which are passed through.
3770 * @param domain_sid The domain sid acquired from the remote server.
3771 * @param cli A cli_state connected to the server.
3772 * @param mem_ctx Talloc context, destroyed on completion of the function.
3773 * @param argc Standard main() style argc.
3774 * @param argv Standard main() style argv. Initial components are already
3777 * @return Normal NTSTATUS return.
3780 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3781 const DOM_SID *domain_sid,
3782 const char *domain_name,
3783 struct cli_state *cli,
3784 struct rpc_pipe_client *pipe_hnd,
3785 TALLOC_CTX *mem_ctx,
3790 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3791 struct srvsvc_NetShareInfoCtr ctr_src;
3792 union srvsvc_NetShareInfo info;
3794 struct rpc_pipe_client *srvsvc_pipe = NULL;
3795 struct cli_state *cli_dst = NULL;
3796 uint32 level = 502; /* includes secdesc */
3797 uint32_t parm_error = 0;
3799 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3802 if (!W_ERROR_IS_OK(result))
3805 /* connect destination PI_SRVSVC */
3806 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3807 &ndr_table_srvsvc.syntax_id);
3808 if (!NT_STATUS_IS_OK(nt_status))
3812 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3814 struct srvsvc_NetShareInfo502 info502 =
3815 ctr_src.ctr.ctr502->array[i];
3817 /* reset error-code */
3818 nt_status = NT_STATUS_UNSUCCESSFUL;
3820 if (!check_share_sanity(c, cli, info502.name, info502.type))
3823 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3824 info502.name, info502.path, info502.comment);
3827 display_sec_desc(info502.sd_buf.sd);
3829 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3830 info.info502 = &info502;
3832 /* finally modify the share on the dst server */
3833 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3834 srvsvc_pipe->desthost,
3840 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3841 printf("cannot set share-acl: %s\n", dos_errstr(result));
3847 nt_status = NT_STATUS_OK;
3851 cli_shutdown(cli_dst);
3859 * Migrate share-acls from a RPC server to another.
3861 * @param argc Standard main() style argc.
3862 * @param argv Standard main() style argv. Initial components are already
3865 * @return A shell status integer (0 for success).
3867 static int rpc_share_migrate_security(struct net_context *c, int argc,
3870 if (c->display_usage) {
3872 "net rpc share migrate security\n"
3873 " Migrate share-acls to local server\n");
3878 d_printf("no server to migrate\n");
3882 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3883 rpc_share_migrate_security_internals,
3888 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3889 * from one server to another.
3891 * @param argc Standard main() style argc.
3892 * @param argv Standard main() style argv. Initial components are already
3895 * @return A shell status integer (0 for success).
3898 static int rpc_share_migrate_all(struct net_context *c, int argc,
3903 if (c->display_usage) {
3905 "net rpc share migrate all\n"
3906 " Migrates shares including all share settings\n");
3911 d_printf("no server to migrate\n");
3915 /* order is important. we don't want to be locked out by the share-acl
3916 * before copying files - gd */
3918 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3919 rpc_share_migrate_shares_internals, argc, argv);
3923 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3924 rpc_share_migrate_files_internals, argc, argv);
3928 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3929 rpc_share_migrate_security_internals, argc,
3935 * 'net rpc share migrate' entrypoint.
3936 * @param argc Standard main() style argc.
3937 * @param argv Standard main() style argv. Initial components are already
3940 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3943 struct functable func[] = {
3946 rpc_share_migrate_all,
3948 "Migrate shares from remote to local server",
3949 "net rpc share migrate all\n"
3950 " Migrate shares from remote to local server"
3954 rpc_share_migrate_files,
3956 "Migrate files from remote to local server",
3957 "net rpc share migrate files\n"
3958 " Migrate files from remote to local server"
3962 rpc_share_migrate_security,
3964 "Migrate share-ACLs from remote to local server",
3965 "net rpc share migrate security\n"
3966 " Migrate share-ACLs from remote to local server"
3970 rpc_share_migrate_shares,
3972 "Migrate shares from remote to local server",
3973 "net rpc share migrate shares\n"
3974 " Migrate shares from remote to local server"
3976 {NULL, NULL, 0, NULL, NULL}
3979 net_mode_share = NET_MODE_SHARE_MIGRATE;
3981 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3990 static int num_server_aliases;
3991 static struct full_alias *server_aliases;
3994 * Add an alias to the static list.
3996 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3998 if (server_aliases == NULL)
3999 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4001 server_aliases[num_server_aliases] = *alias;
4002 num_server_aliases += 1;
4006 * For a specific domain on the server, fetch all the aliases
4007 * and their members. Add all of them to the server_aliases.
4010 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4011 TALLOC_CTX *mem_ctx,
4012 POLICY_HND *connect_pol,
4013 const DOM_SID *domain_sid)
4015 uint32 start_idx, max_entries, num_entries, i;
4016 struct samr_SamArray *groups = NULL;
4018 POLICY_HND domain_pol;
4020 /* Get domain policy handle */
4022 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
4024 MAXIMUM_ALLOWED_ACCESS,
4025 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4027 if (!NT_STATUS_IS_OK(result))
4034 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
4040 for (i = 0; i < num_entries; i++) {
4042 POLICY_HND alias_pol;
4043 struct full_alias alias;
4044 struct lsa_SidArray sid_array;
4047 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
4049 MAXIMUM_ALLOWED_ACCESS,
4050 groups->entries[i].idx,
4052 if (!NT_STATUS_IS_OK(result))
4055 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
4058 if (!NT_STATUS_IS_OK(result))
4061 alias.num_members = sid_array.num_sids;
4063 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
4064 if (!NT_STATUS_IS_OK(result))
4067 alias.members = NULL;
4069 if (alias.num_members > 0) {
4070 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
4072 for (j = 0; j < alias.num_members; j++)
4073 sid_copy(&alias.members[j],
4074 sid_array.sids[j].sid);
4077 sid_copy(&alias.sid, domain_sid);
4078 sid_append_rid(&alias.sid, groups->entries[i].idx);
4080 push_alias(mem_ctx, &alias);
4082 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4084 result = NT_STATUS_OK;
4087 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4093 * Dump server_aliases as names for debugging purposes.
4096 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4097 const DOM_SID *domain_sid,
4098 const char *domain_name,
4099 struct cli_state *cli,
4100 struct rpc_pipe_client *pipe_hnd,
4101 TALLOC_CTX *mem_ctx,
4109 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4110 SEC_RIGHTS_MAXIMUM_ALLOWED,
4112 if (!NT_STATUS_IS_OK(result))
4115 for (i=0; i<num_server_aliases; i++) {
4118 enum lsa_SidType *types;
4121 struct full_alias *alias = &server_aliases[i];
4123 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4125 &domains, &names, &types);
4126 if (!NT_STATUS_IS_OK(result))
4129 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4131 if (alias->num_members == 0) {
4136 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4139 &domains, &names, &types);
4141 if (!NT_STATUS_IS_OK(result) &&
4142 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4145 for (j=0; j<alias->num_members; j++)
4146 DEBUG(1, ("%s\\%s (%d); ",
4147 domains[j] ? domains[j] : "*unknown*",
4148 names[j] ? names[j] : "*unknown*",types[j]));
4152 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4154 return NT_STATUS_OK;
4158 * Fetch a list of all server aliases and their members into
4162 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4163 const DOM_SID *domain_sid,
4164 const char *domain_name,
4165 struct cli_state *cli,
4166 struct rpc_pipe_client *pipe_hnd,
4167 TALLOC_CTX *mem_ctx,
4172 POLICY_HND connect_pol;
4174 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4176 MAXIMUM_ALLOWED_ACCESS,
4179 if (!NT_STATUS_IS_OK(result))
4182 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4183 &global_sid_Builtin);
4185 if (!NT_STATUS_IS_OK(result))
4188 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4191 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4196 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4198 token->num_sids = 4;
4200 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4201 d_fprintf(stderr, "malloc failed\n");
4202 token->num_sids = 0;
4206 token->user_sids[0] = *user_sid;
4207 sid_copy(&token->user_sids[1], &global_sid_World);
4208 sid_copy(&token->user_sids[2], &global_sid_Network);
4209 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4212 static void free_user_token(NT_USER_TOKEN *token)
4214 SAFE_FREE(token->user_sids);
4217 static bool is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4221 for (i=0; i<token->num_sids; i++) {
4222 if (sid_compare(sid, &token->user_sids[i]) == 0)
4228 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4230 if (is_sid_in_token(token, sid))
4233 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4234 if (!token->user_sids) {
4238 sid_copy(&token->user_sids[token->num_sids], sid);
4240 token->num_sids += 1;
4245 NT_USER_TOKEN token;
4248 static void dump_user_token(struct user_token *token)
4252 d_printf("%s\n", token->name);
4254 for (i=0; i<token->token.num_sids; i++) {
4255 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4259 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4263 for (i=0; i<alias->num_members; i++) {
4264 if (sid_compare(sid, &alias->members[i]) == 0)
4271 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4275 for (i=0; i<num_server_aliases; i++) {
4276 if (is_alias_member(&sid, &server_aliases[i]))
4277 add_sid_to_token(token, &server_aliases[i].sid);
4282 * We got a user token with all the SIDs we can know about without asking the
4283 * server directly. These are the user and domain group sids. All of these can
4284 * be members of aliases. So scan the list of aliases for each of the SIDs and
4285 * add them to the token.
4288 static void collect_alias_memberships(NT_USER_TOKEN *token)
4290 int num_global_sids = token->num_sids;
4293 for (i=0; i<num_global_sids; i++) {
4294 collect_sid_memberships(token, token->user_sids[i]);
4298 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4300 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4301 enum wbcSidType type;
4303 struct wbcDomainSid wsid;
4304 char *sid_str = NULL;
4306 uint32_t num_groups;
4307 gid_t *groups = NULL;
4310 fstr_sprintf(full_name, "%s%c%s",
4311 domain, *lp_winbind_separator(), user);
4313 /* First let's find out the user sid */
4315 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4317 if (!WBC_ERROR_IS_OK(wbc_status)) {
4318 DEBUG(1, ("winbind could not find %s: %s\n",
4319 full_name, wbcErrorString(wbc_status)));
4323 wbc_status = wbcSidToString(&wsid, &sid_str);
4324 if (!WBC_ERROR_IS_OK(wbc_status)) {
4328 if (type != SID_NAME_USER) {
4329 wbcFreeMemory(sid_str);
4330 DEBUG(1, ("%s is not a user\n", full_name));
4334 string_to_sid(&user_sid, sid_str);
4335 wbcFreeMemory(sid_str);
4338 init_user_token(token, &user_sid);
4340 /* And now the groups winbind knows about */
4342 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4343 if (!WBC_ERROR_IS_OK(wbc_status)) {
4344 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4345 full_name, wbcErrorString(wbc_status)));
4349 for (i = 0; i < num_groups; i++) {
4350 gid_t gid = groups[i];
4353 wbc_status = wbcGidToSid(gid, &wsid);
4354 if (!WBC_ERROR_IS_OK(wbc_status)) {
4355 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4356 gid, wbcErrorString(wbc_status)));
4357 wbcFreeMemory(groups);
4361 wbc_status = wbcSidToString(&wsid, &sid_str);
4362 if (!WBC_ERROR_IS_OK(wbc_status)) {
4363 wbcFreeMemory(groups);
4367 DEBUG(3, (" %s\n", sid_str));
4369 string_to_sid(&sid, sid_str);
4370 wbcFreeMemory(sid_str);
4373 add_sid_to_token(token, &sid);
4375 wbcFreeMemory(groups);
4381 * Get a list of all user tokens we want to look at
4384 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4385 struct user_token **user_tokens)
4387 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4388 uint32_t i, num_users;
4390 struct user_token *result;
4391 TALLOC_CTX *frame = NULL;
4393 if (lp_winbind_use_default_domain() &&
4394 (c->opt_target_workgroup == NULL)) {
4395 d_fprintf(stderr, "winbind use default domain = yes set, "
4396 "please specify a workgroup\n");
4400 /* Send request to winbind daemon */
4402 wbc_status = wbcListUsers(NULL, &num_users, &users);
4403 if (!WBC_ERROR_IS_OK(wbc_status)) {
4404 DEBUG(1, ("winbind could not list users: %s\n",
4405 wbcErrorString(wbc_status)));
4409 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4411 if (result == NULL) {
4412 DEBUG(1, ("Could not malloc sid array\n"));
4413 wbcFreeMemory(users);
4417 frame = talloc_stackframe();
4418 for (i=0; i < num_users; i++) {
4419 fstring domain, user;
4422 fstrcpy(result[i].name, users[i]);
4424 p = strchr(users[i], *lp_winbind_separator());
4426 DEBUG(3, ("%s\n", users[i]));
4429 fstrcpy(domain, c->opt_target_workgroup);
4430 fstrcpy(user, users[i]);
4433 fstrcpy(domain, users[i]);
4438 get_user_sids(domain, user, &(result[i].token));
4442 wbcFreeMemory(users);
4444 *num_tokens = num_users;
4445 *user_tokens = result;
4450 static bool get_user_tokens_from_file(FILE *f,
4452 struct user_token **tokens)
4454 struct user_token *token = NULL;
4459 if (fgets(line, sizeof(line)-1, f) == NULL) {
4463 if (line[strlen(line)-1] == '\n')
4464 line[strlen(line)-1] = '\0';
4466 if (line[0] == ' ') {
4470 string_to_sid(&sid, &line[1]);
4472 if (token == NULL) {
4473 DEBUG(0, ("File does not begin with username"));
4477 add_sid_to_token(&token->token, &sid);
4481 /* And a new user... */
4484 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4485 if (*tokens == NULL) {
4486 DEBUG(0, ("Could not realloc tokens\n"));
4490 token = &((*tokens)[*num_tokens-1]);
4492 fstrcpy(token->name, line);
4493 token->token.num_sids = 0;
4494 token->token.user_sids = NULL;
4503 * Show the list of all users that have access to a share
4506 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4507 TALLOC_CTX *mem_ctx,
4508 const char *netname,
4510 struct user_token *tokens)
4513 SEC_DESC *share_sd = NULL;
4514 SEC_DESC *root_sd = NULL;
4515 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4517 union srvsvc_NetShareInfo info;
4522 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4529 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4530 DEBUG(1, ("Coult not query secdesc for share %s\n",
4535 share_sd = info.info502->sd_buf.sd;
4536 if (share_sd == NULL) {
4537 DEBUG(1, ("Got no secdesc for share %s\n",
4543 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4547 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4550 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4553 for (i=0; i<num_tokens; i++) {
4556 if (share_sd != NULL) {
4557 if (!se_access_check(share_sd, &tokens[i].token,
4558 1, &acc_granted, &status)) {
4559 DEBUG(1, ("Could not check share_sd for "
4565 if (!NT_STATUS_IS_OK(status))
4569 if (root_sd == NULL) {
4570 d_printf(" %s\n", tokens[i].name);
4574 if (!se_access_check(root_sd, &tokens[i].token,
4575 1, &acc_granted, &status)) {
4576 DEBUG(1, ("Could not check root_sd for user %s\n",
4581 if (!NT_STATUS_IS_OK(status))
4584 d_printf(" %s\n", tokens[i].name);
4588 cli_close(cli, fnum);
4600 static void collect_share(const char *name, uint32 m,
4601 const char *comment, void *state)
4603 struct share_list *share_list = (struct share_list *)state;
4605 if (m != STYPE_DISKTREE)
4608 share_list->num_shares += 1;
4609 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4610 if (!share_list->shares) {
4611 share_list->num_shares = 0;
4614 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4618 * List shares on a remote RPC server, including the security descriptors.
4620 * All parameters are provided by the run_rpc_command function, except for
4621 * argc, argv which are passed through.
4623 * @param domain_sid The domain sid acquired from the remote server.
4624 * @param cli A cli_state connected to the server.
4625 * @param mem_ctx Talloc context, destroyed on completion of the function.
4626 * @param argc Standard main() style argc.
4627 * @param argv Standard main() style argv. Initial components are already
4630 * @return Normal NTSTATUS return.
4633 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4634 const DOM_SID *domain_sid,
4635 const char *domain_name,
4636 struct cli_state *cli,
4637 struct rpc_pipe_client *pipe_hnd,
4638 TALLOC_CTX *mem_ctx,
4648 struct user_token *tokens = NULL;
4651 struct share_list share_list;
4656 f = fopen(argv[0], "r");
4660 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4661 return NT_STATUS_UNSUCCESSFUL;
4664 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4670 DEBUG(0, ("Could not read users from file\n"));
4671 return NT_STATUS_UNSUCCESSFUL;
4674 for (i=0; i<num_tokens; i++)
4675 collect_alias_memberships(&tokens[i].token);
4677 init_enum_hnd(&hnd, 0);
4679 share_list.num_shares = 0;
4680 share_list.shares = NULL;
4682 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4685 DEBUG(0, ("Error returning browse list: %s\n",
4690 for (i = 0; i < share_list.num_shares; i++) {
4691 char *netname = share_list.shares[i];
4693 if (netname[strlen(netname)-1] == '$')
4696 d_printf("%s\n", netname);
4698 show_userlist(pipe_hnd, mem_ctx, netname,
4699 num_tokens, tokens);
4702 for (i=0; i<num_tokens; i++) {
4703 free_user_token(&tokens[i].token);
4706 SAFE_FREE(share_list.shares);
4708 return NT_STATUS_OK;
4711 static int rpc_share_allowedusers(struct net_context *c, int argc,
4716 if (c->display_usage) {
4718 "net rpc share allowedusers\n"
4719 " List allowed users\n");
4723 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4724 rpc_aliaslist_internals,
4729 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4735 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4736 rpc_share_allowedusers_internals,
4740 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4743 struct user_token *tokens = NULL;
4747 net_usersidlist_usage(c, argc, argv);
4751 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4752 DEBUG(0, ("Could not get the user/sid list\n"));
4756 for (i=0; i<num_tokens; i++) {
4757 dump_user_token(&tokens[i]);
4758 free_user_token(&tokens[i].token);
4765 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4767 d_printf("net usersidlist\n"
4768 "\tprints out a list of all users the running winbind knows\n"
4769 "\tabout, together with all their SIDs. This is used as\n"
4770 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4772 net_common_flags_usage(c, argc, argv);
4777 * 'net rpc share' entrypoint.
4778 * @param argc Standard main() style argc.
4779 * @param argv Standard main() style argv. Initial components are already
4783 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4785 struct functable func[] = {
4791 "net rpc share add\n"
4799 "net rpc share delete\n"
4804 rpc_share_allowedusers,
4806 "Modify allowed users",
4807 "net rpc share allowedusers\n"
4808 " Modify allowed users"
4814 "Migrate share to local server",
4815 "net rpc share migrate\n"
4816 " Migrate share to local server"
4823 "net rpc share list\n"
4826 {NULL, NULL, 0, NULL, NULL}
4831 if (c->display_usage) {
4835 " Alias for net rpc share list\n");
4836 net_display_usage_from_functable(func);
4840 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4841 rpc_share_list_internals,
4845 return net_run_function(c, argc, argv, "net rpc share", func);
4848 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4849 TALLOC_CTX *mem_ctx,
4850 struct rpc_sh_ctx *ctx,
4851 struct rpc_pipe_client *pipe_hnd,
4852 int argc, const char **argv)
4854 return rpc_share_list_internals(c, ctx->domain_sid, ctx->domain_name,
4855 ctx->cli, pipe_hnd, mem_ctx,
4859 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4860 TALLOC_CTX *mem_ctx,
4861 struct rpc_sh_ctx *ctx,
4862 struct rpc_pipe_client *pipe_hnd,
4863 int argc, const char **argv)
4867 uint32_t parm_err = 0;
4868 union srvsvc_NetShareInfo info;
4869 struct srvsvc_NetShareInfo2 info2;
4871 if ((argc < 2) || (argc > 3)) {
4872 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4874 return NT_STATUS_INVALID_PARAMETER;
4877 info2.name = argv[0];
4878 info2.type = STYPE_DISKTREE;
4879 info2.comment = (argc == 3) ? argv[2] : "";
4880 info2.permissions = 0;
4881 info2.max_users = 0;
4882 info2.current_users = 0;
4883 info2.path = argv[1];
4884 info2.password = NULL;
4886 info.info2 = &info2;
4888 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
4898 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4899 TALLOC_CTX *mem_ctx,
4900 struct rpc_sh_ctx *ctx,
4901 struct rpc_pipe_client *pipe_hnd,
4902 int argc, const char **argv)
4908 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4909 return NT_STATUS_INVALID_PARAMETER;
4912 status = rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
4921 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4922 TALLOC_CTX *mem_ctx,
4923 struct rpc_sh_ctx *ctx,
4924 struct rpc_pipe_client *pipe_hnd,
4925 int argc, const char **argv)
4927 union srvsvc_NetShareInfo info;
4932 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4933 return NT_STATUS_INVALID_PARAMETER;
4936 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4942 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4946 d_printf("Name: %s\n", info.info2->name);
4947 d_printf("Comment: %s\n", info.info2->comment);
4948 d_printf("Path: %s\n", info.info2->path);
4949 d_printf("Password: %s\n", info.info2->password);
4952 return werror_to_ntstatus(result);
4955 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4956 struct rpc_sh_ctx *ctx)
4958 static struct rpc_sh_cmd cmds[] = {
4960 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4961 "List available shares" },
4963 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4966 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4969 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4970 "Get information about a share" },
4972 { NULL, NULL, 0, NULL, NULL }
4978 /****************************************************************************/
4980 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4982 return net_file_usage(c, argc, argv);
4986 * Close a file on a remote RPC server.
4988 * All parameters are provided by the run_rpc_command function, except for
4989 * argc, argv which are passed through.
4991 * @param c A net_context structure.
4992 * @param domain_sid The domain sid acquired from the remote server.
4993 * @param cli A cli_state connected to the server.
4994 * @param mem_ctx Talloc context, destroyed on completion of the function.
4995 * @param argc Standard main() style argc.
4996 * @param argv Standard main() style argv. Initial components are already
4999 * @return Normal NTSTATUS return.
5001 static NTSTATUS rpc_file_close_internals(struct net_context *c,
5002 const DOM_SID *domain_sid,
5003 const char *domain_name,
5004 struct cli_state *cli,
5005 struct rpc_pipe_client *pipe_hnd,
5006 TALLOC_CTX *mem_ctx,
5010 return rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx,
5012 atoi(argv[0]), NULL);
5016 * Close a file on a remote RPC server.
5018 * @param argc Standard main() style argc.
5019 * @param argv Standard main() style argv. Initial components are already
5022 * @return A shell status integer (0 for success).
5024 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5026 if (argc < 1 || c->display_usage) {
5027 return rpc_file_usage(c, argc, argv);
5030 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
5031 rpc_file_close_internals,
5036 * Formatted print of open file info
5038 * @param r struct srvsvc_NetFileInfo3 contents
5041 static void display_file_info_3(struct srvsvc_NetFileInfo3 *r)
5043 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5044 r->fid, r->user, r->permissions, r->num_locks, r->path);
5048 * List open files on a remote RPC server.
5050 * All parameters are provided by the run_rpc_command function, except for
5051 * argc, argv which are passed through.
5053 * @param c A net_context structure.
5054 * @param domain_sid The domain sid acquired from the remote server.
5055 * @param cli A cli_state connected to the server.
5056 * @param mem_ctx Talloc context, destroyed on completion of the function.
5057 * @param argc Standard main() style argc.
5058 * @param argv Standard main() style argv. Initial components are already
5061 * @return Normal NTSTATUS return.
5064 static NTSTATUS rpc_file_list_internals(struct net_context *c,
5065 const DOM_SID *domain_sid,
5066 const char *domain_name,
5067 struct cli_state *cli,
5068 struct rpc_pipe_client *pipe_hnd,
5069 TALLOC_CTX *mem_ctx,
5073 struct srvsvc_NetFileInfoCtr info_ctr;
5074 struct srvsvc_NetFileCtr3 ctr3;
5077 uint32 preferred_len = 0xffffffff, i;
5078 const char *username=NULL;
5079 uint32_t total_entries = 0;
5080 uint32_t resume_handle = 0;
5082 /* if argc > 0, must be user command */
5084 username = smb_xstrdup(argv[0]);
5086 ZERO_STRUCT(info_ctr);
5090 info_ctr.ctr.ctr3 = &ctr3;
5092 status = rpccli_srvsvc_NetFileEnum(pipe_hnd, mem_ctx,
5102 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
5105 /* Display results */
5108 "\nEnumerating open files on remote server:\n\n"
5109 "\nFileId Opened by Perms Locks Path"
5110 "\n------ --------- ----- ----- ---- \n");
5111 for (i = 0; i < total_entries; i++)
5112 display_file_info_3(&info_ctr.ctr.ctr3->array[i]);
5114 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
5118 * List files for a user on a remote RPC server.
5120 * @param argc Standard main() style argc.
5121 * @param argv Standard main() style argv. Initial components are already
5124 * @return A shell status integer (0 for success)..
5127 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5129 if (argc < 1 || c->display_usage) {
5130 return rpc_file_usage(c, argc, argv);
5133 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
5134 rpc_file_list_internals,
5139 * 'net rpc file' entrypoint.
5140 * @param argc Standard main() style argc.
5141 * @param argv Standard main() style argv. Initial components are already
5145 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5147 struct functable func[] = {
5152 "Close opened file",
5153 "net rpc file close\n"
5154 " Close opened file"
5160 "List files opened by user",
5161 "net rpc file user\n"
5162 " List files opened by user"
5169 "Display information about opened file",
5170 "net rpc file info\n"
5171 " Display information about opened file"
5174 {NULL, NULL, 0, NULL, NULL}
5178 if (c->display_usage) {
5179 d_printf("Usage:\n");
5180 d_printf("net rpc file\n"
5181 " List opened files\n");
5182 net_display_usage_from_functable(func);
5186 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
5187 rpc_file_list_internals,
5191 return net_run_function(c, argc, argv, "net rpc file", func);
5195 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5197 * All parameters are provided by the run_rpc_command function, except for
5198 * argc, argv which are passed through.
5200 * @param c A net_context structure.
5201 * @param domain_sid The domain sid acquired from the remote server.
5202 * @param cli A cli_state connected to the server.
5203 * @param mem_ctx Talloc context, destroyed on completion of the function.
5204 * @param argc Standard main() style argc.
5205 * @param argv Standard main() style argv. Initial components are already
5208 * @return Normal NTSTATUS return.
5211 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5212 const DOM_SID *domain_sid,
5213 const char *domain_name,
5214 struct cli_state *cli,
5215 struct rpc_pipe_client *pipe_hnd,
5216 TALLOC_CTX *mem_ctx,
5220 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5222 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5224 if (NT_STATUS_IS_OK(result)) {
5225 d_printf("\nShutdown successfully aborted\n");
5226 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5228 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5234 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5236 * All parameters are provided by the run_rpc_command function, except for
5237 * argc, argv which are passed through.
5239 * @param c A net_context structure.
5240 * @param domain_sid The domain sid acquired from the remote server.
5241 * @param cli A cli_state connected to the server.
5242 * @param mem_ctx Talloc context, destroyed on completion of the function.
5243 * @param argc Standard main() style argc.
5244 * @param argv Standard main() style argv. Initial components are already
5247 * @return Normal NTSTATUS return.
5250 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5251 const DOM_SID *domain_sid,
5252 const char *domain_name,
5253 struct cli_state *cli,
5254 struct rpc_pipe_client *pipe_hnd,
5255 TALLOC_CTX *mem_ctx,
5259 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5261 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5263 if (NT_STATUS_IS_OK(result)) {
5264 d_printf("\nShutdown successfully aborted\n");
5265 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5267 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5273 * ABORT the shutdown of a remote RPC server.
5275 * @param argc Standard main() style argc.
5276 * @param argv Standard main() style argv. Initial components are already
5279 * @return A shell status integer (0 for success).
5282 static int rpc_shutdown_abort(struct net_context *c, int argc,
5287 if (c->display_usage) {
5289 "net rpc abortshutdown\n"
5290 " Abort a scheduled shutdown\n");
5294 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5295 rpc_shutdown_abort_internals, argc, argv);
5300 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5302 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5303 rpc_reg_shutdown_abort_internals,
5308 * Shut down a remote RPC Server via initshutdown pipe.
5310 * All parameters are provided by the run_rpc_command function, except for
5311 * argc, argv which are passed through.
5313 * @param c A net_context structure.
5314 * @param domain_sid The domain sid acquired from the remote server.
5315 * @param cli A cli_state connected to the server.
5316 * @param mem_ctx Talloc context, destroyed on completion of the function.
5317 * @param argc Standard main() style argc.
5318 * @param argv Standard main() style argv. Initial components are already
5321 * @return Normal NTSTATUS return.
5324 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5325 const DOM_SID *domain_sid,
5326 const char *domain_name,
5327 struct cli_state *cli,
5328 struct rpc_pipe_client *pipe_hnd,
5329 TALLOC_CTX *mem_ctx,
5333 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5334 const char *msg = "This machine will be shutdown shortly";
5335 uint32 timeout = 20;
5336 struct initshutdown_String msg_string;
5337 struct initshutdown_String_sub s;
5339 if (c->opt_comment) {
5340 msg = c->opt_comment;
5342 if (c->opt_timeout) {
5343 timeout = c->opt_timeout;
5347 msg_string.name = &s;
5349 /* create an entry */
5350 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5351 &msg_string, timeout, c->opt_force, c->opt_reboot,
5354 if (NT_STATUS_IS_OK(result)) {
5355 d_printf("\nShutdown of remote machine succeeded\n");
5356 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5358 DEBUG(1,("Shutdown of remote machine failed!\n"));
5364 * Shut down a remote RPC Server via winreg pipe.
5366 * All parameters are provided by the run_rpc_command function, except for
5367 * argc, argv which are passed through.
5369 * @param c A net_context structure.
5370 * @param domain_sid The domain sid acquired from the remote server.
5371 * @param cli A cli_state connected to the server.
5372 * @param mem_ctx Talloc context, destroyed on completion of the function.
5373 * @param argc Standard main() style argc.
5374 * @param argv Standard main() style argv. Initial components are already
5377 * @return Normal NTSTATUS return.
5380 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5381 const DOM_SID *domain_sid,
5382 const char *domain_name,
5383 struct cli_state *cli,
5384 struct rpc_pipe_client *pipe_hnd,
5385 TALLOC_CTX *mem_ctx,
5389 const char *msg = "This machine will be shutdown shortly";
5390 uint32 timeout = 20;
5391 struct initshutdown_String msg_string;
5392 struct initshutdown_String_sub s;
5396 if (c->opt_comment) {
5397 msg = c->opt_comment;
5400 msg_string.name = &s;
5402 if (c->opt_timeout) {
5403 timeout = c->opt_timeout;
5406 /* create an entry */
5407 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5408 &msg_string, timeout, c->opt_force, c->opt_reboot,
5411 if (NT_STATUS_IS_OK(result)) {
5412 d_printf("\nShutdown of remote machine succeeded\n");
5414 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5415 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5416 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5418 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(werr));
5425 * Shut down a remote RPC server.
5427 * @param argc Standard main() style argc.
5428 * @param argv Standard main() style argv. Initial components are already
5431 * @return A shell status integer (0 for success).
5434 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5438 if (c->display_usage) {
5440 "net rpc shutdown\n"
5441 " Shut down a remote RPC server\n");
5445 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5446 rpc_init_shutdown_internals, argc, argv);
5449 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5450 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5451 rpc_reg_shutdown_internals, argc, argv);
5457 /***************************************************************************
5458 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5459 ***************************************************************************/
5462 * Add interdomain trust account to the RPC server.
5463 * All parameters (except for argc and argv) are passed by run_rpc_command
5466 * @param c A net_context structure.
5467 * @param domain_sid The domain sid acquired from the server.
5468 * @param cli A cli_state connected to the server.
5469 * @param mem_ctx Talloc context, destroyed on completion of the function.
5470 * @param argc Standard main() style argc.
5471 * @param argv Standard main() style argv. Initial components are already
5474 * @return normal NTSTATUS return code.
5477 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5478 const DOM_SID *domain_sid,
5479 const char *domain_name,
5480 struct cli_state *cli,
5481 struct rpc_pipe_client *pipe_hnd,
5482 TALLOC_CTX *mem_ctx,
5486 POLICY_HND connect_pol, domain_pol, user_pol;
5487 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5489 struct lsa_String lsa_acct_name;
5491 uint32 acct_flags=0;
5493 uint32_t access_granted = 0;
5494 union samr_UserInfo info;
5495 unsigned int orig_timeout;
5498 d_printf("Usage: net rpc trustdom add <domain_name> "
5499 "<trust password>\n");
5500 return NT_STATUS_INVALID_PARAMETER;
5504 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5507 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5508 return NT_STATUS_NO_MEMORY;
5511 strupper_m(acct_name);
5513 init_lsa_String(&lsa_acct_name, acct_name);
5515 /* Get samr policy handle */
5516 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5518 MAXIMUM_ALLOWED_ACCESS,
5520 if (!NT_STATUS_IS_OK(result)) {
5524 /* Get domain policy handle */
5525 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5527 MAXIMUM_ALLOWED_ACCESS,
5528 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5530 if (!NT_STATUS_IS_OK(result)) {
5534 /* This call can take a long time - allow the server to time out.
5535 * 35 seconds should do it. */
5537 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5539 /* Create trusting domain's account */
5540 acb_info = ACB_NORMAL;
5541 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5542 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5543 SAMR_USER_ACCESS_SET_PASSWORD |
5544 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5545 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5547 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5556 /* And restore our original timeout. */
5557 rpccli_set_timeout(pipe_hnd, orig_timeout);
5559 if (!NT_STATUS_IS_OK(result)) {
5560 d_printf("net rpc trustdom add: create user %s failed %s\n",
5561 acct_name, nt_errstr(result));
5567 struct samr_LogonHours hours;
5568 struct lsa_BinaryString parameters;
5569 const int units_per_week = 168;
5570 struct samr_CryptPassword crypt_pwd;
5572 ZERO_STRUCT(notime);
5574 ZERO_STRUCT(parameters);
5576 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
5578 result = NT_STATUS_NO_MEMORY;
5581 hours.units_per_week = units_per_week;
5582 memset(hours.bits, 0xFF, units_per_week);
5584 init_samr_CryptPassword(argv[1],
5585 &cli->user_session_key,
5588 init_samr_user_info23(&info.info23,
5589 notime, notime, notime,
5590 notime, notime, notime,
5591 NULL, NULL, NULL, NULL, NULL,
5592 NULL, NULL, NULL, NULL, ¶meters,
5593 0, 0, ACB_DOMTRUST, SAMR_FIELD_ACCT_FLAGS,
5595 0, 0, 0, 0, 0, 0, 0,
5596 crypt_pwd.data, 24);
5598 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5603 if (!NT_STATUS_IS_OK(result)) {
5604 DEBUG(0,("Could not set trust account password: %s\n",
5605 nt_errstr(result)));
5611 SAFE_FREE(acct_name);
5616 * Create interdomain trust account for a remote domain.
5618 * @param argc Standard argc.
5619 * @param argv Standard argv without initial components.
5621 * @return Integer status (0 means success).
5624 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5626 if (argc > 0 && !c->display_usage) {
5627 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5628 rpc_trustdom_add_internals, argc, argv);
5631 "net rpc trustdom add <domain_name> <trust password>\n");
5638 * Remove interdomain trust account from the RPC server.
5639 * All parameters (except for argc and argv) are passed by run_rpc_command
5642 * @param c A net_context structure.
5643 * @param domain_sid The domain sid acquired from the server.
5644 * @param cli A cli_state connected to the server.
5645 * @param mem_ctx Talloc context, destroyed on completion of the function.
5646 * @param argc Standard main() style argc.
5647 * @param argv Standard main() style argv. Initial components are already
5650 * @return normal NTSTATUS return code.
5653 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5654 const DOM_SID *domain_sid,
5655 const char *domain_name,
5656 struct cli_state *cli,
5657 struct rpc_pipe_client *pipe_hnd,
5658 TALLOC_CTX *mem_ctx,
5662 POLICY_HND connect_pol, domain_pol, user_pol;
5663 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5665 DOM_SID trust_acct_sid;
5666 struct samr_Ids user_rids, name_types;
5667 struct lsa_String lsa_acct_name;
5670 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5671 return NT_STATUS_INVALID_PARAMETER;
5675 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5677 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5679 if (acct_name == NULL)
5680 return NT_STATUS_NO_MEMORY;
5682 strupper_m(acct_name);
5684 /* Get samr policy handle */
5685 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5687 MAXIMUM_ALLOWED_ACCESS,
5689 if (!NT_STATUS_IS_OK(result)) {
5693 /* Get domain policy handle */
5694 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5696 MAXIMUM_ALLOWED_ACCESS,
5697 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5699 if (!NT_STATUS_IS_OK(result)) {
5703 init_lsa_String(&lsa_acct_name, acct_name);
5705 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5712 if (!NT_STATUS_IS_OK(result)) {
5713 d_printf("net rpc trustdom del: LookupNames on user %s failed %s\n",
5714 acct_name, nt_errstr(result) );
5718 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5720 MAXIMUM_ALLOWED_ACCESS,
5724 if (!NT_STATUS_IS_OK(result)) {
5725 d_printf("net rpc trustdom del: OpenUser on user %s failed %s\n",
5726 acct_name, nt_errstr(result) );
5730 /* append the rid to the domain sid */
5731 sid_copy(&trust_acct_sid, domain_sid);
5732 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5736 /* remove the sid */
5738 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5741 if (!NT_STATUS_IS_OK(result)) {
5742 d_printf("net rpc trustdom del: RemoveMemberFromForeignDomain on user %s failed %s\n",
5743 acct_name, nt_errstr(result) );
5749 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5752 if (!NT_STATUS_IS_OK(result)) {
5753 d_printf("net rpc trustdom del: DeleteUser on user %s failed %s\n",
5754 acct_name, nt_errstr(result) );
5758 if (!NT_STATUS_IS_OK(result)) {
5759 d_printf("Could not set trust account password: %s\n",
5769 * Delete interdomain trust account for a remote domain.
5771 * @param argc Standard argc.
5772 * @param argv Standard argv without initial components.
5774 * @return Integer status (0 means success).
5777 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5779 if (argc > 0 && !c->display_usage) {
5780 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5781 rpc_trustdom_del_internals, argc, argv);
5784 "net rpc trustdom del <domain>\n");
5789 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5790 struct cli_state *cli,
5791 TALLOC_CTX *mem_ctx,
5792 const char *domain_name)
5794 char *dc_name = NULL;
5795 const char *buffer = NULL;
5796 struct rpc_pipe_client *netr;
5799 /* Use NetServerEnum2 */
5801 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5803 return NT_STATUS_OK;
5806 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5807 for domain %s\n", domain_name));
5809 /* Try netr_GetDcName */
5811 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5813 if (!NT_STATUS_IS_OK(status)) {
5817 status = rpccli_netr_GetDcName(netr, mem_ctx,
5824 if (NT_STATUS_IS_OK(status)) {
5828 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5829 for domain %s\n", domain_name));
5835 * Establish trust relationship to a trusting domain.
5836 * Interdomain account must already be created on remote PDC.
5838 * @param c A net_context structure.
5839 * @param argc Standard argc.
5840 * @param argv Standard argv without initial components.
5842 * @return Integer status (0 means success).
5845 static int rpc_trustdom_establish(struct net_context *c, int argc,
5848 struct cli_state *cli = NULL;
5849 struct sockaddr_storage server_ss;
5850 struct rpc_pipe_client *pipe_hnd = NULL;
5851 POLICY_HND connect_hnd;
5852 TALLOC_CTX *mem_ctx;
5854 DOM_SID *domain_sid;
5859 union lsa_PolicyInformation *info = NULL;
5862 * Connect to \\server\ipc$ as 'our domain' account with password
5865 if (argc != 1 || c->display_usage) {
5867 "net rpc trustdom establish <domain_name>\n");
5871 domain_name = smb_xstrdup(argv[0]);
5872 strupper_m(domain_name);
5874 /* account name used at first is our domain's name with '$' */
5875 asprintf(&acct_name, "%s$", lp_workgroup());
5876 strupper_m(acct_name);
5879 * opt_workgroup will be used by connection functions further,
5880 * hence it should be set to remote domain name instead of ours
5882 if (c->opt_workgroup) {
5883 c->opt_workgroup = smb_xstrdup(domain_name);
5886 c->opt_user_name = acct_name;
5888 /* find the domain controller */
5889 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5890 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5894 /* connect to ipc$ as username/password */
5895 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5896 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5898 /* Is it trusting domain account for sure ? */
5899 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5900 nt_errstr(nt_status)));
5904 /* store who we connected to */
5906 saf_store( domain_name, pdc_name );
5909 * Connect to \\server\ipc$ again (this time anonymously)
5912 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5915 if (NT_STATUS_IS_ERR(nt_status)) {
5916 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5917 domain_name, nt_errstr(nt_status)));
5921 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5922 "domain %s", domain_name))) {
5923 DEBUG(0, ("talloc_init() failed\n"));
5928 /* Make sure we're talking to a proper server */
5930 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5931 if (!NT_STATUS_IS_OK(nt_status)) {
5933 talloc_destroy(mem_ctx);
5938 * Call LsaOpenPolicy and LsaQueryInfo
5941 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5943 if (!NT_STATUS_IS_OK(nt_status)) {
5944 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5946 talloc_destroy(mem_ctx);
5950 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, SEC_RIGHTS_QUERY_VALUE,
5952 if (NT_STATUS_IS_ERR(nt_status)) {
5953 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5954 nt_errstr(nt_status)));
5956 talloc_destroy(mem_ctx);
5960 /* Querying info level 5 */
5962 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5964 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5966 if (NT_STATUS_IS_ERR(nt_status)) {
5967 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5968 nt_errstr(nt_status)));
5970 talloc_destroy(mem_ctx);
5974 domain_sid = info->account_domain.sid;
5976 /* There should be actually query info level 3 (following nt serv behaviour),
5977 but I still don't know if it's _really_ necessary */
5980 * Store the password in secrets db
5983 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5984 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5986 talloc_destroy(mem_ctx);
5991 * Close the pipes and clean up
5994 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5995 if (NT_STATUS_IS_ERR(nt_status)) {
5996 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5997 nt_errstr(nt_status)));
5999 talloc_destroy(mem_ctx);
6005 talloc_destroy(mem_ctx);
6007 d_printf("Trust to domain %s established\n", domain_name);
6012 * Revoke trust relationship to the remote domain.
6014 * @param c A net_context structure.
6015 * @param argc Standard argc.
6016 * @param argv Standard argv without initial components.
6018 * @return Integer status (0 means success).
6021 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6027 if (argc < 1 || c->display_usage) {
6029 "net rpc trustdom revoke <domain_name>\n"
6030 " Revoke trust relationship\n"
6031 " domain_name\tName of domain to revoke trust\n");
6035 /* generate upper cased domain name */
6036 domain_name = smb_xstrdup(argv[0]);
6037 strupper_m(domain_name);
6039 /* delete password of the trust */
6040 if (!pdb_del_trusteddom_pw(domain_name)) {
6041 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6048 SAFE_FREE(domain_name);
6052 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6053 const DOM_SID *domain_sid,
6054 const char *domain_name,
6055 struct cli_state *cli,
6056 struct rpc_pipe_client *pipe_hnd,
6057 TALLOC_CTX *mem_ctx,
6062 sid_to_fstring(str_sid, domain_sid);
6063 d_printf("%s\n", str_sid);
6064 return NT_STATUS_OK;
6067 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
6069 fstring ascii_sid, padding;
6070 int pad_len, col_len = 20;
6072 /* convert sid into ascii string */
6073 sid_to_fstring(ascii_sid, dom_sid);
6075 /* calculate padding space for d_printf to look nicer */
6076 pad_len = col_len - strlen(trusted_dom_name);
6077 padding[pad_len] = 0;
6078 do padding[--pad_len] = ' '; while (pad_len);
6080 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
6083 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6084 TALLOC_CTX *mem_ctx,
6087 const char *trusted_dom_name)
6090 union lsa_TrustedDomainInfo *info = NULL;
6091 char *cleartextpwd = NULL;
6092 uint8_t nt_hash[16];
6095 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
6098 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6100 if (NT_STATUS_IS_ERR(nt_status)) {
6101 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6102 nt_errstr(nt_status)));
6106 data = data_blob(info->password.password->data,
6107 info->password.password->length);
6109 if (!rpccli_get_pwd_hash(pipe_hnd, nt_hash)) {
6110 DEBUG(0, ("Could not retrieve password hash\n"));
6114 cleartextpwd = decrypt_trustdom_secret(nt_hash, &data);
6116 if (cleartextpwd == NULL) {
6117 DEBUG(0,("retrieved NULL password\n"));
6118 nt_status = NT_STATUS_UNSUCCESSFUL;
6122 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6123 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6124 nt_status = NT_STATUS_UNSUCCESSFUL;
6128 #ifdef DEBUG_PASSWORD
6129 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6130 "password: [%s]\n", trusted_dom_name,
6131 sid_string_dbg(&dom_sid), cleartextpwd));
6135 SAFE_FREE(cleartextpwd);
6136 data_blob_free(&data);
6141 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6144 /* common variables */
6145 TALLOC_CTX* mem_ctx;
6146 struct cli_state *cli = NULL;
6147 struct rpc_pipe_client *pipe_hnd = NULL;
6149 const char *domain_name = NULL;
6150 DOM_SID *queried_dom_sid;
6151 POLICY_HND connect_hnd;
6152 union lsa_PolicyInformation *info = NULL;
6154 /* trusted domains listing variables */
6155 unsigned int enum_ctx = 0;
6157 struct lsa_DomainList dom_list;
6160 if (c->display_usage) {
6162 "net rpc trustdom vampire\n"
6163 " Vampire trust relationship from remote server\n");
6168 * Listing trusted domains (stored in secrets.tdb, if local)
6171 mem_ctx = talloc_init("trust relationships vampire");
6174 * set domain and pdc name to local samba server (default)
6175 * or to remote one given in command line
6178 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6179 domain_name = c->opt_workgroup;
6180 c->opt_target_workgroup = c->opt_workgroup;
6182 fstrcpy(pdc_name, global_myname());
6183 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6184 c->opt_target_workgroup = domain_name;
6187 /* open \PIPE\lsarpc and open policy handle */
6188 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6189 if (!NT_STATUS_IS_OK(nt_status)) {
6190 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6191 nt_errstr(nt_status)));
6192 talloc_destroy(mem_ctx);
6196 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6198 if (!NT_STATUS_IS_OK(nt_status)) {
6199 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6200 nt_errstr(nt_status) ));
6202 talloc_destroy(mem_ctx);
6206 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6208 if (NT_STATUS_IS_ERR(nt_status)) {
6209 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6210 nt_errstr(nt_status)));
6212 talloc_destroy(mem_ctx);
6216 /* query info level 5 to obtain sid of a domain being queried */
6217 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6219 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6222 if (NT_STATUS_IS_ERR(nt_status)) {
6223 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6224 nt_errstr(nt_status)));
6226 talloc_destroy(mem_ctx);
6230 queried_dom_sid = info->account_domain.sid;
6233 * Keep calling LsaEnumTrustdom over opened pipe until
6234 * the end of enumeration is reached
6237 d_printf("Vampire trusted domains:\n\n");
6240 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6245 if (NT_STATUS_IS_ERR(nt_status)) {
6246 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6247 nt_errstr(nt_status)));
6249 talloc_destroy(mem_ctx);
6253 for (i = 0; i < dom_list.count; i++) {
6255 print_trusted_domain(dom_list.domains[i].sid,
6256 dom_list.domains[i].name.string);
6258 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6259 *dom_list.domains[i].sid,
6260 dom_list.domains[i].name.string);
6261 if (!NT_STATUS_IS_OK(nt_status)) {
6263 talloc_destroy(mem_ctx);
6269 * in case of no trusted domains say something rather
6270 * than just display blank line
6272 if (!dom_list.count) d_printf("none\n");
6274 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6276 /* close this connection before doing next one */
6277 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6278 if (NT_STATUS_IS_ERR(nt_status)) {
6279 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6280 nt_errstr(nt_status)));
6282 talloc_destroy(mem_ctx);
6286 /* close lsarpc pipe and connection to IPC$ */
6289 talloc_destroy(mem_ctx);
6293 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6295 /* common variables */
6296 TALLOC_CTX* mem_ctx;
6297 struct cli_state *cli = NULL, *remote_cli = NULL;
6298 struct rpc_pipe_client *pipe_hnd = NULL;
6300 const char *domain_name = NULL;
6301 DOM_SID *queried_dom_sid;
6303 int ascii_dom_name_len;
6304 POLICY_HND connect_hnd;
6305 union lsa_PolicyInformation *info = NULL;
6307 /* trusted domains listing variables */
6308 unsigned int num_domains, enum_ctx = 0;
6309 int i, pad_len, col_len = 20;
6310 struct lsa_DomainList dom_list;
6313 /* trusting domains listing variables */
6314 POLICY_HND domain_hnd;
6315 struct samr_SamArray *trusts = NULL;
6317 if (c->display_usage) {
6319 "net rpc trustdom list\n"
6320 " List trust relationships\n");
6325 * Listing trusted domains (stored in secrets.tdb, if local)
6328 mem_ctx = talloc_init("trust relationships listing");
6331 * set domain and pdc name to local samba server (default)
6332 * or to remote one given in command line
6335 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6336 domain_name = c->opt_workgroup;
6337 c->opt_target_workgroup = c->opt_workgroup;
6339 fstrcpy(pdc_name, global_myname());
6340 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6341 c->opt_target_workgroup = domain_name;
6344 /* open \PIPE\lsarpc and open policy handle */
6345 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6346 if (!NT_STATUS_IS_OK(nt_status)) {
6347 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6348 nt_errstr(nt_status)));
6349 talloc_destroy(mem_ctx);
6353 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6355 if (!NT_STATUS_IS_OK(nt_status)) {
6356 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6357 nt_errstr(nt_status) ));
6359 talloc_destroy(mem_ctx);
6363 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6365 if (NT_STATUS_IS_ERR(nt_status)) {
6366 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6367 nt_errstr(nt_status)));
6369 talloc_destroy(mem_ctx);
6373 /* query info level 5 to obtain sid of a domain being queried */
6374 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6376 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6379 if (NT_STATUS_IS_ERR(nt_status)) {
6380 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6381 nt_errstr(nt_status)));
6383 talloc_destroy(mem_ctx);
6387 queried_dom_sid = info->account_domain.sid;
6390 * Keep calling LsaEnumTrustdom over opened pipe until
6391 * the end of enumeration is reached
6394 d_printf("Trusted domains list:\n\n");
6397 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6402 if (NT_STATUS_IS_ERR(nt_status)) {
6403 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6404 nt_errstr(nt_status)));
6406 talloc_destroy(mem_ctx);
6410 for (i = 0; i < dom_list.count; i++) {
6411 print_trusted_domain(dom_list.domains[i].sid,
6412 dom_list.domains[i].name.string);
6416 * in case of no trusted domains say something rather
6417 * than just display blank line
6419 if (!dom_list.count) d_printf("none\n");
6421 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6423 /* close this connection before doing next one */
6424 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6425 if (NT_STATUS_IS_ERR(nt_status)) {
6426 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6427 nt_errstr(nt_status)));
6429 talloc_destroy(mem_ctx);
6433 TALLOC_FREE(pipe_hnd);
6436 * Listing trusting domains (stored in passdb backend, if local)
6439 d_printf("\nTrusting domains list:\n\n");
6442 * Open \PIPE\samr and get needed policy handles
6444 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6446 if (!NT_STATUS_IS_OK(nt_status)) {
6447 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6449 talloc_destroy(mem_ctx);
6454 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6456 SA_RIGHT_SAM_OPEN_DOMAIN,
6458 if (!NT_STATUS_IS_OK(nt_status)) {
6459 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6460 nt_errstr(nt_status)));
6462 talloc_destroy(mem_ctx);
6466 /* SamrOpenDomain - we have to open domain policy handle in order to be
6467 able to enumerate accounts*/
6468 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6470 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6473 if (!NT_STATUS_IS_OK(nt_status)) {
6474 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6475 nt_errstr(nt_status)));
6477 talloc_destroy(mem_ctx);
6482 * perform actual enumeration
6485 enum_ctx = 0; /* reset enumeration context from last enumeration */
6488 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6495 if (NT_STATUS_IS_ERR(nt_status)) {
6496 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6497 nt_errstr(nt_status)));
6499 talloc_destroy(mem_ctx);
6503 for (i = 0; i < num_domains; i++) {
6505 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6508 * get each single domain's sid (do we _really_ need this ?):
6509 * 1) connect to domain's pdc
6510 * 2) query the pdc for domain's sid
6513 /* get rid of '$' tail */
6514 ascii_dom_name_len = strlen(str);
6515 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6516 str[ascii_dom_name_len - 1] = '\0';
6518 /* calculate padding space for d_printf to look nicer */
6519 pad_len = col_len - strlen(str);
6520 padding[pad_len] = 0;
6521 do padding[--pad_len] = ' '; while (pad_len);
6523 /* set opt_* variables to remote domain */
6525 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6526 c->opt_target_workgroup = c->opt_workgroup;
6528 d_printf("%s%s", str, padding);
6530 /* connect to remote domain controller */
6531 nt_status = net_make_ipc_connection(c,
6532 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6534 if (NT_STATUS_IS_OK(nt_status)) {
6535 /* query for domain's sid */
6536 if (run_rpc_command(
6538 &ndr_table_lsarpc.syntax_id, 0,
6539 rpc_query_domain_sid, argc,
6541 d_fprintf(stderr, "couldn't get domain's sid\n");
6543 cli_shutdown(remote_cli);
6546 d_fprintf(stderr, "domain controller is not "
6548 nt_errstr(nt_status));
6552 if (!num_domains) d_printf("none\n");
6554 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6556 /* close opened samr and domain policy handles */
6557 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6558 if (!NT_STATUS_IS_OK(nt_status)) {
6559 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6562 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6563 if (!NT_STATUS_IS_OK(nt_status)) {
6564 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6567 /* close samr pipe and connection to IPC$ */
6570 talloc_destroy(mem_ctx);
6575 * Entrypoint for 'net rpc trustdom' code.
6577 * @param argc Standard argc.
6578 * @param argv Standard argv without initial components.
6580 * @return Integer status (0 means success).
6583 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6585 struct functable func[] = {
6590 "Add trusted domain's account",
6591 "net rpc trustdom add\n"
6592 " Add trusted domain's account"
6598 "Remove trusted domain's account",
6599 "net rpc trustdom del\n"
6600 " Remove trusted domain's account"
6604 rpc_trustdom_establish,
6606 "Establish trust relationship",
6607 "net rpc trustdom establish\n"
6608 " Establish trust relationship"
6612 rpc_trustdom_revoke,
6614 "Revoke trust relationship",
6615 "net rpc trustdom revoke\n"
6616 " Revoke trust relationship"
6622 "List domain trusts",
6623 "net rpc trustdom list\n"
6624 " List domain trusts"
6628 rpc_trustdom_vampire,
6630 "Vampire trusts from remote server",
6631 "net rpc trustdom vampire\n"
6632 " Vampire trusts from remote server"
6634 {NULL, NULL, 0, NULL, NULL}
6637 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6641 * Check if a server will take rpc commands
6642 * @param flags Type of server to connect to (PDC, DMB, localhost)
6643 * if the host is not explicitly specified
6644 * @return bool (true means rpc supported)
6646 bool net_rpc_check(struct net_context *c, unsigned flags)
6648 struct cli_state *cli;
6650 struct sockaddr_storage server_ss;
6651 char *server_name = NULL;
6654 /* flags (i.e. server type) may depend on command */
6655 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6658 if ((cli = cli_initialise()) == NULL) {
6662 status = cli_connect(cli, server_name, &server_ss);
6663 if (!NT_STATUS_IS_OK(status))
6665 if (!attempt_netbios_session_request(&cli, global_myname(),
6666 server_name, &server_ss))
6668 if (!cli_negprot(cli))
6670 if (cli->protocol < PROTOCOL_NT1)
6679 /* dump sam database via samsync rpc calls */
6680 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6681 if (c->display_usage) {
6684 " Dump remote SAM database\n");
6688 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6689 NET_FLAGS_ANONYMOUS,
6690 rpc_samdump_internals, argc, argv);
6693 /* syncronise sam database via samsync rpc calls */
6694 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6696 struct functable func[] = {
6701 "Dump remote SAM database to ldif",
6702 "net rpc vampire ldif\n"
6703 " Dump remote SAM database to LDIF file or stdout"
6709 "Dump remote SAM database to Kerberos Keytab",
6710 "net rpc vampire keytab\n"
6711 " Dump remote SAM database to Kerberos keytab file"
6714 {NULL, NULL, 0, NULL, NULL}
6718 if (c->display_usage) {
6721 " Vampire remote SAM database\n");
6725 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6726 NET_FLAGS_ANONYMOUS,
6727 rpc_vampire_internals,
6731 return net_run_function(c, argc, argv, "net rpc vampire", func);
6735 * Migrate everything from a print server.
6737 * @param c A net_context structure.
6738 * @param argc Standard main() style argc.
6739 * @param argv Standard main() style argv. Initial components are already
6742 * @return A shell status integer (0 for success).
6744 * The order is important !
6745 * To successfully add drivers the print queues have to exist !
6746 * Applying ACLs should be the last step, because you're easily locked out.
6749 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6754 if (c->display_usage) {
6756 "net rpc printer migrate all\n"
6757 " Migrate everything from a print server\n");
6762 d_printf("no server to migrate\n");
6766 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6767 rpc_printer_migrate_printers_internals, argc,
6772 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6773 rpc_printer_migrate_drivers_internals, argc,
6778 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6779 rpc_printer_migrate_forms_internals, argc, argv);
6783 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6784 rpc_printer_migrate_settings_internals, argc,
6789 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6790 rpc_printer_migrate_security_internals, argc,
6796 * Migrate print drivers from a print server.
6798 * @param c A net_context structure.
6799 * @param argc Standard main() style argc.
6800 * @param argv Standard main() style argv. Initial components are already
6803 * @return A shell status integer (0 for success).
6805 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6808 if (c->display_usage) {
6810 "net rpc printer migrate drivers\n"
6811 " Migrate print-drivers from a print-server\n");
6816 d_printf("no server to migrate\n");
6820 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6821 rpc_printer_migrate_drivers_internals,
6826 * Migrate print-forms from a print-server.
6828 * @param c A net_context structure.
6829 * @param argc Standard main() style argc.
6830 * @param argv Standard main() style argv. Initial components are already
6833 * @return A shell status integer (0 for success).
6835 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6838 if (c->display_usage) {
6840 "net rpc printer migrate forms\n"
6841 " Migrate print-forms from a print-server\n");
6846 d_printf("no server to migrate\n");
6850 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6851 rpc_printer_migrate_forms_internals,
6856 * Migrate printers from a print-server.
6858 * @param c A net_context structure.
6859 * @param argc Standard main() style argc.
6860 * @param argv Standard main() style argv. Initial components are already
6863 * @return A shell status integer (0 for success).
6865 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6868 if (c->display_usage) {
6870 "net rpc printer migrate printers\n"
6871 " Migrate printers from a print-server\n");
6876 d_printf("no server to migrate\n");
6880 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6881 rpc_printer_migrate_printers_internals,
6886 * Migrate printer-ACLs from a print-server
6888 * @param c A net_context structure.
6889 * @param argc Standard main() style argc.
6890 * @param argv Standard main() style argv. Initial components are already
6893 * @return A shell status integer (0 for success).
6895 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6898 if (c->display_usage) {
6900 "net rpc printer migrate security\n"
6901 " Migrate printer-ACLs from a print-server\n");
6906 d_printf("no server to migrate\n");
6910 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6911 rpc_printer_migrate_security_internals,
6916 * Migrate printer-settings from a print-server.
6918 * @param c A net_context structure.
6919 * @param argc Standard main() style argc.
6920 * @param argv Standard main() style argv. Initial components are already
6923 * @return A shell status integer (0 for success).
6925 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6928 if (c->display_usage) {
6930 "net rpc printer migrate settings\n"
6931 " Migrate printer-settings from a print-server\n");
6936 d_printf("no server to migrate\n");
6940 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6941 rpc_printer_migrate_settings_internals,
6946 * 'net rpc printer' entrypoint.
6948 * @param c A net_context structure.
6949 * @param argc Standard main() style argc.
6950 * @param argv Standard main() style argv. Initial components are already
6954 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6957 /* ouch: when addriver and setdriver are called from within
6958 rpc_printer_migrate_drivers_internals, the printer-queue already
6961 struct functable func[] = {
6964 rpc_printer_migrate_all,
6966 "Migrate all from remote to local print server",
6967 "net rpc printer migrate all\n"
6968 " Migrate all from remote to local print server"
6972 rpc_printer_migrate_drivers,
6974 "Migrate drivers to local server",
6975 "net rpc printer migrate drivers\n"
6976 " Migrate drivers to local server"
6980 rpc_printer_migrate_forms,
6982 "Migrate froms to local server",
6983 "net rpc printer migrate forms\n"
6984 " Migrate froms to local server"
6988 rpc_printer_migrate_printers,
6990 "Migrate printers to local server",
6991 "net rpc printer migrate printers\n"
6992 " Migrate printers to local server"
6996 rpc_printer_migrate_security,
6998 "Mirgate printer ACLs to local server",
6999 "net rpc printer migrate security\n"
7000 " Mirgate printer ACLs to local server"
7004 rpc_printer_migrate_settings,
7006 "Migrate printer settings to local server",
7007 "net rpc printer migrate settings\n"
7008 " Migrate printer settings to local server"
7010 {NULL, NULL, 0, NULL, NULL}
7013 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7018 * List printers on a remote RPC server.
7020 * @param c A net_context structure.
7021 * @param argc Standard main() style argc.
7022 * @param argv Standard main() style argv. Initial components are already
7025 * @return A shell status integer (0 for success).
7027 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7029 if (c->display_usage) {
7031 "net rpc printer list\n"
7032 " List printers on a remote RPC server\n");
7036 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7037 rpc_printer_list_internals,
7042 * List printer-drivers on a remote RPC server.
7044 * @param c A net_context structure.
7045 * @param argc Standard main() style argc.
7046 * @param argv Standard main() style argv. Initial components are already
7049 * @return A shell status integer (0 for success).
7051 static int rpc_printer_driver_list(struct net_context *c, int argc,
7054 if (c->display_usage) {
7056 "net rpc printer driver\n"
7057 " List printer-drivers on a remote RPC server\n");
7061 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7062 rpc_printer_driver_list_internals,
7067 * Publish printer in ADS via MSRPC.
7069 * @param c A net_context structure.
7070 * @param argc Standard main() style argc.
7071 * @param argv Standard main() style argv. Initial components are already
7074 * @return A shell status integer (0 for success).
7076 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7079 if (c->display_usage) {
7081 "net rpc printer publish publish\n"
7082 " Publish printer in ADS via MSRPC\n");
7086 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7087 rpc_printer_publish_publish_internals,
7092 * Update printer in ADS via MSRPC.
7094 * @param c A net_context structure.
7095 * @param argc Standard main() style argc.
7096 * @param argv Standard main() style argv. Initial components are already
7099 * @return A shell status integer (0 for success).
7101 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7103 if (c->display_usage) {
7105 "net rpc printer publish update\n"
7106 " Update printer in ADS via MSRPC\n");
7110 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7111 rpc_printer_publish_update_internals,
7116 * UnPublish printer in ADS via MSRPC.
7118 * @param c A net_context structure.
7119 * @param argc Standard main() style argc.
7120 * @param argv Standard main() style argv. Initial components are already
7123 * @return A shell status integer (0 for success).
7125 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7128 if (c->display_usage) {
7130 "net rpc printer publish unpublish\n"
7131 " UnPublish printer in ADS via MSRPC\n");
7135 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7136 rpc_printer_publish_unpublish_internals,
7141 * List published printers via MSRPC.
7143 * @param c A net_context structure.
7144 * @param argc Standard main() style argc.
7145 * @param argv Standard main() style argv. Initial components are already
7148 * @return A shell status integer (0 for success).
7150 static int rpc_printer_publish_list(struct net_context *c, int argc,
7153 if (c->display_usage) {
7155 "net rpc printer publish list\n"
7156 " List published printers via MSRPC\n");
7160 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7161 rpc_printer_publish_list_internals,
7167 * Publish printer in ADS.
7169 * @param c A net_context structure.
7170 * @param argc Standard main() style argc.
7171 * @param argv Standard main() style argv. Initial components are already
7174 * @return A shell status integer (0 for success).
7176 static int rpc_printer_publish(struct net_context *c, int argc,
7180 struct functable func[] = {
7183 rpc_printer_publish_publish,
7185 "Publish printer in AD",
7186 "net rpc printer publish publish\n"
7187 " Publish printer in AD"
7191 rpc_printer_publish_update,
7193 "Update printer in AD",
7194 "net rpc printer publish update\n"
7195 " Update printer in AD"
7199 rpc_printer_publish_unpublish,
7201 "Unpublish printer",
7202 "net rpc printer publish unpublish\n"
7203 " Unpublish printer"
7207 rpc_printer_publish_list,
7209 "List published printers",
7210 "net rpc printer publish list\n"
7211 " List published printers"
7213 {NULL, NULL, 0, NULL, NULL}
7217 if (c->display_usage) {
7218 d_printf("Usage:\n");
7219 d_printf("net rpc printer publish\n"
7220 " List published printers\n"
7221 " Alias of net rpc printer publish list\n");
7222 net_display_usage_from_functable(func);
7225 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7226 rpc_printer_publish_list_internals,
7230 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7236 * Display rpc printer help page.
7238 * @param c A net_context structure.
7239 * @param argc Standard main() style argc.
7240 * @param argv Standard main() style argv. Initial components are already
7243 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7245 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"
7246 "\tlists all printers on print-server\n\n");
7247 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7248 "\tlists all printer-drivers on print-server\n\n");
7249 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7250 "\tpublishes printer settings in Active Directory\n"
7251 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
7252 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7253 "\n\tmigrates printers from remote to local server\n\n");
7254 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7255 "\n\tmigrates printer-settings from remote to local server\n\n");
7256 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7257 "\n\tmigrates printer-drivers from remote to local server\n\n");
7258 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7259 "\n\tmigrates printer-forms from remote to local server\n\n");
7260 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7261 "\n\tmigrates printer-ACLs from remote to local server\n\n");
7262 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7263 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7264 "\tremote to local print-server\n\n");
7265 net_common_methods_usage(c, argc, argv);
7266 net_common_flags_usage(c, argc, argv);
7268 "\t-v or --verbose\t\t\tgive verbose output\n"
7269 "\t --destination\t\tmigration target server (default: localhost)\n");
7275 * 'net rpc printer' entrypoint.
7277 * @param c A net_context structure.
7278 * @param argc Standard main() style argc.
7279 * @param argv Standard main() style argv. Initial components are already
7282 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7284 struct functable func[] = {
7289 "List all printers on print server",
7290 "net rpc printer list\n"
7291 " List all printers on print server"
7295 rpc_printer_migrate,
7297 "Migrate printer to local server",
7298 "net rpc printer migrate\n"
7299 " Migrate printer to local server"
7303 rpc_printer_driver_list,
7305 "List printer drivers",
7306 "net rpc printer driver\n"
7307 " List printer drivers"
7311 rpc_printer_publish,
7313 "Publish printer in AD",
7314 "net rpc printer publish\n"
7315 " Publish printer in AD"
7317 {NULL, NULL, 0, NULL, NULL}
7321 if (c->display_usage) {
7322 d_printf("Usage:\n");
7323 d_printf("net rpc printer\n"
7324 " List printers\n");
7325 net_display_usage_from_functable(func);
7328 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7329 rpc_printer_list_internals,
7333 return net_run_function(c, argc, argv, "net rpc printer", func);
7337 * 'net rpc' entrypoint.
7339 * @param c A net_context structure.
7340 * @param argc Standard main() style argc.
7341 * @param argv Standard main() style argv. Initial components are already
7345 int net_rpc(struct net_context *c, int argc, const char **argv)
7347 struct functable func[] = {
7352 "Modify global audit settings",
7354 " Modify global audit settings"
7360 "Show basic info about a domain",
7362 " Show basic info about a domain"
7376 "Join a domain created in server manager",
7378 " Join a domain created in server manager"
7384 "Test that a join is valid",
7385 "net rpc testjoin\n"
7386 " Test that a join is valid"
7392 "List/modify users",
7394 " List/modify users"
7400 "Change a user password",
7401 "net rpc password\n"
7402 " Change a user password\n"
7403 " Alias for net rpc user password"
7409 "List/modify groups",
7411 " List/modify groups"
7417 "List/modify shares",
7419 " List/modify shares"
7433 "List/modify printers",
7435 " List/modify printers"
7439 net_rpc_changetrustpw,
7441 "Change trust account password",
7442 "net rpc changetrustpw\n"
7443 " Change trust account password"
7449 "Modify domain trusts",
7450 "net rpc trustdom\n"
7451 " Modify domain trusts"
7457 "Abort a remote shutdown",
7458 "net rpc abortshutdown\n"
7459 " Abort a remote shutdown"
7465 "Shutdown a remote server",
7466 "net rpc shutdown\n"
7467 " Shutdown a remote server"
7473 "Dump SAM data of remote NT PDC",
7475 " Dump SAM data of remote NT PDC"
7481 "Sync a remote NT PDC's data into local passdb",
7483 " Sync a remote NT PDC's data into local passdb"
7489 "Fetch the domain sid into local secrets.tdb",
7491 " Fetch the domain sid into local secrets.tdb"
7497 "Manage privileges assigned to SID",
7499 " Manage privileges assigned to SID"
7505 "Start/stop/query remote services",
7507 " Start/stop/query remote services"
7513 "Manage registry hives",
7514 "net rpc registry\n"
7515 " Manage registry hives"
7521 "Open interactive shell on remote server",
7523 " Open interactive shell on remote server"
7525 {NULL, NULL, 0, NULL, NULL}
7527 return net_run_function(c, argc, argv, "net rpc", func);