net: use netapi for rpc_sh_share_add as well.
[Samba.git] / source / utils / net_rpc.c
blob6c8674a07df1f06cb909f859dc5097f79df6ca92
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 * All parameters are provided by the run_rpc_command function, except for
2852 * argc, argv which are passed through.
2854 * @param domain_sid The domain sid acquired from the remote server.
2855 * @param cli A cli_state connected to the server.
2856 * @param mem_ctx Talloc context, destroyed on completion of the function.
2857 * @param argc Standard main() style argc.
2858 * @param argv Standard main() style argv. Initial components are already
2859 * stripped.
2861 * @return Normal NTSTATUS return.
2863 static NTSTATUS rpc_share_del_internals(struct net_context *c,
2864 const DOM_SID *domain_sid,
2865 const char *domain_name,
2866 struct cli_state *cli,
2867 struct rpc_pipe_client *pipe_hnd,
2868 TALLOC_CTX *mem_ctx,
2869 int argc,
2870 const char **argv)
2872 WERROR result;
2874 return rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
2875 pipe_hnd->desthost,
2876 argv[0],
2878 &result);
2882 * Delete a share on a remote RPC server.
2884 * @param domain_sid The domain sid acquired from the remote server.
2885 * @param argc Standard main() style argc.
2886 * @param argv Standard main() style argv. Initial components are already
2887 * stripped.
2889 * @return A shell status integer (0 for success).
2891 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
2893 if (argc < 1 || c->display_usage) {
2894 return rpc_share_usage(c, argc, argv);
2896 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
2897 rpc_share_del_internals,
2898 argc, argv);
2902 * Formatted print of share info
2904 * @param info1 pointer to SRV_SHARE_INFO_1 to format
2907 static void display_share_info_1(struct net_context *c,
2908 struct srvsvc_NetShareInfo1 *r)
2910 if (c->opt_long_list_entries) {
2911 d_printf("%-12s %-8.8s %-50s\n",
2912 r->name,
2913 share_type[r->type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)],
2914 r->comment);
2915 } else {
2916 d_printf("%s\n", r->name);
2920 static WERROR get_share_info(struct net_context *c,
2921 struct rpc_pipe_client *pipe_hnd,
2922 TALLOC_CTX *mem_ctx,
2923 uint32 level,
2924 int argc,
2925 const char **argv,
2926 struct srvsvc_NetShareInfoCtr *info_ctr)
2928 WERROR result;
2929 NTSTATUS status;
2930 union srvsvc_NetShareInfo info;
2932 /* no specific share requested, enumerate all */
2933 if (argc == 0) {
2935 uint32_t preferred_len = 0xffffffff;
2936 uint32_t total_entries = 0;
2937 uint32_t resume_handle = 0;
2939 info_ctr->level = level;
2941 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
2942 pipe_hnd->desthost,
2943 info_ctr,
2944 preferred_len,
2945 &total_entries,
2946 &resume_handle,
2947 &result);
2948 return result;
2951 /* request just one share */
2952 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
2953 pipe_hnd->desthost,
2954 argv[0],
2955 level,
2956 &info,
2957 &result);
2959 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
2960 goto done;
2963 /* construct ctr */
2964 ZERO_STRUCTP(info_ctr);
2966 info_ctr->level = level;
2968 switch (level) {
2969 case 1:
2971 struct srvsvc_NetShareCtr1 *ctr1;
2973 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
2974 W_ERROR_HAVE_NO_MEMORY(ctr1);
2976 ctr1->count = 1;
2977 ctr1->array = info.info1;
2979 info_ctr->ctr.ctr1 = ctr1;
2981 case 2:
2983 struct srvsvc_NetShareCtr2 *ctr2;
2985 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
2986 W_ERROR_HAVE_NO_MEMORY(ctr2);
2988 ctr2->count = 1;
2989 ctr2->array = info.info2;
2991 info_ctr->ctr.ctr2 = ctr2;
2993 case 502:
2995 struct srvsvc_NetShareCtr502 *ctr502;
2997 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
2998 W_ERROR_HAVE_NO_MEMORY(ctr502);
3000 ctr502->count = 1;
3001 ctr502->array = info.info502;
3003 info_ctr->ctr.ctr502 = ctr502;
3005 } /* switch */
3006 done:
3007 return result;
3011 * List shares on a remote RPC server.
3013 * All parameters are provided by the run_rpc_command function, except for
3014 * argc, argv which are passed through.
3016 * @param domain_sid The domain sid acquired from the remote server.
3017 * @param cli A cli_state connected to the server.
3018 * @param mem_ctx Talloc context, destroyed on completion of the function.
3019 * @param argc Standard main() style argc.
3020 * @param argv Standard main() style argv. Initial components are already
3021 * stripped.
3023 * @return Normal NTSTATUS return.
3026 static NTSTATUS rpc_share_list_internals(struct net_context *c,
3027 const DOM_SID *domain_sid,
3028 const char *domain_name,
3029 struct cli_state *cli,
3030 struct rpc_pipe_client *pipe_hnd,
3031 TALLOC_CTX *mem_ctx,
3032 int argc,
3033 const char **argv)
3035 struct srvsvc_NetShareInfoCtr info_ctr;
3036 struct srvsvc_NetShareCtr1 ctr1;
3037 WERROR result;
3038 uint32 i, level = 1;
3040 ZERO_STRUCT(info_ctr);
3041 ZERO_STRUCT(ctr1);
3043 info_ctr.level = 1;
3044 info_ctr.ctr.ctr1 = &ctr1;
3046 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3047 &info_ctr);
3048 if (!W_ERROR_IS_OK(result))
3049 goto done;
3051 /* Display results */
3053 if (c->opt_long_list_entries) {
3054 d_printf(
3055 "\nEnumerating shared resources (exports) on remote server:\n\n"
3056 "\nShare name Type Description\n"
3057 "---------- ---- -----------\n");
3059 for (i = 0; i < info_ctr.ctr.ctr1->count; i++)
3060 display_share_info_1(c, &info_ctr.ctr.ctr1->array[i]);
3061 done:
3062 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3065 /***
3066 * 'net rpc share list' entrypoint.
3067 * @param argc Standard main() style argc.
3068 * @param argv Standard main() style argv. Initial components are already
3069 * stripped.
3071 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3073 if (c->display_usage) {
3074 d_printf("Usage\n"
3075 "net rpc share list\n"
3076 " List shares on remote server\n");
3077 return 0;
3080 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3081 rpc_share_list_internals, argc, argv);
3084 static bool check_share_availability(struct cli_state *cli, const char *netname)
3086 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3087 d_printf("skipping [%s]: not a file share.\n", netname);
3088 return false;
3091 if (!cli_tdis(cli))
3092 return false;
3094 return true;
3097 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3098 const char *netname, uint32 type)
3100 /* only support disk shares */
3101 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3102 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3103 return false;
3106 /* skip builtin shares */
3107 /* FIXME: should print$ be added too ? */
3108 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3109 strequal(netname,"global"))
3110 return false;
3112 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3113 printf("excluding [%s]\n", netname);
3114 return false;
3117 return check_share_availability(cli, netname);
3121 * Migrate shares from a remote RPC server to the local RPC server.
3123 * All parameters are provided by the run_rpc_command function, except for
3124 * argc, argv which are passed through.
3126 * @param domain_sid The domain sid acquired from the remote server.
3127 * @param cli A cli_state connected to the server.
3128 * @param mem_ctx Talloc context, destroyed on completion of the function.
3129 * @param argc Standard main() style argc.
3130 * @param argv Standard main() style argv. Initial components are already
3131 * stripped.
3133 * @return Normal NTSTATUS return.
3136 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3137 const DOM_SID *domain_sid,
3138 const char *domain_name,
3139 struct cli_state *cli,
3140 struct rpc_pipe_client *pipe_hnd,
3141 TALLOC_CTX *mem_ctx,
3142 int argc,
3143 const char **argv)
3145 WERROR result;
3146 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3147 struct srvsvc_NetShareInfoCtr ctr_src;
3148 uint32 i;
3149 struct rpc_pipe_client *srvsvc_pipe = NULL;
3150 struct cli_state *cli_dst = NULL;
3151 uint32 level = 502; /* includes secdesc */
3152 uint32_t parm_error = 0;
3154 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3155 &ctr_src);
3156 if (!W_ERROR_IS_OK(result))
3157 goto done;
3159 /* connect destination PI_SRVSVC */
3160 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3161 &ndr_table_srvsvc.syntax_id);
3162 if (!NT_STATUS_IS_OK(nt_status))
3163 return nt_status;
3166 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3168 union srvsvc_NetShareInfo info;
3169 struct srvsvc_NetShareInfo502 info502 =
3170 ctr_src.ctr.ctr502->array[i];
3172 /* reset error-code */
3173 nt_status = NT_STATUS_UNSUCCESSFUL;
3175 if (!check_share_sanity(c, cli, info502.name, info502.type))
3176 continue;
3178 /* finally add the share on the dst server */
3180 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3181 info502.name, info502.path, info502.comment);
3183 info.info502 = &info502;
3185 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3186 srvsvc_pipe->desthost,
3187 502,
3188 &info,
3189 &parm_error,
3190 &result);
3192 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3193 printf(" [%s] does already exist\n",
3194 info502.name);
3195 continue;
3198 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3199 printf("cannot add share: %s\n", dos_errstr(result));
3200 goto done;
3205 nt_status = NT_STATUS_OK;
3207 done:
3208 if (cli_dst) {
3209 cli_shutdown(cli_dst);
3212 return nt_status;
3217 * Migrate shares from a RPC server to another.
3219 * @param argc Standard main() style argc.
3220 * @param argv Standard main() style argv. Initial components are already
3221 * stripped.
3223 * @return A shell status integer (0 for success).
3225 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3226 const char **argv)
3228 if (c->display_usage) {
3229 d_printf("Usage:\n"
3230 "net rpc share migrate shares\n"
3231 " Migrate shares to local server\n");
3232 return 0;
3235 if (!c->opt_host) {
3236 printf("no server to migrate\n");
3237 return -1;
3240 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3241 rpc_share_migrate_shares_internals,
3242 argc, argv);
3246 * Copy a file/dir
3248 * @param f file_info
3249 * @param mask current search mask
3250 * @param state arg-pointer
3253 static void copy_fn(const char *mnt, file_info *f,
3254 const char *mask, void *state)
3256 static NTSTATUS nt_status;
3257 static struct copy_clistate *local_state;
3258 static fstring filename, new_mask;
3259 fstring dir;
3260 char *old_dir;
3261 struct net_context *c;
3263 local_state = (struct copy_clistate *)state;
3264 nt_status = NT_STATUS_UNSUCCESSFUL;
3266 c = local_state->c;
3268 if (strequal(f->name, ".") || strequal(f->name, ".."))
3269 return;
3271 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3273 /* DIRECTORY */
3274 if (f->mode & aDIR) {
3276 DEBUG(3,("got dir: %s\n", f->name));
3278 fstrcpy(dir, local_state->cwd);
3279 fstrcat(dir, "\\");
3280 fstrcat(dir, f->name);
3282 switch (net_mode_share)
3284 case NET_MODE_SHARE_MIGRATE:
3285 /* create that directory */
3286 nt_status = net_copy_file(c, local_state->mem_ctx,
3287 local_state->cli_share_src,
3288 local_state->cli_share_dst,
3289 dir, dir,
3290 c->opt_acls? true : false,
3291 c->opt_attrs? true : false,
3292 c->opt_timestamps? true:false,
3293 false);
3294 break;
3295 default:
3296 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3297 return;
3300 if (!NT_STATUS_IS_OK(nt_status))
3301 printf("could not handle dir %s: %s\n",
3302 dir, nt_errstr(nt_status));
3304 /* search below that directory */
3305 fstrcpy(new_mask, dir);
3306 fstrcat(new_mask, "\\*");
3308 old_dir = local_state->cwd;
3309 local_state->cwd = dir;
3310 if (!sync_files(local_state, new_mask))
3311 printf("could not handle files\n");
3312 local_state->cwd = old_dir;
3314 return;
3318 /* FILE */
3319 fstrcpy(filename, local_state->cwd);
3320 fstrcat(filename, "\\");
3321 fstrcat(filename, f->name);
3323 DEBUG(3,("got file: %s\n", filename));
3325 switch (net_mode_share)
3327 case NET_MODE_SHARE_MIGRATE:
3328 nt_status = net_copy_file(c, local_state->mem_ctx,
3329 local_state->cli_share_src,
3330 local_state->cli_share_dst,
3331 filename, filename,
3332 c->opt_acls? true : false,
3333 c->opt_attrs? true : false,
3334 c->opt_timestamps? true: false,
3335 true);
3336 break;
3337 default:
3338 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3339 return;
3342 if (!NT_STATUS_IS_OK(nt_status))
3343 printf("could not handle file %s: %s\n",
3344 filename, nt_errstr(nt_status));
3349 * sync files, can be called recursivly to list files
3350 * and then call copy_fn for each file
3352 * @param cp_clistate pointer to the copy_clistate we work with
3353 * @param mask the current search mask
3355 * @return Boolean result
3357 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3359 struct cli_state *targetcli;
3360 char *targetpath = NULL;
3362 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3364 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate->cli_share_src,
3365 mask, &targetcli, &targetpath ) ) {
3366 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3367 mask, cli_errstr(cp_clistate->cli_share_src));
3368 return false;
3371 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3372 d_fprintf(stderr, "listing %s failed with error: %s\n",
3373 mask, cli_errstr(targetcli));
3374 return false;
3377 return true;
3382 * Set the top level directory permissions before we do any further copies.
3383 * Should set up ACL inheritance.
3386 bool copy_top_level_perms(struct net_context *c,
3387 struct copy_clistate *cp_clistate,
3388 const char *sharename)
3390 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3392 switch (net_mode_share) {
3393 case NET_MODE_SHARE_MIGRATE:
3394 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3395 nt_status = net_copy_fileattr(c,
3396 cp_clistate->mem_ctx,
3397 cp_clistate->cli_share_src,
3398 cp_clistate->cli_share_dst,
3399 "\\", "\\",
3400 c->opt_acls? true : false,
3401 c->opt_attrs? true : false,
3402 c->opt_timestamps? true: false,
3403 false);
3404 break;
3405 default:
3406 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3407 break;
3410 if (!NT_STATUS_IS_OK(nt_status)) {
3411 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3412 sharename, nt_errstr(nt_status));
3413 return false;
3416 return true;
3420 * Sync all files inside a remote share to another share (over smb).
3422 * All parameters are provided by the run_rpc_command function, except for
3423 * argc, argv which are passed through.
3425 * @param domain_sid The domain sid acquired from the remote server.
3426 * @param cli A cli_state connected to the server.
3427 * @param mem_ctx Talloc context, destroyed on completion of the function.
3428 * @param argc Standard main() style argc.
3429 * @param argv Standard main() style argv. Initial components are already
3430 * stripped.
3432 * @return Normal NTSTATUS return.
3435 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3436 const DOM_SID *domain_sid,
3437 const char *domain_name,
3438 struct cli_state *cli,
3439 struct rpc_pipe_client *pipe_hnd,
3440 TALLOC_CTX *mem_ctx,
3441 int argc,
3442 const char **argv)
3444 WERROR result;
3445 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3446 struct srvsvc_NetShareInfoCtr ctr_src;
3447 uint32 i;
3448 uint32 level = 502;
3449 struct copy_clistate cp_clistate;
3450 bool got_src_share = false;
3451 bool got_dst_share = false;
3452 const char *mask = "\\*";
3453 char *dst = NULL;
3455 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3456 if (dst == NULL) {
3457 nt_status = NT_STATUS_NO_MEMORY;
3458 goto done;
3461 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3462 &ctr_src);
3464 if (!W_ERROR_IS_OK(result))
3465 goto done;
3467 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3469 struct srvsvc_NetShareInfo502 info502 =
3470 ctr_src.ctr.ctr502->array[i];
3472 if (!check_share_sanity(c, cli, info502.name, info502.type))
3473 continue;
3475 /* one might not want to mirror whole discs :) */
3476 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3477 d_printf("skipping [%s]: builtin/hidden share\n", info502.name);
3478 continue;
3481 switch (net_mode_share)
3483 case NET_MODE_SHARE_MIGRATE:
3484 printf("syncing");
3485 break;
3486 default:
3487 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3488 break;
3490 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3491 info502.name,
3492 c->opt_acls ? "including" : "without",
3493 c->opt_attrs ? "including" : "without",
3494 c->opt_timestamps ? "(preserving timestamps)" : "");
3496 cp_clistate.mem_ctx = mem_ctx;
3497 cp_clistate.cli_share_src = NULL;
3498 cp_clistate.cli_share_dst = NULL;
3499 cp_clistate.cwd = NULL;
3500 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3501 cp_clistate.c = c;
3503 /* open share source */
3504 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3505 &cli->dest_ss, cli->desthost,
3506 info502.name, "A:");
3507 if (!NT_STATUS_IS_OK(nt_status))
3508 goto done;
3510 got_src_share = true;
3512 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3513 /* open share destination */
3514 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3515 NULL, dst, info502.name, "A:");
3516 if (!NT_STATUS_IS_OK(nt_status))
3517 goto done;
3519 got_dst_share = true;
3522 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3523 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", info502.name);
3524 nt_status = NT_STATUS_UNSUCCESSFUL;
3525 goto done;
3528 if (!sync_files(&cp_clistate, mask)) {
3529 d_fprintf(stderr, "could not handle files for share: %s\n", info502.name);
3530 nt_status = NT_STATUS_UNSUCCESSFUL;
3531 goto done;
3535 nt_status = NT_STATUS_OK;
3537 done:
3539 if (got_src_share)
3540 cli_shutdown(cp_clistate.cli_share_src);
3542 if (got_dst_share)
3543 cli_shutdown(cp_clistate.cli_share_dst);
3545 SAFE_FREE(dst);
3546 return nt_status;
3550 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3552 if (c->display_usage) {
3553 d_printf("Usage:\n"
3554 "net share migrate files\n"
3555 " Migrate files to local server\n");
3556 return 0;
3559 if (!c->opt_host) {
3560 d_printf("no server to migrate\n");
3561 return -1;
3564 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3565 rpc_share_migrate_files_internals,
3566 argc, argv);
3570 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3572 * All parameters are provided by the run_rpc_command function, except for
3573 * argc, argv which are passed through.
3575 * @param domain_sid The domain sid acquired from the remote server.
3576 * @param cli A cli_state connected to the server.
3577 * @param mem_ctx Talloc context, destroyed on completion of the function.
3578 * @param argc Standard main() style argc.
3579 * @param argv Standard main() style argv. Initial components are already
3580 * stripped.
3582 * @return Normal NTSTATUS return.
3585 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3586 const DOM_SID *domain_sid,
3587 const char *domain_name,
3588 struct cli_state *cli,
3589 struct rpc_pipe_client *pipe_hnd,
3590 TALLOC_CTX *mem_ctx,
3591 int argc,
3592 const char **argv)
3594 WERROR result;
3595 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3596 struct srvsvc_NetShareInfoCtr ctr_src;
3597 union srvsvc_NetShareInfo info;
3598 uint32 i;
3599 struct rpc_pipe_client *srvsvc_pipe = NULL;
3600 struct cli_state *cli_dst = NULL;
3601 uint32 level = 502; /* includes secdesc */
3602 uint32_t parm_error = 0;
3604 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3605 &ctr_src);
3607 if (!W_ERROR_IS_OK(result))
3608 goto done;
3610 /* connect destination PI_SRVSVC */
3611 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3612 &ndr_table_srvsvc.syntax_id);
3613 if (!NT_STATUS_IS_OK(nt_status))
3614 return nt_status;
3617 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3619 struct srvsvc_NetShareInfo502 info502 =
3620 ctr_src.ctr.ctr502->array[i];
3622 /* reset error-code */
3623 nt_status = NT_STATUS_UNSUCCESSFUL;
3625 if (!check_share_sanity(c, cli, info502.name, info502.type))
3626 continue;
3628 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3629 info502.name, info502.path, info502.comment);
3631 if (c->opt_verbose)
3632 display_sec_desc(info502.sd_buf.sd);
3634 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3635 info.info502 = &info502;
3637 /* finally modify the share on the dst server */
3638 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3639 srvsvc_pipe->desthost,
3640 info502.name,
3641 level,
3642 &info,
3643 &parm_error,
3644 &result);
3645 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3646 printf("cannot set share-acl: %s\n", dos_errstr(result));
3647 goto done;
3652 nt_status = NT_STATUS_OK;
3654 done:
3655 if (cli_dst) {
3656 cli_shutdown(cli_dst);
3659 return nt_status;
3664 * Migrate share-acls from a RPC server to another.
3666 * @param argc Standard main() style argc.
3667 * @param argv Standard main() style argv. Initial components are already
3668 * stripped.
3670 * @return A shell status integer (0 for success).
3672 static int rpc_share_migrate_security(struct net_context *c, int argc,
3673 const char **argv)
3675 if (c->display_usage) {
3676 d_printf("Usage:\n"
3677 "net rpc share migrate security\n"
3678 " Migrate share-acls to local server\n");
3679 return 0;
3682 if (!c->opt_host) {
3683 d_printf("no server to migrate\n");
3684 return -1;
3687 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3688 rpc_share_migrate_security_internals,
3689 argc, argv);
3693 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
3694 * from one server to another.
3696 * @param argc Standard main() style argc.
3697 * @param argv Standard main() style argv. Initial components are already
3698 * stripped.
3700 * @return A shell status integer (0 for success).
3703 static int rpc_share_migrate_all(struct net_context *c, int argc,
3704 const char **argv)
3706 int ret;
3708 if (c->display_usage) {
3709 d_printf("Usage:\n"
3710 "net rpc share migrate all\n"
3711 " Migrates shares including all share settings\n");
3712 return 0;
3715 if (!c->opt_host) {
3716 d_printf("no server to migrate\n");
3717 return -1;
3720 /* order is important. we don't want to be locked out by the share-acl
3721 * before copying files - gd */
3723 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3724 rpc_share_migrate_shares_internals, argc, argv);
3725 if (ret)
3726 return ret;
3728 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3729 rpc_share_migrate_files_internals, argc, argv);
3730 if (ret)
3731 return ret;
3733 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3734 rpc_share_migrate_security_internals, argc,
3735 argv);
3740 * 'net rpc share migrate' entrypoint.
3741 * @param argc Standard main() style argc.
3742 * @param argv Standard main() style argv. Initial components are already
3743 * stripped.
3745 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
3748 struct functable func[] = {
3750 "all",
3751 rpc_share_migrate_all,
3752 NET_TRANSPORT_RPC,
3753 "Migrate shares from remote to local server",
3754 "net rpc share migrate all\n"
3755 " Migrate shares from remote to local server"
3758 "files",
3759 rpc_share_migrate_files,
3760 NET_TRANSPORT_RPC,
3761 "Migrate files from remote to local server",
3762 "net rpc share migrate files\n"
3763 " Migrate files from remote to local server"
3766 "security",
3767 rpc_share_migrate_security,
3768 NET_TRANSPORT_RPC,
3769 "Migrate share-ACLs from remote to local server",
3770 "net rpc share migrate security\n"
3771 " Migrate share-ACLs from remote to local server"
3774 "shares",
3775 rpc_share_migrate_shares,
3776 NET_TRANSPORT_RPC,
3777 "Migrate shares from remote to local server",
3778 "net rpc share migrate shares\n"
3779 " Migrate shares from remote to local server"
3781 {NULL, NULL, 0, NULL, NULL}
3784 net_mode_share = NET_MODE_SHARE_MIGRATE;
3786 return net_run_function(c, argc, argv, "net rpc share migrate", func);
3789 struct full_alias {
3790 DOM_SID sid;
3791 uint32 num_members;
3792 DOM_SID *members;
3795 static int num_server_aliases;
3796 static struct full_alias *server_aliases;
3799 * Add an alias to the static list.
3801 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
3803 if (server_aliases == NULL)
3804 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
3806 server_aliases[num_server_aliases] = *alias;
3807 num_server_aliases += 1;
3811 * For a specific domain on the server, fetch all the aliases
3812 * and their members. Add all of them to the server_aliases.
3815 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
3816 TALLOC_CTX *mem_ctx,
3817 POLICY_HND *connect_pol,
3818 const DOM_SID *domain_sid)
3820 uint32 start_idx, max_entries, num_entries, i;
3821 struct samr_SamArray *groups = NULL;
3822 NTSTATUS result;
3823 POLICY_HND domain_pol;
3825 /* Get domain policy handle */
3827 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
3828 connect_pol,
3829 MAXIMUM_ALLOWED_ACCESS,
3830 CONST_DISCARD(struct dom_sid2 *, domain_sid),
3831 &domain_pol);
3832 if (!NT_STATUS_IS_OK(result))
3833 return result;
3835 start_idx = 0;
3836 max_entries = 250;
3838 do {
3839 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
3840 &domain_pol,
3841 &start_idx,
3842 &groups,
3843 max_entries,
3844 &num_entries);
3845 for (i = 0; i < num_entries; i++) {
3847 POLICY_HND alias_pol;
3848 struct full_alias alias;
3849 struct lsa_SidArray sid_array;
3850 int j;
3852 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
3853 &domain_pol,
3854 MAXIMUM_ALLOWED_ACCESS,
3855 groups->entries[i].idx,
3856 &alias_pol);
3857 if (!NT_STATUS_IS_OK(result))
3858 goto done;
3860 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
3861 &alias_pol,
3862 &sid_array);
3863 if (!NT_STATUS_IS_OK(result))
3864 goto done;
3866 alias.num_members = sid_array.num_sids;
3868 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
3869 if (!NT_STATUS_IS_OK(result))
3870 goto done;
3872 alias.members = NULL;
3874 if (alias.num_members > 0) {
3875 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
3877 for (j = 0; j < alias.num_members; j++)
3878 sid_copy(&alias.members[j],
3879 sid_array.sids[j].sid);
3882 sid_copy(&alias.sid, domain_sid);
3883 sid_append_rid(&alias.sid, groups->entries[i].idx);
3885 push_alias(mem_ctx, &alias);
3887 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
3889 result = NT_STATUS_OK;
3891 done:
3892 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
3894 return result;
3898 * Dump server_aliases as names for debugging purposes.
3901 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
3902 const DOM_SID *domain_sid,
3903 const char *domain_name,
3904 struct cli_state *cli,
3905 struct rpc_pipe_client *pipe_hnd,
3906 TALLOC_CTX *mem_ctx,
3907 int argc,
3908 const char **argv)
3910 int i;
3911 NTSTATUS result;
3912 POLICY_HND lsa_pol;
3914 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
3915 SEC_RIGHTS_MAXIMUM_ALLOWED,
3916 &lsa_pol);
3917 if (!NT_STATUS_IS_OK(result))
3918 return result;
3920 for (i=0; i<num_server_aliases; i++) {
3921 char **names;
3922 char **domains;
3923 enum lsa_SidType *types;
3924 int j;
3926 struct full_alias *alias = &server_aliases[i];
3928 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
3929 &alias->sid,
3930 &domains, &names, &types);
3931 if (!NT_STATUS_IS_OK(result))
3932 continue;
3934 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
3936 if (alias->num_members == 0) {
3937 DEBUG(1, ("\n"));
3938 continue;
3941 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
3942 alias->num_members,
3943 alias->members,
3944 &domains, &names, &types);
3946 if (!NT_STATUS_IS_OK(result) &&
3947 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
3948 continue;
3950 for (j=0; j<alias->num_members; j++)
3951 DEBUG(1, ("%s\\%s (%d); ",
3952 domains[j] ? domains[j] : "*unknown*",
3953 names[j] ? names[j] : "*unknown*",types[j]));
3954 DEBUG(1, ("\n"));
3957 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
3959 return NT_STATUS_OK;
3963 * Fetch a list of all server aliases and their members into
3964 * server_aliases.
3967 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
3968 const DOM_SID *domain_sid,
3969 const char *domain_name,
3970 struct cli_state *cli,
3971 struct rpc_pipe_client *pipe_hnd,
3972 TALLOC_CTX *mem_ctx,
3973 int argc,
3974 const char **argv)
3976 NTSTATUS result;
3977 POLICY_HND connect_pol;
3979 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
3980 pipe_hnd->desthost,
3981 MAXIMUM_ALLOWED_ACCESS,
3982 &connect_pol);
3984 if (!NT_STATUS_IS_OK(result))
3985 goto done;
3987 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3988 &global_sid_Builtin);
3990 if (!NT_STATUS_IS_OK(result))
3991 goto done;
3993 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
3994 domain_sid);
3996 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
3997 done:
3998 return result;
4001 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4003 token->num_sids = 4;
4005 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4006 d_fprintf(stderr, "malloc failed\n");
4007 token->num_sids = 0;
4008 return;
4011 token->user_sids[0] = *user_sid;
4012 sid_copy(&token->user_sids[1], &global_sid_World);
4013 sid_copy(&token->user_sids[2], &global_sid_Network);
4014 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4017 static void free_user_token(NT_USER_TOKEN *token)
4019 SAFE_FREE(token->user_sids);
4022 static bool is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4024 int i;
4026 for (i=0; i<token->num_sids; i++) {
4027 if (sid_compare(sid, &token->user_sids[i]) == 0)
4028 return true;
4030 return false;
4033 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4035 if (is_sid_in_token(token, sid))
4036 return;
4038 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4039 if (!token->user_sids) {
4040 return;
4043 sid_copy(&token->user_sids[token->num_sids], sid);
4045 token->num_sids += 1;
4048 struct user_token {
4049 fstring name;
4050 NT_USER_TOKEN token;
4053 static void dump_user_token(struct user_token *token)
4055 int i;
4057 d_printf("%s\n", token->name);
4059 for (i=0; i<token->token.num_sids; i++) {
4060 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4064 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4066 int i;
4068 for (i=0; i<alias->num_members; i++) {
4069 if (sid_compare(sid, &alias->members[i]) == 0)
4070 return true;
4073 return false;
4076 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4078 int i;
4080 for (i=0; i<num_server_aliases; i++) {
4081 if (is_alias_member(&sid, &server_aliases[i]))
4082 add_sid_to_token(token, &server_aliases[i].sid);
4087 * We got a user token with all the SIDs we can know about without asking the
4088 * server directly. These are the user and domain group sids. All of these can
4089 * be members of aliases. So scan the list of aliases for each of the SIDs and
4090 * add them to the token.
4093 static void collect_alias_memberships(NT_USER_TOKEN *token)
4095 int num_global_sids = token->num_sids;
4096 int i;
4098 for (i=0; i<num_global_sids; i++) {
4099 collect_sid_memberships(token, token->user_sids[i]);
4103 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4105 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4106 enum wbcSidType type;
4107 fstring full_name;
4108 struct wbcDomainSid wsid;
4109 char *sid_str = NULL;
4110 DOM_SID user_sid;
4111 uint32_t num_groups;
4112 gid_t *groups = NULL;
4113 uint32_t i;
4115 fstr_sprintf(full_name, "%s%c%s",
4116 domain, *lp_winbind_separator(), user);
4118 /* First let's find out the user sid */
4120 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4122 if (!WBC_ERROR_IS_OK(wbc_status)) {
4123 DEBUG(1, ("winbind could not find %s: %s\n",
4124 full_name, wbcErrorString(wbc_status)));
4125 return false;
4128 wbc_status = wbcSidToString(&wsid, &sid_str);
4129 if (!WBC_ERROR_IS_OK(wbc_status)) {
4130 return false;
4133 if (type != SID_NAME_USER) {
4134 wbcFreeMemory(sid_str);
4135 DEBUG(1, ("%s is not a user\n", full_name));
4136 return false;
4139 string_to_sid(&user_sid, sid_str);
4140 wbcFreeMemory(sid_str);
4141 sid_str = NULL;
4143 init_user_token(token, &user_sid);
4145 /* And now the groups winbind knows about */
4147 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4148 if (!WBC_ERROR_IS_OK(wbc_status)) {
4149 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4150 full_name, wbcErrorString(wbc_status)));
4151 return false;
4154 for (i = 0; i < num_groups; i++) {
4155 gid_t gid = groups[i];
4156 DOM_SID sid;
4158 wbc_status = wbcGidToSid(gid, &wsid);
4159 if (!WBC_ERROR_IS_OK(wbc_status)) {
4160 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4161 gid, wbcErrorString(wbc_status)));
4162 wbcFreeMemory(groups);
4163 return false;
4166 wbc_status = wbcSidToString(&wsid, &sid_str);
4167 if (!WBC_ERROR_IS_OK(wbc_status)) {
4168 wbcFreeMemory(groups);
4169 return false;
4172 DEBUG(3, (" %s\n", sid_str));
4174 string_to_sid(&sid, sid_str);
4175 wbcFreeMemory(sid_str);
4176 sid_str = NULL;
4178 add_sid_to_token(token, &sid);
4180 wbcFreeMemory(groups);
4182 return true;
4186 * Get a list of all user tokens we want to look at
4189 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4190 struct user_token **user_tokens)
4192 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4193 uint32_t i, num_users;
4194 const char **users;
4195 struct user_token *result;
4196 TALLOC_CTX *frame = NULL;
4198 if (lp_winbind_use_default_domain() &&
4199 (c->opt_target_workgroup == NULL)) {
4200 d_fprintf(stderr, "winbind use default domain = yes set, "
4201 "please specify a workgroup\n");
4202 return false;
4205 /* Send request to winbind daemon */
4207 wbc_status = wbcListUsers(NULL, &num_users, &users);
4208 if (!WBC_ERROR_IS_OK(wbc_status)) {
4209 DEBUG(1, ("winbind could not list users: %s\n",
4210 wbcErrorString(wbc_status)));
4211 return false;
4214 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4216 if (result == NULL) {
4217 DEBUG(1, ("Could not malloc sid array\n"));
4218 wbcFreeMemory(users);
4219 return false;
4222 frame = talloc_stackframe();
4223 for (i=0; i < num_users; i++) {
4224 fstring domain, user;
4225 char *p;
4227 fstrcpy(result[i].name, users[i]);
4229 p = strchr(users[i], *lp_winbind_separator());
4231 DEBUG(3, ("%s\n", users[i]));
4233 if (p == NULL) {
4234 fstrcpy(domain, c->opt_target_workgroup);
4235 fstrcpy(user, users[i]);
4236 } else {
4237 *p++ = '\0';
4238 fstrcpy(domain, users[i]);
4239 strupper_m(domain);
4240 fstrcpy(user, p);
4243 get_user_sids(domain, user, &(result[i].token));
4244 i+=1;
4246 TALLOC_FREE(frame);
4247 wbcFreeMemory(users);
4249 *num_tokens = num_users;
4250 *user_tokens = result;
4252 return true;
4255 static bool get_user_tokens_from_file(FILE *f,
4256 int *num_tokens,
4257 struct user_token **tokens)
4259 struct user_token *token = NULL;
4261 while (!feof(f)) {
4262 fstring line;
4264 if (fgets(line, sizeof(line)-1, f) == NULL) {
4265 return true;
4268 if (line[strlen(line)-1] == '\n')
4269 line[strlen(line)-1] = '\0';
4271 if (line[0] == ' ') {
4272 /* We have a SID */
4274 DOM_SID sid;
4275 string_to_sid(&sid, &line[1]);
4277 if (token == NULL) {
4278 DEBUG(0, ("File does not begin with username"));
4279 return false;
4282 add_sid_to_token(&token->token, &sid);
4283 continue;
4286 /* And a new user... */
4288 *num_tokens += 1;
4289 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4290 if (*tokens == NULL) {
4291 DEBUG(0, ("Could not realloc tokens\n"));
4292 return false;
4295 token = &((*tokens)[*num_tokens-1]);
4297 fstrcpy(token->name, line);
4298 token->token.num_sids = 0;
4299 token->token.user_sids = NULL;
4300 continue;
4303 return false;
4308 * Show the list of all users that have access to a share
4311 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4312 TALLOC_CTX *mem_ctx,
4313 const char *netname,
4314 int num_tokens,
4315 struct user_token *tokens)
4317 int fnum;
4318 SEC_DESC *share_sd = NULL;
4319 SEC_DESC *root_sd = NULL;
4320 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4321 int i;
4322 union srvsvc_NetShareInfo info;
4323 WERROR result;
4324 NTSTATUS status;
4325 uint16 cnum;
4327 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4328 pipe_hnd->desthost,
4329 netname,
4330 502,
4331 &info,
4332 &result);
4334 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4335 DEBUG(1, ("Coult not query secdesc for share %s\n",
4336 netname));
4337 return;
4340 share_sd = info.info502->sd_buf.sd;
4341 if (share_sd == NULL) {
4342 DEBUG(1, ("Got no secdesc for share %s\n",
4343 netname));
4346 cnum = cli->cnum;
4348 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4349 return;
4352 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4354 if (fnum != -1) {
4355 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4358 for (i=0; i<num_tokens; i++) {
4359 uint32 acc_granted;
4361 if (share_sd != NULL) {
4362 if (!se_access_check(share_sd, &tokens[i].token,
4363 1, &acc_granted, &status)) {
4364 DEBUG(1, ("Could not check share_sd for "
4365 "user %s\n",
4366 tokens[i].name));
4367 continue;
4370 if (!NT_STATUS_IS_OK(status))
4371 continue;
4374 if (root_sd == NULL) {
4375 d_printf(" %s\n", tokens[i].name);
4376 continue;
4379 if (!se_access_check(root_sd, &tokens[i].token,
4380 1, &acc_granted, &status)) {
4381 DEBUG(1, ("Could not check root_sd for user %s\n",
4382 tokens[i].name));
4383 continue;
4386 if (!NT_STATUS_IS_OK(status))
4387 continue;
4389 d_printf(" %s\n", tokens[i].name);
4392 if (fnum != -1)
4393 cli_close(cli, fnum);
4394 cli_tdis(cli);
4395 cli->cnum = cnum;
4397 return;
4400 struct share_list {
4401 int num_shares;
4402 char **shares;
4405 static void collect_share(const char *name, uint32 m,
4406 const char *comment, void *state)
4408 struct share_list *share_list = (struct share_list *)state;
4410 if (m != STYPE_DISKTREE)
4411 return;
4413 share_list->num_shares += 1;
4414 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4415 if (!share_list->shares) {
4416 share_list->num_shares = 0;
4417 return;
4419 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4423 * List shares on a remote RPC server, including the security descriptors.
4425 * All parameters are provided by the run_rpc_command function, except for
4426 * argc, argv which are passed through.
4428 * @param domain_sid The domain sid acquired from the remote server.
4429 * @param cli A cli_state connected to the server.
4430 * @param mem_ctx Talloc context, destroyed on completion of the function.
4431 * @param argc Standard main() style argc.
4432 * @param argv Standard main() style argv. Initial components are already
4433 * stripped.
4435 * @return Normal NTSTATUS return.
4438 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4439 const DOM_SID *domain_sid,
4440 const char *domain_name,
4441 struct cli_state *cli,
4442 struct rpc_pipe_client *pipe_hnd,
4443 TALLOC_CTX *mem_ctx,
4444 int argc,
4445 const char **argv)
4447 int ret;
4448 bool r;
4449 ENUM_HND hnd;
4450 uint32 i;
4451 FILE *f;
4453 struct user_token *tokens = NULL;
4454 int num_tokens = 0;
4456 struct share_list share_list;
4458 if (argc == 0) {
4459 f = stdin;
4460 } else {
4461 f = fopen(argv[0], "r");
4464 if (f == NULL) {
4465 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4466 return NT_STATUS_UNSUCCESSFUL;
4469 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4471 if (f != stdin)
4472 fclose(f);
4474 if (!r) {
4475 DEBUG(0, ("Could not read users from file\n"));
4476 return NT_STATUS_UNSUCCESSFUL;
4479 for (i=0; i<num_tokens; i++)
4480 collect_alias_memberships(&tokens[i].token);
4482 init_enum_hnd(&hnd, 0);
4484 share_list.num_shares = 0;
4485 share_list.shares = NULL;
4487 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4489 if (ret == -1) {
4490 DEBUG(0, ("Error returning browse list: %s\n",
4491 cli_errstr(cli)));
4492 goto done;
4495 for (i = 0; i < share_list.num_shares; i++) {
4496 char *netname = share_list.shares[i];
4498 if (netname[strlen(netname)-1] == '$')
4499 continue;
4501 d_printf("%s\n", netname);
4503 show_userlist(pipe_hnd, mem_ctx, netname,
4504 num_tokens, tokens);
4506 done:
4507 for (i=0; i<num_tokens; i++) {
4508 free_user_token(&tokens[i].token);
4510 SAFE_FREE(tokens);
4511 SAFE_FREE(share_list.shares);
4513 return NT_STATUS_OK;
4516 static int rpc_share_allowedusers(struct net_context *c, int argc,
4517 const char **argv)
4519 int result;
4521 if (c->display_usage) {
4522 d_printf("Usage:\n"
4523 "net rpc share allowedusers\n"
4524 " List allowed users\n");
4525 return 0;
4528 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4529 rpc_aliaslist_internals,
4530 argc, argv);
4531 if (result != 0)
4532 return result;
4534 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4535 rpc_aliaslist_dump,
4536 argc, argv);
4537 if (result != 0)
4538 return result;
4540 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4541 rpc_share_allowedusers_internals,
4542 argc, argv);
4545 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4547 int num_tokens = 0;
4548 struct user_token *tokens = NULL;
4549 int i;
4551 if (argc != 0) {
4552 net_usersidlist_usage(c, argc, argv);
4553 return 0;
4556 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4557 DEBUG(0, ("Could not get the user/sid list\n"));
4558 return 0;
4561 for (i=0; i<num_tokens; i++) {
4562 dump_user_token(&tokens[i]);
4563 free_user_token(&tokens[i].token);
4566 SAFE_FREE(tokens);
4567 return 1;
4570 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4572 d_printf("net usersidlist\n"
4573 "\tprints out a list of all users the running winbind knows\n"
4574 "\tabout, together with all their SIDs. This is used as\n"
4575 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4577 net_common_flags_usage(c, argc, argv);
4578 return -1;
4582 * 'net rpc share' entrypoint.
4583 * @param argc Standard main() style argc.
4584 * @param argv Standard main() style argv. Initial components are already
4585 * stripped.
4588 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4590 NET_API_STATUS status;
4592 struct functable func[] = {
4594 "add",
4595 rpc_share_add,
4596 NET_TRANSPORT_RPC,
4597 "Add share",
4598 "net rpc share add\n"
4599 " Add share"
4602 "delete",
4603 rpc_share_delete,
4604 NET_TRANSPORT_RPC,
4605 "Remove share",
4606 "net rpc share delete\n"
4607 " Remove share"
4610 "allowedusers",
4611 rpc_share_allowedusers,
4612 NET_TRANSPORT_RPC,
4613 "Modify allowed users",
4614 "net rpc share allowedusers\n"
4615 " Modify allowed users"
4618 "migrate",
4619 rpc_share_migrate,
4620 NET_TRANSPORT_RPC,
4621 "Migrate share to local server",
4622 "net rpc share migrate\n"
4623 " Migrate share to local server"
4626 "list",
4627 rpc_share_list,
4628 NET_TRANSPORT_RPC,
4629 "List shares",
4630 "net rpc share list\n"
4631 " List shares"
4633 {NULL, NULL, 0, NULL, NULL}
4636 status = libnetapi_init(&c->netapi_ctx);
4637 if (status != 0) {
4638 return -1;
4640 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
4641 libnetapi_set_password(c->netapi_ctx, c->opt_password);
4642 if (c->opt_kerberos) {
4643 libnetapi_set_use_kerberos(c->netapi_ctx);
4646 if (argc == 0) {
4647 if (c->display_usage) {
4648 d_printf("Usage:\n"
4649 "net rpc share\n"
4650 " List shares\n"
4651 " Alias for net rpc share list\n");
4652 net_display_usage_from_functable(func);
4653 return 0;
4656 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4657 rpc_share_list_internals,
4658 argc, argv);
4661 return net_run_function(c, argc, argv, "net rpc share", func);
4664 static NTSTATUS rpc_sh_share_list(struct net_context *c,
4665 TALLOC_CTX *mem_ctx,
4666 struct rpc_sh_ctx *ctx,
4667 struct rpc_pipe_client *pipe_hnd,
4668 int argc, const char **argv)
4670 return rpc_share_list_internals(c, ctx->domain_sid, ctx->domain_name,
4671 ctx->cli, pipe_hnd, mem_ctx,
4672 argc, argv);
4675 static NTSTATUS rpc_sh_share_add(struct net_context *c,
4676 TALLOC_CTX *mem_ctx,
4677 struct rpc_sh_ctx *ctx,
4678 struct rpc_pipe_client *pipe_hnd,
4679 int argc, const char **argv)
4681 NET_API_STATUS status;
4682 uint32_t parm_err = 0;
4683 struct SHARE_INFO_2 i2;
4685 if ((argc < 2) || (argc > 3)) {
4686 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
4687 ctx->whoami);
4688 return NT_STATUS_INVALID_PARAMETER;
4691 i2.shi2_netname = argv[0];
4692 i2.shi2_type = STYPE_DISKTREE;
4693 i2.shi2_remark = (argc == 3) ? argv[2] : "";
4694 i2.shi2_permissions = 0;
4695 i2.shi2_max_uses = 0;
4696 i2.shi2_current_uses = 0;
4697 i2.shi2_path = argv[1];
4698 i2.shi2_passwd = NULL;
4700 status = NetShareAdd(pipe_hnd->desthost,
4702 (uint8_t *)&i2,
4703 &parm_err);
4705 return werror_to_ntstatus(W_ERROR(status));
4708 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
4709 TALLOC_CTX *mem_ctx,
4710 struct rpc_sh_ctx *ctx,
4711 struct rpc_pipe_client *pipe_hnd,
4712 int argc, const char **argv)
4714 WERROR result;
4715 NTSTATUS status;
4717 if (argc != 1) {
4718 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4719 return NT_STATUS_INVALID_PARAMETER;
4722 status = rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
4723 pipe_hnd->desthost,
4724 argv[0],
4726 &result);
4728 return status;
4731 static NTSTATUS rpc_sh_share_info(struct net_context *c,
4732 TALLOC_CTX *mem_ctx,
4733 struct rpc_sh_ctx *ctx,
4734 struct rpc_pipe_client *pipe_hnd,
4735 int argc, const char **argv)
4737 union srvsvc_NetShareInfo info;
4738 WERROR result;
4739 NTSTATUS status;
4741 if (argc != 1) {
4742 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
4743 return NT_STATUS_INVALID_PARAMETER;
4746 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4747 pipe_hnd->desthost,
4748 argv[0],
4750 &info,
4751 &result);
4752 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4753 goto done;
4756 d_printf("Name: %s\n", info.info2->name);
4757 d_printf("Comment: %s\n", info.info2->comment);
4758 d_printf("Path: %s\n", info.info2->path);
4759 d_printf("Password: %s\n", info.info2->password);
4761 done:
4762 return werror_to_ntstatus(result);
4765 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
4766 struct rpc_sh_ctx *ctx)
4768 static struct rpc_sh_cmd cmds[] = {
4770 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
4771 "List available shares" },
4773 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
4774 "Add a share" },
4776 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
4777 "Delete a share" },
4779 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
4780 "Get information about a share" },
4782 { NULL, NULL, 0, NULL, NULL }
4785 return cmds;
4788 /****************************************************************************/
4790 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
4792 return net_file_usage(c, argc, argv);
4796 * Close a file on a remote RPC server.
4798 * All parameters are provided by the run_rpc_command function, except for
4799 * argc, argv which are passed through.
4801 * @param c A net_context structure.
4802 * @param domain_sid The domain sid acquired from the remote server.
4803 * @param cli A cli_state connected to the server.
4804 * @param mem_ctx Talloc context, destroyed on completion of the function.
4805 * @param argc Standard main() style argc.
4806 * @param argv Standard main() style argv. Initial components are already
4807 * stripped.
4809 * @return Normal NTSTATUS return.
4811 static NTSTATUS rpc_file_close_internals(struct net_context *c,
4812 const DOM_SID *domain_sid,
4813 const char *domain_name,
4814 struct cli_state *cli,
4815 struct rpc_pipe_client *pipe_hnd,
4816 TALLOC_CTX *mem_ctx,
4817 int argc,
4818 const char **argv)
4820 return rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx,
4821 pipe_hnd->desthost,
4822 atoi(argv[0]), NULL);
4826 * Close a file on a remote RPC server.
4828 * @param argc Standard main() style argc.
4829 * @param argv Standard main() style argv. Initial components are already
4830 * stripped.
4832 * @return A shell status integer (0 for success).
4834 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
4836 if (argc < 1 || c->display_usage) {
4837 return rpc_file_usage(c, argc, argv);
4840 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4841 rpc_file_close_internals,
4842 argc, argv);
4846 * Formatted print of open file info
4848 * @param r struct srvsvc_NetFileInfo3 contents
4851 static void display_file_info_3(struct srvsvc_NetFileInfo3 *r)
4853 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
4854 r->fid, r->user, r->permissions, r->num_locks, r->path);
4858 * List open files on a remote RPC server.
4860 * All parameters are provided by the run_rpc_command function, except for
4861 * argc, argv which are passed through.
4863 * @param c A net_context structure.
4864 * @param domain_sid The domain sid acquired from the remote server.
4865 * @param cli A cli_state connected to the server.
4866 * @param mem_ctx Talloc context, destroyed on completion of the function.
4867 * @param argc Standard main() style argc.
4868 * @param argv Standard main() style argv. Initial components are already
4869 * stripped.
4871 * @return Normal NTSTATUS return.
4874 static NTSTATUS rpc_file_list_internals(struct net_context *c,
4875 const DOM_SID *domain_sid,
4876 const char *domain_name,
4877 struct cli_state *cli,
4878 struct rpc_pipe_client *pipe_hnd,
4879 TALLOC_CTX *mem_ctx,
4880 int argc,
4881 const char **argv)
4883 struct srvsvc_NetFileInfoCtr info_ctr;
4884 struct srvsvc_NetFileCtr3 ctr3;
4885 WERROR result;
4886 NTSTATUS status;
4887 uint32 preferred_len = 0xffffffff, i;
4888 const char *username=NULL;
4889 uint32_t total_entries = 0;
4890 uint32_t resume_handle = 0;
4892 /* if argc > 0, must be user command */
4893 if (argc > 0)
4894 username = smb_xstrdup(argv[0]);
4896 ZERO_STRUCT(info_ctr);
4897 ZERO_STRUCT(ctr3);
4899 info_ctr.level = 3;
4900 info_ctr.ctr.ctr3 = &ctr3;
4902 status = rpccli_srvsvc_NetFileEnum(pipe_hnd, mem_ctx,
4903 pipe_hnd->desthost,
4904 NULL,
4905 username,
4906 &info_ctr,
4907 preferred_len,
4908 &total_entries,
4909 &resume_handle,
4910 &result);
4912 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
4913 goto done;
4915 /* Display results */
4917 d_printf(
4918 "\nEnumerating open files on remote server:\n\n"
4919 "\nFileId Opened by Perms Locks Path"
4920 "\n------ --------- ----- ----- ---- \n");
4921 for (i = 0; i < total_entries; i++)
4922 display_file_info_3(&info_ctr.ctr.ctr3->array[i]);
4923 done:
4924 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
4928 * List files for a user on a remote RPC server.
4930 * @param argc Standard main() style argc.
4931 * @param argv Standard main() style argv. Initial components are already
4932 * stripped.
4934 * @return A shell status integer (0 for success)..
4937 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
4939 if (argc < 1 || c->display_usage) {
4940 return rpc_file_usage(c, argc, argv);
4943 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4944 rpc_file_list_internals,
4945 argc, argv);
4949 * 'net rpc file' entrypoint.
4950 * @param argc Standard main() style argc.
4951 * @param argv Standard main() style argv. Initial components are already
4952 * stripped.
4955 int net_rpc_file(struct net_context *c, int argc, const char **argv)
4957 struct functable func[] = {
4959 "close",
4960 rpc_file_close,
4961 NET_TRANSPORT_RPC,
4962 "Close opened file",
4963 "net rpc file close\n"
4964 " Close opened file"
4967 "user",
4968 rpc_file_user,
4969 NET_TRANSPORT_RPC,
4970 "List files opened by user",
4971 "net rpc file user\n"
4972 " List files opened by user"
4974 #if 0
4976 "info",
4977 rpc_file_info,
4978 NET_TRANSPORT_RPC,
4979 "Display information about opened file",
4980 "net rpc file info\n"
4981 " Display information about opened file"
4983 #endif
4984 {NULL, NULL, 0, NULL, NULL}
4987 if (argc == 0) {
4988 if (c->display_usage) {
4989 d_printf("Usage:\n");
4990 d_printf("net rpc file\n"
4991 " List opened files\n");
4992 net_display_usage_from_functable(func);
4993 return 0;
4996 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4997 rpc_file_list_internals,
4998 argc, argv);
5001 return net_run_function(c, argc, argv, "net rpc file", func);
5005 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5007 * All parameters are provided by the run_rpc_command function, except for
5008 * argc, argv which are passed through.
5010 * @param c A net_context structure.
5011 * @param domain_sid The domain sid acquired from the remote server.
5012 * @param cli A cli_state connected to the server.
5013 * @param mem_ctx Talloc context, destroyed on completion of the function.
5014 * @param argc Standard main() style argc.
5015 * @param argv Standard main() style argv. Initial components are already
5016 * stripped.
5018 * @return Normal NTSTATUS return.
5021 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5022 const DOM_SID *domain_sid,
5023 const char *domain_name,
5024 struct cli_state *cli,
5025 struct rpc_pipe_client *pipe_hnd,
5026 TALLOC_CTX *mem_ctx,
5027 int argc,
5028 const char **argv)
5030 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5032 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5034 if (NT_STATUS_IS_OK(result)) {
5035 d_printf("\nShutdown successfully aborted\n");
5036 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5037 } else
5038 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5040 return result;
5044 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5046 * All parameters are provided by the run_rpc_command function, except for
5047 * argc, argv which are passed through.
5049 * @param c A net_context structure.
5050 * @param domain_sid The domain sid acquired from the remote server.
5051 * @param cli A cli_state connected to the server.
5052 * @param mem_ctx Talloc context, destroyed on completion of the function.
5053 * @param argc Standard main() style argc.
5054 * @param argv Standard main() style argv. Initial components are already
5055 * stripped.
5057 * @return Normal NTSTATUS return.
5060 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5061 const DOM_SID *domain_sid,
5062 const char *domain_name,
5063 struct cli_state *cli,
5064 struct rpc_pipe_client *pipe_hnd,
5065 TALLOC_CTX *mem_ctx,
5066 int argc,
5067 const char **argv)
5069 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5071 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5073 if (NT_STATUS_IS_OK(result)) {
5074 d_printf("\nShutdown successfully aborted\n");
5075 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5076 } else
5077 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5079 return result;
5083 * ABORT the shutdown of a remote RPC server.
5085 * @param argc Standard main() style argc.
5086 * @param argv Standard main() style argv. Initial components are already
5087 * stripped.
5089 * @return A shell status integer (0 for success).
5092 static int rpc_shutdown_abort(struct net_context *c, int argc,
5093 const char **argv)
5095 int rc = -1;
5097 if (c->display_usage) {
5098 d_printf("Usage:\n"
5099 "net rpc abortshutdown\n"
5100 " Abort a scheduled shutdown\n");
5101 return 0;
5104 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5105 rpc_shutdown_abort_internals, argc, argv);
5107 if (rc == 0)
5108 return rc;
5110 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5112 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5113 rpc_reg_shutdown_abort_internals,
5114 argc, argv);
5118 * Shut down a remote RPC Server via initshutdown pipe.
5120 * All parameters are provided by the run_rpc_command function, except for
5121 * argc, argv which are passed through.
5123 * @param c A net_context structure.
5124 * @param domain_sid The domain sid acquired from the remote server.
5125 * @param cli A cli_state connected to the server.
5126 * @param mem_ctx Talloc context, destroyed on completion of the function.
5127 * @param argc Standard main() style argc.
5128 * @param argv Standard main() style argv. Initial components are already
5129 * stripped.
5131 * @return Normal NTSTATUS return.
5134 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5135 const DOM_SID *domain_sid,
5136 const char *domain_name,
5137 struct cli_state *cli,
5138 struct rpc_pipe_client *pipe_hnd,
5139 TALLOC_CTX *mem_ctx,
5140 int argc,
5141 const char **argv)
5143 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5144 const char *msg = "This machine will be shutdown shortly";
5145 uint32 timeout = 20;
5146 struct initshutdown_String msg_string;
5147 struct initshutdown_String_sub s;
5149 if (c->opt_comment) {
5150 msg = c->opt_comment;
5152 if (c->opt_timeout) {
5153 timeout = c->opt_timeout;
5156 s.name = msg;
5157 msg_string.name = &s;
5159 /* create an entry */
5160 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5161 &msg_string, timeout, c->opt_force, c->opt_reboot,
5162 NULL);
5164 if (NT_STATUS_IS_OK(result)) {
5165 d_printf("\nShutdown of remote machine succeeded\n");
5166 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5167 } else {
5168 DEBUG(1,("Shutdown of remote machine failed!\n"));
5170 return result;
5174 * Shut down a remote RPC Server via winreg pipe.
5176 * All parameters are provided by the run_rpc_command function, except for
5177 * argc, argv which are passed through.
5179 * @param c A net_context structure.
5180 * @param domain_sid The domain sid acquired from the remote server.
5181 * @param cli A cli_state connected to the server.
5182 * @param mem_ctx Talloc context, destroyed on completion of the function.
5183 * @param argc Standard main() style argc.
5184 * @param argv Standard main() style argv. Initial components are already
5185 * stripped.
5187 * @return Normal NTSTATUS return.
5190 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5191 const DOM_SID *domain_sid,
5192 const char *domain_name,
5193 struct cli_state *cli,
5194 struct rpc_pipe_client *pipe_hnd,
5195 TALLOC_CTX *mem_ctx,
5196 int argc,
5197 const char **argv)
5199 const char *msg = "This machine will be shutdown shortly";
5200 uint32 timeout = 20;
5201 struct initshutdown_String msg_string;
5202 struct initshutdown_String_sub s;
5203 NTSTATUS result;
5204 WERROR werr;
5206 if (c->opt_comment) {
5207 msg = c->opt_comment;
5209 s.name = msg;
5210 msg_string.name = &s;
5212 if (c->opt_timeout) {
5213 timeout = c->opt_timeout;
5216 /* create an entry */
5217 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5218 &msg_string, timeout, c->opt_force, c->opt_reboot,
5219 &werr);
5221 if (NT_STATUS_IS_OK(result)) {
5222 d_printf("\nShutdown of remote machine succeeded\n");
5223 } else {
5224 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5225 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5226 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5227 else
5228 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(werr));
5231 return result;
5235 * Shut down a remote RPC server.
5237 * @param argc Standard main() style argc.
5238 * @param argv Standard main() style argv. Initial components are already
5239 * stripped.
5241 * @return A shell status integer (0 for success).
5244 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5246 int rc = -1;
5248 if (c->display_usage) {
5249 d_printf("Usage:\n"
5250 "net rpc shutdown\n"
5251 " Shut down a remote RPC server\n");
5252 return 0;
5255 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5256 rpc_init_shutdown_internals, argc, argv);
5258 if (rc) {
5259 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5260 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5261 rpc_reg_shutdown_internals, argc, argv);
5264 return rc;
5267 /***************************************************************************
5268 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5269 ***************************************************************************/
5272 * Add interdomain trust account to the RPC server.
5273 * All parameters (except for argc and argv) are passed by run_rpc_command
5274 * function.
5276 * @param c A net_context structure.
5277 * @param domain_sid The domain sid acquired from the server.
5278 * @param cli A cli_state connected to the server.
5279 * @param mem_ctx Talloc context, destroyed on completion of the function.
5280 * @param argc Standard main() style argc.
5281 * @param argv Standard main() style argv. Initial components are already
5282 * stripped.
5284 * @return normal NTSTATUS return code.
5287 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5288 const DOM_SID *domain_sid,
5289 const char *domain_name,
5290 struct cli_state *cli,
5291 struct rpc_pipe_client *pipe_hnd,
5292 TALLOC_CTX *mem_ctx,
5293 int argc,
5294 const char **argv)
5296 POLICY_HND connect_pol, domain_pol, user_pol;
5297 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5298 char *acct_name;
5299 struct lsa_String lsa_acct_name;
5300 uint32 acb_info;
5301 uint32 acct_flags=0;
5302 uint32 user_rid;
5303 uint32_t access_granted = 0;
5304 union samr_UserInfo info;
5305 unsigned int orig_timeout;
5307 if (argc != 2) {
5308 d_printf("Usage: net rpc trustdom add <domain_name> "
5309 "<trust password>\n");
5310 return NT_STATUS_INVALID_PARAMETER;
5314 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5317 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5318 return NT_STATUS_NO_MEMORY;
5321 strupper_m(acct_name);
5323 init_lsa_String(&lsa_acct_name, acct_name);
5325 /* Get samr policy handle */
5326 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5327 pipe_hnd->desthost,
5328 MAXIMUM_ALLOWED_ACCESS,
5329 &connect_pol);
5330 if (!NT_STATUS_IS_OK(result)) {
5331 goto done;
5334 /* Get domain policy handle */
5335 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5336 &connect_pol,
5337 MAXIMUM_ALLOWED_ACCESS,
5338 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5339 &domain_pol);
5340 if (!NT_STATUS_IS_OK(result)) {
5341 goto done;
5344 /* This call can take a long time - allow the server to time out.
5345 * 35 seconds should do it. */
5347 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5349 /* Create trusting domain's account */
5350 acb_info = ACB_NORMAL;
5351 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5352 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5353 SAMR_USER_ACCESS_SET_PASSWORD |
5354 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5355 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5357 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5358 &domain_pol,
5359 &lsa_acct_name,
5360 acb_info,
5361 acct_flags,
5362 &user_pol,
5363 &access_granted,
5364 &user_rid);
5366 /* And restore our original timeout. */
5367 rpccli_set_timeout(pipe_hnd, orig_timeout);
5369 if (!NT_STATUS_IS_OK(result)) {
5370 d_printf("net rpc trustdom add: create user %s failed %s\n",
5371 acct_name, nt_errstr(result));
5372 goto done;
5376 NTTIME notime;
5377 struct samr_LogonHours hours;
5378 struct lsa_BinaryString parameters;
5379 const int units_per_week = 168;
5380 struct samr_CryptPassword crypt_pwd;
5382 ZERO_STRUCT(notime);
5383 ZERO_STRUCT(hours);
5384 ZERO_STRUCT(parameters);
5386 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
5387 if (!hours.bits) {
5388 result = NT_STATUS_NO_MEMORY;
5389 goto done;
5391 hours.units_per_week = units_per_week;
5392 memset(hours.bits, 0xFF, units_per_week);
5394 init_samr_CryptPassword(argv[1],
5395 &cli->user_session_key,
5396 &crypt_pwd);
5398 init_samr_user_info23(&info.info23,
5399 notime, notime, notime,
5400 notime, notime, notime,
5401 NULL, NULL, NULL, NULL, NULL,
5402 NULL, NULL, NULL, NULL, &parameters,
5403 0, 0, ACB_DOMTRUST, SAMR_FIELD_ACCT_FLAGS,
5404 hours,
5405 0, 0, 0, 0, 0, 0, 0,
5406 crypt_pwd.data, 24);
5408 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5409 &user_pol,
5411 &info);
5413 if (!NT_STATUS_IS_OK(result)) {
5414 DEBUG(0,("Could not set trust account password: %s\n",
5415 nt_errstr(result)));
5416 goto done;
5420 done:
5421 SAFE_FREE(acct_name);
5422 return result;
5426 * Create interdomain trust account for a remote domain.
5428 * @param argc Standard argc.
5429 * @param argv Standard argv without initial components.
5431 * @return Integer status (0 means success).
5434 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5436 if (argc > 0 && !c->display_usage) {
5437 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5438 rpc_trustdom_add_internals, argc, argv);
5439 } else {
5440 d_printf("Usage:\n"
5441 "net rpc trustdom add <domain_name> <trust password>\n");
5442 return -1;
5448 * Remove interdomain trust account from the RPC server.
5449 * All parameters (except for argc and argv) are passed by run_rpc_command
5450 * function.
5452 * @param c A net_context structure.
5453 * @param domain_sid The domain sid acquired from the server.
5454 * @param cli A cli_state connected to the server.
5455 * @param mem_ctx Talloc context, destroyed on completion of the function.
5456 * @param argc Standard main() style argc.
5457 * @param argv Standard main() style argv. Initial components are already
5458 * stripped.
5460 * @return normal NTSTATUS return code.
5463 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5464 const DOM_SID *domain_sid,
5465 const char *domain_name,
5466 struct cli_state *cli,
5467 struct rpc_pipe_client *pipe_hnd,
5468 TALLOC_CTX *mem_ctx,
5469 int argc,
5470 const char **argv)
5472 POLICY_HND connect_pol, domain_pol, user_pol;
5473 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5474 char *acct_name;
5475 DOM_SID trust_acct_sid;
5476 struct samr_Ids user_rids, name_types;
5477 struct lsa_String lsa_acct_name;
5479 if (argc != 1) {
5480 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5481 return NT_STATUS_INVALID_PARAMETER;
5485 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5487 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5489 if (acct_name == NULL)
5490 return NT_STATUS_NO_MEMORY;
5492 strupper_m(acct_name);
5494 /* Get samr policy handle */
5495 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5496 pipe_hnd->desthost,
5497 MAXIMUM_ALLOWED_ACCESS,
5498 &connect_pol);
5499 if (!NT_STATUS_IS_OK(result)) {
5500 goto done;
5503 /* Get domain policy handle */
5504 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5505 &connect_pol,
5506 MAXIMUM_ALLOWED_ACCESS,
5507 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5508 &domain_pol);
5509 if (!NT_STATUS_IS_OK(result)) {
5510 goto done;
5513 init_lsa_String(&lsa_acct_name, acct_name);
5515 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5516 &domain_pol,
5518 &lsa_acct_name,
5519 &user_rids,
5520 &name_types);
5522 if (!NT_STATUS_IS_OK(result)) {
5523 d_printf("net rpc trustdom del: LookupNames on user %s failed %s\n",
5524 acct_name, nt_errstr(result) );
5525 goto done;
5528 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5529 &domain_pol,
5530 MAXIMUM_ALLOWED_ACCESS,
5531 user_rids.ids[0],
5532 &user_pol);
5534 if (!NT_STATUS_IS_OK(result)) {
5535 d_printf("net rpc trustdom del: OpenUser on user %s failed %s\n",
5536 acct_name, nt_errstr(result) );
5537 goto done;
5540 /* append the rid to the domain sid */
5541 sid_copy(&trust_acct_sid, domain_sid);
5542 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5543 goto done;
5546 /* remove the sid */
5548 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5549 &user_pol,
5550 &trust_acct_sid);
5551 if (!NT_STATUS_IS_OK(result)) {
5552 d_printf("net rpc trustdom del: RemoveMemberFromForeignDomain on user %s failed %s\n",
5553 acct_name, nt_errstr(result) );
5554 goto done;
5557 /* Delete user */
5559 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5560 &user_pol);
5562 if (!NT_STATUS_IS_OK(result)) {
5563 d_printf("net rpc trustdom del: DeleteUser on user %s failed %s\n",
5564 acct_name, nt_errstr(result) );
5565 goto done;
5568 if (!NT_STATUS_IS_OK(result)) {
5569 d_printf("Could not set trust account password: %s\n",
5570 nt_errstr(result));
5571 goto done;
5574 done:
5575 return result;
5579 * Delete interdomain trust account for a remote domain.
5581 * @param argc Standard argc.
5582 * @param argv Standard argv without initial components.
5584 * @return Integer status (0 means success).
5587 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5589 if (argc > 0 && !c->display_usage) {
5590 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5591 rpc_trustdom_del_internals, argc, argv);
5592 } else {
5593 d_printf("Usage:\n"
5594 "net rpc trustdom del <domain>\n");
5595 return -1;
5599 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5600 struct cli_state *cli,
5601 TALLOC_CTX *mem_ctx,
5602 const char *domain_name)
5604 char *dc_name = NULL;
5605 const char *buffer = NULL;
5606 struct rpc_pipe_client *netr;
5607 NTSTATUS status;
5609 /* Use NetServerEnum2 */
5611 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5612 SAFE_FREE(dc_name);
5613 return NT_STATUS_OK;
5616 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5617 for domain %s\n", domain_name));
5619 /* Try netr_GetDcName */
5621 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5622 &netr);
5623 if (!NT_STATUS_IS_OK(status)) {
5624 return status;
5627 status = rpccli_netr_GetDcName(netr, mem_ctx,
5628 cli->desthost,
5629 domain_name,
5630 &buffer,
5631 NULL);
5632 TALLOC_FREE(netr);
5634 if (NT_STATUS_IS_OK(status)) {
5635 return status;
5638 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5639 for domain %s\n", domain_name));
5641 return status;
5645 * Establish trust relationship to a trusting domain.
5646 * Interdomain account must already be created on remote PDC.
5648 * @param c A net_context structure.
5649 * @param argc Standard argc.
5650 * @param argv Standard argv without initial components.
5652 * @return Integer status (0 means success).
5655 static int rpc_trustdom_establish(struct net_context *c, int argc,
5656 const char **argv)
5658 struct cli_state *cli = NULL;
5659 struct sockaddr_storage server_ss;
5660 struct rpc_pipe_client *pipe_hnd = NULL;
5661 POLICY_HND connect_hnd;
5662 TALLOC_CTX *mem_ctx;
5663 NTSTATUS nt_status;
5664 DOM_SID *domain_sid;
5666 char* domain_name;
5667 char* acct_name;
5668 fstring pdc_name;
5669 union lsa_PolicyInformation *info = NULL;
5672 * Connect to \\server\ipc$ as 'our domain' account with password
5675 if (argc != 1 || c->display_usage) {
5676 d_printf("Usage:\n"
5677 "net rpc trustdom establish <domain_name>\n");
5678 return -1;
5681 domain_name = smb_xstrdup(argv[0]);
5682 strupper_m(domain_name);
5684 /* account name used at first is our domain's name with '$' */
5685 asprintf(&acct_name, "%s$", lp_workgroup());
5686 strupper_m(acct_name);
5689 * opt_workgroup will be used by connection functions further,
5690 * hence it should be set to remote domain name instead of ours
5692 if (c->opt_workgroup) {
5693 c->opt_workgroup = smb_xstrdup(domain_name);
5696 c->opt_user_name = acct_name;
5698 /* find the domain controller */
5699 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
5700 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
5701 return -1;
5704 /* connect to ipc$ as username/password */
5705 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
5706 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
5708 /* Is it trusting domain account for sure ? */
5709 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
5710 nt_errstr(nt_status)));
5711 return -1;
5714 /* store who we connected to */
5716 saf_store( domain_name, pdc_name );
5719 * Connect to \\server\ipc$ again (this time anonymously)
5722 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
5723 (char*)pdc_name);
5725 if (NT_STATUS_IS_ERR(nt_status)) {
5726 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
5727 domain_name, nt_errstr(nt_status)));
5728 return -1;
5731 if (!(mem_ctx = talloc_init("establishing trust relationship to "
5732 "domain %s", domain_name))) {
5733 DEBUG(0, ("talloc_init() failed\n"));
5734 cli_shutdown(cli);
5735 return -1;
5738 /* Make sure we're talking to a proper server */
5740 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
5741 if (!NT_STATUS_IS_OK(nt_status)) {
5742 cli_shutdown(cli);
5743 talloc_destroy(mem_ctx);
5744 return -1;
5748 * Call LsaOpenPolicy and LsaQueryInfo
5751 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
5752 &pipe_hnd);
5753 if (!NT_STATUS_IS_OK(nt_status)) {
5754 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
5755 cli_shutdown(cli);
5756 talloc_destroy(mem_ctx);
5757 return -1;
5760 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, SEC_RIGHTS_QUERY_VALUE,
5761 &connect_hnd);
5762 if (NT_STATUS_IS_ERR(nt_status)) {
5763 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
5764 nt_errstr(nt_status)));
5765 cli_shutdown(cli);
5766 talloc_destroy(mem_ctx);
5767 return -1;
5770 /* Querying info level 5 */
5772 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
5773 &connect_hnd,
5774 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
5775 &info);
5776 if (NT_STATUS_IS_ERR(nt_status)) {
5777 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
5778 nt_errstr(nt_status)));
5779 cli_shutdown(cli);
5780 talloc_destroy(mem_ctx);
5781 return -1;
5784 domain_sid = info->account_domain.sid;
5786 /* There should be actually query info level 3 (following nt serv behaviour),
5787 but I still don't know if it's _really_ necessary */
5790 * Store the password in secrets db
5793 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
5794 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5795 cli_shutdown(cli);
5796 talloc_destroy(mem_ctx);
5797 return -1;
5801 * Close the pipes and clean up
5804 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
5805 if (NT_STATUS_IS_ERR(nt_status)) {
5806 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
5807 nt_errstr(nt_status)));
5808 cli_shutdown(cli);
5809 talloc_destroy(mem_ctx);
5810 return -1;
5813 cli_shutdown(cli);
5815 talloc_destroy(mem_ctx);
5817 d_printf("Trust to domain %s established\n", domain_name);
5818 return 0;
5822 * Revoke trust relationship to the remote domain.
5824 * @param c A net_context structure.
5825 * @param argc Standard argc.
5826 * @param argv Standard argv without initial components.
5828 * @return Integer status (0 means success).
5831 static int rpc_trustdom_revoke(struct net_context *c, int argc,
5832 const char **argv)
5834 char* domain_name;
5835 int rc = -1;
5837 if (argc < 1 || c->display_usage) {
5838 d_printf("Usage:\n"
5839 "net rpc trustdom revoke <domain_name>\n"
5840 " Revoke trust relationship\n"
5841 " domain_name\tName of domain to revoke trust\n");
5842 return -1;
5845 /* generate upper cased domain name */
5846 domain_name = smb_xstrdup(argv[0]);
5847 strupper_m(domain_name);
5849 /* delete password of the trust */
5850 if (!pdb_del_trusteddom_pw(domain_name)) {
5851 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
5852 domain_name));
5853 goto done;
5856 rc = 0;
5857 done:
5858 SAFE_FREE(domain_name);
5859 return rc;
5862 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
5863 const DOM_SID *domain_sid,
5864 const char *domain_name,
5865 struct cli_state *cli,
5866 struct rpc_pipe_client *pipe_hnd,
5867 TALLOC_CTX *mem_ctx,
5868 int argc,
5869 const char **argv)
5871 fstring str_sid;
5872 sid_to_fstring(str_sid, domain_sid);
5873 d_printf("%s\n", str_sid);
5874 return NT_STATUS_OK;
5877 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
5879 fstring ascii_sid, padding;
5880 int pad_len, col_len = 20;
5882 /* convert sid into ascii string */
5883 sid_to_fstring(ascii_sid, dom_sid);
5885 /* calculate padding space for d_printf to look nicer */
5886 pad_len = col_len - strlen(trusted_dom_name);
5887 padding[pad_len] = 0;
5888 do padding[--pad_len] = ' '; while (pad_len);
5890 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
5893 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
5894 TALLOC_CTX *mem_ctx,
5895 POLICY_HND *pol,
5896 DOM_SID dom_sid,
5897 const char *trusted_dom_name)
5899 NTSTATUS nt_status;
5900 union lsa_TrustedDomainInfo *info = NULL;
5901 char *cleartextpwd = NULL;
5902 uint8_t nt_hash[16];
5903 DATA_BLOB data;
5905 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
5906 pol,
5907 &dom_sid,
5908 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
5909 &info);
5910 if (NT_STATUS_IS_ERR(nt_status)) {
5911 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
5912 nt_errstr(nt_status)));
5913 goto done;
5916 data = data_blob(info->password.password->data,
5917 info->password.password->length);
5919 if (!rpccli_get_pwd_hash(pipe_hnd, nt_hash)) {
5920 DEBUG(0, ("Could not retrieve password hash\n"));
5921 goto done;
5924 cleartextpwd = decrypt_trustdom_secret(nt_hash, &data);
5926 if (cleartextpwd == NULL) {
5927 DEBUG(0,("retrieved NULL password\n"));
5928 nt_status = NT_STATUS_UNSUCCESSFUL;
5929 goto done;
5932 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
5933 DEBUG(0, ("Storing password for trusted domain failed.\n"));
5934 nt_status = NT_STATUS_UNSUCCESSFUL;
5935 goto done;
5938 #ifdef DEBUG_PASSWORD
5939 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
5940 "password: [%s]\n", trusted_dom_name,
5941 sid_string_dbg(&dom_sid), cleartextpwd));
5942 #endif
5944 done:
5945 SAFE_FREE(cleartextpwd);
5946 data_blob_free(&data);
5948 return nt_status;
5951 static int rpc_trustdom_vampire(struct net_context *c, int argc,
5952 const char **argv)
5954 /* common variables */
5955 TALLOC_CTX* mem_ctx;
5956 struct cli_state *cli = NULL;
5957 struct rpc_pipe_client *pipe_hnd = NULL;
5958 NTSTATUS nt_status;
5959 const char *domain_name = NULL;
5960 DOM_SID *queried_dom_sid;
5961 POLICY_HND connect_hnd;
5962 union lsa_PolicyInformation *info = NULL;
5964 /* trusted domains listing variables */
5965 unsigned int enum_ctx = 0;
5966 int i;
5967 struct lsa_DomainList dom_list;
5968 fstring pdc_name;
5970 if (c->display_usage) {
5971 d_printf("Usage:\n"
5972 "net rpc trustdom vampire\n"
5973 " Vampire trust relationship from remote server\n");
5974 return 0;
5978 * Listing trusted domains (stored in secrets.tdb, if local)
5981 mem_ctx = talloc_init("trust relationships vampire");
5984 * set domain and pdc name to local samba server (default)
5985 * or to remote one given in command line
5988 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
5989 domain_name = c->opt_workgroup;
5990 c->opt_target_workgroup = c->opt_workgroup;
5991 } else {
5992 fstrcpy(pdc_name, global_myname());
5993 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
5994 c->opt_target_workgroup = domain_name;
5997 /* open \PIPE\lsarpc and open policy handle */
5998 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
5999 if (!NT_STATUS_IS_OK(nt_status)) {
6000 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6001 nt_errstr(nt_status)));
6002 talloc_destroy(mem_ctx);
6003 return -1;
6006 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6007 &pipe_hnd);
6008 if (!NT_STATUS_IS_OK(nt_status)) {
6009 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6010 nt_errstr(nt_status) ));
6011 cli_shutdown(cli);
6012 talloc_destroy(mem_ctx);
6013 return -1;
6016 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6017 &connect_hnd);
6018 if (NT_STATUS_IS_ERR(nt_status)) {
6019 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6020 nt_errstr(nt_status)));
6021 cli_shutdown(cli);
6022 talloc_destroy(mem_ctx);
6023 return -1;
6026 /* query info level 5 to obtain sid of a domain being queried */
6027 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6028 &connect_hnd,
6029 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6030 &info);
6032 if (NT_STATUS_IS_ERR(nt_status)) {
6033 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6034 nt_errstr(nt_status)));
6035 cli_shutdown(cli);
6036 talloc_destroy(mem_ctx);
6037 return -1;
6040 queried_dom_sid = info->account_domain.sid;
6043 * Keep calling LsaEnumTrustdom over opened pipe until
6044 * the end of enumeration is reached
6047 d_printf("Vampire trusted domains:\n\n");
6049 do {
6050 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6051 &connect_hnd,
6052 &enum_ctx,
6053 &dom_list,
6054 (uint32_t)-1);
6055 if (NT_STATUS_IS_ERR(nt_status)) {
6056 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6057 nt_errstr(nt_status)));
6058 cli_shutdown(cli);
6059 talloc_destroy(mem_ctx);
6060 return -1;
6063 for (i = 0; i < dom_list.count; i++) {
6065 print_trusted_domain(dom_list.domains[i].sid,
6066 dom_list.domains[i].name.string);
6068 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6069 *dom_list.domains[i].sid,
6070 dom_list.domains[i].name.string);
6071 if (!NT_STATUS_IS_OK(nt_status)) {
6072 cli_shutdown(cli);
6073 talloc_destroy(mem_ctx);
6074 return -1;
6079 * in case of no trusted domains say something rather
6080 * than just display blank line
6082 if (!dom_list.count) d_printf("none\n");
6084 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6086 /* close this connection before doing next one */
6087 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6088 if (NT_STATUS_IS_ERR(nt_status)) {
6089 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6090 nt_errstr(nt_status)));
6091 cli_shutdown(cli);
6092 talloc_destroy(mem_ctx);
6093 return -1;
6096 /* close lsarpc pipe and connection to IPC$ */
6097 cli_shutdown(cli);
6099 talloc_destroy(mem_ctx);
6100 return 0;
6103 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6105 /* common variables */
6106 TALLOC_CTX* mem_ctx;
6107 struct cli_state *cli = NULL, *remote_cli = NULL;
6108 struct rpc_pipe_client *pipe_hnd = NULL;
6109 NTSTATUS nt_status;
6110 const char *domain_name = NULL;
6111 DOM_SID *queried_dom_sid;
6112 fstring padding;
6113 int ascii_dom_name_len;
6114 POLICY_HND connect_hnd;
6115 union lsa_PolicyInformation *info = NULL;
6117 /* trusted domains listing variables */
6118 unsigned int num_domains, enum_ctx = 0;
6119 int i, pad_len, col_len = 20;
6120 struct lsa_DomainList dom_list;
6121 fstring pdc_name;
6123 /* trusting domains listing variables */
6124 POLICY_HND domain_hnd;
6125 struct samr_SamArray *trusts = NULL;
6127 if (c->display_usage) {
6128 d_printf("Usage:\n"
6129 "net rpc trustdom list\n"
6130 " List trust relationships\n");
6131 return 0;
6135 * Listing trusted domains (stored in secrets.tdb, if local)
6138 mem_ctx = talloc_init("trust relationships listing");
6141 * set domain and pdc name to local samba server (default)
6142 * or to remote one given in command line
6145 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6146 domain_name = c->opt_workgroup;
6147 c->opt_target_workgroup = c->opt_workgroup;
6148 } else {
6149 fstrcpy(pdc_name, global_myname());
6150 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6151 c->opt_target_workgroup = domain_name;
6154 /* open \PIPE\lsarpc and open policy handle */
6155 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6156 if (!NT_STATUS_IS_OK(nt_status)) {
6157 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6158 nt_errstr(nt_status)));
6159 talloc_destroy(mem_ctx);
6160 return -1;
6163 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6164 &pipe_hnd);
6165 if (!NT_STATUS_IS_OK(nt_status)) {
6166 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6167 nt_errstr(nt_status) ));
6168 cli_shutdown(cli);
6169 talloc_destroy(mem_ctx);
6170 return -1;
6173 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6174 &connect_hnd);
6175 if (NT_STATUS_IS_ERR(nt_status)) {
6176 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6177 nt_errstr(nt_status)));
6178 cli_shutdown(cli);
6179 talloc_destroy(mem_ctx);
6180 return -1;
6183 /* query info level 5 to obtain sid of a domain being queried */
6184 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6185 &connect_hnd,
6186 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6187 &info);
6189 if (NT_STATUS_IS_ERR(nt_status)) {
6190 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6191 nt_errstr(nt_status)));
6192 cli_shutdown(cli);
6193 talloc_destroy(mem_ctx);
6194 return -1;
6197 queried_dom_sid = info->account_domain.sid;
6200 * Keep calling LsaEnumTrustdom over opened pipe until
6201 * the end of enumeration is reached
6204 d_printf("Trusted domains list:\n\n");
6206 do {
6207 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6208 &connect_hnd,
6209 &enum_ctx,
6210 &dom_list,
6211 (uint32_t)-1);
6212 if (NT_STATUS_IS_ERR(nt_status)) {
6213 DEBUG(0, ("Couldn't enumerate trusted domains. 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 < dom_list.count; i++) {
6221 print_trusted_domain(dom_list.domains[i].sid,
6222 dom_list.domains[i].name.string);
6226 * in case of no trusted domains say something rather
6227 * than just display blank line
6229 if (!dom_list.count) d_printf("none\n");
6231 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6233 /* close this connection before doing next one */
6234 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6235 if (NT_STATUS_IS_ERR(nt_status)) {
6236 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6237 nt_errstr(nt_status)));
6238 cli_shutdown(cli);
6239 talloc_destroy(mem_ctx);
6240 return -1;
6243 TALLOC_FREE(pipe_hnd);
6246 * Listing trusting domains (stored in passdb backend, if local)
6249 d_printf("\nTrusting domains list:\n\n");
6252 * Open \PIPE\samr and get needed policy handles
6254 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6255 &pipe_hnd);
6256 if (!NT_STATUS_IS_OK(nt_status)) {
6257 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6258 cli_shutdown(cli);
6259 talloc_destroy(mem_ctx);
6260 return -1;
6263 /* SamrConnect2 */
6264 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6265 pipe_hnd->desthost,
6266 SA_RIGHT_SAM_OPEN_DOMAIN,
6267 &connect_hnd);
6268 if (!NT_STATUS_IS_OK(nt_status)) {
6269 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6270 nt_errstr(nt_status)));
6271 cli_shutdown(cli);
6272 talloc_destroy(mem_ctx);
6273 return -1;
6276 /* SamrOpenDomain - we have to open domain policy handle in order to be
6277 able to enumerate accounts*/
6278 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6279 &connect_hnd,
6280 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6281 queried_dom_sid,
6282 &domain_hnd);
6283 if (!NT_STATUS_IS_OK(nt_status)) {
6284 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6285 nt_errstr(nt_status)));
6286 cli_shutdown(cli);
6287 talloc_destroy(mem_ctx);
6288 return -1;
6292 * perform actual enumeration
6295 enum_ctx = 0; /* reset enumeration context from last enumeration */
6296 do {
6298 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6299 &domain_hnd,
6300 &enum_ctx,
6301 ACB_DOMTRUST,
6302 &trusts,
6303 0xffff,
6304 &num_domains);
6305 if (NT_STATUS_IS_ERR(nt_status)) {
6306 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6307 nt_errstr(nt_status)));
6308 cli_shutdown(cli);
6309 talloc_destroy(mem_ctx);
6310 return -1;
6313 for (i = 0; i < num_domains; i++) {
6315 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6318 * get each single domain's sid (do we _really_ need this ?):
6319 * 1) connect to domain's pdc
6320 * 2) query the pdc for domain's sid
6323 /* get rid of '$' tail */
6324 ascii_dom_name_len = strlen(str);
6325 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6326 str[ascii_dom_name_len - 1] = '\0';
6328 /* calculate padding space for d_printf to look nicer */
6329 pad_len = col_len - strlen(str);
6330 padding[pad_len] = 0;
6331 do padding[--pad_len] = ' '; while (pad_len);
6333 /* set opt_* variables to remote domain */
6334 strupper_m(str);
6335 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6336 c->opt_target_workgroup = c->opt_workgroup;
6338 d_printf("%s%s", str, padding);
6340 /* connect to remote domain controller */
6341 nt_status = net_make_ipc_connection(c,
6342 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6343 &remote_cli);
6344 if (NT_STATUS_IS_OK(nt_status)) {
6345 /* query for domain's sid */
6346 if (run_rpc_command(
6347 c, remote_cli,
6348 &ndr_table_lsarpc.syntax_id, 0,
6349 rpc_query_domain_sid, argc,
6350 argv))
6351 d_fprintf(stderr, "couldn't get domain's sid\n");
6353 cli_shutdown(remote_cli);
6355 } else {
6356 d_fprintf(stderr, "domain controller is not "
6357 "responding: %s\n",
6358 nt_errstr(nt_status));
6362 if (!num_domains) d_printf("none\n");
6364 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6366 /* close opened samr and domain policy handles */
6367 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6368 if (!NT_STATUS_IS_OK(nt_status)) {
6369 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6372 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6373 if (!NT_STATUS_IS_OK(nt_status)) {
6374 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6377 /* close samr pipe and connection to IPC$ */
6378 cli_shutdown(cli);
6380 talloc_destroy(mem_ctx);
6381 return 0;
6385 * Entrypoint for 'net rpc trustdom' code.
6387 * @param argc Standard argc.
6388 * @param argv Standard argv without initial components.
6390 * @return Integer status (0 means success).
6393 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6395 struct functable func[] = {
6397 "add",
6398 rpc_trustdom_add,
6399 NET_TRANSPORT_RPC,
6400 "Add trusted domain's account",
6401 "net rpc trustdom add\n"
6402 " Add trusted domain's account"
6405 "del",
6406 rpc_trustdom_del,
6407 NET_TRANSPORT_RPC,
6408 "Remove trusted domain's account",
6409 "net rpc trustdom del\n"
6410 " Remove trusted domain's account"
6413 "establish",
6414 rpc_trustdom_establish,
6415 NET_TRANSPORT_RPC,
6416 "Establish trust relationship",
6417 "net rpc trustdom establish\n"
6418 " Establish trust relationship"
6421 "revoke",
6422 rpc_trustdom_revoke,
6423 NET_TRANSPORT_RPC,
6424 "Revoke trust relationship",
6425 "net rpc trustdom revoke\n"
6426 " Revoke trust relationship"
6429 "list",
6430 rpc_trustdom_list,
6431 NET_TRANSPORT_RPC,
6432 "List domain trusts",
6433 "net rpc trustdom list\n"
6434 " List domain trusts"
6437 "vampire",
6438 rpc_trustdom_vampire,
6439 NET_TRANSPORT_RPC,
6440 "Vampire trusts from remote server",
6441 "net rpc trustdom vampire\n"
6442 " Vampire trusts from remote server"
6444 {NULL, NULL, 0, NULL, NULL}
6447 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6451 * Check if a server will take rpc commands
6452 * @param flags Type of server to connect to (PDC, DMB, localhost)
6453 * if the host is not explicitly specified
6454 * @return bool (true means rpc supported)
6456 bool net_rpc_check(struct net_context *c, unsigned flags)
6458 struct cli_state *cli;
6459 bool ret = false;
6460 struct sockaddr_storage server_ss;
6461 char *server_name = NULL;
6462 NTSTATUS status;
6464 /* flags (i.e. server type) may depend on command */
6465 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6466 return false;
6468 if ((cli = cli_initialise()) == NULL) {
6469 return false;
6472 status = cli_connect(cli, server_name, &server_ss);
6473 if (!NT_STATUS_IS_OK(status))
6474 goto done;
6475 if (!attempt_netbios_session_request(&cli, global_myname(),
6476 server_name, &server_ss))
6477 goto done;
6478 if (!cli_negprot(cli))
6479 goto done;
6480 if (cli->protocol < PROTOCOL_NT1)
6481 goto done;
6483 ret = true;
6484 done:
6485 cli_shutdown(cli);
6486 return ret;
6489 /* dump sam database via samsync rpc calls */
6490 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6491 if (c->display_usage) {
6492 d_printf("Usage:\n"
6493 "net rpc samdump\n"
6494 " Dump remote SAM database\n");
6495 return 0;
6498 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6499 NET_FLAGS_ANONYMOUS,
6500 rpc_samdump_internals, argc, argv);
6503 /* syncronise sam database via samsync rpc calls */
6504 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6506 struct functable func[] = {
6508 "ldif",
6509 rpc_vampire_ldif,
6510 NET_TRANSPORT_RPC,
6511 "Dump remote SAM database to ldif",
6512 "net rpc vampire ldif\n"
6513 " Dump remote SAM database to LDIF file or stdout"
6516 "keytab",
6517 rpc_vampire_keytab,
6518 NET_TRANSPORT_RPC,
6519 "Dump remote SAM database to Kerberos Keytab",
6520 "net rpc vampire keytab\n"
6521 " Dump remote SAM database to Kerberos keytab file"
6524 {NULL, NULL, 0, NULL, NULL}
6527 if (argc == 0) {
6528 if (c->display_usage) {
6529 d_printf("Usage:\n"
6530 "net rpc vampire\n"
6531 " Vampire remote SAM database\n");
6532 return 0;
6535 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6536 NET_FLAGS_ANONYMOUS,
6537 rpc_vampire_internals,
6538 argc, argv);
6541 return net_run_function(c, argc, argv, "net rpc vampire", func);
6545 * Migrate everything from a print server.
6547 * @param c A net_context structure.
6548 * @param argc Standard main() style argc.
6549 * @param argv Standard main() style argv. Initial components are already
6550 * stripped.
6552 * @return A shell status integer (0 for success).
6554 * The order is important !
6555 * To successfully add drivers the print queues have to exist !
6556 * Applying ACLs should be the last step, because you're easily locked out.
6559 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6560 const char **argv)
6562 int ret;
6564 if (c->display_usage) {
6565 d_printf("Usage:\n"
6566 "net rpc printer migrate all\n"
6567 " Migrate everything from a print server\n");
6568 return 0;
6571 if (!c->opt_host) {
6572 d_printf("no server to migrate\n");
6573 return -1;
6576 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6577 rpc_printer_migrate_printers_internals, argc,
6578 argv);
6579 if (ret)
6580 return ret;
6582 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6583 rpc_printer_migrate_drivers_internals, argc,
6584 argv);
6585 if (ret)
6586 return ret;
6588 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6589 rpc_printer_migrate_forms_internals, argc, argv);
6590 if (ret)
6591 return ret;
6593 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6594 rpc_printer_migrate_settings_internals, argc,
6595 argv);
6596 if (ret)
6597 return ret;
6599 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6600 rpc_printer_migrate_security_internals, argc,
6601 argv);
6606 * Migrate print drivers from a print server.
6608 * @param c A net_context structure.
6609 * @param argc Standard main() style argc.
6610 * @param argv Standard main() style argv. Initial components are already
6611 * stripped.
6613 * @return A shell status integer (0 for success).
6615 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6616 const char **argv)
6618 if (c->display_usage) {
6619 d_printf("Usage:\n"
6620 "net rpc printer migrate drivers\n"
6621 " Migrate print-drivers from a print-server\n");
6622 return 0;
6625 if (!c->opt_host) {
6626 d_printf("no server to migrate\n");
6627 return -1;
6630 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6631 rpc_printer_migrate_drivers_internals,
6632 argc, argv);
6636 * Migrate print-forms from a print-server.
6638 * @param c A net_context structure.
6639 * @param argc Standard main() style argc.
6640 * @param argv Standard main() style argv. Initial components are already
6641 * stripped.
6643 * @return A shell status integer (0 for success).
6645 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
6646 const char **argv)
6648 if (c->display_usage) {
6649 d_printf("Usage:\n"
6650 "net rpc printer migrate forms\n"
6651 " Migrate print-forms from a print-server\n");
6652 return 0;
6655 if (!c->opt_host) {
6656 d_printf("no server to migrate\n");
6657 return -1;
6660 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6661 rpc_printer_migrate_forms_internals,
6662 argc, argv);
6666 * Migrate printers from a print-server.
6668 * @param c A net_context structure.
6669 * @param argc Standard main() style argc.
6670 * @param argv Standard main() style argv. Initial components are already
6671 * stripped.
6673 * @return A shell status integer (0 for success).
6675 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
6676 const char **argv)
6678 if (c->display_usage) {
6679 d_printf("Usage:\n"
6680 "net rpc printer migrate printers\n"
6681 " Migrate printers from a print-server\n");
6682 return 0;
6685 if (!c->opt_host) {
6686 d_printf("no server to migrate\n");
6687 return -1;
6690 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6691 rpc_printer_migrate_printers_internals,
6692 argc, argv);
6696 * Migrate printer-ACLs from a print-server
6698 * @param c A net_context structure.
6699 * @param argc Standard main() style argc.
6700 * @param argv Standard main() style argv. Initial components are already
6701 * stripped.
6703 * @return A shell status integer (0 for success).
6705 static int rpc_printer_migrate_security(struct net_context *c, int argc,
6706 const char **argv)
6708 if (c->display_usage) {
6709 d_printf("Usage:\n"
6710 "net rpc printer migrate security\n"
6711 " Migrate printer-ACLs from a print-server\n");
6712 return 0;
6715 if (!c->opt_host) {
6716 d_printf("no server to migrate\n");
6717 return -1;
6720 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6721 rpc_printer_migrate_security_internals,
6722 argc, argv);
6726 * Migrate printer-settings from a print-server.
6728 * @param c A net_context structure.
6729 * @param argc Standard main() style argc.
6730 * @param argv Standard main() style argv. Initial components are already
6731 * stripped.
6733 * @return A shell status integer (0 for success).
6735 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
6736 const char **argv)
6738 if (c->display_usage) {
6739 d_printf("Usage:\n"
6740 "net rpc printer migrate settings\n"
6741 " Migrate printer-settings from a print-server\n");
6742 return 0;
6745 if (!c->opt_host) {
6746 d_printf("no server to migrate\n");
6747 return -1;
6750 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6751 rpc_printer_migrate_settings_internals,
6752 argc, argv);
6756 * 'net rpc printer' entrypoint.
6758 * @param c A net_context structure.
6759 * @param argc Standard main() style argc.
6760 * @param argv Standard main() style argv. Initial components are already
6761 * stripped.
6764 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
6767 /* ouch: when addriver and setdriver are called from within
6768 rpc_printer_migrate_drivers_internals, the printer-queue already
6769 *has* to exist */
6771 struct functable func[] = {
6773 "all",
6774 rpc_printer_migrate_all,
6775 NET_TRANSPORT_RPC,
6776 "Migrate all from remote to local print server",
6777 "net rpc printer migrate all\n"
6778 " Migrate all from remote to local print server"
6781 "drivers",
6782 rpc_printer_migrate_drivers,
6783 NET_TRANSPORT_RPC,
6784 "Migrate drivers to local server",
6785 "net rpc printer migrate drivers\n"
6786 " Migrate drivers to local server"
6789 "forms",
6790 rpc_printer_migrate_forms,
6791 NET_TRANSPORT_RPC,
6792 "Migrate froms to local server",
6793 "net rpc printer migrate forms\n"
6794 " Migrate froms to local server"
6797 "printers",
6798 rpc_printer_migrate_printers,
6799 NET_TRANSPORT_RPC,
6800 "Migrate printers to local server",
6801 "net rpc printer migrate printers\n"
6802 " Migrate printers to local server"
6805 "security",
6806 rpc_printer_migrate_security,
6807 NET_TRANSPORT_RPC,
6808 "Mirgate printer ACLs to local server",
6809 "net rpc printer migrate security\n"
6810 " Mirgate printer ACLs to local server"
6813 "settings",
6814 rpc_printer_migrate_settings,
6815 NET_TRANSPORT_RPC,
6816 "Migrate printer settings to local server",
6817 "net rpc printer migrate settings\n"
6818 " Migrate printer settings to local server"
6820 {NULL, NULL, 0, NULL, NULL}
6823 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
6828 * List printers on a remote RPC server.
6830 * @param c A net_context structure.
6831 * @param argc Standard main() style argc.
6832 * @param argv Standard main() style argv. Initial components are already
6833 * stripped.
6835 * @return A shell status integer (0 for success).
6837 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
6839 if (c->display_usage) {
6840 d_printf("Usage:\n"
6841 "net rpc printer list\n"
6842 " List printers on a remote RPC server\n");
6843 return 0;
6846 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6847 rpc_printer_list_internals,
6848 argc, argv);
6852 * List printer-drivers on a remote RPC server.
6854 * @param c A net_context structure.
6855 * @param argc Standard main() style argc.
6856 * @param argv Standard main() style argv. Initial components are already
6857 * stripped.
6859 * @return A shell status integer (0 for success).
6861 static int rpc_printer_driver_list(struct net_context *c, int argc,
6862 const char **argv)
6864 if (c->display_usage) {
6865 d_printf("Usage:\n"
6866 "net rpc printer driver\n"
6867 " List printer-drivers on a remote RPC server\n");
6868 return 0;
6871 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6872 rpc_printer_driver_list_internals,
6873 argc, argv);
6877 * Publish printer in ADS via MSRPC.
6879 * @param c A net_context structure.
6880 * @param argc Standard main() style argc.
6881 * @param argv Standard main() style argv. Initial components are already
6882 * stripped.
6884 * @return A shell status integer (0 for success).
6886 static int rpc_printer_publish_publish(struct net_context *c, int argc,
6887 const char **argv)
6889 if (c->display_usage) {
6890 d_printf("Usage:\n"
6891 "net rpc printer publish publish\n"
6892 " Publish printer in ADS via MSRPC\n");
6893 return 0;
6896 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6897 rpc_printer_publish_publish_internals,
6898 argc, argv);
6902 * Update printer in ADS via MSRPC.
6904 * @param c A net_context structure.
6905 * @param argc Standard main() style argc.
6906 * @param argv Standard main() style argv. Initial components are already
6907 * stripped.
6909 * @return A shell status integer (0 for success).
6911 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
6913 if (c->display_usage) {
6914 d_printf("Usage:\n"
6915 "net rpc printer publish update\n"
6916 " Update printer in ADS via MSRPC\n");
6917 return 0;
6920 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6921 rpc_printer_publish_update_internals,
6922 argc, argv);
6926 * UnPublish printer in ADS via MSRPC.
6928 * @param c A net_context structure.
6929 * @param argc Standard main() style argc.
6930 * @param argv Standard main() style argv. Initial components are already
6931 * stripped.
6933 * @return A shell status integer (0 for success).
6935 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
6936 const char **argv)
6938 if (c->display_usage) {
6939 d_printf("Usage:\n"
6940 "net rpc printer publish unpublish\n"
6941 " UnPublish printer in ADS via MSRPC\n");
6942 return 0;
6945 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6946 rpc_printer_publish_unpublish_internals,
6947 argc, argv);
6951 * List published printers via MSRPC.
6953 * @param c A net_context structure.
6954 * @param argc Standard main() style argc.
6955 * @param argv Standard main() style argv. Initial components are already
6956 * stripped.
6958 * @return A shell status integer (0 for success).
6960 static int rpc_printer_publish_list(struct net_context *c, int argc,
6961 const char **argv)
6963 if (c->display_usage) {
6964 d_printf("Usage:\n"
6965 "net rpc printer publish list\n"
6966 " List published printers via MSRPC\n");
6967 return 0;
6970 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6971 rpc_printer_publish_list_internals,
6972 argc, argv);
6977 * Publish printer in ADS.
6979 * @param c A net_context structure.
6980 * @param argc Standard main() style argc.
6981 * @param argv Standard main() style argv. Initial components are already
6982 * stripped.
6984 * @return A shell status integer (0 for success).
6986 static int rpc_printer_publish(struct net_context *c, int argc,
6987 const char **argv)
6990 struct functable func[] = {
6992 "publish",
6993 rpc_printer_publish_publish,
6994 NET_TRANSPORT_RPC,
6995 "Publish printer in AD",
6996 "net rpc printer publish publish\n"
6997 " Publish printer in AD"
7000 "update",
7001 rpc_printer_publish_update,
7002 NET_TRANSPORT_RPC,
7003 "Update printer in AD",
7004 "net rpc printer publish update\n"
7005 " Update printer in AD"
7008 "unpublish",
7009 rpc_printer_publish_unpublish,
7010 NET_TRANSPORT_RPC,
7011 "Unpublish printer",
7012 "net rpc printer publish unpublish\n"
7013 " Unpublish printer"
7016 "list",
7017 rpc_printer_publish_list,
7018 NET_TRANSPORT_RPC,
7019 "List published printers",
7020 "net rpc printer publish list\n"
7021 " List published printers"
7023 {NULL, NULL, 0, NULL, NULL}
7026 if (argc == 0) {
7027 if (c->display_usage) {
7028 d_printf("Usage:\n");
7029 d_printf("net rpc printer publish\n"
7030 " List published printers\n"
7031 " Alias of net rpc printer publish list\n");
7032 net_display_usage_from_functable(func);
7033 return 0;
7035 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7036 rpc_printer_publish_list_internals,
7037 argc, argv);
7040 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7046 * Display rpc printer help page.
7048 * @param c A net_context structure.
7049 * @param argc Standard main() style argc.
7050 * @param argv Standard main() style argv. Initial components are already
7051 * stripped.
7053 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7055 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"
7056 "\tlists all printers on print-server\n\n");
7057 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7058 "\tlists all printer-drivers on print-server\n\n");
7059 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7060 "\tpublishes printer settings in Active Directory\n"
7061 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
7062 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7063 "\n\tmigrates printers from remote to local server\n\n");
7064 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7065 "\n\tmigrates printer-settings from remote to local server\n\n");
7066 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7067 "\n\tmigrates printer-drivers from remote to local server\n\n");
7068 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7069 "\n\tmigrates printer-forms from remote to local server\n\n");
7070 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7071 "\n\tmigrates printer-ACLs from remote to local server\n\n");
7072 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7073 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7074 "\tremote to local print-server\n\n");
7075 net_common_methods_usage(c, argc, argv);
7076 net_common_flags_usage(c, argc, argv);
7077 d_printf(
7078 "\t-v or --verbose\t\t\tgive verbose output\n"
7079 "\t --destination\t\tmigration target server (default: localhost)\n");
7081 return -1;
7085 * 'net rpc printer' entrypoint.
7087 * @param c A net_context structure.
7088 * @param argc Standard main() style argc.
7089 * @param argv Standard main() style argv. Initial components are already
7090 * stripped.
7092 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7094 struct functable func[] = {
7096 "list",
7097 rpc_printer_list,
7098 NET_TRANSPORT_RPC,
7099 "List all printers on print server",
7100 "net rpc printer list\n"
7101 " List all printers on print server"
7104 "migrate",
7105 rpc_printer_migrate,
7106 NET_TRANSPORT_RPC,
7107 "Migrate printer to local server",
7108 "net rpc printer migrate\n"
7109 " Migrate printer to local server"
7112 "driver",
7113 rpc_printer_driver_list,
7114 NET_TRANSPORT_RPC,
7115 "List printer drivers",
7116 "net rpc printer driver\n"
7117 " List printer drivers"
7120 "publish",
7121 rpc_printer_publish,
7122 NET_TRANSPORT_RPC,
7123 "Publish printer in AD",
7124 "net rpc printer publish\n"
7125 " Publish printer in AD"
7127 {NULL, NULL, 0, NULL, NULL}
7130 if (argc == 0) {
7131 if (c->display_usage) {
7132 d_printf("Usage:\n");
7133 d_printf("net rpc printer\n"
7134 " List printers\n");
7135 net_display_usage_from_functable(func);
7136 return 0;
7138 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7139 rpc_printer_list_internals,
7140 argc, argv);
7143 return net_run_function(c, argc, argv, "net rpc printer", func);
7147 * 'net rpc' entrypoint.
7149 * @param c A net_context structure.
7150 * @param argc Standard main() style argc.
7151 * @param argv Standard main() style argv. Initial components are already
7152 * stripped.
7155 int net_rpc(struct net_context *c, int argc, const char **argv)
7157 struct functable func[] = {
7159 "audit",
7160 net_rpc_audit,
7161 NET_TRANSPORT_RPC,
7162 "Modify global audit settings",
7163 "net rpc audit\n"
7164 " Modify global audit settings"
7167 "info",
7168 net_rpc_info,
7169 NET_TRANSPORT_RPC,
7170 "Show basic info about a domain",
7171 "net rpc info\n"
7172 " Show basic info about a domain"
7175 "join",
7176 net_rpc_join,
7177 NET_TRANSPORT_RPC,
7178 "Join a domain",
7179 "net rpc join\n"
7180 " Join a domain"
7183 "oldjoin",
7184 net_rpc_oldjoin,
7185 NET_TRANSPORT_RPC,
7186 "Join a domain created in server manager",
7187 "net rpc oldjoin\n"
7188 " Join a domain created in server manager"
7191 "testjoin",
7192 net_rpc_testjoin,
7193 NET_TRANSPORT_RPC,
7194 "Test that a join is valid",
7195 "net rpc testjoin\n"
7196 " Test that a join is valid"
7199 "user",
7200 net_rpc_user,
7201 NET_TRANSPORT_RPC,
7202 "List/modify users",
7203 "net rpc user\n"
7204 " List/modify users"
7207 "password",
7208 rpc_user_password,
7209 NET_TRANSPORT_RPC,
7210 "Change a user password",
7211 "net rpc password\n"
7212 " Change a user password\n"
7213 " Alias for net rpc user password"
7216 "group",
7217 net_rpc_group,
7218 NET_TRANSPORT_RPC,
7219 "List/modify groups",
7220 "net rpc group\n"
7221 " List/modify groups"
7224 "share",
7225 net_rpc_share,
7226 NET_TRANSPORT_RPC,
7227 "List/modify shares",
7228 "net rpc share\n"
7229 " List/modify shares"
7232 "file",
7233 net_rpc_file,
7234 NET_TRANSPORT_RPC,
7235 "List open files",
7236 "net rpc file\n"
7237 " List open files"
7240 "printer",
7241 net_rpc_printer,
7242 NET_TRANSPORT_RPC,
7243 "List/modify printers",
7244 "net rpc printer\n"
7245 " List/modify printers"
7248 "changetrustpw",
7249 net_rpc_changetrustpw,
7250 NET_TRANSPORT_RPC,
7251 "Change trust account password",
7252 "net rpc changetrustpw\n"
7253 " Change trust account password"
7256 "trustdom",
7257 rpc_trustdom,
7258 NET_TRANSPORT_RPC,
7259 "Modify domain trusts",
7260 "net rpc trustdom\n"
7261 " Modify domain trusts"
7264 "abortshutdown",
7265 rpc_shutdown_abort,
7266 NET_TRANSPORT_RPC,
7267 "Abort a remote shutdown",
7268 "net rpc abortshutdown\n"
7269 " Abort a remote shutdown"
7272 "shutdown",
7273 rpc_shutdown,
7274 NET_TRANSPORT_RPC,
7275 "Shutdown a remote server",
7276 "net rpc shutdown\n"
7277 " Shutdown a remote server"
7280 "samdump",
7281 rpc_samdump,
7282 NET_TRANSPORT_RPC,
7283 "Dump SAM data of remote NT PDC",
7284 "net rpc samdump\n"
7285 " Dump SAM data of remote NT PDC"
7288 "vampire",
7289 rpc_vampire,
7290 NET_TRANSPORT_RPC,
7291 "Sync a remote NT PDC's data into local passdb",
7292 "net rpc vampire\n"
7293 " Sync a remote NT PDC's data into local passdb"
7296 "getsid",
7297 net_rpc_getsid,
7298 NET_TRANSPORT_RPC,
7299 "Fetch the domain sid into local secrets.tdb",
7300 "net rpc getsid\n"
7301 " Fetch the domain sid into local secrets.tdb"
7304 "rights",
7305 net_rpc_rights,
7306 NET_TRANSPORT_RPC,
7307 "Manage privileges assigned to SID",
7308 "net rpc rights\n"
7309 " Manage privileges assigned to SID"
7312 "service",
7313 net_rpc_service,
7314 NET_TRANSPORT_RPC,
7315 "Start/stop/query remote services",
7316 "net rpc service\n"
7317 " Start/stop/query remote services"
7320 "registry",
7321 net_rpc_registry,
7322 NET_TRANSPORT_RPC,
7323 "Manage registry hives",
7324 "net rpc registry\n"
7325 " Manage registry hives"
7328 "shell",
7329 net_rpc_shell,
7330 NET_TRANSPORT_RPC,
7331 "Open interactive shell on remote server",
7332 "net rpc shell\n"
7333 " Open interactive shell on remote server"
7335 {NULL, NULL, 0, NULL, NULL}
7337 return net_run_function(c, argc, argv, "net rpc", func);