s3-secdesc: move all winreg access bits to IDL.
[Samba.git] / source3 / utils / net_rpc.c
blobd100189bfcc727a517ddc4ebd4b883b30aecb0be
1 /*
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/>. */
23 #include "includes.h"
24 #include "utils/net.h"
26 static int net_mode_share;
27 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
29 /**
30 * @file net_rpc.c
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.
41 **/
44 /**
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.
51 **/
53 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
54 DOM_SID **domain_sid,
55 const char **domain_name)
57 struct rpc_pipe_client *lsa_pipe = NULL;
58 struct policy_handle pol;
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,
63 &lsa_pipe);
64 if (!NT_STATUS_IS_OK(result)) {
65 d_fprintf(stderr, "Could not initialise lsa pipe\n");
66 return result;
69 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
70 SEC_FLAG_MAXIMUM_ALLOWED,
71 &pol);
72 if (!NT_STATUS_IS_OK(result)) {
73 d_fprintf(stderr, "open_policy failed: %s\n",
74 nt_errstr(result));
75 return result;
78 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
79 &pol,
80 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
81 &info);
82 if (!NT_STATUS_IS_OK(result)) {
83 d_fprintf(stderr, "lsaquery failed: %s\n",
84 nt_errstr(result));
85 return result;
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);
94 return NT_STATUS_OK;
97 /**
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
105 * stripped.
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,
112 int conn_flags,
113 rpc_command_fn fn,
114 int argc,
115 const char **argv)
117 struct cli_state *cli = NULL;
118 struct rpc_pipe_client *pipe_hnd = NULL;
119 TALLOC_CTX *mem_ctx;
120 NTSTATUS nt_status;
121 DOM_SID *domain_sid;
122 const char *domain_name;
123 int ret = -1;
125 /* make use of cli_state handed over as an argument, if possible */
126 if (!cli_arg) {
127 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
128 if (!NT_STATUS_IS_OK(nt_status)) {
129 DEBUG(1, ("failed to make ipc connection: %s\n",
130 nt_errstr(nt_status)));
131 return -1;
133 } else {
134 cli = cli_arg;
137 if (!cli) {
138 return -1;
141 /* Create mem_ctx */
143 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
144 DEBUG(0, ("talloc_init() failed\n"));
145 goto fail;
148 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
149 &domain_name);
150 if (!NT_STATUS_IS_OK(nt_status)) {
151 goto fail;
154 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
155 if (lp_client_schannel()
156 && (ndr_syntax_id_equal(interface,
157 &ndr_table_netlogon.syntax_id))) {
158 /* Always try and create an schannel netlogon pipe. */
159 nt_status = cli_rpc_pipe_open_schannel(
160 cli, interface,
161 PIPE_AUTH_LEVEL_PRIVACY, domain_name,
162 &pipe_hnd);
163 if (!NT_STATUS_IS_OK(nt_status)) {
164 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
165 nt_errstr(nt_status) ));
166 goto fail;
168 } else {
169 if (conn_flags & NET_FLAGS_SEAL) {
170 nt_status = cli_rpc_pipe_open_ntlmssp(
171 cli, interface,
172 PIPE_AUTH_LEVEL_PRIVACY,
173 lp_workgroup(), c->opt_user_name,
174 c->opt_password, &pipe_hnd);
175 } else {
176 nt_status = cli_rpc_pipe_open_noauth(
177 cli, interface,
178 &pipe_hnd);
180 if (!NT_STATUS_IS_OK(nt_status)) {
181 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
182 get_pipe_name_from_iface(interface),
183 nt_errstr(nt_status) ));
184 goto fail;
189 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
191 if (!NT_STATUS_IS_OK(nt_status)) {
192 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
193 } else {
194 ret = 0;
195 DEBUG(5, ("rpc command function succedded\n"));
198 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
199 if (pipe_hnd) {
200 TALLOC_FREE(pipe_hnd);
204 fail:
205 /* close the connection only if it was opened here */
206 if (!cli_arg) {
207 cli_shutdown(cli);
210 talloc_destroy(mem_ctx);
211 return ret;
215 * Force a change of the trust acccount password.
217 * All parameters are provided by the run_rpc_command function, except for
218 * argc, argv which are passed through.
220 * @param domain_sid The domain sid acquired from the remote server.
221 * @param cli A cli_state connected to the server.
222 * @param mem_ctx Talloc context, destroyed on completion of the function.
223 * @param argc Standard main() style argc.
224 * @param argv Standard main() style argv. Initial components are already
225 * stripped.
227 * @return Normal NTSTATUS return.
230 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
231 const DOM_SID *domain_sid,
232 const char *domain_name,
233 struct cli_state *cli,
234 struct rpc_pipe_client *pipe_hnd,
235 TALLOC_CTX *mem_ctx,
236 int argc,
237 const char **argv)
240 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
244 * Force a change of the trust acccount password.
246 * @param argc Standard main() style argc.
247 * @param argv Standard main() style argv. Initial components are already
248 * stripped.
250 * @return A shell status integer (0 for success).
253 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
255 if (c->display_usage) {
256 d_printf("Usage:\n"
257 "net rpc changetrustpw\n"
258 " Change the machine trust password\n");
259 return 0;
262 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
263 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
264 rpc_changetrustpw_internals,
265 argc, argv);
269 * Join a domain, the old way.
271 * This uses 'machinename' as the inital password, and changes it.
273 * The password should be created with 'server manager' or equiv first.
275 * All parameters are provided by the run_rpc_command function, except for
276 * argc, argv which are passed through.
278 * @param domain_sid The domain sid acquired from the remote server.
279 * @param cli A cli_state connected to the server.
280 * @param mem_ctx Talloc context, destroyed on completion of the function.
281 * @param argc Standard main() style argc.
282 * @param argv Standard main() style argv. Initial components are already
283 * stripped.
285 * @return Normal NTSTATUS return.
288 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
289 const DOM_SID *domain_sid,
290 const char *domain_name,
291 struct cli_state *cli,
292 struct rpc_pipe_client *pipe_hnd,
293 TALLOC_CTX *mem_ctx,
294 int argc,
295 const char **argv)
298 fstring trust_passwd;
299 unsigned char orig_trust_passwd_hash[16];
300 NTSTATUS result;
301 uint32 sec_channel_type;
303 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
304 &pipe_hnd);
305 if (!NT_STATUS_IS_OK(result)) {
306 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
307 "error was %s\n",
308 cli->desthost,
309 nt_errstr(result) ));
310 return result;
314 check what type of join - if the user want's to join as
315 a BDC, the server must agree that we are a BDC.
317 if (argc >= 0) {
318 sec_channel_type = get_sec_channel_type(argv[0]);
319 } else {
320 sec_channel_type = get_sec_channel_type(NULL);
323 fstrcpy(trust_passwd, global_myname());
324 strlower_m(trust_passwd);
327 * Machine names can be 15 characters, but the max length on
328 * a password is 14. --jerry
331 trust_passwd[14] = '\0';
333 E_md4hash(trust_passwd, orig_trust_passwd_hash);
335 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
336 orig_trust_passwd_hash,
337 sec_channel_type);
339 if (NT_STATUS_IS_OK(result))
340 printf("Joined domain %s.\n", c->opt_target_workgroup);
343 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
344 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
345 result = NT_STATUS_UNSUCCESSFUL;
348 return result;
352 * Join a domain, the old way.
354 * @param argc Standard main() style argc.
355 * @param argv Standard main() style argv. Initial components are already
356 * stripped.
358 * @return A shell status integer (0 for success).
361 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
363 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
364 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
365 rpc_oldjoin_internals,
366 argc, argv);
370 * Join a domain, the old way. This function exists to allow
371 * the message to be displayed when oldjoin was explicitly
372 * requested, but not when it was implied by "net rpc join".
374 * @param argc Standard main() style argc.
375 * @param argv Standard main() style argv. Initial components are already
376 * stripped.
378 * @return A shell status integer (0 for success).
381 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
383 int rc = -1;
385 if (c->display_usage) {
386 d_printf("Usage:\n"
387 "net rpc oldjoin\n"
388 " Join a domain the old way\n");
389 return 0;
392 rc = net_rpc_perform_oldjoin(c, argc, argv);
394 if (rc) {
395 d_fprintf(stderr, "Failed to join domain\n");
398 return rc;
402 * 'net rpc join' entrypoint.
403 * @param argc Standard main() style argc.
404 * @param argv Standard main() style argv. Initial components are already
405 * stripped
407 * Main 'net_rpc_join()' (where the admin username/password is used) is
408 * in net_rpc_join.c.
409 * Try to just change the password, but if that doesn't work, use/prompt
410 * for a username/password.
413 int net_rpc_join(struct net_context *c, int argc, const char **argv)
415 if (c->display_usage) {
416 d_printf("Usage:\n"
417 "net rpc join -U <username>[%%password] <type>\n"
418 " Join a domain\n"
419 " username\tName of the admin user"
420 " password\tPassword of the admin user, will "
421 "prompt if not specified\n"
422 " type\tCan be one of the following:\n"
423 "\t\tMEMBER\tJoin as member server (default)\n"
424 "\t\tBDC\tJoin as BDC\n"
425 "\t\tPDC\tJoin as PDC\n");
426 return 0;
429 if (lp_server_role() == ROLE_STANDALONE) {
430 d_printf("cannot join as standalone machine\n");
431 return -1;
434 if (strlen(global_myname()) > 15) {
435 d_printf("Our netbios name can be at most 15 chars long, "
436 "\"%s\" is %u chars long\n",
437 global_myname(), (unsigned int)strlen(global_myname()));
438 return -1;
441 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
442 return 0;
444 return net_rpc_join_newstyle(c, argc, argv);
448 * display info about a rpc domain
450 * All parameters are provided by the run_rpc_command function, except for
451 * argc, argv which are passed through.
453 * @param domain_sid The domain sid acquired from the remote server
454 * @param cli A cli_state connected to the server.
455 * @param mem_ctx Talloc context, destroyed on completion of the function.
456 * @param argc Standard main() style argc.
457 * @param argv Standard main() style argv. Initial components are already
458 * stripped.
460 * @return Normal NTSTATUS return.
463 NTSTATUS rpc_info_internals(struct net_context *c,
464 const DOM_SID *domain_sid,
465 const char *domain_name,
466 struct cli_state *cli,
467 struct rpc_pipe_client *pipe_hnd,
468 TALLOC_CTX *mem_ctx,
469 int argc,
470 const char **argv)
472 struct policy_handle connect_pol, domain_pol;
473 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
474 union samr_DomainInfo *info = NULL;
475 fstring sid_str;
477 sid_to_fstring(sid_str, domain_sid);
479 /* Get sam policy handle */
480 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
481 pipe_hnd->desthost,
482 MAXIMUM_ALLOWED_ACCESS,
483 &connect_pol);
484 if (!NT_STATUS_IS_OK(result)) {
485 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
486 goto done;
489 /* Get domain policy handle */
490 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
491 &connect_pol,
492 MAXIMUM_ALLOWED_ACCESS,
493 CONST_DISCARD(struct dom_sid2 *, domain_sid),
494 &domain_pol);
495 if (!NT_STATUS_IS_OK(result)) {
496 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
497 goto done;
500 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
501 &domain_pol,
503 &info);
504 if (NT_STATUS_IS_OK(result)) {
505 d_printf("Domain Name: %s\n", info->general.domain_name.string);
506 d_printf("Domain SID: %s\n", sid_str);
507 d_printf("Sequence number: %llu\n",
508 (unsigned long long)info->general.sequence_num);
509 d_printf("Num users: %u\n", info->general.num_users);
510 d_printf("Num domain groups: %u\n", info->general.num_groups);
511 d_printf("Num local groups: %u\n", info->general.num_aliases);
514 done:
515 return result;
519 * 'net rpc info' entrypoint.
520 * @param argc Standard main() style argc.
521 * @param argv Standard main() style argv. Initial components are already
522 * stripped.
525 int net_rpc_info(struct net_context *c, int argc, const char **argv)
527 if (c->display_usage) {
528 d_printf("Usage:\n"
529 "net rpc info\n"
530 " Display information about the domain\n");
531 return 0;
534 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
535 NET_FLAGS_PDC, rpc_info_internals,
536 argc, argv);
540 * Fetch domain SID into the local secrets.tdb.
542 * All parameters are provided by the run_rpc_command function, except for
543 * argc, argv which are passed through.
545 * @param domain_sid The domain sid acquired from the remote server.
546 * @param cli A cli_state connected to the server.
547 * @param mem_ctx Talloc context, destroyed on completion of the function.
548 * @param argc Standard main() style argc.
549 * @param argv Standard main() style argv. Initial components are already
550 * stripped.
552 * @return Normal NTSTATUS return.
555 static NTSTATUS rpc_getsid_internals(struct net_context *c,
556 const DOM_SID *domain_sid,
557 const char *domain_name,
558 struct cli_state *cli,
559 struct rpc_pipe_client *pipe_hnd,
560 TALLOC_CTX *mem_ctx,
561 int argc,
562 const char **argv)
564 fstring sid_str;
566 sid_to_fstring(sid_str, domain_sid);
567 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
568 sid_str, domain_name);
570 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
571 DEBUG(0,("Can't store domain SID\n"));
572 return NT_STATUS_UNSUCCESSFUL;
575 return NT_STATUS_OK;
579 * 'net rpc getsid' entrypoint.
580 * @param argc Standard main() style argc.
581 * @param argv Standard main() style argv. Initial components are already
582 * stripped.
585 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
587 if (c->display_usage) {
588 d_printf("Usage:\n"
589 "net rpc getsid\n"
590 " Fetch domain SID into local secrets.tdb\n");
591 return 0;
594 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
595 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
596 rpc_getsid_internals,
597 argc, argv);
600 /****************************************************************************/
603 * Basic usage function for 'net rpc user'.
604 * @param argc Standard main() style argc.
605 * @param argv Standard main() style argv. Initial components are already
606 * stripped.
609 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
611 return net_user_usage(c, argc, argv);
615 * Add a new user to a remote RPC server.
617 * @param argc Standard main() style argc.
618 * @param argv Standard main() style argv. Initial components are already
619 * stripped.
621 * @return A shell status integer (0 for success).
624 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
626 NET_API_STATUS status;
627 struct USER_INFO_1 info1;
628 uint32_t parm_error = 0;
630 if (argc < 1 || c->display_usage) {
631 rpc_user_usage(c, argc, argv);
632 return 0;
635 ZERO_STRUCT(info1);
637 info1.usri1_name = argv[0];
638 if (argc == 2) {
639 info1.usri1_password = argv[1];
642 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
644 if (status != 0) {
645 d_fprintf(stderr, "Failed to add user '%s' with: %s.\n",
646 argv[0], libnetapi_get_error_string(c->netapi_ctx,
647 status));
648 return -1;
649 } else {
650 d_printf("Added user '%s'.\n", argv[0]);
653 return 0;
657 * Rename a user on a remote RPC server.
659 * @param argc Standard main() style argc.
660 * @param argv Standard main() style argv. Initial components are already
661 * stripped.
663 * @return A shell status integer (0 for success).
666 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
668 NET_API_STATUS status;
669 struct USER_INFO_0 u0;
670 uint32_t parm_err = 0;
672 if (argc != 2 || c->display_usage) {
673 rpc_user_usage(c, argc, argv);
674 return 0;
677 u0.usri0_name = argv[1];
679 status = NetUserSetInfo(c->opt_host, argv[0],
680 0, (uint8_t *)&u0, &parm_err);
681 if (status) {
682 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n",
683 argv[0], argv[1],
684 libnetapi_get_error_string(c->netapi_ctx, status));
685 } else {
686 d_printf("Renamed user from %s to %s\n", argv[0], argv[1]);
689 return status;
693 * Delete a user from a remote RPC server.
695 * @param argc Standard main() style argc.
696 * @param argv Standard main() style argv. Initial components are already
697 * stripped.
699 * @return A shell status integer (0 for success).
702 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
704 NET_API_STATUS status;
706 if (argc < 1 || c->display_usage) {
707 rpc_user_usage(c, argc, argv);
708 return 0;
711 status = NetUserDel(c->opt_host, argv[0]);
713 if (status != 0) {
714 d_fprintf(stderr, "Failed to delete user '%s' with: %s.\n",
715 argv[0],
716 libnetapi_get_error_string(c->netapi_ctx, status));
717 return -1;
718 } else {
719 d_printf("Deleted user '%s'.\n", argv[0]);
722 return 0;
726 * Set a user's password on a remote RPC server.
728 * @param argc Standard main() style argc.
729 * @param argv Standard main() style argv. Initial components are already
730 * stripped.
732 * @return A shell status integer (0 for success).
735 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
737 NET_API_STATUS status;
738 char *prompt = NULL;
739 struct USER_INFO_1003 u1003;
740 uint32_t parm_err = 0;
742 if (argc < 1 || c->display_usage) {
743 rpc_user_usage(c, argc, argv);
744 return 0;
747 if (argv[1]) {
748 u1003.usri1003_password = argv[1];
749 } else {
750 if (asprintf(&prompt, "Enter new password for %s:", argv[0]) == -1) {
751 return -1;
753 u1003.usri1003_password = getpass(prompt);
754 SAFE_FREE(prompt);
757 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
759 /* Display results */
760 if (status != 0) {
761 d_fprintf(stderr, "Failed to set password for '%s' with: %s.\n",
762 argv[0], libnetapi_get_error_string(c->netapi_ctx,
763 status));
764 return -1;
767 return 0;
771 * List a user's groups from a remote RPC server.
773 * @param argc Standard main() style argc.
774 * @param argv Standard main() style argv. Initial components are already
775 * stripped.
777 * @return A shell status integer (0 for success)
780 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
783 NET_API_STATUS status;
784 struct GROUP_USERS_INFO_0 *u0 = NULL;
785 uint32_t entries_read = 0;
786 uint32_t total_entries = 0;
787 int i;
790 if (argc < 1 || c->display_usage) {
791 rpc_user_usage(c, argc, argv);
792 return 0;
795 status = NetUserGetGroups(c->opt_host,
796 argv[0],
798 (uint8_t **)(void *)&u0,
799 (uint32_t)-1,
800 &entries_read,
801 &total_entries);
802 if (status != 0) {
803 d_fprintf(stderr, "Failed to get groups for '%s' with: %s.\n",
804 argv[0], libnetapi_get_error_string(c->netapi_ctx,
805 status));
806 return -1;
809 for (i=0; i < entries_read; i++) {
810 printf("%s\n", u0->grui0_name);
811 u0++;
814 return 0;
818 * List users on a remote RPC server.
820 * All parameters are provided by the run_rpc_command function, except for
821 * argc, argv which are passed through.
823 * @param domain_sid The domain sid acquired from the remote server.
824 * @param cli A cli_state connected to the server.
825 * @param mem_ctx Talloc context, destroyed on completion of the function.
826 * @param argc Standard main() style argc.
827 * @param argv Standard main() style argv. Initial components are already
828 * stripped.
830 * @return Normal NTSTATUS return.
833 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
835 NET_API_STATUS status;
836 uint32_t start_idx=0, num_entries, i, loop_count = 0;
837 struct NET_DISPLAY_USER *info = NULL;
838 void *buffer = NULL;
840 /* Query domain users */
841 if (c->opt_long_list_entries)
842 d_printf("\nUser name Comment"
843 "\n-----------------------------\n");
844 do {
845 uint32_t max_entries, max_size;
847 get_query_dispinfo_params(
848 loop_count, &max_entries, &max_size);
850 status = NetQueryDisplayInformation(c->opt_host,
852 start_idx,
853 max_entries,
854 max_size,
855 &num_entries,
856 &buffer);
857 if (status != 0 && status != ERROR_MORE_DATA) {
858 return status;
861 info = (struct NET_DISPLAY_USER *)buffer;
863 for (i = 0; i < num_entries; i++) {
865 if (c->opt_long_list_entries)
866 printf("%-21.21s %s\n", info->usri1_name,
867 info->usri1_comment);
868 else
869 printf("%s\n", info->usri1_name);
870 info++;
873 NetApiBufferFree(buffer);
875 loop_count++;
876 start_idx += num_entries;
878 } while (status == ERROR_MORE_DATA);
880 return status;
884 * 'net rpc user' entrypoint.
885 * @param argc Standard main() style argc.
886 * @param argv Standard main() style argv. Initial components are already
887 * stripped.
890 int net_rpc_user(struct net_context *c, int argc, const char **argv)
892 NET_API_STATUS status;
894 struct functable func[] = {
896 "add",
897 rpc_user_add,
898 NET_TRANSPORT_RPC,
899 "Add specified user",
900 "net rpc user add\n"
901 " Add specified user"
904 "info",
905 rpc_user_info,
906 NET_TRANSPORT_RPC,
907 "List domain groups of user",
908 "net rpc user info\n"
909 " Lis domain groups of user"
912 "delete",
913 rpc_user_delete,
914 NET_TRANSPORT_RPC,
915 "Remove specified user",
916 "net rpc user delete\n"
917 " Remove specified user"
920 "password",
921 rpc_user_password,
922 NET_TRANSPORT_RPC,
923 "Change user password",
924 "net rpc user password\n"
925 " Change user password"
928 "rename",
929 rpc_user_rename,
930 NET_TRANSPORT_RPC,
931 "Rename specified user",
932 "net rpc user rename\n"
933 " Rename specified user"
935 {NULL, NULL, 0, NULL, NULL}
938 status = libnetapi_init(&c->netapi_ctx);
939 if (status != 0) {
940 return -1;
942 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
943 libnetapi_set_password(c->netapi_ctx, c->opt_password);
944 if (c->opt_kerberos) {
945 libnetapi_set_use_kerberos(c->netapi_ctx);
948 if (argc == 0) {
949 if (c->display_usage) {
950 d_printf("Usage:\n");
951 d_printf("net rpc user\n"
952 " List all users\n");
953 net_display_usage_from_functable(func);
954 return 0;
957 return rpc_user_list(c, argc, argv);
960 return net_run_function(c, argc, argv, "net rpc user", func);
963 static NTSTATUS rpc_sh_user_list(struct net_context *c,
964 TALLOC_CTX *mem_ctx,
965 struct rpc_sh_ctx *ctx,
966 struct rpc_pipe_client *pipe_hnd,
967 int argc, const char **argv)
969 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
972 static NTSTATUS rpc_sh_user_info(struct net_context *c,
973 TALLOC_CTX *mem_ctx,
974 struct rpc_sh_ctx *ctx,
975 struct rpc_pipe_client *pipe_hnd,
976 int argc, const char **argv)
978 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
981 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
982 TALLOC_CTX *mem_ctx,
983 struct rpc_sh_ctx *ctx,
984 struct rpc_pipe_client *pipe_hnd,
985 int argc, const char **argv,
986 NTSTATUS (*fn)(
987 struct net_context *c,
988 TALLOC_CTX *mem_ctx,
989 struct rpc_sh_ctx *ctx,
990 struct rpc_pipe_client *pipe_hnd,
991 struct policy_handle *user_hnd,
992 int argc, const char **argv))
994 struct policy_handle connect_pol, domain_pol, user_pol;
995 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
996 DOM_SID sid;
997 uint32 rid;
998 enum lsa_SidType type;
1000 if (argc == 0) {
1001 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1002 return NT_STATUS_INVALID_PARAMETER;
1005 ZERO_STRUCT(connect_pol);
1006 ZERO_STRUCT(domain_pol);
1007 ZERO_STRUCT(user_pol);
1009 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1010 argv[0], NULL, NULL, &sid, &type);
1011 if (!NT_STATUS_IS_OK(result)) {
1012 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1013 nt_errstr(result));
1014 goto done;
1017 if (type != SID_NAME_USER) {
1018 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1019 sid_type_lookup(type));
1020 result = NT_STATUS_NO_SUCH_USER;
1021 goto done;
1024 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1025 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1026 result = NT_STATUS_NO_SUCH_USER;
1027 goto done;
1030 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1031 pipe_hnd->desthost,
1032 MAXIMUM_ALLOWED_ACCESS,
1033 &connect_pol);
1034 if (!NT_STATUS_IS_OK(result)) {
1035 goto done;
1038 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1039 &connect_pol,
1040 MAXIMUM_ALLOWED_ACCESS,
1041 ctx->domain_sid,
1042 &domain_pol);
1043 if (!NT_STATUS_IS_OK(result)) {
1044 goto done;
1047 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1048 &domain_pol,
1049 MAXIMUM_ALLOWED_ACCESS,
1050 rid,
1051 &user_pol);
1052 if (!NT_STATUS_IS_OK(result)) {
1053 goto done;
1056 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1058 done:
1059 if (is_valid_policy_hnd(&user_pol)) {
1060 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1062 if (is_valid_policy_hnd(&domain_pol)) {
1063 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1065 if (is_valid_policy_hnd(&connect_pol)) {
1066 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1068 return result;
1071 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1072 TALLOC_CTX *mem_ctx,
1073 struct rpc_sh_ctx *ctx,
1074 struct rpc_pipe_client *pipe_hnd,
1075 struct policy_handle *user_hnd,
1076 int argc, const char **argv)
1078 NTSTATUS result;
1079 union samr_UserInfo *info = NULL;
1081 if (argc != 0) {
1082 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1083 return NT_STATUS_INVALID_PARAMETER;
1086 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1087 user_hnd,
1089 &info);
1090 if (!NT_STATUS_IS_OK(result)) {
1091 return result;
1094 d_printf("user rid: %d, group rid: %d\n",
1095 info->info21.rid,
1096 info->info21.primary_gid);
1098 return result;
1101 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1102 TALLOC_CTX *mem_ctx,
1103 struct rpc_sh_ctx *ctx,
1104 struct rpc_pipe_client *pipe_hnd,
1105 int argc, const char **argv)
1107 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1108 rpc_sh_user_show_internals);
1111 #define FETCHSTR(name, rec) \
1112 do { if (strequal(ctx->thiscmd, name)) { \
1113 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1114 } while (0);
1116 #define SETSTR(name, rec, flag) \
1117 do { if (strequal(ctx->thiscmd, name)) { \
1118 init_lsa_String(&(info->info21.rec), argv[0]); \
1119 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1120 } while (0);
1122 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1123 TALLOC_CTX *mem_ctx,
1124 struct rpc_sh_ctx *ctx,
1125 struct rpc_pipe_client *pipe_hnd,
1126 struct policy_handle *user_hnd,
1127 int argc, const char **argv)
1129 NTSTATUS result;
1130 const char *username;
1131 const char *oldval = "";
1132 union samr_UserInfo *info = NULL;
1134 if (argc > 1) {
1135 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1136 ctx->whoami);
1137 return NT_STATUS_INVALID_PARAMETER;
1140 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1141 user_hnd,
1143 &info);
1144 if (!NT_STATUS_IS_OK(result)) {
1145 return result;
1148 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1150 FETCHSTR("fullname", full_name);
1151 FETCHSTR("homedir", home_directory);
1152 FETCHSTR("homedrive", home_drive);
1153 FETCHSTR("logonscript", logon_script);
1154 FETCHSTR("profilepath", profile_path);
1155 FETCHSTR("description", description);
1157 if (argc == 0) {
1158 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1159 goto done;
1162 if (strcmp(argv[0], "NULL") == 0) {
1163 argv[0] = "";
1166 ZERO_STRUCT(info->info21);
1168 SETSTR("fullname", full_name, FULL_NAME);
1169 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1170 SETSTR("homedrive", home_drive, HOME_DRIVE);
1171 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1172 SETSTR("profilepath", profile_path, PROFILE_PATH);
1173 SETSTR("description", description, DESCRIPTION);
1175 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1176 user_hnd,
1178 info);
1180 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1181 ctx->thiscmd, oldval, argv[0]);
1183 done:
1185 return result;
1188 #define HANDLEFLG(name, rec) \
1189 do { if (strequal(ctx->thiscmd, name)) { \
1190 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1191 if (newval) { \
1192 newflags = oldflags | ACB_##rec; \
1193 } else { \
1194 newflags = oldflags & ~ACB_##rec; \
1195 } } } while (0);
1197 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1198 TALLOC_CTX *mem_ctx,
1199 struct rpc_sh_ctx *ctx,
1200 struct rpc_pipe_client *pipe_hnd,
1201 int argc, const char **argv)
1203 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1204 rpc_sh_user_str_edit_internals);
1207 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1208 TALLOC_CTX *mem_ctx,
1209 struct rpc_sh_ctx *ctx,
1210 struct rpc_pipe_client *pipe_hnd,
1211 struct policy_handle *user_hnd,
1212 int argc, const char **argv)
1214 NTSTATUS result;
1215 const char *username;
1216 const char *oldval = "unknown";
1217 uint32 oldflags, newflags;
1218 bool newval;
1219 union samr_UserInfo *info = NULL;
1221 if ((argc > 1) ||
1222 ((argc == 1) && !strequal(argv[0], "yes") &&
1223 !strequal(argv[0], "no"))) {
1224 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1225 ctx->whoami);
1226 return NT_STATUS_INVALID_PARAMETER;
1229 newval = strequal(argv[0], "yes");
1231 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1232 user_hnd,
1234 &info);
1235 if (!NT_STATUS_IS_OK(result)) {
1236 return result;
1239 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1240 oldflags = info->info21.acct_flags;
1241 newflags = info->info21.acct_flags;
1243 HANDLEFLG("disabled", DISABLED);
1244 HANDLEFLG("pwnotreq", PWNOTREQ);
1245 HANDLEFLG("autolock", AUTOLOCK);
1246 HANDLEFLG("pwnoexp", PWNOEXP);
1248 if (argc == 0) {
1249 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1250 goto done;
1253 ZERO_STRUCT(info->info21);
1255 info->info21.acct_flags = newflags;
1256 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1258 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1259 user_hnd,
1261 info);
1263 if (NT_STATUS_IS_OK(result)) {
1264 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1265 ctx->thiscmd, oldval, argv[0]);
1268 done:
1270 return result;
1273 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1274 TALLOC_CTX *mem_ctx,
1275 struct rpc_sh_ctx *ctx,
1276 struct rpc_pipe_client *pipe_hnd,
1277 int argc, const char **argv)
1279 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1280 rpc_sh_user_flag_edit_internals);
1283 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1284 TALLOC_CTX *mem_ctx,
1285 struct rpc_sh_ctx *ctx)
1287 static struct rpc_sh_cmd cmds[] = {
1289 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1290 "Show/Set a user's full name" },
1292 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1293 "Show/Set a user's home directory" },
1295 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1296 "Show/Set a user's home drive" },
1298 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1299 "Show/Set a user's logon script" },
1301 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1302 "Show/Set a user's profile path" },
1304 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1305 "Show/Set a user's description" },
1307 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1308 "Show/Set whether a user is disabled" },
1310 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1311 "Show/Set whether a user locked out" },
1313 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1314 "Show/Set whether a user does not need a password" },
1316 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1317 "Show/Set whether a user's password does not expire" },
1319 { NULL, NULL, 0, NULL, NULL }
1322 return cmds;
1325 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1326 TALLOC_CTX *mem_ctx,
1327 struct rpc_sh_ctx *ctx)
1329 static struct rpc_sh_cmd cmds[] = {
1331 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1332 "List available users" },
1334 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1335 "List the domain groups a user is member of" },
1337 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1338 "Show info about a user" },
1340 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1341 "Show/Modify a user's fields" },
1343 { NULL, NULL, 0, NULL, NULL }
1346 return cmds;
1349 /****************************************************************************/
1352 * Basic usage function for 'net rpc group'.
1353 * @param argc Standard main() style argc.
1354 * @param argv Standard main() style argv. Initial components are already
1355 * stripped.
1358 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1360 return net_group_usage(c, argc, argv);
1364 * Delete group on a remote RPC server.
1366 * All parameters are provided by the run_rpc_command function, except for
1367 * argc, argv which are passed through.
1369 * @param domain_sid The domain sid acquired from the remote server.
1370 * @param cli A cli_state connected to the server.
1371 * @param mem_ctx Talloc context, destroyed on completion of the function.
1372 * @param argc Standard main() style argc.
1373 * @param argv Standard main() style argv. Initial components are already
1374 * stripped.
1376 * @return Normal NTSTATUS return.
1379 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1380 const DOM_SID *domain_sid,
1381 const char *domain_name,
1382 struct cli_state *cli,
1383 struct rpc_pipe_client *pipe_hnd,
1384 TALLOC_CTX *mem_ctx,
1385 int argc,
1386 const char **argv)
1388 struct policy_handle connect_pol, domain_pol, group_pol, user_pol;
1389 bool group_is_primary = false;
1390 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1391 uint32_t group_rid;
1392 struct samr_RidTypeArray *rids = NULL;
1393 /* char **names; */
1394 int i;
1395 /* struct samr_RidWithAttribute *user_gids; */
1397 struct samr_Ids group_rids, name_types;
1398 struct lsa_String lsa_acct_name;
1399 union samr_UserInfo *info = NULL;
1401 if (argc < 1 || c->display_usage) {
1402 rpc_group_usage(c, argc,argv);
1403 return NT_STATUS_OK; /* ok? */
1406 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1407 pipe_hnd->desthost,
1408 MAXIMUM_ALLOWED_ACCESS,
1409 &connect_pol);
1411 if (!NT_STATUS_IS_OK(result)) {
1412 d_fprintf(stderr, "Request samr_Connect2 failed\n");
1413 goto done;
1416 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1417 &connect_pol,
1418 MAXIMUM_ALLOWED_ACCESS,
1419 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1420 &domain_pol);
1422 if (!NT_STATUS_IS_OK(result)) {
1423 d_fprintf(stderr, "Request open_domain failed\n");
1424 goto done;
1427 init_lsa_String(&lsa_acct_name, argv[0]);
1429 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1430 &domain_pol,
1432 &lsa_acct_name,
1433 &group_rids,
1434 &name_types);
1435 if (!NT_STATUS_IS_OK(result)) {
1436 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1437 goto done;
1440 switch (name_types.ids[0])
1442 case SID_NAME_DOM_GRP:
1443 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1444 &domain_pol,
1445 MAXIMUM_ALLOWED_ACCESS,
1446 group_rids.ids[0],
1447 &group_pol);
1448 if (!NT_STATUS_IS_OK(result)) {
1449 d_fprintf(stderr, "Request open_group failed");
1450 goto done;
1453 group_rid = group_rids.ids[0];
1455 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1456 &group_pol,
1457 &rids);
1459 if (!NT_STATUS_IS_OK(result)) {
1460 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1461 goto done;
1464 if (c->opt_verbose) {
1465 d_printf("Domain Group %s (rid: %d) has %d members\n",
1466 argv[0],group_rid, rids->count);
1469 /* Check if group is anyone's primary group */
1470 for (i = 0; i < rids->count; i++)
1472 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1473 &domain_pol,
1474 MAXIMUM_ALLOWED_ACCESS,
1475 rids->rids[i],
1476 &user_pol);
1478 if (!NT_STATUS_IS_OK(result)) {
1479 d_fprintf(stderr, "Unable to open group member %d\n",
1480 rids->rids[i]);
1481 goto done;
1484 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1485 &user_pol,
1487 &info);
1489 if (!NT_STATUS_IS_OK(result)) {
1490 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",
1491 rids->rids[i]);
1492 goto done;
1495 if (info->info21.primary_gid == group_rid) {
1496 if (c->opt_verbose) {
1497 d_printf("Group is primary group of %s\n",
1498 info->info21.account_name.string);
1500 group_is_primary = true;
1503 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1506 if (group_is_primary) {
1507 d_fprintf(stderr, "Unable to delete group because some "
1508 "of it's members have it as primary group\n");
1509 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1510 goto done;
1513 /* remove all group members */
1514 for (i = 0; i < rids->count; i++)
1516 if (c->opt_verbose)
1517 d_printf("Remove group member %d...",
1518 rids->rids[i]);
1519 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1520 &group_pol,
1521 rids->rids[i]);
1523 if (NT_STATUS_IS_OK(result)) {
1524 if (c->opt_verbose)
1525 d_printf("ok\n");
1526 } else {
1527 if (c->opt_verbose)
1528 d_printf("failed\n");
1529 goto done;
1533 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1534 &group_pol);
1536 break;
1537 /* removing a local group is easier... */
1538 case SID_NAME_ALIAS:
1539 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1540 &domain_pol,
1541 MAXIMUM_ALLOWED_ACCESS,
1542 group_rids.ids[0],
1543 &group_pol);
1545 if (!NT_STATUS_IS_OK(result)) {
1546 d_fprintf(stderr, "Request open_alias failed\n");
1547 goto done;
1550 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1551 &group_pol);
1552 break;
1553 default:
1554 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1555 argv[0],sid_type_lookup(name_types.ids[0]));
1556 result = NT_STATUS_UNSUCCESSFUL;
1557 goto done;
1560 if (NT_STATUS_IS_OK(result)) {
1561 if (c->opt_verbose)
1562 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types.ids[0]),argv[0]);
1563 } else {
1564 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1565 get_friendly_nt_error_msg(result));
1568 done:
1569 return result;
1573 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1575 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1576 rpc_group_delete_internals, argc,argv);
1579 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1581 NET_API_STATUS status;
1582 struct GROUP_INFO_1 info1;
1583 uint32_t parm_error = 0;
1585 if (argc != 1 || c->display_usage) {
1586 rpc_group_usage(c, argc, argv);
1587 return 0;
1590 ZERO_STRUCT(info1);
1592 info1.grpi1_name = argv[0];
1593 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1594 info1.grpi1_comment = c->opt_comment;
1597 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1599 if (status != 0) {
1600 d_fprintf(stderr, "Failed to add group '%s' with: %s.\n",
1601 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1602 status));
1603 return -1;
1604 } else {
1605 d_printf("Added group '%s'.\n", argv[0]);
1608 return 0;
1611 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1613 NET_API_STATUS status;
1614 struct LOCALGROUP_INFO_1 info1;
1615 uint32_t parm_error = 0;
1617 if (argc != 1 || c->display_usage) {
1618 rpc_group_usage(c, argc, argv);
1619 return 0;
1622 ZERO_STRUCT(info1);
1624 info1.lgrpi1_name = argv[0];
1625 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1626 info1.lgrpi1_comment = c->opt_comment;
1629 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1631 if (status != 0) {
1632 d_fprintf(stderr, "Failed to add alias '%s' with: %s.\n",
1633 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1634 status));
1635 return -1;
1636 } else {
1637 d_printf("Added alias '%s'.\n", argv[0]);
1640 return 0;
1643 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1645 if (c->opt_localgroup)
1646 return rpc_alias_add_internals(c, argc, argv);
1648 return rpc_group_add_internals(c, argc, argv);
1651 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1652 TALLOC_CTX *mem_ctx,
1653 const char *name,
1654 DOM_SID *sid,
1655 enum lsa_SidType *type)
1657 DOM_SID *sids = NULL;
1658 enum lsa_SidType *types = NULL;
1659 struct rpc_pipe_client *pipe_hnd = NULL;
1660 struct policy_handle lsa_pol;
1661 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1663 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1664 &pipe_hnd);
1665 if (!NT_STATUS_IS_OK(result)) {
1666 goto done;
1669 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1670 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
1672 if (!NT_STATUS_IS_OK(result)) {
1673 goto done;
1676 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1677 &name, NULL, 1, &sids, &types);
1679 if (NT_STATUS_IS_OK(result)) {
1680 sid_copy(sid, &sids[0]);
1681 *type = types[0];
1684 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1686 done:
1687 if (pipe_hnd) {
1688 TALLOC_FREE(pipe_hnd);
1691 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1693 /* Try as S-1-5-whatever */
1695 DOM_SID tmp_sid;
1697 if (string_to_sid(&tmp_sid, name)) {
1698 sid_copy(sid, &tmp_sid);
1699 *type = SID_NAME_UNKNOWN;
1700 result = NT_STATUS_OK;
1704 return result;
1707 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1708 TALLOC_CTX *mem_ctx,
1709 const DOM_SID *group_sid,
1710 const char *member)
1712 struct policy_handle connect_pol, domain_pol;
1713 NTSTATUS result;
1714 uint32 group_rid;
1715 struct policy_handle group_pol;
1717 struct samr_Ids rids, rid_types;
1718 struct lsa_String lsa_acct_name;
1720 DOM_SID sid;
1722 sid_copy(&sid, group_sid);
1724 if (!sid_split_rid(&sid, &group_rid)) {
1725 return NT_STATUS_UNSUCCESSFUL;
1728 /* Get sam policy handle */
1729 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1730 pipe_hnd->desthost,
1731 MAXIMUM_ALLOWED_ACCESS,
1732 &connect_pol);
1733 if (!NT_STATUS_IS_OK(result)) {
1734 return result;
1737 /* Get domain policy handle */
1738 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1739 &connect_pol,
1740 MAXIMUM_ALLOWED_ACCESS,
1741 &sid,
1742 &domain_pol);
1743 if (!NT_STATUS_IS_OK(result)) {
1744 return result;
1747 init_lsa_String(&lsa_acct_name, member);
1749 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1750 &domain_pol,
1752 &lsa_acct_name,
1753 &rids,
1754 &rid_types);
1756 if (!NT_STATUS_IS_OK(result)) {
1757 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1758 goto done;
1761 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1762 &domain_pol,
1763 MAXIMUM_ALLOWED_ACCESS,
1764 group_rid,
1765 &group_pol);
1767 if (!NT_STATUS_IS_OK(result)) {
1768 goto done;
1771 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1772 &group_pol,
1773 rids.ids[0],
1774 0x0005); /* unknown flags */
1776 done:
1777 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1778 return result;
1781 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1782 TALLOC_CTX *mem_ctx,
1783 const DOM_SID *alias_sid,
1784 const char *member)
1786 struct policy_handle connect_pol, domain_pol;
1787 NTSTATUS result;
1788 uint32 alias_rid;
1789 struct policy_handle alias_pol;
1791 DOM_SID member_sid;
1792 enum lsa_SidType member_type;
1794 DOM_SID sid;
1796 sid_copy(&sid, alias_sid);
1798 if (!sid_split_rid(&sid, &alias_rid)) {
1799 return NT_STATUS_UNSUCCESSFUL;
1802 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1803 member, &member_sid, &member_type);
1805 if (!NT_STATUS_IS_OK(result)) {
1806 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1807 return result;
1810 /* Get sam policy handle */
1811 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1812 pipe_hnd->desthost,
1813 MAXIMUM_ALLOWED_ACCESS,
1814 &connect_pol);
1815 if (!NT_STATUS_IS_OK(result)) {
1816 goto done;
1819 /* Get domain policy handle */
1820 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1821 &connect_pol,
1822 MAXIMUM_ALLOWED_ACCESS,
1823 &sid,
1824 &domain_pol);
1825 if (!NT_STATUS_IS_OK(result)) {
1826 goto done;
1829 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1830 &domain_pol,
1831 MAXIMUM_ALLOWED_ACCESS,
1832 alias_rid,
1833 &alias_pol);
1835 if (!NT_STATUS_IS_OK(result)) {
1836 return result;
1839 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1840 &alias_pol,
1841 &member_sid);
1843 if (!NT_STATUS_IS_OK(result)) {
1844 return result;
1847 done:
1848 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1849 return result;
1852 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1853 const DOM_SID *domain_sid,
1854 const char *domain_name,
1855 struct cli_state *cli,
1856 struct rpc_pipe_client *pipe_hnd,
1857 TALLOC_CTX *mem_ctx,
1858 int argc,
1859 const char **argv)
1861 DOM_SID group_sid;
1862 enum lsa_SidType group_type;
1864 if (argc != 2 || c->display_usage) {
1865 d_printf("Usage:\n"
1866 "net rpc group addmem <group> <member>\n"
1867 " Add a member to a group\n"
1868 " group\tGroup to add member to\n"
1869 " member\tMember to add to group\n");
1870 return NT_STATUS_UNSUCCESSFUL;
1873 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1874 &group_sid, &group_type))) {
1875 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
1876 return NT_STATUS_UNSUCCESSFUL;
1879 if (group_type == SID_NAME_DOM_GRP) {
1880 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
1881 &group_sid, argv[1]);
1883 if (!NT_STATUS_IS_OK(result)) {
1884 d_fprintf(stderr, "Could not add %s to %s: %s\n",
1885 argv[1], argv[0], nt_errstr(result));
1887 return result;
1890 if (group_type == SID_NAME_ALIAS) {
1891 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
1892 &group_sid, argv[1]);
1894 if (!NT_STATUS_IS_OK(result)) {
1895 d_fprintf(stderr, "Could not add %s to %s: %s\n",
1896 argv[1], argv[0], nt_errstr(result));
1898 return result;
1901 d_fprintf(stderr, "Can only add members to global or local groups "
1902 "which %s is not\n", argv[0]);
1904 return NT_STATUS_UNSUCCESSFUL;
1907 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
1909 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1910 rpc_group_addmem_internals,
1911 argc, argv);
1914 static NTSTATUS rpc_del_groupmem(struct net_context *c,
1915 struct rpc_pipe_client *pipe_hnd,
1916 TALLOC_CTX *mem_ctx,
1917 const DOM_SID *group_sid,
1918 const char *member)
1920 struct policy_handle connect_pol, domain_pol;
1921 NTSTATUS result;
1922 uint32 group_rid;
1923 struct policy_handle group_pol;
1925 struct samr_Ids rids, rid_types;
1926 struct lsa_String lsa_acct_name;
1928 DOM_SID sid;
1930 sid_copy(&sid, group_sid);
1932 if (!sid_split_rid(&sid, &group_rid))
1933 return NT_STATUS_UNSUCCESSFUL;
1935 /* Get sam policy handle */
1936 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1937 pipe_hnd->desthost,
1938 MAXIMUM_ALLOWED_ACCESS,
1939 &connect_pol);
1940 if (!NT_STATUS_IS_OK(result))
1941 return result;
1943 /* Get domain policy handle */
1944 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1945 &connect_pol,
1946 MAXIMUM_ALLOWED_ACCESS,
1947 &sid,
1948 &domain_pol);
1949 if (!NT_STATUS_IS_OK(result))
1950 return result;
1952 init_lsa_String(&lsa_acct_name, member);
1954 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1955 &domain_pol,
1957 &lsa_acct_name,
1958 &rids,
1959 &rid_types);
1960 if (!NT_STATUS_IS_OK(result)) {
1961 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1962 goto done;
1965 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1966 &domain_pol,
1967 MAXIMUM_ALLOWED_ACCESS,
1968 group_rid,
1969 &group_pol);
1971 if (!NT_STATUS_IS_OK(result))
1972 goto done;
1974 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1975 &group_pol,
1976 rids.ids[0]);
1978 done:
1979 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1980 return result;
1983 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
1984 TALLOC_CTX *mem_ctx,
1985 const DOM_SID *alias_sid,
1986 const char *member)
1988 struct policy_handle connect_pol, domain_pol;
1989 NTSTATUS result;
1990 uint32 alias_rid;
1991 struct policy_handle alias_pol;
1993 DOM_SID member_sid;
1994 enum lsa_SidType member_type;
1996 DOM_SID sid;
1998 sid_copy(&sid, alias_sid);
2000 if (!sid_split_rid(&sid, &alias_rid))
2001 return NT_STATUS_UNSUCCESSFUL;
2003 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2004 member, &member_sid, &member_type);
2006 if (!NT_STATUS_IS_OK(result)) {
2007 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2008 return result;
2011 /* Get sam policy handle */
2012 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2013 pipe_hnd->desthost,
2014 MAXIMUM_ALLOWED_ACCESS,
2015 &connect_pol);
2016 if (!NT_STATUS_IS_OK(result)) {
2017 goto done;
2020 /* Get domain policy handle */
2021 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2022 &connect_pol,
2023 MAXIMUM_ALLOWED_ACCESS,
2024 &sid,
2025 &domain_pol);
2026 if (!NT_STATUS_IS_OK(result)) {
2027 goto done;
2030 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2031 &domain_pol,
2032 MAXIMUM_ALLOWED_ACCESS,
2033 alias_rid,
2034 &alias_pol);
2036 if (!NT_STATUS_IS_OK(result))
2037 return result;
2039 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2040 &alias_pol,
2041 &member_sid);
2043 if (!NT_STATUS_IS_OK(result))
2044 return result;
2046 done:
2047 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2048 return result;
2051 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2052 const DOM_SID *domain_sid,
2053 const char *domain_name,
2054 struct cli_state *cli,
2055 struct rpc_pipe_client *pipe_hnd,
2056 TALLOC_CTX *mem_ctx,
2057 int argc,
2058 const char **argv)
2060 DOM_SID group_sid;
2061 enum lsa_SidType group_type;
2063 if (argc != 2 || c->display_usage) {
2064 d_printf("Usage:\n"
2065 "net rpc group delmem <group> <member>\n"
2066 " Delete a member from a group\n"
2067 " group\tGroup to delete member from\n"
2068 " member\tMember to delete from group\n");
2069 return NT_STATUS_UNSUCCESSFUL;
2072 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2073 &group_sid, &group_type))) {
2074 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2075 return NT_STATUS_UNSUCCESSFUL;
2078 if (group_type == SID_NAME_DOM_GRP) {
2079 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2080 &group_sid, argv[1]);
2082 if (!NT_STATUS_IS_OK(result)) {
2083 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2084 argv[1], argv[0], nt_errstr(result));
2086 return result;
2089 if (group_type == SID_NAME_ALIAS) {
2090 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2091 &group_sid, argv[1]);
2093 if (!NT_STATUS_IS_OK(result)) {
2094 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2095 argv[1], argv[0], nt_errstr(result));
2097 return result;
2100 d_fprintf(stderr, "Can only delete members from global or local groups "
2101 "which %s is not\n", argv[0]);
2103 return NT_STATUS_UNSUCCESSFUL;
2106 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2108 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2109 rpc_group_delmem_internals,
2110 argc, argv);
2114 * List groups on a remote RPC server.
2116 * All parameters are provided by the run_rpc_command function, except for
2117 * argc, argv which are passes through.
2119 * @param domain_sid The domain sid acquired from the remote server.
2120 * @param cli A cli_state connected to the server.
2121 * @param mem_ctx Talloc context, destroyed on completion of the function.
2122 * @param argc Standard main() style argc.
2123 * @param argv Standard main() style argv. Initial components are already
2124 * stripped.
2126 * @return Normal NTSTATUS return.
2129 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2130 const DOM_SID *domain_sid,
2131 const char *domain_name,
2132 struct cli_state *cli,
2133 struct rpc_pipe_client *pipe_hnd,
2134 TALLOC_CTX *mem_ctx,
2135 int argc,
2136 const char **argv)
2138 struct policy_handle connect_pol, domain_pol;
2139 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2140 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2141 struct samr_SamArray *groups = NULL;
2142 bool global = false;
2143 bool local = false;
2144 bool builtin = false;
2146 if (c->display_usage) {
2147 d_printf("Usage:\n"
2148 "net rpc group list [global] [local] [builtin]\n"
2149 " List groups on RPC server\n"
2150 " global\tList global groups\n"
2151 " local\tList local groups\n"
2152 " builtin\tList builtin groups\n"
2153 " If none of global, local or builtin is "
2154 "specified, all three options are considered set\n");
2155 return NT_STATUS_OK;
2158 if (argc == 0) {
2159 global = true;
2160 local = true;
2161 builtin = true;
2164 for (i=0; i<argc; i++) {
2165 if (strequal(argv[i], "global"))
2166 global = true;
2168 if (strequal(argv[i], "local"))
2169 local = true;
2171 if (strequal(argv[i], "builtin"))
2172 builtin = true;
2175 /* Get sam policy handle */
2177 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2178 pipe_hnd->desthost,
2179 MAXIMUM_ALLOWED_ACCESS,
2180 &connect_pol);
2181 if (!NT_STATUS_IS_OK(result)) {
2182 goto done;
2185 /* Get domain policy handle */
2187 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2188 &connect_pol,
2189 MAXIMUM_ALLOWED_ACCESS,
2190 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2191 &domain_pol);
2192 if (!NT_STATUS_IS_OK(result)) {
2193 goto done;
2196 /* Query domain groups */
2197 if (c->opt_long_list_entries)
2198 d_printf("\nGroup name Comment"
2199 "\n-----------------------------\n");
2200 do {
2201 uint32_t max_size, total_size, returned_size;
2202 union samr_DispInfo info;
2204 if (!global) break;
2206 get_query_dispinfo_params(
2207 loop_count, &max_entries, &max_size);
2209 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2210 &domain_pol,
2212 start_idx,
2213 max_entries,
2214 max_size,
2215 &total_size,
2216 &returned_size,
2217 &info);
2218 num_entries = info.info3.count;
2219 start_idx += info.info3.count;
2221 if (!NT_STATUS_IS_OK(result) &&
2222 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2223 break;
2225 for (i = 0; i < num_entries; i++) {
2227 const char *group = NULL;
2228 const char *desc = NULL;
2230 group = info.info3.entries[i].account_name.string;
2231 desc = info.info3.entries[i].description.string;
2233 if (c->opt_long_list_entries)
2234 printf("%-21.21s %-50.50s\n",
2235 group, desc);
2236 else
2237 printf("%s\n", group);
2239 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2240 /* query domain aliases */
2241 start_idx = 0;
2242 do {
2243 if (!local) break;
2245 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2246 &domain_pol,
2247 &start_idx,
2248 &groups,
2249 0xffff,
2250 &num_entries);
2251 if (!NT_STATUS_IS_OK(result) &&
2252 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2253 break;
2255 for (i = 0; i < num_entries; i++) {
2257 const char *description = NULL;
2259 if (c->opt_long_list_entries) {
2261 struct policy_handle alias_pol;
2262 union samr_AliasInfo *info = NULL;
2264 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2265 &domain_pol,
2266 0x8,
2267 groups->entries[i].idx,
2268 &alias_pol))) &&
2269 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2270 &alias_pol,
2272 &info))) &&
2273 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2274 &alias_pol)))) {
2275 description = info->description.string;
2279 if (description != NULL) {
2280 printf("%-21.21s %-50.50s\n",
2281 groups->entries[i].name.string,
2282 description);
2283 } else {
2284 printf("%s\n", groups->entries[i].name.string);
2287 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2288 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2289 /* Get builtin policy handle */
2291 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2292 &connect_pol,
2293 MAXIMUM_ALLOWED_ACCESS,
2294 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2295 &domain_pol);
2296 if (!NT_STATUS_IS_OK(result)) {
2297 goto done;
2299 /* query builtin aliases */
2300 start_idx = 0;
2301 do {
2302 if (!builtin) break;
2304 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2305 &domain_pol,
2306 &start_idx,
2307 &groups,
2308 max_entries,
2309 &num_entries);
2310 if (!NT_STATUS_IS_OK(result) &&
2311 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2312 break;
2314 for (i = 0; i < num_entries; i++) {
2316 const char *description = NULL;
2318 if (c->opt_long_list_entries) {
2320 struct policy_handle alias_pol;
2321 union samr_AliasInfo *info = NULL;
2323 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2324 &domain_pol,
2325 0x8,
2326 groups->entries[i].idx,
2327 &alias_pol))) &&
2328 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2329 &alias_pol,
2331 &info))) &&
2332 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2333 &alias_pol)))) {
2334 description = info->description.string;
2338 if (description != NULL) {
2339 printf("%-21.21s %-50.50s\n",
2340 groups->entries[i].name.string,
2341 description);
2342 } else {
2343 printf("%s\n", groups->entries[i].name.string);
2346 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2348 done:
2349 return result;
2352 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2354 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2355 rpc_group_list_internals,
2356 argc, argv);
2359 static NTSTATUS rpc_list_group_members(struct net_context *c,
2360 struct rpc_pipe_client *pipe_hnd,
2361 TALLOC_CTX *mem_ctx,
2362 const char *domain_name,
2363 const DOM_SID *domain_sid,
2364 struct policy_handle *domain_pol,
2365 uint32 rid)
2367 NTSTATUS result;
2368 struct policy_handle group_pol;
2369 uint32 num_members, *group_rids;
2370 int i;
2371 struct samr_RidTypeArray *rids = NULL;
2372 struct lsa_Strings names;
2373 struct samr_Ids types;
2375 fstring sid_str;
2376 sid_to_fstring(sid_str, domain_sid);
2378 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2379 domain_pol,
2380 MAXIMUM_ALLOWED_ACCESS,
2381 rid,
2382 &group_pol);
2384 if (!NT_STATUS_IS_OK(result))
2385 return result;
2387 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2388 &group_pol,
2389 &rids);
2391 if (!NT_STATUS_IS_OK(result))
2392 return result;
2394 num_members = rids->count;
2395 group_rids = rids->rids;
2397 while (num_members > 0) {
2398 int this_time = 512;
2400 if (num_members < this_time)
2401 this_time = num_members;
2403 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2404 domain_pol,
2405 this_time,
2406 group_rids,
2407 &names,
2408 &types);
2410 if (!NT_STATUS_IS_OK(result))
2411 return result;
2413 /* We only have users as members, but make the output
2414 the same as the output of alias members */
2416 for (i = 0; i < this_time; i++) {
2418 if (c->opt_long_list_entries) {
2419 printf("%s-%d %s\\%s %d\n", sid_str,
2420 group_rids[i], domain_name,
2421 names.names[i].string,
2422 SID_NAME_USER);
2423 } else {
2424 printf("%s\\%s\n", domain_name,
2425 names.names[i].string);
2429 num_members -= this_time;
2430 group_rids += 512;
2433 return NT_STATUS_OK;
2436 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2437 struct rpc_pipe_client *pipe_hnd,
2438 TALLOC_CTX *mem_ctx,
2439 struct policy_handle *domain_pol,
2440 uint32 rid)
2442 NTSTATUS result;
2443 struct rpc_pipe_client *lsa_pipe;
2444 struct policy_handle alias_pol, lsa_pol;
2445 uint32 num_members;
2446 DOM_SID *alias_sids;
2447 char **domains;
2448 char **names;
2449 enum lsa_SidType *types;
2450 int i;
2451 struct lsa_SidArray sid_array;
2453 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2454 domain_pol,
2455 MAXIMUM_ALLOWED_ACCESS,
2456 rid,
2457 &alias_pol);
2459 if (!NT_STATUS_IS_OK(result))
2460 return result;
2462 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2463 &alias_pol,
2464 &sid_array);
2466 if (!NT_STATUS_IS_OK(result)) {
2467 d_fprintf(stderr, "Couldn't list alias members\n");
2468 return result;
2471 num_members = sid_array.num_sids;
2473 if (num_members == 0) {
2474 return NT_STATUS_OK;
2477 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2478 &ndr_table_lsarpc.syntax_id,
2479 &lsa_pipe);
2480 if (!NT_STATUS_IS_OK(result)) {
2481 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2482 nt_errstr(result) );
2483 return result;
2486 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2487 SEC_FLAG_MAXIMUM_ALLOWED, &lsa_pol);
2489 if (!NT_STATUS_IS_OK(result)) {
2490 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2491 TALLOC_FREE(lsa_pipe);
2492 return result;
2495 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2496 if (!alias_sids) {
2497 d_fprintf(stderr, "Out of memory\n");
2498 TALLOC_FREE(lsa_pipe);
2499 return NT_STATUS_NO_MEMORY;
2502 for (i=0; i<num_members; i++) {
2503 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2506 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2507 num_members, alias_sids,
2508 &domains, &names, &types);
2510 if (!NT_STATUS_IS_OK(result) &&
2511 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2512 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2513 TALLOC_FREE(lsa_pipe);
2514 return result;
2517 for (i = 0; i < num_members; i++) {
2518 fstring sid_str;
2519 sid_to_fstring(sid_str, &alias_sids[i]);
2521 if (c->opt_long_list_entries) {
2522 printf("%s %s\\%s %d\n", sid_str,
2523 domains[i] ? domains[i] : "*unknown*",
2524 names[i] ? names[i] : "*unknown*", types[i]);
2525 } else {
2526 if (domains[i])
2527 printf("%s\\%s\n", domains[i], names[i]);
2528 else
2529 printf("%s\n", sid_str);
2533 TALLOC_FREE(lsa_pipe);
2534 return NT_STATUS_OK;
2537 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2538 const DOM_SID *domain_sid,
2539 const char *domain_name,
2540 struct cli_state *cli,
2541 struct rpc_pipe_client *pipe_hnd,
2542 TALLOC_CTX *mem_ctx,
2543 int argc,
2544 const char **argv)
2546 NTSTATUS result;
2547 struct policy_handle connect_pol, domain_pol;
2548 struct samr_Ids rids, rid_types;
2549 struct lsa_String lsa_acct_name;
2551 /* Get sam policy handle */
2553 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2554 pipe_hnd->desthost,
2555 MAXIMUM_ALLOWED_ACCESS,
2556 &connect_pol);
2558 if (!NT_STATUS_IS_OK(result))
2559 return result;
2561 /* Get domain policy handle */
2563 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2564 &connect_pol,
2565 MAXIMUM_ALLOWED_ACCESS,
2566 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2567 &domain_pol);
2569 if (!NT_STATUS_IS_OK(result))
2570 return result;
2572 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2574 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2575 &domain_pol,
2577 &lsa_acct_name,
2578 &rids,
2579 &rid_types);
2581 if (!NT_STATUS_IS_OK(result)) {
2583 /* Ok, did not find it in the global sam, try with builtin */
2585 DOM_SID sid_Builtin;
2587 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2589 sid_copy(&sid_Builtin, &global_sid_Builtin);
2591 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2592 &connect_pol,
2593 MAXIMUM_ALLOWED_ACCESS,
2594 &sid_Builtin,
2595 &domain_pol);
2597 if (!NT_STATUS_IS_OK(result)) {
2598 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2599 return result;
2602 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2603 &domain_pol,
2605 &lsa_acct_name,
2606 &rids,
2607 &rid_types);
2609 if (!NT_STATUS_IS_OK(result)) {
2610 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2611 return result;
2615 if (rids.count != 1) {
2616 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2617 return result;
2620 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2621 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2622 domain_sid, &domain_pol,
2623 rids.ids[0]);
2626 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2627 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2628 rids.ids[0]);
2631 return NT_STATUS_NO_SUCH_GROUP;
2634 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2636 if (argc != 1 || c->display_usage) {
2637 return rpc_group_usage(c, argc, argv);
2640 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2641 rpc_group_members_internals,
2642 argc, argv);
2645 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2647 NET_API_STATUS status;
2648 struct GROUP_INFO_0 g0;
2649 uint32_t parm_err;
2651 if (argc != 2) {
2652 d_printf("Usage: 'net rpc group rename group newname'\n");
2653 return -1;
2656 g0.grpi0_name = argv[1];
2658 status = NetGroupSetInfo(c->opt_host,
2659 argv[0],
2661 (uint8_t *)&g0,
2662 &parm_err);
2664 if (status != 0) {
2665 d_fprintf(stderr, "Renaming group %s failed with: %s\n",
2666 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2667 status));
2668 return -1;
2671 return 0;
2674 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2676 if (argc != 2 || c->display_usage) {
2677 return rpc_group_usage(c, argc, argv);
2680 return rpc_group_rename_internals(c, argc, argv);
2684 * 'net rpc group' entrypoint.
2685 * @param argc Standard main() style argc.
2686 * @param argv Standard main() style argv. Initial components are already
2687 * stripped.
2690 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2692 NET_API_STATUS status;
2694 struct functable func[] = {
2696 "add",
2697 rpc_group_add,
2698 NET_TRANSPORT_RPC,
2699 "Create specified group",
2700 "net rpc group add\n"
2701 " Create specified group"
2704 "delete",
2705 rpc_group_delete,
2706 NET_TRANSPORT_RPC,
2707 "Delete specified group",
2708 "net rpc group delete\n"
2709 " Delete specified group"
2712 "addmem",
2713 rpc_group_addmem,
2714 NET_TRANSPORT_RPC,
2715 "Add member to group",
2716 "net rpc group addmem\n"
2717 " Add member to group"
2720 "delmem",
2721 rpc_group_delmem,
2722 NET_TRANSPORT_RPC,
2723 "Remove member from group",
2724 "net rpc group delmem\n"
2725 " Remove member from group"
2728 "list",
2729 rpc_group_list,
2730 NET_TRANSPORT_RPC,
2731 "List groups",
2732 "net rpc group list\n"
2733 " List groups"
2736 "members",
2737 rpc_group_members,
2738 NET_TRANSPORT_RPC,
2739 "List group members",
2740 "net rpc group members\n"
2741 " List group members"
2744 "rename",
2745 rpc_group_rename,
2746 NET_TRANSPORT_RPC,
2747 "Rename group",
2748 "net rpc group rename\n"
2749 " Rename group"
2751 {NULL, NULL, 0, NULL, NULL}
2754 status = libnetapi_init(&c->netapi_ctx);
2755 if (status != 0) {
2756 return -1;
2758 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2759 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2760 if (c->opt_kerberos) {
2761 libnetapi_set_use_kerberos(c->netapi_ctx);
2764 if (argc == 0) {
2765 if (c->display_usage) {
2766 d_printf("Usage:\n");
2767 d_printf("net rpc group\n"
2768 " Alias for net rpc group list global local "
2769 "builtin\n");
2770 net_display_usage_from_functable(func);
2771 return 0;
2774 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2775 rpc_group_list_internals,
2776 argc, argv);
2779 return net_run_function(c, argc, argv, "net rpc group", func);
2782 /****************************************************************************/
2784 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2786 return net_share_usage(c, argc, argv);
2790 * Add a share on a remote RPC server.
2792 * @param argc Standard main() style argc.
2793 * @param argv Standard main() style argv. Initial components are already
2794 * stripped.
2796 * @return A shell status integer (0 for success).
2799 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2801 NET_API_STATUS status;
2802 char *sharename;
2803 char *path;
2804 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2805 uint32 num_users=0, perms=0;
2806 char *password=NULL; /* don't allow a share password */
2807 struct SHARE_INFO_2 i2;
2808 uint32_t parm_error = 0;
2810 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2811 return rpc_share_usage(c, argc, argv);
2814 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2815 return -1;
2818 path = strchr(sharename, '=');
2819 if (!path) {
2820 return -1;
2823 *path++ = '\0';
2825 i2.shi2_netname = sharename;
2826 i2.shi2_type = type;
2827 i2.shi2_remark = c->opt_comment;
2828 i2.shi2_permissions = perms;
2829 i2.shi2_max_uses = c->opt_maxusers;
2830 i2.shi2_current_uses = num_users;
2831 i2.shi2_path = path;
2832 i2.shi2_passwd = password;
2834 status = NetShareAdd(c->opt_host,
2836 (uint8_t *)&i2,
2837 &parm_error);
2838 if (status != 0) {
2839 printf("NetShareAdd failed with: %s\n",
2840 libnetapi_get_error_string(c->netapi_ctx, status));
2843 return status;
2847 * Delete a share on a remote RPC server.
2849 * @param domain_sid The domain sid acquired from the remote server.
2850 * @param argc Standard main() style argc.
2851 * @param argv Standard main() style argv. Initial components are already
2852 * stripped.
2854 * @return A shell status integer (0 for success).
2856 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
2858 if (argc < 1 || c->display_usage) {
2859 return rpc_share_usage(c, argc, argv);
2862 return NetShareDel(c->opt_host, argv[0], 0);
2866 * Formatted print of share info
2868 * @param r pointer to SHARE_INFO_1 to format
2871 static void display_share_info_1(struct net_context *c,
2872 struct SHARE_INFO_1 *r)
2874 if (c->opt_long_list_entries) {
2875 d_printf("%-12s %-8.8s %-50s\n",
2876 r->shi1_netname,
2877 net_share_type_str(r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)),
2878 r->shi1_remark);
2879 } else {
2880 d_printf("%s\n", r->shi1_netname);
2884 static WERROR get_share_info(struct net_context *c,
2885 struct rpc_pipe_client *pipe_hnd,
2886 TALLOC_CTX *mem_ctx,
2887 uint32 level,
2888 int argc,
2889 const char **argv,
2890 struct srvsvc_NetShareInfoCtr *info_ctr)
2892 WERROR result;
2893 NTSTATUS status;
2894 union srvsvc_NetShareInfo info;
2896 /* no specific share requested, enumerate all */
2897 if (argc == 0) {
2899 uint32_t preferred_len = 0xffffffff;
2900 uint32_t total_entries = 0;
2901 uint32_t resume_handle = 0;
2903 info_ctr->level = level;
2905 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
2906 pipe_hnd->desthost,
2907 info_ctr,
2908 preferred_len,
2909 &total_entries,
2910 &resume_handle,
2911 &result);
2912 return result;
2915 /* request just one share */
2916 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
2917 pipe_hnd->desthost,
2918 argv[0],
2919 level,
2920 &info,
2921 &result);
2923 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
2924 goto done;
2927 /* construct ctr */
2928 ZERO_STRUCTP(info_ctr);
2930 info_ctr->level = level;
2932 switch (level) {
2933 case 1:
2935 struct srvsvc_NetShareCtr1 *ctr1;
2937 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
2938 W_ERROR_HAVE_NO_MEMORY(ctr1);
2940 ctr1->count = 1;
2941 ctr1->array = info.info1;
2943 info_ctr->ctr.ctr1 = ctr1;
2945 case 2:
2947 struct srvsvc_NetShareCtr2 *ctr2;
2949 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
2950 W_ERROR_HAVE_NO_MEMORY(ctr2);
2952 ctr2->count = 1;
2953 ctr2->array = info.info2;
2955 info_ctr->ctr.ctr2 = ctr2;
2957 case 502:
2959 struct srvsvc_NetShareCtr502 *ctr502;
2961 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
2962 W_ERROR_HAVE_NO_MEMORY(ctr502);
2964 ctr502->count = 1;
2965 ctr502->array = info.info502;
2967 info_ctr->ctr.ctr502 = ctr502;
2969 } /* switch */
2970 done:
2971 return result;
2974 /***
2975 * 'net rpc share list' entrypoint.
2976 * @param argc Standard main() style argc.
2977 * @param argv Standard main() style argv. Initial components are already
2978 * stripped.
2980 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
2982 NET_API_STATUS status;
2983 struct SHARE_INFO_1 *i1 = NULL;
2984 uint32_t entries_read = 0;
2985 uint32_t total_entries = 0;
2986 uint32_t resume_handle = 0;
2987 uint32_t i, level = 1;
2989 if (c->display_usage) {
2990 d_printf("Usage\n"
2991 "net rpc share list\n"
2992 " List shares on remote server\n");
2993 return 0;
2996 status = NetShareEnum(c->opt_host,
2997 level,
2998 (uint8_t **)(void *)&i1,
2999 (uint32_t)-1,
3000 &entries_read,
3001 &total_entries,
3002 &resume_handle);
3003 if (status != 0) {
3004 goto done;
3007 /* Display results */
3009 if (c->opt_long_list_entries) {
3010 d_printf(
3011 "\nEnumerating shared resources (exports) on remote server:\n\n"
3012 "\nShare name Type Description\n"
3013 "---------- ---- -----------\n");
3015 for (i = 0; i < entries_read; i++)
3016 display_share_info_1(c, &i1[i]);
3017 done:
3018 return status;
3021 static bool check_share_availability(struct cli_state *cli, const char *netname)
3023 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
3024 d_printf("skipping [%s]: not a file share.\n", netname);
3025 return false;
3028 if (!cli_tdis(cli))
3029 return false;
3031 return true;
3034 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3035 const char *netname, uint32 type)
3037 /* only support disk shares */
3038 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3039 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3040 return false;
3043 /* skip builtin shares */
3044 /* FIXME: should print$ be added too ? */
3045 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3046 strequal(netname,"global"))
3047 return false;
3049 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3050 printf("excluding [%s]\n", netname);
3051 return false;
3054 return check_share_availability(cli, netname);
3058 * Migrate shares from a remote RPC server to the local RPC server.
3060 * All parameters are provided by the run_rpc_command function, except for
3061 * argc, argv which are passed through.
3063 * @param domain_sid The domain sid acquired from the remote server.
3064 * @param cli A cli_state connected to the server.
3065 * @param mem_ctx Talloc context, destroyed on completion of the function.
3066 * @param argc Standard main() style argc.
3067 * @param argv Standard main() style argv. Initial components are already
3068 * stripped.
3070 * @return Normal NTSTATUS return.
3073 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3074 const DOM_SID *domain_sid,
3075 const char *domain_name,
3076 struct cli_state *cli,
3077 struct rpc_pipe_client *pipe_hnd,
3078 TALLOC_CTX *mem_ctx,
3079 int argc,
3080 const char **argv)
3082 WERROR result;
3083 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3084 struct srvsvc_NetShareInfoCtr ctr_src;
3085 uint32 i;
3086 struct rpc_pipe_client *srvsvc_pipe = NULL;
3087 struct cli_state *cli_dst = NULL;
3088 uint32 level = 502; /* includes secdesc */
3089 uint32_t parm_error = 0;
3091 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3092 &ctr_src);
3093 if (!W_ERROR_IS_OK(result))
3094 goto done;
3096 /* connect destination PI_SRVSVC */
3097 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3098 &ndr_table_srvsvc.syntax_id);
3099 if (!NT_STATUS_IS_OK(nt_status))
3100 return nt_status;
3103 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3105 union srvsvc_NetShareInfo info;
3106 struct srvsvc_NetShareInfo502 info502 =
3107 ctr_src.ctr.ctr502->array[i];
3109 /* reset error-code */
3110 nt_status = NT_STATUS_UNSUCCESSFUL;
3112 if (!check_share_sanity(c, cli, info502.name, info502.type))
3113 continue;
3115 /* finally add the share on the dst server */
3117 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3118 info502.name, info502.path, info502.comment);
3120 info.info502 = &info502;
3122 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3123 srvsvc_pipe->desthost,
3124 502,
3125 &info,
3126 &parm_error,
3127 &result);
3129 if (W_ERROR_V(result) == W_ERROR_V(WERR_FILE_EXISTS)) {
3130 printf(" [%s] does already exist\n",
3131 info502.name);
3132 continue;
3135 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3136 printf("cannot add share: %s\n", win_errstr(result));
3137 goto done;
3142 nt_status = NT_STATUS_OK;
3144 done:
3145 if (cli_dst) {
3146 cli_shutdown(cli_dst);
3149 return nt_status;
3154 * Migrate shares from a RPC server to another.
3156 * @param argc Standard main() style argc.
3157 * @param argv Standard main() style argv. Initial components are already
3158 * stripped.
3160 * @return A shell status integer (0 for success).
3162 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3163 const char **argv)
3165 if (c->display_usage) {
3166 d_printf("Usage:\n"
3167 "net rpc share migrate shares\n"
3168 " Migrate shares to local server\n");
3169 return 0;
3172 if (!c->opt_host) {
3173 printf("no server to migrate\n");
3174 return -1;
3177 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3178 rpc_share_migrate_shares_internals,
3179 argc, argv);
3183 * Copy a file/dir
3185 * @param f file_info
3186 * @param mask current search mask
3187 * @param state arg-pointer
3190 static void copy_fn(const char *mnt, file_info *f,
3191 const char *mask, void *state)
3193 static NTSTATUS nt_status;
3194 static struct copy_clistate *local_state;
3195 static fstring filename, new_mask;
3196 fstring dir;
3197 char *old_dir;
3198 struct net_context *c;
3200 local_state = (struct copy_clistate *)state;
3201 nt_status = NT_STATUS_UNSUCCESSFUL;
3203 c = local_state->c;
3205 if (strequal(f->name, ".") || strequal(f->name, ".."))
3206 return;
3208 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3210 /* DIRECTORY */
3211 if (f->mode & aDIR) {
3213 DEBUG(3,("got dir: %s\n", f->name));
3215 fstrcpy(dir, local_state->cwd);
3216 fstrcat(dir, "\\");
3217 fstrcat(dir, f->name);
3219 switch (net_mode_share)
3221 case NET_MODE_SHARE_MIGRATE:
3222 /* create that directory */
3223 nt_status = net_copy_file(c, local_state->mem_ctx,
3224 local_state->cli_share_src,
3225 local_state->cli_share_dst,
3226 dir, dir,
3227 c->opt_acls? true : false,
3228 c->opt_attrs? true : false,
3229 c->opt_timestamps? true:false,
3230 false);
3231 break;
3232 default:
3233 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3234 return;
3237 if (!NT_STATUS_IS_OK(nt_status))
3238 printf("could not handle dir %s: %s\n",
3239 dir, nt_errstr(nt_status));
3241 /* search below that directory */
3242 fstrcpy(new_mask, dir);
3243 fstrcat(new_mask, "\\*");
3245 old_dir = local_state->cwd;
3246 local_state->cwd = dir;
3247 if (!sync_files(local_state, new_mask))
3248 printf("could not handle files\n");
3249 local_state->cwd = old_dir;
3251 return;
3255 /* FILE */
3256 fstrcpy(filename, local_state->cwd);
3257 fstrcat(filename, "\\");
3258 fstrcat(filename, f->name);
3260 DEBUG(3,("got file: %s\n", filename));
3262 switch (net_mode_share)
3264 case NET_MODE_SHARE_MIGRATE:
3265 nt_status = net_copy_file(c, local_state->mem_ctx,
3266 local_state->cli_share_src,
3267 local_state->cli_share_dst,
3268 filename, filename,
3269 c->opt_acls? true : false,
3270 c->opt_attrs? true : false,
3271 c->opt_timestamps? true: false,
3272 true);
3273 break;
3274 default:
3275 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3276 return;
3279 if (!NT_STATUS_IS_OK(nt_status))
3280 printf("could not handle file %s: %s\n",
3281 filename, nt_errstr(nt_status));
3286 * sync files, can be called recursivly to list files
3287 * and then call copy_fn for each file
3289 * @param cp_clistate pointer to the copy_clistate we work with
3290 * @param mask the current search mask
3292 * @return Boolean result
3294 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3296 struct cli_state *targetcli;
3297 char *targetpath = NULL;
3299 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3301 if ( !cli_resolve_path(talloc_tos(), "", NULL, cp_clistate->cli_share_src,
3302 mask, &targetcli, &targetpath ) ) {
3303 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3304 mask, cli_errstr(cp_clistate->cli_share_src));
3305 return false;
3308 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3309 d_fprintf(stderr, "listing %s failed with error: %s\n",
3310 mask, cli_errstr(targetcli));
3311 return false;
3314 return true;
3319 * Set the top level directory permissions before we do any further copies.
3320 * Should set up ACL inheritance.
3323 bool copy_top_level_perms(struct net_context *c,
3324 struct copy_clistate *cp_clistate,
3325 const char *sharename)
3327 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3329 switch (net_mode_share) {
3330 case NET_MODE_SHARE_MIGRATE:
3331 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3332 nt_status = net_copy_fileattr(c,
3333 cp_clistate->mem_ctx,
3334 cp_clistate->cli_share_src,
3335 cp_clistate->cli_share_dst,
3336 "\\", "\\",
3337 c->opt_acls? true : false,
3338 c->opt_attrs? true : false,
3339 c->opt_timestamps? true: false,
3340 false);
3341 break;
3342 default:
3343 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3344 break;
3347 if (!NT_STATUS_IS_OK(nt_status)) {
3348 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3349 sharename, nt_errstr(nt_status));
3350 return false;
3353 return true;
3357 * Sync all files inside a remote share to another share (over smb).
3359 * All parameters are provided by the run_rpc_command function, except for
3360 * argc, argv which are passed through.
3362 * @param domain_sid The domain sid acquired from the remote server.
3363 * @param cli A cli_state connected to the server.
3364 * @param mem_ctx Talloc context, destroyed on completion of the function.
3365 * @param argc Standard main() style argc.
3366 * @param argv Standard main() style argv. Initial components are already
3367 * stripped.
3369 * @return Normal NTSTATUS return.
3372 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3373 const DOM_SID *domain_sid,
3374 const char *domain_name,
3375 struct cli_state *cli,
3376 struct rpc_pipe_client *pipe_hnd,
3377 TALLOC_CTX *mem_ctx,
3378 int argc,
3379 const char **argv)
3381 WERROR result;
3382 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3383 struct srvsvc_NetShareInfoCtr ctr_src;
3384 uint32 i;
3385 uint32 level = 502;
3386 struct copy_clistate cp_clistate;
3387 bool got_src_share = false;
3388 bool got_dst_share = false;
3389 const char *mask = "\\*";
3390 char *dst = NULL;
3392 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3393 if (dst == NULL) {
3394 nt_status = NT_STATUS_NO_MEMORY;
3395 goto done;
3398 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3399 &ctr_src);
3401 if (!W_ERROR_IS_OK(result))
3402 goto done;
3404 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3406 struct srvsvc_NetShareInfo502 info502 =
3407 ctr_src.ctr.ctr502->array[i];
3409 if (!check_share_sanity(c, cli, info502.name, info502.type))
3410 continue;
3412 /* one might not want to mirror whole discs :) */
3413 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3414 d_printf("skipping [%s]: builtin/hidden share\n", info502.name);
3415 continue;
3418 switch (net_mode_share)
3420 case NET_MODE_SHARE_MIGRATE:
3421 printf("syncing");
3422 break;
3423 default:
3424 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3425 break;
3427 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3428 info502.name,
3429 c->opt_acls ? "including" : "without",
3430 c->opt_attrs ? "including" : "without",
3431 c->opt_timestamps ? "(preserving timestamps)" : "");
3433 cp_clistate.mem_ctx = mem_ctx;
3434 cp_clistate.cli_share_src = NULL;
3435 cp_clistate.cli_share_dst = NULL;
3436 cp_clistate.cwd = NULL;
3437 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3438 cp_clistate.c = c;
3440 /* open share source */
3441 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3442 &cli->dest_ss, cli->desthost,
3443 info502.name, "A:");
3444 if (!NT_STATUS_IS_OK(nt_status))
3445 goto done;
3447 got_src_share = true;
3449 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3450 /* open share destination */
3451 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3452 NULL, dst, info502.name, "A:");
3453 if (!NT_STATUS_IS_OK(nt_status))
3454 goto done;
3456 got_dst_share = true;
3459 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3460 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", info502.name);
3461 nt_status = NT_STATUS_UNSUCCESSFUL;
3462 goto done;
3465 if (!sync_files(&cp_clistate, mask)) {
3466 d_fprintf(stderr, "could not handle files for share: %s\n", info502.name);
3467 nt_status = NT_STATUS_UNSUCCESSFUL;
3468 goto done;
3472 nt_status = NT_STATUS_OK;
3474 done:
3476 if (got_src_share)
3477 cli_shutdown(cp_clistate.cli_share_src);
3479 if (got_dst_share)
3480 cli_shutdown(cp_clistate.cli_share_dst);
3482 SAFE_FREE(dst);
3483 return nt_status;
3487 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3489 if (c->display_usage) {
3490 d_printf("Usage:\n"
3491 "net share migrate files\n"
3492 " Migrate files to local server\n");
3493 return 0;
3496 if (!c->opt_host) {
3497 d_printf("no server to migrate\n");
3498 return -1;
3501 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3502 rpc_share_migrate_files_internals,
3503 argc, argv);
3507 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3509 * All parameters are provided by the run_rpc_command function, except for
3510 * argc, argv which are passed through.
3512 * @param domain_sid The domain sid acquired from the remote server.
3513 * @param cli A cli_state connected to the server.
3514 * @param mem_ctx Talloc context, destroyed on completion of the function.
3515 * @param argc Standard main() style argc.
3516 * @param argv Standard main() style argv. Initial components are already
3517 * stripped.
3519 * @return Normal NTSTATUS return.
3522 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3523 const DOM_SID *domain_sid,
3524 const char *domain_name,
3525 struct cli_state *cli,
3526 struct rpc_pipe_client *pipe_hnd,
3527 TALLOC_CTX *mem_ctx,
3528 int argc,
3529 const char **argv)
3531 WERROR result;
3532 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3533 struct srvsvc_NetShareInfoCtr ctr_src;
3534 union srvsvc_NetShareInfo info;
3535 uint32 i;
3536 struct rpc_pipe_client *srvsvc_pipe = NULL;
3537 struct cli_state *cli_dst = NULL;
3538 uint32 level = 502; /* includes secdesc */
3539 uint32_t parm_error = 0;
3541 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3542 &ctr_src);
3544 if (!W_ERROR_IS_OK(result))
3545 goto done;
3547 /* connect destination PI_SRVSVC */
3548 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3549 &ndr_table_srvsvc.syntax_id);
3550 if (!NT_STATUS_IS_OK(nt_status))
3551 return nt_status;
3554 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3556 struct srvsvc_NetShareInfo502 info502 =
3557 ctr_src.ctr.ctr502->array[i];
3559 /* reset error-code */
3560 nt_status = NT_STATUS_UNSUCCESSFUL;
3562 if (!check_share_sanity(c, cli, info502.name, info502.type))
3563 continue;
3565 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3566 info502.name, info502.path, info502.comment);
3568 if (c->opt_verbose)
3569 display_sec_desc(info502.sd_buf.sd);
3571 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3572 info.info502 = &info502;
3574 /* finally modify the share on the dst server */
3575 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3576 srvsvc_pipe->desthost,
3577 info502.name,
3578 level,
3579 &info,
3580 &parm_error,
3581 &result);
3582 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3583 printf("cannot set share-acl: %s\n", win_errstr(result));
3584 goto done;
3589 nt_status = NT_STATUS_OK;
3591 done:
3592 if (cli_dst) {
3593 cli_shutdown(cli_dst);
3596 return nt_status;
3601 * Migrate share-acls from a RPC server to another.
3603 * @param argc Standard main() style argc.
3604 * @param argv Standard main() style argv. Initial components are already
3605 * stripped.
3607 * @return A shell status integer (0 for success).
3609 static int rpc_share_migrate_security(struct net_context *c, int argc,
3610 const char **argv)
3612 if (c->display_usage) {
3613 d_printf("Usage:\n"
3614 "net rpc share migrate security\n"
3615 " Migrate share-acls to local server\n");
3616 return 0;
3619 if (!c->opt_host) {
3620 d_printf("no server to migrate\n");
3621 return -1;
3624 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3625 rpc_share_migrate_security_internals,
3626 argc, argv);
3630 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3631 * from one server to another.
3633 * @param argc Standard main() style argc.
3634 * @param argv Standard main() style argv. Initial components are already
3635 * stripped.
3637 * @return A shell status integer (0 for success).
3640 static int rpc_share_migrate_all(struct net_context *c, int argc,
3641 const char **argv)
3643 int ret;
3645 if (c->display_usage) {
3646 d_printf("Usage:\n"
3647 "net rpc share migrate all\n"
3648 " Migrates shares including all share settings\n");
3649 return 0;
3652 if (!c->opt_host) {
3653 d_printf("no server to migrate\n");
3654 return -1;
3657 /* order is important. we don't want to be locked out by the share-acl
3658 * before copying files - gd */
3660 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3661 rpc_share_migrate_shares_internals, argc, argv);
3662 if (ret)
3663 return ret;
3665 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3666 rpc_share_migrate_files_internals, argc, argv);
3667 if (ret)
3668 return ret;
3670 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3671 rpc_share_migrate_security_internals, argc,
3672 argv);
3677 * 'net rpc share migrate' entrypoint.
3678 * @param argc Standard main() style argc.
3679 * @param argv Standard main() style argv. Initial components are already
3680 * stripped.
3682 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3685 struct functable func[] = {
3687 "all",
3688 rpc_share_migrate_all,
3689 NET_TRANSPORT_RPC,
3690 "Migrate shares from remote to local server",
3691 "net rpc share migrate all\n"
3692 " Migrate shares from remote to local server"
3695 "files",
3696 rpc_share_migrate_files,
3697 NET_TRANSPORT_RPC,
3698 "Migrate files from remote to local server",
3699 "net rpc share migrate files\n"
3700 " Migrate files from remote to local server"
3703 "security",
3704 rpc_share_migrate_security,
3705 NET_TRANSPORT_RPC,
3706 "Migrate share-ACLs from remote to local server",
3707 "net rpc share migrate security\n"
3708 " Migrate share-ACLs from remote to local server"
3711 "shares",
3712 rpc_share_migrate_shares,
3713 NET_TRANSPORT_RPC,
3714 "Migrate shares from remote to local server",
3715 "net rpc share migrate shares\n"
3716 " Migrate shares from remote to local server"
3718 {NULL, NULL, 0, NULL, NULL}
3721 net_mode_share = NET_MODE_SHARE_MIGRATE;
3723 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3726 struct full_alias {
3727 DOM_SID sid;
3728 uint32 num_members;
3729 DOM_SID *members;
3732 static int num_server_aliases;
3733 static struct full_alias *server_aliases;
3736 * Add an alias to the static list.
3738 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3740 if (server_aliases == NULL)
3741 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3743 server_aliases[num_server_aliases] = *alias;
3744 num_server_aliases += 1;
3748 * For a specific domain on the server, fetch all the aliases
3749 * and their members. Add all of them to the server_aliases.
3752 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3753 TALLOC_CTX *mem_ctx,
3754 struct policy_handle *connect_pol,
3755 const DOM_SID *domain_sid)
3757 uint32 start_idx, max_entries, num_entries, i;
3758 struct samr_SamArray *groups = NULL;
3759 NTSTATUS result;
3760 struct policy_handle domain_pol;
3762 /* Get domain policy handle */
3764 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3765 connect_pol,
3766 MAXIMUM_ALLOWED_ACCESS,
3767 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3768 &domain_pol);
3769 if (!NT_STATUS_IS_OK(result))
3770 return result;
3772 start_idx = 0;
3773 max_entries = 250;
3775 do {
3776 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3777 &domain_pol,
3778 &start_idx,
3779 &groups,
3780 max_entries,
3781 &num_entries);
3782 for (i = 0; i < num_entries; i++) {
3784 struct policy_handle alias_pol;
3785 struct full_alias alias;
3786 struct lsa_SidArray sid_array;
3787 int j;
3789 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3790 &domain_pol,
3791 MAXIMUM_ALLOWED_ACCESS,
3792 groups->entries[i].idx,
3793 &alias_pol);
3794 if (!NT_STATUS_IS_OK(result))
3795 goto done;
3797 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3798 &alias_pol,
3799 &sid_array);
3800 if (!NT_STATUS_IS_OK(result))
3801 goto done;
3803 alias.num_members = sid_array.num_sids;
3805 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3806 if (!NT_STATUS_IS_OK(result))
3807 goto done;
3809 alias.members = NULL;
3811 if (alias.num_members > 0) {
3812 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3814 for (j = 0; j < alias.num_members; j++)
3815 sid_copy(&alias.members[j],
3816 sid_array.sids[j].sid);
3819 sid_copy(&alias.sid, domain_sid);
3820 sid_append_rid(&alias.sid, groups->entries[i].idx);
3822 push_alias(mem_ctx, &alias);
3824 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3826 result = NT_STATUS_OK;
3828 done:
3829 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3831 return result;
3835 * Dump server_aliases as names for debugging purposes.
3838 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
3839 const DOM_SID *domain_sid,
3840 const char *domain_name,
3841 struct cli_state *cli,
3842 struct rpc_pipe_client *pipe_hnd,
3843 TALLOC_CTX *mem_ctx,
3844 int argc,
3845 const char **argv)
3847 int i;
3848 NTSTATUS result;
3849 struct policy_handle lsa_pol;
3851 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
3852 SEC_FLAG_MAXIMUM_ALLOWED,
3853 &lsa_pol);
3854 if (!NT_STATUS_IS_OK(result))
3855 return result;
3857 for (i=0; i<num_server_aliases; i++) {
3858 char **names;
3859 char **domains;
3860 enum lsa_SidType *types;
3861 int j;
3863 struct full_alias *alias = &server_aliases[i];
3865 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3866 &alias->sid,
3867 &domains, &names, &types);
3868 if (!NT_STATUS_IS_OK(result))
3869 continue;
3871 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3873 if (alias->num_members == 0) {
3874 DEBUG(1, ("\n"));
3875 continue;
3878 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
3879 alias->num_members,
3880 alias->members,
3881 &domains, &names, &types);
3883 if (!NT_STATUS_IS_OK(result) &&
3884 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3885 continue;
3887 for (j=0; j<alias->num_members; j++)
3888 DEBUG(1, ("%s\\%s (%d); ",
3889 domains[j] ? domains[j] : "*unknown*",
3890 names[j] ? names[j] : "*unknown*",types[j]));
3891 DEBUG(1, ("\n"));
3894 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
3896 return NT_STATUS_OK;
3900 * Fetch a list of all server aliases and their members into
3901 * server_aliases.
3904 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
3905 const DOM_SID *domain_sid,
3906 const char *domain_name,
3907 struct cli_state *cli,
3908 struct rpc_pipe_client *pipe_hnd,
3909 TALLOC_CTX *mem_ctx,
3910 int argc,
3911 const char **argv)
3913 NTSTATUS result;
3914 struct policy_handle connect_pol;
3916 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3917 pipe_hnd->desthost,
3918 MAXIMUM_ALLOWED_ACCESS,
3919 &connect_pol);
3921 if (!NT_STATUS_IS_OK(result))
3922 goto done;
3924 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3925 &global_sid_Builtin);
3927 if (!NT_STATUS_IS_OK(result))
3928 goto done;
3930 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3931 domain_sid);
3933 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
3934 done:
3935 return result;
3938 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
3940 token->num_sids = 4;
3942 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
3943 d_fprintf(stderr, "malloc failed\n");
3944 token->num_sids = 0;
3945 return;
3948 token->user_sids[0] = *user_sid;
3949 sid_copy(&token->user_sids[1], &global_sid_World);
3950 sid_copy(&token->user_sids[2], &global_sid_Network);
3951 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
3954 static void free_user_token(NT_USER_TOKEN *token)
3956 SAFE_FREE(token->user_sids);
3959 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
3961 if (is_sid_in_token(token, sid))
3962 return;
3964 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
3965 if (!token->user_sids) {
3966 return;
3969 sid_copy(&token->user_sids[token->num_sids], sid);
3971 token->num_sids += 1;
3974 struct user_token {
3975 fstring name;
3976 NT_USER_TOKEN token;
3979 static void dump_user_token(struct user_token *token)
3981 int i;
3983 d_printf("%s\n", token->name);
3985 for (i=0; i<token->token.num_sids; i++) {
3986 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
3990 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
3992 int i;
3994 for (i=0; i<alias->num_members; i++) {
3995 if (sid_compare(sid, &alias->members[i]) == 0)
3996 return true;
3999 return false;
4002 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4004 int i;
4006 for (i=0; i<num_server_aliases; i++) {
4007 if (is_alias_member(&sid, &server_aliases[i]))
4008 add_sid_to_token(token, &server_aliases[i].sid);
4013 * We got a user token with all the SIDs we can know about without asking the
4014 * server directly. These are the user and domain group sids. All of these can
4015 * be members of aliases. So scan the list of aliases for each of the SIDs and
4016 * add them to the token.
4019 static void collect_alias_memberships(NT_USER_TOKEN *token)
4021 int num_global_sids = token->num_sids;
4022 int i;
4024 for (i=0; i<num_global_sids; i++) {
4025 collect_sid_memberships(token, token->user_sids[i]);
4029 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4031 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4032 enum wbcSidType type;
4033 fstring full_name;
4034 struct wbcDomainSid wsid;
4035 char *sid_str = NULL;
4036 DOM_SID user_sid;
4037 uint32_t num_groups;
4038 gid_t *groups = NULL;
4039 uint32_t i;
4041 fstr_sprintf(full_name, "%s%c%s",
4042 domain, *lp_winbind_separator(), user);
4044 /* First let's find out the user sid */
4046 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4048 if (!WBC_ERROR_IS_OK(wbc_status)) {
4049 DEBUG(1, ("winbind could not find %s: %s\n",
4050 full_name, wbcErrorString(wbc_status)));
4051 return false;
4054 wbc_status = wbcSidToString(&wsid, &sid_str);
4055 if (!WBC_ERROR_IS_OK(wbc_status)) {
4056 return false;
4059 if (type != SID_NAME_USER) {
4060 wbcFreeMemory(sid_str);
4061 DEBUG(1, ("%s is not a user\n", full_name));
4062 return false;
4065 if (!string_to_sid(&user_sid, sid_str)) {
4066 DEBUG(1,("Could not convert sid %s from string\n", sid_str));
4067 return false;
4070 wbcFreeMemory(sid_str);
4071 sid_str = NULL;
4073 init_user_token(token, &user_sid);
4075 /* And now the groups winbind knows about */
4077 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4078 if (!WBC_ERROR_IS_OK(wbc_status)) {
4079 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4080 full_name, wbcErrorString(wbc_status)));
4081 return false;
4084 for (i = 0; i < num_groups; i++) {
4085 gid_t gid = groups[i];
4086 DOM_SID sid;
4088 wbc_status = wbcGidToSid(gid, &wsid);
4089 if (!WBC_ERROR_IS_OK(wbc_status)) {
4090 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4091 gid, wbcErrorString(wbc_status)));
4092 wbcFreeMemory(groups);
4093 return false;
4096 wbc_status = wbcSidToString(&wsid, &sid_str);
4097 if (!WBC_ERROR_IS_OK(wbc_status)) {
4098 wbcFreeMemory(groups);
4099 return false;
4102 DEBUG(3, (" %s\n", sid_str));
4104 string_to_sid(&sid, sid_str);
4105 wbcFreeMemory(sid_str);
4106 sid_str = NULL;
4108 add_sid_to_token(token, &sid);
4110 wbcFreeMemory(groups);
4112 return true;
4116 * Get a list of all user tokens we want to look at
4119 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4120 struct user_token **user_tokens)
4122 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4123 uint32_t i, num_users;
4124 const char **users;
4125 struct user_token *result;
4126 TALLOC_CTX *frame = NULL;
4128 if (lp_winbind_use_default_domain() &&
4129 (c->opt_target_workgroup == NULL)) {
4130 d_fprintf(stderr, "winbind use default domain = yes set, "
4131 "please specify a workgroup\n");
4132 return false;
4135 /* Send request to winbind daemon */
4137 wbc_status = wbcListUsers(NULL, &num_users, &users);
4138 if (!WBC_ERROR_IS_OK(wbc_status)) {
4139 DEBUG(1, ("winbind could not list users: %s\n",
4140 wbcErrorString(wbc_status)));
4141 return false;
4144 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4146 if (result == NULL) {
4147 DEBUG(1, ("Could not malloc sid array\n"));
4148 wbcFreeMemory(users);
4149 return false;
4152 frame = talloc_stackframe();
4153 for (i=0; i < num_users; i++) {
4154 fstring domain, user;
4155 char *p;
4157 fstrcpy(result[i].name, users[i]);
4159 p = strchr(users[i], *lp_winbind_separator());
4161 DEBUG(3, ("%s\n", users[i]));
4163 if (p == NULL) {
4164 fstrcpy(domain, c->opt_target_workgroup);
4165 fstrcpy(user, users[i]);
4166 } else {
4167 *p++ = '\0';
4168 fstrcpy(domain, users[i]);
4169 strupper_m(domain);
4170 fstrcpy(user, p);
4173 get_user_sids(domain, user, &(result[i].token));
4174 i+=1;
4176 TALLOC_FREE(frame);
4177 wbcFreeMemory(users);
4179 *num_tokens = num_users;
4180 *user_tokens = result;
4182 return true;
4185 static bool get_user_tokens_from_file(FILE *f,
4186 int *num_tokens,
4187 struct user_token **tokens)
4189 struct user_token *token = NULL;
4191 while (!feof(f)) {
4192 fstring line;
4194 if (fgets(line, sizeof(line)-1, f) == NULL) {
4195 return true;
4198 if (line[strlen(line)-1] == '\n')
4199 line[strlen(line)-1] = '\0';
4201 if (line[0] == ' ') {
4202 /* We have a SID */
4204 DOM_SID sid;
4205 if(!string_to_sid(&sid, &line[1])) {
4206 DEBUG(1,("get_user_tokens_from_file: Could "
4207 "not convert sid %s \n",&line[1]));
4208 return false;
4211 if (token == NULL) {
4212 DEBUG(0, ("File does not begin with username"));
4213 return false;
4216 add_sid_to_token(&token->token, &sid);
4217 continue;
4220 /* And a new user... */
4222 *num_tokens += 1;
4223 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4224 if (*tokens == NULL) {
4225 DEBUG(0, ("Could not realloc tokens\n"));
4226 return false;
4229 token = &((*tokens)[*num_tokens-1]);
4231 fstrcpy(token->name, line);
4232 token->token.num_sids = 0;
4233 token->token.user_sids = NULL;
4234 continue;
4237 return false;
4242 * Show the list of all users that have access to a share
4245 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4246 TALLOC_CTX *mem_ctx,
4247 const char *netname,
4248 int num_tokens,
4249 struct user_token *tokens)
4251 int fnum;
4252 SEC_DESC *share_sd = NULL;
4253 SEC_DESC *root_sd = NULL;
4254 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4255 int i;
4256 union srvsvc_NetShareInfo info;
4257 WERROR result;
4258 NTSTATUS status;
4259 uint16 cnum;
4261 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4262 pipe_hnd->desthost,
4263 netname,
4264 502,
4265 &info,
4266 &result);
4268 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4269 DEBUG(1, ("Coult not query secdesc for share %s\n",
4270 netname));
4271 return;
4274 share_sd = info.info502->sd_buf.sd;
4275 if (share_sd == NULL) {
4276 DEBUG(1, ("Got no secdesc for share %s\n",
4277 netname));
4280 cnum = cli->cnum;
4282 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli, netname, "A:", "", 0))) {
4283 return;
4286 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4288 if (fnum != -1) {
4289 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4292 for (i=0; i<num_tokens; i++) {
4293 uint32 acc_granted;
4295 if (share_sd != NULL) {
4296 status = se_access_check(share_sd, &tokens[i].token,
4297 1, &acc_granted);
4299 if (!NT_STATUS_IS_OK(status)) {
4300 DEBUG(1, ("Could not check share_sd for "
4301 "user %s\n",
4302 tokens[i].name));
4303 continue;
4307 if (root_sd == NULL) {
4308 d_printf(" %s\n", tokens[i].name);
4309 continue;
4312 status = se_access_check(root_sd, &tokens[i].token,
4313 1, &acc_granted);
4314 if (!NT_STATUS_IS_OK(status)) {
4315 DEBUG(1, ("Could not check root_sd for user %s\n",
4316 tokens[i].name));
4317 continue;
4319 d_printf(" %s\n", tokens[i].name);
4322 if (fnum != -1)
4323 cli_close(cli, fnum);
4324 cli_tdis(cli);
4325 cli->cnum = cnum;
4327 return;
4330 struct share_list {
4331 int num_shares;
4332 char **shares;
4335 static void collect_share(const char *name, uint32 m,
4336 const char *comment, void *state)
4338 struct share_list *share_list = (struct share_list *)state;
4340 if (m != STYPE_DISKTREE)
4341 return;
4343 share_list->num_shares += 1;
4344 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4345 if (!share_list->shares) {
4346 share_list->num_shares = 0;
4347 return;
4349 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4353 * List shares on a remote RPC server, including the security descriptors.
4355 * All parameters are provided by the run_rpc_command function, except for
4356 * argc, argv which are passed through.
4358 * @param domain_sid The domain sid acquired from the remote server.
4359 * @param cli A cli_state connected to the server.
4360 * @param mem_ctx Talloc context, destroyed on completion of the function.
4361 * @param argc Standard main() style argc.
4362 * @param argv Standard main() style argv. Initial components are already
4363 * stripped.
4365 * @return Normal NTSTATUS return.
4368 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4369 const DOM_SID *domain_sid,
4370 const char *domain_name,
4371 struct cli_state *cli,
4372 struct rpc_pipe_client *pipe_hnd,
4373 TALLOC_CTX *mem_ctx,
4374 int argc,
4375 const char **argv)
4377 int ret;
4378 bool r;
4379 uint32 i;
4380 FILE *f;
4382 struct user_token *tokens = NULL;
4383 int num_tokens = 0;
4385 struct share_list share_list;
4387 if (argc == 0) {
4388 f = stdin;
4389 } else {
4390 f = fopen(argv[0], "r");
4393 if (f == NULL) {
4394 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4395 return NT_STATUS_UNSUCCESSFUL;
4398 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4400 if (f != stdin)
4401 fclose(f);
4403 if (!r) {
4404 DEBUG(0, ("Could not read users from file\n"));
4405 return NT_STATUS_UNSUCCESSFUL;
4408 for (i=0; i<num_tokens; i++)
4409 collect_alias_memberships(&tokens[i].token);
4411 share_list.num_shares = 0;
4412 share_list.shares = NULL;
4414 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4416 if (ret == -1) {
4417 DEBUG(0, ("Error returning browse list: %s\n",
4418 cli_errstr(cli)));
4419 goto done;
4422 for (i = 0; i < share_list.num_shares; i++) {
4423 char *netname = share_list.shares[i];
4425 if (netname[strlen(netname)-1] == '$')
4426 continue;
4428 d_printf("%s\n", netname);
4430 show_userlist(pipe_hnd, mem_ctx, netname,
4431 num_tokens, tokens);
4433 done:
4434 for (i=0; i<num_tokens; i++) {
4435 free_user_token(&tokens[i].token);
4437 SAFE_FREE(tokens);
4438 SAFE_FREE(share_list.shares);
4440 return NT_STATUS_OK;
4443 static int rpc_share_allowedusers(struct net_context *c, int argc,
4444 const char **argv)
4446 int result;
4448 if (c->display_usage) {
4449 d_printf("Usage:\n"
4450 "net rpc share allowedusers\n"
4451 " List allowed users\n");
4452 return 0;
4455 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4456 rpc_aliaslist_internals,
4457 argc, argv);
4458 if (result != 0)
4459 return result;
4461 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4462 rpc_aliaslist_dump,
4463 argc, argv);
4464 if (result != 0)
4465 return result;
4467 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4468 rpc_share_allowedusers_internals,
4469 argc, argv);
4472 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4474 int num_tokens = 0;
4475 struct user_token *tokens = NULL;
4476 int i;
4478 if (argc != 0) {
4479 net_usersidlist_usage(c, argc, argv);
4480 return 0;
4483 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4484 DEBUG(0, ("Could not get the user/sid list\n"));
4485 return 0;
4488 for (i=0; i<num_tokens; i++) {
4489 dump_user_token(&tokens[i]);
4490 free_user_token(&tokens[i].token);
4493 SAFE_FREE(tokens);
4494 return 1;
4497 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4499 d_printf("net usersidlist\n"
4500 "\tprints out a list of all users the running winbind knows\n"
4501 "\tabout, together with all their SIDs. This is used as\n"
4502 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4504 net_common_flags_usage(c, argc, argv);
4505 return -1;
4509 * 'net rpc share' entrypoint.
4510 * @param argc Standard main() style argc.
4511 * @param argv Standard main() style argv. Initial components are already
4512 * stripped.
4515 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4517 NET_API_STATUS status;
4519 struct functable func[] = {
4521 "add",
4522 rpc_share_add,
4523 NET_TRANSPORT_RPC,
4524 "Add share",
4525 "net rpc share add\n"
4526 " Add share"
4529 "delete",
4530 rpc_share_delete,
4531 NET_TRANSPORT_RPC,
4532 "Remove share",
4533 "net rpc share delete\n"
4534 " Remove share"
4537 "allowedusers",
4538 rpc_share_allowedusers,
4539 NET_TRANSPORT_RPC,
4540 "Modify allowed users",
4541 "net rpc share allowedusers\n"
4542 " Modify allowed users"
4545 "migrate",
4546 rpc_share_migrate,
4547 NET_TRANSPORT_RPC,
4548 "Migrate share to local server",
4549 "net rpc share migrate\n"
4550 " Migrate share to local server"
4553 "list",
4554 rpc_share_list,
4555 NET_TRANSPORT_RPC,
4556 "List shares",
4557 "net rpc share list\n"
4558 " List shares"
4560 {NULL, NULL, 0, NULL, NULL}
4563 status = libnetapi_init(&c->netapi_ctx);
4564 if (status != 0) {
4565 return -1;
4567 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4568 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4569 if (c->opt_kerberos) {
4570 libnetapi_set_use_kerberos(c->netapi_ctx);
4573 if (argc == 0) {
4574 if (c->display_usage) {
4575 d_printf("Usage:\n"
4576 "net rpc share\n"
4577 " List shares\n"
4578 " Alias for net rpc share list\n");
4579 net_display_usage_from_functable(func);
4580 return 0;
4583 return rpc_share_list(c, argc, argv);
4586 return net_run_function(c, argc, argv, "net rpc share", func);
4589 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4590 TALLOC_CTX *mem_ctx,
4591 struct rpc_sh_ctx *ctx,
4592 struct rpc_pipe_client *pipe_hnd,
4593 int argc, const char **argv)
4596 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4599 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4600 TALLOC_CTX *mem_ctx,
4601 struct rpc_sh_ctx *ctx,
4602 struct rpc_pipe_client *pipe_hnd,
4603 int argc, const char **argv)
4605 NET_API_STATUS status;
4606 uint32_t parm_err = 0;
4607 struct SHARE_INFO_2 i2;
4609 if ((argc < 2) || (argc > 3)) {
4610 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4611 ctx->whoami);
4612 return NT_STATUS_INVALID_PARAMETER;
4615 i2.shi2_netname = argv[0];
4616 i2.shi2_type = STYPE_DISKTREE;
4617 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4618 i2.shi2_permissions = 0;
4619 i2.shi2_max_uses = 0;
4620 i2.shi2_current_uses = 0;
4621 i2.shi2_path = argv[1];
4622 i2.shi2_passwd = NULL;
4624 status = NetShareAdd(pipe_hnd->desthost,
4626 (uint8_t *)&i2,
4627 &parm_err);
4629 return werror_to_ntstatus(W_ERROR(status));
4632 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4633 TALLOC_CTX *mem_ctx,
4634 struct rpc_sh_ctx *ctx,
4635 struct rpc_pipe_client *pipe_hnd,
4636 int argc, const char **argv)
4638 if (argc != 1) {
4639 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4640 return NT_STATUS_INVALID_PARAMETER;
4643 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4646 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4647 TALLOC_CTX *mem_ctx,
4648 struct rpc_sh_ctx *ctx,
4649 struct rpc_pipe_client *pipe_hnd,
4650 int argc, const char **argv)
4652 union srvsvc_NetShareInfo info;
4653 WERROR result;
4654 NTSTATUS status;
4656 if (argc != 1) {
4657 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4658 return NT_STATUS_INVALID_PARAMETER;
4661 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4662 pipe_hnd->desthost,
4663 argv[0],
4665 &info,
4666 &result);
4667 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4668 goto done;
4671 d_printf("Name: %s\n", info.info2->name);
4672 d_printf("Comment: %s\n", info.info2->comment);
4673 d_printf("Path: %s\n", info.info2->path);
4674 d_printf("Password: %s\n", info.info2->password);
4676 done:
4677 return werror_to_ntstatus(result);
4680 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4681 struct rpc_sh_ctx *ctx)
4683 static struct rpc_sh_cmd cmds[] = {
4685 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4686 "List available shares" },
4688 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4689 "Add a share" },
4691 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4692 "Delete a share" },
4694 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4695 "Get information about a share" },
4697 { NULL, NULL, 0, NULL, NULL }
4700 return cmds;
4703 /****************************************************************************/
4705 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4707 return net_file_usage(c, argc, argv);
4711 * Close a file on a remote RPC server.
4713 * @param argc Standard main() style argc.
4714 * @param argv Standard main() style argv. Initial components are already
4715 * stripped.
4717 * @return A shell status integer (0 for success).
4719 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4721 if (argc < 1 || c->display_usage) {
4722 return rpc_file_usage(c, argc, argv);
4725 return NetFileClose(c->opt_host, atoi(argv[0]));
4729 * Formatted print of open file info
4731 * @param r struct FILE_INFO_3 contents
4734 static void display_file_info_3(struct FILE_INFO_3 *r)
4736 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4737 r->fi3_id, r->fi3_username, r->fi3_permissions,
4738 r->fi3_num_locks, r->fi3_pathname);
4742 * List files for a user on a remote RPC server.
4744 * @param argc Standard main() style argc.
4745 * @param argv Standard main() style argv. Initial components are already
4746 * stripped.
4748 * @return A shell status integer (0 for success)..
4751 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4753 NET_API_STATUS status;
4754 uint32 preferred_len = 0xffffffff, i;
4755 const char *username=NULL;
4756 uint32_t total_entries = 0;
4757 uint32_t entries_read = 0;
4758 uint32_t resume_handle = 0;
4759 struct FILE_INFO_3 *i3 = NULL;
4761 if (c->display_usage) {
4762 return rpc_file_usage(c, argc, argv);
4765 /* if argc > 0, must be user command */
4766 if (argc > 0) {
4767 username = smb_xstrdup(argv[0]);
4770 status = NetFileEnum(c->opt_host,
4771 NULL,
4772 username,
4774 (uint8_t **)(void *)&i3,
4775 preferred_len,
4776 &entries_read,
4777 &total_entries,
4778 &resume_handle);
4780 if (status != 0) {
4781 goto done;
4784 /* Display results */
4786 d_printf(
4787 "\nEnumerating open files on remote server:\n\n"
4788 "\nFileId Opened by Perms Locks Path"
4789 "\n------ --------- ----- ----- ---- \n");
4790 for (i = 0; i < entries_read; i++) {
4791 display_file_info_3(&i3[i]);
4793 done:
4794 return status;
4798 * 'net rpc file' entrypoint.
4799 * @param argc Standard main() style argc.
4800 * @param argv Standard main() style argv. Initial components are already
4801 * stripped.
4804 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4806 NET_API_STATUS status;
4808 struct functable func[] = {
4810 "close",
4811 rpc_file_close,
4812 NET_TRANSPORT_RPC,
4813 "Close opened file",
4814 "net rpc file close\n"
4815 " Close opened file"
4818 "user",
4819 rpc_file_user,
4820 NET_TRANSPORT_RPC,
4821 "List files opened by user",
4822 "net rpc file user\n"
4823 " List files opened by user"
4825 #if 0
4827 "info",
4828 rpc_file_info,
4829 NET_TRANSPORT_RPC,
4830 "Display information about opened file",
4831 "net rpc file info\n"
4832 " Display information about opened file"
4834 #endif
4835 {NULL, NULL, 0, NULL, NULL}
4838 status = libnetapi_init(&c->netapi_ctx);
4839 if (status != 0) {
4840 return -1;
4842 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4843 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4844 if (c->opt_kerberos) {
4845 libnetapi_set_use_kerberos(c->netapi_ctx);
4848 if (argc == 0) {
4849 if (c->display_usage) {
4850 d_printf("Usage:\n");
4851 d_printf("net rpc file\n"
4852 " List opened files\n");
4853 net_display_usage_from_functable(func);
4854 return 0;
4857 return rpc_file_user(c, argc, argv);
4860 return net_run_function(c, argc, argv, "net rpc file", func);
4864 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
4866 * All parameters are provided by the run_rpc_command function, except for
4867 * argc, argv which are passed through.
4869 * @param c A net_context structure.
4870 * @param domain_sid The domain sid acquired from the remote server.
4871 * @param cli A cli_state connected to the server.
4872 * @param mem_ctx Talloc context, destroyed on completion of the function.
4873 * @param argc Standard main() style argc.
4874 * @param argv Standard main() style argv. Initial components are already
4875 * stripped.
4877 * @return Normal NTSTATUS return.
4880 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
4881 const DOM_SID *domain_sid,
4882 const char *domain_name,
4883 struct cli_state *cli,
4884 struct rpc_pipe_client *pipe_hnd,
4885 TALLOC_CTX *mem_ctx,
4886 int argc,
4887 const char **argv)
4889 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4891 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
4893 if (NT_STATUS_IS_OK(result)) {
4894 d_printf("\nShutdown successfully aborted\n");
4895 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4896 } else
4897 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4899 return result;
4903 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
4905 * All parameters are provided by the run_rpc_command function, except for
4906 * argc, argv which are passed through.
4908 * @param c A net_context structure.
4909 * @param domain_sid The domain sid acquired from the remote server.
4910 * @param cli A cli_state connected to the server.
4911 * @param mem_ctx Talloc context, destroyed on completion of the function.
4912 * @param argc Standard main() style argc.
4913 * @param argv Standard main() style argv. Initial components are already
4914 * stripped.
4916 * @return Normal NTSTATUS return.
4919 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
4920 const DOM_SID *domain_sid,
4921 const char *domain_name,
4922 struct cli_state *cli,
4923 struct rpc_pipe_client *pipe_hnd,
4924 TALLOC_CTX *mem_ctx,
4925 int argc,
4926 const char **argv)
4928 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4930 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
4932 if (NT_STATUS_IS_OK(result)) {
4933 d_printf("\nShutdown successfully aborted\n");
4934 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
4935 } else
4936 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
4938 return result;
4942 * ABORT the shutdown of a remote RPC server.
4944 * @param argc Standard main() style argc.
4945 * @param argv Standard main() style argv. Initial components are already
4946 * stripped.
4948 * @return A shell status integer (0 for success).
4951 static int rpc_shutdown_abort(struct net_context *c, int argc,
4952 const char **argv)
4954 int rc = -1;
4956 if (c->display_usage) {
4957 d_printf("Usage:\n"
4958 "net rpc abortshutdown\n"
4959 " Abort a scheduled shutdown\n");
4960 return 0;
4963 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
4964 rpc_shutdown_abort_internals, argc, argv);
4966 if (rc == 0)
4967 return rc;
4969 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
4971 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
4972 rpc_reg_shutdown_abort_internals,
4973 argc, argv);
4977 * Shut down a remote RPC Server via initshutdown pipe.
4979 * All parameters are provided by the run_rpc_command function, except for
4980 * argc, argv which are passed through.
4982 * @param c A net_context structure.
4983 * @param domain_sid The domain sid acquired from the remote server.
4984 * @param cli A cli_state connected to the server.
4985 * @param mem_ctx Talloc context, destroyed on completion of the function.
4986 * @param argc Standard main() style argc.
4987 * @param argv Standard main() style argv. Initial components are already
4988 * stripped.
4990 * @return Normal NTSTATUS return.
4993 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
4994 const DOM_SID *domain_sid,
4995 const char *domain_name,
4996 struct cli_state *cli,
4997 struct rpc_pipe_client *pipe_hnd,
4998 TALLOC_CTX *mem_ctx,
4999 int argc,
5000 const char **argv)
5002 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5003 const char *msg = "This machine will be shutdown shortly";
5004 uint32 timeout = 20;
5005 struct lsa_StringLarge msg_string;
5007 if (c->opt_comment) {
5008 msg = c->opt_comment;
5010 if (c->opt_timeout) {
5011 timeout = c->opt_timeout;
5014 msg_string.string = msg;
5016 /* create an entry */
5017 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5018 &msg_string, timeout, c->opt_force, c->opt_reboot,
5019 NULL);
5021 if (NT_STATUS_IS_OK(result)) {
5022 d_printf("\nShutdown of remote machine succeeded\n");
5023 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5024 } else {
5025 DEBUG(1,("Shutdown of remote machine failed!\n"));
5027 return result;
5031 * Shut down a remote RPC Server via winreg pipe.
5033 * All parameters are provided by the run_rpc_command function, except for
5034 * argc, argv which are passed through.
5036 * @param c A net_context structure.
5037 * @param domain_sid The domain sid acquired from the remote server.
5038 * @param cli A cli_state connected to the server.
5039 * @param mem_ctx Talloc context, destroyed on completion of the function.
5040 * @param argc Standard main() style argc.
5041 * @param argv Standard main() style argv. Initial components are already
5042 * stripped.
5044 * @return Normal NTSTATUS return.
5047 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5048 const DOM_SID *domain_sid,
5049 const char *domain_name,
5050 struct cli_state *cli,
5051 struct rpc_pipe_client *pipe_hnd,
5052 TALLOC_CTX *mem_ctx,
5053 int argc,
5054 const char **argv)
5056 const char *msg = "This machine will be shutdown shortly";
5057 uint32 timeout = 20;
5058 struct lsa_StringLarge msg_string;
5059 NTSTATUS result;
5060 WERROR werr;
5062 if (c->opt_comment) {
5063 msg = c->opt_comment;
5065 msg_string.string = msg;
5067 if (c->opt_timeout) {
5068 timeout = c->opt_timeout;
5071 /* create an entry */
5072 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5073 &msg_string, timeout, c->opt_force, c->opt_reboot,
5074 &werr);
5076 if (NT_STATUS_IS_OK(result)) {
5077 d_printf("\nShutdown of remote machine succeeded\n");
5078 } else {
5079 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5080 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5081 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5082 else
5083 d_fprintf(stderr, "\nresult was: %s\n", win_errstr(werr));
5086 return result;
5090 * Shut down a remote RPC server.
5092 * @param argc Standard main() style argc.
5093 * @param argv Standard main() style argv. Initial components are already
5094 * stripped.
5096 * @return A shell status integer (0 for success).
5099 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5101 int rc = -1;
5103 if (c->display_usage) {
5104 d_printf("Usage:\n"
5105 "net rpc shutdown\n"
5106 " Shut down a remote RPC server\n");
5107 return 0;
5110 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5111 rpc_init_shutdown_internals, argc, argv);
5113 if (rc) {
5114 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5115 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5116 rpc_reg_shutdown_internals, argc, argv);
5119 return rc;
5122 /***************************************************************************
5123 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5124 ***************************************************************************/
5127 * Add interdomain trust account to the RPC server.
5128 * All parameters (except for argc and argv) are passed by run_rpc_command
5129 * function.
5131 * @param c A net_context structure.
5132 * @param domain_sid The domain sid acquired from the server.
5133 * @param cli A cli_state connected to the server.
5134 * @param mem_ctx Talloc context, destroyed on completion of the function.
5135 * @param argc Standard main() style argc.
5136 * @param argv Standard main() style argv. Initial components are already
5137 * stripped.
5139 * @return normal NTSTATUS return code.
5142 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5143 const DOM_SID *domain_sid,
5144 const char *domain_name,
5145 struct cli_state *cli,
5146 struct rpc_pipe_client *pipe_hnd,
5147 TALLOC_CTX *mem_ctx,
5148 int argc,
5149 const char **argv)
5151 struct policy_handle connect_pol, domain_pol, user_pol;
5152 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5153 char *acct_name;
5154 struct lsa_String lsa_acct_name;
5155 uint32 acb_info;
5156 uint32 acct_flags=0;
5157 uint32 user_rid;
5158 uint32_t access_granted = 0;
5159 union samr_UserInfo info;
5160 unsigned int orig_timeout;
5162 if (argc != 2) {
5163 d_printf("Usage: net rpc trustdom add <domain_name> "
5164 "<trust password>\n");
5165 return NT_STATUS_INVALID_PARAMETER;
5169 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5172 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5173 return NT_STATUS_NO_MEMORY;
5176 strupper_m(acct_name);
5178 init_lsa_String(&lsa_acct_name, acct_name);
5180 /* Get samr policy handle */
5181 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5182 pipe_hnd->desthost,
5183 MAXIMUM_ALLOWED_ACCESS,
5184 &connect_pol);
5185 if (!NT_STATUS_IS_OK(result)) {
5186 goto done;
5189 /* Get domain policy handle */
5190 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5191 &connect_pol,
5192 MAXIMUM_ALLOWED_ACCESS,
5193 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5194 &domain_pol);
5195 if (!NT_STATUS_IS_OK(result)) {
5196 goto done;
5199 /* This call can take a long time - allow the server to time out.
5200 * 35 seconds should do it. */
5202 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5204 /* Create trusting domain's account */
5205 acb_info = ACB_NORMAL;
5206 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5207 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5208 SAMR_USER_ACCESS_SET_PASSWORD |
5209 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5210 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5212 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5213 &domain_pol,
5214 &lsa_acct_name,
5215 acb_info,
5216 acct_flags,
5217 &user_pol,
5218 &access_granted,
5219 &user_rid);
5221 /* And restore our original timeout. */
5222 rpccli_set_timeout(pipe_hnd, orig_timeout);
5224 if (!NT_STATUS_IS_OK(result)) {
5225 d_printf("net rpc trustdom add: create user %s failed %s\n",
5226 acct_name, nt_errstr(result));
5227 goto done;
5231 struct samr_CryptPassword crypt_pwd;
5233 ZERO_STRUCT(info.info23);
5235 init_samr_CryptPassword(argv[1],
5236 &cli->user_session_key,
5237 &crypt_pwd);
5239 info.info23.info.fields_present = SAMR_FIELD_ACCT_FLAGS |
5240 SAMR_FIELD_NT_PASSWORD_PRESENT;
5241 info.info23.info.acct_flags = ACB_DOMTRUST;
5242 info.info23.password = crypt_pwd;
5244 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5245 &user_pol,
5247 &info);
5249 if (!NT_STATUS_IS_OK(result)) {
5250 DEBUG(0,("Could not set trust account password: %s\n",
5251 nt_errstr(result)));
5252 goto done;
5256 done:
5257 SAFE_FREE(acct_name);
5258 return result;
5262 * Create interdomain trust account for a remote domain.
5264 * @param argc Standard argc.
5265 * @param argv Standard argv without initial components.
5267 * @return Integer status (0 means success).
5270 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5272 if (argc > 0 && !c->display_usage) {
5273 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5274 rpc_trustdom_add_internals, argc, argv);
5275 } else {
5276 d_printf("Usage:\n"
5277 "net rpc trustdom add <domain_name> <trust password>\n");
5278 return -1;
5284 * Remove interdomain trust account from the RPC server.
5285 * All parameters (except for argc and argv) are passed by run_rpc_command
5286 * function.
5288 * @param c A net_context structure.
5289 * @param domain_sid The domain sid acquired from the server.
5290 * @param cli A cli_state connected to the server.
5291 * @param mem_ctx Talloc context, destroyed on completion of the function.
5292 * @param argc Standard main() style argc.
5293 * @param argv Standard main() style argv. Initial components are already
5294 * stripped.
5296 * @return normal NTSTATUS return code.
5299 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5300 const DOM_SID *domain_sid,
5301 const char *domain_name,
5302 struct cli_state *cli,
5303 struct rpc_pipe_client *pipe_hnd,
5304 TALLOC_CTX *mem_ctx,
5305 int argc,
5306 const char **argv)
5308 struct policy_handle connect_pol, domain_pol, user_pol;
5309 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5310 char *acct_name;
5311 DOM_SID trust_acct_sid;
5312 struct samr_Ids user_rids, name_types;
5313 struct lsa_String lsa_acct_name;
5315 if (argc != 1) {
5316 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5317 return NT_STATUS_INVALID_PARAMETER;
5321 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5323 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5325 if (acct_name == NULL)
5326 return NT_STATUS_NO_MEMORY;
5328 strupper_m(acct_name);
5330 /* Get samr policy handle */
5331 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5332 pipe_hnd->desthost,
5333 MAXIMUM_ALLOWED_ACCESS,
5334 &connect_pol);
5335 if (!NT_STATUS_IS_OK(result)) {
5336 goto done;
5339 /* Get domain policy handle */
5340 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5341 &connect_pol,
5342 MAXIMUM_ALLOWED_ACCESS,
5343 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5344 &domain_pol);
5345 if (!NT_STATUS_IS_OK(result)) {
5346 goto done;
5349 init_lsa_String(&lsa_acct_name, acct_name);
5351 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5352 &domain_pol,
5354 &lsa_acct_name,
5355 &user_rids,
5356 &name_types);
5358 if (!NT_STATUS_IS_OK(result)) {
5359 d_printf("net rpc trustdom del: LookupNames on user %s failed %s\n",
5360 acct_name, nt_errstr(result) );
5361 goto done;
5364 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5365 &domain_pol,
5366 MAXIMUM_ALLOWED_ACCESS,
5367 user_rids.ids[0],
5368 &user_pol);
5370 if (!NT_STATUS_IS_OK(result)) {
5371 d_printf("net rpc trustdom del: OpenUser on user %s failed %s\n",
5372 acct_name, nt_errstr(result) );
5373 goto done;
5376 /* append the rid to the domain sid */
5377 sid_copy(&trust_acct_sid, domain_sid);
5378 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5379 goto done;
5382 /* remove the sid */
5384 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5385 &user_pol,
5386 &trust_acct_sid);
5387 if (!NT_STATUS_IS_OK(result)) {
5388 d_printf("net rpc trustdom del: RemoveMemberFromForeignDomain on user %s failed %s\n",
5389 acct_name, nt_errstr(result) );
5390 goto done;
5393 /* Delete user */
5395 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5396 &user_pol);
5398 if (!NT_STATUS_IS_OK(result)) {
5399 d_printf("net rpc trustdom del: DeleteUser on user %s failed %s\n",
5400 acct_name, nt_errstr(result) );
5401 goto done;
5404 if (!NT_STATUS_IS_OK(result)) {
5405 d_printf("Could not set trust account password: %s\n",
5406 nt_errstr(result));
5407 goto done;
5410 done:
5411 return result;
5415 * Delete interdomain trust account for a remote domain.
5417 * @param argc Standard argc.
5418 * @param argv Standard argv without initial components.
5420 * @return Integer status (0 means success).
5423 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5425 if (argc > 0 && !c->display_usage) {
5426 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5427 rpc_trustdom_del_internals, argc, argv);
5428 } else {
5429 d_printf("Usage:\n"
5430 "net rpc trustdom del <domain>\n");
5431 return -1;
5435 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5436 struct cli_state *cli,
5437 TALLOC_CTX *mem_ctx,
5438 const char *domain_name)
5440 char *dc_name = NULL;
5441 const char *buffer = NULL;
5442 struct rpc_pipe_client *netr;
5443 NTSTATUS status;
5445 /* Use NetServerEnum2 */
5447 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5448 SAFE_FREE(dc_name);
5449 return NT_STATUS_OK;
5452 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5453 for domain %s\n", domain_name));
5455 /* Try netr_GetDcName */
5457 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5458 &netr);
5459 if (!NT_STATUS_IS_OK(status)) {
5460 return status;
5463 status = rpccli_netr_GetDcName(netr, mem_ctx,
5464 cli->desthost,
5465 domain_name,
5466 &buffer,
5467 NULL);
5468 TALLOC_FREE(netr);
5470 if (NT_STATUS_IS_OK(status)) {
5471 return status;
5474 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5475 for domain %s\n", domain_name));
5477 return status;
5481 * Establish trust relationship to a trusting domain.
5482 * Interdomain account must already be created on remote PDC.
5484 * @param c A net_context structure.
5485 * @param argc Standard argc.
5486 * @param argv Standard argv without initial components.
5488 * @return Integer status (0 means success).
5491 static int rpc_trustdom_establish(struct net_context *c, int argc,
5492 const char **argv)
5494 struct cli_state *cli = NULL;
5495 struct sockaddr_storage server_ss;
5496 struct rpc_pipe_client *pipe_hnd = NULL;
5497 struct policy_handle connect_hnd;
5498 TALLOC_CTX *mem_ctx;
5499 NTSTATUS nt_status;
5500 DOM_SID *domain_sid;
5502 char* domain_name;
5503 char* acct_name;
5504 fstring pdc_name;
5505 union lsa_PolicyInformation *info = NULL;
5508 * Connect to \\server\ipc$ as 'our domain' account with password
5511 if (argc != 1 || c->display_usage) {
5512 d_printf("Usage:\n"
5513 "net rpc trustdom establish <domain_name>\n");
5514 return -1;
5517 domain_name = smb_xstrdup(argv[0]);
5518 strupper_m(domain_name);
5520 /* account name used at first is our domain's name with '$' */
5521 if (asprintf(&acct_name, "%s$", lp_workgroup()) == -1) {
5522 return -1;
5524 strupper_m(acct_name);
5527 * opt_workgroup will be used by connection functions further,
5528 * hence it should be set to remote domain name instead of ours
5530 if (c->opt_workgroup) {
5531 c->opt_workgroup = smb_xstrdup(domain_name);
5534 c->opt_user_name = acct_name;
5536 /* find the domain controller */
5537 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5538 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5539 return -1;
5542 /* connect to ipc$ as username/password */
5543 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5544 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5546 /* Is it trusting domain account for sure ? */
5547 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5548 nt_errstr(nt_status)));
5549 return -1;
5552 /* store who we connected to */
5554 saf_store( domain_name, pdc_name );
5557 * Connect to \\server\ipc$ again (this time anonymously)
5560 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5561 (char*)pdc_name);
5563 if (NT_STATUS_IS_ERR(nt_status)) {
5564 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5565 domain_name, nt_errstr(nt_status)));
5566 return -1;
5569 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5570 "domain %s", domain_name))) {
5571 DEBUG(0, ("talloc_init() failed\n"));
5572 cli_shutdown(cli);
5573 return -1;
5576 /* Make sure we're talking to a proper server */
5578 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5579 if (!NT_STATUS_IS_OK(nt_status)) {
5580 cli_shutdown(cli);
5581 talloc_destroy(mem_ctx);
5582 return -1;
5586 * Call LsaOpenPolicy and LsaQueryInfo
5589 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5590 &pipe_hnd);
5591 if (!NT_STATUS_IS_OK(nt_status)) {
5592 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5593 cli_shutdown(cli);
5594 talloc_destroy(mem_ctx);
5595 return -1;
5598 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, KEY_QUERY_VALUE,
5599 &connect_hnd);
5600 if (NT_STATUS_IS_ERR(nt_status)) {
5601 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5602 nt_errstr(nt_status)));
5603 cli_shutdown(cli);
5604 talloc_destroy(mem_ctx);
5605 return -1;
5608 /* Querying info level 5 */
5610 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5611 &connect_hnd,
5612 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5613 &info);
5614 if (NT_STATUS_IS_ERR(nt_status)) {
5615 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5616 nt_errstr(nt_status)));
5617 cli_shutdown(cli);
5618 talloc_destroy(mem_ctx);
5619 return -1;
5622 domain_sid = info->account_domain.sid;
5624 /* There should be actually query info level 3 (following nt serv behaviour),
5625 but I still don't know if it's _really_ necessary */
5628 * Store the password in secrets db
5631 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5632 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5633 cli_shutdown(cli);
5634 talloc_destroy(mem_ctx);
5635 return -1;
5639 * Close the pipes and clean up
5642 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5643 if (NT_STATUS_IS_ERR(nt_status)) {
5644 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5645 nt_errstr(nt_status)));
5646 cli_shutdown(cli);
5647 talloc_destroy(mem_ctx);
5648 return -1;
5651 cli_shutdown(cli);
5653 talloc_destroy(mem_ctx);
5655 d_printf("Trust to domain %s established\n", domain_name);
5656 return 0;
5660 * Revoke trust relationship to the remote domain.
5662 * @param c A net_context structure.
5663 * @param argc Standard argc.
5664 * @param argv Standard argv without initial components.
5666 * @return Integer status (0 means success).
5669 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5670 const char **argv)
5672 char* domain_name;
5673 int rc = -1;
5675 if (argc < 1 || c->display_usage) {
5676 d_printf("Usage:\n"
5677 "net rpc trustdom revoke <domain_name>\n"
5678 " Revoke trust relationship\n"
5679 " domain_name\tName of domain to revoke trust\n");
5680 return -1;
5683 /* generate upper cased domain name */
5684 domain_name = smb_xstrdup(argv[0]);
5685 strupper_m(domain_name);
5687 /* delete password of the trust */
5688 if (!pdb_del_trusteddom_pw(domain_name)) {
5689 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5690 domain_name));
5691 goto done;
5694 rc = 0;
5695 done:
5696 SAFE_FREE(domain_name);
5697 return rc;
5700 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5701 const DOM_SID *domain_sid,
5702 const char *domain_name,
5703 struct cli_state *cli,
5704 struct rpc_pipe_client *pipe_hnd,
5705 TALLOC_CTX *mem_ctx,
5706 int argc,
5707 const char **argv)
5709 fstring str_sid;
5710 sid_to_fstring(str_sid, domain_sid);
5711 d_printf("%s\n", str_sid);
5712 return NT_STATUS_OK;
5715 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5717 fstring ascii_sid, padding;
5718 int pad_len, col_len = 20;
5720 /* convert sid into ascii string */
5721 sid_to_fstring(ascii_sid, dom_sid);
5723 /* calculate padding space for d_printf to look nicer */
5724 pad_len = col_len - strlen(trusted_dom_name);
5725 padding[pad_len] = 0;
5726 do padding[--pad_len] = ' '; while (pad_len);
5728 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5731 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5732 TALLOC_CTX *mem_ctx,
5733 struct policy_handle *pol,
5734 DOM_SID dom_sid,
5735 const char *trusted_dom_name)
5737 NTSTATUS nt_status;
5738 union lsa_TrustedDomainInfo *info = NULL;
5739 char *cleartextpwd = NULL;
5740 uint8_t nt_hash[16];
5741 DATA_BLOB data;
5743 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5744 pol,
5745 &dom_sid,
5746 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5747 &info);
5748 if (NT_STATUS_IS_ERR(nt_status)) {
5749 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5750 nt_errstr(nt_status)));
5751 goto done;
5754 data = data_blob(info->password.password->data,
5755 info->password.password->length);
5757 if (!rpccli_get_pwd_hash(pipe_hnd, nt_hash)) {
5758 DEBUG(0, ("Could not retrieve password hash\n"));
5759 goto done;
5762 cleartextpwd = decrypt_trustdom_secret(nt_hash, &data);
5764 if (cleartextpwd == NULL) {
5765 DEBUG(0,("retrieved NULL password\n"));
5766 nt_status = NT_STATUS_UNSUCCESSFUL;
5767 goto done;
5770 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5771 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5772 nt_status = NT_STATUS_UNSUCCESSFUL;
5773 goto done;
5776 #ifdef DEBUG_PASSWORD
5777 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5778 "password: [%s]\n", trusted_dom_name,
5779 sid_string_dbg(&dom_sid), cleartextpwd));
5780 #endif
5782 done:
5783 SAFE_FREE(cleartextpwd);
5784 data_blob_free(&data);
5786 return nt_status;
5789 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5790 const char **argv)
5792 /* common variables */
5793 TALLOC_CTX* mem_ctx;
5794 struct cli_state *cli = NULL;
5795 struct rpc_pipe_client *pipe_hnd = NULL;
5796 NTSTATUS nt_status;
5797 const char *domain_name = NULL;
5798 DOM_SID *queried_dom_sid;
5799 struct policy_handle connect_hnd;
5800 union lsa_PolicyInformation *info = NULL;
5802 /* trusted domains listing variables */
5803 unsigned int enum_ctx = 0;
5804 int i;
5805 struct lsa_DomainList dom_list;
5806 fstring pdc_name;
5808 if (c->display_usage) {
5809 d_printf("Usage:\n"
5810 "net rpc trustdom vampire\n"
5811 " Vampire trust relationship from remote server\n");
5812 return 0;
5816 * Listing trusted domains (stored in secrets.tdb, if local)
5819 mem_ctx = talloc_init("trust relationships vampire");
5822 * set domain and pdc name to local samba server (default)
5823 * or to remote one given in command line
5826 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
5827 domain_name = c->opt_workgroup;
5828 c->opt_target_workgroup = c->opt_workgroup;
5829 } else {
5830 fstrcpy(pdc_name, global_myname());
5831 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5832 c->opt_target_workgroup = domain_name;
5835 /* open \PIPE\lsarpc and open policy handle */
5836 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
5837 if (!NT_STATUS_IS_OK(nt_status)) {
5838 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
5839 nt_errstr(nt_status)));
5840 talloc_destroy(mem_ctx);
5841 return -1;
5844 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5845 &pipe_hnd);
5846 if (!NT_STATUS_IS_OK(nt_status)) {
5847 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5848 nt_errstr(nt_status) ));
5849 cli_shutdown(cli);
5850 talloc_destroy(mem_ctx);
5851 return -1;
5854 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
5855 &connect_hnd);
5856 if (NT_STATUS_IS_ERR(nt_status)) {
5857 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5858 nt_errstr(nt_status)));
5859 cli_shutdown(cli);
5860 talloc_destroy(mem_ctx);
5861 return -1;
5864 /* query info level 5 to obtain sid of a domain being queried */
5865 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5866 &connect_hnd,
5867 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5868 &info);
5870 if (NT_STATUS_IS_ERR(nt_status)) {
5871 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5872 nt_errstr(nt_status)));
5873 cli_shutdown(cli);
5874 talloc_destroy(mem_ctx);
5875 return -1;
5878 queried_dom_sid = info->account_domain.sid;
5881 * Keep calling LsaEnumTrustdom over opened pipe until
5882 * the end of enumeration is reached
5885 d_printf("Vampire trusted domains:\n\n");
5887 do {
5888 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
5889 &connect_hnd,
5890 &enum_ctx,
5891 &dom_list,
5892 (uint32_t)-1);
5893 if (NT_STATUS_IS_ERR(nt_status)) {
5894 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5895 nt_errstr(nt_status)));
5896 cli_shutdown(cli);
5897 talloc_destroy(mem_ctx);
5898 return -1;
5901 for (i = 0; i < dom_list.count; i++) {
5903 print_trusted_domain(dom_list.domains[i].sid,
5904 dom_list.domains[i].name.string);
5906 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5907 *dom_list.domains[i].sid,
5908 dom_list.domains[i].name.string);
5909 if (!NT_STATUS_IS_OK(nt_status)) {
5910 cli_shutdown(cli);
5911 talloc_destroy(mem_ctx);
5912 return -1;
5917 * in case of no trusted domains say something rather
5918 * than just display blank line
5920 if (!dom_list.count) d_printf("none\n");
5922 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5924 /* close this connection before doing next one */
5925 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5926 if (NT_STATUS_IS_ERR(nt_status)) {
5927 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5928 nt_errstr(nt_status)));
5929 cli_shutdown(cli);
5930 talloc_destroy(mem_ctx);
5931 return -1;
5934 /* close lsarpc pipe and connection to IPC$ */
5935 cli_shutdown(cli);
5937 talloc_destroy(mem_ctx);
5938 return 0;
5941 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
5943 /* common variables */
5944 TALLOC_CTX* mem_ctx;
5945 struct cli_state *cli = NULL, *remote_cli = NULL;
5946 struct rpc_pipe_client *pipe_hnd = NULL;
5947 NTSTATUS nt_status;
5948 const char *domain_name = NULL;
5949 DOM_SID *queried_dom_sid;
5950 fstring padding;
5951 int ascii_dom_name_len;
5952 struct policy_handle connect_hnd;
5953 union lsa_PolicyInformation *info = NULL;
5955 /* trusted domains listing variables */
5956 unsigned int num_domains, enum_ctx = 0;
5957 int i, pad_len, col_len = 20;
5958 struct lsa_DomainList dom_list;
5959 fstring pdc_name;
5961 /* trusting domains listing variables */
5962 struct policy_handle domain_hnd;
5963 struct samr_SamArray *trusts = NULL;
5965 if (c->display_usage) {
5966 d_printf("Usage:\n"
5967 "net rpc trustdom list\n"
5968 " List trust relationships\n");
5969 return 0;
5973 * Listing trusted domains (stored in secrets.tdb, if local)
5976 mem_ctx = talloc_init("trust relationships listing");
5979 * set domain and pdc name to local samba server (default)
5980 * or to remote one given in command line
5983 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
5984 domain_name = c->opt_workgroup;
5985 c->opt_target_workgroup = c->opt_workgroup;
5986 } else {
5987 fstrcpy(pdc_name, global_myname());
5988 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5989 c->opt_target_workgroup = domain_name;
5992 /* open \PIPE\lsarpc and open policy handle */
5993 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
5994 if (!NT_STATUS_IS_OK(nt_status)) {
5995 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
5996 nt_errstr(nt_status)));
5997 talloc_destroy(mem_ctx);
5998 return -1;
6001 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6002 &pipe_hnd);
6003 if (!NT_STATUS_IS_OK(nt_status)) {
6004 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6005 nt_errstr(nt_status) ));
6006 cli_shutdown(cli);
6007 talloc_destroy(mem_ctx);
6008 return -1;
6011 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, KEY_QUERY_VALUE,
6012 &connect_hnd);
6013 if (NT_STATUS_IS_ERR(nt_status)) {
6014 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6015 nt_errstr(nt_status)));
6016 cli_shutdown(cli);
6017 talloc_destroy(mem_ctx);
6018 return -1;
6021 /* query info level 5 to obtain sid of a domain being queried */
6022 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6023 &connect_hnd,
6024 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6025 &info);
6027 if (NT_STATUS_IS_ERR(nt_status)) {
6028 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6029 nt_errstr(nt_status)));
6030 cli_shutdown(cli);
6031 talloc_destroy(mem_ctx);
6032 return -1;
6035 queried_dom_sid = info->account_domain.sid;
6038 * Keep calling LsaEnumTrustdom over opened pipe until
6039 * the end of enumeration is reached
6042 d_printf("Trusted domains list:\n\n");
6044 do {
6045 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6046 &connect_hnd,
6047 &enum_ctx,
6048 &dom_list,
6049 (uint32_t)-1);
6050 if (NT_STATUS_IS_ERR(nt_status)) {
6051 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6052 nt_errstr(nt_status)));
6053 cli_shutdown(cli);
6054 talloc_destroy(mem_ctx);
6055 return -1;
6058 for (i = 0; i < dom_list.count; i++) {
6059 print_trusted_domain(dom_list.domains[i].sid,
6060 dom_list.domains[i].name.string);
6064 * in case of no trusted domains say something rather
6065 * than just display blank line
6067 if (!dom_list.count) d_printf("none\n");
6069 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6071 /* close this connection before doing next one */
6072 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6073 if (NT_STATUS_IS_ERR(nt_status)) {
6074 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6075 nt_errstr(nt_status)));
6076 cli_shutdown(cli);
6077 talloc_destroy(mem_ctx);
6078 return -1;
6081 TALLOC_FREE(pipe_hnd);
6084 * Listing trusting domains (stored in passdb backend, if local)
6087 d_printf("\nTrusting domains list:\n\n");
6090 * Open \PIPE\samr and get needed policy handles
6092 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6093 &pipe_hnd);
6094 if (!NT_STATUS_IS_OK(nt_status)) {
6095 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6096 cli_shutdown(cli);
6097 talloc_destroy(mem_ctx);
6098 return -1;
6101 /* SamrConnect2 */
6102 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6103 pipe_hnd->desthost,
6104 SAMR_ACCESS_LOOKUP_DOMAIN,
6105 &connect_hnd);
6106 if (!NT_STATUS_IS_OK(nt_status)) {
6107 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6108 nt_errstr(nt_status)));
6109 cli_shutdown(cli);
6110 talloc_destroy(mem_ctx);
6111 return -1;
6114 /* SamrOpenDomain - we have to open domain policy handle in order to be
6115 able to enumerate accounts*/
6116 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6117 &connect_hnd,
6118 SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
6119 queried_dom_sid,
6120 &domain_hnd);
6121 if (!NT_STATUS_IS_OK(nt_status)) {
6122 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6123 nt_errstr(nt_status)));
6124 cli_shutdown(cli);
6125 talloc_destroy(mem_ctx);
6126 return -1;
6130 * perform actual enumeration
6133 enum_ctx = 0; /* reset enumeration context from last enumeration */
6134 do {
6136 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6137 &domain_hnd,
6138 &enum_ctx,
6139 ACB_DOMTRUST,
6140 &trusts,
6141 0xffff,
6142 &num_domains);
6143 if (NT_STATUS_IS_ERR(nt_status)) {
6144 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6145 nt_errstr(nt_status)));
6146 cli_shutdown(cli);
6147 talloc_destroy(mem_ctx);
6148 return -1;
6151 for (i = 0; i < num_domains; i++) {
6153 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6156 * get each single domain's sid (do we _really_ need this ?):
6157 * 1) connect to domain's pdc
6158 * 2) query the pdc for domain's sid
6161 /* get rid of '$' tail */
6162 ascii_dom_name_len = strlen(str);
6163 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6164 str[ascii_dom_name_len - 1] = '\0';
6166 /* calculate padding space for d_printf to look nicer */
6167 pad_len = col_len - strlen(str);
6168 padding[pad_len] = 0;
6169 do padding[--pad_len] = ' '; while (pad_len);
6171 /* set opt_* variables to remote domain */
6172 strupper_m(str);
6173 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6174 c->opt_target_workgroup = c->opt_workgroup;
6176 d_printf("%s%s", str, padding);
6178 /* connect to remote domain controller */
6179 nt_status = net_make_ipc_connection(c,
6180 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6181 &remote_cli);
6182 if (NT_STATUS_IS_OK(nt_status)) {
6183 /* query for domain's sid */
6184 if (run_rpc_command(
6185 c, remote_cli,
6186 &ndr_table_lsarpc.syntax_id, 0,
6187 rpc_query_domain_sid, argc,
6188 argv))
6189 d_fprintf(stderr, "couldn't get domain's sid\n");
6191 cli_shutdown(remote_cli);
6193 } else {
6194 d_fprintf(stderr, "domain controller is not "
6195 "responding: %s\n",
6196 nt_errstr(nt_status));
6200 if (!num_domains) d_printf("none\n");
6202 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6204 /* close opened samr and domain policy handles */
6205 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6206 if (!NT_STATUS_IS_OK(nt_status)) {
6207 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6210 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6211 if (!NT_STATUS_IS_OK(nt_status)) {
6212 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6215 /* close samr pipe and connection to IPC$ */
6216 cli_shutdown(cli);
6218 talloc_destroy(mem_ctx);
6219 return 0;
6223 * Entrypoint for 'net rpc trustdom' code.
6225 * @param argc Standard argc.
6226 * @param argv Standard argv without initial components.
6228 * @return Integer status (0 means success).
6231 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6233 struct functable func[] = {
6235 "add",
6236 rpc_trustdom_add,
6237 NET_TRANSPORT_RPC,
6238 "Add trusted domain's account",
6239 "net rpc trustdom add\n"
6240 " Add trusted domain's account"
6243 "del",
6244 rpc_trustdom_del,
6245 NET_TRANSPORT_RPC,
6246 "Remove trusted domain's account",
6247 "net rpc trustdom del\n"
6248 " Remove trusted domain's account"
6251 "establish",
6252 rpc_trustdom_establish,
6253 NET_TRANSPORT_RPC,
6254 "Establish trust relationship",
6255 "net rpc trustdom establish\n"
6256 " Establish trust relationship"
6259 "revoke",
6260 rpc_trustdom_revoke,
6261 NET_TRANSPORT_RPC,
6262 "Revoke trust relationship",
6263 "net rpc trustdom revoke\n"
6264 " Revoke trust relationship"
6267 "list",
6268 rpc_trustdom_list,
6269 NET_TRANSPORT_RPC,
6270 "List domain trusts",
6271 "net rpc trustdom list\n"
6272 " List domain trusts"
6275 "vampire",
6276 rpc_trustdom_vampire,
6277 NET_TRANSPORT_RPC,
6278 "Vampire trusts from remote server",
6279 "net rpc trustdom vampire\n"
6280 " Vampire trusts from remote server"
6282 {NULL, NULL, 0, NULL, NULL}
6285 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6289 * Check if a server will take rpc commands
6290 * @param flags Type of server to connect to (PDC, DMB, localhost)
6291 * if the host is not explicitly specified
6292 * @return bool (true means rpc supported)
6294 bool net_rpc_check(struct net_context *c, unsigned flags)
6296 struct cli_state *cli;
6297 bool ret = false;
6298 struct sockaddr_storage server_ss;
6299 char *server_name = NULL;
6300 NTSTATUS status;
6302 /* flags (i.e. server type) may depend on command */
6303 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6304 return false;
6306 if ((cli = cli_initialise()) == NULL) {
6307 return false;
6310 status = cli_connect(cli, server_name, &server_ss);
6311 if (!NT_STATUS_IS_OK(status))
6312 goto done;
6313 if (!attempt_netbios_session_request(&cli, global_myname(),
6314 server_name, &server_ss))
6315 goto done;
6316 status = cli_negprot(cli);
6317 if (!NT_STATUS_IS_OK(status))
6318 goto done;
6319 if (cli->protocol < PROTOCOL_NT1)
6320 goto done;
6322 ret = true;
6323 done:
6324 cli_shutdown(cli);
6325 return ret;
6328 /* dump sam database via samsync rpc calls */
6329 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6330 if (c->display_usage) {
6331 d_printf("Usage:\n"
6332 "net rpc samdump\n"
6333 " Dump remote SAM database\n");
6334 return 0;
6337 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6338 NET_FLAGS_ANONYMOUS,
6339 rpc_samdump_internals, argc, argv);
6342 /* syncronise sam database via samsync rpc calls */
6343 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6345 struct functable func[] = {
6347 "ldif",
6348 rpc_vampire_ldif,
6349 NET_TRANSPORT_RPC,
6350 "Dump remote SAM database to ldif",
6351 "net rpc vampire ldif\n"
6352 " Dump remote SAM database to LDIF file or stdout"
6355 "keytab",
6356 rpc_vampire_keytab,
6357 NET_TRANSPORT_RPC,
6358 "Dump remote SAM database to Kerberos Keytab",
6359 "net rpc vampire keytab\n"
6360 " Dump remote SAM database to Kerberos keytab file"
6363 "passdb",
6364 rpc_vampire_passdb,
6365 NET_TRANSPORT_RPC,
6366 "Dump remote SAM database to passdb",
6367 "net rpc vampire passdb\n"
6368 " Dump remote SAM database to passdb"
6371 {NULL, NULL, 0, NULL, NULL}
6374 if (argc == 0) {
6375 if (c->display_usage) {
6376 d_printf("Usage:\n"
6377 "net rpc vampire\n"
6378 " Vampire remote SAM database\n");
6379 return 0;
6382 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6383 NET_FLAGS_ANONYMOUS,
6384 rpc_vampire_internals,
6385 argc, argv);
6388 return net_run_function(c, argc, argv, "net rpc vampire", func);
6392 * Migrate everything from a print server.
6394 * @param c A net_context structure.
6395 * @param argc Standard main() style argc.
6396 * @param argv Standard main() style argv. Initial components are already
6397 * stripped.
6399 * @return A shell status integer (0 for success).
6401 * The order is important !
6402 * To successfully add drivers the print queues have to exist !
6403 * Applying ACLs should be the last step, because you're easily locked out.
6406 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6407 const char **argv)
6409 int ret;
6411 if (c->display_usage) {
6412 d_printf("Usage:\n"
6413 "net rpc printer migrate all\n"
6414 " Migrate everything from a print server\n");
6415 return 0;
6418 if (!c->opt_host) {
6419 d_printf("no server to migrate\n");
6420 return -1;
6423 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6424 rpc_printer_migrate_printers_internals, argc,
6425 argv);
6426 if (ret)
6427 return ret;
6429 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6430 rpc_printer_migrate_drivers_internals, argc,
6431 argv);
6432 if (ret)
6433 return ret;
6435 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6436 rpc_printer_migrate_forms_internals, argc, argv);
6437 if (ret)
6438 return ret;
6440 ret = run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6441 rpc_printer_migrate_settings_internals, argc,
6442 argv);
6443 if (ret)
6444 return ret;
6446 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6447 rpc_printer_migrate_security_internals, argc,
6448 argv);
6453 * Migrate print drivers from a print server.
6455 * @param c A net_context structure.
6456 * @param argc Standard main() style argc.
6457 * @param argv Standard main() style argv. Initial components are already
6458 * stripped.
6460 * @return A shell status integer (0 for success).
6462 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6463 const char **argv)
6465 if (c->display_usage) {
6466 d_printf("Usage:\n"
6467 "net rpc printer migrate drivers\n"
6468 " Migrate print-drivers from a print-server\n");
6469 return 0;
6472 if (!c->opt_host) {
6473 d_printf("no server to migrate\n");
6474 return -1;
6477 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6478 rpc_printer_migrate_drivers_internals,
6479 argc, argv);
6483 * Migrate print-forms from a print-server.
6485 * @param c A net_context structure.
6486 * @param argc Standard main() style argc.
6487 * @param argv Standard main() style argv. Initial components are already
6488 * stripped.
6490 * @return A shell status integer (0 for success).
6492 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6493 const char **argv)
6495 if (c->display_usage) {
6496 d_printf("Usage:\n"
6497 "net rpc printer migrate forms\n"
6498 " Migrate print-forms from a print-server\n");
6499 return 0;
6502 if (!c->opt_host) {
6503 d_printf("no server to migrate\n");
6504 return -1;
6507 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6508 rpc_printer_migrate_forms_internals,
6509 argc, argv);
6513 * Migrate printers from a print-server.
6515 * @param c A net_context structure.
6516 * @param argc Standard main() style argc.
6517 * @param argv Standard main() style argv. Initial components are already
6518 * stripped.
6520 * @return A shell status integer (0 for success).
6522 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6523 const char **argv)
6525 if (c->display_usage) {
6526 d_printf("Usage:\n"
6527 "net rpc printer migrate printers\n"
6528 " Migrate printers from a print-server\n");
6529 return 0;
6532 if (!c->opt_host) {
6533 d_printf("no server to migrate\n");
6534 return -1;
6537 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6538 rpc_printer_migrate_printers_internals,
6539 argc, argv);
6543 * Migrate printer-ACLs from a print-server
6545 * @param c A net_context structure.
6546 * @param argc Standard main() style argc.
6547 * @param argv Standard main() style argv. Initial components are already
6548 * stripped.
6550 * @return A shell status integer (0 for success).
6552 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6553 const char **argv)
6555 if (c->display_usage) {
6556 d_printf("Usage:\n"
6557 "net rpc printer migrate security\n"
6558 " Migrate printer-ACLs from a print-server\n");
6559 return 0;
6562 if (!c->opt_host) {
6563 d_printf("no server to migrate\n");
6564 return -1;
6567 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6568 rpc_printer_migrate_security_internals,
6569 argc, argv);
6573 * Migrate printer-settings from a print-server.
6575 * @param c A net_context structure.
6576 * @param argc Standard main() style argc.
6577 * @param argv Standard main() style argv. Initial components are already
6578 * stripped.
6580 * @return A shell status integer (0 for success).
6582 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6583 const char **argv)
6585 if (c->display_usage) {
6586 d_printf("Usage:\n"
6587 "net rpc printer migrate settings\n"
6588 " Migrate printer-settings from a print-server\n");
6589 return 0;
6592 if (!c->opt_host) {
6593 d_printf("no server to migrate\n");
6594 return -1;
6597 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6598 rpc_printer_migrate_settings_internals,
6599 argc, argv);
6603 * 'net rpc printer' entrypoint.
6605 * @param c A net_context structure.
6606 * @param argc Standard main() style argc.
6607 * @param argv Standard main() style argv. Initial components are already
6608 * stripped.
6611 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6614 /* ouch: when addriver and setdriver are called from within
6615 rpc_printer_migrate_drivers_internals, the printer-queue already
6616 *has* to exist */
6618 struct functable func[] = {
6620 "all",
6621 rpc_printer_migrate_all,
6622 NET_TRANSPORT_RPC,
6623 "Migrate all from remote to local print server",
6624 "net rpc printer migrate all\n"
6625 " Migrate all from remote to local print server"
6628 "drivers",
6629 rpc_printer_migrate_drivers,
6630 NET_TRANSPORT_RPC,
6631 "Migrate drivers to local server",
6632 "net rpc printer migrate drivers\n"
6633 " Migrate drivers to local server"
6636 "forms",
6637 rpc_printer_migrate_forms,
6638 NET_TRANSPORT_RPC,
6639 "Migrate froms to local server",
6640 "net rpc printer migrate forms\n"
6641 " Migrate froms to local server"
6644 "printers",
6645 rpc_printer_migrate_printers,
6646 NET_TRANSPORT_RPC,
6647 "Migrate printers to local server",
6648 "net rpc printer migrate printers\n"
6649 " Migrate printers to local server"
6652 "security",
6653 rpc_printer_migrate_security,
6654 NET_TRANSPORT_RPC,
6655 "Mirgate printer ACLs to local server",
6656 "net rpc printer migrate security\n"
6657 " Mirgate printer ACLs to local server"
6660 "settings",
6661 rpc_printer_migrate_settings,
6662 NET_TRANSPORT_RPC,
6663 "Migrate printer settings to local server",
6664 "net rpc printer migrate settings\n"
6665 " Migrate printer settings to local server"
6667 {NULL, NULL, 0, NULL, NULL}
6670 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6675 * List printers on a remote RPC server.
6677 * @param c A net_context structure.
6678 * @param argc Standard main() style argc.
6679 * @param argv Standard main() style argv. Initial components are already
6680 * stripped.
6682 * @return A shell status integer (0 for success).
6684 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6686 if (c->display_usage) {
6687 d_printf("Usage:\n"
6688 "net rpc printer list\n"
6689 " List printers on a remote RPC server\n");
6690 return 0;
6693 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6694 rpc_printer_list_internals,
6695 argc, argv);
6699 * List printer-drivers on a remote RPC server.
6701 * @param c A net_context structure.
6702 * @param argc Standard main() style argc.
6703 * @param argv Standard main() style argv. Initial components are already
6704 * stripped.
6706 * @return A shell status integer (0 for success).
6708 static int rpc_printer_driver_list(struct net_context *c, int argc,
6709 const char **argv)
6711 if (c->display_usage) {
6712 d_printf("Usage:\n"
6713 "net rpc printer driver\n"
6714 " List printer-drivers on a remote RPC server\n");
6715 return 0;
6718 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6719 rpc_printer_driver_list_internals,
6720 argc, argv);
6724 * Publish printer in ADS via MSRPC.
6726 * @param c A net_context structure.
6727 * @param argc Standard main() style argc.
6728 * @param argv Standard main() style argv. Initial components are already
6729 * stripped.
6731 * @return A shell status integer (0 for success).
6733 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6734 const char **argv)
6736 if (c->display_usage) {
6737 d_printf("Usage:\n"
6738 "net rpc printer publish publish\n"
6739 " Publish printer in ADS via MSRPC\n");
6740 return 0;
6743 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6744 rpc_printer_publish_publish_internals,
6745 argc, argv);
6749 * Update printer in ADS via MSRPC.
6751 * @param c A net_context structure.
6752 * @param argc Standard main() style argc.
6753 * @param argv Standard main() style argv. Initial components are already
6754 * stripped.
6756 * @return A shell status integer (0 for success).
6758 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6760 if (c->display_usage) {
6761 d_printf("Usage:\n"
6762 "net rpc printer publish update\n"
6763 " Update printer in ADS via MSRPC\n");
6764 return 0;
6767 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6768 rpc_printer_publish_update_internals,
6769 argc, argv);
6773 * UnPublish printer in ADS via MSRPC.
6775 * @param c A net_context structure.
6776 * @param argc Standard main() style argc.
6777 * @param argv Standard main() style argv. Initial components are already
6778 * stripped.
6780 * @return A shell status integer (0 for success).
6782 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
6783 const char **argv)
6785 if (c->display_usage) {
6786 d_printf("Usage:\n"
6787 "net rpc printer publish unpublish\n"
6788 " UnPublish printer in ADS via MSRPC\n");
6789 return 0;
6792 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6793 rpc_printer_publish_unpublish_internals,
6794 argc, argv);
6798 * List published printers via MSRPC.
6800 * @param c A net_context structure.
6801 * @param argc Standard main() style argc.
6802 * @param argv Standard main() style argv. Initial components are already
6803 * stripped.
6805 * @return A shell status integer (0 for success).
6807 static int rpc_printer_publish_list(struct net_context *c, int argc,
6808 const char **argv)
6810 if (c->display_usage) {
6811 d_printf("Usage:\n"
6812 "net rpc printer publish list\n"
6813 " List published printers via MSRPC\n");
6814 return 0;
6817 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6818 rpc_printer_publish_list_internals,
6819 argc, argv);
6824 * Publish printer in ADS.
6826 * @param c A net_context structure.
6827 * @param argc Standard main() style argc.
6828 * @param argv Standard main() style argv. Initial components are already
6829 * stripped.
6831 * @return A shell status integer (0 for success).
6833 static int rpc_printer_publish(struct net_context *c, int argc,
6834 const char **argv)
6837 struct functable func[] = {
6839 "publish",
6840 rpc_printer_publish_publish,
6841 NET_TRANSPORT_RPC,
6842 "Publish printer in AD",
6843 "net rpc printer publish publish\n"
6844 " Publish printer in AD"
6847 "update",
6848 rpc_printer_publish_update,
6849 NET_TRANSPORT_RPC,
6850 "Update printer in AD",
6851 "net rpc printer publish update\n"
6852 " Update printer in AD"
6855 "unpublish",
6856 rpc_printer_publish_unpublish,
6857 NET_TRANSPORT_RPC,
6858 "Unpublish printer",
6859 "net rpc printer publish unpublish\n"
6860 " Unpublish printer"
6863 "list",
6864 rpc_printer_publish_list,
6865 NET_TRANSPORT_RPC,
6866 "List published printers",
6867 "net rpc printer publish list\n"
6868 " List published printers"
6870 {NULL, NULL, 0, NULL, NULL}
6873 if (argc == 0) {
6874 if (c->display_usage) {
6875 d_printf("Usage:\n");
6876 d_printf("net rpc printer publish\n"
6877 " List published printers\n"
6878 " Alias of net rpc printer publish list\n");
6879 net_display_usage_from_functable(func);
6880 return 0;
6882 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6883 rpc_printer_publish_list_internals,
6884 argc, argv);
6887 return net_run_function(c, argc, argv, "net rpc printer publish",func);
6893 * Display rpc printer help page.
6895 * @param c A net_context structure.
6896 * @param argc Standard main() style argc.
6897 * @param argv Standard main() style argv. Initial components are already
6898 * stripped.
6900 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
6902 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"
6903 "\tlists all printers on print-server\n\n");
6904 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
6905 "\tlists all printer-drivers on print-server\n\n");
6906 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
6907 "\tpublishes printer settings in Active Directory\n"
6908 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
6909 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
6910 "\n\tmigrates printers from remote to local server\n\n");
6911 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
6912 "\n\tmigrates printer-settings from remote to local server\n\n");
6913 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
6914 "\n\tmigrates printer-drivers from remote to local server\n\n");
6915 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
6916 "\n\tmigrates printer-forms from remote to local server\n\n");
6917 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
6918 "\n\tmigrates printer-ACLs from remote to local server\n\n");
6919 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
6920 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
6921 "\tremote to local print-server\n\n");
6922 net_common_methods_usage(c, argc, argv);
6923 net_common_flags_usage(c, argc, argv);
6924 d_printf(
6925 "\t-v or --verbose\t\t\tgive verbose output\n"
6926 "\t --destination\t\tmigration target server (default: localhost)\n");
6928 return -1;
6932 * 'net rpc printer' entrypoint.
6934 * @param c A net_context structure.
6935 * @param argc Standard main() style argc.
6936 * @param argv Standard main() style argv. Initial components are already
6937 * stripped.
6939 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
6941 struct functable func[] = {
6943 "list",
6944 rpc_printer_list,
6945 NET_TRANSPORT_RPC,
6946 "List all printers on print server",
6947 "net rpc printer list\n"
6948 " List all printers on print server"
6951 "migrate",
6952 rpc_printer_migrate,
6953 NET_TRANSPORT_RPC,
6954 "Migrate printer to local server",
6955 "net rpc printer migrate\n"
6956 " Migrate printer to local server"
6959 "driver",
6960 rpc_printer_driver_list,
6961 NET_TRANSPORT_RPC,
6962 "List printer drivers",
6963 "net rpc printer driver\n"
6964 " List printer drivers"
6967 "publish",
6968 rpc_printer_publish,
6969 NET_TRANSPORT_RPC,
6970 "Publish printer in AD",
6971 "net rpc printer publish\n"
6972 " Publish printer in AD"
6974 {NULL, NULL, 0, NULL, NULL}
6977 if (argc == 0) {
6978 if (c->display_usage) {
6979 d_printf("Usage:\n");
6980 d_printf("net rpc printer\n"
6981 " List printers\n");
6982 net_display_usage_from_functable(func);
6983 return 0;
6985 return run_rpc_command(c, NULL, &ndr_table_spoolss.syntax_id, 0,
6986 rpc_printer_list_internals,
6987 argc, argv);
6990 return net_run_function(c, argc, argv, "net rpc printer", func);
6994 * 'net rpc' entrypoint.
6996 * @param c A net_context structure.
6997 * @param argc Standard main() style argc.
6998 * @param argv Standard main() style argv. Initial components are already
6999 * stripped.
7002 int net_rpc(struct net_context *c, int argc, const char **argv)
7004 struct functable func[] = {
7006 "audit",
7007 net_rpc_audit,
7008 NET_TRANSPORT_RPC,
7009 "Modify global audit settings",
7010 "net rpc audit\n"
7011 " Modify global audit settings"
7014 "info",
7015 net_rpc_info,
7016 NET_TRANSPORT_RPC,
7017 "Show basic info about a domain",
7018 "net rpc info\n"
7019 " Show basic info about a domain"
7022 "join",
7023 net_rpc_join,
7024 NET_TRANSPORT_RPC,
7025 "Join a domain",
7026 "net rpc join\n"
7027 " Join a domain"
7030 "oldjoin",
7031 net_rpc_oldjoin,
7032 NET_TRANSPORT_RPC,
7033 "Join a domain created in server manager",
7034 "net rpc oldjoin\n"
7035 " Join a domain created in server manager"
7038 "testjoin",
7039 net_rpc_testjoin,
7040 NET_TRANSPORT_RPC,
7041 "Test that a join is valid",
7042 "net rpc testjoin\n"
7043 " Test that a join is valid"
7046 "user",
7047 net_rpc_user,
7048 NET_TRANSPORT_RPC,
7049 "List/modify users",
7050 "net rpc user\n"
7051 " List/modify users"
7054 "password",
7055 rpc_user_password,
7056 NET_TRANSPORT_RPC,
7057 "Change a user password",
7058 "net rpc password\n"
7059 " Change a user password\n"
7060 " Alias for net rpc user password"
7063 "group",
7064 net_rpc_group,
7065 NET_TRANSPORT_RPC,
7066 "List/modify groups",
7067 "net rpc group\n"
7068 " List/modify groups"
7071 "share",
7072 net_rpc_share,
7073 NET_TRANSPORT_RPC,
7074 "List/modify shares",
7075 "net rpc share\n"
7076 " List/modify shares"
7079 "file",
7080 net_rpc_file,
7081 NET_TRANSPORT_RPC,
7082 "List open files",
7083 "net rpc file\n"
7084 " List open files"
7087 "printer",
7088 net_rpc_printer,
7089 NET_TRANSPORT_RPC,
7090 "List/modify printers",
7091 "net rpc printer\n"
7092 " List/modify printers"
7095 "changetrustpw",
7096 net_rpc_changetrustpw,
7097 NET_TRANSPORT_RPC,
7098 "Change trust account password",
7099 "net rpc changetrustpw\n"
7100 " Change trust account password"
7103 "trustdom",
7104 rpc_trustdom,
7105 NET_TRANSPORT_RPC,
7106 "Modify domain trusts",
7107 "net rpc trustdom\n"
7108 " Modify domain trusts"
7111 "abortshutdown",
7112 rpc_shutdown_abort,
7113 NET_TRANSPORT_RPC,
7114 "Abort a remote shutdown",
7115 "net rpc abortshutdown\n"
7116 " Abort a remote shutdown"
7119 "shutdown",
7120 rpc_shutdown,
7121 NET_TRANSPORT_RPC,
7122 "Shutdown a remote server",
7123 "net rpc shutdown\n"
7124 " Shutdown a remote server"
7127 "samdump",
7128 rpc_samdump,
7129 NET_TRANSPORT_RPC,
7130 "Dump SAM data of remote NT PDC",
7131 "net rpc samdump\n"
7132 " Dump SAM data of remote NT PDC"
7135 "vampire",
7136 rpc_vampire,
7137 NET_TRANSPORT_RPC,
7138 "Sync a remote NT PDC's data into local passdb",
7139 "net rpc vampire\n"
7140 " Sync a remote NT PDC's data into local passdb"
7143 "getsid",
7144 net_rpc_getsid,
7145 NET_TRANSPORT_RPC,
7146 "Fetch the domain sid into local secrets.tdb",
7147 "net rpc getsid\n"
7148 " Fetch the domain sid into local secrets.tdb"
7151 "rights",
7152 net_rpc_rights,
7153 NET_TRANSPORT_RPC,
7154 "Manage privileges assigned to SID",
7155 "net rpc rights\n"
7156 " Manage privileges assigned to SID"
7159 "service",
7160 net_rpc_service,
7161 NET_TRANSPORT_RPC,
7162 "Start/stop/query remote services",
7163 "net rpc service\n"
7164 " Start/stop/query remote services"
7167 "registry",
7168 net_rpc_registry,
7169 NET_TRANSPORT_RPC,
7170 "Manage registry hives",
7171 "net rpc registry\n"
7172 " Manage registry hives"
7175 "shell",
7176 net_rpc_shell,
7177 NET_TRANSPORT_RPC,
7178 "Open interactive shell on remote server",
7179 "net rpc shell\n"
7180 " Open interactive shell on remote server"
7182 {NULL, NULL, 0, NULL, NULL}
7184 return net_run_function(c, argc, argv, "net rpc", func);