net: use netapi for NetFileClose.
[Samba.git] / source / utils / net_rpc.c
blobc584b54e9e90a7a06d788fc8d980b279e667d689
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 extern const char *share_type[];
31 /**
32 * @file net_rpc.c
34 * @brief RPC based subcommands for the 'net' utility.
36 * This file should contain much of the functionality that used to
37 * be found in rpcclient, execpt that the commands should change
38 * less often, and the fucntionality should be sane (the user is not
39 * expected to know a rid/sid before they conduct an operation etc.)
41 * @todo Perhaps eventually these should be split out into a number
42 * of files, as this could get quite big.
43 **/
46 /**
47 * Many of the RPC functions need the domain sid. This function gets
48 * it at the start of every run
50 * @param cli A cli_state already connected to the remote machine
52 * @return The Domain SID of the remote machine.
53 **/
55 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
56 DOM_SID **domain_sid,
57 const char **domain_name)
59 struct rpc_pipe_client *lsa_pipe;
60 POLICY_HND pol;
61 NTSTATUS result = NT_STATUS_OK;
62 union lsa_PolicyInformation *info = NULL;
64 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
65 &lsa_pipe);
66 if (!NT_STATUS_IS_OK(result)) {
67 d_fprintf(stderr, "Could not initialise lsa pipe\n");
68 return result;
71 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
72 SEC_RIGHTS_MAXIMUM_ALLOWED,
73 &pol);
74 if (!NT_STATUS_IS_OK(result)) {
75 d_fprintf(stderr, "open_policy failed: %s\n",
76 nt_errstr(result));
77 return result;
80 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
81 &pol,
82 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
83 &info);
84 if (!NT_STATUS_IS_OK(result)) {
85 d_fprintf(stderr, "lsaquery failed: %s\n",
86 nt_errstr(result));
87 return result;
90 *domain_name = info->account_domain.name.string;
91 *domain_sid = info->account_domain.sid;
93 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
94 TALLOC_FREE(lsa_pipe);
96 return NT_STATUS_OK;
99 /**
100 * Run a single RPC command, from start to finish.
102 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
103 * @param conn_flag a NET_FLAG_ combination. Passed to
104 * net_make_ipc_connection.
105 * @param argc Standard main() style argc.
106 * @param argv Standard main() style argv. Initial components are already
107 * stripped.
108 * @return A shell status integer (0 for success).
111 int run_rpc_command(struct net_context *c,
112 struct cli_state *cli_arg,
113 const struct ndr_syntax_id *interface,
114 int conn_flags,
115 rpc_command_fn fn,
116 int argc,
117 const char **argv)
119 struct cli_state *cli = NULL;
120 struct rpc_pipe_client *pipe_hnd = NULL;
121 TALLOC_CTX *mem_ctx;
122 NTSTATUS nt_status;
123 DOM_SID *domain_sid;
124 const char *domain_name;
126 /* make use of cli_state handed over as an argument, if possible */
127 if (!cli_arg) {
128 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
129 if (!NT_STATUS_IS_OK(nt_status)) {
130 DEBUG(1, ("failed to make ipc connection: %s\n",
131 nt_errstr(nt_status)));
132 return -1;
134 } else {
135 cli = cli_arg;
138 if (!cli) {
139 return -1;
142 /* Create mem_ctx */
144 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
145 DEBUG(0, ("talloc_init() failed\n"));
146 cli_shutdown(cli);
147 return -1;
150 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
151 &domain_name);
152 if (!NT_STATUS_IS_OK(nt_status)) {
153 cli_shutdown(cli);
154 return -1;
157 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
158 if (lp_client_schannel()
159 && (ndr_syntax_id_equal(interface,
160 &ndr_table_netlogon.syntax_id))) {
161 /* Always try and create an schannel netlogon pipe. */
162 nt_status = cli_rpc_pipe_open_schannel(
163 cli, interface,
164 PIPE_AUTH_LEVEL_PRIVACY, domain_name,
165 &pipe_hnd);
166 if (!NT_STATUS_IS_OK(nt_status)) {
167 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
168 nt_errstr(nt_status) ));
169 cli_shutdown(cli);
170 return -1;
172 } else {
173 if (conn_flags & NET_FLAGS_SEAL) {
174 nt_status = cli_rpc_pipe_open_ntlmssp(
175 cli, interface,
176 PIPE_AUTH_LEVEL_PRIVACY,
177 lp_workgroup(), c->opt_user_name,
178 c->opt_password, &pipe_hnd);
179 } else {
180 nt_status = cli_rpc_pipe_open_noauth(
181 cli, interface,
182 &pipe_hnd);
184 if (!NT_STATUS_IS_OK(nt_status)) {
185 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
186 cli_get_pipe_name_from_iface(
187 debug_ctx(), cli, interface),
188 nt_errstr(nt_status) ));
189 cli_shutdown(cli);
190 return -1;
195 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
197 if (!NT_STATUS_IS_OK(nt_status)) {
198 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
199 } else {
200 DEBUG(5, ("rpc command function succedded\n"));
203 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
204 if (pipe_hnd) {
205 TALLOC_FREE(pipe_hnd);
209 /* close the connection only if it was opened here */
210 if (!cli_arg) {
211 cli_shutdown(cli);
214 talloc_destroy(mem_ctx);
215 return (!NT_STATUS_IS_OK(nt_status));
219 * Force a change of the trust acccount password.
221 * All parameters are provided by the run_rpc_command function, except for
222 * argc, argv which are passed through.
224 * @param domain_sid The domain sid acquired from the remote server.
225 * @param cli A cli_state connected to the server.
226 * @param mem_ctx Talloc context, destroyed on completion of the function.
227 * @param argc Standard main() style argc.
228 * @param argv Standard main() style argv. Initial components are already
229 * stripped.
231 * @return Normal NTSTATUS return.
234 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
235 const DOM_SID *domain_sid,
236 const char *domain_name,
237 struct cli_state *cli,
238 struct rpc_pipe_client *pipe_hnd,
239 TALLOC_CTX *mem_ctx,
240 int argc,
241 const char **argv)
244 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
248 * Force a change of the trust acccount password.
250 * @param argc Standard main() style argc.
251 * @param argv Standard main() style argv. Initial components are already
252 * stripped.
254 * @return A shell status integer (0 for success).
257 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
259 if (c->display_usage) {
260 d_printf("Usage:\n"
261 "net rpc changetrustpw\n"
262 " Change the machine trust password\n");
263 return 0;
266 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
267 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
268 rpc_changetrustpw_internals,
269 argc, argv);
273 * Join a domain, the old way.
275 * This uses 'machinename' as the inital password, and changes it.
277 * The password should be created with 'server manager' or equiv first.
279 * All parameters are provided by the run_rpc_command function, except for
280 * argc, argv which are passed through.
282 * @param domain_sid The domain sid acquired from the remote server.
283 * @param cli A cli_state connected to the server.
284 * @param mem_ctx Talloc context, destroyed on completion of the function.
285 * @param argc Standard main() style argc.
286 * @param argv Standard main() style argv. Initial components are already
287 * stripped.
289 * @return Normal NTSTATUS return.
292 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
293 const DOM_SID *domain_sid,
294 const char *domain_name,
295 struct cli_state *cli,
296 struct rpc_pipe_client *pipe_hnd,
297 TALLOC_CTX *mem_ctx,
298 int argc,
299 const char **argv)
302 fstring trust_passwd;
303 unsigned char orig_trust_passwd_hash[16];
304 NTSTATUS result;
305 uint32 sec_channel_type;
307 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
308 &pipe_hnd);
309 if (!NT_STATUS_IS_OK(result)) {
310 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
311 "error was %s\n",
312 cli->desthost,
313 nt_errstr(result) ));
314 return result;
318 check what type of join - if the user want's to join as
319 a BDC, the server must agree that we are a BDC.
321 if (argc >= 0) {
322 sec_channel_type = get_sec_channel_type(argv[0]);
323 } else {
324 sec_channel_type = get_sec_channel_type(NULL);
327 fstrcpy(trust_passwd, global_myname());
328 strlower_m(trust_passwd);
331 * Machine names can be 15 characters, but the max length on
332 * a password is 14. --jerry
335 trust_passwd[14] = '\0';
337 E_md4hash(trust_passwd, orig_trust_passwd_hash);
339 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
340 orig_trust_passwd_hash,
341 sec_channel_type);
343 if (NT_STATUS_IS_OK(result))
344 printf("Joined domain %s.\n", c->opt_target_workgroup);
347 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
348 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
349 result = NT_STATUS_UNSUCCESSFUL;
352 return result;
356 * Join a domain, the old way.
358 * @param argc Standard main() style argc.
359 * @param argv Standard main() style argv. Initial components are already
360 * stripped.
362 * @return A shell status integer (0 for success).
365 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
367 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
368 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
369 rpc_oldjoin_internals,
370 argc, argv);
374 * Join a domain, the old way. This function exists to allow
375 * the message to be displayed when oldjoin was explicitly
376 * requested, but not when it was implied by "net rpc join".
378 * @param argc Standard main() style argc.
379 * @param argv Standard main() style argv. Initial components are already
380 * stripped.
382 * @return A shell status integer (0 for success).
385 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
387 int rc = -1;
389 if (c->display_usage) {
390 d_printf("Usage:\n"
391 "net rpc oldjoin\n"
392 " Join a domain the old way\n");
393 return 0;
396 rc = net_rpc_perform_oldjoin(c, argc, argv);
398 if (rc) {
399 d_fprintf(stderr, "Failed to join domain\n");
402 return rc;
406 * 'net rpc join' entrypoint.
407 * @param argc Standard main() style argc.
408 * @param argv Standard main() style argv. Initial components are already
409 * stripped
411 * Main 'net_rpc_join()' (where the admin username/password is used) is
412 * in net_rpc_join.c.
413 * Try to just change the password, but if that doesn't work, use/prompt
414 * for a username/password.
417 int net_rpc_join(struct net_context *c, int argc, const char **argv)
419 if (c->display_usage) {
420 d_printf("Usage:\n"
421 "net rpc join -U <username>[%%password] <type>\n"
422 " Join a domain\n"
423 " username\tName of the admin user"
424 " password\tPassword of the admin user, will "
425 "prompt if not specified\n"
426 " type\tCan be one of the following:\n"
427 "\t\tMEMBER\tJoin as member server (default)\n"
428 "\t\tBDC\tJoin as BDC\n"
429 "\t\tPDC\tJoin as PDC\n");
430 return 0;
433 if (lp_server_role() == ROLE_STANDALONE) {
434 d_printf("cannot join as standalone machine\n");
435 return -1;
438 if (strlen(global_myname()) > 15) {
439 d_printf("Our netbios name can be at most 15 chars long, "
440 "\"%s\" is %u chars long\n",
441 global_myname(), (unsigned int)strlen(global_myname()));
442 return -1;
445 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
446 return 0;
448 return net_rpc_join_newstyle(c, argc, argv);
452 * display info about a rpc domain
454 * All parameters are provided by the run_rpc_command function, except for
455 * argc, argv which are passed through.
457 * @param domain_sid The domain sid acquired from the remote server
458 * @param cli A cli_state connected to the server.
459 * @param mem_ctx Talloc context, destroyed on completion of the function.
460 * @param argc Standard main() style argc.
461 * @param argv Standard main() style argv. Initial components are already
462 * stripped.
464 * @return Normal NTSTATUS return.
467 NTSTATUS rpc_info_internals(struct net_context *c,
468 const DOM_SID *domain_sid,
469 const char *domain_name,
470 struct cli_state *cli,
471 struct rpc_pipe_client *pipe_hnd,
472 TALLOC_CTX *mem_ctx,
473 int argc,
474 const char **argv)
476 POLICY_HND connect_pol, domain_pol;
477 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
478 union samr_DomainInfo *info = NULL;
479 fstring sid_str;
481 sid_to_fstring(sid_str, domain_sid);
483 /* Get sam policy handle */
484 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
485 pipe_hnd->desthost,
486 MAXIMUM_ALLOWED_ACCESS,
487 &connect_pol);
488 if (!NT_STATUS_IS_OK(result)) {
489 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
490 goto done;
493 /* Get domain policy handle */
494 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
495 &connect_pol,
496 MAXIMUM_ALLOWED_ACCESS,
497 CONST_DISCARD(struct dom_sid2 *, domain_sid),
498 &domain_pol);
499 if (!NT_STATUS_IS_OK(result)) {
500 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
501 goto done;
504 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
505 &domain_pol,
507 &info);
508 if (NT_STATUS_IS_OK(result)) {
509 d_printf("Domain Name: %s\n", info->info2.domain_name.string);
510 d_printf("Domain SID: %s\n", sid_str);
511 d_printf("Sequence number: %llu\n",
512 (unsigned long long)info->info2.sequence_num);
513 d_printf("Num users: %u\n", info->info2.num_users);
514 d_printf("Num domain groups: %u\n", info->info2.num_groups);
515 d_printf("Num local groups: %u\n", info->info2.num_aliases);
518 done:
519 return result;
523 * 'net rpc info' entrypoint.
524 * @param argc Standard main() style argc.
525 * @param argv Standard main() style argv. Initial components are already
526 * stripped.
529 int net_rpc_info(struct net_context *c, int argc, const char **argv)
531 if (c->display_usage) {
532 d_printf("Usage:\n"
533 "net rpc info\n"
534 " Display information about the domain\n");
535 return 0;
538 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
539 NET_FLAGS_PDC, rpc_info_internals,
540 argc, argv);
544 * Fetch domain SID into the local secrets.tdb.
546 * All parameters are provided by the run_rpc_command function, except for
547 * argc, argv which are passed through.
549 * @param domain_sid The domain sid acquired from the remote server.
550 * @param cli A cli_state connected to the server.
551 * @param mem_ctx Talloc context, destroyed on completion of the function.
552 * @param argc Standard main() style argc.
553 * @param argv Standard main() style argv. Initial components are already
554 * stripped.
556 * @return Normal NTSTATUS return.
559 static NTSTATUS rpc_getsid_internals(struct net_context *c,
560 const DOM_SID *domain_sid,
561 const char *domain_name,
562 struct cli_state *cli,
563 struct rpc_pipe_client *pipe_hnd,
564 TALLOC_CTX *mem_ctx,
565 int argc,
566 const char **argv)
568 fstring sid_str;
570 sid_to_fstring(sid_str, domain_sid);
571 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
572 sid_str, domain_name);
574 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
575 DEBUG(0,("Can't store domain SID\n"));
576 return NT_STATUS_UNSUCCESSFUL;
579 return NT_STATUS_OK;
583 * 'net rpc getsid' entrypoint.
584 * @param argc Standard main() style argc.
585 * @param argv Standard main() style argv. Initial components are already
586 * stripped.
589 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
591 if (c->display_usage) {
592 d_printf("Usage:\n"
593 "net rpc getsid\n"
594 " Fetch domain SID into local secrets.tdb\n");
595 return 0;
598 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
599 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
600 rpc_getsid_internals,
601 argc, argv);
604 /****************************************************************************/
607 * Basic usage function for 'net rpc user'.
608 * @param argc Standard main() style argc.
609 * @param argv Standard main() style argv. Initial components are already
610 * stripped.
613 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
615 return net_user_usage(c, argc, argv);
619 * Add a new user to a remote RPC server.
621 * @param argc Standard main() style argc.
622 * @param argv Standard main() style argv. Initial components are already
623 * stripped.
625 * @return A shell status integer (0 for success).
628 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
630 NET_API_STATUS status;
631 struct USER_INFO_1 info1;
632 uint32_t parm_error = 0;
634 if (argc < 1 || c->display_usage) {
635 rpc_user_usage(c, argc, argv);
636 return 0;
639 ZERO_STRUCT(info1);
641 info1.usri1_name = argv[0];
642 if (argc == 2) {
643 info1.usri1_password = argv[1];
646 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
648 if (status != 0) {
649 d_fprintf(stderr, "Failed to add user '%s' with: %s.\n",
650 argv[0], libnetapi_get_error_string(c->netapi_ctx,
651 status));
652 return -1;
653 } else {
654 d_printf("Added user '%s'.\n", argv[0]);
657 return 0;
661 * Rename a user on a remote RPC server.
663 * @param argc Standard main() style argc.
664 * @param argv Standard main() style argv. Initial components are already
665 * stripped.
667 * @return A shell status integer (0 for success).
670 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
672 NET_API_STATUS status;
673 struct USER_INFO_0 u0;
674 uint32_t parm_err = 0;
676 if (argc != 2 || c->display_usage) {
677 rpc_user_usage(c, argc, argv);
678 return 0;
681 u0.usri0_name = argv[1];
683 status = NetUserSetInfo(c->opt_host, argv[0],
684 0, (uint8_t *)&u0, &parm_err);
685 if (status) {
686 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n",
687 argv[0], argv[1],
688 libnetapi_get_error_string(c->netapi_ctx, status));
689 } else {
690 d_printf("Renamed user from %s to %s\n", argv[0], argv[1]);
693 return status;
697 * Delete a user from a remote RPC server.
699 * @param argc Standard main() style argc.
700 * @param argv Standard main() style argv. Initial components are already
701 * stripped.
703 * @return A shell status integer (0 for success).
706 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
708 NET_API_STATUS status;
710 if (argc < 1 || c->display_usage) {
711 rpc_user_usage(c, argc, argv);
712 return 0;
715 status = NetUserDel(c->opt_host, argv[0]);
717 if (status != 0) {
718 d_fprintf(stderr, "Failed to delete user '%s' with: %s.\n",
719 argv[0],
720 libnetapi_get_error_string(c->netapi_ctx, status));
721 return -1;
722 } else {
723 d_printf("Deleted user '%s'.\n", argv[0]);
726 return 0;
730 * Set a user's password on a remote RPC server.
732 * @param argc Standard main() style argc.
733 * @param argv Standard main() style argv. Initial components are already
734 * stripped.
736 * @return A shell status integer (0 for success).
739 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
741 NET_API_STATUS status;
742 char *prompt = NULL;
743 struct USER_INFO_1003 u1003;
744 uint32_t parm_err = 0;
746 if (argc < 1 || c->display_usage) {
747 rpc_user_usage(c, argc, argv);
748 return 0;
751 if (argv[1]) {
752 u1003.usri1003_password = argv[1];
753 } else {
754 asprintf(&prompt, "Enter new password for %s:", argv[0]);
755 u1003.usri1003_password = getpass(prompt);
756 SAFE_FREE(prompt);
759 status = NetUserSetInfo(c->opt_host, argv[0], 1003, (uint8_t *)&u1003, &parm_err);
761 /* Display results */
762 if (status != 0) {
763 d_fprintf(stderr, "Failed to set password for '%s' with: %s.\n",
764 argv[0], libnetapi_get_error_string(c->netapi_ctx,
765 status));
766 return -1;
769 return 0;
773 * List a user's groups from a remote RPC server.
775 * @param argc Standard main() style argc.
776 * @param argv Standard main() style argv. Initial components are already
777 * stripped.
779 * @return A shell status integer (0 for success)
782 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
785 NET_API_STATUS status;
786 struct GROUP_USERS_INFO_0 *u0 = NULL;
787 uint32_t entries_read = 0;
788 uint32_t total_entries = 0;
789 int i;
792 if (argc < 1 || c->display_usage) {
793 rpc_user_usage(c, argc, argv);
794 return 0;
797 status = NetUserGetGroups(c->opt_host,
798 argv[0],
800 (uint8_t **)&u0,
801 (uint32_t)-1,
802 &entries_read,
803 &total_entries);
804 if (status != 0) {
805 d_fprintf(stderr, "Failed to get groups for '%s' with: %s.\n",
806 argv[0], libnetapi_get_error_string(c->netapi_ctx,
807 status));
808 return -1;
811 for (i=0; i < entries_read; i++) {
812 printf("%s\n", u0->grui0_name);
813 u0++;
816 return 0;
820 * List users on a remote RPC server.
822 * All parameters are provided by the run_rpc_command function, except for
823 * argc, argv which are passed through.
825 * @param domain_sid The domain sid acquired from the remote server.
826 * @param cli A cli_state connected to the server.
827 * @param mem_ctx Talloc context, destroyed on completion of the function.
828 * @param argc Standard main() style argc.
829 * @param argv Standard main() style argv. Initial components are already
830 * stripped.
832 * @return Normal NTSTATUS return.
835 static int rpc_user_list(struct net_context *c, int argc, const char **argv)
837 NET_API_STATUS status;
838 uint32_t start_idx=0, num_entries, i, loop_count = 0;
839 struct NET_DISPLAY_USER *info = NULL;
840 void *buffer = NULL;
842 /* Query domain users */
843 if (c->opt_long_list_entries)
844 d_printf("\nUser name Comment"
845 "\n-----------------------------\n");
846 do {
847 uint32_t max_entries, max_size;
849 get_query_dispinfo_params(
850 loop_count, &max_entries, &max_size);
852 status = NetQueryDisplayInformation(c->opt_host,
854 start_idx,
855 max_entries,
856 max_size,
857 &num_entries,
858 &buffer);
859 if (status != 0 && status != ERROR_MORE_DATA) {
860 return status;
863 info = (struct NET_DISPLAY_USER *)buffer;
865 for (i = 0; i < num_entries; i++) {
867 if (c->opt_long_list_entries)
868 printf("%-21.21s %s\n", info->usri1_name,
869 info->usri1_comment);
870 else
871 printf("%s\n", info->usri1_name);
872 info++;
875 NetApiBufferFree(buffer);
877 loop_count++;
878 start_idx += num_entries;
880 } while (status == ERROR_MORE_DATA);
882 return status;
886 * 'net rpc user' entrypoint.
887 * @param argc Standard main() style argc.
888 * @param argv Standard main() style argv. Initial components are already
889 * stripped.
892 int net_rpc_user(struct net_context *c, int argc, const char **argv)
894 NET_API_STATUS status;
896 struct functable func[] = {
898 "add",
899 rpc_user_add,
900 NET_TRANSPORT_RPC,
901 "Add specified user",
902 "net rpc user add\n"
903 " Add specified user"
906 "info",
907 rpc_user_info,
908 NET_TRANSPORT_RPC,
909 "List domain groups of user",
910 "net rpc user info\n"
911 " Lis domain groups of user"
914 "delete",
915 rpc_user_delete,
916 NET_TRANSPORT_RPC,
917 "Remove specified user",
918 "net rpc user delete\n"
919 " Remove specified user"
922 "password",
923 rpc_user_password,
924 NET_TRANSPORT_RPC,
925 "Change user password",
926 "net rpc user password\n"
927 " Change user password"
930 "rename",
931 rpc_user_rename,
932 NET_TRANSPORT_RPC,
933 "Rename specified user",
934 "net rpc user rename\n"
935 " Rename specified user"
937 {NULL, NULL, 0, NULL, NULL}
940 status = libnetapi_init(&c->netapi_ctx);
941 if (status != 0) {
942 return -1;
944 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
945 libnetapi_set_password(c->netapi_ctx, c->opt_password);
946 if (c->opt_kerberos) {
947 libnetapi_set_use_kerberos(c->netapi_ctx);
950 if (argc == 0) {
951 if (c->display_usage) {
952 d_printf("Usage:\n");
953 d_printf("net rpc user\n"
954 " List all users\n");
955 net_display_usage_from_functable(func);
956 return 0;
959 return rpc_user_list(c, argc, argv);
962 return net_run_function(c, argc, argv, "net rpc user", func);
965 static NTSTATUS rpc_sh_user_list(struct net_context *c,
966 TALLOC_CTX *mem_ctx,
967 struct rpc_sh_ctx *ctx,
968 struct rpc_pipe_client *pipe_hnd,
969 int argc, const char **argv)
971 return werror_to_ntstatus(W_ERROR(rpc_user_list(c, argc, argv)));
974 static NTSTATUS rpc_sh_user_info(struct net_context *c,
975 TALLOC_CTX *mem_ctx,
976 struct rpc_sh_ctx *ctx,
977 struct rpc_pipe_client *pipe_hnd,
978 int argc, const char **argv)
980 return werror_to_ntstatus(W_ERROR(rpc_user_info(c, argc, argv)));
983 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
984 TALLOC_CTX *mem_ctx,
985 struct rpc_sh_ctx *ctx,
986 struct rpc_pipe_client *pipe_hnd,
987 int argc, const char **argv,
988 NTSTATUS (*fn)(
989 struct net_context *c,
990 TALLOC_CTX *mem_ctx,
991 struct rpc_sh_ctx *ctx,
992 struct rpc_pipe_client *pipe_hnd,
993 POLICY_HND *user_hnd,
994 int argc, const char **argv))
996 POLICY_HND connect_pol, domain_pol, user_pol;
997 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
998 DOM_SID sid;
999 uint32 rid;
1000 enum lsa_SidType type;
1002 if (argc == 0) {
1003 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1004 return NT_STATUS_INVALID_PARAMETER;
1007 ZERO_STRUCT(connect_pol);
1008 ZERO_STRUCT(domain_pol);
1009 ZERO_STRUCT(user_pol);
1011 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1012 argv[0], NULL, NULL, &sid, &type);
1013 if (!NT_STATUS_IS_OK(result)) {
1014 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1015 nt_errstr(result));
1016 goto done;
1019 if (type != SID_NAME_USER) {
1020 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1021 sid_type_lookup(type));
1022 result = NT_STATUS_NO_SUCH_USER;
1023 goto done;
1026 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1027 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1028 result = NT_STATUS_NO_SUCH_USER;
1029 goto done;
1032 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1033 pipe_hnd->desthost,
1034 MAXIMUM_ALLOWED_ACCESS,
1035 &connect_pol);
1036 if (!NT_STATUS_IS_OK(result)) {
1037 goto done;
1040 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1041 &connect_pol,
1042 MAXIMUM_ALLOWED_ACCESS,
1043 ctx->domain_sid,
1044 &domain_pol);
1045 if (!NT_STATUS_IS_OK(result)) {
1046 goto done;
1049 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1050 &domain_pol,
1051 MAXIMUM_ALLOWED_ACCESS,
1052 rid,
1053 &user_pol);
1054 if (!NT_STATUS_IS_OK(result)) {
1055 goto done;
1058 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1060 done:
1061 if (is_valid_policy_hnd(&user_pol)) {
1062 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1064 if (is_valid_policy_hnd(&domain_pol)) {
1065 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1067 if (is_valid_policy_hnd(&connect_pol)) {
1068 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1070 return result;
1073 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1074 TALLOC_CTX *mem_ctx,
1075 struct rpc_sh_ctx *ctx,
1076 struct rpc_pipe_client *pipe_hnd,
1077 POLICY_HND *user_hnd,
1078 int argc, const char **argv)
1080 NTSTATUS result;
1081 union samr_UserInfo *info = NULL;
1083 if (argc != 0) {
1084 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1085 return NT_STATUS_INVALID_PARAMETER;
1088 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1089 user_hnd,
1091 &info);
1092 if (!NT_STATUS_IS_OK(result)) {
1093 return result;
1096 d_printf("user rid: %d, group rid: %d\n",
1097 info->info21.rid,
1098 info->info21.primary_gid);
1100 return result;
1103 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1104 TALLOC_CTX *mem_ctx,
1105 struct rpc_sh_ctx *ctx,
1106 struct rpc_pipe_client *pipe_hnd,
1107 int argc, const char **argv)
1109 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1110 rpc_sh_user_show_internals);
1113 #define FETCHSTR(name, rec) \
1114 do { if (strequal(ctx->thiscmd, name)) { \
1115 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1116 } while (0);
1118 #define SETSTR(name, rec, flag) \
1119 do { if (strequal(ctx->thiscmd, name)) { \
1120 init_lsa_String(&(info->info21.rec), argv[0]); \
1121 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1122 } while (0);
1124 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1125 TALLOC_CTX *mem_ctx,
1126 struct rpc_sh_ctx *ctx,
1127 struct rpc_pipe_client *pipe_hnd,
1128 POLICY_HND *user_hnd,
1129 int argc, const char **argv)
1131 NTSTATUS result;
1132 const char *username;
1133 const char *oldval = "";
1134 union samr_UserInfo *info = NULL;
1136 if (argc > 1) {
1137 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1138 ctx->whoami);
1139 return NT_STATUS_INVALID_PARAMETER;
1142 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1143 user_hnd,
1145 &info);
1146 if (!NT_STATUS_IS_OK(result)) {
1147 return result;
1150 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1152 FETCHSTR("fullname", full_name);
1153 FETCHSTR("homedir", home_directory);
1154 FETCHSTR("homedrive", home_drive);
1155 FETCHSTR("logonscript", logon_script);
1156 FETCHSTR("profilepath", profile_path);
1157 FETCHSTR("description", description);
1159 if (argc == 0) {
1160 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1161 goto done;
1164 if (strcmp(argv[0], "NULL") == 0) {
1165 argv[0] = "";
1168 ZERO_STRUCT(info->info21);
1170 SETSTR("fullname", full_name, FULL_NAME);
1171 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1172 SETSTR("homedrive", home_drive, HOME_DRIVE);
1173 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1174 SETSTR("profilepath", profile_path, PROFILE_PATH);
1175 SETSTR("description", description, DESCRIPTION);
1177 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1178 user_hnd,
1180 info);
1182 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1183 ctx->thiscmd, oldval, argv[0]);
1185 done:
1187 return result;
1190 #define HANDLEFLG(name, rec) \
1191 do { if (strequal(ctx->thiscmd, name)) { \
1192 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1193 if (newval) { \
1194 newflags = oldflags | ACB_##rec; \
1195 } else { \
1196 newflags = oldflags & ~ACB_##rec; \
1197 } } } while (0);
1199 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1200 TALLOC_CTX *mem_ctx,
1201 struct rpc_sh_ctx *ctx,
1202 struct rpc_pipe_client *pipe_hnd,
1203 int argc, const char **argv)
1205 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1206 rpc_sh_user_str_edit_internals);
1209 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1210 TALLOC_CTX *mem_ctx,
1211 struct rpc_sh_ctx *ctx,
1212 struct rpc_pipe_client *pipe_hnd,
1213 POLICY_HND *user_hnd,
1214 int argc, const char **argv)
1216 NTSTATUS result;
1217 const char *username;
1218 const char *oldval = "unknown";
1219 uint32 oldflags, newflags;
1220 bool newval;
1221 union samr_UserInfo *info = NULL;
1223 if ((argc > 1) ||
1224 ((argc == 1) && !strequal(argv[0], "yes") &&
1225 !strequal(argv[0], "no"))) {
1226 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1227 ctx->whoami);
1228 return NT_STATUS_INVALID_PARAMETER;
1231 newval = strequal(argv[0], "yes");
1233 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1234 user_hnd,
1236 &info);
1237 if (!NT_STATUS_IS_OK(result)) {
1238 return result;
1241 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1242 oldflags = info->info21.acct_flags;
1243 newflags = info->info21.acct_flags;
1245 HANDLEFLG("disabled", DISABLED);
1246 HANDLEFLG("pwnotreq", PWNOTREQ);
1247 HANDLEFLG("autolock", AUTOLOCK);
1248 HANDLEFLG("pwnoexp", PWNOEXP);
1250 if (argc == 0) {
1251 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1252 goto done;
1255 ZERO_STRUCT(info->info21);
1257 info->info21.acct_flags = newflags;
1258 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1260 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1261 user_hnd,
1263 info);
1265 if (NT_STATUS_IS_OK(result)) {
1266 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1267 ctx->thiscmd, oldval, argv[0]);
1270 done:
1272 return result;
1275 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1276 TALLOC_CTX *mem_ctx,
1277 struct rpc_sh_ctx *ctx,
1278 struct rpc_pipe_client *pipe_hnd,
1279 int argc, const char **argv)
1281 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1282 rpc_sh_user_flag_edit_internals);
1285 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1286 TALLOC_CTX *mem_ctx,
1287 struct rpc_sh_ctx *ctx)
1289 static struct rpc_sh_cmd cmds[] = {
1291 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1292 "Show/Set a user's full name" },
1294 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1295 "Show/Set a user's home directory" },
1297 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1298 "Show/Set a user's home drive" },
1300 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1301 "Show/Set a user's logon script" },
1303 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1304 "Show/Set a user's profile path" },
1306 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1307 "Show/Set a user's description" },
1309 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1310 "Show/Set whether a user is disabled" },
1312 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1313 "Show/Set whether a user locked out" },
1315 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1316 "Show/Set whether a user does not need a password" },
1318 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1319 "Show/Set whether a user's password does not expire" },
1321 { NULL, NULL, 0, NULL, NULL }
1324 return cmds;
1327 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1328 TALLOC_CTX *mem_ctx,
1329 struct rpc_sh_ctx *ctx)
1331 static struct rpc_sh_cmd cmds[] = {
1333 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1334 "List available users" },
1336 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1337 "List the domain groups a user is member of" },
1339 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1340 "Show info about a user" },
1342 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1343 "Show/Modify a user's fields" },
1345 { NULL, NULL, 0, NULL, NULL }
1348 return cmds;
1351 /****************************************************************************/
1354 * Basic usage function for 'net rpc group'.
1355 * @param argc Standard main() style argc.
1356 * @param argv Standard main() style argv. Initial components are already
1357 * stripped.
1360 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1362 return net_group_usage(c, argc, argv);
1366 * Delete group on a remote RPC server.
1368 * All parameters are provided by the run_rpc_command function, except for
1369 * argc, argv which are passed through.
1371 * @param domain_sid The domain sid acquired from the remote server.
1372 * @param cli A cli_state connected to the server.
1373 * @param mem_ctx Talloc context, destroyed on completion of the function.
1374 * @param argc Standard main() style argc.
1375 * @param argv Standard main() style argv. Initial components are already
1376 * stripped.
1378 * @return Normal NTSTATUS return.
1381 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1382 const DOM_SID *domain_sid,
1383 const char *domain_name,
1384 struct cli_state *cli,
1385 struct rpc_pipe_client *pipe_hnd,
1386 TALLOC_CTX *mem_ctx,
1387 int argc,
1388 const char **argv)
1390 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1391 bool group_is_primary = false;
1392 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1393 uint32_t group_rid;
1394 struct samr_RidTypeArray *rids = NULL;
1395 /* char **names; */
1396 int i;
1397 /* DOM_GID *user_gids; */
1399 struct samr_Ids group_rids, name_types;
1400 struct lsa_String lsa_acct_name;
1401 union samr_UserInfo *info = NULL;
1403 if (argc < 1 || c->display_usage) {
1404 rpc_group_usage(c, argc,argv);
1405 return NT_STATUS_OK; /* ok? */
1408 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1409 pipe_hnd->desthost,
1410 MAXIMUM_ALLOWED_ACCESS,
1411 &connect_pol);
1413 if (!NT_STATUS_IS_OK(result)) {
1414 d_fprintf(stderr, "Request samr_Connect2 failed\n");
1415 goto done;
1418 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1419 &connect_pol,
1420 MAXIMUM_ALLOWED_ACCESS,
1421 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1422 &domain_pol);
1424 if (!NT_STATUS_IS_OK(result)) {
1425 d_fprintf(stderr, "Request open_domain failed\n");
1426 goto done;
1429 init_lsa_String(&lsa_acct_name, argv[0]);
1431 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1432 &domain_pol,
1434 &lsa_acct_name,
1435 &group_rids,
1436 &name_types);
1437 if (!NT_STATUS_IS_OK(result)) {
1438 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1439 goto done;
1442 switch (name_types.ids[0])
1444 case SID_NAME_DOM_GRP:
1445 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1446 &domain_pol,
1447 MAXIMUM_ALLOWED_ACCESS,
1448 group_rids.ids[0],
1449 &group_pol);
1450 if (!NT_STATUS_IS_OK(result)) {
1451 d_fprintf(stderr, "Request open_group failed");
1452 goto done;
1455 group_rid = group_rids.ids[0];
1457 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1458 &group_pol,
1459 &rids);
1461 if (!NT_STATUS_IS_OK(result)) {
1462 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1463 goto done;
1466 if (c->opt_verbose) {
1467 d_printf("Domain Group %s (rid: %d) has %d members\n",
1468 argv[0],group_rid, rids->count);
1471 /* Check if group is anyone's primary group */
1472 for (i = 0; i < rids->count; i++)
1474 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1475 &domain_pol,
1476 MAXIMUM_ALLOWED_ACCESS,
1477 rids->rids[i],
1478 &user_pol);
1480 if (!NT_STATUS_IS_OK(result)) {
1481 d_fprintf(stderr, "Unable to open group member %d\n",
1482 rids->rids[i]);
1483 goto done;
1486 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1487 &user_pol,
1489 &info);
1491 if (!NT_STATUS_IS_OK(result)) {
1492 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",
1493 rids->rids[i]);
1494 goto done;
1497 if (info->info21.primary_gid == group_rid) {
1498 if (c->opt_verbose) {
1499 d_printf("Group is primary group of %s\n",
1500 info->info21.account_name.string);
1502 group_is_primary = true;
1505 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1508 if (group_is_primary) {
1509 d_fprintf(stderr, "Unable to delete group because some "
1510 "of it's members have it as primary group\n");
1511 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1512 goto done;
1515 /* remove all group members */
1516 for (i = 0; i < rids->count; i++)
1518 if (c->opt_verbose)
1519 d_printf("Remove group member %d...",
1520 rids->rids[i]);
1521 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1522 &group_pol,
1523 rids->rids[i]);
1525 if (NT_STATUS_IS_OK(result)) {
1526 if (c->opt_verbose)
1527 d_printf("ok\n");
1528 } else {
1529 if (c->opt_verbose)
1530 d_printf("failed\n");
1531 goto done;
1535 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1536 &group_pol);
1538 break;
1539 /* removing a local group is easier... */
1540 case SID_NAME_ALIAS:
1541 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1542 &domain_pol,
1543 MAXIMUM_ALLOWED_ACCESS,
1544 group_rids.ids[0],
1545 &group_pol);
1547 if (!NT_STATUS_IS_OK(result)) {
1548 d_fprintf(stderr, "Request open_alias failed\n");
1549 goto done;
1552 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1553 &group_pol);
1554 break;
1555 default:
1556 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1557 argv[0],sid_type_lookup(name_types.ids[0]));
1558 result = NT_STATUS_UNSUCCESSFUL;
1559 goto done;
1562 if (NT_STATUS_IS_OK(result)) {
1563 if (c->opt_verbose)
1564 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types.ids[0]),argv[0]);
1565 } else {
1566 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1567 get_friendly_nt_error_msg(result));
1570 done:
1571 return result;
1575 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1577 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1578 rpc_group_delete_internals, argc,argv);
1581 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1583 NET_API_STATUS status;
1584 struct GROUP_INFO_1 info1;
1585 uint32_t parm_error = 0;
1587 if (argc != 1 || c->display_usage) {
1588 rpc_group_usage(c, argc, argv);
1589 return 0;
1592 ZERO_STRUCT(info1);
1594 info1.grpi1_name = argv[0];
1595 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1596 info1.grpi1_comment = c->opt_comment;
1599 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1601 if (status != 0) {
1602 d_fprintf(stderr, "Failed to add group '%s' with: %s.\n",
1603 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1604 status));
1605 return -1;
1606 } else {
1607 d_printf("Added group '%s'.\n", argv[0]);
1610 return 0;
1613 static int rpc_alias_add_internals(struct net_context *c, int argc, const char **argv)
1615 NET_API_STATUS status;
1616 struct LOCALGROUP_INFO_1 info1;
1617 uint32_t parm_error = 0;
1619 if (argc != 1 || c->display_usage) {
1620 rpc_group_usage(c, argc, argv);
1621 return 0;
1624 ZERO_STRUCT(info1);
1626 info1.lgrpi1_name = argv[0];
1627 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1628 info1.lgrpi1_comment = c->opt_comment;
1631 status = NetLocalGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1633 if (status != 0) {
1634 d_fprintf(stderr, "Failed to add alias '%s' with: %s.\n",
1635 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1636 status));
1637 return -1;
1638 } else {
1639 d_printf("Added alias '%s'.\n", argv[0]);
1642 return 0;
1645 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1647 if (c->opt_localgroup)
1648 return rpc_alias_add_internals(c, argc, argv);
1650 return rpc_group_add_internals(c, argc, argv);
1653 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1654 TALLOC_CTX *mem_ctx,
1655 const char *name,
1656 DOM_SID *sid,
1657 enum lsa_SidType *type)
1659 DOM_SID *sids = NULL;
1660 enum lsa_SidType *types = NULL;
1661 struct rpc_pipe_client *pipe_hnd;
1662 POLICY_HND lsa_pol;
1663 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1665 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
1666 &pipe_hnd);
1667 if (!NT_STATUS_IS_OK(result)) {
1668 goto done;
1671 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
1672 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
1674 if (!NT_STATUS_IS_OK(result)) {
1675 goto done;
1678 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
1679 &name, NULL, 1, &sids, &types);
1681 if (NT_STATUS_IS_OK(result)) {
1682 sid_copy(sid, &sids[0]);
1683 *type = types[0];
1686 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
1688 done:
1689 if (pipe_hnd) {
1690 TALLOC_FREE(pipe_hnd);
1693 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
1695 /* Try as S-1-5-whatever */
1697 DOM_SID tmp_sid;
1699 if (string_to_sid(&tmp_sid, name)) {
1700 sid_copy(sid, &tmp_sid);
1701 *type = SID_NAME_UNKNOWN;
1702 result = NT_STATUS_OK;
1706 return result;
1709 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
1710 TALLOC_CTX *mem_ctx,
1711 const DOM_SID *group_sid,
1712 const char *member)
1714 POLICY_HND connect_pol, domain_pol;
1715 NTSTATUS result;
1716 uint32 group_rid;
1717 POLICY_HND group_pol;
1719 struct samr_Ids rids, rid_types;
1720 struct lsa_String lsa_acct_name;
1722 DOM_SID sid;
1724 sid_copy(&sid, group_sid);
1726 if (!sid_split_rid(&sid, &group_rid)) {
1727 return NT_STATUS_UNSUCCESSFUL;
1730 /* Get sam policy handle */
1731 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1732 pipe_hnd->desthost,
1733 MAXIMUM_ALLOWED_ACCESS,
1734 &connect_pol);
1735 if (!NT_STATUS_IS_OK(result)) {
1736 return result;
1739 /* Get domain policy handle */
1740 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1741 &connect_pol,
1742 MAXIMUM_ALLOWED_ACCESS,
1743 &sid,
1744 &domain_pol);
1745 if (!NT_STATUS_IS_OK(result)) {
1746 return result;
1749 init_lsa_String(&lsa_acct_name, member);
1751 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1752 &domain_pol,
1754 &lsa_acct_name,
1755 &rids,
1756 &rid_types);
1758 if (!NT_STATUS_IS_OK(result)) {
1759 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1760 goto done;
1763 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1764 &domain_pol,
1765 MAXIMUM_ALLOWED_ACCESS,
1766 group_rid,
1767 &group_pol);
1769 if (!NT_STATUS_IS_OK(result)) {
1770 goto done;
1773 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
1774 &group_pol,
1775 rids.ids[0],
1776 0x0005); /* unknown flags */
1778 done:
1779 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1780 return result;
1783 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
1784 TALLOC_CTX *mem_ctx,
1785 const DOM_SID *alias_sid,
1786 const char *member)
1788 POLICY_HND connect_pol, domain_pol;
1789 NTSTATUS result;
1790 uint32 alias_rid;
1791 POLICY_HND alias_pol;
1793 DOM_SID member_sid;
1794 enum lsa_SidType member_type;
1796 DOM_SID sid;
1798 sid_copy(&sid, alias_sid);
1800 if (!sid_split_rid(&sid, &alias_rid)) {
1801 return NT_STATUS_UNSUCCESSFUL;
1804 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
1805 member, &member_sid, &member_type);
1807 if (!NT_STATUS_IS_OK(result)) {
1808 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1809 return result;
1812 /* Get sam policy handle */
1813 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1814 pipe_hnd->desthost,
1815 MAXIMUM_ALLOWED_ACCESS,
1816 &connect_pol);
1817 if (!NT_STATUS_IS_OK(result)) {
1818 goto done;
1821 /* Get domain policy handle */
1822 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1823 &connect_pol,
1824 MAXIMUM_ALLOWED_ACCESS,
1825 &sid,
1826 &domain_pol);
1827 if (!NT_STATUS_IS_OK(result)) {
1828 goto done;
1831 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1832 &domain_pol,
1833 MAXIMUM_ALLOWED_ACCESS,
1834 alias_rid,
1835 &alias_pol);
1837 if (!NT_STATUS_IS_OK(result)) {
1838 return result;
1841 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
1842 &alias_pol,
1843 &member_sid);
1845 if (!NT_STATUS_IS_OK(result)) {
1846 return result;
1849 done:
1850 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1851 return result;
1854 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
1855 const DOM_SID *domain_sid,
1856 const char *domain_name,
1857 struct cli_state *cli,
1858 struct rpc_pipe_client *pipe_hnd,
1859 TALLOC_CTX *mem_ctx,
1860 int argc,
1861 const char **argv)
1863 DOM_SID group_sid;
1864 enum lsa_SidType group_type;
1866 if (argc != 2 || c->display_usage) {
1867 d_printf("Usage:\n"
1868 "net rpc group addmem <group> <member>\n"
1869 " Add a member to a group\n"
1870 " group\tGroup to add member to\n"
1871 " member\tMember to add to group\n");
1872 return NT_STATUS_UNSUCCESSFUL;
1875 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
1876 &group_sid, &group_type))) {
1877 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
1878 return NT_STATUS_UNSUCCESSFUL;
1881 if (group_type == SID_NAME_DOM_GRP) {
1882 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
1883 &group_sid, argv[1]);
1885 if (!NT_STATUS_IS_OK(result)) {
1886 d_fprintf(stderr, "Could not add %s to %s: %s\n",
1887 argv[1], argv[0], nt_errstr(result));
1889 return result;
1892 if (group_type == SID_NAME_ALIAS) {
1893 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
1894 &group_sid, argv[1]);
1896 if (!NT_STATUS_IS_OK(result)) {
1897 d_fprintf(stderr, "Could not add %s to %s: %s\n",
1898 argv[1], argv[0], nt_errstr(result));
1900 return result;
1903 d_fprintf(stderr, "Can only add members to global or local groups "
1904 "which %s is not\n", argv[0]);
1906 return NT_STATUS_UNSUCCESSFUL;
1909 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
1911 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1912 rpc_group_addmem_internals,
1913 argc, argv);
1916 static NTSTATUS rpc_del_groupmem(struct net_context *c,
1917 struct rpc_pipe_client *pipe_hnd,
1918 TALLOC_CTX *mem_ctx,
1919 const DOM_SID *group_sid,
1920 const char *member)
1922 POLICY_HND connect_pol, domain_pol;
1923 NTSTATUS result;
1924 uint32 group_rid;
1925 POLICY_HND group_pol;
1927 struct samr_Ids rids, rid_types;
1928 struct lsa_String lsa_acct_name;
1930 DOM_SID sid;
1932 sid_copy(&sid, group_sid);
1934 if (!sid_split_rid(&sid, &group_rid))
1935 return NT_STATUS_UNSUCCESSFUL;
1937 /* Get sam policy handle */
1938 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1939 pipe_hnd->desthost,
1940 MAXIMUM_ALLOWED_ACCESS,
1941 &connect_pol);
1942 if (!NT_STATUS_IS_OK(result))
1943 return result;
1945 /* Get domain policy handle */
1946 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1947 &connect_pol,
1948 MAXIMUM_ALLOWED_ACCESS,
1949 &sid,
1950 &domain_pol);
1951 if (!NT_STATUS_IS_OK(result))
1952 return result;
1954 init_lsa_String(&lsa_acct_name, member);
1956 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1957 &domain_pol,
1959 &lsa_acct_name,
1960 &rids,
1961 &rid_types);
1962 if (!NT_STATUS_IS_OK(result)) {
1963 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
1964 goto done;
1967 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1968 &domain_pol,
1969 MAXIMUM_ALLOWED_ACCESS,
1970 group_rid,
1971 &group_pol);
1973 if (!NT_STATUS_IS_OK(result))
1974 goto done;
1976 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1977 &group_pol,
1978 rids.ids[0]);
1980 done:
1981 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1982 return result;
1985 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
1986 TALLOC_CTX *mem_ctx,
1987 const DOM_SID *alias_sid,
1988 const char *member)
1990 POLICY_HND connect_pol, domain_pol;
1991 NTSTATUS result;
1992 uint32 alias_rid;
1993 POLICY_HND alias_pol;
1995 DOM_SID member_sid;
1996 enum lsa_SidType member_type;
1998 DOM_SID sid;
2000 sid_copy(&sid, alias_sid);
2002 if (!sid_split_rid(&sid, &alias_rid))
2003 return NT_STATUS_UNSUCCESSFUL;
2005 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2006 member, &member_sid, &member_type);
2008 if (!NT_STATUS_IS_OK(result)) {
2009 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2010 return result;
2013 /* Get sam policy handle */
2014 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2015 pipe_hnd->desthost,
2016 MAXIMUM_ALLOWED_ACCESS,
2017 &connect_pol);
2018 if (!NT_STATUS_IS_OK(result)) {
2019 goto done;
2022 /* Get domain policy handle */
2023 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2024 &connect_pol,
2025 MAXIMUM_ALLOWED_ACCESS,
2026 &sid,
2027 &domain_pol);
2028 if (!NT_STATUS_IS_OK(result)) {
2029 goto done;
2032 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2033 &domain_pol,
2034 MAXIMUM_ALLOWED_ACCESS,
2035 alias_rid,
2036 &alias_pol);
2038 if (!NT_STATUS_IS_OK(result))
2039 return result;
2041 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2042 &alias_pol,
2043 &member_sid);
2045 if (!NT_STATUS_IS_OK(result))
2046 return result;
2048 done:
2049 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2050 return result;
2053 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2054 const DOM_SID *domain_sid,
2055 const char *domain_name,
2056 struct cli_state *cli,
2057 struct rpc_pipe_client *pipe_hnd,
2058 TALLOC_CTX *mem_ctx,
2059 int argc,
2060 const char **argv)
2062 DOM_SID group_sid;
2063 enum lsa_SidType group_type;
2065 if (argc != 2 || c->display_usage) {
2066 d_printf("Usage:\n"
2067 "net rpc group delmem <group> <member>\n"
2068 " Delete a member from a group\n"
2069 " group\tGroup to delete member from\n"
2070 " member\tMember to delete from group\n");
2071 return NT_STATUS_UNSUCCESSFUL;
2074 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2075 &group_sid, &group_type))) {
2076 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2077 return NT_STATUS_UNSUCCESSFUL;
2080 if (group_type == SID_NAME_DOM_GRP) {
2081 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2082 &group_sid, argv[1]);
2084 if (!NT_STATUS_IS_OK(result)) {
2085 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2086 argv[1], argv[0], nt_errstr(result));
2088 return result;
2091 if (group_type == SID_NAME_ALIAS) {
2092 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2093 &group_sid, argv[1]);
2095 if (!NT_STATUS_IS_OK(result)) {
2096 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2097 argv[1], argv[0], nt_errstr(result));
2099 return result;
2102 d_fprintf(stderr, "Can only delete members from global or local groups "
2103 "which %s is not\n", argv[0]);
2105 return NT_STATUS_UNSUCCESSFUL;
2108 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2110 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2111 rpc_group_delmem_internals,
2112 argc, argv);
2116 * List groups on a remote RPC server.
2118 * All parameters are provided by the run_rpc_command function, except for
2119 * argc, argv which are passes through.
2121 * @param domain_sid The domain sid acquired from the remote server.
2122 * @param cli A cli_state connected to the server.
2123 * @param mem_ctx Talloc context, destroyed on completion of the function.
2124 * @param argc Standard main() style argc.
2125 * @param argv Standard main() style argv. Initial components are already
2126 * stripped.
2128 * @return Normal NTSTATUS return.
2131 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2132 const DOM_SID *domain_sid,
2133 const char *domain_name,
2134 struct cli_state *cli,
2135 struct rpc_pipe_client *pipe_hnd,
2136 TALLOC_CTX *mem_ctx,
2137 int argc,
2138 const char **argv)
2140 POLICY_HND connect_pol, domain_pol;
2141 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2142 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2143 struct samr_SamArray *groups = NULL;
2144 bool global = false;
2145 bool local = false;
2146 bool builtin = false;
2148 if (c->display_usage) {
2149 d_printf("Usage:\n"
2150 "net rpc group list [global] [local] [builtin]\n"
2151 " List groups on RPC server\n"
2152 " global\tList global groups\n"
2153 " local\tList local groups\n"
2154 " builtin\tList builtin groups\n"
2155 " If none of global, local or builtin is "
2156 "specified, all three options are considered set\n");
2157 return NT_STATUS_OK;
2160 if (argc == 0) {
2161 global = true;
2162 local = true;
2163 builtin = true;
2166 for (i=0; i<argc; i++) {
2167 if (strequal(argv[i], "global"))
2168 global = true;
2170 if (strequal(argv[i], "local"))
2171 local = true;
2173 if (strequal(argv[i], "builtin"))
2174 builtin = true;
2177 /* Get sam policy handle */
2179 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2180 pipe_hnd->desthost,
2181 MAXIMUM_ALLOWED_ACCESS,
2182 &connect_pol);
2183 if (!NT_STATUS_IS_OK(result)) {
2184 goto done;
2187 /* Get domain policy handle */
2189 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2190 &connect_pol,
2191 MAXIMUM_ALLOWED_ACCESS,
2192 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2193 &domain_pol);
2194 if (!NT_STATUS_IS_OK(result)) {
2195 goto done;
2198 /* Query domain groups */
2199 if (c->opt_long_list_entries)
2200 d_printf("\nGroup name Comment"
2201 "\n-----------------------------\n");
2202 do {
2203 uint32_t max_size, total_size, returned_size;
2204 union samr_DispInfo info;
2206 if (!global) break;
2208 get_query_dispinfo_params(
2209 loop_count, &max_entries, &max_size);
2211 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2212 &domain_pol,
2214 start_idx,
2215 max_entries,
2216 max_size,
2217 &total_size,
2218 &returned_size,
2219 &info);
2220 num_entries = info.info3.count;
2221 start_idx += info.info3.count;
2223 if (!NT_STATUS_IS_OK(result) &&
2224 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2225 break;
2227 for (i = 0; i < num_entries; i++) {
2229 const char *group = NULL;
2230 const char *desc = NULL;
2232 group = info.info3.entries[i].account_name.string;
2233 desc = info.info3.entries[i].description.string;
2235 if (c->opt_long_list_entries)
2236 printf("%-21.21s %-50.50s\n",
2237 group, desc);
2238 else
2239 printf("%s\n", group);
2241 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2242 /* query domain aliases */
2243 start_idx = 0;
2244 do {
2245 if (!local) break;
2247 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2248 &domain_pol,
2249 &start_idx,
2250 &groups,
2251 0xffff,
2252 &num_entries);
2253 if (!NT_STATUS_IS_OK(result) &&
2254 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2255 break;
2257 for (i = 0; i < num_entries; i++) {
2259 const char *description = NULL;
2261 if (c->opt_long_list_entries) {
2263 POLICY_HND alias_pol;
2264 union samr_AliasInfo *info = NULL;
2266 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2267 &domain_pol,
2268 0x8,
2269 groups->entries[i].idx,
2270 &alias_pol))) &&
2271 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2272 &alias_pol,
2274 &info))) &&
2275 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2276 &alias_pol)))) {
2277 description = info->description.string;
2281 if (description != NULL) {
2282 printf("%-21.21s %-50.50s\n",
2283 groups->entries[i].name.string,
2284 description);
2285 } else {
2286 printf("%s\n", groups->entries[i].name.string);
2289 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2290 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2291 /* Get builtin policy handle */
2293 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2294 &connect_pol,
2295 MAXIMUM_ALLOWED_ACCESS,
2296 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2297 &domain_pol);
2298 if (!NT_STATUS_IS_OK(result)) {
2299 goto done;
2301 /* query builtin aliases */
2302 start_idx = 0;
2303 do {
2304 if (!builtin) break;
2306 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2307 &domain_pol,
2308 &start_idx,
2309 &groups,
2310 max_entries,
2311 &num_entries);
2312 if (!NT_STATUS_IS_OK(result) &&
2313 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2314 break;
2316 for (i = 0; i < num_entries; i++) {
2318 const char *description = NULL;
2320 if (c->opt_long_list_entries) {
2322 POLICY_HND alias_pol;
2323 union samr_AliasInfo *info = NULL;
2325 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2326 &domain_pol,
2327 0x8,
2328 groups->entries[i].idx,
2329 &alias_pol))) &&
2330 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2331 &alias_pol,
2333 &info))) &&
2334 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2335 &alias_pol)))) {
2336 description = info->description.string;
2340 if (description != NULL) {
2341 printf("%-21.21s %-50.50s\n",
2342 groups->entries[i].name.string,
2343 description);
2344 } else {
2345 printf("%s\n", groups->entries[i].name.string);
2348 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2350 done:
2351 return result;
2354 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2356 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2357 rpc_group_list_internals,
2358 argc, argv);
2361 static NTSTATUS rpc_list_group_members(struct net_context *c,
2362 struct rpc_pipe_client *pipe_hnd,
2363 TALLOC_CTX *mem_ctx,
2364 const char *domain_name,
2365 const DOM_SID *domain_sid,
2366 POLICY_HND *domain_pol,
2367 uint32 rid)
2369 NTSTATUS result;
2370 POLICY_HND group_pol;
2371 uint32 num_members, *group_rids;
2372 int i;
2373 struct samr_RidTypeArray *rids = NULL;
2374 struct lsa_Strings names;
2375 struct samr_Ids types;
2377 fstring sid_str;
2378 sid_to_fstring(sid_str, domain_sid);
2380 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2381 domain_pol,
2382 MAXIMUM_ALLOWED_ACCESS,
2383 rid,
2384 &group_pol);
2386 if (!NT_STATUS_IS_OK(result))
2387 return result;
2389 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2390 &group_pol,
2391 &rids);
2393 if (!NT_STATUS_IS_OK(result))
2394 return result;
2396 num_members = rids->count;
2397 group_rids = rids->rids;
2399 while (num_members > 0) {
2400 int this_time = 512;
2402 if (num_members < this_time)
2403 this_time = num_members;
2405 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2406 domain_pol,
2407 this_time,
2408 group_rids,
2409 &names,
2410 &types);
2412 if (!NT_STATUS_IS_OK(result))
2413 return result;
2415 /* We only have users as members, but make the output
2416 the same as the output of alias members */
2418 for (i = 0; i < this_time; i++) {
2420 if (c->opt_long_list_entries) {
2421 printf("%s-%d %s\\%s %d\n", sid_str,
2422 group_rids[i], domain_name,
2423 names.names[i].string,
2424 SID_NAME_USER);
2425 } else {
2426 printf("%s\\%s\n", domain_name,
2427 names.names[i].string);
2431 num_members -= this_time;
2432 group_rids += 512;
2435 return NT_STATUS_OK;
2438 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2439 struct rpc_pipe_client *pipe_hnd,
2440 TALLOC_CTX *mem_ctx,
2441 POLICY_HND *domain_pol,
2442 uint32 rid)
2444 NTSTATUS result;
2445 struct rpc_pipe_client *lsa_pipe;
2446 POLICY_HND alias_pol, lsa_pol;
2447 uint32 num_members;
2448 DOM_SID *alias_sids;
2449 char **domains;
2450 char **names;
2451 enum lsa_SidType *types;
2452 int i;
2453 struct lsa_SidArray sid_array;
2455 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2456 domain_pol,
2457 MAXIMUM_ALLOWED_ACCESS,
2458 rid,
2459 &alias_pol);
2461 if (!NT_STATUS_IS_OK(result))
2462 return result;
2464 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2465 &alias_pol,
2466 &sid_array);
2468 if (!NT_STATUS_IS_OK(result)) {
2469 d_fprintf(stderr, "Couldn't list alias members\n");
2470 return result;
2473 num_members = sid_array.num_sids;
2475 if (num_members == 0) {
2476 return NT_STATUS_OK;
2479 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2480 &ndr_table_lsarpc.syntax_id,
2481 &lsa_pipe);
2482 if (!NT_STATUS_IS_OK(result)) {
2483 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2484 nt_errstr(result) );
2485 return result;
2488 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2489 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2491 if (!NT_STATUS_IS_OK(result)) {
2492 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2493 TALLOC_FREE(lsa_pipe);
2494 return result;
2497 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2498 if (!alias_sids) {
2499 d_fprintf(stderr, "Out of memory\n");
2500 TALLOC_FREE(lsa_pipe);
2501 return NT_STATUS_NO_MEMORY;
2504 for (i=0; i<num_members; i++) {
2505 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2508 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2509 num_members, alias_sids,
2510 &domains, &names, &types);
2512 if (!NT_STATUS_IS_OK(result) &&
2513 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2514 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2515 TALLOC_FREE(lsa_pipe);
2516 return result;
2519 for (i = 0; i < num_members; i++) {
2520 fstring sid_str;
2521 sid_to_fstring(sid_str, &alias_sids[i]);
2523 if (c->opt_long_list_entries) {
2524 printf("%s %s\\%s %d\n", sid_str,
2525 domains[i] ? domains[i] : "*unknown*",
2526 names[i] ? names[i] : "*unknown*", types[i]);
2527 } else {
2528 if (domains[i])
2529 printf("%s\\%s\n", domains[i], names[i]);
2530 else
2531 printf("%s\n", sid_str);
2535 TALLOC_FREE(lsa_pipe);
2536 return NT_STATUS_OK;
2539 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2540 const DOM_SID *domain_sid,
2541 const char *domain_name,
2542 struct cli_state *cli,
2543 struct rpc_pipe_client *pipe_hnd,
2544 TALLOC_CTX *mem_ctx,
2545 int argc,
2546 const char **argv)
2548 NTSTATUS result;
2549 POLICY_HND connect_pol, domain_pol;
2550 struct samr_Ids rids, rid_types;
2551 struct lsa_String lsa_acct_name;
2553 /* Get sam policy handle */
2555 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2556 pipe_hnd->desthost,
2557 MAXIMUM_ALLOWED_ACCESS,
2558 &connect_pol);
2560 if (!NT_STATUS_IS_OK(result))
2561 return result;
2563 /* Get domain policy handle */
2565 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2566 &connect_pol,
2567 MAXIMUM_ALLOWED_ACCESS,
2568 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2569 &domain_pol);
2571 if (!NT_STATUS_IS_OK(result))
2572 return result;
2574 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2576 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2577 &domain_pol,
2579 &lsa_acct_name,
2580 &rids,
2581 &rid_types);
2583 if (!NT_STATUS_IS_OK(result)) {
2585 /* Ok, did not find it in the global sam, try with builtin */
2587 DOM_SID sid_Builtin;
2589 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2591 sid_copy(&sid_Builtin, &global_sid_Builtin);
2593 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2594 &connect_pol,
2595 MAXIMUM_ALLOWED_ACCESS,
2596 &sid_Builtin,
2597 &domain_pol);
2599 if (!NT_STATUS_IS_OK(result)) {
2600 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2601 return result;
2604 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2605 &domain_pol,
2607 &lsa_acct_name,
2608 &rids,
2609 &rid_types);
2611 if (!NT_STATUS_IS_OK(result)) {
2612 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2613 return result;
2617 if (rids.count != 1) {
2618 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2619 return result;
2622 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2623 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2624 domain_sid, &domain_pol,
2625 rids.ids[0]);
2628 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2629 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2630 rids.ids[0]);
2633 return NT_STATUS_NO_SUCH_GROUP;
2636 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2638 if (argc != 1 || c->display_usage) {
2639 return rpc_group_usage(c, argc, argv);
2642 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2643 rpc_group_members_internals,
2644 argc, argv);
2647 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2649 NET_API_STATUS status;
2650 struct GROUP_INFO_0 g0;
2651 uint32_t parm_err;
2653 if (argc != 2) {
2654 d_printf("Usage: 'net rpc group rename group newname'\n");
2655 return -1;
2658 g0.grpi0_name = argv[1];
2660 status = NetGroupSetInfo(c->opt_host,
2661 argv[0],
2663 (uint8_t *)&g0,
2664 &parm_err);
2666 if (status != 0) {
2667 d_fprintf(stderr, "Renaming group %s failed with: %s\n",
2668 argv[0], libnetapi_get_error_string(c->netapi_ctx,
2669 status));
2670 return -1;
2673 return 0;
2676 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
2678 if (argc != 2 || c->display_usage) {
2679 return rpc_group_usage(c, argc, argv);
2682 return rpc_group_rename_internals(c, argc, argv);
2686 * 'net rpc group' entrypoint.
2687 * @param argc Standard main() style argc.
2688 * @param argv Standard main() style argv. Initial components are already
2689 * stripped.
2692 int net_rpc_group(struct net_context *c, int argc, const char **argv)
2694 NET_API_STATUS status;
2696 struct functable func[] = {
2698 "add",
2699 rpc_group_add,
2700 NET_TRANSPORT_RPC,
2701 "Create specified group",
2702 "net rpc group add\n"
2703 " Create specified group"
2706 "delete",
2707 rpc_group_delete,
2708 NET_TRANSPORT_RPC,
2709 "Delete specified group",
2710 "net rpc group delete\n"
2711 " Delete specified group"
2714 "addmem",
2715 rpc_group_addmem,
2716 NET_TRANSPORT_RPC,
2717 "Add member to group",
2718 "net rpc group addmem\n"
2719 " Add member to group"
2722 "delmem",
2723 rpc_group_delmem,
2724 NET_TRANSPORT_RPC,
2725 "Remove member from group",
2726 "net rpc group delmem\n"
2727 " Remove member from group"
2730 "list",
2731 rpc_group_list,
2732 NET_TRANSPORT_RPC,
2733 "List groups",
2734 "net rpc group list\n"
2735 " List groups"
2738 "members",
2739 rpc_group_members,
2740 NET_TRANSPORT_RPC,
2741 "List group members",
2742 "net rpc group members\n"
2743 " List group members"
2746 "rename",
2747 rpc_group_rename,
2748 NET_TRANSPORT_RPC,
2749 "Rename group",
2750 "net rpc group rename\n"
2751 " Rename group"
2753 {NULL, NULL, 0, NULL, NULL}
2756 status = libnetapi_init(&c->netapi_ctx);
2757 if (status != 0) {
2758 return -1;
2760 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
2761 libnetapi_set_password(c->netapi_ctx, c->opt_password);
2762 if (c->opt_kerberos) {
2763 libnetapi_set_use_kerberos(c->netapi_ctx);
2766 if (argc == 0) {
2767 if (c->display_usage) {
2768 d_printf("Usage:\n");
2769 d_printf("net rpc group\n"
2770 " Alias for net rpc group list global local "
2771 "builtin\n");
2772 net_display_usage_from_functable(func);
2773 return 0;
2776 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2777 rpc_group_list_internals,
2778 argc, argv);
2781 return net_run_function(c, argc, argv, "net rpc group", func);
2784 /****************************************************************************/
2786 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
2788 return net_share_usage(c, argc, argv);
2792 * Add a share on a remote RPC server.
2794 * @param argc Standard main() style argc.
2795 * @param argv Standard main() style argv. Initial components are already
2796 * stripped.
2798 * @return A shell status integer (0 for success).
2801 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
2803 NET_API_STATUS status;
2804 char *sharename;
2805 char *path;
2806 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
2807 uint32 num_users=0, perms=0;
2808 char *password=NULL; /* don't allow a share password */
2809 struct SHARE_INFO_2 i2;
2810 uint32_t parm_error = 0;
2812 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
2813 return rpc_share_usage(c, argc, argv);
2816 if ((sharename = talloc_strdup(c, argv[0])) == NULL) {
2817 return -1;
2820 path = strchr(sharename, '=');
2821 if (!path) {
2822 return -1;
2825 *path++ = '\0';
2827 i2.shi2_netname = sharename;
2828 i2.shi2_type = type;
2829 i2.shi2_remark = c->opt_comment;
2830 i2.shi2_permissions = perms;
2831 i2.shi2_max_uses = c->opt_maxusers;
2832 i2.shi2_current_uses = num_users;
2833 i2.shi2_path = path;
2834 i2.shi2_passwd = password;
2836 status = NetShareAdd(c->opt_host,
2838 (uint8_t *)&i2,
2839 &parm_error);
2840 if (status != 0) {
2841 printf("NetShareAdd failed with: %s\n",
2842 libnetapi_get_error_string(c->netapi_ctx, status));
2845 return status;
2849 * Delete a share on a remote RPC server.
2851 * @param domain_sid The domain sid acquired from the remote server.
2852 * @param argc Standard main() style argc.
2853 * @param argv Standard main() style argv. Initial components are already
2854 * stripped.
2856 * @return A shell status integer (0 for success).
2858 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
2860 if (argc < 1 || c->display_usage) {
2861 return rpc_share_usage(c, argc, argv);
2864 return NetShareDel(c->opt_host, argv[0], 0);
2868 * Formatted print of share info
2870 * @param r pointer to SHARE_INFO_1 to format
2873 static void display_share_info_1(struct net_context *c,
2874 struct SHARE_INFO_1 *r)
2876 if (c->opt_long_list_entries) {
2877 d_printf("%-12s %-8.8s %-50s\n",
2878 r->shi1_netname,
2879 share_type[r->shi1_type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)],
2880 r->shi1_remark);
2881 } else {
2882 d_printf("%s\n", r->shi1_netname);
2886 static WERROR get_share_info(struct net_context *c,
2887 struct rpc_pipe_client *pipe_hnd,
2888 TALLOC_CTX *mem_ctx,
2889 uint32 level,
2890 int argc,
2891 const char **argv,
2892 struct srvsvc_NetShareInfoCtr *info_ctr)
2894 WERROR result;
2895 NTSTATUS status;
2896 union srvsvc_NetShareInfo info;
2898 /* no specific share requested, enumerate all */
2899 if (argc == 0) {
2901 uint32_t preferred_len = 0xffffffff;
2902 uint32_t total_entries = 0;
2903 uint32_t resume_handle = 0;
2905 info_ctr->level = level;
2907 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
2908 pipe_hnd->desthost,
2909 info_ctr,
2910 preferred_len,
2911 &total_entries,
2912 &resume_handle,
2913 &result);
2914 return result;
2917 /* request just one share */
2918 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
2919 pipe_hnd->desthost,
2920 argv[0],
2921 level,
2922 &info,
2923 &result);
2925 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
2926 goto done;
2929 /* construct ctr */
2930 ZERO_STRUCTP(info_ctr);
2932 info_ctr->level = level;
2934 switch (level) {
2935 case 1:
2937 struct srvsvc_NetShareCtr1 *ctr1;
2939 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
2940 W_ERROR_HAVE_NO_MEMORY(ctr1);
2942 ctr1->count = 1;
2943 ctr1->array = info.info1;
2945 info_ctr->ctr.ctr1 = ctr1;
2947 case 2:
2949 struct srvsvc_NetShareCtr2 *ctr2;
2951 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
2952 W_ERROR_HAVE_NO_MEMORY(ctr2);
2954 ctr2->count = 1;
2955 ctr2->array = info.info2;
2957 info_ctr->ctr.ctr2 = ctr2;
2959 case 502:
2961 struct srvsvc_NetShareCtr502 *ctr502;
2963 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
2964 W_ERROR_HAVE_NO_MEMORY(ctr502);
2966 ctr502->count = 1;
2967 ctr502->array = info.info502;
2969 info_ctr->ctr.ctr502 = ctr502;
2971 } /* switch */
2972 done:
2973 return result;
2976 /***
2977 * 'net rpc share list' entrypoint.
2978 * @param argc Standard main() style argc.
2979 * @param argv Standard main() style argv. Initial components are already
2980 * stripped.
2982 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
2984 NET_API_STATUS status;
2985 struct SHARE_INFO_1 *i1 = NULL;
2986 uint32_t entries_read = 0;
2987 uint32_t total_entries = 0;
2988 uint32_t resume_handle = 0;
2989 uint32_t i, level = 1;
2991 if (c->display_usage) {
2992 d_printf("Usage\n"
2993 "net rpc share list\n"
2994 " List shares on remote server\n");
2995 return 0;
2998 status = NetShareEnum(c->opt_host,
2999 level,
3000 (uint8_t **)&i1,
3001 (uint32_t)-1,
3002 &entries_read,
3003 &total_entries,
3004 &resume_handle);
3005 if (status != 0) {
3006 goto done;
3009 /* Display results */
3011 if (c->opt_long_list_entries) {
3012 d_printf(
3013 "\nEnumerating shared resources (exports) on remote server:\n\n"
3014 "\nShare name Type Description\n"
3015 "---------- ---- -----------\n");
3017 for (i = 0; i < entries_read; i++)
3018 display_share_info_1(c, &i1[i]);
3019 done:
3020 return status;
3023 static bool check_share_availability(struct cli_state *cli, const char *netname)
3025 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3026 d_printf("skipping [%s]: not a file share.\n", netname);
3027 return false;
3030 if (!cli_tdis(cli))
3031 return false;
3033 return true;
3036 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3037 const char *netname, uint32 type)
3039 /* only support disk shares */
3040 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3041 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3042 return false;
3045 /* skip builtin shares */
3046 /* FIXME: should print$ be added too ? */
3047 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3048 strequal(netname,"global"))
3049 return false;
3051 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3052 printf("excluding [%s]\n", netname);
3053 return false;
3056 return check_share_availability(cli, netname);
3060 * Migrate shares from a remote RPC server to the local RPC server.
3062 * All parameters are provided by the run_rpc_command function, except for
3063 * argc, argv which are passed through.
3065 * @param domain_sid The domain sid acquired from the remote server.
3066 * @param cli A cli_state connected to the server.
3067 * @param mem_ctx Talloc context, destroyed on completion of the function.
3068 * @param argc Standard main() style argc.
3069 * @param argv Standard main() style argv. Initial components are already
3070 * stripped.
3072 * @return Normal NTSTATUS return.
3075 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3076 const DOM_SID *domain_sid,
3077 const char *domain_name,
3078 struct cli_state *cli,
3079 struct rpc_pipe_client *pipe_hnd,
3080 TALLOC_CTX *mem_ctx,
3081 int argc,
3082 const char **argv)
3084 WERROR result;
3085 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3086 struct srvsvc_NetShareInfoCtr ctr_src;
3087 uint32 i;
3088 struct rpc_pipe_client *srvsvc_pipe = NULL;
3089 struct cli_state *cli_dst = NULL;
3090 uint32 level = 502; /* includes secdesc */
3091 uint32_t parm_error = 0;
3093 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3094 &ctr_src);
3095 if (!W_ERROR_IS_OK(result))
3096 goto done;
3098 /* connect destination PI_SRVSVC */
3099 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3100 &ndr_table_srvsvc.syntax_id);
3101 if (!NT_STATUS_IS_OK(nt_status))
3102 return nt_status;
3105 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3107 union srvsvc_NetShareInfo info;
3108 struct srvsvc_NetShareInfo502 info502 =
3109 ctr_src.ctr.ctr502->array[i];
3111 /* reset error-code */
3112 nt_status = NT_STATUS_UNSUCCESSFUL;
3114 if (!check_share_sanity(c, cli, info502.name, info502.type))
3115 continue;
3117 /* finally add the share on the dst server */
3119 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3120 info502.name, info502.path, info502.comment);
3122 info.info502 = &info502;
3124 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3125 srvsvc_pipe->desthost,
3126 502,
3127 &info,
3128 &parm_error,
3129 &result);
3131 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3132 printf(" [%s] does already exist\n",
3133 info502.name);
3134 continue;
3137 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3138 printf("cannot add share: %s\n", dos_errstr(result));
3139 goto done;
3144 nt_status = NT_STATUS_OK;
3146 done:
3147 if (cli_dst) {
3148 cli_shutdown(cli_dst);
3151 return nt_status;
3156 * Migrate shares from a RPC server to another.
3158 * @param argc Standard main() style argc.
3159 * @param argv Standard main() style argv. Initial components are already
3160 * stripped.
3162 * @return A shell status integer (0 for success).
3164 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3165 const char **argv)
3167 if (c->display_usage) {
3168 d_printf("Usage:\n"
3169 "net rpc share migrate shares\n"
3170 " Migrate shares to local server\n");
3171 return 0;
3174 if (!c->opt_host) {
3175 printf("no server to migrate\n");
3176 return -1;
3179 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3180 rpc_share_migrate_shares_internals,
3181 argc, argv);
3185 * Copy a file/dir
3187 * @param f file_info
3188 * @param mask current search mask
3189 * @param state arg-pointer
3192 static void copy_fn(const char *mnt, file_info *f,
3193 const char *mask, void *state)
3195 static NTSTATUS nt_status;
3196 static struct copy_clistate *local_state;
3197 static fstring filename, new_mask;
3198 fstring dir;
3199 char *old_dir;
3200 struct net_context *c;
3202 local_state = (struct copy_clistate *)state;
3203 nt_status = NT_STATUS_UNSUCCESSFUL;
3205 c = local_state->c;
3207 if (strequal(f->name, ".") || strequal(f->name, ".."))
3208 return;
3210 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3212 /* DIRECTORY */
3213 if (f->mode & aDIR) {
3215 DEBUG(3,("got dir: %s\n", f->name));
3217 fstrcpy(dir, local_state->cwd);
3218 fstrcat(dir, "\\");
3219 fstrcat(dir, f->name);
3221 switch (net_mode_share)
3223 case NET_MODE_SHARE_MIGRATE:
3224 /* create that directory */
3225 nt_status = net_copy_file(c, local_state->mem_ctx,
3226 local_state->cli_share_src,
3227 local_state->cli_share_dst,
3228 dir, dir,
3229 c->opt_acls? true : false,
3230 c->opt_attrs? true : false,
3231 c->opt_timestamps? true:false,
3232 false);
3233 break;
3234 default:
3235 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3236 return;
3239 if (!NT_STATUS_IS_OK(nt_status))
3240 printf("could not handle dir %s: %s\n",
3241 dir, nt_errstr(nt_status));
3243 /* search below that directory */
3244 fstrcpy(new_mask, dir);
3245 fstrcat(new_mask, "\\*");
3247 old_dir = local_state->cwd;
3248 local_state->cwd = dir;
3249 if (!sync_files(local_state, new_mask))
3250 printf("could not handle files\n");
3251 local_state->cwd = old_dir;
3253 return;
3257 /* FILE */
3258 fstrcpy(filename, local_state->cwd);
3259 fstrcat(filename, "\\");
3260 fstrcat(filename, f->name);
3262 DEBUG(3,("got file: %s\n", filename));
3264 switch (net_mode_share)
3266 case NET_MODE_SHARE_MIGRATE:
3267 nt_status = net_copy_file(c, local_state->mem_ctx,
3268 local_state->cli_share_src,
3269 local_state->cli_share_dst,
3270 filename, filename,
3271 c->opt_acls? true : false,
3272 c->opt_attrs? true : false,
3273 c->opt_timestamps? true: false,
3274 true);
3275 break;
3276 default:
3277 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3278 return;
3281 if (!NT_STATUS_IS_OK(nt_status))
3282 printf("could not handle file %s: %s\n",
3283 filename, nt_errstr(nt_status));
3288 * sync files, can be called recursivly to list files
3289 * and then call copy_fn for each file
3291 * @param cp_clistate pointer to the copy_clistate we work with
3292 * @param mask the current search mask
3294 * @return Boolean result
3296 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3298 struct cli_state *targetcli;
3299 char *targetpath = NULL;
3301 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3303 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate->cli_share_src,
3304 mask, &targetcli, &targetpath ) ) {
3305 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3306 mask, cli_errstr(cp_clistate->cli_share_src));
3307 return false;
3310 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3311 d_fprintf(stderr, "listing %s failed with error: %s\n",
3312 mask, cli_errstr(targetcli));
3313 return false;
3316 return true;
3321 * Set the top level directory permissions before we do any further copies.
3322 * Should set up ACL inheritance.
3325 bool copy_top_level_perms(struct net_context *c,
3326 struct copy_clistate *cp_clistate,
3327 const char *sharename)
3329 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3331 switch (net_mode_share) {
3332 case NET_MODE_SHARE_MIGRATE:
3333 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3334 nt_status = net_copy_fileattr(c,
3335 cp_clistate->mem_ctx,
3336 cp_clistate->cli_share_src,
3337 cp_clistate->cli_share_dst,
3338 "\\", "\\",
3339 c->opt_acls? true : false,
3340 c->opt_attrs? true : false,
3341 c->opt_timestamps? true: false,
3342 false);
3343 break;
3344 default:
3345 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3346 break;
3349 if (!NT_STATUS_IS_OK(nt_status)) {
3350 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3351 sharename, nt_errstr(nt_status));
3352 return false;
3355 return true;
3359 * Sync all files inside a remote share to another share (over smb).
3361 * All parameters are provided by the run_rpc_command function, except for
3362 * argc, argv which are passed through.
3364 * @param domain_sid The domain sid acquired from the remote server.
3365 * @param cli A cli_state connected to the server.
3366 * @param mem_ctx Talloc context, destroyed on completion of the function.
3367 * @param argc Standard main() style argc.
3368 * @param argv Standard main() style argv. Initial components are already
3369 * stripped.
3371 * @return Normal NTSTATUS return.
3374 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3375 const DOM_SID *domain_sid,
3376 const char *domain_name,
3377 struct cli_state *cli,
3378 struct rpc_pipe_client *pipe_hnd,
3379 TALLOC_CTX *mem_ctx,
3380 int argc,
3381 const char **argv)
3383 WERROR result;
3384 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3385 struct srvsvc_NetShareInfoCtr ctr_src;
3386 uint32 i;
3387 uint32 level = 502;
3388 struct copy_clistate cp_clistate;
3389 bool got_src_share = false;
3390 bool got_dst_share = false;
3391 const char *mask = "\\*";
3392 char *dst = NULL;
3394 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3395 if (dst == NULL) {
3396 nt_status = NT_STATUS_NO_MEMORY;
3397 goto done;
3400 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3401 &ctr_src);
3403 if (!W_ERROR_IS_OK(result))
3404 goto done;
3406 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3408 struct srvsvc_NetShareInfo502 info502 =
3409 ctr_src.ctr.ctr502->array[i];
3411 if (!check_share_sanity(c, cli, info502.name, info502.type))
3412 continue;
3414 /* one might not want to mirror whole discs :) */
3415 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3416 d_printf("skipping [%s]: builtin/hidden share\n", info502.name);
3417 continue;
3420 switch (net_mode_share)
3422 case NET_MODE_SHARE_MIGRATE:
3423 printf("syncing");
3424 break;
3425 default:
3426 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3427 break;
3429 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3430 info502.name,
3431 c->opt_acls ? "including" : "without",
3432 c->opt_attrs ? "including" : "without",
3433 c->opt_timestamps ? "(preserving timestamps)" : "");
3435 cp_clistate.mem_ctx = mem_ctx;
3436 cp_clistate.cli_share_src = NULL;
3437 cp_clistate.cli_share_dst = NULL;
3438 cp_clistate.cwd = NULL;
3439 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3440 cp_clistate.c = c;
3442 /* open share source */
3443 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3444 &cli->dest_ss, cli->desthost,
3445 info502.name, "A:");
3446 if (!NT_STATUS_IS_OK(nt_status))
3447 goto done;
3449 got_src_share = true;
3451 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3452 /* open share destination */
3453 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3454 NULL, dst, info502.name, "A:");
3455 if (!NT_STATUS_IS_OK(nt_status))
3456 goto done;
3458 got_dst_share = true;
3461 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3462 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", info502.name);
3463 nt_status = NT_STATUS_UNSUCCESSFUL;
3464 goto done;
3467 if (!sync_files(&cp_clistate, mask)) {
3468 d_fprintf(stderr, "could not handle files for share: %s\n", info502.name);
3469 nt_status = NT_STATUS_UNSUCCESSFUL;
3470 goto done;
3474 nt_status = NT_STATUS_OK;
3476 done:
3478 if (got_src_share)
3479 cli_shutdown(cp_clistate.cli_share_src);
3481 if (got_dst_share)
3482 cli_shutdown(cp_clistate.cli_share_dst);
3484 SAFE_FREE(dst);
3485 return nt_status;
3489 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3491 if (c->display_usage) {
3492 d_printf("Usage:\n"
3493 "net share migrate files\n"
3494 " Migrate files to local server\n");
3495 return 0;
3498 if (!c->opt_host) {
3499 d_printf("no server to migrate\n");
3500 return -1;
3503 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3504 rpc_share_migrate_files_internals,
3505 argc, argv);
3509 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3511 * All parameters are provided by the run_rpc_command function, except for
3512 * argc, argv which are passed through.
3514 * @param domain_sid The domain sid acquired from the remote server.
3515 * @param cli A cli_state connected to the server.
3516 * @param mem_ctx Talloc context, destroyed on completion of the function.
3517 * @param argc Standard main() style argc.
3518 * @param argv Standard main() style argv. Initial components are already
3519 * stripped.
3521 * @return Normal NTSTATUS return.
3524 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3525 const DOM_SID *domain_sid,
3526 const char *domain_name,
3527 struct cli_state *cli,
3528 struct rpc_pipe_client *pipe_hnd,
3529 TALLOC_CTX *mem_ctx,
3530 int argc,
3531 const char **argv)
3533 WERROR result;
3534 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3535 struct srvsvc_NetShareInfoCtr ctr_src;
3536 union srvsvc_NetShareInfo info;
3537 uint32 i;
3538 struct rpc_pipe_client *srvsvc_pipe = NULL;
3539 struct cli_state *cli_dst = NULL;
3540 uint32 level = 502; /* includes secdesc */
3541 uint32_t parm_error = 0;
3543 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3544 &ctr_src);
3546 if (!W_ERROR_IS_OK(result))
3547 goto done;
3549 /* connect destination PI_SRVSVC */
3550 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3551 &ndr_table_srvsvc.syntax_id);
3552 if (!NT_STATUS_IS_OK(nt_status))
3553 return nt_status;
3556 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3558 struct srvsvc_NetShareInfo502 info502 =
3559 ctr_src.ctr.ctr502->array[i];
3561 /* reset error-code */
3562 nt_status = NT_STATUS_UNSUCCESSFUL;
3564 if (!check_share_sanity(c, cli, info502.name, info502.type))
3565 continue;
3567 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3568 info502.name, info502.path, info502.comment);
3570 if (c->opt_verbose)
3571 display_sec_desc(info502.sd_buf.sd);
3573 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3574 info.info502 = &info502;
3576 /* finally modify the share on the dst server */
3577 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3578 srvsvc_pipe->desthost,
3579 info502.name,
3580 level,
3581 &info,
3582 &parm_error,
3583 &result);
3584 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3585 printf("cannot set share-acl: %s\n", dos_errstr(result));
3586 goto done;
3591 nt_status = NT_STATUS_OK;
3593 done:
3594 if (cli_dst) {
3595 cli_shutdown(cli_dst);
3598 return nt_status;
3603 * Migrate share-acls from a RPC server to another.
3605 * @param argc Standard main() style argc.
3606 * @param argv Standard main() style argv. Initial components are already
3607 * stripped.
3609 * @return A shell status integer (0 for success).
3611 static int rpc_share_migrate_security(struct net_context *c, int argc,
3612 const char **argv)
3614 if (c->display_usage) {
3615 d_printf("Usage:\n"
3616 "net rpc share migrate security\n"
3617 " Migrate share-acls to local server\n");
3618 return 0;
3621 if (!c->opt_host) {
3622 d_printf("no server to migrate\n");
3623 return -1;
3626 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3627 rpc_share_migrate_security_internals,
3628 argc, argv);
3632 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3633 * from one server to another.
3635 * @param argc Standard main() style argc.
3636 * @param argv Standard main() style argv. Initial components are already
3637 * stripped.
3639 * @return A shell status integer (0 for success).
3642 static int rpc_share_migrate_all(struct net_context *c, int argc,
3643 const char **argv)
3645 int ret;
3647 if (c->display_usage) {
3648 d_printf("Usage:\n"
3649 "net rpc share migrate all\n"
3650 " Migrates shares including all share settings\n");
3651 return 0;
3654 if (!c->opt_host) {
3655 d_printf("no server to migrate\n");
3656 return -1;
3659 /* order is important. we don't want to be locked out by the share-acl
3660 * before copying files - gd */
3662 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3663 rpc_share_migrate_shares_internals, argc, argv);
3664 if (ret)
3665 return ret;
3667 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3668 rpc_share_migrate_files_internals, argc, argv);
3669 if (ret)
3670 return ret;
3672 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3673 rpc_share_migrate_security_internals, argc,
3674 argv);
3679 * 'net rpc share migrate' entrypoint.
3680 * @param argc Standard main() style argc.
3681 * @param argv Standard main() style argv. Initial components are already
3682 * stripped.
3684 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3687 struct functable func[] = {
3689 "all",
3690 rpc_share_migrate_all,
3691 NET_TRANSPORT_RPC,
3692 "Migrate shares from remote to local server",
3693 "net rpc share migrate all\n"
3694 " Migrate shares from remote to local server"
3697 "files",
3698 rpc_share_migrate_files,
3699 NET_TRANSPORT_RPC,
3700 "Migrate files from remote to local server",
3701 "net rpc share migrate files\n"
3702 " Migrate files from remote to local server"
3705 "security",
3706 rpc_share_migrate_security,
3707 NET_TRANSPORT_RPC,
3708 "Migrate share-ACLs from remote to local server",
3709 "net rpc share migrate security\n"
3710 " Migrate share-ACLs from remote to local server"
3713 "shares",
3714 rpc_share_migrate_shares,
3715 NET_TRANSPORT_RPC,
3716 "Migrate shares from remote to local server",
3717 "net rpc share migrate shares\n"
3718 " Migrate shares from remote to local server"
3720 {NULL, NULL, 0, NULL, NULL}
3723 net_mode_share = NET_MODE_SHARE_MIGRATE;
3725 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3728 struct full_alias {
3729 DOM_SID sid;
3730 uint32 num_members;
3731 DOM_SID *members;
3734 static int num_server_aliases;
3735 static struct full_alias *server_aliases;
3738 * Add an alias to the static list.
3740 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3742 if (server_aliases == NULL)
3743 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3745 server_aliases[num_server_aliases] = *alias;
3746 num_server_aliases += 1;
3750 * For a specific domain on the server, fetch all the aliases
3751 * and their members. Add all of them to the server_aliases.
3754 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3755 TALLOC_CTX *mem_ctx,
3756 POLICY_HND *connect_pol,
3757 const DOM_SID *domain_sid)
3759 uint32 start_idx, max_entries, num_entries, i;
3760 struct samr_SamArray *groups = NULL;
3761 NTSTATUS result;
3762 POLICY_HND domain_pol;
3764 /* Get domain policy handle */
3766 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3767 connect_pol,
3768 MAXIMUM_ALLOWED_ACCESS,
3769 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3770 &domain_pol);
3771 if (!NT_STATUS_IS_OK(result))
3772 return result;
3774 start_idx = 0;
3775 max_entries = 250;
3777 do {
3778 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3779 &domain_pol,
3780 &start_idx,
3781 &groups,
3782 max_entries,
3783 &num_entries);
3784 for (i = 0; i < num_entries; i++) {
3786 POLICY_HND alias_pol;
3787 struct full_alias alias;
3788 struct lsa_SidArray sid_array;
3789 int j;
3791 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3792 &domain_pol,
3793 MAXIMUM_ALLOWED_ACCESS,
3794 groups->entries[i].idx,
3795 &alias_pol);
3796 if (!NT_STATUS_IS_OK(result))
3797 goto done;
3799 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3800 &alias_pol,
3801 &sid_array);
3802 if (!NT_STATUS_IS_OK(result))
3803 goto done;
3805 alias.num_members = sid_array.num_sids;
3807 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3808 if (!NT_STATUS_IS_OK(result))
3809 goto done;
3811 alias.members = NULL;
3813 if (alias.num_members > 0) {
3814 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3816 for (j = 0; j < alias.num_members; j++)
3817 sid_copy(&alias.members[j],
3818 sid_array.sids[j].sid);
3821 sid_copy(&alias.sid, domain_sid);
3822 sid_append_rid(&alias.sid, groups->entries[i].idx);
3824 push_alias(mem_ctx, &alias);
3826 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3828 result = NT_STATUS_OK;
3830 done:
3831 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3833 return result;
3837 * Dump server_aliases as names for debugging purposes.
3840 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
3841 const DOM_SID *domain_sid,
3842 const char *domain_name,
3843 struct cli_state *cli,
3844 struct rpc_pipe_client *pipe_hnd,
3845 TALLOC_CTX *mem_ctx,
3846 int argc,
3847 const char **argv)
3849 int i;
3850 NTSTATUS result;
3851 POLICY_HND lsa_pol;
3853 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
3854 SEC_RIGHTS_MAXIMUM_ALLOWED,
3855 &lsa_pol);
3856 if (!NT_STATUS_IS_OK(result))
3857 return result;
3859 for (i=0; i<num_server_aliases; i++) {
3860 char **names;
3861 char **domains;
3862 enum lsa_SidType *types;
3863 int j;
3865 struct full_alias *alias = &server_aliases[i];
3867 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3868 &alias->sid,
3869 &domains, &names, &types);
3870 if (!NT_STATUS_IS_OK(result))
3871 continue;
3873 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3875 if (alias->num_members == 0) {
3876 DEBUG(1, ("\n"));
3877 continue;
3880 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
3881 alias->num_members,
3882 alias->members,
3883 &domains, &names, &types);
3885 if (!NT_STATUS_IS_OK(result) &&
3886 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3887 continue;
3889 for (j=0; j<alias->num_members; j++)
3890 DEBUG(1, ("%s\\%s (%d); ",
3891 domains[j] ? domains[j] : "*unknown*",
3892 names[j] ? names[j] : "*unknown*",types[j]));
3893 DEBUG(1, ("\n"));
3896 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
3898 return NT_STATUS_OK;
3902 * Fetch a list of all server aliases and their members into
3903 * server_aliases.
3906 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
3907 const DOM_SID *domain_sid,
3908 const char *domain_name,
3909 struct cli_state *cli,
3910 struct rpc_pipe_client *pipe_hnd,
3911 TALLOC_CTX *mem_ctx,
3912 int argc,
3913 const char **argv)
3915 NTSTATUS result;
3916 POLICY_HND connect_pol;
3918 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3919 pipe_hnd->desthost,
3920 MAXIMUM_ALLOWED_ACCESS,
3921 &connect_pol);
3923 if (!NT_STATUS_IS_OK(result))
3924 goto done;
3926 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3927 &global_sid_Builtin);
3929 if (!NT_STATUS_IS_OK(result))
3930 goto done;
3932 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3933 domain_sid);
3935 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
3936 done:
3937 return result;
3940 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
3942 token->num_sids = 4;
3944 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
3945 d_fprintf(stderr, "malloc failed\n");
3946 token->num_sids = 0;
3947 return;
3950 token->user_sids[0] = *user_sid;
3951 sid_copy(&token->user_sids[1], &global_sid_World);
3952 sid_copy(&token->user_sids[2], &global_sid_Network);
3953 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
3956 static void free_user_token(NT_USER_TOKEN *token)
3958 SAFE_FREE(token->user_sids);
3961 static bool is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
3963 int i;
3965 for (i=0; i<token->num_sids; i++) {
3966 if (sid_compare(sid, &token->user_sids[i]) == 0)
3967 return true;
3969 return false;
3972 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
3974 if (is_sid_in_token(token, sid))
3975 return;
3977 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
3978 if (!token->user_sids) {
3979 return;
3982 sid_copy(&token->user_sids[token->num_sids], sid);
3984 token->num_sids += 1;
3987 struct user_token {
3988 fstring name;
3989 NT_USER_TOKEN token;
3992 static void dump_user_token(struct user_token *token)
3994 int i;
3996 d_printf("%s\n", token->name);
3998 for (i=0; i<token->token.num_sids; i++) {
3999 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4003 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4005 int i;
4007 for (i=0; i<alias->num_members; i++) {
4008 if (sid_compare(sid, &alias->members[i]) == 0)
4009 return true;
4012 return false;
4015 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4017 int i;
4019 for (i=0; i<num_server_aliases; i++) {
4020 if (is_alias_member(&sid, &server_aliases[i]))
4021 add_sid_to_token(token, &server_aliases[i].sid);
4026 * We got a user token with all the SIDs we can know about without asking the
4027 * server directly. These are the user and domain group sids. All of these can
4028 * be members of aliases. So scan the list of aliases for each of the SIDs and
4029 * add them to the token.
4032 static void collect_alias_memberships(NT_USER_TOKEN *token)
4034 int num_global_sids = token->num_sids;
4035 int i;
4037 for (i=0; i<num_global_sids; i++) {
4038 collect_sid_memberships(token, token->user_sids[i]);
4042 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4044 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4045 enum wbcSidType type;
4046 fstring full_name;
4047 struct wbcDomainSid wsid;
4048 char *sid_str = NULL;
4049 DOM_SID user_sid;
4050 uint32_t num_groups;
4051 gid_t *groups = NULL;
4052 uint32_t i;
4054 fstr_sprintf(full_name, "%s%c%s",
4055 domain, *lp_winbind_separator(), user);
4057 /* First let's find out the user sid */
4059 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4061 if (!WBC_ERROR_IS_OK(wbc_status)) {
4062 DEBUG(1, ("winbind could not find %s: %s\n",
4063 full_name, wbcErrorString(wbc_status)));
4064 return false;
4067 wbc_status = wbcSidToString(&wsid, &sid_str);
4068 if (!WBC_ERROR_IS_OK(wbc_status)) {
4069 return false;
4072 if (type != SID_NAME_USER) {
4073 wbcFreeMemory(sid_str);
4074 DEBUG(1, ("%s is not a user\n", full_name));
4075 return false;
4078 string_to_sid(&user_sid, sid_str);
4079 wbcFreeMemory(sid_str);
4080 sid_str = NULL;
4082 init_user_token(token, &user_sid);
4084 /* And now the groups winbind knows about */
4086 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4087 if (!WBC_ERROR_IS_OK(wbc_status)) {
4088 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4089 full_name, wbcErrorString(wbc_status)));
4090 return false;
4093 for (i = 0; i < num_groups; i++) {
4094 gid_t gid = groups[i];
4095 DOM_SID sid;
4097 wbc_status = wbcGidToSid(gid, &wsid);
4098 if (!WBC_ERROR_IS_OK(wbc_status)) {
4099 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4100 gid, wbcErrorString(wbc_status)));
4101 wbcFreeMemory(groups);
4102 return false;
4105 wbc_status = wbcSidToString(&wsid, &sid_str);
4106 if (!WBC_ERROR_IS_OK(wbc_status)) {
4107 wbcFreeMemory(groups);
4108 return false;
4111 DEBUG(3, (" %s\n", sid_str));
4113 string_to_sid(&sid, sid_str);
4114 wbcFreeMemory(sid_str);
4115 sid_str = NULL;
4117 add_sid_to_token(token, &sid);
4119 wbcFreeMemory(groups);
4121 return true;
4125 * Get a list of all user tokens we want to look at
4128 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4129 struct user_token **user_tokens)
4131 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4132 uint32_t i, num_users;
4133 const char **users;
4134 struct user_token *result;
4135 TALLOC_CTX *frame = NULL;
4137 if (lp_winbind_use_default_domain() &&
4138 (c->opt_target_workgroup == NULL)) {
4139 d_fprintf(stderr, "winbind use default domain = yes set, "
4140 "please specify a workgroup\n");
4141 return false;
4144 /* Send request to winbind daemon */
4146 wbc_status = wbcListUsers(NULL, &num_users, &users);
4147 if (!WBC_ERROR_IS_OK(wbc_status)) {
4148 DEBUG(1, ("winbind could not list users: %s\n",
4149 wbcErrorString(wbc_status)));
4150 return false;
4153 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4155 if (result == NULL) {
4156 DEBUG(1, ("Could not malloc sid array\n"));
4157 wbcFreeMemory(users);
4158 return false;
4161 frame = talloc_stackframe();
4162 for (i=0; i < num_users; i++) {
4163 fstring domain, user;
4164 char *p;
4166 fstrcpy(result[i].name, users[i]);
4168 p = strchr(users[i], *lp_winbind_separator());
4170 DEBUG(3, ("%s\n", users[i]));
4172 if (p == NULL) {
4173 fstrcpy(domain, c->opt_target_workgroup);
4174 fstrcpy(user, users[i]);
4175 } else {
4176 *p++ = '\0';
4177 fstrcpy(domain, users[i]);
4178 strupper_m(domain);
4179 fstrcpy(user, p);
4182 get_user_sids(domain, user, &(result[i].token));
4183 i+=1;
4185 TALLOC_FREE(frame);
4186 wbcFreeMemory(users);
4188 *num_tokens = num_users;
4189 *user_tokens = result;
4191 return true;
4194 static bool get_user_tokens_from_file(FILE *f,
4195 int *num_tokens,
4196 struct user_token **tokens)
4198 struct user_token *token = NULL;
4200 while (!feof(f)) {
4201 fstring line;
4203 if (fgets(line, sizeof(line)-1, f) == NULL) {
4204 return true;
4207 if (line[strlen(line)-1] == '\n')
4208 line[strlen(line)-1] = '\0';
4210 if (line[0] == ' ') {
4211 /* We have a SID */
4213 DOM_SID sid;
4214 string_to_sid(&sid, &line[1]);
4216 if (token == NULL) {
4217 DEBUG(0, ("File does not begin with username"));
4218 return false;
4221 add_sid_to_token(&token->token, &sid);
4222 continue;
4225 /* And a new user... */
4227 *num_tokens += 1;
4228 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4229 if (*tokens == NULL) {
4230 DEBUG(0, ("Could not realloc tokens\n"));
4231 return false;
4234 token = &((*tokens)[*num_tokens-1]);
4236 fstrcpy(token->name, line);
4237 token->token.num_sids = 0;
4238 token->token.user_sids = NULL;
4239 continue;
4242 return false;
4247 * Show the list of all users that have access to a share
4250 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4251 TALLOC_CTX *mem_ctx,
4252 const char *netname,
4253 int num_tokens,
4254 struct user_token *tokens)
4256 int fnum;
4257 SEC_DESC *share_sd = NULL;
4258 SEC_DESC *root_sd = NULL;
4259 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4260 int i;
4261 union srvsvc_NetShareInfo info;
4262 WERROR result;
4263 NTSTATUS status;
4264 uint16 cnum;
4266 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4267 pipe_hnd->desthost,
4268 netname,
4269 502,
4270 &info,
4271 &result);
4273 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4274 DEBUG(1, ("Coult not query secdesc for share %s\n",
4275 netname));
4276 return;
4279 share_sd = info.info502->sd_buf.sd;
4280 if (share_sd == NULL) {
4281 DEBUG(1, ("Got no secdesc for share %s\n",
4282 netname));
4285 cnum = cli->cnum;
4287 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4288 return;
4291 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4293 if (fnum != -1) {
4294 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4297 for (i=0; i<num_tokens; i++) {
4298 uint32 acc_granted;
4300 if (share_sd != NULL) {
4301 if (!se_access_check(share_sd, &tokens[i].token,
4302 1, &acc_granted, &status)) {
4303 DEBUG(1, ("Could not check share_sd for "
4304 "user %s\n",
4305 tokens[i].name));
4306 continue;
4309 if (!NT_STATUS_IS_OK(status))
4310 continue;
4313 if (root_sd == NULL) {
4314 d_printf(" %s\n", tokens[i].name);
4315 continue;
4318 if (!se_access_check(root_sd, &tokens[i].token,
4319 1, &acc_granted, &status)) {
4320 DEBUG(1, ("Could not check root_sd for user %s\n",
4321 tokens[i].name));
4322 continue;
4325 if (!NT_STATUS_IS_OK(status))
4326 continue;
4328 d_printf(" %s\n", tokens[i].name);
4331 if (fnum != -1)
4332 cli_close(cli, fnum);
4333 cli_tdis(cli);
4334 cli->cnum = cnum;
4336 return;
4339 struct share_list {
4340 int num_shares;
4341 char **shares;
4344 static void collect_share(const char *name, uint32 m,
4345 const char *comment, void *state)
4347 struct share_list *share_list = (struct share_list *)state;
4349 if (m != STYPE_DISKTREE)
4350 return;
4352 share_list->num_shares += 1;
4353 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4354 if (!share_list->shares) {
4355 share_list->num_shares = 0;
4356 return;
4358 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4362 * List shares on a remote RPC server, including the security descriptors.
4364 * All parameters are provided by the run_rpc_command function, except for
4365 * argc, argv which are passed through.
4367 * @param domain_sid The domain sid acquired from the remote server.
4368 * @param cli A cli_state connected to the server.
4369 * @param mem_ctx Talloc context, destroyed on completion of the function.
4370 * @param argc Standard main() style argc.
4371 * @param argv Standard main() style argv. Initial components are already
4372 * stripped.
4374 * @return Normal NTSTATUS return.
4377 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4378 const DOM_SID *domain_sid,
4379 const char *domain_name,
4380 struct cli_state *cli,
4381 struct rpc_pipe_client *pipe_hnd,
4382 TALLOC_CTX *mem_ctx,
4383 int argc,
4384 const char **argv)
4386 int ret;
4387 bool r;
4388 ENUM_HND hnd;
4389 uint32 i;
4390 FILE *f;
4392 struct user_token *tokens = NULL;
4393 int num_tokens = 0;
4395 struct share_list share_list;
4397 if (argc == 0) {
4398 f = stdin;
4399 } else {
4400 f = fopen(argv[0], "r");
4403 if (f == NULL) {
4404 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4405 return NT_STATUS_UNSUCCESSFUL;
4408 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4410 if (f != stdin)
4411 fclose(f);
4413 if (!r) {
4414 DEBUG(0, ("Could not read users from file\n"));
4415 return NT_STATUS_UNSUCCESSFUL;
4418 for (i=0; i<num_tokens; i++)
4419 collect_alias_memberships(&tokens[i].token);
4421 init_enum_hnd(&hnd, 0);
4423 share_list.num_shares = 0;
4424 share_list.shares = NULL;
4426 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4428 if (ret == -1) {
4429 DEBUG(0, ("Error returning browse list: %s\n",
4430 cli_errstr(cli)));
4431 goto done;
4434 for (i = 0; i < share_list.num_shares; i++) {
4435 char *netname = share_list.shares[i];
4437 if (netname[strlen(netname)-1] == '$')
4438 continue;
4440 d_printf("%s\n", netname);
4442 show_userlist(pipe_hnd, mem_ctx, netname,
4443 num_tokens, tokens);
4445 done:
4446 for (i=0; i<num_tokens; i++) {
4447 free_user_token(&tokens[i].token);
4449 SAFE_FREE(tokens);
4450 SAFE_FREE(share_list.shares);
4452 return NT_STATUS_OK;
4455 static int rpc_share_allowedusers(struct net_context *c, int argc,
4456 const char **argv)
4458 int result;
4460 if (c->display_usage) {
4461 d_printf("Usage:\n"
4462 "net rpc share allowedusers\n"
4463 " List allowed users\n");
4464 return 0;
4467 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4468 rpc_aliaslist_internals,
4469 argc, argv);
4470 if (result != 0)
4471 return result;
4473 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4474 rpc_aliaslist_dump,
4475 argc, argv);
4476 if (result != 0)
4477 return result;
4479 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4480 rpc_share_allowedusers_internals,
4481 argc, argv);
4484 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4486 int num_tokens = 0;
4487 struct user_token *tokens = NULL;
4488 int i;
4490 if (argc != 0) {
4491 net_usersidlist_usage(c, argc, argv);
4492 return 0;
4495 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4496 DEBUG(0, ("Could not get the user/sid list\n"));
4497 return 0;
4500 for (i=0; i<num_tokens; i++) {
4501 dump_user_token(&tokens[i]);
4502 free_user_token(&tokens[i].token);
4505 SAFE_FREE(tokens);
4506 return 1;
4509 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4511 d_printf("net usersidlist\n"
4512 "\tprints out a list of all users the running winbind knows\n"
4513 "\tabout, together with all their SIDs. This is used as\n"
4514 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4516 net_common_flags_usage(c, argc, argv);
4517 return -1;
4521 * 'net rpc share' entrypoint.
4522 * @param argc Standard main() style argc.
4523 * @param argv Standard main() style argv. Initial components are already
4524 * stripped.
4527 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4529 NET_API_STATUS status;
4531 struct functable func[] = {
4533 "add",
4534 rpc_share_add,
4535 NET_TRANSPORT_RPC,
4536 "Add share",
4537 "net rpc share add\n"
4538 " Add share"
4541 "delete",
4542 rpc_share_delete,
4543 NET_TRANSPORT_RPC,
4544 "Remove share",
4545 "net rpc share delete\n"
4546 " Remove share"
4549 "allowedusers",
4550 rpc_share_allowedusers,
4551 NET_TRANSPORT_RPC,
4552 "Modify allowed users",
4553 "net rpc share allowedusers\n"
4554 " Modify allowed users"
4557 "migrate",
4558 rpc_share_migrate,
4559 NET_TRANSPORT_RPC,
4560 "Migrate share to local server",
4561 "net rpc share migrate\n"
4562 " Migrate share to local server"
4565 "list",
4566 rpc_share_list,
4567 NET_TRANSPORT_RPC,
4568 "List shares",
4569 "net rpc share list\n"
4570 " List shares"
4572 {NULL, NULL, 0, NULL, NULL}
4575 status = libnetapi_init(&c->netapi_ctx);
4576 if (status != 0) {
4577 return -1;
4579 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4580 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4581 if (c->opt_kerberos) {
4582 libnetapi_set_use_kerberos(c->netapi_ctx);
4585 if (argc == 0) {
4586 if (c->display_usage) {
4587 d_printf("Usage:\n"
4588 "net rpc share\n"
4589 " List shares\n"
4590 " Alias for net rpc share list\n");
4591 net_display_usage_from_functable(func);
4592 return 0;
4595 return rpc_share_list(c, argc, argv);
4598 return net_run_function(c, argc, argv, "net rpc share", func);
4601 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4602 TALLOC_CTX *mem_ctx,
4603 struct rpc_sh_ctx *ctx,
4604 struct rpc_pipe_client *pipe_hnd,
4605 int argc, const char **argv)
4608 return werror_to_ntstatus(W_ERROR(rpc_share_list(c, argc, argv)));
4611 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4612 TALLOC_CTX *mem_ctx,
4613 struct rpc_sh_ctx *ctx,
4614 struct rpc_pipe_client *pipe_hnd,
4615 int argc, const char **argv)
4617 NET_API_STATUS status;
4618 uint32_t parm_err = 0;
4619 struct SHARE_INFO_2 i2;
4621 if ((argc < 2) || (argc > 3)) {
4622 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4623 ctx->whoami);
4624 return NT_STATUS_INVALID_PARAMETER;
4627 i2.shi2_netname = argv[0];
4628 i2.shi2_type = STYPE_DISKTREE;
4629 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4630 i2.shi2_permissions = 0;
4631 i2.shi2_max_uses = 0;
4632 i2.shi2_current_uses = 0;
4633 i2.shi2_path = argv[1];
4634 i2.shi2_passwd = NULL;
4636 status = NetShareAdd(pipe_hnd->desthost,
4638 (uint8_t *)&i2,
4639 &parm_err);
4641 return werror_to_ntstatus(W_ERROR(status));
4644 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4645 TALLOC_CTX *mem_ctx,
4646 struct rpc_sh_ctx *ctx,
4647 struct rpc_pipe_client *pipe_hnd,
4648 int argc, const char **argv)
4650 if (argc != 1) {
4651 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4652 return NT_STATUS_INVALID_PARAMETER;
4655 return werror_to_ntstatus(W_ERROR(NetShareDel(pipe_hnd->desthost, argv[0], 0)));
4658 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4659 TALLOC_CTX *mem_ctx,
4660 struct rpc_sh_ctx *ctx,
4661 struct rpc_pipe_client *pipe_hnd,
4662 int argc, const char **argv)
4664 union srvsvc_NetShareInfo info;
4665 WERROR result;
4666 NTSTATUS status;
4668 if (argc != 1) {
4669 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4670 return NT_STATUS_INVALID_PARAMETER;
4673 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4674 pipe_hnd->desthost,
4675 argv[0],
4677 &info,
4678 &result);
4679 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4680 goto done;
4683 d_printf("Name: %s\n", info.info2->name);
4684 d_printf("Comment: %s\n", info.info2->comment);
4685 d_printf("Path: %s\n", info.info2->path);
4686 d_printf("Password: %s\n", info.info2->password);
4688 done:
4689 return werror_to_ntstatus(result);
4692 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4693 struct rpc_sh_ctx *ctx)
4695 static struct rpc_sh_cmd cmds[] = {
4697 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4698 "List available shares" },
4700 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4701 "Add a share" },
4703 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4704 "Delete a share" },
4706 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4707 "Get information about a share" },
4709 { NULL, NULL, 0, NULL, NULL }
4712 return cmds;
4715 /****************************************************************************/
4717 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4719 return net_file_usage(c, argc, argv);
4723 * Close a file on a remote RPC server.
4725 * @param argc Standard main() style argc.
4726 * @param argv Standard main() style argv. Initial components are already
4727 * stripped.
4729 * @return A shell status integer (0 for success).
4731 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4733 if (argc < 1 || c->display_usage) {
4734 return rpc_file_usage(c, argc, argv);
4737 return NetFileClose(c->opt_host, atoi(argv[0]));
4741 * Formatted print of open file info
4743 * @param r struct srvsvc_NetFileInfo3 contents
4746 static void display_file_info_3(struct srvsvc_NetFileInfo3 *r)
4748 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4749 r->fid, r->user, r->permissions, r->num_locks, r->path);
4753 * List open files on a remote RPC server.
4755 * All parameters are provided by the run_rpc_command function, except for
4756 * argc, argv which are passed through.
4758 * @param c A net_context structure.
4759 * @param domain_sid The domain sid acquired from the remote server.
4760 * @param cli A cli_state connected to the server.
4761 * @param mem_ctx Talloc context, destroyed on completion of the function.
4762 * @param argc Standard main() style argc.
4763 * @param argv Standard main() style argv. Initial components are already
4764 * stripped.
4766 * @return Normal NTSTATUS return.
4769 static NTSTATUS rpc_file_list_internals(struct net_context *c,
4770 const DOM_SID *domain_sid,
4771 const char *domain_name,
4772 struct cli_state *cli,
4773 struct rpc_pipe_client *pipe_hnd,
4774 TALLOC_CTX *mem_ctx,
4775 int argc,
4776 const char **argv)
4778 struct srvsvc_NetFileInfoCtr info_ctr;
4779 struct srvsvc_NetFileCtr3 ctr3;
4780 WERROR result;
4781 NTSTATUS status;
4782 uint32 preferred_len = 0xffffffff, i;
4783 const char *username=NULL;
4784 uint32_t total_entries = 0;
4785 uint32_t resume_handle = 0;
4787 /* if argc > 0, must be user command */
4788 if (argc > 0)
4789 username = smb_xstrdup(argv[0]);
4791 ZERO_STRUCT(info_ctr);
4792 ZERO_STRUCT(ctr3);
4794 info_ctr.level = 3;
4795 info_ctr.ctr.ctr3 = &ctr3;
4797 status = rpccli_srvsvc_NetFileEnum(pipe_hnd, mem_ctx,
4798 pipe_hnd->desthost,
4799 NULL,
4800 username,
4801 &info_ctr,
4802 preferred_len,
4803 &total_entries,
4804 &resume_handle,
4805 &result);
4807 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
4808 goto done;
4810 /* Display results */
4812 d_printf(
4813 "\nEnumerating open files on remote server:\n\n"
4814 "\nFileId Opened by Perms Locks Path"
4815 "\n------ --------- ----- ----- ---- \n");
4816 for (i = 0; i < total_entries; i++)
4817 display_file_info_3(&info_ctr.ctr.ctr3->array[i]);
4818 done:
4819 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4823 * List files for a user on a remote RPC server.
4825 * @param argc Standard main() style argc.
4826 * @param argv Standard main() style argv. Initial components are already
4827 * stripped.
4829 * @return A shell status integer (0 for success)..
4832 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4834 if (argc < 1 || c->display_usage) {
4835 return rpc_file_usage(c, argc, argv);
4838 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4839 rpc_file_list_internals,
4840 argc, argv);
4844 * 'net rpc file' entrypoint.
4845 * @param argc Standard main() style argc.
4846 * @param argv Standard main() style argv. Initial components are already
4847 * stripped.
4850 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4852 NET_API_STATUS status;
4854 struct functable func[] = {
4856 "close",
4857 rpc_file_close,
4858 NET_TRANSPORT_RPC,
4859 "Close opened file",
4860 "net rpc file close\n"
4861 " Close opened file"
4864 "user",
4865 rpc_file_user,
4866 NET_TRANSPORT_RPC,
4867 "List files opened by user",
4868 "net rpc file user\n"
4869 " List files opened by user"
4871 #if 0
4873 "info",
4874 rpc_file_info,
4875 NET_TRANSPORT_RPC,
4876 "Display information about opened file",
4877 "net rpc file info\n"
4878 " Display information about opened file"
4880 #endif
4881 {NULL, NULL, 0, NULL, NULL}
4884 status = libnetapi_init(&c->netapi_ctx);
4885 if (status != 0) {
4886 return -1;
4888 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4889 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4890 if (c->opt_kerberos) {
4891 libnetapi_set_use_kerberos(c->netapi_ctx);
4894 if (argc == 0) {
4895 if (c->display_usage) {
4896 d_printf("Usage:\n");
4897 d_printf("net rpc file\n"
4898 " List opened files\n");
4899 net_display_usage_from_functable(func);
4900 return 0;
4903 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4904 rpc_file_list_internals,
4905 argc, argv);
4908 return net_run_function(c, argc, argv, "net rpc file", func);
4912 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
4914 * All parameters are provided by the run_rpc_command function, except for
4915 * argc, argv which are passed through.
4917 * @param c A net_context structure.
4918 * @param domain_sid The domain sid acquired from the remote server.
4919 * @param cli A cli_state connected to the server.
4920 * @param mem_ctx Talloc context, destroyed on completion of the function.
4921 * @param argc Standard main() style argc.
4922 * @param argv Standard main() style argv. Initial components are already
4923 * stripped.
4925 * @return Normal NTSTATUS return.
4928 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
4929 const DOM_SID *domain_sid,
4930 const char *domain_name,
4931 struct cli_state *cli,
4932 struct rpc_pipe_client *pipe_hnd,
4933 TALLOC_CTX *mem_ctx,
4934 int argc,
4935 const char **argv)
4937 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4939 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
4941 if (NT_STATUS_IS_OK(result)) {
4942 d_printf("\nShutdown successfully aborted\n");
4943 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
4944 } else
4945 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
4947 return result;
4951 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
4953 * All parameters are provided by the run_rpc_command function, except for
4954 * argc, argv which are passed through.
4956 * @param c A net_context structure.
4957 * @param domain_sid The domain sid acquired from the remote server.
4958 * @param cli A cli_state connected to the server.
4959 * @param mem_ctx Talloc context, destroyed on completion of the function.
4960 * @param argc Standard main() style argc.
4961 * @param argv Standard main() style argv. Initial components are already
4962 * stripped.
4964 * @return Normal NTSTATUS return.
4967 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
4968 const DOM_SID *domain_sid,
4969 const char *domain_name,
4970 struct cli_state *cli,
4971 struct rpc_pipe_client *pipe_hnd,
4972 TALLOC_CTX *mem_ctx,
4973 int argc,
4974 const char **argv)
4976 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
4978 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
4980 if (NT_STATUS_IS_OK(result)) {
4981 d_printf("\nShutdown successfully aborted\n");
4982 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
4983 } else
4984 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
4986 return result;
4990 * ABORT the shutdown of a remote RPC server.
4992 * @param argc Standard main() style argc.
4993 * @param argv Standard main() style argv. Initial components are already
4994 * stripped.
4996 * @return A shell status integer (0 for success).
4999 static int rpc_shutdown_abort(struct net_context *c, int argc,
5000 const char **argv)
5002 int rc = -1;
5004 if (c->display_usage) {
5005 d_printf("Usage:\n"
5006 "net rpc abortshutdown\n"
5007 " Abort a scheduled shutdown\n");
5008 return 0;
5011 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5012 rpc_shutdown_abort_internals, argc, argv);
5014 if (rc == 0)
5015 return rc;
5017 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5019 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5020 rpc_reg_shutdown_abort_internals,
5021 argc, argv);
5025 * Shut down a remote RPC Server via initshutdown pipe.
5027 * All parameters are provided by the run_rpc_command function, except for
5028 * argc, argv which are passed through.
5030 * @param c A net_context structure.
5031 * @param domain_sid The domain sid acquired from the remote server.
5032 * @param cli A cli_state connected to the server.
5033 * @param mem_ctx Talloc context, destroyed on completion of the function.
5034 * @param argc Standard main() style argc.
5035 * @param argv Standard main() style argv. Initial components are already
5036 * stripped.
5038 * @return Normal NTSTATUS return.
5041 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5042 const DOM_SID *domain_sid,
5043 const char *domain_name,
5044 struct cli_state *cli,
5045 struct rpc_pipe_client *pipe_hnd,
5046 TALLOC_CTX *mem_ctx,
5047 int argc,
5048 const char **argv)
5050 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5051 const char *msg = "This machine will be shutdown shortly";
5052 uint32 timeout = 20;
5053 struct initshutdown_String msg_string;
5054 struct initshutdown_String_sub s;
5056 if (c->opt_comment) {
5057 msg = c->opt_comment;
5059 if (c->opt_timeout) {
5060 timeout = c->opt_timeout;
5063 s.name = msg;
5064 msg_string.name = &s;
5066 /* create an entry */
5067 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5068 &msg_string, timeout, c->opt_force, c->opt_reboot,
5069 NULL);
5071 if (NT_STATUS_IS_OK(result)) {
5072 d_printf("\nShutdown of remote machine succeeded\n");
5073 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5074 } else {
5075 DEBUG(1,("Shutdown of remote machine failed!\n"));
5077 return result;
5081 * Shut down a remote RPC Server via winreg pipe.
5083 * All parameters are provided by the run_rpc_command function, except for
5084 * argc, argv which are passed through.
5086 * @param c A net_context structure.
5087 * @param domain_sid The domain sid acquired from the remote server.
5088 * @param cli A cli_state connected to the server.
5089 * @param mem_ctx Talloc context, destroyed on completion of the function.
5090 * @param argc Standard main() style argc.
5091 * @param argv Standard main() style argv. Initial components are already
5092 * stripped.
5094 * @return Normal NTSTATUS return.
5097 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5098 const DOM_SID *domain_sid,
5099 const char *domain_name,
5100 struct cli_state *cli,
5101 struct rpc_pipe_client *pipe_hnd,
5102 TALLOC_CTX *mem_ctx,
5103 int argc,
5104 const char **argv)
5106 const char *msg = "This machine will be shutdown shortly";
5107 uint32 timeout = 20;
5108 struct initshutdown_String msg_string;
5109 struct initshutdown_String_sub s;
5110 NTSTATUS result;
5111 WERROR werr;
5113 if (c->opt_comment) {
5114 msg = c->opt_comment;
5116 s.name = msg;
5117 msg_string.name = &s;
5119 if (c->opt_timeout) {
5120 timeout = c->opt_timeout;
5123 /* create an entry */
5124 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5125 &msg_string, timeout, c->opt_force, c->opt_reboot,
5126 &werr);
5128 if (NT_STATUS_IS_OK(result)) {
5129 d_printf("\nShutdown of remote machine succeeded\n");
5130 } else {
5131 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5132 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5133 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5134 else
5135 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(werr));
5138 return result;
5142 * Shut down a remote RPC server.
5144 * @param argc Standard main() style argc.
5145 * @param argv Standard main() style argv. Initial components are already
5146 * stripped.
5148 * @return A shell status integer (0 for success).
5151 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5153 int rc = -1;
5155 if (c->display_usage) {
5156 d_printf("Usage:\n"
5157 "net rpc shutdown\n"
5158 " Shut down a remote RPC server\n");
5159 return 0;
5162 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5163 rpc_init_shutdown_internals, argc, argv);
5165 if (rc) {
5166 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5167 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5168 rpc_reg_shutdown_internals, argc, argv);
5171 return rc;
5174 /***************************************************************************
5175 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5176 ***************************************************************************/
5179 * Add interdomain trust account to the RPC server.
5180 * All parameters (except for argc and argv) are passed by run_rpc_command
5181 * function.
5183 * @param c A net_context structure.
5184 * @param domain_sid The domain sid acquired from the server.
5185 * @param cli A cli_state connected to the server.
5186 * @param mem_ctx Talloc context, destroyed on completion of the function.
5187 * @param argc Standard main() style argc.
5188 * @param argv Standard main() style argv. Initial components are already
5189 * stripped.
5191 * @return normal NTSTATUS return code.
5194 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5195 const DOM_SID *domain_sid,
5196 const char *domain_name,
5197 struct cli_state *cli,
5198 struct rpc_pipe_client *pipe_hnd,
5199 TALLOC_CTX *mem_ctx,
5200 int argc,
5201 const char **argv)
5203 POLICY_HND connect_pol, domain_pol, user_pol;
5204 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5205 char *acct_name;
5206 struct lsa_String lsa_acct_name;
5207 uint32 acb_info;
5208 uint32 acct_flags=0;
5209 uint32 user_rid;
5210 uint32_t access_granted = 0;
5211 union samr_UserInfo info;
5212 unsigned int orig_timeout;
5214 if (argc != 2) {
5215 d_printf("Usage: net rpc trustdom add <domain_name> "
5216 "<trust password>\n");
5217 return NT_STATUS_INVALID_PARAMETER;
5221 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5224 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5225 return NT_STATUS_NO_MEMORY;
5228 strupper_m(acct_name);
5230 init_lsa_String(&lsa_acct_name, acct_name);
5232 /* Get samr policy handle */
5233 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5234 pipe_hnd->desthost,
5235 MAXIMUM_ALLOWED_ACCESS,
5236 &connect_pol);
5237 if (!NT_STATUS_IS_OK(result)) {
5238 goto done;
5241 /* Get domain policy handle */
5242 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5243 &connect_pol,
5244 MAXIMUM_ALLOWED_ACCESS,
5245 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5246 &domain_pol);
5247 if (!NT_STATUS_IS_OK(result)) {
5248 goto done;
5251 /* This call can take a long time - allow the server to time out.
5252 * 35 seconds should do it. */
5254 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5256 /* Create trusting domain's account */
5257 acb_info = ACB_NORMAL;
5258 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5259 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5260 SAMR_USER_ACCESS_SET_PASSWORD |
5261 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5262 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5264 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5265 &domain_pol,
5266 &lsa_acct_name,
5267 acb_info,
5268 acct_flags,
5269 &user_pol,
5270 &access_granted,
5271 &user_rid);
5273 /* And restore our original timeout. */
5274 rpccli_set_timeout(pipe_hnd, orig_timeout);
5276 if (!NT_STATUS_IS_OK(result)) {
5277 d_printf("net rpc trustdom add: create user %s failed %s\n",
5278 acct_name, nt_errstr(result));
5279 goto done;
5283 NTTIME notime;
5284 struct samr_LogonHours hours;
5285 struct lsa_BinaryString parameters;
5286 const int units_per_week = 168;
5287 struct samr_CryptPassword crypt_pwd;
5289 ZERO_STRUCT(notime);
5290 ZERO_STRUCT(hours);
5291 ZERO_STRUCT(parameters);
5293 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
5294 if (!hours.bits) {
5295 result = NT_STATUS_NO_MEMORY;
5296 goto done;
5298 hours.units_per_week = units_per_week;
5299 memset(hours.bits, 0xFF, units_per_week);
5301 init_samr_CryptPassword(argv[1],
5302 &cli->user_session_key,
5303 &crypt_pwd);
5305 init_samr_user_info23(&info.info23,
5306 notime, notime, notime,
5307 notime, notime, notime,
5308 NULL, NULL, NULL, NULL, NULL,
5309 NULL, NULL, NULL, NULL, &parameters,
5310 0, 0, ACB_DOMTRUST, SAMR_FIELD_ACCT_FLAGS,
5311 hours,
5312 0, 0, 0, 0, 0, 0, 0,
5313 crypt_pwd.data, 24);
5315 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5316 &user_pol,
5318 &info);
5320 if (!NT_STATUS_IS_OK(result)) {
5321 DEBUG(0,("Could not set trust account password: %s\n",
5322 nt_errstr(result)));
5323 goto done;
5327 done:
5328 SAFE_FREE(acct_name);
5329 return result;
5333 * Create interdomain trust account for a remote domain.
5335 * @param argc Standard argc.
5336 * @param argv Standard argv without initial components.
5338 * @return Integer status (0 means success).
5341 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5343 if (argc > 0 && !c->display_usage) {
5344 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5345 rpc_trustdom_add_internals, argc, argv);
5346 } else {
5347 d_printf("Usage:\n"
5348 "net rpc trustdom add <domain_name> <trust password>\n");
5349 return -1;
5355 * Remove interdomain trust account from the RPC server.
5356 * All parameters (except for argc and argv) are passed by run_rpc_command
5357 * function.
5359 * @param c A net_context structure.
5360 * @param domain_sid The domain sid acquired from the server.
5361 * @param cli A cli_state connected to the server.
5362 * @param mem_ctx Talloc context, destroyed on completion of the function.
5363 * @param argc Standard main() style argc.
5364 * @param argv Standard main() style argv. Initial components are already
5365 * stripped.
5367 * @return normal NTSTATUS return code.
5370 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5371 const DOM_SID *domain_sid,
5372 const char *domain_name,
5373 struct cli_state *cli,
5374 struct rpc_pipe_client *pipe_hnd,
5375 TALLOC_CTX *mem_ctx,
5376 int argc,
5377 const char **argv)
5379 POLICY_HND connect_pol, domain_pol, user_pol;
5380 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5381 char *acct_name;
5382 DOM_SID trust_acct_sid;
5383 struct samr_Ids user_rids, name_types;
5384 struct lsa_String lsa_acct_name;
5386 if (argc != 1) {
5387 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5388 return NT_STATUS_INVALID_PARAMETER;
5392 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5394 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5396 if (acct_name == NULL)
5397 return NT_STATUS_NO_MEMORY;
5399 strupper_m(acct_name);
5401 /* Get samr policy handle */
5402 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5403 pipe_hnd->desthost,
5404 MAXIMUM_ALLOWED_ACCESS,
5405 &connect_pol);
5406 if (!NT_STATUS_IS_OK(result)) {
5407 goto done;
5410 /* Get domain policy handle */
5411 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5412 &connect_pol,
5413 MAXIMUM_ALLOWED_ACCESS,
5414 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5415 &domain_pol);
5416 if (!NT_STATUS_IS_OK(result)) {
5417 goto done;
5420 init_lsa_String(&lsa_acct_name, acct_name);
5422 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5423 &domain_pol,
5425 &lsa_acct_name,
5426 &user_rids,
5427 &name_types);
5429 if (!NT_STATUS_IS_OK(result)) {
5430 d_printf("net rpc trustdom del: LookupNames on user %s failed %s\n",
5431 acct_name, nt_errstr(result) );
5432 goto done;
5435 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5436 &domain_pol,
5437 MAXIMUM_ALLOWED_ACCESS,
5438 user_rids.ids[0],
5439 &user_pol);
5441 if (!NT_STATUS_IS_OK(result)) {
5442 d_printf("net rpc trustdom del: OpenUser on user %s failed %s\n",
5443 acct_name, nt_errstr(result) );
5444 goto done;
5447 /* append the rid to the domain sid */
5448 sid_copy(&trust_acct_sid, domain_sid);
5449 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5450 goto done;
5453 /* remove the sid */
5455 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5456 &user_pol,
5457 &trust_acct_sid);
5458 if (!NT_STATUS_IS_OK(result)) {
5459 d_printf("net rpc trustdom del: RemoveMemberFromForeignDomain on user %s failed %s\n",
5460 acct_name, nt_errstr(result) );
5461 goto done;
5464 /* Delete user */
5466 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5467 &user_pol);
5469 if (!NT_STATUS_IS_OK(result)) {
5470 d_printf("net rpc trustdom del: DeleteUser on user %s failed %s\n",
5471 acct_name, nt_errstr(result) );
5472 goto done;
5475 if (!NT_STATUS_IS_OK(result)) {
5476 d_printf("Could not set trust account password: %s\n",
5477 nt_errstr(result));
5478 goto done;
5481 done:
5482 return result;
5486 * Delete interdomain trust account for a remote domain.
5488 * @param argc Standard argc.
5489 * @param argv Standard argv without initial components.
5491 * @return Integer status (0 means success).
5494 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5496 if (argc > 0 && !c->display_usage) {
5497 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5498 rpc_trustdom_del_internals, argc, argv);
5499 } else {
5500 d_printf("Usage:\n"
5501 "net rpc trustdom del <domain>\n");
5502 return -1;
5506 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5507 struct cli_state *cli,
5508 TALLOC_CTX *mem_ctx,
5509 const char *domain_name)
5511 char *dc_name = NULL;
5512 const char *buffer = NULL;
5513 struct rpc_pipe_client *netr;
5514 NTSTATUS status;
5516 /* Use NetServerEnum2 */
5518 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5519 SAFE_FREE(dc_name);
5520 return NT_STATUS_OK;
5523 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5524 for domain %s\n", domain_name));
5526 /* Try netr_GetDcName */
5528 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5529 &netr);
5530 if (!NT_STATUS_IS_OK(status)) {
5531 return status;
5534 status = rpccli_netr_GetDcName(netr, mem_ctx,
5535 cli->desthost,
5536 domain_name,
5537 &buffer,
5538 NULL);
5539 TALLOC_FREE(netr);
5541 if (NT_STATUS_IS_OK(status)) {
5542 return status;
5545 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5546 for domain %s\n", domain_name));
5548 return status;
5552 * Establish trust relationship to a trusting domain.
5553 * Interdomain account must already be created on remote PDC.
5555 * @param c A net_context structure.
5556 * @param argc Standard argc.
5557 * @param argv Standard argv without initial components.
5559 * @return Integer status (0 means success).
5562 static int rpc_trustdom_establish(struct net_context *c, int argc,
5563 const char **argv)
5565 struct cli_state *cli = NULL;
5566 struct sockaddr_storage server_ss;
5567 struct rpc_pipe_client *pipe_hnd = NULL;
5568 POLICY_HND connect_hnd;
5569 TALLOC_CTX *mem_ctx;
5570 NTSTATUS nt_status;
5571 DOM_SID *domain_sid;
5573 char* domain_name;
5574 char* acct_name;
5575 fstring pdc_name;
5576 union lsa_PolicyInformation *info = NULL;
5579 * Connect to \\server\ipc$ as 'our domain' account with password
5582 if (argc != 1 || c->display_usage) {
5583 d_printf("Usage:\n"
5584 "net rpc trustdom establish <domain_name>\n");
5585 return -1;
5588 domain_name = smb_xstrdup(argv[0]);
5589 strupper_m(domain_name);
5591 /* account name used at first is our domain's name with '$' */
5592 asprintf(&acct_name, "%s$", lp_workgroup());
5593 strupper_m(acct_name);
5596 * opt_workgroup will be used by connection functions further,
5597 * hence it should be set to remote domain name instead of ours
5599 if (c->opt_workgroup) {
5600 c->opt_workgroup = smb_xstrdup(domain_name);
5603 c->opt_user_name = acct_name;
5605 /* find the domain controller */
5606 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5607 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5608 return -1;
5611 /* connect to ipc$ as username/password */
5612 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5613 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5615 /* Is it trusting domain account for sure ? */
5616 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5617 nt_errstr(nt_status)));
5618 return -1;
5621 /* store who we connected to */
5623 saf_store( domain_name, pdc_name );
5626 * Connect to \\server\ipc$ again (this time anonymously)
5629 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5630 (char*)pdc_name);
5632 if (NT_STATUS_IS_ERR(nt_status)) {
5633 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5634 domain_name, nt_errstr(nt_status)));
5635 return -1;
5638 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5639 "domain %s", domain_name))) {
5640 DEBUG(0, ("talloc_init() failed\n"));
5641 cli_shutdown(cli);
5642 return -1;
5645 /* Make sure we're talking to a proper server */
5647 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5648 if (!NT_STATUS_IS_OK(nt_status)) {
5649 cli_shutdown(cli);
5650 talloc_destroy(mem_ctx);
5651 return -1;
5655 * Call LsaOpenPolicy and LsaQueryInfo
5658 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5659 &pipe_hnd);
5660 if (!NT_STATUS_IS_OK(nt_status)) {
5661 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5662 cli_shutdown(cli);
5663 talloc_destroy(mem_ctx);
5664 return -1;
5667 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, SEC_RIGHTS_QUERY_VALUE,
5668 &connect_hnd);
5669 if (NT_STATUS_IS_ERR(nt_status)) {
5670 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5671 nt_errstr(nt_status)));
5672 cli_shutdown(cli);
5673 talloc_destroy(mem_ctx);
5674 return -1;
5677 /* Querying info level 5 */
5679 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5680 &connect_hnd,
5681 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5682 &info);
5683 if (NT_STATUS_IS_ERR(nt_status)) {
5684 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5685 nt_errstr(nt_status)));
5686 cli_shutdown(cli);
5687 talloc_destroy(mem_ctx);
5688 return -1;
5691 domain_sid = info->account_domain.sid;
5693 /* There should be actually query info level 3 (following nt serv behaviour),
5694 but I still don't know if it's _really_ necessary */
5697 * Store the password in secrets db
5700 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5701 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5702 cli_shutdown(cli);
5703 talloc_destroy(mem_ctx);
5704 return -1;
5708 * Close the pipes and clean up
5711 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5712 if (NT_STATUS_IS_ERR(nt_status)) {
5713 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5714 nt_errstr(nt_status)));
5715 cli_shutdown(cli);
5716 talloc_destroy(mem_ctx);
5717 return -1;
5720 cli_shutdown(cli);
5722 talloc_destroy(mem_ctx);
5724 d_printf("Trust to domain %s established\n", domain_name);
5725 return 0;
5729 * Revoke trust relationship to the remote domain.
5731 * @param c A net_context structure.
5732 * @param argc Standard argc.
5733 * @param argv Standard argv without initial components.
5735 * @return Integer status (0 means success).
5738 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5739 const char **argv)
5741 char* domain_name;
5742 int rc = -1;
5744 if (argc < 1 || c->display_usage) {
5745 d_printf("Usage:\n"
5746 "net rpc trustdom revoke <domain_name>\n"
5747 " Revoke trust relationship\n"
5748 " domain_name\tName of domain to revoke trust\n");
5749 return -1;
5752 /* generate upper cased domain name */
5753 domain_name = smb_xstrdup(argv[0]);
5754 strupper_m(domain_name);
5756 /* delete password of the trust */
5757 if (!pdb_del_trusteddom_pw(domain_name)) {
5758 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5759 domain_name));
5760 goto done;
5763 rc = 0;
5764 done:
5765 SAFE_FREE(domain_name);
5766 return rc;
5769 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5770 const DOM_SID *domain_sid,
5771 const char *domain_name,
5772 struct cli_state *cli,
5773 struct rpc_pipe_client *pipe_hnd,
5774 TALLOC_CTX *mem_ctx,
5775 int argc,
5776 const char **argv)
5778 fstring str_sid;
5779 sid_to_fstring(str_sid, domain_sid);
5780 d_printf("%s\n", str_sid);
5781 return NT_STATUS_OK;
5784 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5786 fstring ascii_sid, padding;
5787 int pad_len, col_len = 20;
5789 /* convert sid into ascii string */
5790 sid_to_fstring(ascii_sid, dom_sid);
5792 /* calculate padding space for d_printf to look nicer */
5793 pad_len = col_len - strlen(trusted_dom_name);
5794 padding[pad_len] = 0;
5795 do padding[--pad_len] = ' '; while (pad_len);
5797 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5800 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5801 TALLOC_CTX *mem_ctx,
5802 POLICY_HND *pol,
5803 DOM_SID dom_sid,
5804 const char *trusted_dom_name)
5806 NTSTATUS nt_status;
5807 union lsa_TrustedDomainInfo *info = NULL;
5808 char *cleartextpwd = NULL;
5809 uint8_t nt_hash[16];
5810 DATA_BLOB data;
5812 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5813 pol,
5814 &dom_sid,
5815 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5816 &info);
5817 if (NT_STATUS_IS_ERR(nt_status)) {
5818 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5819 nt_errstr(nt_status)));
5820 goto done;
5823 data = data_blob(info->password.password->data,
5824 info->password.password->length);
5826 if (!rpccli_get_pwd_hash(pipe_hnd, nt_hash)) {
5827 DEBUG(0, ("Could not retrieve password hash\n"));
5828 goto done;
5831 cleartextpwd = decrypt_trustdom_secret(nt_hash, &data);
5833 if (cleartextpwd == NULL) {
5834 DEBUG(0,("retrieved NULL password\n"));
5835 nt_status = NT_STATUS_UNSUCCESSFUL;
5836 goto done;
5839 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5840 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5841 nt_status = NT_STATUS_UNSUCCESSFUL;
5842 goto done;
5845 #ifdef DEBUG_PASSWORD
5846 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5847 "password: [%s]\n", trusted_dom_name,
5848 sid_string_dbg(&dom_sid), cleartextpwd));
5849 #endif
5851 done:
5852 SAFE_FREE(cleartextpwd);
5853 data_blob_free(&data);
5855 return nt_status;
5858 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5859 const char **argv)
5861 /* common variables */
5862 TALLOC_CTX* mem_ctx;
5863 struct cli_state *cli = NULL;
5864 struct rpc_pipe_client *pipe_hnd = NULL;
5865 NTSTATUS nt_status;
5866 const char *domain_name = NULL;
5867 DOM_SID *queried_dom_sid;
5868 POLICY_HND connect_hnd;
5869 union lsa_PolicyInformation *info = NULL;
5871 /* trusted domains listing variables */
5872 unsigned int enum_ctx = 0;
5873 int i;
5874 struct lsa_DomainList dom_list;
5875 fstring pdc_name;
5877 if (c->display_usage) {
5878 d_printf("Usage:\n"
5879 "net rpc trustdom vampire\n"
5880 " Vampire trust relationship from remote server\n");
5881 return 0;
5885 * Listing trusted domains (stored in secrets.tdb, if local)
5888 mem_ctx = talloc_init("trust relationships vampire");
5891 * set domain and pdc name to local samba server (default)
5892 * or to remote one given in command line
5895 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
5896 domain_name = c->opt_workgroup;
5897 c->opt_target_workgroup = c->opt_workgroup;
5898 } else {
5899 fstrcpy(pdc_name, global_myname());
5900 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5901 c->opt_target_workgroup = domain_name;
5904 /* open \PIPE\lsarpc and open policy handle */
5905 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
5906 if (!NT_STATUS_IS_OK(nt_status)) {
5907 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
5908 nt_errstr(nt_status)));
5909 talloc_destroy(mem_ctx);
5910 return -1;
5913 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5914 &pipe_hnd);
5915 if (!NT_STATUS_IS_OK(nt_status)) {
5916 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
5917 nt_errstr(nt_status) ));
5918 cli_shutdown(cli);
5919 talloc_destroy(mem_ctx);
5920 return -1;
5923 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
5924 &connect_hnd);
5925 if (NT_STATUS_IS_ERR(nt_status)) {
5926 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5927 nt_errstr(nt_status)));
5928 cli_shutdown(cli);
5929 talloc_destroy(mem_ctx);
5930 return -1;
5933 /* query info level 5 to obtain sid of a domain being queried */
5934 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5935 &connect_hnd,
5936 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5937 &info);
5939 if (NT_STATUS_IS_ERR(nt_status)) {
5940 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5941 nt_errstr(nt_status)));
5942 cli_shutdown(cli);
5943 talloc_destroy(mem_ctx);
5944 return -1;
5947 queried_dom_sid = info->account_domain.sid;
5950 * Keep calling LsaEnumTrustdom over opened pipe until
5951 * the end of enumeration is reached
5954 d_printf("Vampire trusted domains:\n\n");
5956 do {
5957 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
5958 &connect_hnd,
5959 &enum_ctx,
5960 &dom_list,
5961 (uint32_t)-1);
5962 if (NT_STATUS_IS_ERR(nt_status)) {
5963 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
5964 nt_errstr(nt_status)));
5965 cli_shutdown(cli);
5966 talloc_destroy(mem_ctx);
5967 return -1;
5970 for (i = 0; i < dom_list.count; i++) {
5972 print_trusted_domain(dom_list.domains[i].sid,
5973 dom_list.domains[i].name.string);
5975 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
5976 *dom_list.domains[i].sid,
5977 dom_list.domains[i].name.string);
5978 if (!NT_STATUS_IS_OK(nt_status)) {
5979 cli_shutdown(cli);
5980 talloc_destroy(mem_ctx);
5981 return -1;
5986 * in case of no trusted domains say something rather
5987 * than just display blank line
5989 if (!dom_list.count) d_printf("none\n");
5991 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
5993 /* close this connection before doing next one */
5994 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5995 if (NT_STATUS_IS_ERR(nt_status)) {
5996 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
5997 nt_errstr(nt_status)));
5998 cli_shutdown(cli);
5999 talloc_destroy(mem_ctx);
6000 return -1;
6003 /* close lsarpc pipe and connection to IPC$ */
6004 cli_shutdown(cli);
6006 talloc_destroy(mem_ctx);
6007 return 0;
6010 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6012 /* common variables */
6013 TALLOC_CTX* mem_ctx;
6014 struct cli_state *cli = NULL, *remote_cli = NULL;
6015 struct rpc_pipe_client *pipe_hnd = NULL;
6016 NTSTATUS nt_status;
6017 const char *domain_name = NULL;
6018 DOM_SID *queried_dom_sid;
6019 fstring padding;
6020 int ascii_dom_name_len;
6021 POLICY_HND connect_hnd;
6022 union lsa_PolicyInformation *info = NULL;
6024 /* trusted domains listing variables */
6025 unsigned int num_domains, enum_ctx = 0;
6026 int i, pad_len, col_len = 20;
6027 struct lsa_DomainList dom_list;
6028 fstring pdc_name;
6030 /* trusting domains listing variables */
6031 POLICY_HND domain_hnd;
6032 struct samr_SamArray *trusts = NULL;
6034 if (c->display_usage) {
6035 d_printf("Usage:\n"
6036 "net rpc trustdom list\n"
6037 " List trust relationships\n");
6038 return 0;
6042 * Listing trusted domains (stored in secrets.tdb, if local)
6045 mem_ctx = talloc_init("trust relationships listing");
6048 * set domain and pdc name to local samba server (default)
6049 * or to remote one given in command line
6052 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6053 domain_name = c->opt_workgroup;
6054 c->opt_target_workgroup = c->opt_workgroup;
6055 } else {
6056 fstrcpy(pdc_name, global_myname());
6057 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6058 c->opt_target_workgroup = domain_name;
6061 /* open \PIPE\lsarpc and open policy handle */
6062 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6063 if (!NT_STATUS_IS_OK(nt_status)) {
6064 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6065 nt_errstr(nt_status)));
6066 talloc_destroy(mem_ctx);
6067 return -1;
6070 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6071 &pipe_hnd);
6072 if (!NT_STATUS_IS_OK(nt_status)) {
6073 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6074 nt_errstr(nt_status) ));
6075 cli_shutdown(cli);
6076 talloc_destroy(mem_ctx);
6077 return -1;
6080 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6081 &connect_hnd);
6082 if (NT_STATUS_IS_ERR(nt_status)) {
6083 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6084 nt_errstr(nt_status)));
6085 cli_shutdown(cli);
6086 talloc_destroy(mem_ctx);
6087 return -1;
6090 /* query info level 5 to obtain sid of a domain being queried */
6091 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6092 &connect_hnd,
6093 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6094 &info);
6096 if (NT_STATUS_IS_ERR(nt_status)) {
6097 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6098 nt_errstr(nt_status)));
6099 cli_shutdown(cli);
6100 talloc_destroy(mem_ctx);
6101 return -1;
6104 queried_dom_sid = info->account_domain.sid;
6107 * Keep calling LsaEnumTrustdom over opened pipe until
6108 * the end of enumeration is reached
6111 d_printf("Trusted domains list:\n\n");
6113 do {
6114 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6115 &connect_hnd,
6116 &enum_ctx,
6117 &dom_list,
6118 (uint32_t)-1);
6119 if (NT_STATUS_IS_ERR(nt_status)) {
6120 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6121 nt_errstr(nt_status)));
6122 cli_shutdown(cli);
6123 talloc_destroy(mem_ctx);
6124 return -1;
6127 for (i = 0; i < dom_list.count; i++) {
6128 print_trusted_domain(dom_list.domains[i].sid,
6129 dom_list.domains[i].name.string);
6133 * in case of no trusted domains say something rather
6134 * than just display blank line
6136 if (!dom_list.count) d_printf("none\n");
6138 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6140 /* close this connection before doing next one */
6141 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6142 if (NT_STATUS_IS_ERR(nt_status)) {
6143 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6144 nt_errstr(nt_status)));
6145 cli_shutdown(cli);
6146 talloc_destroy(mem_ctx);
6147 return -1;
6150 TALLOC_FREE(pipe_hnd);
6153 * Listing trusting domains (stored in passdb backend, if local)
6156 d_printf("\nTrusting domains list:\n\n");
6159 * Open \PIPE\samr and get needed policy handles
6161 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6162 &pipe_hnd);
6163 if (!NT_STATUS_IS_OK(nt_status)) {
6164 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6165 cli_shutdown(cli);
6166 talloc_destroy(mem_ctx);
6167 return -1;
6170 /* SamrConnect2 */
6171 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6172 pipe_hnd->desthost,
6173 SA_RIGHT_SAM_OPEN_DOMAIN,
6174 &connect_hnd);
6175 if (!NT_STATUS_IS_OK(nt_status)) {
6176 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6177 nt_errstr(nt_status)));
6178 cli_shutdown(cli);
6179 talloc_destroy(mem_ctx);
6180 return -1;
6183 /* SamrOpenDomain - we have to open domain policy handle in order to be
6184 able to enumerate accounts*/
6185 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6186 &connect_hnd,
6187 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6188 queried_dom_sid,
6189 &domain_hnd);
6190 if (!NT_STATUS_IS_OK(nt_status)) {
6191 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6192 nt_errstr(nt_status)));
6193 cli_shutdown(cli);
6194 talloc_destroy(mem_ctx);
6195 return -1;
6199 * perform actual enumeration
6202 enum_ctx = 0; /* reset enumeration context from last enumeration */
6203 do {
6205 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6206 &domain_hnd,
6207 &enum_ctx,
6208 ACB_DOMTRUST,
6209 &trusts,
6210 0xffff,
6211 &num_domains);
6212 if (NT_STATUS_IS_ERR(nt_status)) {
6213 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6214 nt_errstr(nt_status)));
6215 cli_shutdown(cli);
6216 talloc_destroy(mem_ctx);
6217 return -1;
6220 for (i = 0; i < num_domains; i++) {
6222 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6225 * get each single domain's sid (do we _really_ need this ?):
6226 * 1) connect to domain's pdc
6227 * 2) query the pdc for domain's sid
6230 /* get rid of '$' tail */
6231 ascii_dom_name_len = strlen(str);
6232 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6233 str[ascii_dom_name_len - 1] = '\0';
6235 /* calculate padding space for d_printf to look nicer */
6236 pad_len = col_len - strlen(str);
6237 padding[pad_len] = 0;
6238 do padding[--pad_len] = ' '; while (pad_len);
6240 /* set opt_* variables to remote domain */
6241 strupper_m(str);
6242 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6243 c->opt_target_workgroup = c->opt_workgroup;
6245 d_printf("%s%s", str, padding);
6247 /* connect to remote domain controller */
6248 nt_status = net_make_ipc_connection(c,
6249 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6250 &remote_cli);
6251 if (NT_STATUS_IS_OK(nt_status)) {
6252 /* query for domain's sid */
6253 if (run_rpc_command(
6254 c, remote_cli,
6255 &ndr_table_lsarpc.syntax_id, 0,
6256 rpc_query_domain_sid, argc,
6257 argv))
6258 d_fprintf(stderr, "couldn't get domain's sid\n");
6260 cli_shutdown(remote_cli);
6262 } else {
6263 d_fprintf(stderr, "domain controller is not "
6264 "responding: %s\n",
6265 nt_errstr(nt_status));
6269 if (!num_domains) d_printf("none\n");
6271 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6273 /* close opened samr and domain policy handles */
6274 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6275 if (!NT_STATUS_IS_OK(nt_status)) {
6276 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6279 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6280 if (!NT_STATUS_IS_OK(nt_status)) {
6281 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6284 /* close samr pipe and connection to IPC$ */
6285 cli_shutdown(cli);
6287 talloc_destroy(mem_ctx);
6288 return 0;
6292 * Entrypoint for 'net rpc trustdom' code.
6294 * @param argc Standard argc.
6295 * @param argv Standard argv without initial components.
6297 * @return Integer status (0 means success).
6300 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6302 struct functable func[] = {
6304 "add",
6305 rpc_trustdom_add,
6306 NET_TRANSPORT_RPC,
6307 "Add trusted domain's account",
6308 "net rpc trustdom add\n"
6309 " Add trusted domain's account"
6312 "del",
6313 rpc_trustdom_del,
6314 NET_TRANSPORT_RPC,
6315 "Remove trusted domain's account",
6316 "net rpc trustdom del\n"
6317 " Remove trusted domain's account"
6320 "establish",
6321 rpc_trustdom_establish,
6322 NET_TRANSPORT_RPC,
6323 "Establish trust relationship",
6324 "net rpc trustdom establish\n"
6325 " Establish trust relationship"
6328 "revoke",
6329 rpc_trustdom_revoke,
6330 NET_TRANSPORT_RPC,
6331 "Revoke trust relationship",
6332 "net rpc trustdom revoke\n"
6333 " Revoke trust relationship"
6336 "list",
6337 rpc_trustdom_list,
6338 NET_TRANSPORT_RPC,
6339 "List domain trusts",
6340 "net rpc trustdom list\n"
6341 " List domain trusts"
6344 "vampire",
6345 rpc_trustdom_vampire,
6346 NET_TRANSPORT_RPC,
6347 "Vampire trusts from remote server",
6348 "net rpc trustdom vampire\n"
6349 " Vampire trusts from remote server"
6351 {NULL, NULL, 0, NULL, NULL}
6354 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6358 * Check if a server will take rpc commands
6359 * @param flags Type of server to connect to (PDC, DMB, localhost)
6360 * if the host is not explicitly specified
6361 * @return bool (true means rpc supported)
6363 bool net_rpc_check(struct net_context *c, unsigned flags)
6365 struct cli_state *cli;
6366 bool ret = false;
6367 struct sockaddr_storage server_ss;
6368 char *server_name = NULL;
6369 NTSTATUS status;
6371 /* flags (i.e. server type) may depend on command */
6372 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6373 return false;
6375 if ((cli = cli_initialise()) == NULL) {
6376 return false;
6379 status = cli_connect(cli, server_name, &server_ss);
6380 if (!NT_STATUS_IS_OK(status))
6381 goto done;
6382 if (!attempt_netbios_session_request(&cli, global_myname(),
6383 server_name, &server_ss))
6384 goto done;
6385 if (!cli_negprot(cli))
6386 goto done;
6387 if (cli->protocol < PROTOCOL_NT1)
6388 goto done;
6390 ret = true;
6391 done:
6392 cli_shutdown(cli);
6393 return ret;
6396 /* dump sam database via samsync rpc calls */
6397 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6398 if (c->display_usage) {
6399 d_printf("Usage:\n"
6400 "net rpc samdump\n"
6401 " Dump remote SAM database\n");
6402 return 0;
6405 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6406 NET_FLAGS_ANONYMOUS,
6407 rpc_samdump_internals, argc, argv);
6410 /* syncronise sam database via samsync rpc calls */
6411 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6413 struct functable func[] = {
6415 "ldif",
6416 rpc_vampire_ldif,
6417 NET_TRANSPORT_RPC,
6418 "Dump remote SAM database to ldif",
6419 "net rpc vampire ldif\n"
6420 " Dump remote SAM database to LDIF file or stdout"
6423 "keytab",
6424 rpc_vampire_keytab,
6425 NET_TRANSPORT_RPC,
6426 "Dump remote SAM database to Kerberos Keytab",
6427 "net rpc vampire keytab\n"
6428 " Dump remote SAM database to Kerberos keytab file"
6431 {NULL, NULL, 0, NULL, NULL}
6434 if (argc == 0) {
6435 if (c->display_usage) {
6436 d_printf("Usage:\n"
6437 "net rpc vampire\n"
6438 " Vampire remote SAM database\n");
6439 return 0;
6442 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6443 NET_FLAGS_ANONYMOUS,
6444 rpc_vampire_internals,
6445 argc, argv);
6448 return net_run_function(c, argc, argv, "net rpc vampire", func);
6452 * Migrate everything from a print server.
6454 * @param c A net_context structure.
6455 * @param argc Standard main() style argc.
6456 * @param argv Standard main() style argv. Initial components are already
6457 * stripped.
6459 * @return A shell status integer (0 for success).
6461 * The order is important !
6462 * To successfully add drivers the print queues have to exist !
6463 * Applying ACLs should be the last step, because you're easily locked out.
6466 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6467 const char **argv)
6469 int ret;
6471 if (c->display_usage) {
6472 d_printf("Usage:\n"
6473 "net rpc printer migrate all\n"
6474 " Migrate everything from a print server\n");
6475 return 0;
6478 if (!c->opt_host) {
6479 d_printf("no server to migrate\n");
6480 return -1;
6483 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6484 rpc_printer_migrate_printers_internals, argc,
6485 argv);
6486 if (ret)
6487 return ret;
6489 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6490 rpc_printer_migrate_drivers_internals, argc,
6491 argv);
6492 if (ret)
6493 return ret;
6495 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6496 rpc_printer_migrate_forms_internals, argc, argv);
6497 if (ret)
6498 return ret;
6500 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6501 rpc_printer_migrate_settings_internals, argc,
6502 argv);
6503 if (ret)
6504 return ret;
6506 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6507 rpc_printer_migrate_security_internals, argc,
6508 argv);
6513 * Migrate print drivers 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_drivers(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 drivers\n"
6528 " Migrate print-drivers 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, &syntax_spoolss, 0,
6538 rpc_printer_migrate_drivers_internals,
6539 argc, argv);
6543 * Migrate print-forms 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_forms(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 forms\n"
6558 " Migrate print-forms 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, &syntax_spoolss, 0,
6568 rpc_printer_migrate_forms_internals,
6569 argc, argv);
6573 * Migrate printers 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_printers(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 printers\n"
6588 " Migrate printers 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, &syntax_spoolss, 0,
6598 rpc_printer_migrate_printers_internals,
6599 argc, argv);
6603 * Migrate printer-ACLs from a print-server
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.
6610 * @return A shell status integer (0 for success).
6612 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6613 const char **argv)
6615 if (c->display_usage) {
6616 d_printf("Usage:\n"
6617 "net rpc printer migrate security\n"
6618 " Migrate printer-ACLs from a print-server\n");
6619 return 0;
6622 if (!c->opt_host) {
6623 d_printf("no server to migrate\n");
6624 return -1;
6627 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6628 rpc_printer_migrate_security_internals,
6629 argc, argv);
6633 * Migrate printer-settings from a print-server.
6635 * @param c A net_context structure.
6636 * @param argc Standard main() style argc.
6637 * @param argv Standard main() style argv. Initial components are already
6638 * stripped.
6640 * @return A shell status integer (0 for success).
6642 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6643 const char **argv)
6645 if (c->display_usage) {
6646 d_printf("Usage:\n"
6647 "net rpc printer migrate settings\n"
6648 " Migrate printer-settings from a print-server\n");
6649 return 0;
6652 if (!c->opt_host) {
6653 d_printf("no server to migrate\n");
6654 return -1;
6657 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6658 rpc_printer_migrate_settings_internals,
6659 argc, argv);
6663 * 'net rpc printer' entrypoint.
6665 * @param c A net_context structure.
6666 * @param argc Standard main() style argc.
6667 * @param argv Standard main() style argv. Initial components are already
6668 * stripped.
6671 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6674 /* ouch: when addriver and setdriver are called from within
6675 rpc_printer_migrate_drivers_internals, the printer-queue already
6676 *has* to exist */
6678 struct functable func[] = {
6680 "all",
6681 rpc_printer_migrate_all,
6682 NET_TRANSPORT_RPC,
6683 "Migrate all from remote to local print server",
6684 "net rpc printer migrate all\n"
6685 " Migrate all from remote to local print server"
6688 "drivers",
6689 rpc_printer_migrate_drivers,
6690 NET_TRANSPORT_RPC,
6691 "Migrate drivers to local server",
6692 "net rpc printer migrate drivers\n"
6693 " Migrate drivers to local server"
6696 "forms",
6697 rpc_printer_migrate_forms,
6698 NET_TRANSPORT_RPC,
6699 "Migrate froms to local server",
6700 "net rpc printer migrate forms\n"
6701 " Migrate froms to local server"
6704 "printers",
6705 rpc_printer_migrate_printers,
6706 NET_TRANSPORT_RPC,
6707 "Migrate printers to local server",
6708 "net rpc printer migrate printers\n"
6709 " Migrate printers to local server"
6712 "security",
6713 rpc_printer_migrate_security,
6714 NET_TRANSPORT_RPC,
6715 "Mirgate printer ACLs to local server",
6716 "net rpc printer migrate security\n"
6717 " Mirgate printer ACLs to local server"
6720 "settings",
6721 rpc_printer_migrate_settings,
6722 NET_TRANSPORT_RPC,
6723 "Migrate printer settings to local server",
6724 "net rpc printer migrate settings\n"
6725 " Migrate printer settings to local server"
6727 {NULL, NULL, 0, NULL, NULL}
6730 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6735 * List printers on a remote RPC server.
6737 * @param c A net_context structure.
6738 * @param argc Standard main() style argc.
6739 * @param argv Standard main() style argv. Initial components are already
6740 * stripped.
6742 * @return A shell status integer (0 for success).
6744 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6746 if (c->display_usage) {
6747 d_printf("Usage:\n"
6748 "net rpc printer list\n"
6749 " List printers on a remote RPC server\n");
6750 return 0;
6753 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6754 rpc_printer_list_internals,
6755 argc, argv);
6759 * List printer-drivers on a remote RPC server.
6761 * @param c A net_context structure.
6762 * @param argc Standard main() style argc.
6763 * @param argv Standard main() style argv. Initial components are already
6764 * stripped.
6766 * @return A shell status integer (0 for success).
6768 static int rpc_printer_driver_list(struct net_context *c, int argc,
6769 const char **argv)
6771 if (c->display_usage) {
6772 d_printf("Usage:\n"
6773 "net rpc printer driver\n"
6774 " List printer-drivers on a remote RPC server\n");
6775 return 0;
6778 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6779 rpc_printer_driver_list_internals,
6780 argc, argv);
6784 * Publish printer in ADS via MSRPC.
6786 * @param c A net_context structure.
6787 * @param argc Standard main() style argc.
6788 * @param argv Standard main() style argv. Initial components are already
6789 * stripped.
6791 * @return A shell status integer (0 for success).
6793 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6794 const char **argv)
6796 if (c->display_usage) {
6797 d_printf("Usage:\n"
6798 "net rpc printer publish publish\n"
6799 " Publish printer in ADS via MSRPC\n");
6800 return 0;
6803 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6804 rpc_printer_publish_publish_internals,
6805 argc, argv);
6809 * Update printer in ADS via MSRPC.
6811 * @param c A net_context structure.
6812 * @param argc Standard main() style argc.
6813 * @param argv Standard main() style argv. Initial components are already
6814 * stripped.
6816 * @return A shell status integer (0 for success).
6818 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6820 if (c->display_usage) {
6821 d_printf("Usage:\n"
6822 "net rpc printer publish update\n"
6823 " Update printer in ADS via MSRPC\n");
6824 return 0;
6827 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6828 rpc_printer_publish_update_internals,
6829 argc, argv);
6833 * UnPublish printer in ADS via MSRPC.
6835 * @param c A net_context structure.
6836 * @param argc Standard main() style argc.
6837 * @param argv Standard main() style argv. Initial components are already
6838 * stripped.
6840 * @return A shell status integer (0 for success).
6842 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
6843 const char **argv)
6845 if (c->display_usage) {
6846 d_printf("Usage:\n"
6847 "net rpc printer publish unpublish\n"
6848 " UnPublish printer in ADS via MSRPC\n");
6849 return 0;
6852 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6853 rpc_printer_publish_unpublish_internals,
6854 argc, argv);
6858 * List published printers via MSRPC.
6860 * @param c A net_context structure.
6861 * @param argc Standard main() style argc.
6862 * @param argv Standard main() style argv. Initial components are already
6863 * stripped.
6865 * @return A shell status integer (0 for success).
6867 static int rpc_printer_publish_list(struct net_context *c, int argc,
6868 const char **argv)
6870 if (c->display_usage) {
6871 d_printf("Usage:\n"
6872 "net rpc printer publish list\n"
6873 " List published printers via MSRPC\n");
6874 return 0;
6877 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6878 rpc_printer_publish_list_internals,
6879 argc, argv);
6884 * Publish printer in ADS.
6886 * @param c A net_context structure.
6887 * @param argc Standard main() style argc.
6888 * @param argv Standard main() style argv. Initial components are already
6889 * stripped.
6891 * @return A shell status integer (0 for success).
6893 static int rpc_printer_publish(struct net_context *c, int argc,
6894 const char **argv)
6897 struct functable func[] = {
6899 "publish",
6900 rpc_printer_publish_publish,
6901 NET_TRANSPORT_RPC,
6902 "Publish printer in AD",
6903 "net rpc printer publish publish\n"
6904 " Publish printer in AD"
6907 "update",
6908 rpc_printer_publish_update,
6909 NET_TRANSPORT_RPC,
6910 "Update printer in AD",
6911 "net rpc printer publish update\n"
6912 " Update printer in AD"
6915 "unpublish",
6916 rpc_printer_publish_unpublish,
6917 NET_TRANSPORT_RPC,
6918 "Unpublish printer",
6919 "net rpc printer publish unpublish\n"
6920 " Unpublish printer"
6923 "list",
6924 rpc_printer_publish_list,
6925 NET_TRANSPORT_RPC,
6926 "List published printers",
6927 "net rpc printer publish list\n"
6928 " List published printers"
6930 {NULL, NULL, 0, NULL, NULL}
6933 if (argc == 0) {
6934 if (c->display_usage) {
6935 d_printf("Usage:\n");
6936 d_printf("net rpc printer publish\n"
6937 " List published printers\n"
6938 " Alias of net rpc printer publish list\n");
6939 net_display_usage_from_functable(func);
6940 return 0;
6942 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6943 rpc_printer_publish_list_internals,
6944 argc, argv);
6947 return net_run_function(c, argc, argv, "net rpc printer publish",func);
6953 * Display rpc printer help page.
6955 * @param c A net_context structure.
6956 * @param argc Standard main() style argc.
6957 * @param argv Standard main() style argv. Initial components are already
6958 * stripped.
6960 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
6962 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"
6963 "\tlists all printers on print-server\n\n");
6964 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
6965 "\tlists all printer-drivers on print-server\n\n");
6966 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
6967 "\tpublishes printer settings in Active Directory\n"
6968 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
6969 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
6970 "\n\tmigrates printers from remote to local server\n\n");
6971 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
6972 "\n\tmigrates printer-settings from remote to local server\n\n");
6973 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
6974 "\n\tmigrates printer-drivers from remote to local server\n\n");
6975 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
6976 "\n\tmigrates printer-forms from remote to local server\n\n");
6977 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
6978 "\n\tmigrates printer-ACLs from remote to local server\n\n");
6979 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
6980 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
6981 "\tremote to local print-server\n\n");
6982 net_common_methods_usage(c, argc, argv);
6983 net_common_flags_usage(c, argc, argv);
6984 d_printf(
6985 "\t-v or --verbose\t\t\tgive verbose output\n"
6986 "\t --destination\t\tmigration target server (default: localhost)\n");
6988 return -1;
6992 * 'net rpc printer' entrypoint.
6994 * @param c A net_context structure.
6995 * @param argc Standard main() style argc.
6996 * @param argv Standard main() style argv. Initial components are already
6997 * stripped.
6999 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7001 struct functable func[] = {
7003 "list",
7004 rpc_printer_list,
7005 NET_TRANSPORT_RPC,
7006 "List all printers on print server",
7007 "net rpc printer list\n"
7008 " List all printers on print server"
7011 "migrate",
7012 rpc_printer_migrate,
7013 NET_TRANSPORT_RPC,
7014 "Migrate printer to local server",
7015 "net rpc printer migrate\n"
7016 " Migrate printer to local server"
7019 "driver",
7020 rpc_printer_driver_list,
7021 NET_TRANSPORT_RPC,
7022 "List printer drivers",
7023 "net rpc printer driver\n"
7024 " List printer drivers"
7027 "publish",
7028 rpc_printer_publish,
7029 NET_TRANSPORT_RPC,
7030 "Publish printer in AD",
7031 "net rpc printer publish\n"
7032 " Publish printer in AD"
7034 {NULL, NULL, 0, NULL, NULL}
7037 if (argc == 0) {
7038 if (c->display_usage) {
7039 d_printf("Usage:\n");
7040 d_printf("net rpc printer\n"
7041 " List printers\n");
7042 net_display_usage_from_functable(func);
7043 return 0;
7045 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7046 rpc_printer_list_internals,
7047 argc, argv);
7050 return net_run_function(c, argc, argv, "net rpc printer", func);
7054 * 'net rpc' entrypoint.
7056 * @param c A net_context structure.
7057 * @param argc Standard main() style argc.
7058 * @param argv Standard main() style argv. Initial components are already
7059 * stripped.
7062 int net_rpc(struct net_context *c, int argc, const char **argv)
7064 struct functable func[] = {
7066 "audit",
7067 net_rpc_audit,
7068 NET_TRANSPORT_RPC,
7069 "Modify global audit settings",
7070 "net rpc audit\n"
7071 " Modify global audit settings"
7074 "info",
7075 net_rpc_info,
7076 NET_TRANSPORT_RPC,
7077 "Show basic info about a domain",
7078 "net rpc info\n"
7079 " Show basic info about a domain"
7082 "join",
7083 net_rpc_join,
7084 NET_TRANSPORT_RPC,
7085 "Join a domain",
7086 "net rpc join\n"
7087 " Join a domain"
7090 "oldjoin",
7091 net_rpc_oldjoin,
7092 NET_TRANSPORT_RPC,
7093 "Join a domain created in server manager",
7094 "net rpc oldjoin\n"
7095 " Join a domain created in server manager"
7098 "testjoin",
7099 net_rpc_testjoin,
7100 NET_TRANSPORT_RPC,
7101 "Test that a join is valid",
7102 "net rpc testjoin\n"
7103 " Test that a join is valid"
7106 "user",
7107 net_rpc_user,
7108 NET_TRANSPORT_RPC,
7109 "List/modify users",
7110 "net rpc user\n"
7111 " List/modify users"
7114 "password",
7115 rpc_user_password,
7116 NET_TRANSPORT_RPC,
7117 "Change a user password",
7118 "net rpc password\n"
7119 " Change a user password\n"
7120 " Alias for net rpc user password"
7123 "group",
7124 net_rpc_group,
7125 NET_TRANSPORT_RPC,
7126 "List/modify groups",
7127 "net rpc group\n"
7128 " List/modify groups"
7131 "share",
7132 net_rpc_share,
7133 NET_TRANSPORT_RPC,
7134 "List/modify shares",
7135 "net rpc share\n"
7136 " List/modify shares"
7139 "file",
7140 net_rpc_file,
7141 NET_TRANSPORT_RPC,
7142 "List open files",
7143 "net rpc file\n"
7144 " List open files"
7147 "printer",
7148 net_rpc_printer,
7149 NET_TRANSPORT_RPC,
7150 "List/modify printers",
7151 "net rpc printer\n"
7152 " List/modify printers"
7155 "changetrustpw",
7156 net_rpc_changetrustpw,
7157 NET_TRANSPORT_RPC,
7158 "Change trust account password",
7159 "net rpc changetrustpw\n"
7160 " Change trust account password"
7163 "trustdom",
7164 rpc_trustdom,
7165 NET_TRANSPORT_RPC,
7166 "Modify domain trusts",
7167 "net rpc trustdom\n"
7168 " Modify domain trusts"
7171 "abortshutdown",
7172 rpc_shutdown_abort,
7173 NET_TRANSPORT_RPC,
7174 "Abort a remote shutdown",
7175 "net rpc abortshutdown\n"
7176 " Abort a remote shutdown"
7179 "shutdown",
7180 rpc_shutdown,
7181 NET_TRANSPORT_RPC,
7182 "Shutdown a remote server",
7183 "net rpc shutdown\n"
7184 " Shutdown a remote server"
7187 "samdump",
7188 rpc_samdump,
7189 NET_TRANSPORT_RPC,
7190 "Dump SAM data of remote NT PDC",
7191 "net rpc samdump\n"
7192 " Dump SAM data of remote NT PDC"
7195 "vampire",
7196 rpc_vampire,
7197 NET_TRANSPORT_RPC,
7198 "Sync a remote NT PDC's data into local passdb",
7199 "net rpc vampire\n"
7200 " Sync a remote NT PDC's data into local passdb"
7203 "getsid",
7204 net_rpc_getsid,
7205 NET_TRANSPORT_RPC,
7206 "Fetch the domain sid into local secrets.tdb",
7207 "net rpc getsid\n"
7208 " Fetch the domain sid into local secrets.tdb"
7211 "rights",
7212 net_rpc_rights,
7213 NET_TRANSPORT_RPC,
7214 "Manage privileges assigned to SID",
7215 "net rpc rights\n"
7216 " Manage privileges assigned to SID"
7219 "service",
7220 net_rpc_service,
7221 NET_TRANSPORT_RPC,
7222 "Start/stop/query remote services",
7223 "net rpc service\n"
7224 " Start/stop/query remote services"
7227 "registry",
7228 net_rpc_registry,
7229 NET_TRANSPORT_RPC,
7230 "Manage registry hives",
7231 "net rpc registry\n"
7232 " Manage registry hives"
7235 "shell",
7236 net_rpc_shell,
7237 NET_TRANSPORT_RPC,
7238 "Open interactive shell on remote server",
7239 "net rpc shell\n"
7240 " Open interactive shell on remote server"
7242 {NULL, NULL, 0, NULL, NULL}
7244 return net_run_function(c, argc, argv, "net rpc", func);