make read/write to internal pipes available externally
[Samba.git] / source / utils / net_rpc.c
blobf6b6d3a095a84856f279e57ed16dc7aa3e077c43
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
5 Copyright (C) 2002 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2004,2008 Guenther Deschner (gd@samba.org)
7 Copyright (C) 2005 Jeremy Allison (jra@samba.org)
8 Copyright (C) 2006 Jelmer Vernooij (jelmer@samba.org)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "includes.h"
24 #include "utils/net.h"
26 static int net_mode_share;
27 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask);
29 /**
30 * @file net_rpc.c
32 * @brief RPC based subcommands for the 'net' utility.
34 * This file should contain much of the functionality that used to
35 * be found in rpcclient, execpt that the commands should change
36 * less often, and the fucntionality should be sane (the user is not
37 * expected to know a rid/sid before they conduct an operation etc.)
39 * @todo Perhaps eventually these should be split out into a number
40 * of files, as this could get quite big.
41 **/
44 /**
45 * Many of the RPC functions need the domain sid. This function gets
46 * it at the start of every run
48 * @param cli A cli_state already connected to the remote machine
50 * @return The Domain SID of the remote machine.
51 **/
53 NTSTATUS net_get_remote_domain_sid(struct cli_state *cli, TALLOC_CTX *mem_ctx,
54 DOM_SID **domain_sid,
55 const char **domain_name)
57 struct rpc_pipe_client *lsa_pipe;
58 POLICY_HND pol;
59 NTSTATUS result = NT_STATUS_OK;
60 union lsa_PolicyInformation *info = NULL;
62 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
63 &lsa_pipe);
64 if (!NT_STATUS_IS_OK(result)) {
65 d_fprintf(stderr, "Could not initialise lsa pipe\n");
66 return result;
69 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, false,
70 SEC_RIGHTS_MAXIMUM_ALLOWED,
71 &pol);
72 if (!NT_STATUS_IS_OK(result)) {
73 d_fprintf(stderr, "open_policy failed: %s\n",
74 nt_errstr(result));
75 return result;
78 result = rpccli_lsa_QueryInfoPolicy(lsa_pipe, mem_ctx,
79 &pol,
80 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
81 &info);
82 if (!NT_STATUS_IS_OK(result)) {
83 d_fprintf(stderr, "lsaquery failed: %s\n",
84 nt_errstr(result));
85 return result;
88 *domain_name = info->account_domain.name.string;
89 *domain_sid = info->account_domain.sid;
91 rpccli_lsa_Close(lsa_pipe, mem_ctx, &pol);
92 TALLOC_FREE(lsa_pipe);
94 return NT_STATUS_OK;
97 /**
98 * Run a single RPC command, from start to finish.
100 * @param pipe_name the pipe to connect to (usually a PIPE_ constant)
101 * @param conn_flag a NET_FLAG_ combination. Passed to
102 * net_make_ipc_connection.
103 * @param argc Standard main() style argc.
104 * @param argv Standard main() style argv. Initial components are already
105 * stripped.
106 * @return A shell status integer (0 for success).
109 int run_rpc_command(struct net_context *c,
110 struct cli_state *cli_arg,
111 const struct ndr_syntax_id *interface,
112 int conn_flags,
113 rpc_command_fn fn,
114 int argc,
115 const char **argv)
117 struct cli_state *cli = NULL;
118 struct rpc_pipe_client *pipe_hnd = NULL;
119 TALLOC_CTX *mem_ctx;
120 NTSTATUS nt_status;
121 DOM_SID *domain_sid;
122 const char *domain_name;
124 /* make use of cli_state handed over as an argument, if possible */
125 if (!cli_arg) {
126 nt_status = net_make_ipc_connection(c, conn_flags, &cli);
127 if (!NT_STATUS_IS_OK(nt_status)) {
128 DEBUG(1, ("failed to make ipc connection: %s\n",
129 nt_errstr(nt_status)));
130 return -1;
132 } else {
133 cli = cli_arg;
136 if (!cli) {
137 return -1;
140 /* Create mem_ctx */
142 if (!(mem_ctx = talloc_init("run_rpc_command"))) {
143 DEBUG(0, ("talloc_init() failed\n"));
144 cli_shutdown(cli);
145 return -1;
148 nt_status = net_get_remote_domain_sid(cli, mem_ctx, &domain_sid,
149 &domain_name);
150 if (!NT_STATUS_IS_OK(nt_status)) {
151 cli_shutdown(cli);
152 return -1;
155 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
156 if (lp_client_schannel()
157 && (ndr_syntax_id_equal(interface,
158 &ndr_table_netlogon.syntax_id))) {
159 /* Always try and create an schannel netlogon pipe. */
160 nt_status = cli_rpc_pipe_open_schannel(
161 cli, interface,
162 PIPE_AUTH_LEVEL_PRIVACY, domain_name,
163 &pipe_hnd);
164 if (!NT_STATUS_IS_OK(nt_status)) {
165 DEBUG(0, ("Could not initialise schannel netlogon pipe. Error was %s\n",
166 nt_errstr(nt_status) ));
167 cli_shutdown(cli);
168 return -1;
170 } else {
171 if (conn_flags & NET_FLAGS_SEAL) {
172 nt_status = cli_rpc_pipe_open_ntlmssp(
173 cli, interface,
174 PIPE_AUTH_LEVEL_PRIVACY,
175 lp_workgroup(), c->opt_user_name,
176 c->opt_password, &pipe_hnd);
177 } else {
178 nt_status = cli_rpc_pipe_open_noauth(
179 cli, interface,
180 &pipe_hnd);
182 if (!NT_STATUS_IS_OK(nt_status)) {
183 DEBUG(0, ("Could not initialise pipe %s. Error was %s\n",
184 cli_get_pipe_name_from_iface(
185 debug_ctx(), cli, interface),
186 nt_errstr(nt_status) ));
187 cli_shutdown(cli);
188 return -1;
193 nt_status = fn(c, domain_sid, domain_name, cli, pipe_hnd, mem_ctx, argc, argv);
195 if (!NT_STATUS_IS_OK(nt_status)) {
196 DEBUG(1, ("rpc command function failed! (%s)\n", nt_errstr(nt_status)));
197 } else {
198 DEBUG(5, ("rpc command function succedded\n"));
201 if (!(conn_flags & NET_FLAGS_NO_PIPE)) {
202 if (pipe_hnd) {
203 TALLOC_FREE(pipe_hnd);
207 /* close the connection only if it was opened here */
208 if (!cli_arg) {
209 cli_shutdown(cli);
212 talloc_destroy(mem_ctx);
213 return (!NT_STATUS_IS_OK(nt_status));
217 * Force a change of the trust acccount password.
219 * All parameters are provided by the run_rpc_command function, except for
220 * argc, argv which are passed through.
222 * @param domain_sid The domain sid acquired from the remote server.
223 * @param cli A cli_state connected to the server.
224 * @param mem_ctx Talloc context, destroyed on completion of the function.
225 * @param argc Standard main() style argc.
226 * @param argv Standard main() style argv. Initial components are already
227 * stripped.
229 * @return Normal NTSTATUS return.
232 static NTSTATUS rpc_changetrustpw_internals(struct net_context *c,
233 const DOM_SID *domain_sid,
234 const char *domain_name,
235 struct cli_state *cli,
236 struct rpc_pipe_client *pipe_hnd,
237 TALLOC_CTX *mem_ctx,
238 int argc,
239 const char **argv)
242 return trust_pw_find_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup);
246 * Force a change of the trust acccount password.
248 * @param argc Standard main() style argc.
249 * @param argv Standard main() style argv. Initial components are already
250 * stripped.
252 * @return A shell status integer (0 for success).
255 int net_rpc_changetrustpw(struct net_context *c, int argc, const char **argv)
257 if (c->display_usage) {
258 d_printf("Usage:\n"
259 "net rpc changetrustpw\n"
260 " Change the machine trust password\n");
261 return 0;
264 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
265 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
266 rpc_changetrustpw_internals,
267 argc, argv);
271 * Join a domain, the old way.
273 * This uses 'machinename' as the inital password, and changes it.
275 * The password should be created with 'server manager' or equiv first.
277 * All parameters are provided by the run_rpc_command function, except for
278 * argc, argv which are passed through.
280 * @param domain_sid The domain sid acquired from the remote server.
281 * @param cli A cli_state connected to the server.
282 * @param mem_ctx Talloc context, destroyed on completion of the function.
283 * @param argc Standard main() style argc.
284 * @param argv Standard main() style argv. Initial components are already
285 * stripped.
287 * @return Normal NTSTATUS return.
290 static NTSTATUS rpc_oldjoin_internals(struct net_context *c,
291 const DOM_SID *domain_sid,
292 const char *domain_name,
293 struct cli_state *cli,
294 struct rpc_pipe_client *pipe_hnd,
295 TALLOC_CTX *mem_ctx,
296 int argc,
297 const char **argv)
300 fstring trust_passwd;
301 unsigned char orig_trust_passwd_hash[16];
302 NTSTATUS result;
303 uint32 sec_channel_type;
305 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
306 &pipe_hnd);
307 if (!NT_STATUS_IS_OK(result)) {
308 DEBUG(0,("rpc_oldjoin_internals: netlogon pipe open to machine %s failed. "
309 "error was %s\n",
310 cli->desthost,
311 nt_errstr(result) ));
312 return result;
316 check what type of join - if the user want's to join as
317 a BDC, the server must agree that we are a BDC.
319 if (argc >= 0) {
320 sec_channel_type = get_sec_channel_type(argv[0]);
321 } else {
322 sec_channel_type = get_sec_channel_type(NULL);
325 fstrcpy(trust_passwd, global_myname());
326 strlower_m(trust_passwd);
329 * Machine names can be 15 characters, but the max length on
330 * a password is 14. --jerry
333 trust_passwd[14] = '\0';
335 E_md4hash(trust_passwd, orig_trust_passwd_hash);
337 result = trust_pw_change_and_store_it(pipe_hnd, mem_ctx, c->opt_target_workgroup,
338 orig_trust_passwd_hash,
339 sec_channel_type);
341 if (NT_STATUS_IS_OK(result))
342 printf("Joined domain %s.\n", c->opt_target_workgroup);
345 if (!secrets_store_domain_sid(c->opt_target_workgroup, domain_sid)) {
346 DEBUG(0, ("error storing domain sid for %s\n", c->opt_target_workgroup));
347 result = NT_STATUS_UNSUCCESSFUL;
350 return result;
354 * Join a domain, the old way.
356 * @param argc Standard main() style argc.
357 * @param argv Standard main() style argv. Initial components are already
358 * stripped.
360 * @return A shell status integer (0 for success).
363 static int net_rpc_perform_oldjoin(struct net_context *c, int argc, const char **argv)
365 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
366 NET_FLAGS_NO_PIPE | NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
367 rpc_oldjoin_internals,
368 argc, argv);
372 * Join a domain, the old way. This function exists to allow
373 * the message to be displayed when oldjoin was explicitly
374 * requested, but not when it was implied by "net rpc join".
376 * @param argc Standard main() style argc.
377 * @param argv Standard main() style argv. Initial components are already
378 * stripped.
380 * @return A shell status integer (0 for success).
383 static int net_rpc_oldjoin(struct net_context *c, int argc, const char **argv)
385 int rc = -1;
387 if (c->display_usage) {
388 d_printf("Usage:\n"
389 "net rpc oldjoin\n"
390 " Join a domain the old way\n");
391 return 0;
394 rc = net_rpc_perform_oldjoin(c, argc, argv);
396 if (rc) {
397 d_fprintf(stderr, "Failed to join domain\n");
400 return rc;
404 * 'net rpc join' entrypoint.
405 * @param argc Standard main() style argc.
406 * @param argv Standard main() style argv. Initial components are already
407 * stripped
409 * Main 'net_rpc_join()' (where the admin username/password is used) is
410 * in net_rpc_join.c.
411 * Try to just change the password, but if that doesn't work, use/prompt
412 * for a username/password.
415 int net_rpc_join(struct net_context *c, int argc, const char **argv)
417 if (c->display_usage) {
418 d_printf("Usage:\n"
419 "net rpc join -U <username>[%%password] <type>\n"
420 " Join a domain\n"
421 " username\tName of the admin user"
422 " password\tPassword of the admin user, will "
423 "prompt if not specified\n"
424 " type\tCan be one of the following:\n"
425 "\t\tMEMBER\tJoin as member server (default)\n"
426 "\t\tBDC\tJoin as BDC\n"
427 "\t\tPDC\tJoin as PDC\n");
428 return 0;
431 if (lp_server_role() == ROLE_STANDALONE) {
432 d_printf("cannot join as standalone machine\n");
433 return -1;
436 if (strlen(global_myname()) > 15) {
437 d_printf("Our netbios name can be at most 15 chars long, "
438 "\"%s\" is %u chars long\n",
439 global_myname(), (unsigned int)strlen(global_myname()));
440 return -1;
443 if ((net_rpc_perform_oldjoin(c, argc, argv) == 0))
444 return 0;
446 return net_rpc_join_newstyle(c, argc, argv);
450 * display info about a rpc domain
452 * All parameters are provided by the run_rpc_command function, except for
453 * argc, argv which are passed through.
455 * @param domain_sid The domain sid acquired from the remote server
456 * @param cli A cli_state connected to the server.
457 * @param mem_ctx Talloc context, destroyed on completion of the function.
458 * @param argc Standard main() style argc.
459 * @param argv Standard main() style argv. Initial components are already
460 * stripped.
462 * @return Normal NTSTATUS return.
465 NTSTATUS rpc_info_internals(struct net_context *c,
466 const DOM_SID *domain_sid,
467 const char *domain_name,
468 struct cli_state *cli,
469 struct rpc_pipe_client *pipe_hnd,
470 TALLOC_CTX *mem_ctx,
471 int argc,
472 const char **argv)
474 POLICY_HND connect_pol, domain_pol;
475 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
476 union samr_DomainInfo *info = NULL;
477 fstring sid_str;
479 sid_to_fstring(sid_str, domain_sid);
481 /* Get sam policy handle */
482 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
483 pipe_hnd->desthost,
484 MAXIMUM_ALLOWED_ACCESS,
485 &connect_pol);
486 if (!NT_STATUS_IS_OK(result)) {
487 d_fprintf(stderr, "Could not connect to SAM: %s\n", nt_errstr(result));
488 goto done;
491 /* Get domain policy handle */
492 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
493 &connect_pol,
494 MAXIMUM_ALLOWED_ACCESS,
495 CONST_DISCARD(struct dom_sid2 *, domain_sid),
496 &domain_pol);
497 if (!NT_STATUS_IS_OK(result)) {
498 d_fprintf(stderr, "Could not open domain: %s\n", nt_errstr(result));
499 goto done;
502 result = rpccli_samr_QueryDomainInfo(pipe_hnd, mem_ctx,
503 &domain_pol,
505 &info);
506 if (NT_STATUS_IS_OK(result)) {
507 d_printf("Domain Name: %s\n", info->info2.domain_name.string);
508 d_printf("Domain SID: %s\n", sid_str);
509 d_printf("Sequence number: %llu\n",
510 (unsigned long long)info->info2.sequence_num);
511 d_printf("Num users: %u\n", info->info2.num_users);
512 d_printf("Num domain groups: %u\n", info->info2.num_groups);
513 d_printf("Num local groups: %u\n", info->info2.num_aliases);
516 done:
517 return result;
521 * 'net rpc info' entrypoint.
522 * @param argc Standard main() style argc.
523 * @param argv Standard main() style argv. Initial components are already
524 * stripped.
527 int net_rpc_info(struct net_context *c, int argc, const char **argv)
529 if (c->display_usage) {
530 d_printf("Usage:\n"
531 "net rpc info\n"
532 " Display information about the domain\n");
533 return 0;
536 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
537 NET_FLAGS_PDC, rpc_info_internals,
538 argc, argv);
542 * Fetch domain SID into the local secrets.tdb.
544 * All parameters are provided by the run_rpc_command function, except for
545 * argc, argv which are passed through.
547 * @param domain_sid The domain sid acquired from the remote server.
548 * @param cli A cli_state connected to the server.
549 * @param mem_ctx Talloc context, destroyed on completion of the function.
550 * @param argc Standard main() style argc.
551 * @param argv Standard main() style argv. Initial components are already
552 * stripped.
554 * @return Normal NTSTATUS return.
557 static NTSTATUS rpc_getsid_internals(struct net_context *c,
558 const DOM_SID *domain_sid,
559 const char *domain_name,
560 struct cli_state *cli,
561 struct rpc_pipe_client *pipe_hnd,
562 TALLOC_CTX *mem_ctx,
563 int argc,
564 const char **argv)
566 fstring sid_str;
568 sid_to_fstring(sid_str, domain_sid);
569 d_printf("Storing SID %s for Domain %s in secrets.tdb\n",
570 sid_str, domain_name);
572 if (!secrets_store_domain_sid(domain_name, domain_sid)) {
573 DEBUG(0,("Can't store domain SID\n"));
574 return NT_STATUS_UNSUCCESSFUL;
577 return NT_STATUS_OK;
581 * 'net rpc getsid' entrypoint.
582 * @param argc Standard main() style argc.
583 * @param argv Standard main() style argv. Initial components are already
584 * stripped.
587 int net_rpc_getsid(struct net_context *c, int argc, const char **argv)
589 if (c->display_usage) {
590 d_printf("Usage:\n"
591 "net rpc getsid\n"
592 " Fetch domain SID into local secrets.tdb\n");
593 return 0;
596 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id,
597 NET_FLAGS_ANONYMOUS | NET_FLAGS_PDC,
598 rpc_getsid_internals,
599 argc, argv);
602 /****************************************************************************/
605 * Basic usage function for 'net rpc user'.
606 * @param argc Standard main() style argc.
607 * @param argv Standard main() style argv. Initial components are already
608 * stripped.
611 static int rpc_user_usage(struct net_context *c, int argc, const char **argv)
613 return net_user_usage(c, argc, argv);
617 * Add a new user to a remote RPC server.
619 * @param argc Standard main() style argc.
620 * @param argv Standard main() style argv. Initial components are already
621 * stripped.
623 * @return A shell status integer (0 for success).
626 static int rpc_user_add(struct net_context *c, int argc, const char **argv)
628 NET_API_STATUS status;
629 struct USER_INFO_1 info1;
630 uint32_t parm_error = 0;
632 if (argc < 1 || c->display_usage) {
633 rpc_user_usage(c, argc, argv);
634 return 0;
637 ZERO_STRUCT(info1);
639 info1.usri1_name = argv[0];
640 if (argc == 2) {
641 info1.usri1_password = argv[1];
644 status = NetUserAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
646 if (status != 0) {
647 d_fprintf(stderr, "Failed to add user '%s' with: %s.\n",
648 argv[0], libnetapi_get_error_string(c->netapi_ctx,
649 status));
650 return -1;
651 } else {
652 d_printf("Added user '%s'.\n", argv[0]);
655 return 0;
659 * Rename a user on a remote RPC server.
661 * All parameters are provided by the run_rpc_command function, except for
662 * argc, argv which are passed through.
664 * @param domain_sid The domain sid acquired from the remote server.
665 * @param cli A cli_state connected to the server.
666 * @param mem_ctx Talloc context, destroyed on completion of the function.
667 * @param argc Standard main() style argc.
668 * @param argv Standard main() style argv. Initial components are already
669 * stripped.
671 * @return Normal NTSTATUS return.
674 static NTSTATUS rpc_user_rename_internals(struct net_context *c,
675 const DOM_SID *domain_sid,
676 const char *domain_name,
677 struct cli_state *cli,
678 struct rpc_pipe_client *pipe_hnd,
679 TALLOC_CTX *mem_ctx,
680 int argc,
681 const char **argv)
683 POLICY_HND connect_pol, domain_pol, user_pol;
684 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
685 uint32 info_level = 7;
686 const char *old_name, *new_name;
687 struct samr_Ids user_rids, name_types;
688 struct lsa_String lsa_acct_name;
689 union samr_UserInfo *info = NULL;
691 if (argc != 2 || c->display_usage) {
692 rpc_user_usage(c, argc, argv);
693 return NT_STATUS_OK;
696 old_name = argv[0];
697 new_name = argv[1];
699 /* Get sam policy handle */
701 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
702 pipe_hnd->desthost,
703 MAXIMUM_ALLOWED_ACCESS,
704 &connect_pol);
706 if (!NT_STATUS_IS_OK(result)) {
707 goto done;
710 /* Get domain policy handle */
712 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
713 &connect_pol,
714 MAXIMUM_ALLOWED_ACCESS,
715 CONST_DISCARD(struct dom_sid2 *, domain_sid),
716 &domain_pol);
717 if (!NT_STATUS_IS_OK(result)) {
718 goto done;
721 init_lsa_String(&lsa_acct_name, old_name);
723 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
724 &domain_pol,
726 &lsa_acct_name,
727 &user_rids,
728 &name_types);
729 if (!NT_STATUS_IS_OK(result)) {
730 goto done;
733 /* Open domain user */
734 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
735 &domain_pol,
736 MAXIMUM_ALLOWED_ACCESS,
737 user_rids.ids[0],
738 &user_pol);
740 if (!NT_STATUS_IS_OK(result)) {
741 goto done;
744 /* Query user info */
745 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
746 &user_pol,
747 info_level,
748 &info);
750 if (!NT_STATUS_IS_OK(result)) {
751 goto done;
754 init_samr_user_info7(&info->info7, new_name);
756 /* Set new name */
757 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
758 &user_pol,
759 info_level,
760 info);
762 if (!NT_STATUS_IS_OK(result)) {
763 goto done;
766 done:
767 if (!NT_STATUS_IS_OK(result)) {
768 d_fprintf(stderr, "Failed to rename user from %s to %s - %s\n", old_name, new_name,
769 nt_errstr(result));
770 } else {
771 d_printf("Renamed user from %s to %s\n", old_name, new_name);
773 return result;
777 * Rename a user on a remote RPC server.
779 * @param argc Standard main() style argc.
780 * @param argv Standard main() style argv. Initial components are already
781 * stripped.
783 * @return A shell status integer (0 for success).
786 static int rpc_user_rename(struct net_context *c, int argc, const char **argv)
788 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
789 rpc_user_rename_internals, argc, argv);
793 * Delete a user from a remote RPC server.
795 * @param argc Standard main() style argc.
796 * @param argv Standard main() style argv. Initial components are already
797 * stripped.
799 * @return A shell status integer (0 for success).
802 static int rpc_user_delete(struct net_context *c, int argc, const char **argv)
804 NET_API_STATUS status;
806 if (argc < 1 || c->display_usage) {
807 rpc_user_usage(c, argc, argv);
808 return 0;
811 status = NetUserDel(c->opt_host, argv[0]);
813 if (status != 0) {
814 d_fprintf(stderr, "Failed to delete user '%s' with: %s.\n",
815 argv[0],
816 libnetapi_get_error_string(c->netapi_ctx, status));
817 return -1;
818 } else {
819 d_printf("Deleted user '%s'.\n", argv[0]);
822 return 0;
826 * Set a password for a user on a remote RPC server.
828 * All parameters are provided by the run_rpc_command function, except for
829 * argc, argv which are passed through.
831 * @param domain_sid The domain sid acquired from the remote server.
832 * @param cli A cli_state connected to the server.
833 * @param mem_ctx Talloc context, destroyed on completion of the function.
834 * @param argc Standard main() style argc.
835 * @param argv Standard main() style argv. Initial components are already
836 * stripped.
838 * @return Normal NTSTATUS return.
841 static NTSTATUS rpc_user_password_internals(struct net_context *c,
842 const DOM_SID *domain_sid,
843 const char *domain_name,
844 struct cli_state *cli,
845 struct rpc_pipe_client *pipe_hnd,
846 TALLOC_CTX *mem_ctx,
847 int argc,
848 const char **argv)
850 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
851 POLICY_HND connect_pol, domain_pol, user_pol;
852 uchar pwbuf[516];
853 const char *user;
854 const char *new_password;
855 char *prompt = NULL;
856 union samr_UserInfo info;
858 if (argc < 1 || c->display_usage) {
859 rpc_user_usage(c, argc, argv);
860 return NT_STATUS_OK;
863 user = argv[0];
865 if (argv[1]) {
866 new_password = argv[1];
867 } else {
868 asprintf(&prompt, "Enter new password for %s:", user);
869 new_password = getpass(prompt);
870 SAFE_FREE(prompt);
873 /* Get sam policy and domain handles */
875 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
876 pipe_hnd->desthost,
877 MAXIMUM_ALLOWED_ACCESS,
878 &connect_pol);
880 if (!NT_STATUS_IS_OK(result)) {
881 goto done;
884 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
885 &connect_pol,
886 MAXIMUM_ALLOWED_ACCESS,
887 CONST_DISCARD(struct dom_sid2 *, domain_sid),
888 &domain_pol);
890 if (!NT_STATUS_IS_OK(result)) {
891 goto done;
894 /* Get handle on user */
897 struct samr_Ids user_rids, name_types;
898 struct lsa_String lsa_acct_name;
900 init_lsa_String(&lsa_acct_name, user);
902 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
903 &domain_pol,
905 &lsa_acct_name,
906 &user_rids,
907 &name_types);
908 if (!NT_STATUS_IS_OK(result)) {
909 goto done;
912 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
913 &domain_pol,
914 MAXIMUM_ALLOWED_ACCESS,
915 user_rids.ids[0],
916 &user_pol);
918 if (!NT_STATUS_IS_OK(result)) {
919 goto done;
923 /* Set password on account */
925 encode_pw_buffer(pwbuf, new_password, STR_UNICODE);
927 init_samr_user_info24(&info.info24, pwbuf, 24);
929 SamOEMhashBlob(info.info24.password.data, 516,
930 &cli->user_session_key);
932 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
933 &user_pol,
935 &info);
937 if (!NT_STATUS_IS_OK(result)) {
938 goto done;
941 /* Display results */
943 done:
944 return result;
949 * Set a user's password on a remote RPC server.
951 * @param argc Standard main() style argc.
952 * @param argv Standard main() style argv. Initial components are already
953 * stripped.
955 * @return A shell status integer (0 for success).
958 static int rpc_user_password(struct net_context *c, int argc, const char **argv)
960 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
961 rpc_user_password_internals, argc, argv);
965 * List user's groups on a remote RPC server.
967 * All parameters are provided by the run_rpc_command function, except for
968 * argc, argv which are passed through.
970 * @param domain_sid The domain sid acquired from the remote server.
971 * @param cli A cli_state connected to the server.
972 * @param mem_ctx Talloc context, destroyed on completion of the function.
973 * @param argc Standard main() style argc.
974 * @param argv Standard main() style argv. Initial components are already
975 * stripped.
977 * @return Normal NTSTATUS return.
980 static NTSTATUS rpc_user_info_internals(struct net_context *c,
981 const DOM_SID *domain_sid,
982 const char *domain_name,
983 struct cli_state *cli,
984 struct rpc_pipe_client *pipe_hnd,
985 TALLOC_CTX *mem_ctx,
986 int argc,
987 const char **argv)
989 POLICY_HND connect_pol, domain_pol, user_pol;
990 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
991 int i;
992 struct samr_RidWithAttributeArray *rid_array = NULL;
993 struct lsa_Strings names;
994 struct samr_Ids types;
995 uint32_t *lrids = NULL;
996 struct samr_Ids rids, name_types;
997 struct lsa_String lsa_acct_name;
1000 if (argc < 1 || c->display_usage) {
1001 rpc_user_usage(c, argc, argv);
1002 return NT_STATUS_OK;
1004 /* Get sam policy handle */
1006 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1007 pipe_hnd->desthost,
1008 MAXIMUM_ALLOWED_ACCESS,
1009 &connect_pol);
1010 if (!NT_STATUS_IS_OK(result)) goto done;
1012 /* Get domain policy handle */
1014 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1015 &connect_pol,
1016 MAXIMUM_ALLOWED_ACCESS,
1017 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1018 &domain_pol);
1019 if (!NT_STATUS_IS_OK(result)) goto done;
1021 /* Get handle on user */
1023 init_lsa_String(&lsa_acct_name, argv[0]);
1025 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1026 &domain_pol,
1028 &lsa_acct_name,
1029 &rids,
1030 &name_types);
1032 if (!NT_STATUS_IS_OK(result)) goto done;
1034 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1035 &domain_pol,
1036 MAXIMUM_ALLOWED_ACCESS,
1037 rids.ids[0],
1038 &user_pol);
1039 if (!NT_STATUS_IS_OK(result)) goto done;
1041 result = rpccli_samr_GetGroupsForUser(pipe_hnd, mem_ctx,
1042 &user_pol,
1043 &rid_array);
1045 if (!NT_STATUS_IS_OK(result)) goto done;
1047 /* Look up rids */
1049 if (rid_array->count) {
1050 if ((lrids = TALLOC_ARRAY(mem_ctx, uint32, rid_array->count)) == NULL) {
1051 result = NT_STATUS_NO_MEMORY;
1052 goto done;
1055 for (i = 0; i < rid_array->count; i++)
1056 lrids[i] = rid_array->rids[i].rid;
1058 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
1059 &domain_pol,
1060 rid_array->count,
1061 lrids,
1062 &names,
1063 &types);
1065 if (!NT_STATUS_IS_OK(result)) {
1066 goto done;
1069 /* Display results */
1071 for (i = 0; i < names.count; i++)
1072 printf("%s\n", names.names[i].string);
1074 done:
1075 return result;
1079 * List a user's groups from a remote RPC server.
1081 * @param argc Standard main() style argc.
1082 * @param argv Standard main() style argv. Initial components are already
1083 * stripped.
1085 * @return A shell status integer (0 for success)
1088 static int rpc_user_info(struct net_context *c, int argc, const char **argv)
1090 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1091 rpc_user_info_internals, argc, argv);
1095 * List users on a remote RPC server.
1097 * All parameters are provided by the run_rpc_command function, except for
1098 * argc, argv which are passed through.
1100 * @param domain_sid The domain sid acquired from the remote server.
1101 * @param cli A cli_state connected to the server.
1102 * @param mem_ctx Talloc context, destroyed on completion of the function.
1103 * @param argc Standard main() style argc.
1104 * @param argv Standard main() style argv. Initial components are already
1105 * stripped.
1107 * @return Normal NTSTATUS return.
1110 static NTSTATUS rpc_user_list_internals(struct net_context *c,
1111 const DOM_SID *domain_sid,
1112 const char *domain_name,
1113 struct cli_state *cli,
1114 struct rpc_pipe_client *pipe_hnd,
1115 TALLOC_CTX *mem_ctx,
1116 int argc,
1117 const char **argv)
1119 POLICY_HND connect_pol, domain_pol;
1120 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1121 uint32 start_idx=0, num_entries, i, loop_count = 0;
1123 /* Get sam policy handle */
1125 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1126 pipe_hnd->desthost,
1127 MAXIMUM_ALLOWED_ACCESS,
1128 &connect_pol);
1129 if (!NT_STATUS_IS_OK(result)) {
1130 goto done;
1133 /* Get domain policy handle */
1135 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1136 &connect_pol,
1137 MAXIMUM_ALLOWED_ACCESS,
1138 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1139 &domain_pol);
1140 if (!NT_STATUS_IS_OK(result)) {
1141 goto done;
1144 /* Query domain users */
1145 if (c->opt_long_list_entries)
1146 d_printf("\nUser name Comment"
1147 "\n-----------------------------\n");
1148 do {
1149 const char *user = NULL;
1150 const char *desc = NULL;
1151 uint32 max_entries, max_size;
1152 uint32_t total_size, returned_size;
1153 union samr_DispInfo info;
1155 get_query_dispinfo_params(
1156 loop_count, &max_entries, &max_size);
1158 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
1159 &domain_pol,
1161 start_idx,
1162 max_entries,
1163 max_size,
1164 &total_size,
1165 &returned_size,
1166 &info);
1167 loop_count++;
1168 start_idx += info.info1.count;
1169 num_entries = info.info1.count;
1171 for (i = 0; i < num_entries; i++) {
1172 user = info.info1.entries[i].account_name.string;
1173 if (c->opt_long_list_entries)
1174 desc = info.info1.entries[i].description.string;
1175 if (c->opt_long_list_entries)
1176 printf("%-21.21s %s\n", user, desc);
1177 else
1178 printf("%s\n", user);
1180 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
1182 done:
1183 return result;
1187 * 'net rpc user' entrypoint.
1188 * @param argc Standard main() style argc.
1189 * @param argv Standard main() style argv. Initial components are already
1190 * stripped.
1193 int net_rpc_user(struct net_context *c, int argc, const char **argv)
1195 NET_API_STATUS status;
1197 struct functable func[] = {
1199 "add",
1200 rpc_user_add,
1201 NET_TRANSPORT_RPC,
1202 "Add specified user",
1203 "net rpc user add\n"
1204 " Add specified user"
1207 "info",
1208 rpc_user_info,
1209 NET_TRANSPORT_RPC,
1210 "List domain groups of user",
1211 "net rpc user info\n"
1212 " Lis domain groups of user"
1215 "delete",
1216 rpc_user_delete,
1217 NET_TRANSPORT_RPC,
1218 "Remove specified user",
1219 "net rpc user delete\n"
1220 " Remove specified user"
1223 "password",
1224 rpc_user_password,
1225 NET_TRANSPORT_RPC,
1226 "Change user password",
1227 "net rpc user password\n"
1228 " Change user password"
1231 "rename",
1232 rpc_user_rename,
1233 NET_TRANSPORT_RPC,
1234 "Rename specified user",
1235 "net rpc user rename\n"
1236 " Rename specified user"
1238 {NULL, NULL, 0, NULL, NULL}
1241 status = libnetapi_init(&c->netapi_ctx);
1242 if (status != 0) {
1243 return -1;
1245 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
1246 libnetapi_set_password(c->netapi_ctx, c->opt_password);
1248 if (argc == 0) {
1249 if (c->display_usage) {
1250 d_printf("Usage:\n");
1251 d_printf("net rpc user\n"
1252 " List all users\n");
1253 net_display_usage_from_functable(func);
1254 return 0;
1257 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1258 rpc_user_list_internals,
1259 argc, argv);
1262 return net_run_function(c, argc, argv, "net rpc user", func);
1265 static NTSTATUS rpc_sh_user_list(struct net_context *c,
1266 TALLOC_CTX *mem_ctx,
1267 struct rpc_sh_ctx *ctx,
1268 struct rpc_pipe_client *pipe_hnd,
1269 int argc, const char **argv)
1271 return rpc_user_list_internals(c, ctx->domain_sid, ctx->domain_name,
1272 ctx->cli, pipe_hnd, mem_ctx,
1273 argc, argv);
1276 static NTSTATUS rpc_sh_user_info(struct net_context *c,
1277 TALLOC_CTX *mem_ctx,
1278 struct rpc_sh_ctx *ctx,
1279 struct rpc_pipe_client *pipe_hnd,
1280 int argc, const char **argv)
1282 return rpc_user_info_internals(c, ctx->domain_sid, ctx->domain_name,
1283 ctx->cli, pipe_hnd, mem_ctx,
1284 argc, argv);
1287 static NTSTATUS rpc_sh_handle_user(struct net_context *c,
1288 TALLOC_CTX *mem_ctx,
1289 struct rpc_sh_ctx *ctx,
1290 struct rpc_pipe_client *pipe_hnd,
1291 int argc, const char **argv,
1292 NTSTATUS (*fn)(
1293 struct net_context *c,
1294 TALLOC_CTX *mem_ctx,
1295 struct rpc_sh_ctx *ctx,
1296 struct rpc_pipe_client *pipe_hnd,
1297 POLICY_HND *user_hnd,
1298 int argc, const char **argv))
1300 POLICY_HND connect_pol, domain_pol, user_pol;
1301 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1302 DOM_SID sid;
1303 uint32 rid;
1304 enum lsa_SidType type;
1306 if (argc == 0) {
1307 d_fprintf(stderr, "usage: %s <username>\n", ctx->whoami);
1308 return NT_STATUS_INVALID_PARAMETER;
1311 ZERO_STRUCT(connect_pol);
1312 ZERO_STRUCT(domain_pol);
1313 ZERO_STRUCT(user_pol);
1315 result = net_rpc_lookup_name(c, mem_ctx, rpc_pipe_np_smb_conn(pipe_hnd),
1316 argv[0], NULL, NULL, &sid, &type);
1317 if (!NT_STATUS_IS_OK(result)) {
1318 d_fprintf(stderr, "Could not lookup %s: %s\n", argv[0],
1319 nt_errstr(result));
1320 goto done;
1323 if (type != SID_NAME_USER) {
1324 d_fprintf(stderr, "%s is a %s, not a user\n", argv[0],
1325 sid_type_lookup(type));
1326 result = NT_STATUS_NO_SUCH_USER;
1327 goto done;
1330 if (!sid_peek_check_rid(ctx->domain_sid, &sid, &rid)) {
1331 d_fprintf(stderr, "%s is not in our domain\n", argv[0]);
1332 result = NT_STATUS_NO_SUCH_USER;
1333 goto done;
1336 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1337 pipe_hnd->desthost,
1338 MAXIMUM_ALLOWED_ACCESS,
1339 &connect_pol);
1340 if (!NT_STATUS_IS_OK(result)) {
1341 goto done;
1344 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1345 &connect_pol,
1346 MAXIMUM_ALLOWED_ACCESS,
1347 ctx->domain_sid,
1348 &domain_pol);
1349 if (!NT_STATUS_IS_OK(result)) {
1350 goto done;
1353 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1354 &domain_pol,
1355 MAXIMUM_ALLOWED_ACCESS,
1356 rid,
1357 &user_pol);
1358 if (!NT_STATUS_IS_OK(result)) {
1359 goto done;
1362 result = fn(c, mem_ctx, ctx, pipe_hnd, &user_pol, argc-1, argv+1);
1364 done:
1365 if (is_valid_policy_hnd(&user_pol)) {
1366 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1368 if (is_valid_policy_hnd(&domain_pol)) {
1369 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
1371 if (is_valid_policy_hnd(&connect_pol)) {
1372 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
1374 return result;
1377 static NTSTATUS rpc_sh_user_show_internals(struct net_context *c,
1378 TALLOC_CTX *mem_ctx,
1379 struct rpc_sh_ctx *ctx,
1380 struct rpc_pipe_client *pipe_hnd,
1381 POLICY_HND *user_hnd,
1382 int argc, const char **argv)
1384 NTSTATUS result;
1385 union samr_UserInfo *info = NULL;
1387 if (argc != 0) {
1388 d_fprintf(stderr, "usage: %s show <username>\n", ctx->whoami);
1389 return NT_STATUS_INVALID_PARAMETER;
1392 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1393 user_hnd,
1395 &info);
1396 if (!NT_STATUS_IS_OK(result)) {
1397 return result;
1400 d_printf("user rid: %d, group rid: %d\n",
1401 info->info21.rid,
1402 info->info21.primary_gid);
1404 return result;
1407 static NTSTATUS rpc_sh_user_show(struct net_context *c,
1408 TALLOC_CTX *mem_ctx,
1409 struct rpc_sh_ctx *ctx,
1410 struct rpc_pipe_client *pipe_hnd,
1411 int argc, const char **argv)
1413 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1414 rpc_sh_user_show_internals);
1417 #define FETCHSTR(name, rec) \
1418 do { if (strequal(ctx->thiscmd, name)) { \
1419 oldval = talloc_strdup(mem_ctx, info->info21.rec.string); } \
1420 } while (0);
1422 #define SETSTR(name, rec, flag) \
1423 do { if (strequal(ctx->thiscmd, name)) { \
1424 init_lsa_String(&(info->info21.rec), argv[0]); \
1425 info->info21.fields_present |= SAMR_FIELD_##flag; } \
1426 } while (0);
1428 static NTSTATUS rpc_sh_user_str_edit_internals(struct net_context *c,
1429 TALLOC_CTX *mem_ctx,
1430 struct rpc_sh_ctx *ctx,
1431 struct rpc_pipe_client *pipe_hnd,
1432 POLICY_HND *user_hnd,
1433 int argc, const char **argv)
1435 NTSTATUS result;
1436 const char *username;
1437 const char *oldval = "";
1438 union samr_UserInfo *info = NULL;
1440 if (argc > 1) {
1441 d_fprintf(stderr, "usage: %s <username> [new value|NULL]\n",
1442 ctx->whoami);
1443 return NT_STATUS_INVALID_PARAMETER;
1446 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1447 user_hnd,
1449 &info);
1450 if (!NT_STATUS_IS_OK(result)) {
1451 return result;
1454 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1456 FETCHSTR("fullname", full_name);
1457 FETCHSTR("homedir", home_directory);
1458 FETCHSTR("homedrive", home_drive);
1459 FETCHSTR("logonscript", logon_script);
1460 FETCHSTR("profilepath", profile_path);
1461 FETCHSTR("description", description);
1463 if (argc == 0) {
1464 d_printf("%s's %s: [%s]\n", username, ctx->thiscmd, oldval);
1465 goto done;
1468 if (strcmp(argv[0], "NULL") == 0) {
1469 argv[0] = "";
1472 ZERO_STRUCT(info->info21);
1474 SETSTR("fullname", full_name, FULL_NAME);
1475 SETSTR("homedir", home_directory, HOME_DIRECTORY);
1476 SETSTR("homedrive", home_drive, HOME_DRIVE);
1477 SETSTR("logonscript", logon_script, LOGON_SCRIPT);
1478 SETSTR("profilepath", profile_path, PROFILE_PATH);
1479 SETSTR("description", description, DESCRIPTION);
1481 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1482 user_hnd,
1484 info);
1486 d_printf("Set %s's %s from [%s] to [%s]\n", username,
1487 ctx->thiscmd, oldval, argv[0]);
1489 done:
1491 return result;
1494 #define HANDLEFLG(name, rec) \
1495 do { if (strequal(ctx->thiscmd, name)) { \
1496 oldval = (oldflags & ACB_##rec) ? "yes" : "no"; \
1497 if (newval) { \
1498 newflags = oldflags | ACB_##rec; \
1499 } else { \
1500 newflags = oldflags & ~ACB_##rec; \
1501 } } } while (0);
1503 static NTSTATUS rpc_sh_user_str_edit(struct net_context *c,
1504 TALLOC_CTX *mem_ctx,
1505 struct rpc_sh_ctx *ctx,
1506 struct rpc_pipe_client *pipe_hnd,
1507 int argc, const char **argv)
1509 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1510 rpc_sh_user_str_edit_internals);
1513 static NTSTATUS rpc_sh_user_flag_edit_internals(struct net_context *c,
1514 TALLOC_CTX *mem_ctx,
1515 struct rpc_sh_ctx *ctx,
1516 struct rpc_pipe_client *pipe_hnd,
1517 POLICY_HND *user_hnd,
1518 int argc, const char **argv)
1520 NTSTATUS result;
1521 const char *username;
1522 const char *oldval = "unknown";
1523 uint32 oldflags, newflags;
1524 bool newval;
1525 union samr_UserInfo *info = NULL;
1527 if ((argc > 1) ||
1528 ((argc == 1) && !strequal(argv[0], "yes") &&
1529 !strequal(argv[0], "no"))) {
1530 d_fprintf(stderr, "usage: %s <username> [yes|no]\n",
1531 ctx->whoami);
1532 return NT_STATUS_INVALID_PARAMETER;
1535 newval = strequal(argv[0], "yes");
1537 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1538 user_hnd,
1540 &info);
1541 if (!NT_STATUS_IS_OK(result)) {
1542 return result;
1545 username = talloc_strdup(mem_ctx, info->info21.account_name.string);
1546 oldflags = info->info21.acct_flags;
1547 newflags = info->info21.acct_flags;
1549 HANDLEFLG("disabled", DISABLED);
1550 HANDLEFLG("pwnotreq", PWNOTREQ);
1551 HANDLEFLG("autolock", AUTOLOCK);
1552 HANDLEFLG("pwnoexp", PWNOEXP);
1554 if (argc == 0) {
1555 d_printf("%s's %s flag: %s\n", username, ctx->thiscmd, oldval);
1556 goto done;
1559 ZERO_STRUCT(info->info21);
1561 info->info21.acct_flags = newflags;
1562 info->info21.fields_present = SAMR_FIELD_ACCT_FLAGS;
1564 result = rpccli_samr_SetUserInfo(pipe_hnd, mem_ctx,
1565 user_hnd,
1567 info);
1569 if (NT_STATUS_IS_OK(result)) {
1570 d_printf("Set %s's %s flag from [%s] to [%s]\n", username,
1571 ctx->thiscmd, oldval, argv[0]);
1574 done:
1576 return result;
1579 static NTSTATUS rpc_sh_user_flag_edit(struct net_context *c,
1580 TALLOC_CTX *mem_ctx,
1581 struct rpc_sh_ctx *ctx,
1582 struct rpc_pipe_client *pipe_hnd,
1583 int argc, const char **argv)
1585 return rpc_sh_handle_user(c, mem_ctx, ctx, pipe_hnd, argc, argv,
1586 rpc_sh_user_flag_edit_internals);
1589 struct rpc_sh_cmd *net_rpc_user_edit_cmds(struct net_context *c,
1590 TALLOC_CTX *mem_ctx,
1591 struct rpc_sh_ctx *ctx)
1593 static struct rpc_sh_cmd cmds[] = {
1595 { "fullname", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1596 "Show/Set a user's full name" },
1598 { "homedir", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1599 "Show/Set a user's home directory" },
1601 { "homedrive", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1602 "Show/Set a user's home drive" },
1604 { "logonscript", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1605 "Show/Set a user's logon script" },
1607 { "profilepath", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1608 "Show/Set a user's profile path" },
1610 { "description", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_str_edit,
1611 "Show/Set a user's description" },
1613 { "disabled", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1614 "Show/Set whether a user is disabled" },
1616 { "autolock", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1617 "Show/Set whether a user locked out" },
1619 { "pwnotreq", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1620 "Show/Set whether a user does not need a password" },
1622 { "pwnoexp", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_flag_edit,
1623 "Show/Set whether a user's password does not expire" },
1625 { NULL, NULL, 0, NULL, NULL }
1628 return cmds;
1631 struct rpc_sh_cmd *net_rpc_user_cmds(struct net_context *c,
1632 TALLOC_CTX *mem_ctx,
1633 struct rpc_sh_ctx *ctx)
1635 static struct rpc_sh_cmd cmds[] = {
1637 { "list", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_list,
1638 "List available users" },
1640 { "info", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_info,
1641 "List the domain groups a user is member of" },
1643 { "show", NULL, &ndr_table_samr.syntax_id, rpc_sh_user_show,
1644 "Show info about a user" },
1646 { "edit", net_rpc_user_edit_cmds, 0, NULL,
1647 "Show/Modify a user's fields" },
1649 { NULL, NULL, 0, NULL, NULL }
1652 return cmds;
1655 /****************************************************************************/
1658 * Basic usage function for 'net rpc group'.
1659 * @param argc Standard main() style argc.
1660 * @param argv Standard main() style argv. Initial components are already
1661 * stripped.
1664 static int rpc_group_usage(struct net_context *c, int argc, const char **argv)
1666 return net_group_usage(c, argc, argv);
1670 * Delete group on a remote RPC server.
1672 * All parameters are provided by the run_rpc_command function, except for
1673 * argc, argv which are passed through.
1675 * @param domain_sid The domain sid acquired from the remote server.
1676 * @param cli A cli_state connected to the server.
1677 * @param mem_ctx Talloc context, destroyed on completion of the function.
1678 * @param argc Standard main() style argc.
1679 * @param argv Standard main() style argv. Initial components are already
1680 * stripped.
1682 * @return Normal NTSTATUS return.
1685 static NTSTATUS rpc_group_delete_internals(struct net_context *c,
1686 const DOM_SID *domain_sid,
1687 const char *domain_name,
1688 struct cli_state *cli,
1689 struct rpc_pipe_client *pipe_hnd,
1690 TALLOC_CTX *mem_ctx,
1691 int argc,
1692 const char **argv)
1694 POLICY_HND connect_pol, domain_pol, group_pol, user_pol;
1695 bool group_is_primary = false;
1696 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1697 uint32_t group_rid;
1698 struct samr_RidTypeArray *rids = NULL;
1699 /* char **names; */
1700 int i;
1701 /* DOM_GID *user_gids; */
1703 struct samr_Ids group_rids, name_types;
1704 struct lsa_String lsa_acct_name;
1705 union samr_UserInfo *info = NULL;
1707 if (argc < 1 || c->display_usage) {
1708 rpc_group_usage(c, argc,argv);
1709 return NT_STATUS_OK; /* ok? */
1712 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1713 pipe_hnd->desthost,
1714 MAXIMUM_ALLOWED_ACCESS,
1715 &connect_pol);
1717 if (!NT_STATUS_IS_OK(result)) {
1718 d_fprintf(stderr, "Request samr_Connect2 failed\n");
1719 goto done;
1722 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1723 &connect_pol,
1724 MAXIMUM_ALLOWED_ACCESS,
1725 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1726 &domain_pol);
1728 if (!NT_STATUS_IS_OK(result)) {
1729 d_fprintf(stderr, "Request open_domain failed\n");
1730 goto done;
1733 init_lsa_String(&lsa_acct_name, argv[0]);
1735 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
1736 &domain_pol,
1738 &lsa_acct_name,
1739 &group_rids,
1740 &name_types);
1741 if (!NT_STATUS_IS_OK(result)) {
1742 d_fprintf(stderr, "Lookup of '%s' failed\n",argv[0]);
1743 goto done;
1746 switch (name_types.ids[0])
1748 case SID_NAME_DOM_GRP:
1749 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
1750 &domain_pol,
1751 MAXIMUM_ALLOWED_ACCESS,
1752 group_rids.ids[0],
1753 &group_pol);
1754 if (!NT_STATUS_IS_OK(result)) {
1755 d_fprintf(stderr, "Request open_group failed");
1756 goto done;
1759 group_rid = group_rids.ids[0];
1761 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
1762 &group_pol,
1763 &rids);
1765 if (!NT_STATUS_IS_OK(result)) {
1766 d_fprintf(stderr, "Unable to query group members of %s",argv[0]);
1767 goto done;
1770 if (c->opt_verbose) {
1771 d_printf("Domain Group %s (rid: %d) has %d members\n",
1772 argv[0],group_rid, rids->count);
1775 /* Check if group is anyone's primary group */
1776 for (i = 0; i < rids->count; i++)
1778 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
1779 &domain_pol,
1780 MAXIMUM_ALLOWED_ACCESS,
1781 rids->rids[i],
1782 &user_pol);
1784 if (!NT_STATUS_IS_OK(result)) {
1785 d_fprintf(stderr, "Unable to open group member %d\n",
1786 rids->rids[i]);
1787 goto done;
1790 result = rpccli_samr_QueryUserInfo(pipe_hnd, mem_ctx,
1791 &user_pol,
1793 &info);
1795 if (!NT_STATUS_IS_OK(result)) {
1796 d_fprintf(stderr, "Unable to lookup userinfo for group member %d\n",
1797 rids->rids[i]);
1798 goto done;
1801 if (info->info21.primary_gid == group_rid) {
1802 if (c->opt_verbose) {
1803 d_printf("Group is primary group of %s\n",
1804 info->info21.account_name.string);
1806 group_is_primary = true;
1809 rpccli_samr_Close(pipe_hnd, mem_ctx, &user_pol);
1812 if (group_is_primary) {
1813 d_fprintf(stderr, "Unable to delete group because some "
1814 "of it's members have it as primary group\n");
1815 result = NT_STATUS_MEMBERS_PRIMARY_GROUP;
1816 goto done;
1819 /* remove all group members */
1820 for (i = 0; i < rids->count; i++)
1822 if (c->opt_verbose)
1823 d_printf("Remove group member %d...",
1824 rids->rids[i]);
1825 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
1826 &group_pol,
1827 rids->rids[i]);
1829 if (NT_STATUS_IS_OK(result)) {
1830 if (c->opt_verbose)
1831 d_printf("ok\n");
1832 } else {
1833 if (c->opt_verbose)
1834 d_printf("failed\n");
1835 goto done;
1839 result = rpccli_samr_DeleteDomainGroup(pipe_hnd, mem_ctx,
1840 &group_pol);
1842 break;
1843 /* removing a local group is easier... */
1844 case SID_NAME_ALIAS:
1845 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
1846 &domain_pol,
1847 MAXIMUM_ALLOWED_ACCESS,
1848 group_rids.ids[0],
1849 &group_pol);
1851 if (!NT_STATUS_IS_OK(result)) {
1852 d_fprintf(stderr, "Request open_alias failed\n");
1853 goto done;
1856 result = rpccli_samr_DeleteDomAlias(pipe_hnd, mem_ctx,
1857 &group_pol);
1858 break;
1859 default:
1860 d_fprintf(stderr, "%s is of type %s. This command is only for deleting local or global groups\n",
1861 argv[0],sid_type_lookup(name_types.ids[0]));
1862 result = NT_STATUS_UNSUCCESSFUL;
1863 goto done;
1866 if (NT_STATUS_IS_OK(result)) {
1867 if (c->opt_verbose)
1868 d_printf("Deleted %s '%s'\n",sid_type_lookup(name_types.ids[0]),argv[0]);
1869 } else {
1870 d_fprintf(stderr, "Deleting of %s failed: %s\n",argv[0],
1871 get_friendly_nt_error_msg(result));
1874 done:
1875 return result;
1879 static int rpc_group_delete(struct net_context *c, int argc, const char **argv)
1881 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1882 rpc_group_delete_internals, argc,argv);
1885 static int rpc_group_add_internals(struct net_context *c, int argc, const char **argv)
1887 NET_API_STATUS status;
1888 struct GROUP_INFO_1 info1;
1889 uint32_t parm_error = 0;
1891 if (argc != 1 || c->display_usage) {
1892 rpc_group_usage(c, argc, argv);
1893 return 0;
1896 ZERO_STRUCT(info1);
1898 info1.grpi1_name = argv[0];
1899 if (c->opt_comment && strlen(c->opt_comment) > 0) {
1900 info1.grpi1_comment = c->opt_comment;
1903 status = NetGroupAdd(c->opt_host, 1, (uint8_t *)&info1, &parm_error);
1905 if (status != 0) {
1906 d_fprintf(stderr, "Failed to add group '%s' with: %s.\n",
1907 argv[0], libnetapi_get_error_string(c->netapi_ctx,
1908 status));
1909 return -1;
1910 } else {
1911 d_printf("Added group '%s'.\n", argv[0]);
1914 return 0;
1917 static NTSTATUS rpc_alias_add_internals(struct net_context *c,
1918 const DOM_SID *domain_sid,
1919 const char *domain_name,
1920 struct cli_state *cli,
1921 struct rpc_pipe_client *pipe_hnd,
1922 TALLOC_CTX *mem_ctx,
1923 int argc,
1924 const char **argv)
1926 POLICY_HND connect_pol, domain_pol, alias_pol;
1927 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
1928 union samr_AliasInfo alias_info;
1929 struct lsa_String alias_name;
1930 uint32_t rid = 0;
1932 if (argc != 1 || c->display_usage) {
1933 rpc_group_usage(c, argc, argv);
1934 return NT_STATUS_OK;
1937 init_lsa_String(&alias_name, argv[0]);
1939 /* Get sam policy handle */
1941 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
1942 pipe_hnd->desthost,
1943 MAXIMUM_ALLOWED_ACCESS,
1944 &connect_pol);
1945 if (!NT_STATUS_IS_OK(result)) goto done;
1947 /* Get domain policy handle */
1949 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
1950 &connect_pol,
1951 MAXIMUM_ALLOWED_ACCESS,
1952 CONST_DISCARD(struct dom_sid2 *, domain_sid),
1953 &domain_pol);
1954 if (!NT_STATUS_IS_OK(result)) goto done;
1956 /* Create the group */
1958 result = rpccli_samr_CreateDomAlias(pipe_hnd, mem_ctx,
1959 &domain_pol,
1960 &alias_name,
1961 MAXIMUM_ALLOWED_ACCESS,
1962 &alias_pol,
1963 &rid);
1964 if (!NT_STATUS_IS_OK(result)) goto done;
1966 if (strlen(c->opt_comment) == 0) goto done;
1968 /* We've got a comment to set */
1970 init_lsa_String(&alias_info.description, c->opt_comment);
1972 result = rpccli_samr_SetAliasInfo(pipe_hnd, mem_ctx,
1973 &alias_pol,
1975 &alias_info);
1977 if (!NT_STATUS_IS_OK(result)) goto done;
1979 done:
1980 if (NT_STATUS_IS_OK(result))
1981 DEBUG(5, ("add alias succeeded\n"));
1982 else
1983 d_fprintf(stderr, "add alias failed: %s\n", nt_errstr(result));
1985 return result;
1988 static int rpc_group_add(struct net_context *c, int argc, const char **argv)
1990 if (c->opt_localgroup)
1991 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
1992 rpc_alias_add_internals,
1993 argc, argv);
1995 return rpc_group_add_internals(c, argc, argv);
1998 static NTSTATUS get_sid_from_name(struct cli_state *cli,
1999 TALLOC_CTX *mem_ctx,
2000 const char *name,
2001 DOM_SID *sid,
2002 enum lsa_SidType *type)
2004 DOM_SID *sids = NULL;
2005 enum lsa_SidType *types = NULL;
2006 struct rpc_pipe_client *pipe_hnd;
2007 POLICY_HND lsa_pol;
2008 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2010 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
2011 &pipe_hnd);
2012 if (!NT_STATUS_IS_OK(result)) {
2013 goto done;
2016 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, false,
2017 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2019 if (!NT_STATUS_IS_OK(result)) {
2020 goto done;
2023 result = rpccli_lsa_lookup_names(pipe_hnd, mem_ctx, &lsa_pol, 1,
2024 &name, NULL, 1, &sids, &types);
2026 if (NT_STATUS_IS_OK(result)) {
2027 sid_copy(sid, &sids[0]);
2028 *type = types[0];
2031 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
2033 done:
2034 if (pipe_hnd) {
2035 TALLOC_FREE(pipe_hnd);
2038 if (!NT_STATUS_IS_OK(result) && (StrnCaseCmp(name, "S-", 2) == 0)) {
2040 /* Try as S-1-5-whatever */
2042 DOM_SID tmp_sid;
2044 if (string_to_sid(&tmp_sid, name)) {
2045 sid_copy(sid, &tmp_sid);
2046 *type = SID_NAME_UNKNOWN;
2047 result = NT_STATUS_OK;
2051 return result;
2054 static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
2055 TALLOC_CTX *mem_ctx,
2056 const DOM_SID *group_sid,
2057 const char *member)
2059 POLICY_HND connect_pol, domain_pol;
2060 NTSTATUS result;
2061 uint32 group_rid;
2062 POLICY_HND group_pol;
2064 struct samr_Ids rids, rid_types;
2065 struct lsa_String lsa_acct_name;
2067 DOM_SID sid;
2069 sid_copy(&sid, group_sid);
2071 if (!sid_split_rid(&sid, &group_rid)) {
2072 return NT_STATUS_UNSUCCESSFUL;
2075 /* Get sam policy handle */
2076 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2077 pipe_hnd->desthost,
2078 MAXIMUM_ALLOWED_ACCESS,
2079 &connect_pol);
2080 if (!NT_STATUS_IS_OK(result)) {
2081 return result;
2084 /* Get domain policy handle */
2085 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2086 &connect_pol,
2087 MAXIMUM_ALLOWED_ACCESS,
2088 &sid,
2089 &domain_pol);
2090 if (!NT_STATUS_IS_OK(result)) {
2091 return result;
2094 init_lsa_String(&lsa_acct_name, member);
2096 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2097 &domain_pol,
2099 &lsa_acct_name,
2100 &rids,
2101 &rid_types);
2103 if (!NT_STATUS_IS_OK(result)) {
2104 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2105 goto done;
2108 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2109 &domain_pol,
2110 MAXIMUM_ALLOWED_ACCESS,
2111 group_rid,
2112 &group_pol);
2114 if (!NT_STATUS_IS_OK(result)) {
2115 goto done;
2118 result = rpccli_samr_AddGroupMember(pipe_hnd, mem_ctx,
2119 &group_pol,
2120 rids.ids[0],
2121 0x0005); /* unknown flags */
2123 done:
2124 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2125 return result;
2128 static NTSTATUS rpc_add_aliasmem(struct rpc_pipe_client *pipe_hnd,
2129 TALLOC_CTX *mem_ctx,
2130 const DOM_SID *alias_sid,
2131 const char *member)
2133 POLICY_HND connect_pol, domain_pol;
2134 NTSTATUS result;
2135 uint32 alias_rid;
2136 POLICY_HND alias_pol;
2138 DOM_SID member_sid;
2139 enum lsa_SidType member_type;
2141 DOM_SID sid;
2143 sid_copy(&sid, alias_sid);
2145 if (!sid_split_rid(&sid, &alias_rid)) {
2146 return NT_STATUS_UNSUCCESSFUL;
2149 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2150 member, &member_sid, &member_type);
2152 if (!NT_STATUS_IS_OK(result)) {
2153 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2154 return result;
2157 /* Get sam policy handle */
2158 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2159 pipe_hnd->desthost,
2160 MAXIMUM_ALLOWED_ACCESS,
2161 &connect_pol);
2162 if (!NT_STATUS_IS_OK(result)) {
2163 goto done;
2166 /* Get domain policy handle */
2167 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2168 &connect_pol,
2169 MAXIMUM_ALLOWED_ACCESS,
2170 &sid,
2171 &domain_pol);
2172 if (!NT_STATUS_IS_OK(result)) {
2173 goto done;
2176 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2177 &domain_pol,
2178 MAXIMUM_ALLOWED_ACCESS,
2179 alias_rid,
2180 &alias_pol);
2182 if (!NT_STATUS_IS_OK(result)) {
2183 return result;
2186 result = rpccli_samr_AddAliasMember(pipe_hnd, mem_ctx,
2187 &alias_pol,
2188 &member_sid);
2190 if (!NT_STATUS_IS_OK(result)) {
2191 return result;
2194 done:
2195 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2196 return result;
2199 static NTSTATUS rpc_group_addmem_internals(struct net_context *c,
2200 const DOM_SID *domain_sid,
2201 const char *domain_name,
2202 struct cli_state *cli,
2203 struct rpc_pipe_client *pipe_hnd,
2204 TALLOC_CTX *mem_ctx,
2205 int argc,
2206 const char **argv)
2208 DOM_SID group_sid;
2209 enum lsa_SidType group_type;
2211 if (argc != 2 || c->display_usage) {
2212 d_printf("Usage:\n"
2213 "net rpc group addmem <group> <member>\n"
2214 " Add a member to a group\n"
2215 " group\tGroup to add member to\n"
2216 " member\tMember to add to group\n");
2217 return NT_STATUS_UNSUCCESSFUL;
2220 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2221 &group_sid, &group_type))) {
2222 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2223 return NT_STATUS_UNSUCCESSFUL;
2226 if (group_type == SID_NAME_DOM_GRP) {
2227 NTSTATUS result = rpc_add_groupmem(pipe_hnd, mem_ctx,
2228 &group_sid, argv[1]);
2230 if (!NT_STATUS_IS_OK(result)) {
2231 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2232 argv[1], argv[0], nt_errstr(result));
2234 return result;
2237 if (group_type == SID_NAME_ALIAS) {
2238 NTSTATUS result = rpc_add_aliasmem(pipe_hnd, mem_ctx,
2239 &group_sid, argv[1]);
2241 if (!NT_STATUS_IS_OK(result)) {
2242 d_fprintf(stderr, "Could not add %s to %s: %s\n",
2243 argv[1], argv[0], nt_errstr(result));
2245 return result;
2248 d_fprintf(stderr, "Can only add members to global or local groups "
2249 "which %s is not\n", argv[0]);
2251 return NT_STATUS_UNSUCCESSFUL;
2254 static int rpc_group_addmem(struct net_context *c, int argc, const char **argv)
2256 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2257 rpc_group_addmem_internals,
2258 argc, argv);
2261 static NTSTATUS rpc_del_groupmem(struct net_context *c,
2262 struct rpc_pipe_client *pipe_hnd,
2263 TALLOC_CTX *mem_ctx,
2264 const DOM_SID *group_sid,
2265 const char *member)
2267 POLICY_HND connect_pol, domain_pol;
2268 NTSTATUS result;
2269 uint32 group_rid;
2270 POLICY_HND group_pol;
2272 struct samr_Ids rids, rid_types;
2273 struct lsa_String lsa_acct_name;
2275 DOM_SID sid;
2277 sid_copy(&sid, group_sid);
2279 if (!sid_split_rid(&sid, &group_rid))
2280 return NT_STATUS_UNSUCCESSFUL;
2282 /* Get sam policy handle */
2283 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2284 pipe_hnd->desthost,
2285 MAXIMUM_ALLOWED_ACCESS,
2286 &connect_pol);
2287 if (!NT_STATUS_IS_OK(result))
2288 return result;
2290 /* Get domain policy handle */
2291 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2292 &connect_pol,
2293 MAXIMUM_ALLOWED_ACCESS,
2294 &sid,
2295 &domain_pol);
2296 if (!NT_STATUS_IS_OK(result))
2297 return result;
2299 init_lsa_String(&lsa_acct_name, member);
2301 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2302 &domain_pol,
2304 &lsa_acct_name,
2305 &rids,
2306 &rid_types);
2307 if (!NT_STATUS_IS_OK(result)) {
2308 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2309 goto done;
2312 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2313 &domain_pol,
2314 MAXIMUM_ALLOWED_ACCESS,
2315 group_rid,
2316 &group_pol);
2318 if (!NT_STATUS_IS_OK(result))
2319 goto done;
2321 result = rpccli_samr_DeleteGroupMember(pipe_hnd, mem_ctx,
2322 &group_pol,
2323 rids.ids[0]);
2325 done:
2326 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2327 return result;
2330 static NTSTATUS rpc_del_aliasmem(struct rpc_pipe_client *pipe_hnd,
2331 TALLOC_CTX *mem_ctx,
2332 const DOM_SID *alias_sid,
2333 const char *member)
2335 POLICY_HND connect_pol, domain_pol;
2336 NTSTATUS result;
2337 uint32 alias_rid;
2338 POLICY_HND alias_pol;
2340 DOM_SID member_sid;
2341 enum lsa_SidType member_type;
2343 DOM_SID sid;
2345 sid_copy(&sid, alias_sid);
2347 if (!sid_split_rid(&sid, &alias_rid))
2348 return NT_STATUS_UNSUCCESSFUL;
2350 result = get_sid_from_name(rpc_pipe_np_smb_conn(pipe_hnd), mem_ctx,
2351 member, &member_sid, &member_type);
2353 if (!NT_STATUS_IS_OK(result)) {
2354 d_fprintf(stderr, "Could not lookup up group member %s\n", member);
2355 return result;
2358 /* Get sam policy handle */
2359 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2360 pipe_hnd->desthost,
2361 MAXIMUM_ALLOWED_ACCESS,
2362 &connect_pol);
2363 if (!NT_STATUS_IS_OK(result)) {
2364 goto done;
2367 /* Get domain policy handle */
2368 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2369 &connect_pol,
2370 MAXIMUM_ALLOWED_ACCESS,
2371 &sid,
2372 &domain_pol);
2373 if (!NT_STATUS_IS_OK(result)) {
2374 goto done;
2377 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2378 &domain_pol,
2379 MAXIMUM_ALLOWED_ACCESS,
2380 alias_rid,
2381 &alias_pol);
2383 if (!NT_STATUS_IS_OK(result))
2384 return result;
2386 result = rpccli_samr_DeleteAliasMember(pipe_hnd, mem_ctx,
2387 &alias_pol,
2388 &member_sid);
2390 if (!NT_STATUS_IS_OK(result))
2391 return result;
2393 done:
2394 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
2395 return result;
2398 static NTSTATUS rpc_group_delmem_internals(struct net_context *c,
2399 const DOM_SID *domain_sid,
2400 const char *domain_name,
2401 struct cli_state *cli,
2402 struct rpc_pipe_client *pipe_hnd,
2403 TALLOC_CTX *mem_ctx,
2404 int argc,
2405 const char **argv)
2407 DOM_SID group_sid;
2408 enum lsa_SidType group_type;
2410 if (argc != 2 || c->display_usage) {
2411 d_printf("Usage:\n"
2412 "net rpc group delmem <group> <member>\n"
2413 " Delete a member from a group\n"
2414 " group\tGroup to delete member from\n"
2415 " member\tMember to delete from group\n");
2416 return NT_STATUS_UNSUCCESSFUL;
2419 if (!NT_STATUS_IS_OK(get_sid_from_name(cli, mem_ctx, argv[0],
2420 &group_sid, &group_type))) {
2421 d_fprintf(stderr, "Could not lookup group name %s\n", argv[0]);
2422 return NT_STATUS_UNSUCCESSFUL;
2425 if (group_type == SID_NAME_DOM_GRP) {
2426 NTSTATUS result = rpc_del_groupmem(c, pipe_hnd, mem_ctx,
2427 &group_sid, argv[1]);
2429 if (!NT_STATUS_IS_OK(result)) {
2430 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2431 argv[1], argv[0], nt_errstr(result));
2433 return result;
2436 if (group_type == SID_NAME_ALIAS) {
2437 NTSTATUS result = rpc_del_aliasmem(pipe_hnd, mem_ctx,
2438 &group_sid, argv[1]);
2440 if (!NT_STATUS_IS_OK(result)) {
2441 d_fprintf(stderr, "Could not del %s from %s: %s\n",
2442 argv[1], argv[0], nt_errstr(result));
2444 return result;
2447 d_fprintf(stderr, "Can only delete members from global or local groups "
2448 "which %s is not\n", argv[0]);
2450 return NT_STATUS_UNSUCCESSFUL;
2453 static int rpc_group_delmem(struct net_context *c, int argc, const char **argv)
2455 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2456 rpc_group_delmem_internals,
2457 argc, argv);
2461 * List groups on a remote RPC server.
2463 * All parameters are provided by the run_rpc_command function, except for
2464 * argc, argv which are passes through.
2466 * @param domain_sid The domain sid acquired from the remote server.
2467 * @param cli A cli_state connected to the server.
2468 * @param mem_ctx Talloc context, destroyed on completion of the function.
2469 * @param argc Standard main() style argc.
2470 * @param argv Standard main() style argv. Initial components are already
2471 * stripped.
2473 * @return Normal NTSTATUS return.
2476 static NTSTATUS rpc_group_list_internals(struct net_context *c,
2477 const DOM_SID *domain_sid,
2478 const char *domain_name,
2479 struct cli_state *cli,
2480 struct rpc_pipe_client *pipe_hnd,
2481 TALLOC_CTX *mem_ctx,
2482 int argc,
2483 const char **argv)
2485 POLICY_HND connect_pol, domain_pol;
2486 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
2487 uint32 start_idx=0, max_entries=250, num_entries, i, loop_count = 0;
2488 struct samr_SamArray *groups = NULL;
2489 bool global = false;
2490 bool local = false;
2491 bool builtin = false;
2493 if (c->display_usage) {
2494 d_printf("Usage:\n"
2495 "net rpc group list [global] [local] [builtin]\n"
2496 " List groups on RPC server\n"
2497 " global\tList global groups\n"
2498 " local\tList local groups\n"
2499 " builtin\tList builtin groups\n"
2500 " If none of global, local or builtin is "
2501 "specified, all three options are considered set\n");
2502 return NT_STATUS_OK;
2505 if (argc == 0) {
2506 global = true;
2507 local = true;
2508 builtin = true;
2511 for (i=0; i<argc; i++) {
2512 if (strequal(argv[i], "global"))
2513 global = true;
2515 if (strequal(argv[i], "local"))
2516 local = true;
2518 if (strequal(argv[i], "builtin"))
2519 builtin = true;
2522 /* Get sam policy handle */
2524 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2525 pipe_hnd->desthost,
2526 MAXIMUM_ALLOWED_ACCESS,
2527 &connect_pol);
2528 if (!NT_STATUS_IS_OK(result)) {
2529 goto done;
2532 /* Get domain policy handle */
2534 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2535 &connect_pol,
2536 MAXIMUM_ALLOWED_ACCESS,
2537 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2538 &domain_pol);
2539 if (!NT_STATUS_IS_OK(result)) {
2540 goto done;
2543 /* Query domain groups */
2544 if (c->opt_long_list_entries)
2545 d_printf("\nGroup name Comment"
2546 "\n-----------------------------\n");
2547 do {
2548 uint32_t max_size, total_size, returned_size;
2549 union samr_DispInfo info;
2551 if (!global) break;
2553 get_query_dispinfo_params(
2554 loop_count, &max_entries, &max_size);
2556 result = rpccli_samr_QueryDisplayInfo(pipe_hnd, mem_ctx,
2557 &domain_pol,
2559 start_idx,
2560 max_entries,
2561 max_size,
2562 &total_size,
2563 &returned_size,
2564 &info);
2565 num_entries = info.info3.count;
2566 start_idx += info.info3.count;
2568 if (!NT_STATUS_IS_OK(result) &&
2569 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2570 break;
2572 for (i = 0; i < num_entries; i++) {
2574 const char *group = NULL;
2575 const char *desc = NULL;
2577 group = info.info3.entries[i].account_name.string;
2578 desc = info.info3.entries[i].description.string;
2580 if (c->opt_long_list_entries)
2581 printf("%-21.21s %-50.50s\n",
2582 group, desc);
2583 else
2584 printf("%s\n", group);
2586 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2587 /* query domain aliases */
2588 start_idx = 0;
2589 do {
2590 if (!local) break;
2592 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2593 &domain_pol,
2594 &start_idx,
2595 &groups,
2596 0xffff,
2597 &num_entries);
2598 if (!NT_STATUS_IS_OK(result) &&
2599 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2600 break;
2602 for (i = 0; i < num_entries; i++) {
2604 const char *description = NULL;
2606 if (c->opt_long_list_entries) {
2608 POLICY_HND alias_pol;
2609 union samr_AliasInfo *info = NULL;
2611 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2612 &domain_pol,
2613 0x8,
2614 groups->entries[i].idx,
2615 &alias_pol))) &&
2616 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2617 &alias_pol,
2619 &info))) &&
2620 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2621 &alias_pol)))) {
2622 description = info->description.string;
2626 if (description != NULL) {
2627 printf("%-21.21s %-50.50s\n",
2628 groups->entries[i].name.string,
2629 description);
2630 } else {
2631 printf("%s\n", groups->entries[i].name.string);
2634 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2635 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2636 /* Get builtin policy handle */
2638 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2639 &connect_pol,
2640 MAXIMUM_ALLOWED_ACCESS,
2641 CONST_DISCARD(struct dom_sid2 *, &global_sid_Builtin),
2642 &domain_pol);
2643 if (!NT_STATUS_IS_OK(result)) {
2644 goto done;
2646 /* query builtin aliases */
2647 start_idx = 0;
2648 do {
2649 if (!builtin) break;
2651 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
2652 &domain_pol,
2653 &start_idx,
2654 &groups,
2655 max_entries,
2656 &num_entries);
2657 if (!NT_STATUS_IS_OK(result) &&
2658 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES))
2659 break;
2661 for (i = 0; i < num_entries; i++) {
2663 const char *description = NULL;
2665 if (c->opt_long_list_entries) {
2667 POLICY_HND alias_pol;
2668 union samr_AliasInfo *info = NULL;
2670 if ((NT_STATUS_IS_OK(rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2671 &domain_pol,
2672 0x8,
2673 groups->entries[i].idx,
2674 &alias_pol))) &&
2675 (NT_STATUS_IS_OK(rpccli_samr_QueryAliasInfo(pipe_hnd, mem_ctx,
2676 &alias_pol,
2678 &info))) &&
2679 (NT_STATUS_IS_OK(rpccli_samr_Close(pipe_hnd, mem_ctx,
2680 &alias_pol)))) {
2681 description = info->description.string;
2685 if (description != NULL) {
2686 printf("%-21.21s %-50.50s\n",
2687 groups->entries[i].name.string,
2688 description);
2689 } else {
2690 printf("%s\n", groups->entries[i].name.string);
2693 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
2695 done:
2696 return result;
2699 static int rpc_group_list(struct net_context *c, int argc, const char **argv)
2701 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2702 rpc_group_list_internals,
2703 argc, argv);
2706 static NTSTATUS rpc_list_group_members(struct net_context *c,
2707 struct rpc_pipe_client *pipe_hnd,
2708 TALLOC_CTX *mem_ctx,
2709 const char *domain_name,
2710 const DOM_SID *domain_sid,
2711 POLICY_HND *domain_pol,
2712 uint32 rid)
2714 NTSTATUS result;
2715 POLICY_HND group_pol;
2716 uint32 num_members, *group_rids;
2717 int i;
2718 struct samr_RidTypeArray *rids = NULL;
2719 struct lsa_Strings names;
2720 struct samr_Ids types;
2722 fstring sid_str;
2723 sid_to_fstring(sid_str, domain_sid);
2725 result = rpccli_samr_OpenGroup(pipe_hnd, mem_ctx,
2726 domain_pol,
2727 MAXIMUM_ALLOWED_ACCESS,
2728 rid,
2729 &group_pol);
2731 if (!NT_STATUS_IS_OK(result))
2732 return result;
2734 result = rpccli_samr_QueryGroupMember(pipe_hnd, mem_ctx,
2735 &group_pol,
2736 &rids);
2738 if (!NT_STATUS_IS_OK(result))
2739 return result;
2741 num_members = rids->count;
2742 group_rids = rids->rids;
2744 while (num_members > 0) {
2745 int this_time = 512;
2747 if (num_members < this_time)
2748 this_time = num_members;
2750 result = rpccli_samr_LookupRids(pipe_hnd, mem_ctx,
2751 domain_pol,
2752 this_time,
2753 group_rids,
2754 &names,
2755 &types);
2757 if (!NT_STATUS_IS_OK(result))
2758 return result;
2760 /* We only have users as members, but make the output
2761 the same as the output of alias members */
2763 for (i = 0; i < this_time; i++) {
2765 if (c->opt_long_list_entries) {
2766 printf("%s-%d %s\\%s %d\n", sid_str,
2767 group_rids[i], domain_name,
2768 names.names[i].string,
2769 SID_NAME_USER);
2770 } else {
2771 printf("%s\\%s\n", domain_name,
2772 names.names[i].string);
2776 num_members -= this_time;
2777 group_rids += 512;
2780 return NT_STATUS_OK;
2783 static NTSTATUS rpc_list_alias_members(struct net_context *c,
2784 struct rpc_pipe_client *pipe_hnd,
2785 TALLOC_CTX *mem_ctx,
2786 POLICY_HND *domain_pol,
2787 uint32 rid)
2789 NTSTATUS result;
2790 struct rpc_pipe_client *lsa_pipe;
2791 POLICY_HND alias_pol, lsa_pol;
2792 uint32 num_members;
2793 DOM_SID *alias_sids;
2794 char **domains;
2795 char **names;
2796 enum lsa_SidType *types;
2797 int i;
2798 struct lsa_SidArray sid_array;
2800 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
2801 domain_pol,
2802 MAXIMUM_ALLOWED_ACCESS,
2803 rid,
2804 &alias_pol);
2806 if (!NT_STATUS_IS_OK(result))
2807 return result;
2809 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
2810 &alias_pol,
2811 &sid_array);
2813 if (!NT_STATUS_IS_OK(result)) {
2814 d_fprintf(stderr, "Couldn't list alias members\n");
2815 return result;
2818 num_members = sid_array.num_sids;
2820 if (num_members == 0) {
2821 return NT_STATUS_OK;
2824 result = cli_rpc_pipe_open_noauth(rpc_pipe_np_smb_conn(pipe_hnd),
2825 &ndr_table_lsarpc.syntax_id,
2826 &lsa_pipe);
2827 if (!NT_STATUS_IS_OK(result)) {
2828 d_fprintf(stderr, "Couldn't open LSA pipe. Error was %s\n",
2829 nt_errstr(result) );
2830 return result;
2833 result = rpccli_lsa_open_policy(lsa_pipe, mem_ctx, true,
2834 SEC_RIGHTS_MAXIMUM_ALLOWED, &lsa_pol);
2836 if (!NT_STATUS_IS_OK(result)) {
2837 d_fprintf(stderr, "Couldn't open LSA policy handle\n");
2838 TALLOC_FREE(lsa_pipe);
2839 return result;
2842 alias_sids = TALLOC_ZERO_ARRAY(mem_ctx, DOM_SID, num_members);
2843 if (!alias_sids) {
2844 d_fprintf(stderr, "Out of memory\n");
2845 TALLOC_FREE(lsa_pipe);
2846 return NT_STATUS_NO_MEMORY;
2849 for (i=0; i<num_members; i++) {
2850 sid_copy(&alias_sids[i], sid_array.sids[i].sid);
2853 result = rpccli_lsa_lookup_sids(lsa_pipe, mem_ctx, &lsa_pol,
2854 num_members, alias_sids,
2855 &domains, &names, &types);
2857 if (!NT_STATUS_IS_OK(result) &&
2858 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
2859 d_fprintf(stderr, "Couldn't lookup SIDs\n");
2860 TALLOC_FREE(lsa_pipe);
2861 return result;
2864 for (i = 0; i < num_members; i++) {
2865 fstring sid_str;
2866 sid_to_fstring(sid_str, &alias_sids[i]);
2868 if (c->opt_long_list_entries) {
2869 printf("%s %s\\%s %d\n", sid_str,
2870 domains[i] ? domains[i] : "*unknown*",
2871 names[i] ? names[i] : "*unknown*", types[i]);
2872 } else {
2873 if (domains[i])
2874 printf("%s\\%s\n", domains[i], names[i]);
2875 else
2876 printf("%s\n", sid_str);
2880 TALLOC_FREE(lsa_pipe);
2881 return NT_STATUS_OK;
2884 static NTSTATUS rpc_group_members_internals(struct net_context *c,
2885 const DOM_SID *domain_sid,
2886 const char *domain_name,
2887 struct cli_state *cli,
2888 struct rpc_pipe_client *pipe_hnd,
2889 TALLOC_CTX *mem_ctx,
2890 int argc,
2891 const char **argv)
2893 NTSTATUS result;
2894 POLICY_HND connect_pol, domain_pol;
2895 struct samr_Ids rids, rid_types;
2896 struct lsa_String lsa_acct_name;
2898 /* Get sam policy handle */
2900 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
2901 pipe_hnd->desthost,
2902 MAXIMUM_ALLOWED_ACCESS,
2903 &connect_pol);
2905 if (!NT_STATUS_IS_OK(result))
2906 return result;
2908 /* Get domain policy handle */
2910 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2911 &connect_pol,
2912 MAXIMUM_ALLOWED_ACCESS,
2913 CONST_DISCARD(struct dom_sid2 *, domain_sid),
2914 &domain_pol);
2916 if (!NT_STATUS_IS_OK(result))
2917 return result;
2919 init_lsa_String(&lsa_acct_name, argv[0]); /* sure? */
2921 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2922 &domain_pol,
2924 &lsa_acct_name,
2925 &rids,
2926 &rid_types);
2928 if (!NT_STATUS_IS_OK(result)) {
2930 /* Ok, did not find it in the global sam, try with builtin */
2932 DOM_SID sid_Builtin;
2934 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
2936 sid_copy(&sid_Builtin, &global_sid_Builtin);
2938 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
2939 &connect_pol,
2940 MAXIMUM_ALLOWED_ACCESS,
2941 &sid_Builtin,
2942 &domain_pol);
2944 if (!NT_STATUS_IS_OK(result)) {
2945 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2946 return result;
2949 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
2950 &domain_pol,
2952 &lsa_acct_name,
2953 &rids,
2954 &rid_types);
2956 if (!NT_STATUS_IS_OK(result)) {
2957 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2958 return result;
2962 if (rids.count != 1) {
2963 d_fprintf(stderr, "Couldn't find group %s\n", argv[0]);
2964 return result;
2967 if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
2968 return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
2969 domain_sid, &domain_pol,
2970 rids.ids[0]);
2973 if (rid_types.ids[0] == SID_NAME_ALIAS) {
2974 return rpc_list_alias_members(c, pipe_hnd, mem_ctx, &domain_pol,
2975 rids.ids[0]);
2978 return NT_STATUS_NO_SUCH_GROUP;
2981 static int rpc_group_members(struct net_context *c, int argc, const char **argv)
2983 if (argc != 1 || c->display_usage) {
2984 return rpc_group_usage(c, argc, argv);
2987 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
2988 rpc_group_members_internals,
2989 argc, argv);
2992 static int rpc_group_rename_internals(struct net_context *c, int argc, const char **argv)
2994 NET_API_STATUS status;
2995 struct GROUP_INFO_0 g0;
2996 uint32_t parm_err;
2998 if (argc != 2) {
2999 d_printf("Usage: 'net rpc group rename group newname'\n");
3000 return -1;
3003 g0.grpi0_name = argv[1];
3005 status = NetGroupSetInfo(c->opt_host,
3006 argv[0],
3008 (uint8_t *)&g0,
3009 &parm_err);
3011 if (status != 0) {
3012 d_fprintf(stderr, "Renaming group %s failed with: %s\n",
3013 argv[0], libnetapi_get_error_string(c->netapi_ctx,
3014 status));
3015 return -1;
3018 return 0;
3021 static int rpc_group_rename(struct net_context *c, int argc, const char **argv)
3023 if (argc != 2 || c->display_usage) {
3024 return rpc_group_usage(c, argc, argv);
3027 return rpc_group_rename_internals(c, argc, argv);
3031 * 'net rpc group' entrypoint.
3032 * @param argc Standard main() style argc.
3033 * @param argv Standard main() style argv. Initial components are already
3034 * stripped.
3037 int net_rpc_group(struct net_context *c, int argc, const char **argv)
3039 NET_API_STATUS status;
3041 struct functable func[] = {
3043 "add",
3044 rpc_group_add,
3045 NET_TRANSPORT_RPC,
3046 "Create specified group",
3047 "net rpc group add\n"
3048 " Create specified group"
3051 "delete",
3052 rpc_group_delete,
3053 NET_TRANSPORT_RPC,
3054 "Delete specified group",
3055 "net rpc group delete\n"
3056 " Delete specified group"
3059 "addmem",
3060 rpc_group_addmem,
3061 NET_TRANSPORT_RPC,
3062 "Add member to group",
3063 "net rpc group addmem\n"
3064 " Add member to group"
3067 "delmem",
3068 rpc_group_delmem,
3069 NET_TRANSPORT_RPC,
3070 "Remove member from group",
3071 "net rpc group delmem\n"
3072 " Remove member from group"
3075 "list",
3076 rpc_group_list,
3077 NET_TRANSPORT_RPC,
3078 "List groups",
3079 "net rpc group list\n"
3080 " List groups"
3083 "members",
3084 rpc_group_members,
3085 NET_TRANSPORT_RPC,
3086 "List group members",
3087 "net rpc group members\n"
3088 " List group members"
3091 "rename",
3092 rpc_group_rename,
3093 NET_TRANSPORT_RPC,
3094 "Rename group",
3095 "net rpc group rename\n"
3096 " Rename group"
3098 {NULL, NULL, 0, NULL, NULL}
3101 status = libnetapi_init(&c->netapi_ctx);
3102 if (status != 0) {
3103 return -1;
3105 libnetapi_set_username(c->netapi_ctx, c->opt_user_name);
3106 libnetapi_set_password(c->netapi_ctx, c->opt_password);
3108 if (argc == 0) {
3109 if (c->display_usage) {
3110 d_printf("Usage:\n");
3111 d_printf("net rpc group\n"
3112 " Alias for net rpc group list global local "
3113 "builtin\n");
3114 net_display_usage_from_functable(func);
3115 return 0;
3118 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
3119 rpc_group_list_internals,
3120 argc, argv);
3123 return net_run_function(c, argc, argv, "net rpc group", func);
3126 /****************************************************************************/
3128 static int rpc_share_usage(struct net_context *c, int argc, const char **argv)
3130 return net_share_usage(c, argc, argv);
3134 * Add a share on a remote RPC server.
3136 * All parameters are provided by the run_rpc_command function, except for
3137 * argc, argv which are passed through.
3139 * @param domain_sid The domain sid acquired from the remote server.
3140 * @param cli A cli_state connected to the server.
3141 * @param mem_ctx Talloc context, destroyed on completion of the function.
3142 * @param argc Standard main() style argc.
3143 * @param argv Standard main() style argv. Initial components are already
3144 * stripped.
3146 * @return Normal NTSTATUS return.
3148 static NTSTATUS rpc_share_add_internals(struct net_context *c,
3149 const DOM_SID *domain_sid,
3150 const char *domain_name,
3151 struct cli_state *cli,
3152 struct rpc_pipe_client *pipe_hnd,
3153 TALLOC_CTX *mem_ctx,int argc,
3154 const char **argv)
3156 WERROR result;
3157 NTSTATUS status;
3158 char *sharename;
3159 char *path;
3160 uint32 type = STYPE_DISKTREE; /* only allow disk shares to be added */
3161 uint32 num_users=0, perms=0;
3162 char *password=NULL; /* don't allow a share password */
3163 uint32 level = 2;
3164 union srvsvc_NetShareInfo info;
3165 struct srvsvc_NetShareInfo2 info2;
3166 uint32_t parm_error = 0;
3168 if ((sharename = talloc_strdup(mem_ctx, argv[0])) == NULL) {
3169 return NT_STATUS_NO_MEMORY;
3172 path = strchr(sharename, '=');
3173 if (!path)
3174 return NT_STATUS_UNSUCCESSFUL;
3175 *path++ = '\0';
3177 info2.name = sharename;
3178 info2.type = type;
3179 info2.comment = c->opt_comment;
3180 info2.permissions = perms;
3181 info2.max_users = c->opt_maxusers;
3182 info2.current_users = num_users;
3183 info2.path = path;
3184 info2.password = password;
3186 info.info2 = &info2;
3188 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
3189 pipe_hnd->desthost,
3190 level,
3191 &info,
3192 &parm_error,
3193 &result);
3194 return status;
3197 static int rpc_share_add(struct net_context *c, int argc, const char **argv)
3199 if ((argc < 1) || !strchr(argv[0], '=') || c->display_usage) {
3200 return rpc_share_usage(c, argc, argv);
3202 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3203 rpc_share_add_internals,
3204 argc, argv);
3208 * Delete a share on a remote RPC server.
3210 * All parameters are provided by the run_rpc_command function, except for
3211 * argc, argv which are passed through.
3213 * @param domain_sid The domain sid acquired from the remote server.
3214 * @param cli A cli_state connected to the server.
3215 * @param mem_ctx Talloc context, destroyed on completion of the function.
3216 * @param argc Standard main() style argc.
3217 * @param argv Standard main() style argv. Initial components are already
3218 * stripped.
3220 * @return Normal NTSTATUS return.
3222 static NTSTATUS rpc_share_del_internals(struct net_context *c,
3223 const DOM_SID *domain_sid,
3224 const char *domain_name,
3225 struct cli_state *cli,
3226 struct rpc_pipe_client *pipe_hnd,
3227 TALLOC_CTX *mem_ctx,
3228 int argc,
3229 const char **argv)
3231 WERROR result;
3233 return rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
3234 pipe_hnd->desthost,
3235 argv[0],
3237 &result);
3241 * Delete a share on a remote RPC server.
3243 * @param domain_sid The domain sid acquired from the remote server.
3244 * @param argc Standard main() style argc.
3245 * @param argv Standard main() style argv. Initial components are already
3246 * stripped.
3248 * @return A shell status integer (0 for success).
3250 static int rpc_share_delete(struct net_context *c, int argc, const char **argv)
3252 if (argc < 1 || c->display_usage) {
3253 return rpc_share_usage(c, argc, argv);
3255 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3256 rpc_share_del_internals,
3257 argc, argv);
3261 * Formatted print of share info
3263 * @param info1 pointer to SRV_SHARE_INFO_1 to format
3266 static void display_share_info_1(struct net_context *c,
3267 struct srvsvc_NetShareInfo1 *r)
3269 if (c->opt_long_list_entries) {
3270 d_printf("%-12s %-8.8s %-50s\n",
3271 r->name,
3272 c->share_type[r->type & ~(STYPE_TEMPORARY|STYPE_HIDDEN)],
3273 r->comment);
3274 } else {
3275 d_printf("%s\n", r->name);
3279 static WERROR get_share_info(struct net_context *c,
3280 struct rpc_pipe_client *pipe_hnd,
3281 TALLOC_CTX *mem_ctx,
3282 uint32 level,
3283 int argc,
3284 const char **argv,
3285 struct srvsvc_NetShareInfoCtr *info_ctr)
3287 WERROR result;
3288 NTSTATUS status;
3289 union srvsvc_NetShareInfo info;
3291 /* no specific share requested, enumerate all */
3292 if (argc == 0) {
3294 uint32_t preferred_len = 0xffffffff;
3295 uint32_t total_entries = 0;
3296 uint32_t resume_handle = 0;
3298 info_ctr->level = level;
3300 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
3301 pipe_hnd->desthost,
3302 info_ctr,
3303 preferred_len,
3304 &total_entries,
3305 &resume_handle,
3306 &result);
3307 return result;
3310 /* request just one share */
3311 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
3312 pipe_hnd->desthost,
3313 argv[0],
3314 level,
3315 &info,
3316 &result);
3318 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
3319 goto done;
3322 /* construct ctr */
3323 ZERO_STRUCTP(info_ctr);
3325 info_ctr->level = level;
3327 switch (level) {
3328 case 1:
3330 struct srvsvc_NetShareCtr1 *ctr1;
3332 ctr1 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr1);
3333 W_ERROR_HAVE_NO_MEMORY(ctr1);
3335 ctr1->count = 1;
3336 ctr1->array = info.info1;
3338 info_ctr->ctr.ctr1 = ctr1;
3340 case 2:
3342 struct srvsvc_NetShareCtr2 *ctr2;
3344 ctr2 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr2);
3345 W_ERROR_HAVE_NO_MEMORY(ctr2);
3347 ctr2->count = 1;
3348 ctr2->array = info.info2;
3350 info_ctr->ctr.ctr2 = ctr2;
3352 case 502:
3354 struct srvsvc_NetShareCtr502 *ctr502;
3356 ctr502 = TALLOC_ZERO_P(mem_ctx, struct srvsvc_NetShareCtr502);
3357 W_ERROR_HAVE_NO_MEMORY(ctr502);
3359 ctr502->count = 1;
3360 ctr502->array = info.info502;
3362 info_ctr->ctr.ctr502 = ctr502;
3364 } /* switch */
3365 done:
3366 return result;
3370 * List shares on a remote RPC server.
3372 * All parameters are provided by the run_rpc_command function, except for
3373 * argc, argv which are passed through.
3375 * @param domain_sid The domain sid acquired from the remote server.
3376 * @param cli A cli_state connected to the server.
3377 * @param mem_ctx Talloc context, destroyed on completion of the function.
3378 * @param argc Standard main() style argc.
3379 * @param argv Standard main() style argv. Initial components are already
3380 * stripped.
3382 * @return Normal NTSTATUS return.
3385 static NTSTATUS rpc_share_list_internals(struct net_context *c,
3386 const DOM_SID *domain_sid,
3387 const char *domain_name,
3388 struct cli_state *cli,
3389 struct rpc_pipe_client *pipe_hnd,
3390 TALLOC_CTX *mem_ctx,
3391 int argc,
3392 const char **argv)
3394 struct srvsvc_NetShareInfoCtr info_ctr;
3395 struct srvsvc_NetShareCtr1 ctr1;
3396 WERROR result;
3397 uint32 i, level = 1;
3399 ZERO_STRUCT(info_ctr);
3400 ZERO_STRUCT(ctr1);
3402 info_ctr.level = 1;
3403 info_ctr.ctr.ctr1 = &ctr1;
3405 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3406 &info_ctr);
3407 if (!W_ERROR_IS_OK(result))
3408 goto done;
3410 /* Display results */
3412 if (c->opt_long_list_entries) {
3413 d_printf(
3414 "\nEnumerating shared resources (exports) on remote server:\n\n"
3415 "\nShare name Type Description\n"
3416 "---------- ---- -----------\n");
3418 for (i = 0; i < info_ctr.ctr.ctr1->count; i++)
3419 display_share_info_1(c, &info_ctr.ctr.ctr1->array[i]);
3420 done:
3421 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
3424 /***
3425 * 'net rpc share list' entrypoint.
3426 * @param argc Standard main() style argc.
3427 * @param argv Standard main() style argv. Initial components are already
3428 * stripped.
3430 static int rpc_share_list(struct net_context *c, int argc, const char **argv)
3432 if (c->display_usage) {
3433 d_printf("Usage\n"
3434 "net rpc share list\n"
3435 " List shares on remote server\n");
3436 return 0;
3439 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3440 rpc_share_list_internals, argc, argv);
3443 static bool check_share_availability(struct cli_state *cli, const char *netname)
3445 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
3446 d_printf("skipping [%s]: not a file share.\n", netname);
3447 return false;
3450 if (!cli_tdis(cli))
3451 return false;
3453 return true;
3456 static bool check_share_sanity(struct net_context *c, struct cli_state *cli,
3457 const char *netname, uint32 type)
3459 /* only support disk shares */
3460 if (! ( type == STYPE_DISKTREE || type == (STYPE_DISKTREE | STYPE_HIDDEN)) ) {
3461 printf("share [%s] is not a diskshare (type: %x)\n", netname, type);
3462 return false;
3465 /* skip builtin shares */
3466 /* FIXME: should print$ be added too ? */
3467 if (strequal(netname,"IPC$") || strequal(netname,"ADMIN$") ||
3468 strequal(netname,"global"))
3469 return false;
3471 if (c->opt_exclude && in_list(netname, c->opt_exclude, false)) {
3472 printf("excluding [%s]\n", netname);
3473 return false;
3476 return check_share_availability(cli, netname);
3480 * Migrate shares from a remote RPC server to the local RPC server.
3482 * All parameters are provided by the run_rpc_command function, except for
3483 * argc, argv which are passed through.
3485 * @param domain_sid The domain sid acquired from the remote server.
3486 * @param cli A cli_state connected to the server.
3487 * @param mem_ctx Talloc context, destroyed on completion of the function.
3488 * @param argc Standard main() style argc.
3489 * @param argv Standard main() style argv. Initial components are already
3490 * stripped.
3492 * @return Normal NTSTATUS return.
3495 static NTSTATUS rpc_share_migrate_shares_internals(struct net_context *c,
3496 const DOM_SID *domain_sid,
3497 const char *domain_name,
3498 struct cli_state *cli,
3499 struct rpc_pipe_client *pipe_hnd,
3500 TALLOC_CTX *mem_ctx,
3501 int argc,
3502 const char **argv)
3504 WERROR result;
3505 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3506 struct srvsvc_NetShareInfoCtr ctr_src;
3507 uint32 i;
3508 struct rpc_pipe_client *srvsvc_pipe = NULL;
3509 struct cli_state *cli_dst = NULL;
3510 uint32 level = 502; /* includes secdesc */
3511 uint32_t parm_error = 0;
3513 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3514 &ctr_src);
3515 if (!W_ERROR_IS_OK(result))
3516 goto done;
3518 /* connect destination PI_SRVSVC */
3519 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3520 &ndr_table_srvsvc.syntax_id);
3521 if (!NT_STATUS_IS_OK(nt_status))
3522 return nt_status;
3525 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3527 union srvsvc_NetShareInfo info;
3528 struct srvsvc_NetShareInfo502 info502 =
3529 ctr_src.ctr.ctr502->array[i];
3531 /* reset error-code */
3532 nt_status = NT_STATUS_UNSUCCESSFUL;
3534 if (!check_share_sanity(c, cli, info502.name, info502.type))
3535 continue;
3537 /* finally add the share on the dst server */
3539 printf("migrating: [%s], path: %s, comment: %s, without share-ACLs\n",
3540 info502.name, info502.path, info502.comment);
3542 info.info502 = &info502;
3544 nt_status = rpccli_srvsvc_NetShareAdd(srvsvc_pipe, mem_ctx,
3545 srvsvc_pipe->desthost,
3546 502,
3547 &info,
3548 &parm_error,
3549 &result);
3551 if (W_ERROR_V(result) == W_ERROR_V(WERR_ALREADY_EXISTS)) {
3552 printf(" [%s] does already exist\n",
3553 info502.name);
3554 continue;
3557 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
3558 printf("cannot add share: %s\n", dos_errstr(result));
3559 goto done;
3564 nt_status = NT_STATUS_OK;
3566 done:
3567 if (cli_dst) {
3568 cli_shutdown(cli_dst);
3571 return nt_status;
3576 * Migrate shares from a RPC server to another.
3578 * @param argc Standard main() style argc.
3579 * @param argv Standard main() style argv. Initial components are already
3580 * stripped.
3582 * @return A shell status integer (0 for success).
3584 static int rpc_share_migrate_shares(struct net_context *c, int argc,
3585 const char **argv)
3587 if (c->display_usage) {
3588 d_printf("Usage:\n"
3589 "net rpc share migrate shares\n"
3590 " Migrate shares to local server\n");
3591 return 0;
3594 if (!c->opt_host) {
3595 printf("no server to migrate\n");
3596 return -1;
3599 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3600 rpc_share_migrate_shares_internals,
3601 argc, argv);
3605 * Copy a file/dir
3607 * @param f file_info
3608 * @param mask current search mask
3609 * @param state arg-pointer
3612 static void copy_fn(const char *mnt, file_info *f,
3613 const char *mask, void *state)
3615 static NTSTATUS nt_status;
3616 static struct copy_clistate *local_state;
3617 static fstring filename, new_mask;
3618 fstring dir;
3619 char *old_dir;
3620 struct net_context *c;
3622 local_state = (struct copy_clistate *)state;
3623 nt_status = NT_STATUS_UNSUCCESSFUL;
3625 c = local_state->c;
3627 if (strequal(f->name, ".") || strequal(f->name, ".."))
3628 return;
3630 DEBUG(3,("got mask: %s, name: %s\n", mask, f->name));
3632 /* DIRECTORY */
3633 if (f->mode & aDIR) {
3635 DEBUG(3,("got dir: %s\n", f->name));
3637 fstrcpy(dir, local_state->cwd);
3638 fstrcat(dir, "\\");
3639 fstrcat(dir, f->name);
3641 switch (net_mode_share)
3643 case NET_MODE_SHARE_MIGRATE:
3644 /* create that directory */
3645 nt_status = net_copy_file(c, local_state->mem_ctx,
3646 local_state->cli_share_src,
3647 local_state->cli_share_dst,
3648 dir, dir,
3649 c->opt_acls? true : false,
3650 c->opt_attrs? true : false,
3651 c->opt_timestamps? true:false,
3652 false);
3653 break;
3654 default:
3655 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3656 return;
3659 if (!NT_STATUS_IS_OK(nt_status))
3660 printf("could not handle dir %s: %s\n",
3661 dir, nt_errstr(nt_status));
3663 /* search below that directory */
3664 fstrcpy(new_mask, dir);
3665 fstrcat(new_mask, "\\*");
3667 old_dir = local_state->cwd;
3668 local_state->cwd = dir;
3669 if (!sync_files(local_state, new_mask))
3670 printf("could not handle files\n");
3671 local_state->cwd = old_dir;
3673 return;
3677 /* FILE */
3678 fstrcpy(filename, local_state->cwd);
3679 fstrcat(filename, "\\");
3680 fstrcat(filename, f->name);
3682 DEBUG(3,("got file: %s\n", filename));
3684 switch (net_mode_share)
3686 case NET_MODE_SHARE_MIGRATE:
3687 nt_status = net_copy_file(c, local_state->mem_ctx,
3688 local_state->cli_share_src,
3689 local_state->cli_share_dst,
3690 filename, filename,
3691 c->opt_acls? true : false,
3692 c->opt_attrs? true : false,
3693 c->opt_timestamps? true: false,
3694 true);
3695 break;
3696 default:
3697 d_fprintf(stderr, "Unsupported file mode %d\n", net_mode_share);
3698 return;
3701 if (!NT_STATUS_IS_OK(nt_status))
3702 printf("could not handle file %s: %s\n",
3703 filename, nt_errstr(nt_status));
3708 * sync files, can be called recursivly to list files
3709 * and then call copy_fn for each file
3711 * @param cp_clistate pointer to the copy_clistate we work with
3712 * @param mask the current search mask
3714 * @return Boolean result
3716 static bool sync_files(struct copy_clistate *cp_clistate, const char *mask)
3718 struct cli_state *targetcli;
3719 char *targetpath = NULL;
3721 DEBUG(3,("calling cli_list with mask: %s\n", mask));
3723 if ( !cli_resolve_path(talloc_tos(), "", cp_clistate->cli_share_src,
3724 mask, &targetcli, &targetpath ) ) {
3725 d_fprintf(stderr, "cli_resolve_path %s failed with error: %s\n",
3726 mask, cli_errstr(cp_clistate->cli_share_src));
3727 return false;
3730 if (cli_list(targetcli, targetpath, cp_clistate->attribute, copy_fn, cp_clistate) == -1) {
3731 d_fprintf(stderr, "listing %s failed with error: %s\n",
3732 mask, cli_errstr(targetcli));
3733 return false;
3736 return true;
3741 * Set the top level directory permissions before we do any further copies.
3742 * Should set up ACL inheritance.
3745 bool copy_top_level_perms(struct net_context *c,
3746 struct copy_clistate *cp_clistate,
3747 const char *sharename)
3749 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3751 switch (net_mode_share) {
3752 case NET_MODE_SHARE_MIGRATE:
3753 DEBUG(3,("calling net_copy_fileattr for '.' directory in share %s\n", sharename));
3754 nt_status = net_copy_fileattr(c,
3755 cp_clistate->mem_ctx,
3756 cp_clistate->cli_share_src,
3757 cp_clistate->cli_share_dst,
3758 "\\", "\\",
3759 c->opt_acls? true : false,
3760 c->opt_attrs? true : false,
3761 c->opt_timestamps? true: false,
3762 false);
3763 break;
3764 default:
3765 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3766 break;
3769 if (!NT_STATUS_IS_OK(nt_status)) {
3770 printf("Could handle directory attributes for top level directory of share %s. Error %s\n",
3771 sharename, nt_errstr(nt_status));
3772 return false;
3775 return true;
3779 * Sync all files inside a remote share to another share (over smb).
3781 * All parameters are provided by the run_rpc_command function, except for
3782 * argc, argv which are passed through.
3784 * @param domain_sid The domain sid acquired from the remote server.
3785 * @param cli A cli_state connected to the server.
3786 * @param mem_ctx Talloc context, destroyed on completion of the function.
3787 * @param argc Standard main() style argc.
3788 * @param argv Standard main() style argv. Initial components are already
3789 * stripped.
3791 * @return Normal NTSTATUS return.
3794 static NTSTATUS rpc_share_migrate_files_internals(struct net_context *c,
3795 const DOM_SID *domain_sid,
3796 const char *domain_name,
3797 struct cli_state *cli,
3798 struct rpc_pipe_client *pipe_hnd,
3799 TALLOC_CTX *mem_ctx,
3800 int argc,
3801 const char **argv)
3803 WERROR result;
3804 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3805 struct srvsvc_NetShareInfoCtr ctr_src;
3806 uint32 i;
3807 uint32 level = 502;
3808 struct copy_clistate cp_clistate;
3809 bool got_src_share = false;
3810 bool got_dst_share = false;
3811 const char *mask = "\\*";
3812 char *dst = NULL;
3814 dst = SMB_STRDUP(c->opt_destination?c->opt_destination:"127.0.0.1");
3815 if (dst == NULL) {
3816 nt_status = NT_STATUS_NO_MEMORY;
3817 goto done;
3820 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3821 &ctr_src);
3823 if (!W_ERROR_IS_OK(result))
3824 goto done;
3826 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3828 struct srvsvc_NetShareInfo502 info502 =
3829 ctr_src.ctr.ctr502->array[i];
3831 if (!check_share_sanity(c, cli, info502.name, info502.type))
3832 continue;
3834 /* one might not want to mirror whole discs :) */
3835 if (strequal(info502.name, "print$") || info502.name[1] == '$') {
3836 d_printf("skipping [%s]: builtin/hidden share\n", info502.name);
3837 continue;
3840 switch (net_mode_share)
3842 case NET_MODE_SHARE_MIGRATE:
3843 printf("syncing");
3844 break;
3845 default:
3846 d_fprintf(stderr, "Unsupported mode %d\n", net_mode_share);
3847 break;
3849 printf(" [%s] files and directories %s ACLs, %s DOS Attributes %s\n",
3850 info502.name,
3851 c->opt_acls ? "including" : "without",
3852 c->opt_attrs ? "including" : "without",
3853 c->opt_timestamps ? "(preserving timestamps)" : "");
3855 cp_clistate.mem_ctx = mem_ctx;
3856 cp_clistate.cli_share_src = NULL;
3857 cp_clistate.cli_share_dst = NULL;
3858 cp_clistate.cwd = NULL;
3859 cp_clistate.attribute = aSYSTEM | aHIDDEN | aDIR;
3860 cp_clistate.c = c;
3862 /* open share source */
3863 nt_status = connect_to_service(c, &cp_clistate.cli_share_src,
3864 &cli->dest_ss, cli->desthost,
3865 info502.name, "A:");
3866 if (!NT_STATUS_IS_OK(nt_status))
3867 goto done;
3869 got_src_share = true;
3871 if (net_mode_share == NET_MODE_SHARE_MIGRATE) {
3872 /* open share destination */
3873 nt_status = connect_to_service(c, &cp_clistate.cli_share_dst,
3874 NULL, dst, info502.name, "A:");
3875 if (!NT_STATUS_IS_OK(nt_status))
3876 goto done;
3878 got_dst_share = true;
3881 if (!copy_top_level_perms(c, &cp_clistate, info502.name)) {
3882 d_fprintf(stderr, "Could not handle the top level directory permissions for the share: %s\n", info502.name);
3883 nt_status = NT_STATUS_UNSUCCESSFUL;
3884 goto done;
3887 if (!sync_files(&cp_clistate, mask)) {
3888 d_fprintf(stderr, "could not handle files for share: %s\n", info502.name);
3889 nt_status = NT_STATUS_UNSUCCESSFUL;
3890 goto done;
3894 nt_status = NT_STATUS_OK;
3896 done:
3898 if (got_src_share)
3899 cli_shutdown(cp_clistate.cli_share_src);
3901 if (got_dst_share)
3902 cli_shutdown(cp_clistate.cli_share_dst);
3904 SAFE_FREE(dst);
3905 return nt_status;
3909 static int rpc_share_migrate_files(struct net_context *c, int argc, const char **argv)
3911 if (c->display_usage) {
3912 d_printf("Usage:\n"
3913 "net share migrate files\n"
3914 " Migrate files to local server\n");
3915 return 0;
3918 if (!c->opt_host) {
3919 d_printf("no server to migrate\n");
3920 return -1;
3923 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
3924 rpc_share_migrate_files_internals,
3925 argc, argv);
3929 * Migrate share-ACLs from a remote RPC server to the local RPC server.
3931 * All parameters are provided by the run_rpc_command function, except for
3932 * argc, argv which are passed through.
3934 * @param domain_sid The domain sid acquired from the remote server.
3935 * @param cli A cli_state connected to the server.
3936 * @param mem_ctx Talloc context, destroyed on completion of the function.
3937 * @param argc Standard main() style argc.
3938 * @param argv Standard main() style argv. Initial components are already
3939 * stripped.
3941 * @return Normal NTSTATUS return.
3944 static NTSTATUS rpc_share_migrate_security_internals(struct net_context *c,
3945 const DOM_SID *domain_sid,
3946 const char *domain_name,
3947 struct cli_state *cli,
3948 struct rpc_pipe_client *pipe_hnd,
3949 TALLOC_CTX *mem_ctx,
3950 int argc,
3951 const char **argv)
3953 WERROR result;
3954 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
3955 struct srvsvc_NetShareInfoCtr ctr_src;
3956 union srvsvc_NetShareInfo info;
3957 uint32 i;
3958 struct rpc_pipe_client *srvsvc_pipe = NULL;
3959 struct cli_state *cli_dst = NULL;
3960 uint32 level = 502; /* includes secdesc */
3961 uint32_t parm_error = 0;
3963 result = get_share_info(c, pipe_hnd, mem_ctx, level, argc, argv,
3964 &ctr_src);
3966 if (!W_ERROR_IS_OK(result))
3967 goto done;
3969 /* connect destination PI_SRVSVC */
3970 nt_status = connect_dst_pipe(c, &cli_dst, &srvsvc_pipe,
3971 &ndr_table_srvsvc.syntax_id);
3972 if (!NT_STATUS_IS_OK(nt_status))
3973 return nt_status;
3976 for (i = 0; i < ctr_src.ctr.ctr502->count; i++) {
3978 struct srvsvc_NetShareInfo502 info502 =
3979 ctr_src.ctr.ctr502->array[i];
3981 /* reset error-code */
3982 nt_status = NT_STATUS_UNSUCCESSFUL;
3984 if (!check_share_sanity(c, cli, info502.name, info502.type))
3985 continue;
3987 printf("migrating: [%s], path: %s, comment: %s, including share-ACLs\n",
3988 info502.name, info502.path, info502.comment);
3990 if (c->opt_verbose)
3991 display_sec_desc(info502.sd_buf.sd);
3993 /* FIXME: shouldn't we be able to just set the security descriptor ? */
3994 info.info502 = &info502;
3996 /* finally modify the share on the dst server */
3997 nt_status = rpccli_srvsvc_NetShareSetInfo(srvsvc_pipe, mem_ctx,
3998 srvsvc_pipe->desthost,
3999 info502.name,
4000 level,
4001 &info,
4002 &parm_error,
4003 &result);
4004 if (!NT_STATUS_IS_OK(nt_status) || !W_ERROR_IS_OK(result)) {
4005 printf("cannot set share-acl: %s\n", dos_errstr(result));
4006 goto done;
4011 nt_status = NT_STATUS_OK;
4013 done:
4014 if (cli_dst) {
4015 cli_shutdown(cli_dst);
4018 return nt_status;
4023 * Migrate share-acls from a RPC server to another.
4025 * @param argc Standard main() style argc.
4026 * @param argv Standard main() style argv. Initial components are already
4027 * stripped.
4029 * @return A shell status integer (0 for success).
4031 static int rpc_share_migrate_security(struct net_context *c, int argc,
4032 const char **argv)
4034 if (c->display_usage) {
4035 d_printf("Usage:\n"
4036 "net rpc share migrate security\n"
4037 " Migrate share-acls to local server\n");
4038 return 0;
4041 if (!c->opt_host) {
4042 d_printf("no server to migrate\n");
4043 return -1;
4046 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4047 rpc_share_migrate_security_internals,
4048 argc, argv);
4052 * Migrate shares (including share-definitions, share-acls and files with acls/attrs)
4053 * from one server to another.
4055 * @param argc Standard main() style argc.
4056 * @param argv Standard main() style argv. Initial components are already
4057 * stripped.
4059 * @return A shell status integer (0 for success).
4062 static int rpc_share_migrate_all(struct net_context *c, int argc,
4063 const char **argv)
4065 int ret;
4067 if (c->display_usage) {
4068 d_printf("Usage:\n"
4069 "net rpc share migrate all\n"
4070 " Migrates shares including all share settings\n");
4071 return 0;
4074 if (!c->opt_host) {
4075 d_printf("no server to migrate\n");
4076 return -1;
4079 /* order is important. we don't want to be locked out by the share-acl
4080 * before copying files - gd */
4082 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4083 rpc_share_migrate_shares_internals, argc, argv);
4084 if (ret)
4085 return ret;
4087 ret = run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4088 rpc_share_migrate_files_internals, argc, argv);
4089 if (ret)
4090 return ret;
4092 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4093 rpc_share_migrate_security_internals, argc,
4094 argv);
4099 * 'net rpc share migrate' entrypoint.
4100 * @param argc Standard main() style argc.
4101 * @param argv Standard main() style argv. Initial components are already
4102 * stripped.
4104 static int rpc_share_migrate(struct net_context *c, int argc, const char **argv)
4107 struct functable func[] = {
4109 "all",
4110 rpc_share_migrate_all,
4111 NET_TRANSPORT_RPC,
4112 "Migrate shares from remote to local server",
4113 "net rpc share migrate all\n"
4114 " Migrate shares from remote to local server"
4117 "files",
4118 rpc_share_migrate_files,
4119 NET_TRANSPORT_RPC,
4120 "Migrate files from remote to local server",
4121 "net rpc share migrate files\n"
4122 " Migrate files from remote to local server"
4125 "security",
4126 rpc_share_migrate_security,
4127 NET_TRANSPORT_RPC,
4128 "Migrate share-ACLs from remote to local server",
4129 "net rpc share migrate security\n"
4130 " Migrate share-ACLs from remote to local server"
4133 "shares",
4134 rpc_share_migrate_shares,
4135 NET_TRANSPORT_RPC,
4136 "Migrate shares from remote to local server",
4137 "net rpc share migrate shares\n"
4138 " Migrate shares from remote to local server"
4140 {NULL, NULL, 0, NULL, NULL}
4143 net_mode_share = NET_MODE_SHARE_MIGRATE;
4145 return net_run_function(c, argc, argv, "net rpc share migrate", func);
4148 struct full_alias {
4149 DOM_SID sid;
4150 uint32 num_members;
4151 DOM_SID *members;
4154 static int num_server_aliases;
4155 static struct full_alias *server_aliases;
4158 * Add an alias to the static list.
4160 static void push_alias(TALLOC_CTX *mem_ctx, struct full_alias *alias)
4162 if (server_aliases == NULL)
4163 server_aliases = SMB_MALLOC_ARRAY(struct full_alias, 100);
4165 server_aliases[num_server_aliases] = *alias;
4166 num_server_aliases += 1;
4170 * For a specific domain on the server, fetch all the aliases
4171 * and their members. Add all of them to the server_aliases.
4174 static NTSTATUS rpc_fetch_domain_aliases(struct rpc_pipe_client *pipe_hnd,
4175 TALLOC_CTX *mem_ctx,
4176 POLICY_HND *connect_pol,
4177 const DOM_SID *domain_sid)
4179 uint32 start_idx, max_entries, num_entries, i;
4180 struct samr_SamArray *groups = NULL;
4181 NTSTATUS result;
4182 POLICY_HND domain_pol;
4184 /* Get domain policy handle */
4186 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
4187 connect_pol,
4188 MAXIMUM_ALLOWED_ACCESS,
4189 CONST_DISCARD(struct dom_sid2 *, domain_sid),
4190 &domain_pol);
4191 if (!NT_STATUS_IS_OK(result))
4192 return result;
4194 start_idx = 0;
4195 max_entries = 250;
4197 do {
4198 result = rpccli_samr_EnumDomainAliases(pipe_hnd, mem_ctx,
4199 &domain_pol,
4200 &start_idx,
4201 &groups,
4202 max_entries,
4203 &num_entries);
4204 for (i = 0; i < num_entries; i++) {
4206 POLICY_HND alias_pol;
4207 struct full_alias alias;
4208 struct lsa_SidArray sid_array;
4209 int j;
4211 result = rpccli_samr_OpenAlias(pipe_hnd, mem_ctx,
4212 &domain_pol,
4213 MAXIMUM_ALLOWED_ACCESS,
4214 groups->entries[i].idx,
4215 &alias_pol);
4216 if (!NT_STATUS_IS_OK(result))
4217 goto done;
4219 result = rpccli_samr_GetMembersInAlias(pipe_hnd, mem_ctx,
4220 &alias_pol,
4221 &sid_array);
4222 if (!NT_STATUS_IS_OK(result))
4223 goto done;
4225 alias.num_members = sid_array.num_sids;
4227 result = rpccli_samr_Close(pipe_hnd, mem_ctx, &alias_pol);
4228 if (!NT_STATUS_IS_OK(result))
4229 goto done;
4231 alias.members = NULL;
4233 if (alias.num_members > 0) {
4234 alias.members = SMB_MALLOC_ARRAY(DOM_SID, alias.num_members);
4236 for (j = 0; j < alias.num_members; j++)
4237 sid_copy(&alias.members[j],
4238 sid_array.sids[j].sid);
4241 sid_copy(&alias.sid, domain_sid);
4242 sid_append_rid(&alias.sid, groups->entries[i].idx);
4244 push_alias(mem_ctx, &alias);
4246 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
4248 result = NT_STATUS_OK;
4250 done:
4251 rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_pol);
4253 return result;
4257 * Dump server_aliases as names for debugging purposes.
4260 static NTSTATUS rpc_aliaslist_dump(struct net_context *c,
4261 const DOM_SID *domain_sid,
4262 const char *domain_name,
4263 struct cli_state *cli,
4264 struct rpc_pipe_client *pipe_hnd,
4265 TALLOC_CTX *mem_ctx,
4266 int argc,
4267 const char **argv)
4269 int i;
4270 NTSTATUS result;
4271 POLICY_HND lsa_pol;
4273 result = rpccli_lsa_open_policy(pipe_hnd, mem_ctx, true,
4274 SEC_RIGHTS_MAXIMUM_ALLOWED,
4275 &lsa_pol);
4276 if (!NT_STATUS_IS_OK(result))
4277 return result;
4279 for (i=0; i<num_server_aliases; i++) {
4280 char **names;
4281 char **domains;
4282 enum lsa_SidType *types;
4283 int j;
4285 struct full_alias *alias = &server_aliases[i];
4287 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol, 1,
4288 &alias->sid,
4289 &domains, &names, &types);
4290 if (!NT_STATUS_IS_OK(result))
4291 continue;
4293 DEBUG(1, ("%s\\%s %d: ", domains[0], names[0], types[0]));
4295 if (alias->num_members == 0) {
4296 DEBUG(1, ("\n"));
4297 continue;
4300 result = rpccli_lsa_lookup_sids(pipe_hnd, mem_ctx, &lsa_pol,
4301 alias->num_members,
4302 alias->members,
4303 &domains, &names, &types);
4305 if (!NT_STATUS_IS_OK(result) &&
4306 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
4307 continue;
4309 for (j=0; j<alias->num_members; j++)
4310 DEBUG(1, ("%s\\%s (%d); ",
4311 domains[j] ? domains[j] : "*unknown*",
4312 names[j] ? names[j] : "*unknown*",types[j]));
4313 DEBUG(1, ("\n"));
4316 rpccli_lsa_Close(pipe_hnd, mem_ctx, &lsa_pol);
4318 return NT_STATUS_OK;
4322 * Fetch a list of all server aliases and their members into
4323 * server_aliases.
4326 static NTSTATUS rpc_aliaslist_internals(struct net_context *c,
4327 const DOM_SID *domain_sid,
4328 const char *domain_name,
4329 struct cli_state *cli,
4330 struct rpc_pipe_client *pipe_hnd,
4331 TALLOC_CTX *mem_ctx,
4332 int argc,
4333 const char **argv)
4335 NTSTATUS result;
4336 POLICY_HND connect_pol;
4338 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
4339 pipe_hnd->desthost,
4340 MAXIMUM_ALLOWED_ACCESS,
4341 &connect_pol);
4343 if (!NT_STATUS_IS_OK(result))
4344 goto done;
4346 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4347 &global_sid_Builtin);
4349 if (!NT_STATUS_IS_OK(result))
4350 goto done;
4352 result = rpc_fetch_domain_aliases(pipe_hnd, mem_ctx, &connect_pol,
4353 domain_sid);
4355 rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_pol);
4356 done:
4357 return result;
4360 static void init_user_token(NT_USER_TOKEN *token, DOM_SID *user_sid)
4362 token->num_sids = 4;
4364 if (!(token->user_sids = SMB_MALLOC_ARRAY(DOM_SID, 4))) {
4365 d_fprintf(stderr, "malloc failed\n");
4366 token->num_sids = 0;
4367 return;
4370 token->user_sids[0] = *user_sid;
4371 sid_copy(&token->user_sids[1], &global_sid_World);
4372 sid_copy(&token->user_sids[2], &global_sid_Network);
4373 sid_copy(&token->user_sids[3], &global_sid_Authenticated_Users);
4376 static void free_user_token(NT_USER_TOKEN *token)
4378 SAFE_FREE(token->user_sids);
4381 static bool is_sid_in_token(NT_USER_TOKEN *token, DOM_SID *sid)
4383 int i;
4385 for (i=0; i<token->num_sids; i++) {
4386 if (sid_compare(sid, &token->user_sids[i]) == 0)
4387 return true;
4389 return false;
4392 static void add_sid_to_token(NT_USER_TOKEN *token, DOM_SID *sid)
4394 if (is_sid_in_token(token, sid))
4395 return;
4397 token->user_sids = SMB_REALLOC_ARRAY(token->user_sids, DOM_SID, token->num_sids+1);
4398 if (!token->user_sids) {
4399 return;
4402 sid_copy(&token->user_sids[token->num_sids], sid);
4404 token->num_sids += 1;
4407 struct user_token {
4408 fstring name;
4409 NT_USER_TOKEN token;
4412 static void dump_user_token(struct user_token *token)
4414 int i;
4416 d_printf("%s\n", token->name);
4418 for (i=0; i<token->token.num_sids; i++) {
4419 d_printf(" %s\n", sid_string_tos(&token->token.user_sids[i]));
4423 static bool is_alias_member(DOM_SID *sid, struct full_alias *alias)
4425 int i;
4427 for (i=0; i<alias->num_members; i++) {
4428 if (sid_compare(sid, &alias->members[i]) == 0)
4429 return true;
4432 return false;
4435 static void collect_sid_memberships(NT_USER_TOKEN *token, DOM_SID sid)
4437 int i;
4439 for (i=0; i<num_server_aliases; i++) {
4440 if (is_alias_member(&sid, &server_aliases[i]))
4441 add_sid_to_token(token, &server_aliases[i].sid);
4446 * We got a user token with all the SIDs we can know about without asking the
4447 * server directly. These are the user and domain group sids. All of these can
4448 * be members of aliases. So scan the list of aliases for each of the SIDs and
4449 * add them to the token.
4452 static void collect_alias_memberships(NT_USER_TOKEN *token)
4454 int num_global_sids = token->num_sids;
4455 int i;
4457 for (i=0; i<num_global_sids; i++) {
4458 collect_sid_memberships(token, token->user_sids[i]);
4462 static bool get_user_sids(const char *domain, const char *user, NT_USER_TOKEN *token)
4464 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4465 enum wbcSidType type;
4466 fstring full_name;
4467 struct wbcDomainSid wsid;
4468 char *sid_str = NULL;
4469 DOM_SID user_sid;
4470 uint32_t num_groups;
4471 gid_t *groups = NULL;
4472 uint32_t i;
4474 fstr_sprintf(full_name, "%s%c%s",
4475 domain, *lp_winbind_separator(), user);
4477 /* First let's find out the user sid */
4479 wbc_status = wbcLookupName(domain, user, &wsid, &type);
4481 if (!WBC_ERROR_IS_OK(wbc_status)) {
4482 DEBUG(1, ("winbind could not find %s: %s\n",
4483 full_name, wbcErrorString(wbc_status)));
4484 return false;
4487 wbc_status = wbcSidToString(&wsid, &sid_str);
4488 if (!WBC_ERROR_IS_OK(wbc_status)) {
4489 return false;
4492 if (type != SID_NAME_USER) {
4493 wbcFreeMemory(sid_str);
4494 DEBUG(1, ("%s is not a user\n", full_name));
4495 return false;
4498 string_to_sid(&user_sid, sid_str);
4499 wbcFreeMemory(sid_str);
4500 sid_str = NULL;
4502 init_user_token(token, &user_sid);
4504 /* And now the groups winbind knows about */
4506 wbc_status = wbcGetGroups(full_name, &num_groups, &groups);
4507 if (!WBC_ERROR_IS_OK(wbc_status)) {
4508 DEBUG(1, ("winbind could not get groups of %s: %s\n",
4509 full_name, wbcErrorString(wbc_status)));
4510 return false;
4513 for (i = 0; i < num_groups; i++) {
4514 gid_t gid = groups[i];
4515 DOM_SID sid;
4517 wbc_status = wbcGidToSid(gid, &wsid);
4518 if (!WBC_ERROR_IS_OK(wbc_status)) {
4519 DEBUG(1, ("winbind could not find SID of gid %d: %s\n",
4520 gid, wbcErrorString(wbc_status)));
4521 wbcFreeMemory(groups);
4522 return false;
4525 wbc_status = wbcSidToString(&wsid, &sid_str);
4526 if (!WBC_ERROR_IS_OK(wbc_status)) {
4527 wbcFreeMemory(groups);
4528 return false;
4531 DEBUG(3, (" %s\n", sid_str));
4533 string_to_sid(&sid, sid_str);
4534 wbcFreeMemory(sid_str);
4535 sid_str = NULL;
4537 add_sid_to_token(token, &sid);
4539 wbcFreeMemory(groups);
4541 return true;
4545 * Get a list of all user tokens we want to look at
4548 static bool get_user_tokens(struct net_context *c, int *num_tokens,
4549 struct user_token **user_tokens)
4551 wbcErr wbc_status = WBC_ERR_UNKNOWN_FAILURE;
4552 uint32_t i, num_users;
4553 const char **users;
4554 struct user_token *result;
4555 TALLOC_CTX *frame = NULL;
4557 if (lp_winbind_use_default_domain() &&
4558 (c->opt_target_workgroup == NULL)) {
4559 d_fprintf(stderr, "winbind use default domain = yes set, "
4560 "please specify a workgroup\n");
4561 return false;
4564 /* Send request to winbind daemon */
4566 wbc_status = wbcListUsers(NULL, &num_users, &users);
4567 if (!WBC_ERROR_IS_OK(wbc_status)) {
4568 DEBUG(1, ("winbind could not list users: %s\n",
4569 wbcErrorString(wbc_status)));
4570 return false;
4573 result = SMB_MALLOC_ARRAY(struct user_token, num_users);
4575 if (result == NULL) {
4576 DEBUG(1, ("Could not malloc sid array\n"));
4577 wbcFreeMemory(users);
4578 return false;
4581 frame = talloc_stackframe();
4582 for (i=0; i < num_users; i++) {
4583 fstring domain, user;
4584 char *p;
4586 fstrcpy(result[i].name, users[i]);
4588 p = strchr(users[i], *lp_winbind_separator());
4590 DEBUG(3, ("%s\n", users[i]));
4592 if (p == NULL) {
4593 fstrcpy(domain, c->opt_target_workgroup);
4594 fstrcpy(user, users[i]);
4595 } else {
4596 *p++ = '\0';
4597 fstrcpy(domain, users[i]);
4598 strupper_m(domain);
4599 fstrcpy(user, p);
4602 get_user_sids(domain, user, &(result[i].token));
4603 i+=1;
4605 TALLOC_FREE(frame);
4606 wbcFreeMemory(users);
4608 *num_tokens = num_users;
4609 *user_tokens = result;
4611 return true;
4614 static bool get_user_tokens_from_file(FILE *f,
4615 int *num_tokens,
4616 struct user_token **tokens)
4618 struct user_token *token = NULL;
4620 while (!feof(f)) {
4621 fstring line;
4623 if (fgets(line, sizeof(line)-1, f) == NULL) {
4624 return true;
4627 if (line[strlen(line)-1] == '\n')
4628 line[strlen(line)-1] = '\0';
4630 if (line[0] == ' ') {
4631 /* We have a SID */
4633 DOM_SID sid;
4634 string_to_sid(&sid, &line[1]);
4636 if (token == NULL) {
4637 DEBUG(0, ("File does not begin with username"));
4638 return false;
4641 add_sid_to_token(&token->token, &sid);
4642 continue;
4645 /* And a new user... */
4647 *num_tokens += 1;
4648 *tokens = SMB_REALLOC_ARRAY(*tokens, struct user_token, *num_tokens);
4649 if (*tokens == NULL) {
4650 DEBUG(0, ("Could not realloc tokens\n"));
4651 return false;
4654 token = &((*tokens)[*num_tokens-1]);
4656 fstrcpy(token->name, line);
4657 token->token.num_sids = 0;
4658 token->token.user_sids = NULL;
4659 continue;
4662 return false;
4667 * Show the list of all users that have access to a share
4670 static void show_userlist(struct rpc_pipe_client *pipe_hnd,
4671 TALLOC_CTX *mem_ctx,
4672 const char *netname,
4673 int num_tokens,
4674 struct user_token *tokens)
4676 int fnum;
4677 SEC_DESC *share_sd = NULL;
4678 SEC_DESC *root_sd = NULL;
4679 struct cli_state *cli = rpc_pipe_np_smb_conn(pipe_hnd);
4680 int i;
4681 union srvsvc_NetShareInfo info;
4682 WERROR result;
4683 NTSTATUS status;
4684 uint16 cnum;
4686 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
4687 pipe_hnd->desthost,
4688 netname,
4689 502,
4690 &info,
4691 &result);
4693 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
4694 DEBUG(1, ("Coult not query secdesc for share %s\n",
4695 netname));
4696 return;
4699 share_sd = info.info502->sd_buf.sd;
4700 if (share_sd == NULL) {
4701 DEBUG(1, ("Got no secdesc for share %s\n",
4702 netname));
4705 cnum = cli->cnum;
4707 if (!cli_send_tconX(cli, netname, "A:", "", 0)) {
4708 return;
4711 fnum = cli_nt_create(cli, "\\", READ_CONTROL_ACCESS);
4713 if (fnum != -1) {
4714 root_sd = cli_query_secdesc(cli, fnum, mem_ctx);
4717 for (i=0; i<num_tokens; i++) {
4718 uint32 acc_granted;
4720 if (share_sd != NULL) {
4721 if (!se_access_check(share_sd, &tokens[i].token,
4722 1, &acc_granted, &status)) {
4723 DEBUG(1, ("Could not check share_sd for "
4724 "user %s\n",
4725 tokens[i].name));
4726 continue;
4729 if (!NT_STATUS_IS_OK(status))
4730 continue;
4733 if (root_sd == NULL) {
4734 d_printf(" %s\n", tokens[i].name);
4735 continue;
4738 if (!se_access_check(root_sd, &tokens[i].token,
4739 1, &acc_granted, &status)) {
4740 DEBUG(1, ("Could not check root_sd for user %s\n",
4741 tokens[i].name));
4742 continue;
4745 if (!NT_STATUS_IS_OK(status))
4746 continue;
4748 d_printf(" %s\n", tokens[i].name);
4751 if (fnum != -1)
4752 cli_close(cli, fnum);
4753 cli_tdis(cli);
4754 cli->cnum = cnum;
4756 return;
4759 struct share_list {
4760 int num_shares;
4761 char **shares;
4764 static void collect_share(const char *name, uint32 m,
4765 const char *comment, void *state)
4767 struct share_list *share_list = (struct share_list *)state;
4769 if (m != STYPE_DISKTREE)
4770 return;
4772 share_list->num_shares += 1;
4773 share_list->shares = SMB_REALLOC_ARRAY(share_list->shares, char *, share_list->num_shares);
4774 if (!share_list->shares) {
4775 share_list->num_shares = 0;
4776 return;
4778 share_list->shares[share_list->num_shares-1] = SMB_STRDUP(name);
4782 * List shares on a remote RPC server, including the security descriptors.
4784 * All parameters are provided by the run_rpc_command function, except for
4785 * argc, argv which are passed through.
4787 * @param domain_sid The domain sid acquired from the remote server.
4788 * @param cli A cli_state connected to the server.
4789 * @param mem_ctx Talloc context, destroyed on completion of the function.
4790 * @param argc Standard main() style argc.
4791 * @param argv Standard main() style argv. Initial components are already
4792 * stripped.
4794 * @return Normal NTSTATUS return.
4797 static NTSTATUS rpc_share_allowedusers_internals(struct net_context *c,
4798 const DOM_SID *domain_sid,
4799 const char *domain_name,
4800 struct cli_state *cli,
4801 struct rpc_pipe_client *pipe_hnd,
4802 TALLOC_CTX *mem_ctx,
4803 int argc,
4804 const char **argv)
4806 int ret;
4807 bool r;
4808 ENUM_HND hnd;
4809 uint32 i;
4810 FILE *f;
4812 struct user_token *tokens = NULL;
4813 int num_tokens = 0;
4815 struct share_list share_list;
4817 if (argc == 0) {
4818 f = stdin;
4819 } else {
4820 f = fopen(argv[0], "r");
4823 if (f == NULL) {
4824 DEBUG(0, ("Could not open userlist: %s\n", strerror(errno)));
4825 return NT_STATUS_UNSUCCESSFUL;
4828 r = get_user_tokens_from_file(f, &num_tokens, &tokens);
4830 if (f != stdin)
4831 fclose(f);
4833 if (!r) {
4834 DEBUG(0, ("Could not read users from file\n"));
4835 return NT_STATUS_UNSUCCESSFUL;
4838 for (i=0; i<num_tokens; i++)
4839 collect_alias_memberships(&tokens[i].token);
4841 init_enum_hnd(&hnd, 0);
4843 share_list.num_shares = 0;
4844 share_list.shares = NULL;
4846 ret = cli_RNetShareEnum(cli, collect_share, &share_list);
4848 if (ret == -1) {
4849 DEBUG(0, ("Error returning browse list: %s\n",
4850 cli_errstr(cli)));
4851 goto done;
4854 for (i = 0; i < share_list.num_shares; i++) {
4855 char *netname = share_list.shares[i];
4857 if (netname[strlen(netname)-1] == '$')
4858 continue;
4860 d_printf("%s\n", netname);
4862 show_userlist(pipe_hnd, mem_ctx, netname,
4863 num_tokens, tokens);
4865 done:
4866 for (i=0; i<num_tokens; i++) {
4867 free_user_token(&tokens[i].token);
4869 SAFE_FREE(tokens);
4870 SAFE_FREE(share_list.shares);
4872 return NT_STATUS_OK;
4875 static int rpc_share_allowedusers(struct net_context *c, int argc,
4876 const char **argv)
4878 int result;
4880 if (c->display_usage) {
4881 d_printf("Usage:\n"
4882 "net rpc share allowedusers\n"
4883 " List allowed users\n");
4884 return 0;
4887 result = run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
4888 rpc_aliaslist_internals,
4889 argc, argv);
4890 if (result != 0)
4891 return result;
4893 result = run_rpc_command(c, NULL, &ndr_table_lsarpc.syntax_id, 0,
4894 rpc_aliaslist_dump,
4895 argc, argv);
4896 if (result != 0)
4897 return result;
4899 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
4900 rpc_share_allowedusers_internals,
4901 argc, argv);
4904 int net_usersidlist(struct net_context *c, int argc, const char **argv)
4906 int num_tokens = 0;
4907 struct user_token *tokens = NULL;
4908 int i;
4910 if (argc != 0) {
4911 net_usersidlist_usage(c, argc, argv);
4912 return 0;
4915 if (!get_user_tokens(c, &num_tokens, &tokens)) {
4916 DEBUG(0, ("Could not get the user/sid list\n"));
4917 return 0;
4920 for (i=0; i<num_tokens; i++) {
4921 dump_user_token(&tokens[i]);
4922 free_user_token(&tokens[i].token);
4925 SAFE_FREE(tokens);
4926 return 1;
4929 int net_usersidlist_usage(struct net_context *c, int argc, const char **argv)
4931 d_printf("net usersidlist\n"
4932 "\tprints out a list of all users the running winbind knows\n"
4933 "\tabout, together with all their SIDs. This is used as\n"
4934 "\tinput to the 'net rpc share allowedusers' command.\n\n");
4936 net_common_flags_usage(c, argc, argv);
4937 return -1;
4941 * 'net rpc share' entrypoint.
4942 * @param argc Standard main() style argc.
4943 * @param argv Standard main() style argv. Initial components are already
4944 * stripped.
4947 int net_rpc_share(struct net_context *c, int argc, const char **argv)
4949 struct functable func[] = {
4951 "add",
4952 rpc_share_add,
4953 NET_TRANSPORT_RPC,
4954 "Add share",
4955 "net rpc share add\n"
4956 " Add share"
4959 "delete",
4960 rpc_share_delete,
4961 NET_TRANSPORT_RPC,
4962 "Remove share",
4963 "net rpc share delete\n"
4964 " Remove share"
4967 "allowedusers",
4968 rpc_share_allowedusers,
4969 NET_TRANSPORT_RPC,
4970 "Modify allowed users",
4971 "net rpc share allowedusers\n"
4972 " Modify allowed users"
4975 "migrate",
4976 rpc_share_migrate,
4977 NET_TRANSPORT_RPC,
4978 "Migrate share to local server",
4979 "net rpc share migrate\n"
4980 " Migrate share to local server"
4983 "list",
4984 rpc_share_list,
4985 NET_TRANSPORT_RPC,
4986 "List shares",
4987 "net rpc share list\n"
4988 " List shares"
4990 {NULL, NULL, 0, NULL, NULL}
4994 if (argc == 0) {
4995 if (c->display_usage) {
4996 d_printf("Usage:\n"
4997 "net rpc share\n"
4998 " List shares\n"
4999 " Alias for net rpc share list\n");
5000 net_display_usage_from_functable(func);
5001 return 0;
5004 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
5005 rpc_share_list_internals,
5006 argc, argv);
5009 return net_run_function(c, argc, argv, "net rpc share", func);
5012 static NTSTATUS rpc_sh_share_list(struct net_context *c,
5013 TALLOC_CTX *mem_ctx,
5014 struct rpc_sh_ctx *ctx,
5015 struct rpc_pipe_client *pipe_hnd,
5016 int argc, const char **argv)
5018 return rpc_share_list_internals(c, ctx->domain_sid, ctx->domain_name,
5019 ctx->cli, pipe_hnd, mem_ctx,
5020 argc, argv);
5023 static NTSTATUS rpc_sh_share_add(struct net_context *c,
5024 TALLOC_CTX *mem_ctx,
5025 struct rpc_sh_ctx *ctx,
5026 struct rpc_pipe_client *pipe_hnd,
5027 int argc, const char **argv)
5029 WERROR result;
5030 NTSTATUS status;
5031 uint32_t parm_err = 0;
5032 union srvsvc_NetShareInfo info;
5033 struct srvsvc_NetShareInfo2 info2;
5035 if ((argc < 2) || (argc > 3)) {
5036 d_fprintf(stderr, "usage: %s <share> <path> [comment]\n",
5037 ctx->whoami);
5038 return NT_STATUS_INVALID_PARAMETER;
5041 info2.name = argv[0];
5042 info2.type = STYPE_DISKTREE;
5043 info2.comment = (argc == 3) ? argv[2] : "";
5044 info2.permissions = 0;
5045 info2.max_users = 0;
5046 info2.current_users = 0;
5047 info2.path = argv[1];
5048 info2.password = NULL;
5050 info.info2 = &info2;
5052 status = rpccli_srvsvc_NetShareAdd(pipe_hnd, mem_ctx,
5053 pipe_hnd->desthost,
5055 &info,
5056 &parm_err,
5057 &result);
5059 return status;
5062 static NTSTATUS rpc_sh_share_delete(struct net_context *c,
5063 TALLOC_CTX *mem_ctx,
5064 struct rpc_sh_ctx *ctx,
5065 struct rpc_pipe_client *pipe_hnd,
5066 int argc, const char **argv)
5068 WERROR result;
5069 NTSTATUS status;
5071 if (argc != 1) {
5072 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
5073 return NT_STATUS_INVALID_PARAMETER;
5076 status = rpccli_srvsvc_NetShareDel(pipe_hnd, mem_ctx,
5077 pipe_hnd->desthost,
5078 argv[0],
5080 &result);
5082 return status;
5085 static NTSTATUS rpc_sh_share_info(struct net_context *c,
5086 TALLOC_CTX *mem_ctx,
5087 struct rpc_sh_ctx *ctx,
5088 struct rpc_pipe_client *pipe_hnd,
5089 int argc, const char **argv)
5091 union srvsvc_NetShareInfo info;
5092 WERROR result;
5093 NTSTATUS status;
5095 if (argc != 1) {
5096 d_fprintf(stderr, "usage: %s <share>\n", ctx->whoami);
5097 return NT_STATUS_INVALID_PARAMETER;
5100 status = rpccli_srvsvc_NetShareGetInfo(pipe_hnd, mem_ctx,
5101 pipe_hnd->desthost,
5102 argv[0],
5104 &info,
5105 &result);
5106 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result)) {
5107 goto done;
5110 d_printf("Name: %s\n", info.info2->name);
5111 d_printf("Comment: %s\n", info.info2->comment);
5112 d_printf("Path: %s\n", info.info2->path);
5113 d_printf("Password: %s\n", info.info2->password);
5115 done:
5116 return werror_to_ntstatus(result);
5119 struct rpc_sh_cmd *net_rpc_share_cmds(struct net_context *c, TALLOC_CTX *mem_ctx,
5120 struct rpc_sh_ctx *ctx)
5122 static struct rpc_sh_cmd cmds[] = {
5124 { "list", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_list,
5125 "List available shares" },
5127 { "add", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_add,
5128 "Add a share" },
5130 { "delete", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_delete,
5131 "Delete a share" },
5133 { "info", NULL, &ndr_table_srvsvc.syntax_id, rpc_sh_share_info,
5134 "Get information about a share" },
5136 { NULL, NULL, 0, NULL, NULL }
5139 return cmds;
5142 /****************************************************************************/
5144 static int rpc_file_usage(struct net_context *c, int argc, const char **argv)
5146 return net_file_usage(c, argc, argv);
5150 * Close a file on a remote RPC server.
5152 * All parameters are provided by the run_rpc_command function, except for
5153 * argc, argv which are passed through.
5155 * @param c A net_context structure.
5156 * @param domain_sid The domain sid acquired from the remote server.
5157 * @param cli A cli_state connected to the server.
5158 * @param mem_ctx Talloc context, destroyed on completion of the function.
5159 * @param argc Standard main() style argc.
5160 * @param argv Standard main() style argv. Initial components are already
5161 * stripped.
5163 * @return Normal NTSTATUS return.
5165 static NTSTATUS rpc_file_close_internals(struct net_context *c,
5166 const DOM_SID *domain_sid,
5167 const char *domain_name,
5168 struct cli_state *cli,
5169 struct rpc_pipe_client *pipe_hnd,
5170 TALLOC_CTX *mem_ctx,
5171 int argc,
5172 const char **argv)
5174 return rpccli_srvsvc_NetFileClose(pipe_hnd, mem_ctx,
5175 pipe_hnd->desthost,
5176 atoi(argv[0]), NULL);
5180 * Close a file on a remote RPC server.
5182 * @param argc Standard main() style argc.
5183 * @param argv Standard main() style argv. Initial components are already
5184 * stripped.
5186 * @return A shell status integer (0 for success).
5188 static int rpc_file_close(struct net_context *c, int argc, const char **argv)
5190 if (argc < 1 || c->display_usage) {
5191 return rpc_file_usage(c, argc, argv);
5194 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
5195 rpc_file_close_internals,
5196 argc, argv);
5200 * Formatted print of open file info
5202 * @param r struct srvsvc_NetFileInfo3 contents
5205 static void display_file_info_3(struct srvsvc_NetFileInfo3 *r)
5207 d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
5208 r->fid, r->user, r->permissions, r->num_locks, r->path);
5212 * List open files on a remote RPC server.
5214 * All parameters are provided by the run_rpc_command function, except for
5215 * argc, argv which are passed through.
5217 * @param c A net_context structure.
5218 * @param domain_sid The domain sid acquired from the remote server.
5219 * @param cli A cli_state connected to the server.
5220 * @param mem_ctx Talloc context, destroyed on completion of the function.
5221 * @param argc Standard main() style argc.
5222 * @param argv Standard main() style argv. Initial components are already
5223 * stripped.
5225 * @return Normal NTSTATUS return.
5228 static NTSTATUS rpc_file_list_internals(struct net_context *c,
5229 const DOM_SID *domain_sid,
5230 const char *domain_name,
5231 struct cli_state *cli,
5232 struct rpc_pipe_client *pipe_hnd,
5233 TALLOC_CTX *mem_ctx,
5234 int argc,
5235 const char **argv)
5237 struct srvsvc_NetFileInfoCtr info_ctr;
5238 struct srvsvc_NetFileCtr3 ctr3;
5239 WERROR result;
5240 NTSTATUS status;
5241 uint32 preferred_len = 0xffffffff, i;
5242 const char *username=NULL;
5243 uint32_t total_entries = 0;
5244 uint32_t resume_handle = 0;
5246 /* if argc > 0, must be user command */
5247 if (argc > 0)
5248 username = smb_xstrdup(argv[0]);
5250 ZERO_STRUCT(info_ctr);
5251 ZERO_STRUCT(ctr3);
5253 info_ctr.level = 3;
5254 info_ctr.ctr.ctr3 = &ctr3;
5256 status = rpccli_srvsvc_NetFileEnum(pipe_hnd, mem_ctx,
5257 pipe_hnd->desthost,
5258 NULL,
5259 username,
5260 &info_ctr,
5261 preferred_len,
5262 &total_entries,
5263 &resume_handle,
5264 &result);
5266 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(result))
5267 goto done;
5269 /* Display results */
5271 d_printf(
5272 "\nEnumerating open files on remote server:\n\n"
5273 "\nFileId Opened by Perms Locks Path"
5274 "\n------ --------- ----- ----- ---- \n");
5275 for (i = 0; i < total_entries; i++)
5276 display_file_info_3(&info_ctr.ctr.ctr3->array[i]);
5277 done:
5278 return W_ERROR_IS_OK(result) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
5282 * List files for a user on a remote RPC server.
5284 * @param argc Standard main() style argc.
5285 * @param argv Standard main() style argv. Initial components are already
5286 * stripped.
5288 * @return A shell status integer (0 for success)..
5291 static int rpc_file_user(struct net_context *c, int argc, const char **argv)
5293 if (argc < 1 || c->display_usage) {
5294 return rpc_file_usage(c, argc, argv);
5297 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
5298 rpc_file_list_internals,
5299 argc, argv);
5303 * 'net rpc file' entrypoint.
5304 * @param argc Standard main() style argc.
5305 * @param argv Standard main() style argv. Initial components are already
5306 * stripped.
5309 int net_rpc_file(struct net_context *c, int argc, const char **argv)
5311 struct functable func[] = {
5313 "close",
5314 rpc_file_close,
5315 NET_TRANSPORT_RPC,
5316 "Close opened file",
5317 "net rpc file close\n"
5318 " Close opened file"
5321 "user",
5322 rpc_file_user,
5323 NET_TRANSPORT_RPC,
5324 "List files opened by user",
5325 "net rpc file user\n"
5326 " List files opened by user"
5328 #if 0
5330 "info",
5331 rpc_file_info,
5332 NET_TRANSPORT_RPC,
5333 "Display information about opened file",
5334 "net rpc file info\n"
5335 " Display information about opened file"
5337 #endif
5338 {NULL, NULL, 0, NULL, NULL}
5341 if (argc == 0) {
5342 if (c->display_usage) {
5343 d_printf("Usage:\n");
5344 d_printf("net rpc file\n"
5345 " List opened files\n");
5346 net_display_usage_from_functable(func);
5347 return 0;
5350 return run_rpc_command(c, NULL, &ndr_table_srvsvc.syntax_id, 0,
5351 rpc_file_list_internals,
5352 argc, argv);
5355 return net_run_function(c, argc, argv, "net rpc file", func);
5359 * ABORT the shutdown of a remote RPC Server, over initshutdown pipe.
5361 * All parameters are provided by the run_rpc_command function, except for
5362 * argc, argv which are passed through.
5364 * @param c A net_context structure.
5365 * @param domain_sid The domain sid acquired from the remote server.
5366 * @param cli A cli_state connected to the server.
5367 * @param mem_ctx Talloc context, destroyed on completion of the function.
5368 * @param argc Standard main() style argc.
5369 * @param argv Standard main() style argv. Initial components are already
5370 * stripped.
5372 * @return Normal NTSTATUS return.
5375 static NTSTATUS rpc_shutdown_abort_internals(struct net_context *c,
5376 const DOM_SID *domain_sid,
5377 const char *domain_name,
5378 struct cli_state *cli,
5379 struct rpc_pipe_client *pipe_hnd,
5380 TALLOC_CTX *mem_ctx,
5381 int argc,
5382 const char **argv)
5384 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5386 result = rpccli_initshutdown_Abort(pipe_hnd, mem_ctx, NULL, NULL);
5388 if (NT_STATUS_IS_OK(result)) {
5389 d_printf("\nShutdown successfully aborted\n");
5390 DEBUG(5,("cmd_shutdown_abort: query succeeded\n"));
5391 } else
5392 DEBUG(5,("cmd_shutdown_abort: query failed\n"));
5394 return result;
5398 * ABORT the shutdown of a remote RPC Server, over winreg pipe.
5400 * All parameters are provided by the run_rpc_command function, except for
5401 * argc, argv which are passed through.
5403 * @param c A net_context structure.
5404 * @param domain_sid The domain sid acquired from the remote server.
5405 * @param cli A cli_state connected to the server.
5406 * @param mem_ctx Talloc context, destroyed on completion of the function.
5407 * @param argc Standard main() style argc.
5408 * @param argv Standard main() style argv. Initial components are already
5409 * stripped.
5411 * @return Normal NTSTATUS return.
5414 static NTSTATUS rpc_reg_shutdown_abort_internals(struct net_context *c,
5415 const DOM_SID *domain_sid,
5416 const char *domain_name,
5417 struct cli_state *cli,
5418 struct rpc_pipe_client *pipe_hnd,
5419 TALLOC_CTX *mem_ctx,
5420 int argc,
5421 const char **argv)
5423 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5425 result = rpccli_winreg_AbortSystemShutdown(pipe_hnd, mem_ctx, NULL, NULL);
5427 if (NT_STATUS_IS_OK(result)) {
5428 d_printf("\nShutdown successfully aborted\n");
5429 DEBUG(5,("cmd_reg_abort_shutdown: query succeeded\n"));
5430 } else
5431 DEBUG(5,("cmd_reg_abort_shutdown: query failed\n"));
5433 return result;
5437 * ABORT the shutdown of a remote RPC server.
5439 * @param argc Standard main() style argc.
5440 * @param argv Standard main() style argv. Initial components are already
5441 * stripped.
5443 * @return A shell status integer (0 for success).
5446 static int rpc_shutdown_abort(struct net_context *c, int argc,
5447 const char **argv)
5449 int rc = -1;
5451 if (c->display_usage) {
5452 d_printf("Usage:\n"
5453 "net rpc abortshutdown\n"
5454 " Abort a scheduled shutdown\n");
5455 return 0;
5458 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5459 rpc_shutdown_abort_internals, argc, argv);
5461 if (rc == 0)
5462 return rc;
5464 DEBUG(1, ("initshutdown pipe didn't work, trying winreg pipe\n"));
5466 return run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5467 rpc_reg_shutdown_abort_internals,
5468 argc, argv);
5472 * Shut down a remote RPC Server via initshutdown pipe.
5474 * All parameters are provided by the run_rpc_command function, except for
5475 * argc, argv which are passed through.
5477 * @param c A net_context structure.
5478 * @param domain_sid The domain sid acquired from the remote server.
5479 * @param cli A cli_state connected to the server.
5480 * @param mem_ctx Talloc context, destroyed on completion of the function.
5481 * @param argc Standard main() style argc.
5482 * @param argv Standard main() style argv. Initial components are already
5483 * stripped.
5485 * @return Normal NTSTATUS return.
5488 NTSTATUS rpc_init_shutdown_internals(struct net_context *c,
5489 const DOM_SID *domain_sid,
5490 const char *domain_name,
5491 struct cli_state *cli,
5492 struct rpc_pipe_client *pipe_hnd,
5493 TALLOC_CTX *mem_ctx,
5494 int argc,
5495 const char **argv)
5497 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5498 const char *msg = "This machine will be shutdown shortly";
5499 uint32 timeout = 20;
5500 struct initshutdown_String msg_string;
5501 struct initshutdown_String_sub s;
5503 if (c->opt_comment) {
5504 msg = c->opt_comment;
5506 if (c->opt_timeout) {
5507 timeout = c->opt_timeout;
5510 s.name = msg;
5511 msg_string.name = &s;
5513 /* create an entry */
5514 result = rpccli_initshutdown_Init(pipe_hnd, mem_ctx, NULL,
5515 &msg_string, timeout, c->opt_force, c->opt_reboot,
5516 NULL);
5518 if (NT_STATUS_IS_OK(result)) {
5519 d_printf("\nShutdown of remote machine succeeded\n");
5520 DEBUG(5,("Shutdown of remote machine succeeded\n"));
5521 } else {
5522 DEBUG(1,("Shutdown of remote machine failed!\n"));
5524 return result;
5528 * Shut down a remote RPC Server via winreg pipe.
5530 * All parameters are provided by the run_rpc_command function, except for
5531 * argc, argv which are passed through.
5533 * @param c A net_context structure.
5534 * @param domain_sid The domain sid acquired from the remote server.
5535 * @param cli A cli_state connected to the server.
5536 * @param mem_ctx Talloc context, destroyed on completion of the function.
5537 * @param argc Standard main() style argc.
5538 * @param argv Standard main() style argv. Initial components are already
5539 * stripped.
5541 * @return Normal NTSTATUS return.
5544 NTSTATUS rpc_reg_shutdown_internals(struct net_context *c,
5545 const DOM_SID *domain_sid,
5546 const char *domain_name,
5547 struct cli_state *cli,
5548 struct rpc_pipe_client *pipe_hnd,
5549 TALLOC_CTX *mem_ctx,
5550 int argc,
5551 const char **argv)
5553 const char *msg = "This machine will be shutdown shortly";
5554 uint32 timeout = 20;
5555 struct initshutdown_String msg_string;
5556 struct initshutdown_String_sub s;
5557 NTSTATUS result;
5558 WERROR werr;
5560 if (c->opt_comment) {
5561 msg = c->opt_comment;
5563 s.name = msg;
5564 msg_string.name = &s;
5566 if (c->opt_timeout) {
5567 timeout = c->opt_timeout;
5570 /* create an entry */
5571 result = rpccli_winreg_InitiateSystemShutdown(pipe_hnd, mem_ctx, NULL,
5572 &msg_string, timeout, c->opt_force, c->opt_reboot,
5573 &werr);
5575 if (NT_STATUS_IS_OK(result)) {
5576 d_printf("\nShutdown of remote machine succeeded\n");
5577 } else {
5578 d_fprintf(stderr, "\nShutdown of remote machine failed\n");
5579 if ( W_ERROR_EQUAL(werr, WERR_MACHINE_LOCKED) )
5580 d_fprintf(stderr, "\nMachine locked, use -f switch to force\n");
5581 else
5582 d_fprintf(stderr, "\nresult was: %s\n", dos_errstr(werr));
5585 return result;
5589 * Shut down a remote RPC server.
5591 * @param argc Standard main() style argc.
5592 * @param argv Standard main() style argv. Initial components are already
5593 * stripped.
5595 * @return A shell status integer (0 for success).
5598 static int rpc_shutdown(struct net_context *c, int argc, const char **argv)
5600 int rc = -1;
5602 if (c->display_usage) {
5603 d_printf("Usage:\n"
5604 "net rpc shutdown\n"
5605 " Shut down a remote RPC server\n");
5606 return 0;
5609 rc = run_rpc_command(c, NULL, &ndr_table_initshutdown.syntax_id, 0,
5610 rpc_init_shutdown_internals, argc, argv);
5612 if (rc) {
5613 DEBUG(1, ("initshutdown pipe failed, trying winreg pipe\n"));
5614 rc = run_rpc_command(c, NULL, &ndr_table_winreg.syntax_id, 0,
5615 rpc_reg_shutdown_internals, argc, argv);
5618 return rc;
5621 /***************************************************************************
5622 NT Domain trusts code (i.e. 'net rpc trustdom' functionality)
5623 ***************************************************************************/
5626 * Add interdomain trust account to the RPC server.
5627 * All parameters (except for argc and argv) are passed by run_rpc_command
5628 * function.
5630 * @param c A net_context structure.
5631 * @param domain_sid The domain sid acquired from the server.
5632 * @param cli A cli_state connected to the server.
5633 * @param mem_ctx Talloc context, destroyed on completion of the function.
5634 * @param argc Standard main() style argc.
5635 * @param argv Standard main() style argv. Initial components are already
5636 * stripped.
5638 * @return normal NTSTATUS return code.
5641 static NTSTATUS rpc_trustdom_add_internals(struct net_context *c,
5642 const DOM_SID *domain_sid,
5643 const char *domain_name,
5644 struct cli_state *cli,
5645 struct rpc_pipe_client *pipe_hnd,
5646 TALLOC_CTX *mem_ctx,
5647 int argc,
5648 const char **argv)
5650 POLICY_HND connect_pol, domain_pol, user_pol;
5651 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5652 char *acct_name;
5653 struct lsa_String lsa_acct_name;
5654 uint32 acb_info;
5655 uint32 acct_flags=0;
5656 uint32 user_rid;
5657 uint32_t access_granted = 0;
5658 union samr_UserInfo info;
5659 unsigned int orig_timeout;
5661 if (argc != 2) {
5662 d_printf("Usage: net rpc trustdom add <domain_name> "
5663 "<trust password>\n");
5664 return NT_STATUS_INVALID_PARAMETER;
5668 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5671 if (asprintf(&acct_name, "%s$", argv[0]) < 0) {
5672 return NT_STATUS_NO_MEMORY;
5675 strupper_m(acct_name);
5677 init_lsa_String(&lsa_acct_name, acct_name);
5679 /* Get samr policy handle */
5680 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5681 pipe_hnd->desthost,
5682 MAXIMUM_ALLOWED_ACCESS,
5683 &connect_pol);
5684 if (!NT_STATUS_IS_OK(result)) {
5685 goto done;
5688 /* Get domain policy handle */
5689 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5690 &connect_pol,
5691 MAXIMUM_ALLOWED_ACCESS,
5692 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5693 &domain_pol);
5694 if (!NT_STATUS_IS_OK(result)) {
5695 goto done;
5698 /* This call can take a long time - allow the server to time out.
5699 * 35 seconds should do it. */
5701 orig_timeout = rpccli_set_timeout(pipe_hnd, 35000);
5703 /* Create trusting domain's account */
5704 acb_info = ACB_NORMAL;
5705 acct_flags = SEC_GENERIC_READ | SEC_GENERIC_WRITE | SEC_GENERIC_EXECUTE |
5706 SEC_STD_WRITE_DAC | SEC_STD_DELETE |
5707 SAMR_USER_ACCESS_SET_PASSWORD |
5708 SAMR_USER_ACCESS_GET_ATTRIBUTES |
5709 SAMR_USER_ACCESS_SET_ATTRIBUTES;
5711 result = rpccli_samr_CreateUser2(pipe_hnd, mem_ctx,
5712 &domain_pol,
5713 &lsa_acct_name,
5714 acb_info,
5715 acct_flags,
5716 &user_pol,
5717 &access_granted,
5718 &user_rid);
5720 /* And restore our original timeout. */
5721 rpccli_set_timeout(pipe_hnd, orig_timeout);
5723 if (!NT_STATUS_IS_OK(result)) {
5724 d_printf("net rpc trustdom add: create user %s failed %s\n",
5725 acct_name, nt_errstr(result));
5726 goto done;
5730 NTTIME notime;
5731 struct samr_LogonHours hours;
5732 struct lsa_BinaryString parameters;
5733 const int units_per_week = 168;
5734 uchar pwbuf[516];
5736 encode_pw_buffer(pwbuf, argv[1], STR_UNICODE);
5738 ZERO_STRUCT(notime);
5739 ZERO_STRUCT(hours);
5740 ZERO_STRUCT(parameters);
5742 hours.bits = talloc_array(mem_ctx, uint8_t, units_per_week);
5743 if (!hours.bits) {
5744 result = NT_STATUS_NO_MEMORY;
5745 goto done;
5747 hours.units_per_week = units_per_week;
5748 memset(hours.bits, 0xFF, units_per_week);
5750 init_samr_user_info23(&info.info23,
5751 notime, notime, notime,
5752 notime, notime, notime,
5753 NULL, NULL, NULL, NULL, NULL,
5754 NULL, NULL, NULL, NULL, &parameters,
5755 0, 0, ACB_DOMTRUST, SAMR_FIELD_ACCT_FLAGS,
5756 hours,
5757 0, 0, 0, 0, 0, 0, 0,
5758 pwbuf, 24);
5760 SamOEMhashBlob(info.info23.password.data, 516,
5761 &cli->user_session_key);
5763 result = rpccli_samr_SetUserInfo2(pipe_hnd, mem_ctx,
5764 &user_pol,
5766 &info);
5768 if (!NT_STATUS_IS_OK(result)) {
5769 DEBUG(0,("Could not set trust account password: %s\n",
5770 nt_errstr(result)));
5771 goto done;
5775 done:
5776 SAFE_FREE(acct_name);
5777 return result;
5781 * Create interdomain trust account for a remote domain.
5783 * @param argc Standard argc.
5784 * @param argv Standard argv without initial components.
5786 * @return Integer status (0 means success).
5789 static int rpc_trustdom_add(struct net_context *c, int argc, const char **argv)
5791 if (argc > 0 && !c->display_usage) {
5792 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5793 rpc_trustdom_add_internals, argc, argv);
5794 } else {
5795 d_printf("Usage:\n"
5796 "net rpc trustdom add <domain_name> <trust password>\n");
5797 return -1;
5803 * Remove interdomain trust account from the RPC server.
5804 * All parameters (except for argc and argv) are passed by run_rpc_command
5805 * function.
5807 * @param c A net_context structure.
5808 * @param domain_sid The domain sid acquired from the server.
5809 * @param cli A cli_state connected to the server.
5810 * @param mem_ctx Talloc context, destroyed on completion of the function.
5811 * @param argc Standard main() style argc.
5812 * @param argv Standard main() style argv. Initial components are already
5813 * stripped.
5815 * @return normal NTSTATUS return code.
5818 static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
5819 const DOM_SID *domain_sid,
5820 const char *domain_name,
5821 struct cli_state *cli,
5822 struct rpc_pipe_client *pipe_hnd,
5823 TALLOC_CTX *mem_ctx,
5824 int argc,
5825 const char **argv)
5827 POLICY_HND connect_pol, domain_pol, user_pol;
5828 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
5829 char *acct_name;
5830 DOM_SID trust_acct_sid;
5831 struct samr_Ids user_rids, name_types;
5832 struct lsa_String lsa_acct_name;
5834 if (argc != 1) {
5835 d_printf("Usage: net rpc trustdom del <domain_name>\n");
5836 return NT_STATUS_INVALID_PARAMETER;
5840 * Make valid trusting domain account (ie. uppercased and with '$' appended)
5842 acct_name = talloc_asprintf(mem_ctx, "%s$", argv[0]);
5844 if (acct_name == NULL)
5845 return NT_STATUS_NO_MEMORY;
5847 strupper_m(acct_name);
5849 /* Get samr policy handle */
5850 result = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
5851 pipe_hnd->desthost,
5852 MAXIMUM_ALLOWED_ACCESS,
5853 &connect_pol);
5854 if (!NT_STATUS_IS_OK(result)) {
5855 goto done;
5858 /* Get domain policy handle */
5859 result = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
5860 &connect_pol,
5861 MAXIMUM_ALLOWED_ACCESS,
5862 CONST_DISCARD(struct dom_sid2 *, domain_sid),
5863 &domain_pol);
5864 if (!NT_STATUS_IS_OK(result)) {
5865 goto done;
5868 init_lsa_String(&lsa_acct_name, acct_name);
5870 result = rpccli_samr_LookupNames(pipe_hnd, mem_ctx,
5871 &domain_pol,
5873 &lsa_acct_name,
5874 &user_rids,
5875 &name_types);
5877 if (!NT_STATUS_IS_OK(result)) {
5878 d_printf("net rpc trustdom del: LookupNames on user %s failed %s\n",
5879 acct_name, nt_errstr(result) );
5880 goto done;
5883 result = rpccli_samr_OpenUser(pipe_hnd, mem_ctx,
5884 &domain_pol,
5885 MAXIMUM_ALLOWED_ACCESS,
5886 user_rids.ids[0],
5887 &user_pol);
5889 if (!NT_STATUS_IS_OK(result)) {
5890 d_printf("net rpc trustdom del: OpenUser on user %s failed %s\n",
5891 acct_name, nt_errstr(result) );
5892 goto done;
5895 /* append the rid to the domain sid */
5896 sid_copy(&trust_acct_sid, domain_sid);
5897 if (!sid_append_rid(&trust_acct_sid, user_rids.ids[0])) {
5898 goto done;
5901 /* remove the sid */
5903 result = rpccli_samr_RemoveMemberFromForeignDomain(pipe_hnd, mem_ctx,
5904 &user_pol,
5905 &trust_acct_sid);
5906 if (!NT_STATUS_IS_OK(result)) {
5907 d_printf("net rpc trustdom del: RemoveMemberFromForeignDomain on user %s failed %s\n",
5908 acct_name, nt_errstr(result) );
5909 goto done;
5912 /* Delete user */
5914 result = rpccli_samr_DeleteUser(pipe_hnd, mem_ctx,
5915 &user_pol);
5917 if (!NT_STATUS_IS_OK(result)) {
5918 d_printf("net rpc trustdom del: DeleteUser on user %s failed %s\n",
5919 acct_name, nt_errstr(result) );
5920 goto done;
5923 if (!NT_STATUS_IS_OK(result)) {
5924 d_printf("Could not set trust account password: %s\n",
5925 nt_errstr(result));
5926 goto done;
5929 done:
5930 return result;
5934 * Delete interdomain trust account for a remote domain.
5936 * @param argc Standard argc.
5937 * @param argv Standard argv without initial components.
5939 * @return Integer status (0 means success).
5942 static int rpc_trustdom_del(struct net_context *c, int argc, const char **argv)
5944 if (argc > 0 && !c->display_usage) {
5945 return run_rpc_command(c, NULL, &ndr_table_samr.syntax_id, 0,
5946 rpc_trustdom_del_internals, argc, argv);
5947 } else {
5948 d_printf("Usage:\n"
5949 "net rpc trustdom del <domain>\n");
5950 return -1;
5954 static NTSTATUS rpc_trustdom_get_pdc(struct net_context *c,
5955 struct cli_state *cli,
5956 TALLOC_CTX *mem_ctx,
5957 const char *domain_name)
5959 char *dc_name = NULL;
5960 const char *buffer = NULL;
5961 struct rpc_pipe_client *netr;
5962 NTSTATUS status;
5964 /* Use NetServerEnum2 */
5966 if (cli_get_pdc_name(cli, domain_name, &dc_name)) {
5967 SAFE_FREE(dc_name);
5968 return NT_STATUS_OK;
5971 DEBUG(1,("NetServerEnum2 error: Couldn't find primary domain controller\
5972 for domain %s\n", domain_name));
5974 /* Try netr_GetDcName */
5976 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_netlogon.syntax_id,
5977 &netr);
5978 if (!NT_STATUS_IS_OK(status)) {
5979 return status;
5982 status = rpccli_netr_GetDcName(netr, mem_ctx,
5983 cli->desthost,
5984 domain_name,
5985 &buffer,
5986 NULL);
5987 TALLOC_FREE(netr);
5989 if (NT_STATUS_IS_OK(status)) {
5990 return status;
5993 DEBUG(1,("netr_GetDcName error: Couldn't find primary domain controller\
5994 for domain %s\n", domain_name));
5996 return status;
6000 * Establish trust relationship to a trusting domain.
6001 * Interdomain account must already be created on remote PDC.
6003 * @param c A net_context structure.
6004 * @param argc Standard argc.
6005 * @param argv Standard argv without initial components.
6007 * @return Integer status (0 means success).
6010 static int rpc_trustdom_establish(struct net_context *c, int argc,
6011 const char **argv)
6013 struct cli_state *cli = NULL;
6014 struct sockaddr_storage server_ss;
6015 struct rpc_pipe_client *pipe_hnd = NULL;
6016 POLICY_HND connect_hnd;
6017 TALLOC_CTX *mem_ctx;
6018 NTSTATUS nt_status;
6019 DOM_SID *domain_sid;
6021 char* domain_name;
6022 char* acct_name;
6023 fstring pdc_name;
6024 union lsa_PolicyInformation *info = NULL;
6027 * Connect to \\server\ipc$ as 'our domain' account with password
6030 if (argc != 1 || c->display_usage) {
6031 d_printf("Usage:\n"
6032 "net rpc trustdom establish <domain_name>\n");
6033 return -1;
6036 domain_name = smb_xstrdup(argv[0]);
6037 strupper_m(domain_name);
6039 /* account name used at first is our domain's name with '$' */
6040 asprintf(&acct_name, "%s$", lp_workgroup());
6041 strupper_m(acct_name);
6044 * opt_workgroup will be used by connection functions further,
6045 * hence it should be set to remote domain name instead of ours
6047 if (c->opt_workgroup) {
6048 c->opt_workgroup = smb_xstrdup(domain_name);
6051 c->opt_user_name = acct_name;
6053 /* find the domain controller */
6054 if (!net_find_pdc(&server_ss, pdc_name, domain_name)) {
6055 DEBUG(0, ("Couldn't find domain controller for domain %s\n", domain_name));
6056 return -1;
6059 /* connect to ipc$ as username/password */
6060 nt_status = connect_to_ipc(c, &cli, &server_ss, pdc_name);
6061 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
6063 /* Is it trusting domain account for sure ? */
6064 DEBUG(0, ("Couldn't verify trusting domain account. Error was %s\n",
6065 nt_errstr(nt_status)));
6066 return -1;
6069 /* store who we connected to */
6071 saf_store( domain_name, pdc_name );
6074 * Connect to \\server\ipc$ again (this time anonymously)
6077 nt_status = connect_to_ipc_anonymous(c, &cli, &server_ss,
6078 (char*)pdc_name);
6080 if (NT_STATUS_IS_ERR(nt_status)) {
6081 DEBUG(0, ("Couldn't connect to domain %s controller. Error was %s.\n",
6082 domain_name, nt_errstr(nt_status)));
6083 return -1;
6086 if (!(mem_ctx = talloc_init("establishing trust relationship to "
6087 "domain %s", domain_name))) {
6088 DEBUG(0, ("talloc_init() failed\n"));
6089 cli_shutdown(cli);
6090 return -1;
6093 /* Make sure we're talking to a proper server */
6095 nt_status = rpc_trustdom_get_pdc(c, cli, mem_ctx, domain_name);
6096 if (!NT_STATUS_IS_OK(nt_status)) {
6097 cli_shutdown(cli);
6098 talloc_destroy(mem_ctx);
6099 return -1;
6103 * Call LsaOpenPolicy and LsaQueryInfo
6106 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6107 &pipe_hnd);
6108 if (!NT_STATUS_IS_OK(nt_status)) {
6109 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n", nt_errstr(nt_status) ));
6110 cli_shutdown(cli);
6111 talloc_destroy(mem_ctx);
6112 return -1;
6115 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, true, SEC_RIGHTS_QUERY_VALUE,
6116 &connect_hnd);
6117 if (NT_STATUS_IS_ERR(nt_status)) {
6118 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6119 nt_errstr(nt_status)));
6120 cli_shutdown(cli);
6121 talloc_destroy(mem_ctx);
6122 return -1;
6125 /* Querying info level 5 */
6127 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6128 &connect_hnd,
6129 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6130 &info);
6131 if (NT_STATUS_IS_ERR(nt_status)) {
6132 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6133 nt_errstr(nt_status)));
6134 cli_shutdown(cli);
6135 talloc_destroy(mem_ctx);
6136 return -1;
6139 domain_sid = info->account_domain.sid;
6141 /* There should be actually query info level 3 (following nt serv behaviour),
6142 but I still don't know if it's _really_ necessary */
6145 * Store the password in secrets db
6148 if (!pdb_set_trusteddom_pw(domain_name, c->opt_password, domain_sid)) {
6149 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6150 cli_shutdown(cli);
6151 talloc_destroy(mem_ctx);
6152 return -1;
6156 * Close the pipes and clean up
6159 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6160 if (NT_STATUS_IS_ERR(nt_status)) {
6161 DEBUG(0, ("Couldn't close LSA pipe. Error was %s\n",
6162 nt_errstr(nt_status)));
6163 cli_shutdown(cli);
6164 talloc_destroy(mem_ctx);
6165 return -1;
6168 cli_shutdown(cli);
6170 talloc_destroy(mem_ctx);
6172 d_printf("Trust to domain %s established\n", domain_name);
6173 return 0;
6177 * Revoke trust relationship to the remote domain.
6179 * @param c A net_context structure.
6180 * @param argc Standard argc.
6181 * @param argv Standard argv without initial components.
6183 * @return Integer status (0 means success).
6186 static int rpc_trustdom_revoke(struct net_context *c, int argc,
6187 const char **argv)
6189 char* domain_name;
6190 int rc = -1;
6192 if (argc < 1 || c->display_usage) {
6193 d_printf("Usage:\n"
6194 "net rpc trustdom revoke <domain_name>\n"
6195 " Revoke trust relationship\n"
6196 " domain_name\tName of domain to revoke trust\n");
6197 return -1;
6200 /* generate upper cased domain name */
6201 domain_name = smb_xstrdup(argv[0]);
6202 strupper_m(domain_name);
6204 /* delete password of the trust */
6205 if (!pdb_del_trusteddom_pw(domain_name)) {
6206 DEBUG(0, ("Failed to revoke relationship to the trusted domain %s\n",
6207 domain_name));
6208 goto done;
6211 rc = 0;
6212 done:
6213 SAFE_FREE(domain_name);
6214 return rc;
6217 static NTSTATUS rpc_query_domain_sid(struct net_context *c,
6218 const DOM_SID *domain_sid,
6219 const char *domain_name,
6220 struct cli_state *cli,
6221 struct rpc_pipe_client *pipe_hnd,
6222 TALLOC_CTX *mem_ctx,
6223 int argc,
6224 const char **argv)
6226 fstring str_sid;
6227 sid_to_fstring(str_sid, domain_sid);
6228 d_printf("%s\n", str_sid);
6229 return NT_STATUS_OK;
6232 static void print_trusted_domain(DOM_SID *dom_sid, const char *trusted_dom_name)
6234 fstring ascii_sid, padding;
6235 int pad_len, col_len = 20;
6237 /* convert sid into ascii string */
6238 sid_to_fstring(ascii_sid, dom_sid);
6240 /* calculate padding space for d_printf to look nicer */
6241 pad_len = col_len - strlen(trusted_dom_name);
6242 padding[pad_len] = 0;
6243 do padding[--pad_len] = ' '; while (pad_len);
6245 d_printf("%s%s%s\n", trusted_dom_name, padding, ascii_sid);
6248 static NTSTATUS vampire_trusted_domain(struct rpc_pipe_client *pipe_hnd,
6249 TALLOC_CTX *mem_ctx,
6250 POLICY_HND *pol,
6251 DOM_SID dom_sid,
6252 const char *trusted_dom_name)
6254 NTSTATUS nt_status;
6255 union lsa_TrustedDomainInfo *info = NULL;
6256 char *cleartextpwd = NULL;
6257 uint8_t nt_hash[16];
6258 DATA_BLOB data;
6260 nt_status = rpccli_lsa_QueryTrustedDomainInfoBySid(pipe_hnd, mem_ctx,
6261 pol,
6262 &dom_sid,
6263 LSA_TRUSTED_DOMAIN_INFO_PASSWORD,
6264 &info);
6265 if (NT_STATUS_IS_ERR(nt_status)) {
6266 DEBUG(0,("Could not query trusted domain info. Error was %s\n",
6267 nt_errstr(nt_status)));
6268 goto done;
6271 data = data_blob(info->password.password->data,
6272 info->password.password->length);
6274 if (!rpccli_get_pwd_hash(pipe_hnd, nt_hash)) {
6275 DEBUG(0, ("Could not retrieve password hash\n"));
6276 goto done;
6279 cleartextpwd = decrypt_trustdom_secret(nt_hash, &data);
6281 if (cleartextpwd == NULL) {
6282 DEBUG(0,("retrieved NULL password\n"));
6283 nt_status = NT_STATUS_UNSUCCESSFUL;
6284 goto done;
6287 if (!pdb_set_trusteddom_pw(trusted_dom_name, cleartextpwd, &dom_sid)) {
6288 DEBUG(0, ("Storing password for trusted domain failed.\n"));
6289 nt_status = NT_STATUS_UNSUCCESSFUL;
6290 goto done;
6293 #ifdef DEBUG_PASSWORD
6294 DEBUG(100,("successfully vampired trusted domain [%s], sid: [%s], "
6295 "password: [%s]\n", trusted_dom_name,
6296 sid_string_dbg(&dom_sid), cleartextpwd));
6297 #endif
6299 done:
6300 SAFE_FREE(cleartextpwd);
6301 data_blob_free(&data);
6303 return nt_status;
6306 static int rpc_trustdom_vampire(struct net_context *c, int argc,
6307 const char **argv)
6309 /* common variables */
6310 TALLOC_CTX* mem_ctx;
6311 struct cli_state *cli = NULL;
6312 struct rpc_pipe_client *pipe_hnd = NULL;
6313 NTSTATUS nt_status;
6314 const char *domain_name = NULL;
6315 DOM_SID *queried_dom_sid;
6316 POLICY_HND connect_hnd;
6317 union lsa_PolicyInformation *info = NULL;
6319 /* trusted domains listing variables */
6320 unsigned int enum_ctx = 0;
6321 int i;
6322 struct lsa_DomainList dom_list;
6323 fstring pdc_name;
6325 if (c->display_usage) {
6326 d_printf("Usage:\n"
6327 "net rpc trustdom vampire\n"
6328 " Vampire trust relationship from remote server\n");
6329 return 0;
6333 * Listing trusted domains (stored in secrets.tdb, if local)
6336 mem_ctx = talloc_init("trust relationships vampire");
6339 * set domain and pdc name to local samba server (default)
6340 * or to remote one given in command line
6343 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6344 domain_name = c->opt_workgroup;
6345 c->opt_target_workgroup = c->opt_workgroup;
6346 } else {
6347 fstrcpy(pdc_name, global_myname());
6348 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6349 c->opt_target_workgroup = domain_name;
6352 /* open \PIPE\lsarpc and open policy handle */
6353 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6354 if (!NT_STATUS_IS_OK(nt_status)) {
6355 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6356 nt_errstr(nt_status)));
6357 talloc_destroy(mem_ctx);
6358 return -1;
6361 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6362 &pipe_hnd);
6363 if (!NT_STATUS_IS_OK(nt_status)) {
6364 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6365 nt_errstr(nt_status) ));
6366 cli_shutdown(cli);
6367 talloc_destroy(mem_ctx);
6368 return -1;
6371 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6372 &connect_hnd);
6373 if (NT_STATUS_IS_ERR(nt_status)) {
6374 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6375 nt_errstr(nt_status)));
6376 cli_shutdown(cli);
6377 talloc_destroy(mem_ctx);
6378 return -1;
6381 /* query info level 5 to obtain sid of a domain being queried */
6382 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6383 &connect_hnd,
6384 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6385 &info);
6387 if (NT_STATUS_IS_ERR(nt_status)) {
6388 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6389 nt_errstr(nt_status)));
6390 cli_shutdown(cli);
6391 talloc_destroy(mem_ctx);
6392 return -1;
6395 queried_dom_sid = info->account_domain.sid;
6398 * Keep calling LsaEnumTrustdom over opened pipe until
6399 * the end of enumeration is reached
6402 d_printf("Vampire trusted domains:\n\n");
6404 do {
6405 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6406 &connect_hnd,
6407 &enum_ctx,
6408 &dom_list,
6409 (uint32_t)-1);
6410 if (NT_STATUS_IS_ERR(nt_status)) {
6411 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6412 nt_errstr(nt_status)));
6413 cli_shutdown(cli);
6414 talloc_destroy(mem_ctx);
6415 return -1;
6418 for (i = 0; i < dom_list.count; i++) {
6420 print_trusted_domain(dom_list.domains[i].sid,
6421 dom_list.domains[i].name.string);
6423 nt_status = vampire_trusted_domain(pipe_hnd, mem_ctx, &connect_hnd,
6424 *dom_list.domains[i].sid,
6425 dom_list.domains[i].name.string);
6426 if (!NT_STATUS_IS_OK(nt_status)) {
6427 cli_shutdown(cli);
6428 talloc_destroy(mem_ctx);
6429 return -1;
6434 * in case of no trusted domains say something rather
6435 * than just display blank line
6437 if (!dom_list.count) d_printf("none\n");
6439 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6441 /* close this connection before doing next one */
6442 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6443 if (NT_STATUS_IS_ERR(nt_status)) {
6444 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6445 nt_errstr(nt_status)));
6446 cli_shutdown(cli);
6447 talloc_destroy(mem_ctx);
6448 return -1;
6451 /* close lsarpc pipe and connection to IPC$ */
6452 cli_shutdown(cli);
6454 talloc_destroy(mem_ctx);
6455 return 0;
6458 static int rpc_trustdom_list(struct net_context *c, int argc, const char **argv)
6460 /* common variables */
6461 TALLOC_CTX* mem_ctx;
6462 struct cli_state *cli = NULL, *remote_cli = NULL;
6463 struct rpc_pipe_client *pipe_hnd = NULL;
6464 NTSTATUS nt_status;
6465 const char *domain_name = NULL;
6466 DOM_SID *queried_dom_sid;
6467 fstring padding;
6468 int ascii_dom_name_len;
6469 POLICY_HND connect_hnd;
6470 union lsa_PolicyInformation *info = NULL;
6472 /* trusted domains listing variables */
6473 unsigned int num_domains, enum_ctx = 0;
6474 int i, pad_len, col_len = 20;
6475 struct lsa_DomainList dom_list;
6476 fstring pdc_name;
6478 /* trusting domains listing variables */
6479 POLICY_HND domain_hnd;
6480 struct samr_SamArray *trusts = NULL;
6482 if (c->display_usage) {
6483 d_printf("Usage:\n"
6484 "net rpc trustdom list\n"
6485 " List trust relationships\n");
6486 return 0;
6490 * Listing trusted domains (stored in secrets.tdb, if local)
6493 mem_ctx = talloc_init("trust relationships listing");
6496 * set domain and pdc name to local samba server (default)
6497 * or to remote one given in command line
6500 if (StrCaseCmp(c->opt_workgroup, lp_workgroup())) {
6501 domain_name = c->opt_workgroup;
6502 c->opt_target_workgroup = c->opt_workgroup;
6503 } else {
6504 fstrcpy(pdc_name, global_myname());
6505 domain_name = talloc_strdup(mem_ctx, lp_workgroup());
6506 c->opt_target_workgroup = domain_name;
6509 /* open \PIPE\lsarpc and open policy handle */
6510 nt_status = net_make_ipc_connection(c, NET_FLAGS_PDC, &cli);
6511 if (!NT_STATUS_IS_OK(nt_status)) {
6512 DEBUG(0, ("Couldn't connect to domain controller: %s\n",
6513 nt_errstr(nt_status)));
6514 talloc_destroy(mem_ctx);
6515 return -1;
6518 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
6519 &pipe_hnd);
6520 if (!NT_STATUS_IS_OK(nt_status)) {
6521 DEBUG(0, ("Could not initialise lsa pipe. Error was %s\n",
6522 nt_errstr(nt_status) ));
6523 cli_shutdown(cli);
6524 talloc_destroy(mem_ctx);
6525 return -1;
6528 nt_status = rpccli_lsa_open_policy2(pipe_hnd, mem_ctx, false, SEC_RIGHTS_QUERY_VALUE,
6529 &connect_hnd);
6530 if (NT_STATUS_IS_ERR(nt_status)) {
6531 DEBUG(0, ("Couldn't open policy handle. Error was %s\n",
6532 nt_errstr(nt_status)));
6533 cli_shutdown(cli);
6534 talloc_destroy(mem_ctx);
6535 return -1;
6538 /* query info level 5 to obtain sid of a domain being queried */
6539 nt_status = rpccli_lsa_QueryInfoPolicy(pipe_hnd, mem_ctx,
6540 &connect_hnd,
6541 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
6542 &info);
6544 if (NT_STATUS_IS_ERR(nt_status)) {
6545 DEBUG(0, ("LSA Query Info failed. Returned error was %s\n",
6546 nt_errstr(nt_status)));
6547 cli_shutdown(cli);
6548 talloc_destroy(mem_ctx);
6549 return -1;
6552 queried_dom_sid = info->account_domain.sid;
6555 * Keep calling LsaEnumTrustdom over opened pipe until
6556 * the end of enumeration is reached
6559 d_printf("Trusted domains list:\n\n");
6561 do {
6562 nt_status = rpccli_lsa_EnumTrustDom(pipe_hnd, mem_ctx,
6563 &connect_hnd,
6564 &enum_ctx,
6565 &dom_list,
6566 (uint32_t)-1);
6567 if (NT_STATUS_IS_ERR(nt_status)) {
6568 DEBUG(0, ("Couldn't enumerate trusted domains. Error was %s\n",
6569 nt_errstr(nt_status)));
6570 cli_shutdown(cli);
6571 talloc_destroy(mem_ctx);
6572 return -1;
6575 for (i = 0; i < dom_list.count; i++) {
6576 print_trusted_domain(dom_list.domains[i].sid,
6577 dom_list.domains[i].name.string);
6581 * in case of no trusted domains say something rather
6582 * than just display blank line
6584 if (!dom_list.count) d_printf("none\n");
6586 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6588 /* close this connection before doing next one */
6589 nt_status = rpccli_lsa_Close(pipe_hnd, mem_ctx, &connect_hnd);
6590 if (NT_STATUS_IS_ERR(nt_status)) {
6591 DEBUG(0, ("Couldn't properly close lsa policy handle. Error was %s\n",
6592 nt_errstr(nt_status)));
6593 cli_shutdown(cli);
6594 talloc_destroy(mem_ctx);
6595 return -1;
6598 TALLOC_FREE(pipe_hnd);
6601 * Listing trusting domains (stored in passdb backend, if local)
6604 d_printf("\nTrusting domains list:\n\n");
6607 * Open \PIPE\samr and get needed policy handles
6609 nt_status = cli_rpc_pipe_open_noauth(cli, &ndr_table_samr.syntax_id,
6610 &pipe_hnd);
6611 if (!NT_STATUS_IS_OK(nt_status)) {
6612 DEBUG(0, ("Could not initialise samr pipe. Error was %s\n", nt_errstr(nt_status)));
6613 cli_shutdown(cli);
6614 talloc_destroy(mem_ctx);
6615 return -1;
6618 /* SamrConnect2 */
6619 nt_status = rpccli_samr_Connect2(pipe_hnd, mem_ctx,
6620 pipe_hnd->desthost,
6621 SA_RIGHT_SAM_OPEN_DOMAIN,
6622 &connect_hnd);
6623 if (!NT_STATUS_IS_OK(nt_status)) {
6624 DEBUG(0, ("Couldn't open SAMR policy handle. Error was %s\n",
6625 nt_errstr(nt_status)));
6626 cli_shutdown(cli);
6627 talloc_destroy(mem_ctx);
6628 return -1;
6631 /* SamrOpenDomain - we have to open domain policy handle in order to be
6632 able to enumerate accounts*/
6633 nt_status = rpccli_samr_OpenDomain(pipe_hnd, mem_ctx,
6634 &connect_hnd,
6635 SA_RIGHT_DOMAIN_ENUM_ACCOUNTS,
6636 queried_dom_sid,
6637 &domain_hnd);
6638 if (!NT_STATUS_IS_OK(nt_status)) {
6639 DEBUG(0, ("Couldn't open domain object. Error was %s\n",
6640 nt_errstr(nt_status)));
6641 cli_shutdown(cli);
6642 talloc_destroy(mem_ctx);
6643 return -1;
6647 * perform actual enumeration
6650 enum_ctx = 0; /* reset enumeration context from last enumeration */
6651 do {
6653 nt_status = rpccli_samr_EnumDomainUsers(pipe_hnd, mem_ctx,
6654 &domain_hnd,
6655 &enum_ctx,
6656 ACB_DOMTRUST,
6657 &trusts,
6658 0xffff,
6659 &num_domains);
6660 if (NT_STATUS_IS_ERR(nt_status)) {
6661 DEBUG(0, ("Couldn't enumerate accounts. Error was: %s\n",
6662 nt_errstr(nt_status)));
6663 cli_shutdown(cli);
6664 talloc_destroy(mem_ctx);
6665 return -1;
6668 for (i = 0; i < num_domains; i++) {
6670 char *str = CONST_DISCARD(char *, trusts->entries[i].name.string);
6673 * get each single domain's sid (do we _really_ need this ?):
6674 * 1) connect to domain's pdc
6675 * 2) query the pdc for domain's sid
6678 /* get rid of '$' tail */
6679 ascii_dom_name_len = strlen(str);
6680 if (ascii_dom_name_len && ascii_dom_name_len < FSTRING_LEN)
6681 str[ascii_dom_name_len - 1] = '\0';
6683 /* calculate padding space for d_printf to look nicer */
6684 pad_len = col_len - strlen(str);
6685 padding[pad_len] = 0;
6686 do padding[--pad_len] = ' '; while (pad_len);
6688 /* set opt_* variables to remote domain */
6689 strupper_m(str);
6690 c->opt_workgroup = talloc_strdup(mem_ctx, str);
6691 c->opt_target_workgroup = c->opt_workgroup;
6693 d_printf("%s%s", str, padding);
6695 /* connect to remote domain controller */
6696 nt_status = net_make_ipc_connection(c,
6697 NET_FLAGS_PDC | NET_FLAGS_ANONYMOUS,
6698 &remote_cli);
6699 if (NT_STATUS_IS_OK(nt_status)) {
6700 /* query for domain's sid */
6701 if (run_rpc_command(
6702 c, remote_cli,
6703 &ndr_table_lsarpc.syntax_id, 0,
6704 rpc_query_domain_sid, argc,
6705 argv))
6706 d_fprintf(stderr, "couldn't get domain's sid\n");
6708 cli_shutdown(remote_cli);
6710 } else {
6711 d_fprintf(stderr, "domain controller is not "
6712 "responding: %s\n",
6713 nt_errstr(nt_status));
6717 if (!num_domains) d_printf("none\n");
6719 } while (NT_STATUS_EQUAL(nt_status, STATUS_MORE_ENTRIES));
6721 /* close opened samr and domain policy handles */
6722 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &domain_hnd);
6723 if (!NT_STATUS_IS_OK(nt_status)) {
6724 DEBUG(0, ("Couldn't properly close domain policy handle for domain %s\n", domain_name));
6727 nt_status = rpccli_samr_Close(pipe_hnd, mem_ctx, &connect_hnd);
6728 if (!NT_STATUS_IS_OK(nt_status)) {
6729 DEBUG(0, ("Couldn't properly close samr policy handle for domain %s\n", domain_name));
6732 /* close samr pipe and connection to IPC$ */
6733 cli_shutdown(cli);
6735 talloc_destroy(mem_ctx);
6736 return 0;
6740 * Entrypoint for 'net rpc trustdom' code.
6742 * @param argc Standard argc.
6743 * @param argv Standard argv without initial components.
6745 * @return Integer status (0 means success).
6748 static int rpc_trustdom(struct net_context *c, int argc, const char **argv)
6750 struct functable func[] = {
6752 "add",
6753 rpc_trustdom_add,
6754 NET_TRANSPORT_RPC,
6755 "Add trusted domain's account",
6756 "net rpc trustdom add\n"
6757 " Add trusted domain's account"
6760 "del",
6761 rpc_trustdom_del,
6762 NET_TRANSPORT_RPC,
6763 "Remove trusted domain's account",
6764 "net rpc trustdom del\n"
6765 " Remove trusted domain's account"
6768 "establish",
6769 rpc_trustdom_establish,
6770 NET_TRANSPORT_RPC,
6771 "Establish trust relationship",
6772 "net rpc trustdom establish\n"
6773 " Establish trust relationship"
6776 "revoke",
6777 rpc_trustdom_revoke,
6778 NET_TRANSPORT_RPC,
6779 "Revoke trust relationship",
6780 "net rpc trustdom revoke\n"
6781 " Revoke trust relationship"
6784 "list",
6785 rpc_trustdom_list,
6786 NET_TRANSPORT_RPC,
6787 "List domain trusts",
6788 "net rpc trustdom list\n"
6789 " List domain trusts"
6792 "vampire",
6793 rpc_trustdom_vampire,
6794 NET_TRANSPORT_RPC,
6795 "Vampire trusts from remote server",
6796 "net rpc trustdom vampire\n"
6797 " Vampire trusts from remote server"
6799 {NULL, NULL, 0, NULL, NULL}
6802 return net_run_function(c, argc, argv, "net rpc trustdom", func);
6806 * Check if a server will take rpc commands
6807 * @param flags Type of server to connect to (PDC, DMB, localhost)
6808 * if the host is not explicitly specified
6809 * @return bool (true means rpc supported)
6811 bool net_rpc_check(struct net_context *c, unsigned flags)
6813 struct cli_state *cli;
6814 bool ret = false;
6815 struct sockaddr_storage server_ss;
6816 char *server_name = NULL;
6817 NTSTATUS status;
6819 /* flags (i.e. server type) may depend on command */
6820 if (!net_find_server(c, NULL, flags, &server_ss, &server_name))
6821 return false;
6823 if ((cli = cli_initialise()) == NULL) {
6824 return false;
6827 status = cli_connect(cli, server_name, &server_ss);
6828 if (!NT_STATUS_IS_OK(status))
6829 goto done;
6830 if (!attempt_netbios_session_request(&cli, global_myname(),
6831 server_name, &server_ss))
6832 goto done;
6833 if (!cli_negprot(cli))
6834 goto done;
6835 if (cli->protocol < PROTOCOL_NT1)
6836 goto done;
6838 ret = true;
6839 done:
6840 cli_shutdown(cli);
6841 return ret;
6844 /* dump sam database via samsync rpc calls */
6845 static int rpc_samdump(struct net_context *c, int argc, const char **argv) {
6846 if (c->display_usage) {
6847 d_printf("Usage:\n"
6848 "net rpc samdump\n"
6849 " Dump remote SAM database\n");
6850 return 0;
6853 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6854 NET_FLAGS_ANONYMOUS,
6855 rpc_samdump_internals, argc, argv);
6858 /* syncronise sam database via samsync rpc calls */
6859 static int rpc_vampire(struct net_context *c, int argc, const char **argv)
6861 struct functable func[] = {
6863 "ldif",
6864 rpc_vampire_ldif,
6865 NET_TRANSPORT_RPC,
6866 "Dump remote SAM database to ldif",
6867 "net rpc vampire ldif\n"
6868 " Dump remote SAM database to LDIF file or stdout"
6871 "keytab",
6872 rpc_vampire_keytab,
6873 NET_TRANSPORT_RPC,
6874 "Dump remote SAM database to Kerberos Keytab",
6875 "net rpc vampire keytab\n"
6876 " Dump remote SAM database to Kerberos keytab file"
6879 {NULL, NULL, 0, NULL, NULL}
6882 if (argc == 0) {
6883 if (c->display_usage) {
6884 d_printf("Usage:\n"
6885 "net rpc vampire\n"
6886 " Vampire remote SAM database\n");
6887 return 0;
6890 return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id,
6891 NET_FLAGS_ANONYMOUS,
6892 rpc_vampire_internals,
6893 argc, argv);
6896 return net_run_function(c, argc, argv, "net rpc vampire", func);
6900 * Migrate everything from a print server.
6902 * @param c A net_context structure.
6903 * @param argc Standard main() style argc.
6904 * @param argv Standard main() style argv. Initial components are already
6905 * stripped.
6907 * @return A shell status integer (0 for success).
6909 * The order is important !
6910 * To successfully add drivers the print queues have to exist !
6911 * Applying ACLs should be the last step, because you're easily locked out.
6914 static int rpc_printer_migrate_all(struct net_context *c, int argc,
6915 const char **argv)
6917 int ret;
6919 if (c->display_usage) {
6920 d_printf("Usage:\n"
6921 "net rpc printer migrate all\n"
6922 " Migrate everything from a print server\n");
6923 return 0;
6926 if (!c->opt_host) {
6927 d_printf("no server to migrate\n");
6928 return -1;
6931 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6932 rpc_printer_migrate_printers_internals, argc,
6933 argv);
6934 if (ret)
6935 return ret;
6937 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6938 rpc_printer_migrate_drivers_internals, argc,
6939 argv);
6940 if (ret)
6941 return ret;
6943 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6944 rpc_printer_migrate_forms_internals, argc, argv);
6945 if (ret)
6946 return ret;
6948 ret = run_rpc_command(c, NULL, &syntax_spoolss, 0,
6949 rpc_printer_migrate_settings_internals, argc,
6950 argv);
6951 if (ret)
6952 return ret;
6954 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6955 rpc_printer_migrate_security_internals, argc,
6956 argv);
6961 * Migrate print drivers from a print server.
6963 * @param c A net_context structure.
6964 * @param argc Standard main() style argc.
6965 * @param argv Standard main() style argv. Initial components are already
6966 * stripped.
6968 * @return A shell status integer (0 for success).
6970 static int rpc_printer_migrate_drivers(struct net_context *c, int argc,
6971 const char **argv)
6973 if (c->display_usage) {
6974 d_printf("Usage:\n"
6975 "net rpc printer migrate drivers\n"
6976 " Migrate print-drivers from a print-server\n");
6977 return 0;
6980 if (!c->opt_host) {
6981 d_printf("no server to migrate\n");
6982 return -1;
6985 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
6986 rpc_printer_migrate_drivers_internals,
6987 argc, argv);
6991 * Migrate print-forms from a print-server.
6993 * @param c A net_context structure.
6994 * @param argc Standard main() style argc.
6995 * @param argv Standard main() style argv. Initial components are already
6996 * stripped.
6998 * @return A shell status integer (0 for success).
7000 static int rpc_printer_migrate_forms(struct net_context *c, int argc,
7001 const char **argv)
7003 if (c->display_usage) {
7004 d_printf("Usage:\n"
7005 "net rpc printer migrate forms\n"
7006 " Migrate print-forms from a print-server\n");
7007 return 0;
7010 if (!c->opt_host) {
7011 d_printf("no server to migrate\n");
7012 return -1;
7015 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7016 rpc_printer_migrate_forms_internals,
7017 argc, argv);
7021 * Migrate printers from a print-server.
7023 * @param c A net_context structure.
7024 * @param argc Standard main() style argc.
7025 * @param argv Standard main() style argv. Initial components are already
7026 * stripped.
7028 * @return A shell status integer (0 for success).
7030 static int rpc_printer_migrate_printers(struct net_context *c, int argc,
7031 const char **argv)
7033 if (c->display_usage) {
7034 d_printf("Usage:\n"
7035 "net rpc printer migrate printers\n"
7036 " Migrate printers from a print-server\n");
7037 return 0;
7040 if (!c->opt_host) {
7041 d_printf("no server to migrate\n");
7042 return -1;
7045 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7046 rpc_printer_migrate_printers_internals,
7047 argc, argv);
7051 * Migrate printer-ACLs from a print-server
7053 * @param c A net_context structure.
7054 * @param argc Standard main() style argc.
7055 * @param argv Standard main() style argv. Initial components are already
7056 * stripped.
7058 * @return A shell status integer (0 for success).
7060 static int rpc_printer_migrate_security(struct net_context *c, int argc,
7061 const char **argv)
7063 if (c->display_usage) {
7064 d_printf("Usage:\n"
7065 "net rpc printer migrate security\n"
7066 " Migrate printer-ACLs from a print-server\n");
7067 return 0;
7070 if (!c->opt_host) {
7071 d_printf("no server to migrate\n");
7072 return -1;
7075 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7076 rpc_printer_migrate_security_internals,
7077 argc, argv);
7081 * Migrate printer-settings from a print-server.
7083 * @param c A net_context structure.
7084 * @param argc Standard main() style argc.
7085 * @param argv Standard main() style argv. Initial components are already
7086 * stripped.
7088 * @return A shell status integer (0 for success).
7090 static int rpc_printer_migrate_settings(struct net_context *c, int argc,
7091 const char **argv)
7093 if (c->display_usage) {
7094 d_printf("Usage:\n"
7095 "net rpc printer migrate settings\n"
7096 " Migrate printer-settings from a print-server\n");
7097 return 0;
7100 if (!c->opt_host) {
7101 d_printf("no server to migrate\n");
7102 return -1;
7105 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7106 rpc_printer_migrate_settings_internals,
7107 argc, argv);
7111 * 'net rpc printer' entrypoint.
7113 * @param c A net_context structure.
7114 * @param argc Standard main() style argc.
7115 * @param argv Standard main() style argv. Initial components are already
7116 * stripped.
7119 int rpc_printer_migrate(struct net_context *c, int argc, const char **argv)
7122 /* ouch: when addriver and setdriver are called from within
7123 rpc_printer_migrate_drivers_internals, the printer-queue already
7124 *has* to exist */
7126 struct functable func[] = {
7128 "all",
7129 rpc_printer_migrate_all,
7130 NET_TRANSPORT_RPC,
7131 "Migrate all from remote to local print server",
7132 "net rpc printer migrate all\n"
7133 " Migrate all from remote to local print server"
7136 "drivers",
7137 rpc_printer_migrate_drivers,
7138 NET_TRANSPORT_RPC,
7139 "Migrate drivers to local server",
7140 "net rpc printer migrate drivers\n"
7141 " Migrate drivers to local server"
7144 "forms",
7145 rpc_printer_migrate_forms,
7146 NET_TRANSPORT_RPC,
7147 "Migrate froms to local server",
7148 "net rpc printer migrate forms\n"
7149 " Migrate froms to local server"
7152 "printers",
7153 rpc_printer_migrate_printers,
7154 NET_TRANSPORT_RPC,
7155 "Migrate printers to local server",
7156 "net rpc printer migrate printers\n"
7157 " Migrate printers to local server"
7160 "security",
7161 rpc_printer_migrate_security,
7162 NET_TRANSPORT_RPC,
7163 "Mirgate printer ACLs to local server",
7164 "net rpc printer migrate security\n"
7165 " Mirgate printer ACLs to local server"
7168 "settings",
7169 rpc_printer_migrate_settings,
7170 NET_TRANSPORT_RPC,
7171 "Migrate printer settings to local server",
7172 "net rpc printer migrate settings\n"
7173 " Migrate printer settings to local server"
7175 {NULL, NULL, 0, NULL, NULL}
7178 return net_run_function(c, argc, argv, "net rpc printer migrate",func);
7183 * List printers on a remote RPC server.
7185 * @param c A net_context structure.
7186 * @param argc Standard main() style argc.
7187 * @param argv Standard main() style argv. Initial components are already
7188 * stripped.
7190 * @return A shell status integer (0 for success).
7192 static int rpc_printer_list(struct net_context *c, int argc, const char **argv)
7194 if (c->display_usage) {
7195 d_printf("Usage:\n"
7196 "net rpc printer list\n"
7197 " List printers on a remote RPC server\n");
7198 return 0;
7201 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7202 rpc_printer_list_internals,
7203 argc, argv);
7207 * List printer-drivers on a remote RPC server.
7209 * @param c A net_context structure.
7210 * @param argc Standard main() style argc.
7211 * @param argv Standard main() style argv. Initial components are already
7212 * stripped.
7214 * @return A shell status integer (0 for success).
7216 static int rpc_printer_driver_list(struct net_context *c, int argc,
7217 const char **argv)
7219 if (c->display_usage) {
7220 d_printf("Usage:\n"
7221 "net rpc printer driver\n"
7222 " List printer-drivers on a remote RPC server\n");
7223 return 0;
7226 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7227 rpc_printer_driver_list_internals,
7228 argc, argv);
7232 * Publish printer in ADS via MSRPC.
7234 * @param c A net_context structure.
7235 * @param argc Standard main() style argc.
7236 * @param argv Standard main() style argv. Initial components are already
7237 * stripped.
7239 * @return A shell status integer (0 for success).
7241 static int rpc_printer_publish_publish(struct net_context *c, int argc,
7242 const char **argv)
7244 if (c->display_usage) {
7245 d_printf("Usage:\n"
7246 "net rpc printer publish publish\n"
7247 " Publish printer in ADS via MSRPC\n");
7248 return 0;
7251 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7252 rpc_printer_publish_publish_internals,
7253 argc, argv);
7257 * Update printer in ADS via MSRPC.
7259 * @param c A net_context structure.
7260 * @param argc Standard main() style argc.
7261 * @param argv Standard main() style argv. Initial components are already
7262 * stripped.
7264 * @return A shell status integer (0 for success).
7266 static int rpc_printer_publish_update(struct net_context *c, int argc, const char **argv)
7268 if (c->display_usage) {
7269 d_printf("Usage:\n"
7270 "net rpc printer publish update\n"
7271 " Update printer in ADS via MSRPC\n");
7272 return 0;
7275 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7276 rpc_printer_publish_update_internals,
7277 argc, argv);
7281 * UnPublish printer in ADS via MSRPC.
7283 * @param c A net_context structure.
7284 * @param argc Standard main() style argc.
7285 * @param argv Standard main() style argv. Initial components are already
7286 * stripped.
7288 * @return A shell status integer (0 for success).
7290 static int rpc_printer_publish_unpublish(struct net_context *c, int argc,
7291 const char **argv)
7293 if (c->display_usage) {
7294 d_printf("Usage:\n"
7295 "net rpc printer publish unpublish\n"
7296 " UnPublish printer in ADS via MSRPC\n");
7297 return 0;
7300 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7301 rpc_printer_publish_unpublish_internals,
7302 argc, argv);
7306 * List published printers via MSRPC.
7308 * @param c A net_context structure.
7309 * @param argc Standard main() style argc.
7310 * @param argv Standard main() style argv. Initial components are already
7311 * stripped.
7313 * @return A shell status integer (0 for success).
7315 static int rpc_printer_publish_list(struct net_context *c, int argc,
7316 const char **argv)
7318 if (c->display_usage) {
7319 d_printf("Usage:\n"
7320 "net rpc printer publish list\n"
7321 " List published printers via MSRPC\n");
7322 return 0;
7325 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7326 rpc_printer_publish_list_internals,
7327 argc, argv);
7332 * Publish printer in ADS.
7334 * @param c A net_context structure.
7335 * @param argc Standard main() style argc.
7336 * @param argv Standard main() style argv. Initial components are already
7337 * stripped.
7339 * @return A shell status integer (0 for success).
7341 static int rpc_printer_publish(struct net_context *c, int argc,
7342 const char **argv)
7345 struct functable func[] = {
7347 "publish",
7348 rpc_printer_publish_publish,
7349 NET_TRANSPORT_RPC,
7350 "Publish printer in AD",
7351 "net rpc printer publish publish\n"
7352 " Publish printer in AD"
7355 "update",
7356 rpc_printer_publish_update,
7357 NET_TRANSPORT_RPC,
7358 "Update printer in AD",
7359 "net rpc printer publish update\n"
7360 " Update printer in AD"
7363 "unpublish",
7364 rpc_printer_publish_unpublish,
7365 NET_TRANSPORT_RPC,
7366 "Unpublish printer",
7367 "net rpc printer publish unpublish\n"
7368 " Unpublish printer"
7371 "list",
7372 rpc_printer_publish_list,
7373 NET_TRANSPORT_RPC,
7374 "List published printers",
7375 "net rpc printer publish list\n"
7376 " List published printers"
7378 {NULL, NULL, 0, NULL, NULL}
7381 if (argc == 0) {
7382 if (c->display_usage) {
7383 d_printf("Usage:\n");
7384 d_printf("net rpc printer publish\n"
7385 " List published printers\n"
7386 " Alias of net rpc printer publish list\n");
7387 net_display_usage_from_functable(func);
7388 return 0;
7390 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7391 rpc_printer_publish_list_internals,
7392 argc, argv);
7395 return net_run_function(c, argc, argv, "net rpc printer publish",func);
7401 * Display rpc printer help page.
7403 * @param c A net_context structure.
7404 * @param argc Standard main() style argc.
7405 * @param argv Standard main() style argv. Initial components are already
7406 * stripped.
7408 int rpc_printer_usage(struct net_context *c, int argc, const char **argv)
7410 d_printf("net rpc printer LIST [printer] [misc. options] [targets]\n"
7411 "\tlists all printers on print-server\n\n");
7412 d_printf("net rpc printer DRIVER [printer] [misc. options] [targets]\n"
7413 "\tlists all printer-drivers on print-server\n\n");
7414 d_printf("net rpc printer PUBLISH action [printer] [misc. options] [targets]\n"
7415 "\tpublishes printer settings in Active Directory\n"
7416 "\taction can be one of PUBLISH, UPDATE, UNPUBLISH or LIST\n\n");
7417 d_printf("net rpc printer MIGRATE PRINTERS [printer] [misc. options] [targets]"
7418 "\n\tmigrates printers from remote to local server\n\n");
7419 d_printf("net rpc printer MIGRATE SETTINGS [printer] [misc. options] [targets]"
7420 "\n\tmigrates printer-settings from remote to local server\n\n");
7421 d_printf("net rpc printer MIGRATE DRIVERS [printer] [misc. options] [targets]"
7422 "\n\tmigrates printer-drivers from remote to local server\n\n");
7423 d_printf("net rpc printer MIGRATE FORMS [printer] [misc. options] [targets]"
7424 "\n\tmigrates printer-forms from remote to local server\n\n");
7425 d_printf("net rpc printer MIGRATE SECURITY [printer] [misc. options] [targets]"
7426 "\n\tmigrates printer-ACLs from remote to local server\n\n");
7427 d_printf("net rpc printer MIGRATE ALL [printer] [misc. options] [targets]"
7428 "\n\tmigrates drivers, forms, queues, settings and acls from\n"
7429 "\tremote to local print-server\n\n");
7430 net_common_methods_usage(c, argc, argv);
7431 net_common_flags_usage(c, argc, argv);
7432 d_printf(
7433 "\t-v or --verbose\t\t\tgive verbose output\n"
7434 "\t --destination\t\tmigration target server (default: localhost)\n");
7436 return -1;
7440 * 'net rpc printer' entrypoint.
7442 * @param c A net_context structure.
7443 * @param argc Standard main() style argc.
7444 * @param argv Standard main() style argv. Initial components are already
7445 * stripped.
7447 int net_rpc_printer(struct net_context *c, int argc, const char **argv)
7449 struct functable func[] = {
7451 "list",
7452 rpc_printer_list,
7453 NET_TRANSPORT_RPC,
7454 "List all printers on print server",
7455 "net rpc printer list\n"
7456 " List all printers on print server"
7459 "migrate",
7460 rpc_printer_migrate,
7461 NET_TRANSPORT_RPC,
7462 "Migrate printer to local server",
7463 "net rpc printer migrate\n"
7464 " Migrate printer to local server"
7467 "driver",
7468 rpc_printer_driver_list,
7469 NET_TRANSPORT_RPC,
7470 "List printer drivers",
7471 "net rpc printer driver\n"
7472 " List printer drivers"
7475 "publish",
7476 rpc_printer_publish,
7477 NET_TRANSPORT_RPC,
7478 "Publish printer in AD",
7479 "net rpc printer publish\n"
7480 " Publish printer in AD"
7482 {NULL, NULL, 0, NULL, NULL}
7485 if (argc == 0) {
7486 if (c->display_usage) {
7487 d_printf("Usage:\n");
7488 d_printf("net rpc printer\n"
7489 " List printers\n");
7490 net_display_usage_from_functable(func);
7491 return 0;
7493 return run_rpc_command(c, NULL, &syntax_spoolss, 0,
7494 rpc_printer_list_internals,
7495 argc, argv);
7498 return net_run_function(c, argc, argv, "net rpc printer", func);
7502 * 'net rpc' entrypoint.
7504 * @param c A net_context structure.
7505 * @param argc Standard main() style argc.
7506 * @param argv Standard main() style argv. Initial components are already
7507 * stripped.
7510 int net_rpc(struct net_context *c, int argc, const char **argv)
7512 struct functable func[] = {
7514 "audit",
7515 net_rpc_audit,
7516 NET_TRANSPORT_RPC,
7517 "Modify global audit settings",
7518 "net rpc audit\n"
7519 " Modify global audit settings"
7522 "info",
7523 net_rpc_info,
7524 NET_TRANSPORT_RPC,
7525 "Show basic info about a domain",
7526 "net rpc info\n"
7527 " Show basic info about a domain"
7530 "join",
7531 net_rpc_join,
7532 NET_TRANSPORT_RPC,
7533 "Join a domain",
7534 "net rpc join\n"
7535 " Join a domain"
7538 "oldjoin",
7539 net_rpc_oldjoin,
7540 NET_TRANSPORT_RPC,
7541 "Join a domain created in server manager",
7542 "net rpc oldjoin\n"
7543 " Join a domain created in server manager"
7546 "testjoin",
7547 net_rpc_testjoin,
7548 NET_TRANSPORT_RPC,
7549 "Test that a join is valid",
7550 "net rpc testjoin\n"
7551 " Test that a join is valid"
7554 "user",
7555 net_rpc_user,
7556 NET_TRANSPORT_RPC,
7557 "List/modify users",
7558 "net rpc user\n"
7559 " List/modify users"
7562 "password",
7563 rpc_user_password,
7564 NET_TRANSPORT_RPC,
7565 "Change a user password",
7566 "net rpc password\n"
7567 " Change a user password\n"
7568 " Alias for net rpc user password"
7571 "group",
7572 net_rpc_group,
7573 NET_TRANSPORT_RPC,
7574 "List/modify groups",
7575 "net rpc group\n"
7576 " List/modify groups"
7579 "share",
7580 net_rpc_share,
7581 NET_TRANSPORT_RPC,
7582 "List/modify shares",
7583 "net rpc share\n"
7584 " List/modify shares"
7587 "file",
7588 net_rpc_file,
7589 NET_TRANSPORT_RPC,
7590 "List open files",
7591 "net rpc file\n"
7592 " List open files"
7595 "printer",
7596 net_rpc_printer,
7597 NET_TRANSPORT_RPC,
7598 "List/modify printers",
7599 "net rpc printer\n"
7600 " List/modify printers"
7603 "changetrustpw",
7604 net_rpc_changetrustpw,
7605 NET_TRANSPORT_RPC,
7606 "Change trust account password",
7607 "net rpc changetrustpw\n"
7608 " Change trust account password"
7611 "trustdom",
7612 rpc_trustdom,
7613 NET_TRANSPORT_RPC,
7614 "Modify domain trusts",
7615 "net rpc trustdom\n"
7616 " Modify domain trusts"
7619 "abortshutdown",
7620 rpc_shutdown_abort,
7621 NET_TRANSPORT_RPC,
7622 "Abort a remote shutdown",
7623 "net rpc abortshutdown\n"
7624 " Abort a remote shutdown"
7627 "shutdown",
7628 rpc_shutdown,
7629 NET_TRANSPORT_RPC,
7630 "Shutdown a remote server",
7631 "net rpc shutdown\n"
7632 " Shutdown a remote server"
7635 "samdump",
7636 rpc_samdump,
7637 NET_TRANSPORT_RPC,
7638 "Dump SAM data of remote NT PDC",
7639 "net rpc samdump\n"
7640 " Dump SAM data of remote NT PDC"
7643 "vampire",
7644 rpc_vampire,
7645 NET_TRANSPORT_RPC,
7646 "Sync a remote NT PDC's data into local passdb",
7647 "net rpc vampire\n"
7648 " Sync a remote NT PDC's data into local passdb"
7651 "getsid",
7652 net_rpc_getsid,
7653 NET_TRANSPORT_RPC,
7654 "Fetch the domain sid into local secrets.tdb",
7655 "net rpc getsid\n"
7656 " Fetch the domain sid into local secrets.tdb"
7659 "rights",
7660 net_rpc_rights,
7661 NET_TRANSPORT_RPC,
7662 "Manage privileges assigned to SID",
7663 "net rpc rights\n"
7664 " Manage privileges assigned to SID"
7667 "service",
7668 net_rpc_service,
7669 NET_TRANSPORT_RPC,
7670 "Start/stop/query remote services",
7671 "net rpc service\n"
7672 " Start/stop/query remote services"
7675 "registry",
7676 net_rpc_registry,
7677 NET_TRANSPORT_RPC,
7678 "Manage registry hives",
7679 "net rpc registry\n"
7680 " Manage registry hives"
7683 "shell",
7684 net_rpc_shell,
7685 NET_TRANSPORT_RPC,
7686 "Open interactive shell on remote server",
7687 "net rpc shell\n"
7688 " Open interactive shell on remote server"
7690 {NULL, NULL, 0, NULL, NULL}
7692 return net_run_function(c, argc, argv, "net rpc", func);